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
30extern "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 */
41typedef status_t otcrypto_status_t;
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 */
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 * Generic struct to hold a fixed-length byte array.
87 *
88 * Made to generalize the structures defined below.
89 */
90typedef struct otcrypto_generic_buf {
91 const void *data;
92 size_t len;
93#ifndef OTCRYPTO_DISABLE_BUF_INTEGRITY_CHECKS
94 uint32_t ptr_checksum;
95#endif
97
98/**
99 * Struct to hold a fixed-length byte array.
100 *
101 * Note: the caller must (1) allocate sufficient space; (2) set the `len`
102 * field and `data` pointer when `otcrypto_byte_buf_t` is used for output; and
103 * (3) set the ptr_checksum using the OTCRYPTO_MAKE_BUF macro. The crypto
104 * library will throw an error if `len` doesn't match expectations.
105 */
106typedef struct otcrypto_byte_buf {
107 // Pointer to the data.
108 uint8_t *data;
109 // Length of the data in bytes.
110 size_t len;
111 // Integrity of the buffer which is over the address and the length but not
112 // the contents.
113#ifndef OTCRYPTO_DISABLE_BUF_INTEGRITY_CHECKS
114 uint32_t ptr_checksum;
115#endif
117
118/**
119 * Struct to hold a constant fixed-length byte array.
120 *
121 * The const annotations prevent any changes to the byte buffer. It is
122 * necessary to have this structure separate from `otcrypto_byte_buf_t` because
123 * data pointed to by a struct does not inherit `const`, so `const
124 * otcrypto_byte_buf_t` would still allow data to change.
125 */
127 // Pointer to the data.
128 const uint8_t *const data;
129 // Length of the data in bytes.
130 const size_t len;
131 // Integrity of the buffer which is over the address and the length but not
132 // the contents.
133#ifndef OTCRYPTO_DISABLE_BUF_INTEGRITY_CHECKS
134 uint32_t ptr_checksum;
135#endif
137
138/**
139 * Struct to hold a fixed-length word array.
140 *
141 * Note: the caller must (1) allocate sufficient space; (2) set the `len`
142 * field and `data` pointer when `otcrypto_word32_buf_t` is used for output; and
143 * (3) set the ptr_checksum using the OTCRYPTO_MAKE_BUF macro. The crypto
144 * library will throw an error if `len` doesn't match expectations.
145 */
146typedef struct otcrypto_word32_buf {
147 // Pointer to the data.
148 uint32_t *data;
149 // Length of the data in words.
150 size_t len;
151 // Integrity of the buffer which is over the address and the length but not
152 // the contents.
153#ifndef OTCRYPTO_DISABLE_BUF_INTEGRITY_CHECKS
154 uint32_t ptr_checksum;
155#endif
157
158/**
159 * Struct to hold a constant fixed-length word array.
160 *
161 * The const annotations prevent any changes to the word buffer. It is
162 * necessary to have this structure separate from `otcrypto_word32_buf_t`
163 * because data pointed to by a struct does not inherit `const`, so `const
164 * otcrypto_word32_buf_t` would still allow data to change.
165 */
167 // Pointer to the data.
168 const uint32_t *const data;
169 // Length of the data in words.
170 const size_t len;
171 // Integrity of the buffer which is over the address and the length but not
172 // the contents.
173#ifndef OTCRYPTO_DISABLE_BUF_INTEGRITY_CHECKS
174 uint32_t ptr_checksum;
175#endif
177
178/**
179 * Enum to denote the key type of the handled key.
180 *
181 * Values are hardened.
182 */
183typedef enum otcrypto_key_type {
184 // Key type AES.
185 kOtcryptoKeyTypeAes = 0x8e9,
186 // Key type HMAC.
187 kOtcryptoKeyTypeHmac = 0xe3f,
188 // Key type KMAC.
189 kOtcryptoKeyTypeKmac = 0xb74,
190 // Key type RSA.
191 kOtcryptoKeyTypeRsa = 0x7ee,
192 // Key type ECC.
193 kOtcryptoKeyTypeEcc = 0x15b,
194 // Key type KDF.
195 kOtcryptoKeyTypeKdf = 0xb87,
197
198/**
199 * Enum to specify the AES modes that use a key.
200 *
201 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
202 * for which the provided key is intended for.
203 *
204 * Values are hardened.
205 */
207 // Mode AES ECB.
208 kOtcryptoAesKeyModeEcb = 0x1b6,
209 // Mode AES CBC.
210 kOtcryptoAesKeyModeCbc = 0xf3a,
211 // Mode AES CFB.
212 kOtcryptoAesKeyModeCfb = 0x0f9,
213 // Mode AES OFB.
214 kOtcryptoAesKeyModeOfb = 0xb49,
215 // Mode AES CTR.
216 kOtcryptoAesKeyModeCtr = 0x4ce,
217 // Mode AES GCM.
218 kOtcryptoAesKeyModeGcm = 0xaa5,
219 // Mode AES KWP.
220 kOtcryptoAesKeyModeKwp = 0x7d5,
221 // Mode AES CMAC.
222 kOtcryptoAesKeyModeCmac = 0x1a2,
224
225/**
226 * Enum to specify the HMAC modes that use a key.
227 *
228 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
229 * for which the provided key is intended for.
230 *
231 * Values are hardened.
232 */
234 // Mode HMAC SHA256.
235 kOtcryptoHmacKeyModeSha256 = 0x7fd,
236 // Mode HMAC SHA384.
237 kOtcryptoHmacKeyModeSha384 = 0x43b,
238 // Mode HMAC SHA512.
239 kOtcryptoHmacKeyModeSha512 = 0x7a2,
241
242/**
243 * Enum to specify the KMAC modes that use a key.
244 *
245 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
246 * for which the provided key is intended for.
247 *
248 * Values are hardened.
249 */
251 // Mode KMAC128.
252 kOtcryptoKmacKeyModeKmac128 = 0xa56,
253 // Mode KMAC256.
254 kOtcryptoKmacKeyModeKmac256 = 0x663,
256
257/**
258 * Enum to specify the RSA modes that use a key.
259 *
260 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
261 * for which the provided key is intended for.
262 *
263 * Values are hardened.
264 */
266 // Mode RSA Sign, RSASSA-PKCS.
267 kOtcryptoRsaKeyModeSignPkcs = 0x3d4,
268 // Mode RSA Sign, RSASSA-PSS.
269 kOtcryptoRsaKeyModeSignPss = 0x761,
270 // Mode RSA Encrypt, RSAES-OAEP.
271 kOtcryptoRsaKeyModeEncryptOaep = 0x585,
273
274/**
275 * Enum to specify the ECC modes that use a key.
276 *
277 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
278 * for which the provided key is intended for.
279 *
280 * Values are hardened.
281 */
283 // Mode ECDSA/P-256.
284 kOtcryptoEccKeyModeEcdsaP256 = 0x31e,
285 // Mode ECDSA/P-384.
286 kOtcryptoEccKeyModeEcdsaP384 = 0x695,
287 // Mode ECDH/P-256.
288 kOtcryptoEccKeyModeEcdhP256 = 0x5fc,
289 // Mode ECDH/P-384.
290 kOtcryptoEccKeyModeEcdhP384 = 0x1c7,
291 // Mode Ed25519.
292 kOtcryptoEccKeyModeEd25519 = 0x663,
293 // Mode X25519.
294 kOtcryptoEccKeyModeX25519 = 0x0bb,
296
297/**
298 * Enum to specify the KDF modes that use a key.
299 *
300 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
301 * for which the provided key is intended for.
302 *
303 * Values are hardened.
304 */
306 // Mode KDF-CTR with HMAC as PRF.
307 kOtcryptoKdfKeyModeCtrHmac = 0x12f,
308 // Mode KDF-KMAC with KMAC128 as PRF.
309 kOtcryptoKdfKeyModeKmac128 = 0xe5e,
310 // Mode KDF-KMAC with KMAC256 as PRF.
311 kOtcryptoKdfKeyModeKmac256 = 0x353,
313
314/**
315 * Enum for opentitan crypto modes that use a key.
316 *
317 * Denotes the crypto mode for which the provided key is to be used.
318 * This `otcrypto_key_mode_t` will be a parameter in the
319 * `otcrypto_blinded_key_t` and `otcrypto_unblinded_key_t` structs.
320 *
321 * Values are hardened.
322 */
323typedef enum otcrypto_key_mode {
324 // Key is intended for AES ECB mode.
325 kOtcryptoKeyModeAesEcb = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeEcb,
326 // Key is intended for AES CBC mode.
327 kOtcryptoKeyModeAesCbc = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeCbc,
328 // Key is intended for AES CFB mode.
329 kOtcryptoKeyModeAesCfb = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeCfb,
330 // Key is intended for AES OFB mode.
331 kOtcryptoKeyModeAesOfb = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeOfb,
332 // Key is intended for AES CTR mode.
333 kOtcryptoKeyModeAesCtr = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeCtr,
334 // Key is intended for AES GCM mode.
335 kOtcryptoKeyModeAesGcm = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeGcm,
336 // Key is intended for AES KWP mode.
337 kOtcryptoKeyModeAesKwp = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeKwp,
338 // Key is intended for AES CMAC mode.
339 kOtcryptoKeyModeAesCmac = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeCmac,
340 // Key is intended for HMAC SHA256 mode.
341 kOtcryptoKeyModeHmacSha256 =
342 kOtcryptoKeyTypeHmac << 16 | kOtcryptoHmacKeyModeSha256,
343 // Key is intended for HMAC SHA384 mode.
344 kOtcryptoKeyModeHmacSha384 =
345 kOtcryptoKeyTypeHmac << 16 | kOtcryptoHmacKeyModeSha384,
346 // Key is intended for HMAC SHA512 mode.
347 kOtcryptoKeyModeHmacSha512 =
348 kOtcryptoKeyTypeHmac << 16 | kOtcryptoHmacKeyModeSha512,
349 // Key is intended for KMAC128 mode.
350 kOtcryptoKeyModeKmac128 =
351 kOtcryptoKeyTypeKmac << 16 | kOtcryptoKmacKeyModeKmac128,
352 // Key is intended for KMAC256 mode.
353 kOtcryptoKeyModeKmac256 =
354 kOtcryptoKeyTypeKmac << 16 | kOtcryptoKmacKeyModeKmac256,
355 // Key is intended for RSA signature RSASSA-PKCS mode.
356 kOtcryptoKeyModeRsaSignPkcs =
357 kOtcryptoKeyTypeRsa << 16 | kOtcryptoRsaKeyModeSignPkcs,
358 // Key is intended for RSA signature RSASSA-PSS mode.
359 kOtcryptoKeyModeRsaSignPss =
360 kOtcryptoKeyTypeRsa << 16 | kOtcryptoRsaKeyModeSignPss,
361 // Key is intended for RSA encryption RSAES-OAEP mode.
362 kOtcryptoKeyModeRsaEncryptOaep =
363 kOtcryptoKeyTypeRsa << 16 | kOtcryptoRsaKeyModeEncryptOaep,
364 // Key is intended for ECDSA with P-256.
365 kOtcryptoKeyModeEcdsaP256 =
366 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEcdsaP256,
367 // Key is intended for ECDSA with P-384.
368 kOtcryptoKeyModeEcdsaP384 =
369 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEcdsaP384,
370 // Key is intended for ECDH with P-256.
371 kOtcryptoKeyModeEcdhP256 =
372 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEcdhP256,
373 // Key is intended for ECDH with P-384.
374 kOtcryptoKeyModeEcdhP384 =
375 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEcdhP384,
376 // Key is intended for Ed25519 mode.
377 kOtcryptoKeyModeEd25519 =
378 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEd25519,
379 // Key is intended for X25519 mode.
380 kOtcryptoKeyModeX25519 =
381 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeX25519,
382 // Key is intended for KDF-CTR with HMAC as PRF.
383 kOtcryptoKeyModeKdfCtrHmac =
384 kOtcryptoKeyTypeKdf << 16 | kOtcryptoKdfKeyModeCtrHmac,
385 // Key is intended for KDF with KMAC128 as PRF.
386 kOtcryptoKeyModeKdfKmac128 =
387 kOtcryptoKeyTypeKdf << 16 | kOtcryptoKdfKeyModeKmac128,
388 // Key is intended for KDF with KMAC256 as PRF.
389 kOtcryptoKeyModeKdfKmac256 =
390 kOtcryptoKeyTypeKdf << 16 | kOtcryptoKdfKeyModeKmac256,
392
393/**
394 * Enum to denote key security level.
395 *
396 * At high security levels, the crypto library will prioritize
397 * protecting the key from sophisticated attacks, even at large
398 * performance costs. If the security level is low, the crypto
399 * library will still try to protect the key, but may forgo the
400 * most costly protections against e.g. sophisticated physical
401 * attacks.
402 *
403 * Values are hardened.
404 */
406 // Security level low.
407 kOtcryptoKeySecurityLevelLow = 0x1e9,
408 // Security level medium.
409 kOtcryptoKeySecurityLevelMedium = 0xeab,
410 // Security level high.
411 kOtcryptoKeySecurityLevelHigh = 0xa7e,
413
414/**
415 * Enum to denote the crypto library version.
416 *
417 * In future updates, this enum will be extended to preserve some
418 * level of backwards-compatibility despite changes to internal
419 * details (for example, the preferred masking scheme for blinded
420 * keys).
421 *
422 * Values are hardened.
423 */
425 // Version 1.
426 kOtcryptoLibVersion1 = 0x7f4,
428
429/**
430 * Struct to represent the configuration of a blinded key.
431 */
432typedef struct otcrypto_key_config {
433 // Crypto library version for this key.
435 // Mode for which the key usage is intended.
436 otcrypto_key_mode_t key_mode;
437 // Length in bytes of the unblinded form of this key.
438 uint32_t key_length;
439 // Whether the hardware key manager should produce this key.
440 // If this is set to `true`, the keyblob must be exactly 8 words long, where
441 // the first word is the version and the remaining 7 words are the salt.
442 hardened_bool_t hw_backed;
443 // Whether the key can be exported (always false if `hw_backed` is true).
444 hardened_bool_t exportable;
445 // Key security level.
446 otcrypto_key_security_level_t security_level;
448
449/**
450 * Maximum number of 32-bit words in a wrapped key.
451 *
452 * Sized for the largest supported key (RSA-4096 private key). Callers can use
453 * this to bound input buffers passed to `otcrypto_key_unwrap`.
454 */
455enum {
456 kOtcryptoWrappedKeyMaxWords =
457 /* key configuration struct */
458 sizeof(otcrypto_key_config_t) / sizeof(uint32_t) +
459 /* checksum and keyblob_length fields */
460 2 +
461 /* RSA-4096 keyblob: 2 shares of 512 bytes each */
462 2 * (4096 / 8 / sizeof(uint32_t)) +
463 /* AES-KWP 64-bit integrity prefix */
464 2,
465};
466
467/**
468 * Struct to handle unmasked key type.
469 */
471 // Mode for which the key usage is intended.
472 otcrypto_key_mode_t key_mode;
473 // Key length in bytes.
474 uint32_t key_length;
475 // Implementation specific, storage provided by caller.
476 uint32_t *key;
477 // Implementation specific, checksum for this struct.
478 uint32_t checksum;
480
481/**
482 * Struct to handle masked key type.
483 */
484typedef struct otcrypto_blinded_key {
485 // Key configuration information.
486 const otcrypto_key_config_t config;
487 // Length of blinded key material in bytes.
488 const uint32_t keyblob_length;
489 // Implementation specific, storage provided by caller.
490 uint32_t *keyblob;
491 // Implementation specific, checksum for this struct.
492 uint32_t checksum;
494
495/**
496 * Enum to define supported hashing modes.
497 *
498 * Values are hardened.
499 */
500typedef enum otcrypto_hash_mode {
501 // SHA2-256 mode.
502 kOtcryptoHashModeSha256 = 0x69b,
503 // SHA2-384 mode.
504 kOtcryptoHashModeSha384 = 0x7ae,
505 // SHA2-512 mode.
506 kOtcryptoHashModeSha512 = 0x171,
507 // SHA3-224 mode.
508 kOtcryptoHashModeSha3_224 = 0x516,
509 // SHA3-256 mode.
510 kOtcryptoHashModeSha3_256 = 0x2d4,
511 // SHA3-384 mode.
512 kOtcryptoHashModeSha3_384 = 0x267,
513 // SHA3-512 mode.
514 kOtcryptoHashModeSha3_512 = 0x44d,
515 // Shake128 mode.
516 kOtcryptoHashXofModeShake128 = 0x5d8,
517 // Shake256 mode.
518 kOtcryptoHashXofModeShake256 = 0x34a,
519 // cShake128 mode.
520 kOtcryptoHashXofModeCshake128 = 0x0bd,
521 // cShake256 mode.
522 kOtcryptoHashXofModeCshake256 = 0x4e2,
524
525/**
526 * Container for a hash digest.
527 */
528typedef struct otcrypto_hash_digest {
529 // Digest type.
531 // Digest data.
532 uint32_t *data;
533 // Digest length in 32-bit words.
534 size_t len;
536
537#ifdef __cplusplus
538} // extern "C"
539#endif // __cplusplus
540
541#endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_INCLUDE_DATATYPES_H_