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#include "api_config.h"
12#ifdef OTCRYPTO_IN_REPO
14#include "sw/device/lib/base/status.h"
15#else
16#include "freestanding/absl_status.h"
17#include "freestanding/defs.h"
18#include "freestanding/hardened.h"
19#endif
20
21/**
22 * @file
23 * @brief Shared datatypes for the OpenTitan cryptography library.
24 *
25 * This header defines status codes, byte buffer representations, and key
26 * representations that are shared between different algorithms within the
27 * library.
28 */
29
30#ifdef __cplusplus
31extern "C" {
32#endif // __cplusplus
33
34/**
35 * Return values for the crypto library.
36 *
37 * The crypto library's return value is defined as OpenTitan's internal
38 * `status_t` in order to simplify testing. However, informally the library
39 * guarantees that the concrete value contained in the status will be one of
40 * the members of the `otcrypto_status_value` enum below.
41 */
42typedef status_t otcrypto_status_t;
43
44/**
45 * Possible status values for the cryptolib.
46 *
47 * As long as the OTCRYPTO_STATUS_DEBUG define is unset, all `otcrypto_status_t`
48 * codes returned by the cryptolib should be bit-by-bit equivalent with one of
49 * the values in this enum.
50 *
51 * Values are built to be bit-compatible with OpenTitan's internal `status_t`
52 * datatypes. The highest (sign) bit indicates if the value is an error (1) or
53 * not (0). For non-error statuses, the rest can be anything; in cryptolib
54 * status codes it is always `kHardenedBoolTrue`. For errors:
55 * - The next 15 bits are a module identifier, which is always 0 in the
56 * cryptolib status codes
57 * - The next 11 bits are a line number or other information; in the
58 * cryptolib status codes, it is a hardened value created to have high
59 * Hamming distance with the other valid status codes
60 * - The final 5 bits are an Abseil-compatible error code
61 *
62 * The hardened values for error codes were generated with:
63 * $ ./util/design/sparse-fsm-encode.py -d 5 -m 5 -n 11 \
64 * -s 4232058530 --language=sv --avoid-zero
65 *
66 * Use the same seed value and a larger `-m` argument to generate new values
67 * without changing all error codes. Remove the seed (-s argument) to generate
68 * completely new 11-bit values.
69 */
71 // Status is OK; no errors.
72 kOtcryptoStatusValueOk = (int32_t)0x739,
73 // Invalid input arguments; wrong length or invalid type.
74 kOtcryptoStatusValueBadArgs = (int32_t)0x8000fea0 | kInvalidArgument,
75 // Error after which it is OK to retry (e.g. timeout).
76 kOtcryptoStatusValueInternalError = (int32_t)0x80005340 | kAborted,
77 // Error after which it is not OK to retry (e.g. integrity check).
78 kOtcryptoStatusValueFatalError = (int32_t)0x80006d80 | kFailedPrecondition,
79 // An asynchronous operation is still in progress.
80 kOtcryptoStatusValueAsyncIncomplete = (int32_t)0x8000ea40 | kUnavailable,
81 // TODO: remove all instances of this error before release; it is to track
82 // implementations that are not yet complete.
83 kOtcryptoStatusValueNotImplemented = (int32_t)0x80008d20 | kUnimplemented,
85
86/**
87 * Generic struct to hold a fixed-length byte array.
88 *
89 * Made to generalize the structures defined below.
90 */
91typedef struct otcrypto_generic_buf {
92 /// Pointer to the data.
93 const void *data;
94 /// Length of the data in bytes.
95 size_t len;
96#ifndef OTCRYPTO_DISABLE_BUF_INTEGRITY_CHECKS
97 /// Integrity of the buffer which is over the address and the length but not
98 /// the contents.
99 uint32_t ptr_checksum;
100#endif
102
103/**
104 * Struct to hold a fixed-length byte array.
105 *
106 * Note: the caller must (1) allocate sufficient space; (2) set the `len`
107 * field and `data` pointer when `otcrypto_byte_buf_t` is used for output; and
108 * (3) set the ptr_checksum using the OTCRYPTO_MAKE_BUF macro. The crypto
109 * library will throw an error if `len` doesn't match expectations.
110 */
111typedef struct otcrypto_byte_buf {
112 /// Pointer to the data.
113 uint8_t *data;
114 /// Length of the data in bytes.
115 size_t len;
116#ifndef OTCRYPTO_DISABLE_BUF_INTEGRITY_CHECKS
117 /// Integrity of the buffer which is over the address and the length but not
118 /// the contents.
119 uint32_t ptr_checksum;
120#endif
122
123/**
124 * Struct to hold a constant fixed-length byte array.
125 *
126 * The const annotations prevent any changes to the byte buffer. It is
127 * necessary to have this structure separate from `otcrypto_byte_buf_t` because
128 * data pointed to by a struct does not inherit `const`, so `const
129 * otcrypto_byte_buf_t` would still allow data to change.
130 */
132 /// Pointer to the data.
133 const uint8_t *const data;
134 /// Length of the data in bytes.
135 const size_t len;
136#ifndef OTCRYPTO_DISABLE_BUF_INTEGRITY_CHECKS
137 /// Integrity of the buffer which is over the address and the length but not
138 /// the contents.
139 uint32_t ptr_checksum;
140#endif
142
143/**
144 * Struct to hold a fixed-length word array.
145 *
146 * Note: the caller must (1) allocate sufficient space; (2) set the `len`
147 * field and `data` pointer when `otcrypto_word32_buf_t` is used for output; and
148 * (3) set the ptr_checksum using the OTCRYPTO_MAKE_BUF macro. The crypto
149 * library will throw an error if `len` doesn't match expectations.
150 */
151typedef struct otcrypto_word32_buf {
152 /// Pointer to the data.
153 uint32_t *data;
154 /// Length of the data in words.
155 size_t len;
156#ifndef OTCRYPTO_DISABLE_BUF_INTEGRITY_CHECKS
157 /// Integrity of the buffer which is over the address and the length but not
158 /// the contents.
159 uint32_t ptr_checksum;
160#endif
162
163/**
164 * Struct to hold a constant fixed-length word array.
165 *
166 * The const annotations prevent any changes to the word buffer. It is
167 * necessary to have this structure separate from `otcrypto_word32_buf_t`
168 * because data pointed to by a struct does not inherit `const`, so `const
169 * otcrypto_word32_buf_t` would still allow data to change.
170 */
172 /// Pointer to the data.
173 const uint32_t *const data;
174 /// Length of the data in words.
175 const size_t len;
176#ifndef OTCRYPTO_DISABLE_BUF_INTEGRITY_CHECKS
177 /// Integrity of the buffer which is over the address and the length but not
178 /// the contents.
179 uint32_t ptr_checksum;
180#endif
182
183/**
184 * Enum to denote the key type of the handled key.
185 *
186 * Values are hardened.
187 */
188typedef enum otcrypto_key_type {
189 /// Key type AES.
191 /// Key type HMAC.
193 /// Key type KMAC.
195 /// Key type RSA.
197 /// Key type ECC.
199 /// Key type KDF.
201 // Key type PQC.
202 kOtcryptoKeyTypePqc = 0xabc,
203 /// Key type TDES (plain software implementation; not FI/SCA protected).
206
207/**
208 * Enum to specify the AES modes that use a key.
209 *
210 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
211 * for which the provided key is intended for.
212 *
213 * Values are hardened.
214 */
216 /// Mode AES ECB.
218 /// Mode AES CBC.
220 /// Mode AES CFB.
222 /// Mode AES OFB.
224 /// Mode AES CTR.
226 /// Mode AES GCM.
228 /// Mode AES KWP.
230 /// Mode AES CMAC.
233
234/**
235 * Enum to specify the TDES modes that use a key.
236 *
237 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
238 * for which the provided key is intended for.
239 *
240 * Note: TDES is a plain software implementation and is NOT protected against
241 * fault injection (FI) or side-channel analysis (SCA) attacks.
242 *
243 * Values are hardened.
244 */
246 /// Mode TDES ECB.
248 /// Mode TDES CBC.
250 /// Mode TDES CFB.
252 /// Mode TDES OFB.
254 /// Mode TDES CTR.
257
258/**
259 * Enum to specify the HMAC modes that use a key.
260 *
261 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
262 * for which the provided key is intended for.
263 *
264 * Values are hardened.
265 */
267 /// Mode HMAC SHA1 (plain software; not FI/SCA protected).
269 /// Mode HMAC SHA256.
271 /// Mode HMAC SHA384.
273 /// Mode HMAC SHA512.
276
277/**
278 * Enum to specify the KMAC modes that use a key.
279 *
280 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
281 * for which the provided key is intended for.
282 *
283 * Values are hardened.
284 */
286 /// Mode KMAC128.
288 /// Mode KMAC256.
291
292/**
293 * Enum to specify the RSA modes that use a key.
294 *
295 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
296 * for which the provided key is intended for.
297 *
298 * Values are hardened.
299 */
301 /// Mode RSA Sign, RSASSA-PKCS.
303 /// Mode RSA Sign, RSASSA-PSS.
305 /// Mode RSA Encrypt, RSAES-OAEP.
307 /// Mode RSA Encrypt, RSAES-OAEP with MGF1-SHA1.
309 /// Mode RSA Encrypt, RSAES-OAEP with MGF1-SHA256.
311 /// Mode RSA Encrypt, RSAES-PKCS1-v1_5.
313 /// Mode RSA Raw (unpadded modular exponentiation).
316
317/**
318 * Enum to specify the ECC modes that use a key.
319 *
320 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
321 * for which the provided key is intended for.
322 *
323 * Values are hardened.
324 */
326 /// Mode ECDSA/P-256.
328 /// Mode ECDSA/P-384.
330 /// Mode ECDH/P-256.
332 /// Mode ECDH/P-384.
334 /// Mode Ed25519.
336 /// Mode X25519.
339
340/**
341 * Enum to specify the KDF modes that use a key.
342 *
343 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
344 * for which the provided key is intended for.
345 *
346 * Values are hardened.
347 */
349 /// Mode KDF-KMAC with KMAC128 as PRF.
351 /// Mode KDF-KMAC with KMAC256 as PRF.
354
355/**
356 * Enum to specify the PQC modes that use a key.
357 *
358 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
359 * for which the provided key is intended for.
360 *
361 * Values are hardened.
362 */
364 // Mode PQC-ML-DSA-87.
365 kOtcryptoPqcKeyModeMldsa87 = 0xcee,
366 // Mode PQC-ML-KEM-1024.
367 kOtcryptoPqcKeyModeMlkem1024 = 0x73a,
369
370/**
371 * Enum for opentitan crypto modes that use a key.
372 *
373 * Denotes the crypto mode for which the provided key is to be used.
374 * This `otcrypto_key_mode_t` will be a parameter in the
375 * `otcrypto_blinded_key_t` and `otcrypto_unblinded_key_t` structs.
376 *
377 * Values are hardened.
378 */
379typedef enum otcrypto_key_mode {
380 /// Key is intended for AES ECB mode.
382 /// Key is intended for AES CBC mode.
384 /// Key is intended for AES CFB mode.
386 /// Key is intended for AES OFB mode.
388 /// Key is intended for AES CTR mode.
390 /// Key is intended for AES GCM mode.
392 /// Key is intended for AES KWP mode.
394 /// Key is intended for AES CMAC mode.
396 /// Key is intended for TDES ECB mode.
399 /// Key is intended for TDES CBC mode.
402 /// Key is intended for TDES CFB mode.
405 /// Key is intended for TDES OFB mode.
408 /// Key is intended for TDES CTR mode.
411 /// Key is intended for HMAC SHA1 mode (plain software; not FI/SCA protected).
414 /// Key is intended for HMAC SHA256 mode.
417 /// Key is intended for HMAC SHA384 mode.
420 /// Key is intended for HMAC SHA512 mode.
423 /// Key is intended for KMAC128 mode.
426 /// Key is intended for KMAC256 mode.
429 /// Key is intended for RSA signature RSASSA-PKCS mode.
432 /// Key is intended for RSA signature RSASSA-PSS mode.
435 /// Key is intended for RSA encryption RSAES-OAEP mode.
438 /// Key is intended for RSA encryption RSAES-OAEP mode with MGF1-SHA1.
441 /// Key is intended for RSA encryption RSAES-OAEP mode with MGF1-SHA256.
444 /// Key is intended for RSA encryption RSAES-PKCS1-v1_5 mode.
447 /// Key is intended for unpadded (raw) RSA operations.
449 /// Key is intended for ECDSA with P-256.
452 /// Key is intended for ECDSA with P-384.
455 /// Key is intended for ECDH with P-256.
458 /// Key is intended for ECDH with P-384.
461 /// Key is intended for Ed25519 mode.
464 /// Key is intended for X25519 mode.
467 /// Key is intended for KDF with KMAC128 as PRF.
470 /// Key is intended for KDF with KMAC256 as PRF.
473 kOtcryptoKeyModePqcMldsa87 =
474 kOtcryptoKeyTypePqc << 16 | kOtcryptoPqcKeyModeMldsa87,
475 kOtcryptoKeyModePqcMlkem1024 =
476 kOtcryptoKeyTypePqc << 16 | kOtcryptoPqcKeyModeMlkem1024,
478
479/**
480 * Enum to denote key security level.
481 *
482 * At high security levels, the crypto library will prioritize
483 * protecting the key from sophisticated attacks, even at large
484 * performance costs. If the security level is low, the crypto
485 * library will still try to protect the key, but may forgo the
486 * most costly protections against e.g. sophisticated physical
487 * attacks.
488 *
489 * Values are hardened.
490 */
492 /// Security level low.
494 /// Security level medium.
496 /// Security level high.
499
500/**
501 * Enum to denote the crypto library version.
502 *
503 * In future updates, this enum will be extended to preserve some
504 * level of backwards-compatibility despite changes to internal
505 * details (for example, the preferred masking scheme for blinded
506 * keys).
507 *
508 * Values are hardened.
509 */
511 /**
512 * Version numbers are encoded with high Hamming distance modular arithmetic.
513 * To generate the hardened integer for a new (major, minor, patch) version,
514 * compute:
515 * version = (((major << 24) | (minor << 16) | (patch << 8) | 0x04) *
516 * 0xc0c001fdu) & 0xffffffffu;
517 *
518 * For example:
519 * - 1.0.0 -> 0x000007f4
520 * - 1.0.1 -> 0xc00204f4
521 * - 1.1.0 -> 0x01fd07f4
522 * - 2.0.0 -> 0xfd0007f4
523 */
524
525 /// Version 1.0.0.
527 /// Version 2.0.0.
530
531/**
532 * Struct to represent the configuration of a blinded key.
533 */
534typedef struct otcrypto_key_config {
535 /// Crypto library version for this key.
537 /// Mode for which the key usage is intended.
539 /// Length in bytes of the unblinded form of this key.
540 uint32_t key_length;
541 /// Whether the hardware key manager should produce this key.
542 /// If this is set to `true`, the keyblob must be exactly 8 words long, where
543 /// the first word is the version and the remaining 7 words are the salt.
545 /// Determines which keymgr DPE slot (see `slot_src_sel` in
546 /// `keymgr_dpe_diversification_t`) is used as the parent for key generation.
547 /// Only applicable if `hw_backed` is `kHardenedBoolTrue`.
549 /// Whether the key can be exported (always false if `hw_backed` is true).
551 /// Key security level.
554
555/**
556 * Maximum number of 32-bit words in a wrapped key.
557 *
558 * Sized for the largest supported key (RSA-4096 private key). Callers can use
559 * this to bound input buffers passed to `otcrypto_key_unwrap`.
560 */
561enum {
562 kOtcryptoWrappedKeyMaxWords =
563 /* key configuration struct */
564 sizeof(otcrypto_key_config_t) / sizeof(uint32_t) +
565 /* checksum and keyblob_length fields */
566 2 +
567 /* RSA-4096 keyblob: 2 shares of 512 bytes each */
568 2 * (4096 / 8 / sizeof(uint32_t)) +
569 /* AES-KWP 64-bit integrity prefix */
570 2,
571};
572
573/**
574 * Struct to handle unmasked key type.
575 */
577 /// Mode for which the key usage is intended.
579 /// Key length in bytes.
580 uint32_t key_length;
581 /// Implementation specific, storage provided by caller.
582 uint32_t *key;
583 /// Implementation specific, checksum for this struct.
584 uint32_t checksum;
586
587/**
588 * Struct to handle masked key type.
589 */
590typedef struct otcrypto_blinded_key {
591 /// Key configuration information.
593 /// Length of blinded key material in bytes.
595 /// Implementation specific, storage provided by caller.
596 uint32_t *keyblob;
597 /// Implementation specific, checksum for this struct.
598 uint32_t checksum;
600
601/**
602 * Enum to define supported hashing modes.
603 *
604 * Values are hardened.
605 */
606typedef enum otcrypto_hash_mode {
607 /// Null (unhashed / raw digest) mode for RSA PKCS#1 v1.5 and raw operations.
609 /// SHA-1 mode (plain software; not FI/SCA protected).
611 /// SHA2-256 mode.
613 /// SHA2-384 mode.
615 /// SHA2-512 mode.
617 /// SHA3-224 mode.
619 /// SHA3-256 mode.
621 /// SHA3-384 mode.
623 /// SHA3-512 mode.
625 /// Shake128 mode.
627 /// Shake256 mode.
629 /// cShake128 mode.
631 /// cShake256 mode.
634
635/**
636 * Container for a hash digest.
637 */
638typedef struct otcrypto_hash_digest {
639 /// Digest type.
641 /// Digest data.
642 uint32_t *data;
643 /// Digest length in 32-bit words.
644 size_t len;
646
647/**
648 * Lock state value indicating the cryptolib is locked specifically due to an
649 * entropy source health test failure.
650 *
651 * Unlike a hard lock (`kHardenedByteBoolTrue`), this state allows recovery
652 * by calling `otcrypto_entropy_init()`, which re-executes the RNG startup
653 * tests.
654 */
655enum {
656 kOtcryptoLockStateEntropy = 0xd6,
657};
658
659/**
660 * CMVP service indicator states.
661 */
663 // No service called or indicator cleared.
664 kOtcryptoCmvpNoService = 0,
665 // Approved crypto service.
666 kOtcryptoCmvpApprovedService = 1,
667 // Non-approved crypto service.
668 kOtcryptoCmvpNotApprovedService = 2,
670
671typedef otcrypto_cmvp_service_indicator_t otcrypto_cmvp_indicator_t;
672#ifdef __cplusplus
673} // extern "C"
674#endif // __cplusplus
675
676#endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_INCLUDE_DATATYPES_H_