Software APIs
datatypes.h
Go to the documentation of this file.
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_CRYPTO_INCLUDE_DATATYPES_H_
6 #define OPENTITAN_SW_DEVICE_LIB_CRYPTO_INCLUDE_DATATYPES_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #ifdef OTCRYPTO_IN_REPO
13 #include "sw/device/lib/base/status.h"
14 #else
15 #include "freestanding/absl_status.h"
16 #include "freestanding/defs.h"
17 #include "freestanding/hardened.h"
18 #endif
19 
20 /**
21  * @file
22  * @brief Shared datatypes for the OpenTitan cryptography library.
23  *
24  * This header defines status codes, byte buffer representations, and key
25  * representations that are shared between different algorithms within the
26  * library.
27  */
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif // __cplusplus
32 
33 /**
34  * Return values for the crypto library.
35  *
36  * The crypto library's return value is defined as OpenTitan's internal
37  * `status_t` in order to simplify testing. However, informally the library
38  * guarantees that the concrete value contained in the status will be one of
39  * the members of the `otcrypto_status_value` enum below.
40  */
42 
43 /**
44  * Possible status values for the cryptolib.
45  *
46  * As long as the OTCRYPTO_STATUS_DEBUG define is unset, all `otcrypto_status_t`
47  * codes returned by the cryptolib should be bit-by-bit equivalent with one of
48  * the values in this enum.
49  *
50  * Values are built to be bit-compatible with OpenTitan's internal `status_t`
51  * datatypes. The highest (sign) bit indicates if the value is an error (1) or
52  * not (0). For non-error statuses, the rest can be anything; in cryptolib
53  * status codes it is always `kHardenedBoolTrue`. For errors:
54  * - The next 15 bits are a module identifier, which is always 0 in the
55  * cryptolib status codes
56  * - The next 11 bits are a line number or other information; in the
57  * cryptolib status codes, it is a hardened value created to have high
58  * Hamming distance with the other valid status codes
59  * - The final 5 bits are an Abseil-compatible error code
60  *
61  * The hardened values for error codes were generated with:
62  * $ ./util/design/sparse-fsm-encode.py -d 5 -m 5 -n 11 \
63  * -s 4232058530 --language=sv --avoid-zero
64  *
65  * Use the same seed value and a larger `-m` argument to generate new values
66  * without changing all error codes. Remove the seed (-s argument) to generate
67  * completely new 11-bit values.
68  */
69 typedef enum otcrypto_status_value {
70  // Status is OK; no errors.
71  kOtcryptoStatusValueOk = (int32_t)0x739,
72  // Invalid input arguments; wrong length or invalid type.
73  kOtcryptoStatusValueBadArgs = (int32_t)0x8000fea0 | kInvalidArgument,
74  // Error after which it is OK to retry (e.g. timeout).
75  kOtcryptoStatusValueInternalError = (int32_t)0x80005340 | kAborted,
76  // Error after which it is not OK to retry (e.g. integrity check).
77  kOtcryptoStatusValueFatalError = (int32_t)0x80006d80 | kFailedPrecondition,
78  // An asynchronous operation is still in progress.
79  kOtcryptoStatusValueAsyncIncomplete = (int32_t)0x8000ea40 | kUnavailable,
80  // TODO: remove all instances of this error before release; it is to track
81  // implementations that are not yet complete.
82  kOtcryptoStatusValueNotImplemented = (int32_t)0x80008d20 | kUnimplemented,
84 
85 /**
86  * Struct to hold a fixed-length byte array.
87  *
88  * Note: the caller must (1) allocate sufficient space and (2) set the `len`
89  * field and `data` pointer when `otcrypto_byte_buf_t` is used for output. The
90  * crypto library will throw an error if `len` doesn't match expectations.
91  */
92 typedef struct otcrypto_byte_buf {
93  // Pointer to the data.
94  uint8_t *data;
95  // Length of the data in bytes.
96  size_t len;
98 
99 /**
100  * Struct to hold a constant fixed-length byte array.
101  *
102  * The const annotations prevent any changes to the byte buffer. It is
103  * necessary to have this structure separate from `otcrypto_byte_buf_t` because
104  * data pointed to by a struct does not inherit `const`, so `const
105  * otcrypto_byte_buf_t` would still allow data to change.
106  */
107 typedef struct otcrypto_const_byte_buf {
108  // Pointer to the data.
109  const uint8_t *const data;
110  // Length of the data in bytes.
111  const size_t len;
113 
114 /**
115  * Struct to hold a fixed-length word array.
116  *
117  * Note: the caller must (1) allocate sufficient space and (2) set the `len`
118  * field and `data` pointer when `otcrypto_word32_buf_t` is used for output. The
119  * crypto library will throw an error if `len` doesn't match expectations.
120  */
121 typedef struct otcrypto_word32_buf {
122  // Pointer to the data.
123  uint32_t *data;
124  // Length of the data in words.
125  size_t len;
127 
128 /**
129  * Struct to hold a constant fixed-length word array.
130  *
131  * The const annotations prevent any changes to the word buffer. It is
132  * necessary to have this structure separate from `otcrypto_word32_buf_t`
133  * because data pointed to by a struct does not inherit `const`, so `const
134  * otcrypto_word32_buf_t` would still allow data to change.
135  */
137  // Pointer to the data.
138  const uint32_t *const data;
139  // Length of the data in words.
140  const size_t len;
142 
143 /**
144  * Enum to denote the key type of the handled key.
145  *
146  * Values are hardened.
147  */
148 typedef enum otcrypto_key_type {
149  // Key type AES.
150  kOtcryptoKeyTypeAes = 0x8e9,
151  // Key type HMAC.
152  kOtcryptoKeyTypeHmac = 0xe3f,
153  // Key type KMAC.
154  kOtcryptoKeyTypeKmac = 0xb74,
155  // Key type RSA.
156  kOtcryptoKeyTypeRsa = 0x7ee,
157  // Key type ECC.
158  kOtcryptoKeyTypeEcc = 0x15b,
159  // Key type KDF.
160  kOtcryptoKeyTypeKdf = 0xb87,
162 
163 /**
164  * Enum to specify the AES modes that use a key.
165  *
166  * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
167  * for which the provided key is intended for.
168  *
169  * Values are hardened.
170  */
171 typedef enum otcrypto_aes_key_mode {
172  // Mode AES ECB.
173  kOtcryptoAesKeyModeEcb = 0x1b6,
174  // Mode AES CBC.
175  kOtcryptoAesKeyModeCbc = 0xf3a,
176  // Mode AES CFB.
177  kOtcryptoAesKeyModeCfb = 0x0f9,
178  // Mode AES OFB.
179  kOtcryptoAesKeyModeOfb = 0xb49,
180  // Mode AES CTR.
181  kOtcryptoAesKeyModeCtr = 0x4ce,
182  // Mode AES GCM.
183  kOtcryptoAesKeyModeGcm = 0xaa5,
184  // Mode AES KWP.
185  kOtcryptoAesKeyModeKwp = 0x7d5,
187 
188 /**
189  * Enum to specify the HMAC modes that use a key.
190  *
191  * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
192  * for which the provided key is intended for.
193  *
194  * Values are hardened.
195  */
197  // Mode HMAC SHA256.
198  kOtcryptoHmacKeyModeSha256 = 0x7fd,
199  // Mode HMAC SHA384.
200  kOtcryptoHmacKeyModeSha384 = 0x43b,
201  // Mode HMAC SHA512.
202  kOtcryptoHmacKeyModeSha512 = 0x7a2,
204 
205 /**
206  * Enum to specify the KMAC modes that use a key.
207  *
208  * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
209  * for which the provided key is intended for.
210  *
211  * Values are hardened.
212  */
214  // Mode KMAC128.
215  kOtcryptoKmacKeyModeKmac128 = 0xa56,
216  // Mode KMAC256.
217  kOtcryptoKmacKeyModeKmac256 = 0x663,
219 
220 /**
221  * Enum to specify the RSA modes that use a key.
222  *
223  * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
224  * for which the provided key is intended for.
225  *
226  * Values are hardened.
227  */
228 typedef enum otcrypto_rsa_key_mode {
229  // Mode RSA Sign, RSASSA-PKCS.
230  kOtcryptoRsaKeyModeSignPkcs = 0x3d4,
231  // Mode RSA Sign, RSASSA-PSS.
232  kOtcryptoRsaKeyModeSignPss = 0x761,
233  // Mode RSA Encrypt, RSAES-OAEP.
234  kOtcryptoRsaKeyModeEncryptOaep = 0x585,
236 
237 /**
238  * Enum to specify the ECC modes that use a key.
239  *
240  * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
241  * for which the provided key is intended for.
242  *
243  * Values are hardened.
244  */
245 typedef enum otcrypto_ecc_key_mode {
246  // Mode ECDSA.
247  kOtcryptoEccKeyModeEcdsa = 0x4e5,
248  // Mode ECDH.
249  kOtcryptoEccKeyModeEcdh = 0x6bb,
250  // Mode Ed25519.
251  kOtcryptoEccKeyModeEd25519 = 0xd32,
252  // Mode X25519.
253  kOtcryptoEccKeyModeX25519 = 0x276,
255 
256 /**
257  * Enum to specify the KDF modes that use a key.
258  *
259  * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
260  * for which the provided key is intended for.
261  *
262  * Values are hardened.
263  */
264 typedef enum otcrypto_kdf_key_mode {
265  // Mode KDF-CTR with HMAC as PRF.
266  kOtcryptoKdfKeyModeCtrHmac = 0x12f,
267  // Mode KDF-KMAC with KMAC128 as PRF.
268  kOtcryptoKdfKeyModeKmac128 = 0xe5e,
269  // Mode KDF-KMAC with KMAC256 as PRF.
270  kOtcryptoKdfKeyModeKmac256 = 0x353,
272 
273 /**
274  * Enum for opentitan crypto modes that use a key.
275  *
276  * Denotes the crypto mode for which the provided key is to be used.
277  * This `otcrypto_key_mode_t` will be a parameter in the
278  * `otcrypto_blinded_key_t` and `otcrypto_unblinded_key_t` structs.
279  *
280  * Values are hardened.
281  */
282 typedef enum otcrypto_key_mode {
283  // Key is intended for AES ECB mode.
284  kOtcryptoKeyModeAesEcb = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeEcb,
285  // Key is intended for AES CBC mode.
286  kOtcryptoKeyModeAesCbc = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeCbc,
287  // Key is intended for AES CFB mode.
288  kOtcryptoKeyModeAesCfb = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeCfb,
289  // Key is intended for AES OFB mode.
290  kOtcryptoKeyModeAesOfb = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeOfb,
291  // Key is intended for AES CTR mode.
292  kOtcryptoKeyModeAesCtr = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeCtr,
293  // Key is intended for AES GCM mode.
294  kOtcryptoKeyModeAesGcm = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeGcm,
295  // Key is intended for AES KWP mode.
296  kOtcryptoKeyModeAesKwp = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeKwp,
297  // Key is intended for HMAC SHA256 mode.
298  kOtcryptoKeyModeHmacSha256 =
299  kOtcryptoKeyTypeHmac << 16 | kOtcryptoHmacKeyModeSha256,
300  // Key is intended for HMAC SHA384 mode.
301  kOtcryptoKeyModeHmacSha384 =
302  kOtcryptoKeyTypeHmac << 16 | kOtcryptoHmacKeyModeSha384,
303  // Key is intended for HMAC SHA512 mode.
304  kOtcryptoKeyModeHmacSha512 =
305  kOtcryptoKeyTypeHmac << 16 | kOtcryptoHmacKeyModeSha512,
306  // Key is intended for KMAC128 mode.
307  kOtcryptoKeyModeKmac128 =
308  kOtcryptoKeyTypeKmac << 16 | kOtcryptoKmacKeyModeKmac128,
309  // Key is intended for KMAC256 mode.
310  kOtcryptoKeyModeKmac256 =
311  kOtcryptoKeyTypeKmac << 16 | kOtcryptoKmacKeyModeKmac256,
312  // Key is intended for RSA signature RSASSA-PKCS mode.
313  kOtcryptoKeyModeRsaSignPkcs =
314  kOtcryptoKeyTypeRsa << 16 | kOtcryptoRsaKeyModeSignPkcs,
315  // Key is intended for RSA signature RSASSA-PSS mode.
316  kOtcryptoKeyModeRsaSignPss =
317  kOtcryptoKeyTypeRsa << 16 | kOtcryptoRsaKeyModeSignPss,
318  // Key is intended for RSA encryption RSAES-OAEP mode.
319  kOtcryptoKeyModeRsaEncryptOaep =
320  kOtcryptoKeyTypeRsa << 16 | kOtcryptoRsaKeyModeEncryptOaep,
321  // Key is intended for ECDSA mode.
322  kOtcryptoKeyModeEcdsa = kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEcdsa,
323  // Key is intended for ECDH mode.
324  kOtcryptoKeyModeEcdh = kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEcdh,
325  // Key is intended for Ed25519 mode.
326  kOtcryptoKeyModeEd25519 =
327  kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEd25519,
328  // Key is intended for X25519 mode.
329  kOtcryptoKeyModeX25519 =
330  kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeX25519,
331  // Key is intended for KDF-CTR with HMAC as PRF.
332  kOtcryptoKeyModeKdfCtrHmac =
333  kOtcryptoKeyTypeKdf << 16 | kOtcryptoKdfKeyModeCtrHmac,
334  // Key is intended for KDF with KMAC128 as PRF.
335  kOtcryptoKeyModeKdfKmac128 =
336  kOtcryptoKeyTypeKdf << 16 | kOtcryptoKdfKeyModeKmac128,
337  // Key is intended for KDF with KMAC256 as PRF.
338  kOtcryptoKeyModeKdfKmac256 =
339  kOtcryptoKeyTypeKdf << 16 | kOtcryptoKdfKeyModeKmac256,
341 
342 /**
343  * Enum to denote key security level.
344  *
345  * At high security levels, the crypto library will prioritize
346  * protecting the key from sophisticated attacks, even at large
347  * performance costs. If the security level is low, the crypto
348  * library will still try to protect the key, but may forgo the
349  * most costly protections against e.g. sophisticated physical
350  * attacks.
351  *
352  * Values are hardened.
353  */
355  // Security level low.
356  kOtcryptoKeySecurityLevelLow = 0x1e9,
357  // Security level medium.
358  kOtcryptoKeySecurityLevelMedium = 0xeab,
359  // Security level high.
360  kOtcryptoKeySecurityLevelHigh = 0xa7e,
362 
363 /**
364  * Enum to denote the crypto library version.
365  *
366  * In future updates, this enum will be extended to preserve some
367  * level of backwards-compatibility despite changes to internal
368  * details (for example, the preferred masking scheme for blinded
369  * keys).
370  *
371  * Values are hardened.
372  */
373 typedef enum otcrypto_lib_version {
374  // Version 1.
375  kOtcryptoLibVersion1 = 0x7f4,
377 
378 /**
379  * Struct to represent the configuration of a blinded key.
380  */
381 typedef struct otcrypto_key_config {
382  // Crypto library version for this key.
383  otcrypto_lib_version_t version;
384  // Mode for which the key usage is intended.
385  otcrypto_key_mode_t key_mode;
386  // Length in bytes of the unblinded form of this key.
387  size_t key_length;
388  // Whether the hardware key manager should produce this key.
389  // If this is set to `true`, the keyblob must be exactly 8 words long, where
390  // the first word is the version and the remaining 7 words are the salt.
391  hardened_bool_t hw_backed;
392  // Whether the key can be exported (always false if `hw_backed` is true).
393  hardened_bool_t exportable;
394  // Key security level.
395  otcrypto_key_security_level_t security_level;
397 
398 /**
399  * Struct to handle unmasked key type.
400  */
401 typedef struct otcrypto_unblinded_key {
402  // Mode for which the key usage is intended.
403  otcrypto_key_mode_t key_mode;
404  // Key length in bytes.
405  size_t key_length;
406  // Implementation specific, storage provided by caller.
407  uint32_t *key;
408  // Implementation specific, checksum for this struct.
409  uint32_t checksum;
411 
412 /**
413  * Struct to handle masked key type.
414  */
415 typedef struct otcrypto_blinded_key {
416  // Key configuration information.
417  const otcrypto_key_config_t config;
418  // Length of blinded key material in bytes.
419  const size_t keyblob_length;
420  // Implementation specific, storage provided by caller.
421  uint32_t *keyblob;
422  // Implementation specific, checksum for this struct.
423  uint32_t checksum;
425 
426 /**
427  * Enum to define supported hashing modes.
428  *
429  * Values are hardened.
430  */
431 typedef enum otcrypto_hash_mode {
432  // SHA2-256 mode.
433  kOtcryptoHashModeSha256 = 0x69b,
434  // SHA2-384 mode.
435  kOtcryptoHashModeSha384 = 0x7ae,
436  // SHA2-512 mode.
437  kOtcryptoHashModeSha512 = 0x171,
438  // SHA3-224 mode.
439  kOtcryptoHashModeSha3_224 = 0x516,
440  // SHA3-256 mode.
441  kOtcryptoHashModeSha3_256 = 0x2d4,
442  // SHA3-384 mode.
443  kOtcryptoHashModeSha3_384 = 0x267,
444  // SHA3-512 mode.
445  kOtcryptoHashModeSha3_512 = 0x44d,
446  // Shake128 mode.
447  kOtcryptoHashXofModeShake128 = 0x5d8,
448  // Shake256 mode.
449  kOtcryptoHashXofModeShake256 = 0x34a,
450  // cShake128 mode.
451  kOtcryptoHashXofModeCshake128 = 0x0bd,
452  // cShake256 mode.
453  kOtcryptoHashXofModeCshake256 = 0x4e2,
455 
456 /**
457  * Container for a hash digest.
458  */
459 typedef struct otcrypto_hash_digest {
460  // Digest type.
462  // Digest data.
463  uint32_t *data;
464  // Digest length in 32-bit words.
465  size_t len;
467 
468 #ifdef __cplusplus
469 } // extern "C"
470 #endif // __cplusplus
471 
472 #endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_INCLUDE_DATATYPES_H_