blob: 072d53d93344a87144b9b30d13826eca13a06a39 [file] [log] [blame]
Gilles Peskine0156a152021-01-26 21:23:56 +01001"""Knowledge about cryptographic mechanisms implemented in Mbed TLS.
2
3This module is entirely based on the PSA API.
4"""
5
6# Copyright The Mbed TLS Contributors
7# SPDX-License-Identifier: Apache-2.0
8#
9# Licensed under the Apache License, Version 2.0 (the "License"); you may
10# not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20
Gilles Peskineee7554e2021-04-29 20:38:01 +020021import enum
Gilles Peskine0156a152021-01-26 21:23:56 +010022import re
gabor-mezei-armf73f8962021-06-29 11:17:54 +020023from typing import Dict, Iterable, Optional, Pattern, Tuple
Gilles Peskine0156a152021-01-26 21:23:56 +010024
Gilles Peskine6f6483f2021-01-27 12:43:24 +010025from mbedtls_dev.asymmetric_key_data import ASYMMETRIC_KEY_DATA
26
Gilles Peskineee7554e2021-04-29 20:38:01 +020027
28BLOCK_MAC_MODES = frozenset(['CBC_MAC', 'CMAC'])
29BLOCK_CIPHER_MODES = frozenset([
30 'CTR', 'CFB', 'OFB', 'XTS', 'CCM_STAR_NO_TAG',
31 'ECB_NO_PADDING', 'CBC_NO_PADDING', 'CBC_PKCS7',
32])
33BLOCK_AEAD_MODES = frozenset(['CCM', 'GCM'])
34
35
Gilles Peskine0156a152021-01-26 21:23:56 +010036class KeyType:
37 """Knowledge about a PSA key type."""
38
Gilles Peskineb9dbb7f2021-04-29 20:19:57 +020039 def __init__(self, name: str, params: Optional[Iterable[str]] = None) -> None:
Gilles Peskine0156a152021-01-26 21:23:56 +010040 """Analyze a key type.
41
42 The key type must be specified in PSA syntax. In its simplest form,
Gilles Peskinefa3c69a2021-02-16 14:29:22 +010043 `name` is a string 'PSA_KEY_TYPE_xxx' which is the name of a PSA key
Gilles Peskine0156a152021-01-26 21:23:56 +010044 type macro. For key types that take arguments, the arguments can
45 be passed either through the optional argument `params` or by
Gilles Peskine4d0b0892021-04-12 13:41:52 +020046 passing an expression of the form 'PSA_KEY_TYPE_xxx(param1, ...)'
Gilles Peskinefa3c69a2021-02-16 14:29:22 +010047 in `name` as a string.
Gilles Peskine0156a152021-01-26 21:23:56 +010048 """
Gilles Peskined75adfc2021-02-17 18:04:28 +010049
Gilles Peskine0156a152021-01-26 21:23:56 +010050 self.name = name.strip()
Gilles Peskinefa3c69a2021-02-16 14:29:22 +010051 """The key type macro name (``PSA_KEY_TYPE_xxx``).
52
53 For key types constructed from a macro with arguments, this is the
54 name of the macro, and the arguments are in `self.params`.
55 """
Gilles Peskine0156a152021-01-26 21:23:56 +010056 if params is None:
57 if '(' in self.name:
58 m = re.match(r'(\w+)\s*\((.*)\)\Z', self.name)
59 assert m is not None
60 self.name = m.group(1)
Gilles Peskine4d0b0892021-04-12 13:41:52 +020061 params = m.group(2).split(',')
Gilles Peskinefa3c69a2021-02-16 14:29:22 +010062 self.params = (None if params is None else
63 [param.strip() for param in params])
64 """The parameters of the key type, if there are any.
65
66 None if the key type is a macro without arguments.
67 """
Gilles Peskined75adfc2021-02-17 18:04:28 +010068 assert re.match(r'PSA_KEY_TYPE_\w+\Z', self.name)
69
Gilles Peskine0156a152021-01-26 21:23:56 +010070 self.expression = self.name
Gilles Peskinefa3c69a2021-02-16 14:29:22 +010071 """A C expression whose value is the key type encoding."""
Gilles Peskine0156a152021-01-26 21:23:56 +010072 if self.params is not None:
73 self.expression += '(' + ', '.join(self.params) + ')'
Gilles Peskined75adfc2021-02-17 18:04:28 +010074
Gilles Peskine0156a152021-01-26 21:23:56 +010075 self.private_type = re.sub(r'_PUBLIC_KEY\Z', r'_KEY_PAIR', self.name)
Gilles Peskinefa3c69a2021-02-16 14:29:22 +010076 """The key type macro name for the corresponding key pair type.
77
78 For everything other than a public key type, this is the same as
79 `self.name`.
80 """
Gilles Peskinedf639682021-01-26 21:25:34 +010081
82 ECC_KEY_SIZES = {
83 'PSA_ECC_FAMILY_SECP_K1': (192, 224, 256),
Gilles Peskine0ac258e2021-01-27 13:11:59 +010084 'PSA_ECC_FAMILY_SECP_R1': (225, 256, 384, 521),
Gilles Peskinedf639682021-01-26 21:25:34 +010085 'PSA_ECC_FAMILY_SECP_R2': (160,),
86 'PSA_ECC_FAMILY_SECT_K1': (163, 233, 239, 283, 409, 571),
87 'PSA_ECC_FAMILY_SECT_R1': (163, 233, 283, 409, 571),
88 'PSA_ECC_FAMILY_SECT_R2': (163,),
89 'PSA_ECC_FAMILY_BRAINPOOL_P_R1': (160, 192, 224, 256, 320, 384, 512),
90 'PSA_ECC_FAMILY_MONTGOMERY': (255, 448),
Gilles Peskinea00abc62021-03-16 18:25:14 +010091 'PSA_ECC_FAMILY_TWISTED_EDWARDS': (255, 448),
Gilles Peskinedf639682021-01-26 21:25:34 +010092 }
93 KEY_TYPE_SIZES = {
94 'PSA_KEY_TYPE_AES': (128, 192, 256), # exhaustive
Gilles Peskinedf639682021-01-26 21:25:34 +010095 'PSA_KEY_TYPE_ARIA': (128, 192, 256), # exhaustive
96 'PSA_KEY_TYPE_CAMELLIA': (128, 192, 256), # exhaustive
97 'PSA_KEY_TYPE_CHACHA20': (256,), # exhaustive
98 'PSA_KEY_TYPE_DERIVE': (120, 128), # sample
99 'PSA_KEY_TYPE_DES': (64, 128, 192), # exhaustive
100 'PSA_KEY_TYPE_HMAC': (128, 160, 224, 256, 384, 512), # standard size for each supported hash
Manuel Pégourié-Gonnardb12de9f2021-05-03 11:02:56 +0200101 'PSA_KEY_TYPE_PASSWORD': (48, 168, 336), # sample
102 'PSA_KEY_TYPE_PASSWORD_HASH': (128, 256), # sample
103 'PSA_KEY_TYPE_PEPPER': (128, 256), # sample
Gilles Peskinedf639682021-01-26 21:25:34 +0100104 'PSA_KEY_TYPE_RAW_DATA': (8, 40, 128), # sample
105 'PSA_KEY_TYPE_RSA_KEY_PAIR': (1024, 1536), # small sample
106 }
107 def sizes_to_test(self) -> Tuple[int, ...]:
108 """Return a tuple of key sizes to test.
109
110 For key types that only allow a single size, or only a small set of
111 sizes, these are all the possible sizes. For key types that allow a
112 wide range of sizes, these are a representative sample of sizes,
113 excluding large sizes for which a typical resource-constrained platform
114 may run out of memory.
115 """
116 if self.private_type == 'PSA_KEY_TYPE_ECC_KEY_PAIR':
117 assert self.params is not None
118 return self.ECC_KEY_SIZES[self.params[0]]
119 return self.KEY_TYPE_SIZES[self.private_type]
Gilles Peskine397b0282021-01-26 21:26:26 +0100120
121 # "48657265006973206b6579a064617461"
122 DATA_BLOCK = b'Here\000is key\240data'
123 def key_material(self, bits: int) -> bytes:
124 """Return a byte string containing suitable key material with the given bit length.
125
126 Use the PSA export representation. The resulting byte string is one that
127 can be obtained with the following code:
128 ```
129 psa_set_key_type(&attributes, `self.expression`);
130 psa_set_key_bits(&attributes, `bits`);
131 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
132 psa_generate_key(&attributes, &id);
133 psa_export_key(id, `material`, ...);
134 ```
135 """
Gilles Peskine6f6483f2021-01-27 12:43:24 +0100136 if self.expression in ASYMMETRIC_KEY_DATA:
137 if bits not in ASYMMETRIC_KEY_DATA[self.expression]:
138 raise ValueError('No key data for {}-bit {}'
139 .format(bits, self.expression))
140 return ASYMMETRIC_KEY_DATA[self.expression][bits]
Gilles Peskine397b0282021-01-26 21:26:26 +0100141 if bits % 8 != 0:
Gilles Peskine6f6483f2021-01-27 12:43:24 +0100142 raise ValueError('Non-integer number of bytes: {} bits for {}'
143 .format(bits, self.expression))
Gilles Peskine397b0282021-01-26 21:26:26 +0100144 length = bits // 8
145 if self.name == 'PSA_KEY_TYPE_DES':
146 # "644573206b457901644573206b457902644573206b457904"
147 des3 = b'dEs kEy\001dEs kEy\002dEs kEy\004'
148 return des3[:length]
Gilles Peskine397b0282021-01-26 21:26:26 +0100149 return b''.join([self.DATA_BLOCK] * (length // len(self.DATA_BLOCK)) +
150 [self.DATA_BLOCK[:length % len(self.DATA_BLOCK)]])
gabor-mezei-arm2784bfe2021-06-28 20:02:11 +0200151
152 KEY_TYPE_FOR_SIGNATURE = {
gabor-mezei-armf73f8962021-06-29 11:17:54 +0200153 'PSA_KEY_USAGE_SIGN_HASH': re.compile('.*KEY_PAIR'),
154 'PSA_KEY_USAGE_VERIFY_HASH': re.compile('.*KEY.*')
155 } #type: Dict[str, Pattern]
gabor-mezei-arm2784bfe2021-06-28 20:02:11 +0200156 """Use a regexp to determine key types for which signature is possible
157 when using the actual usage flag.
158 """
159 def is_valid_for_signature(self, usage: str) -> bool:
160 """Determine if the key type is compatible with the specified
161 signitute type.
162
163 """
164 # This is just temporaly solution for the implicit usage flags.
165 return re.match(self.KEY_TYPE_FOR_SIGNATURE[usage], self.name) is not None
Gilles Peskineee7554e2021-04-29 20:38:01 +0200166
167
168class AlgorithmCategory(enum.Enum):
169 """PSA algorithm categories."""
170 # The numbers are aligned with the category bits in numerical values of
171 # algorithms.
172 HASH = 2
173 MAC = 3
174 CIPHER = 4
175 AEAD = 5
176 SIGN = 6
177 ASYMMETRIC_ENCRYPTION = 7
178 KEY_DERIVATION = 8
179 KEY_AGREEMENT = 9
180 PAKE = 10
181
182 def requires_key(self) -> bool:
183 return self not in {self.HASH, self.KEY_DERIVATION}
184
185
186class AlgorithmNotRecognized(Exception):
187 def __init__(self, expr: str) -> None:
188 super().__init__('Algorithm not recognized: ' + expr)
189 self.expr = expr
190
191
192class Algorithm:
193 """Knowledge about a PSA algorithm."""
194
195 @staticmethod
196 def determine_base(expr: str) -> str:
197 """Return an expression for the "base" of the algorithm.
198
199 This strips off variants of algorithms such as MAC truncation.
200
201 This function does not attempt to detect invalid inputs.
202 """
203 m = re.match(r'PSA_ALG_(?:'
204 r'(?:TRUNCATED|AT_LEAST_THIS_LENGTH)_MAC|'
205 r'AEAD_WITH_(?:SHORTENED|AT_LEAST_THIS_LENGTH)_TAG'
206 r')\((.*),[^,]+\)\Z', expr)
207 if m:
208 expr = m.group(1)
209 return expr
210
211 @staticmethod
212 def determine_head(expr: str) -> str:
213 """Return the head of an algorithm expression.
214
215 The head is the first (outermost) constructor, without its PSA_ALG_
216 prefix, and with some normalization of similar algorithms.
217 """
218 m = re.match(r'PSA_ALG_(?:DETERMINISTIC_)?(\w+)', expr)
219 if not m:
220 raise AlgorithmNotRecognized(expr)
221 head = m.group(1)
222 if head == 'KEY_AGREEMENT':
223 m = re.match(r'PSA_ALG_KEY_AGREEMENT\s*\(\s*PSA_ALG_(\w+)', expr)
224 if not m:
225 raise AlgorithmNotRecognized(expr)
226 head = m.group(1)
227 head = re.sub(r'_ANY\Z', r'', head)
228 if re.match(r'ED[0-9]+PH\Z', head):
229 head = 'EDDSA_PREHASH'
230 return head
231
232 CATEGORY_FROM_HEAD = {
233 'SHA': AlgorithmCategory.HASH,
234 'SHAKE256_512': AlgorithmCategory.HASH,
235 'MD': AlgorithmCategory.HASH,
236 'RIPEMD': AlgorithmCategory.HASH,
237 'ANY_HASH': AlgorithmCategory.HASH,
238 'HMAC': AlgorithmCategory.MAC,
239 'STREAM_CIPHER': AlgorithmCategory.CIPHER,
240 'CHACHA20_POLY1305': AlgorithmCategory.AEAD,
241 'DSA': AlgorithmCategory.SIGN,
242 'ECDSA': AlgorithmCategory.SIGN,
243 'EDDSA': AlgorithmCategory.SIGN,
244 'PURE_EDDSA': AlgorithmCategory.SIGN,
245 'RSA_PSS': AlgorithmCategory.SIGN,
246 'RSA_PKCS1V15_SIGN': AlgorithmCategory.SIGN,
247 'RSA_PKCS1V15_CRYPT': AlgorithmCategory.ASYMMETRIC_ENCRYPTION,
248 'RSA_OAEP': AlgorithmCategory.ASYMMETRIC_ENCRYPTION,
249 'HKDF': AlgorithmCategory.KEY_DERIVATION,
250 'TLS12_PRF': AlgorithmCategory.KEY_DERIVATION,
251 'TLS12_PSK_TO_MS': AlgorithmCategory.KEY_DERIVATION,
252 'PBKDF': AlgorithmCategory.KEY_DERIVATION,
253 'ECDH': AlgorithmCategory.KEY_AGREEMENT,
254 'FFDH': AlgorithmCategory.KEY_AGREEMENT,
255 # KEY_AGREEMENT(...) is a key derivation with a key agreement component
256 'KEY_AGREEMENT': AlgorithmCategory.KEY_DERIVATION,
257 'JPAKE': AlgorithmCategory.PAKE,
258 }
259 for x in BLOCK_MAC_MODES:
260 CATEGORY_FROM_HEAD[x] = AlgorithmCategory.MAC
261 for x in BLOCK_CIPHER_MODES:
262 CATEGORY_FROM_HEAD[x] = AlgorithmCategory.CIPHER
263 for x in BLOCK_AEAD_MODES:
264 CATEGORY_FROM_HEAD[x] = AlgorithmCategory.AEAD
265
266 def determine_category(self, expr: str, head: str) -> AlgorithmCategory:
267 """Return the category of the given algorithm expression.
268
269 This function does not attempt to detect invalid inputs.
270 """
271 prefix = head
272 while prefix:
273 if prefix in self.CATEGORY_FROM_HEAD:
274 return self.CATEGORY_FROM_HEAD[prefix]
275 if re.match(r'.*[0-9]\Z', prefix):
276 prefix = re.sub(r'_*[0-9]+\Z', r'', prefix)
277 else:
278 prefix = re.sub(r'_*[^_]*\Z', r'', prefix)
279 raise AlgorithmNotRecognized(expr)
280
281 @staticmethod
282 def determine_wildcard(expr) -> bool:
283 """Whether the given algorithm expression is a wildcard.
284
285 This function does not attempt to detect invalid inputs.
286 """
287 if re.search(r'\bPSA_ALG_ANY_HASH\b', expr):
288 return True
289 if re.search(r'_AT_LEAST_', expr):
290 return True
291 return False
292
293 def __init__(self, expr: str) -> None:
294 """Analyze an algorithm value.
295
296 The algorithm must be expressed as a C expression containing only
297 calls to PSA algorithm constructor macros and numeric literals.
298
299 This class is only programmed to handle valid expressions. Invalid
300 expressions may result in exceptions or in nonsensical results.
301 """
302 self.expression = re.sub(r'\s+', r'', expr)
303 self.base_expression = self.determine_base(self.expression)
304 self.head = self.determine_head(self.base_expression)
305 self.category = self.determine_category(self.base_expression, self.head)
306 self.is_wildcard = self.determine_wildcard(self.expression)