Lines Matching refs:self
71 def __init__(self, name: str, params: Optional[Iterable[str]] = None) -> None:
82 self.name = name.strip()
86 name of the macro, and the arguments are in `self.params`.
89 if '(' in self.name:
90 m = re.match(r'(\w+)\s*\((.*)\)\Z', self.name)
92 self.name = m.group(1)
94 self.params = (None if params is None else
100 assert re.match(r'PSA_KEY_TYPE_\w+\Z', self.name)
102 self.expression = self.name
104 if self.params is not None:
105 self.expression += '(' + ', '.join(self.params) + ')'
107 m = re.match(r'PSA_KEY_TYPE_(\w+)', self.name)
109 self.head = re.sub(r'_(?:PUBLIC_KEY|KEY_PAIR)\Z', r'', m.group(1))
112 self.private_type = re.sub(r'_PUBLIC_KEY\Z', r'_KEY_PAIR', self.name)
116 `self.name`.
119 def short_expression(self, level: int = 0) -> str:
124 return short_expression(self.expression, level=level)
126 def is_public(self) -> bool:
128 return self.name.endswith('_PUBLIC_KEY')
158 def sizes_to_test(self) -> Tuple[int, ...]:
167 if self.private_type == 'PSA_KEY_TYPE_ECC_KEY_PAIR':
168 assert self.params is not None
169 return self.ECC_KEY_SIZES[self.params[0]]
170 if self.private_type == 'PSA_KEY_TYPE_DH_KEY_PAIR':
171 assert self.params is not None
172 return self.DH_KEY_SIZES[self.params[0]]
173 return self.KEY_TYPE_SIZES[self.private_type]
177 def key_material(self, bits: int) -> bytes:
183 psa_set_key_type(&attributes, `self.expression`);
190 if self.expression in ASYMMETRIC_KEY_DATA:
191 if bits not in ASYMMETRIC_KEY_DATA[self.expression]:
193 .format(bits, self.expression))
194 return ASYMMETRIC_KEY_DATA[self.expression][bits]
197 .format(bits, self.expression))
199 if self.name == 'PSA_KEY_TYPE_DES':
203 return b''.join([self.DATA_BLOCK] * (length // len(self.DATA_BLOCK)) +
204 [self.DATA_BLOCK[:length % len(self.DATA_BLOCK)]])
206 def can_do(self, alg: 'Algorithm') -> bool:
214 if self.head == 'HMAC' and alg.head == 'HMAC':
216 if self.head == 'DES':
222 if self.head in BLOCK_CIPHERS and \
227 self.head in ['ARIA', 'CAMELLIA']:
230 if self.head == 'CHACHA20' and alg.head == 'CHACHA20_POLY1305':
232 if self.head in {'ARC4', 'CHACHA20'} and \
235 if self.head == 'RSA' and alg.head.startswith('RSA_'):
238 self.is_public():
246 if self.head == 'ECC':
247 assert self.params is not None
248 eccc = EllipticCurveCategory.from_family(self.params[0])
259 if self.head == 'DH' and alg.head == 'FFDH':
278 def requires_key(self) -> bool:
280 return self not in {self.HASH, self.KEY_DERIVATION}
282 def is_asymmetric(self) -> bool:
284 return self in {
285 self.SIGN,
286 self.ASYMMETRIC_ENCRYPTION,
287 self.KEY_AGREEMENT
292 def __init__(self, expr: str) -> None:
294 self.expr = expr
372 def determine_category(self, expr: str, head: str) -> AlgorithmCategory:
379 if prefix in self.CATEGORY_FROM_HEAD:
380 return self.CATEGORY_FROM_HEAD[prefix]
399 def __init__(self, expr: str) -> None:
408 self.expression = re.sub(r'\s+', r'', expr)
409 self.base_expression = self.determine_base(self.expression)
410 self.head = self.determine_head(self.base_expression)
411 self.category = self.determine_category(self.base_expression, self.head)
412 self.is_wildcard = self.determine_wildcard(self.expression)
414 def get_key_agreement_derivation(self) -> Optional[str]:
419 if self.category != AlgorithmCategory.KEY_AGREEMENT:
421 m = re.match(r'PSA_ALG_KEY_AGREEMENT\(\w+,\s*(.*)\)\Z', self.expression)
433 def is_valid_key_agreement_with_derivation(self) -> bool:
435 kdf_alg = self.get_key_agreement_derivation()
438 return kdf_alg not in self.KEY_DERIVATIONS_INCOMPATIBLE_WITH_AGREEMENT
440 def is_invalid_key_agreement_with_derivation(self) -> bool:
442 kdf_alg = self.get_key_agreement_derivation()
445 return kdf_alg in self.KEY_DERIVATIONS_INCOMPATIBLE_WITH_AGREEMENT
447 def short_expression(self, level: int = 0) -> str:
452 return short_expression(self.expression, level=level)
503 def is_invalid_truncation(self) -> bool:
510 m = self.TRUNCATED_ALG_RE.match(self.expression)
514 permitted_lengths = self.permitted_truncations(base)
519 def is_valid_for_operation(self) -> bool:
526 if self.is_wildcard:
528 if self.is_invalid_truncation():
532 def can_do(self, category: AlgorithmCategory) -> bool:
535 if category == self.category:
538 self.is_valid_key_agreement_with_derivation():
542 def usage_flags(self, public: bool = False) -> List[str]:
547 if self.category == AlgorithmCategory.HASH:
549 elif self.category == AlgorithmCategory.MAC:
552 elif self.category == AlgorithmCategory.CIPHER or \
553 self.category == AlgorithmCategory.AEAD:
555 elif self.category == AlgorithmCategory.SIGN:
559 elif self.category == AlgorithmCategory.ASYMMETRIC_ENCRYPTION:
563 elif self.category == AlgorithmCategory.KEY_DERIVATION or \
564 self.category == AlgorithmCategory.KEY_AGREEMENT:
567 raise AlgorithmNotRecognized(self.expression)