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,
333
334/**
335 * Enum for opentitan crypto modes that use a key.
336 *
337 * Denotes the crypto mode for which the provided key is to be used.
338 * This `otcrypto_key_mode_t` will be a parameter in the
339 * `otcrypto_blinded_key_t` and `otcrypto_unblinded_key_t` structs.
340 *
341 * Values are hardened.
342 */
343typedef enum otcrypto_key_mode {
344 /// Key is intended for AES ECB mode.
346 /// Key is intended for AES CBC mode.
348 /// Key is intended for AES CFB mode.
350 /// Key is intended for AES OFB mode.
352 /// Key is intended for AES CTR mode.
354 /// Key is intended for AES GCM mode.
356 /// Key is intended for AES KWP mode.
358 /// Key is intended for AES CMAC mode.
360 /// Key is intended for HMAC SHA256 mode.
363 /// Key is intended for HMAC SHA384 mode.
366 /// Key is intended for HMAC SHA512 mode.
369 /// Key is intended for KMAC128 mode.
372 /// Key is intended for KMAC256 mode.
375 /// Key is intended for RSA signature RSASSA-PKCS mode.
378 /// Key is intended for RSA signature RSASSA-PSS mode.
381 /// Key is intended for RSA encryption RSAES-OAEP mode.
384 /// Key is intended for ECDSA with P-256.
387 /// Key is intended for ECDSA with P-384.
390 /// Key is intended for ECDH with P-256.
393 /// Key is intended for ECDH with P-384.
396 /// Key is intended for Ed25519 mode.
399 /// Key is intended for X25519 mode.
402 /// Key is intended for KDF-CTR with HMAC as PRF.
405 /// Key is intended for KDF with KMAC128 as PRF.
408 /// Key is intended for KDF with KMAC256 as PRF.
411 kOtcryptoKeyModePqcMldsa87 =
412 kOtcryptoKeyTypePqc << 16 | kOtcryptoPqcKeyModeMldsa87,
414
415/**
416 * Enum to denote key security level.
417 *
418 * At high security levels, the crypto library will prioritize
419 * protecting the key from sophisticated attacks, even at large
420 * performance costs. If the security level is low, the crypto
421 * library will still try to protect the key, but may forgo the
422 * most costly protections against e.g. sophisticated physical
423 * attacks.
424 *
425 * Values are hardened.
426 */
428 /// Security level low.
430 /// Security level medium.
432 /// Security level high.
435
436/**
437 * Enum to denote the crypto library version.
438 *
439 * In future updates, this enum will be extended to preserve some
440 * level of backwards-compatibility despite changes to internal
441 * details (for example, the preferred masking scheme for blinded
442 * keys).
443 *
444 * Values are hardened.
445 */
450
451/**
452 * Struct to represent the configuration of a blinded key.
453 */
454typedef struct otcrypto_key_config {
455 /// Crypto library version for this key.
457 /// Mode for which the key usage is intended.
459 /// Length in bytes of the unblinded form of this key.
460 uint32_t key_length;
461 /// Whether the hardware key manager should produce this key.
462 /// If this is set to `true`, the keyblob must be exactly 8 words long, where
463 /// the first word is the version and the remaining 7 words are the salt.
465 /// Whether the key can be exported (always false if `hw_backed` is true).
467 /// Key security level.
470
471/**
472 * Maximum number of 32-bit words in a wrapped key.
473 *
474 * Sized for the largest supported key (RSA-4096 private key). Callers can use
475 * this to bound input buffers passed to `otcrypto_key_unwrap`.
476 */
477enum {
478 kOtcryptoWrappedKeyMaxWords =
479 /* key configuration struct */
480 sizeof(otcrypto_key_config_t) / sizeof(uint32_t) +
481 /* checksum and keyblob_length fields */
482 2 +
483 /* RSA-4096 keyblob: 2 shares of 512 bytes each */
484 2 * (4096 / 8 / sizeof(uint32_t)) +
485 /* AES-KWP 64-bit integrity prefix */
486 2,
487};
488
489/**
490 * Struct to handle unmasked key type.
491 */
493 /// Mode for which the key usage is intended.
495 /// Key length in bytes.
496 uint32_t key_length;
497 /// Implementation specific, storage provided by caller.
498 uint32_t *key;
499 /// Implementation specific, checksum for this struct.
500 uint32_t checksum;
502
503/**
504 * Struct to handle masked key type.
505 */
506typedef struct otcrypto_blinded_key {
507 /// Key configuration information.
509 /// Length of blinded key material in bytes.
510 const uint32_t keyblob_length;
511 /// Implementation specific, storage provided by caller.
512 uint32_t *keyblob;
513 /// Implementation specific, checksum for this struct.
514 uint32_t checksum;
516
517/**
518 * Enum to define supported hashing modes.
519 *
520 * Values are hardened.
521 */
522typedef enum otcrypto_hash_mode {
523 /// SHA2-256 mode.
525 /// SHA2-384 mode.
527 /// SHA2-512 mode.
529 /// SHA3-224 mode.
531 /// SHA3-256 mode.
533 /// SHA3-384 mode.
535 /// SHA3-512 mode.
537 /// Shake128 mode.
539 /// Shake256 mode.
541 /// cShake128 mode.
543 /// cShake256 mode.
546
547/**
548 * Container for a hash digest.
549 */
550typedef struct otcrypto_hash_digest {
551 /// Digest type.
553 /// Digest data.
554 uint32_t *data;
555 /// Digest length in 32-bit words.
556 size_t len;
558
559#ifdef __cplusplus
560} // extern "C"
561#endif // __cplusplus
562
563#endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_INCLUDE_DATATYPES_H_