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,
204
205/**
206 * Enum to specify the AES 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 AES ECB.
216 /// Mode AES CBC.
218 /// Mode AES CFB.
220 /// Mode AES OFB.
222 /// Mode AES CTR.
224 /// Mode AES GCM.
226 /// Mode AES KWP.
228 /// Mode AES CMAC.
231
232/**
233 * Enum to specify the HMAC modes that use a key.
234 *
235 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
236 * for which the provided key is intended for.
237 *
238 * Values are hardened.
239 */
241 /// Mode HMAC SHA256.
243 /// Mode HMAC SHA384.
245 /// Mode HMAC SHA512.
248
249/**
250 * Enum to specify the KMAC modes that use a key.
251 *
252 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
253 * for which the provided key is intended for.
254 *
255 * Values are hardened.
256 */
258 /// Mode KMAC128.
260 /// Mode KMAC256.
263
264/**
265 * Enum to specify the RSA modes that use a key.
266 *
267 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
268 * for which the provided key is intended for.
269 *
270 * Values are hardened.
271 */
273 /// Mode RSA Sign, RSASSA-PKCS.
275 /// Mode RSA Sign, RSASSA-PSS.
277 /// Mode RSA Encrypt, RSAES-OAEP.
280
281/**
282 * Enum to specify the ECC modes that use a key.
283 *
284 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
285 * for which the provided key is intended for.
286 *
287 * Values are hardened.
288 */
290 /// Mode ECDSA/P-256.
292 /// Mode ECDSA/P-384.
294 /// Mode ECDH/P-256.
296 /// Mode ECDH/P-384.
298 /// Mode Ed25519.
300 /// Mode X25519.
303
304/**
305 * Enum to specify the KDF modes that use a key.
306 *
307 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
308 * for which the provided key is intended for.
309 *
310 * Values are hardened.
311 */
313 /// Mode KDF-CTR with HMAC as PRF.
315 /// Mode KDF-KMAC with KMAC128 as PRF.
317 /// Mode KDF-KMAC with KMAC256 as PRF.
320
321/**
322 * Enum to specify the PQC modes that use a key.
323 *
324 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
325 * for which the provided key is intended for.
326 *
327 * Values are hardened.
328 */
330 // Mode PQC-ML-DSA-87.
331 kOtcryptoPqcKeyModeMldsa87 = 0xcee,
332 // Mode PQC-ML-KEM-1024.
333 kOtcryptoPqcKeyModeMlkem1024 = 0x73a,
335
336/**
337 * Enum for opentitan crypto modes that use a key.
338 *
339 * Denotes the crypto mode for which the provided key is to be used.
340 * This `otcrypto_key_mode_t` will be a parameter in the
341 * `otcrypto_blinded_key_t` and `otcrypto_unblinded_key_t` structs.
342 *
343 * Values are hardened.
344 */
345typedef enum otcrypto_key_mode {
346 /// Key is intended for AES ECB mode.
348 /// Key is intended for AES CBC mode.
350 /// Key is intended for AES CFB mode.
352 /// Key is intended for AES OFB mode.
354 /// Key is intended for AES CTR mode.
356 /// Key is intended for AES GCM mode.
358 /// Key is intended for AES KWP mode.
360 /// Key is intended for AES CMAC mode.
362 /// Key is intended for HMAC SHA256 mode.
365 /// Key is intended for HMAC SHA384 mode.
368 /// Key is intended for HMAC SHA512 mode.
371 /// Key is intended for KMAC128 mode.
374 /// Key is intended for KMAC256 mode.
377 /// Key is intended for RSA signature RSASSA-PKCS mode.
380 /// Key is intended for RSA signature RSASSA-PSS mode.
383 /// Key is intended for RSA encryption RSAES-OAEP mode.
386 /// Key is intended for ECDSA with P-256.
389 /// Key is intended for ECDSA with P-384.
392 /// Key is intended for ECDH with P-256.
395 /// Key is intended for ECDH with P-384.
398 /// Key is intended for Ed25519 mode.
401 /// Key is intended for X25519 mode.
404 /// Key is intended for KDF-CTR with HMAC as PRF.
407 /// Key is intended for KDF with KMAC128 as PRF.
410 /// Key is intended for KDF with KMAC256 as PRF.
413 kOtcryptoKeyModePqcMldsa87 =
414 kOtcryptoKeyTypePqc << 16 | kOtcryptoPqcKeyModeMldsa87,
415 kOtcryptoKeyModePqcMlkem1024 =
416 kOtcryptoKeyTypePqc << 16 | kOtcryptoPqcKeyModeMlkem1024,
418
419/**
420 * Enum to denote key security level.
421 *
422 * At high security levels, the crypto library will prioritize
423 * protecting the key from sophisticated attacks, even at large
424 * performance costs. If the security level is low, the crypto
425 * library will still try to protect the key, but may forgo the
426 * most costly protections against e.g. sophisticated physical
427 * attacks.
428 *
429 * Values are hardened.
430 */
432 /// Security level low.
434 /// Security level medium.
436 /// Security level high.
439
440/**
441 * Enum to denote the crypto library version.
442 *
443 * In future updates, this enum will be extended to preserve some
444 * level of backwards-compatibility despite changes to internal
445 * details (for example, the preferred masking scheme for blinded
446 * keys).
447 *
448 * Values are hardened.
449 */
451 /**
452 * Version numbers are encoded with high Hamming distance modular arithmetic.
453 * To generate the hardened integer for a new (major, minor, patch) version,
454 * compute:
455 * version = (((major << 24) | (minor << 16) | (patch << 8) | 0x04) *
456 * 0xc0c001fdu) & 0xffffffffu;
457 *
458 * For example:
459 * - 1.0.0 -> 0x000007f4
460 * - 1.0.1 -> 0xc00204f4
461 * - 1.1.0 -> 0x01fd07f4
462 * - 2.0.0 -> 0xfd0007f4
463 */
464
465 /// Version 1.0.0.
467 /// Version 2.0.0.
470
471/**
472 * Struct to represent the configuration of a blinded key.
473 */
474typedef struct otcrypto_key_config {
475 /// Crypto library version for this key.
477 /// Mode for which the key usage is intended.
479 /// Length in bytes of the unblinded form of this key.
480 uint32_t key_length;
481 /// Whether the hardware key manager should produce this key.
482 /// If this is set to `true`, the keyblob must be exactly 8 words long, where
483 /// the first word is the version and the remaining 7 words are the salt.
485 /// Determines which keymgr DPE slot (see `slot_src_sel` in
486 /// `keymgr_dpe_diversification_t`) is used as the parent for key generation.
487 /// Only applicable if `hw_backed` is `kHardenedBoolTrue`.
489 /// Whether the key can be exported (always false if `hw_backed` is true).
491 /// Key security level.
494
495/**
496 * Maximum number of 32-bit words in a wrapped key.
497 *
498 * Sized for the largest supported key (RSA-4096 private key). Callers can use
499 * this to bound input buffers passed to `otcrypto_key_unwrap`.
500 */
501enum {
502 kOtcryptoWrappedKeyMaxWords =
503 /* key configuration struct */
504 sizeof(otcrypto_key_config_t) / sizeof(uint32_t) +
505 /* checksum and keyblob_length fields */
506 2 +
507 /* RSA-4096 keyblob: 2 shares of 512 bytes each */
508 2 * (4096 / 8 / sizeof(uint32_t)) +
509 /* AES-KWP 64-bit integrity prefix */
510 2,
511};
512
513/**
514 * Struct to handle unmasked key type.
515 */
517 /// Mode for which the key usage is intended.
519 /// Key length in bytes.
520 uint32_t key_length;
521 /// Implementation specific, storage provided by caller.
522 uint32_t *key;
523 /// Implementation specific, checksum for this struct.
524 uint32_t checksum;
526
527/**
528 * Struct to handle masked key type.
529 */
530typedef struct otcrypto_blinded_key {
531 /// Key configuration information.
533 /// Length of blinded key material in bytes.
534 const uint32_t keyblob_length;
535 /// Implementation specific, storage provided by caller.
536 uint32_t *keyblob;
537 /// Implementation specific, checksum for this struct.
538 uint32_t checksum;
540
541/**
542 * Enum to define supported hashing modes.
543 *
544 * Values are hardened.
545 */
546typedef enum otcrypto_hash_mode {
547 /// SHA2-256 mode.
549 /// SHA2-384 mode.
551 /// SHA2-512 mode.
553 /// SHA3-224 mode.
555 /// SHA3-256 mode.
557 /// SHA3-384 mode.
559 /// SHA3-512 mode.
561 /// Shake128 mode.
563 /// Shake256 mode.
565 /// cShake128 mode.
567 /// cShake256 mode.
570
571/**
572 * Container for a hash digest.
573 */
574typedef struct otcrypto_hash_digest {
575 /// Digest type.
577 /// Digest data.
578 uint32_t *data;
579 /// Digest length in 32-bit words.
580 size_t len;
582
583#ifdef __cplusplus
584} // extern "C"
585#endif // __cplusplus
586
587#endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_INCLUDE_DATATYPES_H_