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,
222
223/**
224 * Enum to specify the HMAC modes that use a key.
225 *
226 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
227 * for which the provided key is intended for.
228 *
229 * Values are hardened.
230 */
232 // Mode HMAC SHA256.
233 kOtcryptoHmacKeyModeSha256 = 0x7fd,
234 // Mode HMAC SHA384.
235 kOtcryptoHmacKeyModeSha384 = 0x43b,
236 // Mode HMAC SHA512.
237 kOtcryptoHmacKeyModeSha512 = 0x7a2,
239
240/**
241 * Enum to specify the KMAC modes that use a key.
242 *
243 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
244 * for which the provided key is intended for.
245 *
246 * Values are hardened.
247 */
249 // Mode KMAC128.
250 kOtcryptoKmacKeyModeKmac128 = 0xa56,
251 // Mode KMAC256.
252 kOtcryptoKmacKeyModeKmac256 = 0x663,
254
255/**
256 * Enum to specify the RSA modes that use a key.
257 *
258 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
259 * for which the provided key is intended for.
260 *
261 * Values are hardened.
262 */
264 // Mode RSA Sign, RSASSA-PKCS.
265 kOtcryptoRsaKeyModeSignPkcs = 0x3d4,
266 // Mode RSA Sign, RSASSA-PSS.
267 kOtcryptoRsaKeyModeSignPss = 0x761,
268 // Mode RSA Encrypt, RSAES-OAEP.
269 kOtcryptoRsaKeyModeEncryptOaep = 0x585,
271
272/**
273 * Enum to specify the ECC modes that use a key.
274 *
275 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
276 * for which the provided key is intended for.
277 *
278 * Values are hardened.
279 */
281 // Mode ECDSA/P-256.
282 kOtcryptoEccKeyModeEcdsaP256 = 0x31e,
283 // Mode ECDSA/P-384.
284 kOtcryptoEccKeyModeEcdsaP384 = 0x695,
285 // Mode ECDH/P-256.
286 kOtcryptoEccKeyModeEcdhP256 = 0x5fc,
287 // Mode ECDH/P-384.
288 kOtcryptoEccKeyModeEcdhP384 = 0x1c7,
289 // Mode Ed25519.
290 kOtcryptoEccKeyModeEd25519 = 0x663,
291 // Mode X25519.
292 kOtcryptoEccKeyModeX25519 = 0x0bb,
294
295/**
296 * Enum to specify the KDF modes that use a key.
297 *
298 * This will be used in the `otcrypto_key_mode_t` struct to indicate the mode
299 * for which the provided key is intended for.
300 *
301 * Values are hardened.
302 */
304 // Mode KDF-CTR with HMAC as PRF.
305 kOtcryptoKdfKeyModeCtrHmac = 0x12f,
306 // Mode KDF-KMAC with KMAC128 as PRF.
307 kOtcryptoKdfKeyModeKmac128 = 0xe5e,
308 // Mode KDF-KMAC with KMAC256 as PRF.
309 kOtcryptoKdfKeyModeKmac256 = 0x353,
311
312/**
313 * Enum for opentitan crypto modes that use a key.
314 *
315 * Denotes the crypto mode for which the provided key is to be used.
316 * This `otcrypto_key_mode_t` will be a parameter in the
317 * `otcrypto_blinded_key_t` and `otcrypto_unblinded_key_t` structs.
318 *
319 * Values are hardened.
320 */
321typedef enum otcrypto_key_mode {
322 // Key is intended for AES ECB mode.
323 kOtcryptoKeyModeAesEcb = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeEcb,
324 // Key is intended for AES CBC mode.
325 kOtcryptoKeyModeAesCbc = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeCbc,
326 // Key is intended for AES CFB mode.
327 kOtcryptoKeyModeAesCfb = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeCfb,
328 // Key is intended for AES OFB mode.
329 kOtcryptoKeyModeAesOfb = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeOfb,
330 // Key is intended for AES CTR mode.
331 kOtcryptoKeyModeAesCtr = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeCtr,
332 // Key is intended for AES GCM mode.
333 kOtcryptoKeyModeAesGcm = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeGcm,
334 // Key is intended for AES KWP mode.
335 kOtcryptoKeyModeAesKwp = kOtcryptoKeyTypeAes << 16 | kOtcryptoAesKeyModeKwp,
336 // Key is intended for HMAC SHA256 mode.
337 kOtcryptoKeyModeHmacSha256 =
338 kOtcryptoKeyTypeHmac << 16 | kOtcryptoHmacKeyModeSha256,
339 // Key is intended for HMAC SHA384 mode.
340 kOtcryptoKeyModeHmacSha384 =
341 kOtcryptoKeyTypeHmac << 16 | kOtcryptoHmacKeyModeSha384,
342 // Key is intended for HMAC SHA512 mode.
343 kOtcryptoKeyModeHmacSha512 =
344 kOtcryptoKeyTypeHmac << 16 | kOtcryptoHmacKeyModeSha512,
345 // Key is intended for KMAC128 mode.
346 kOtcryptoKeyModeKmac128 =
347 kOtcryptoKeyTypeKmac << 16 | kOtcryptoKmacKeyModeKmac128,
348 // Key is intended for KMAC256 mode.
349 kOtcryptoKeyModeKmac256 =
350 kOtcryptoKeyTypeKmac << 16 | kOtcryptoKmacKeyModeKmac256,
351 // Key is intended for RSA signature RSASSA-PKCS mode.
352 kOtcryptoKeyModeRsaSignPkcs =
353 kOtcryptoKeyTypeRsa << 16 | kOtcryptoRsaKeyModeSignPkcs,
354 // Key is intended for RSA signature RSASSA-PSS mode.
355 kOtcryptoKeyModeRsaSignPss =
356 kOtcryptoKeyTypeRsa << 16 | kOtcryptoRsaKeyModeSignPss,
357 // Key is intended for RSA encryption RSAES-OAEP mode.
358 kOtcryptoKeyModeRsaEncryptOaep =
359 kOtcryptoKeyTypeRsa << 16 | kOtcryptoRsaKeyModeEncryptOaep,
360 // Key is intended for ECDSA with P-256.
361 kOtcryptoKeyModeEcdsaP256 =
362 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEcdsaP256,
363 // Key is intended for ECDSA with P-384.
364 kOtcryptoKeyModeEcdsaP384 =
365 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEcdsaP384,
366 // Key is intended for ECDH with P-256.
367 kOtcryptoKeyModeEcdhP256 =
368 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEcdhP256,
369 // Key is intended for ECDH with P-384.
370 kOtcryptoKeyModeEcdhP384 =
371 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEcdhP384,
372 // Key is intended for Ed25519 mode.
373 kOtcryptoKeyModeEd25519 =
374 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeEd25519,
375 // Key is intended for X25519 mode.
376 kOtcryptoKeyModeX25519 =
377 kOtcryptoKeyTypeEcc << 16 | kOtcryptoEccKeyModeX25519,
378 // Key is intended for KDF-CTR with HMAC as PRF.
379 kOtcryptoKeyModeKdfCtrHmac =
380 kOtcryptoKeyTypeKdf << 16 | kOtcryptoKdfKeyModeCtrHmac,
381 // Key is intended for KDF with KMAC128 as PRF.
382 kOtcryptoKeyModeKdfKmac128 =
383 kOtcryptoKeyTypeKdf << 16 | kOtcryptoKdfKeyModeKmac128,
384 // Key is intended for KDF with KMAC256 as PRF.
385 kOtcryptoKeyModeKdfKmac256 =
386 kOtcryptoKeyTypeKdf << 16 | kOtcryptoKdfKeyModeKmac256,
388
389/**
390 * Enum to denote key security level.
391 *
392 * At high security levels, the crypto library will prioritize
393 * protecting the key from sophisticated attacks, even at large
394 * performance costs. If the security level is low, the crypto
395 * library will still try to protect the key, but may forgo the
396 * most costly protections against e.g. sophisticated physical
397 * attacks.
398 *
399 * Values are hardened.
400 */
402 // Security level low.
403 kOtcryptoKeySecurityLevelLow = 0x1e9,
404 // Security level medium.
405 kOtcryptoKeySecurityLevelMedium = 0xeab,
406 // Security level high.
407 kOtcryptoKeySecurityLevelHigh = 0xa7e,
409
410/**
411 * Enum to denote the crypto library version.
412 *
413 * In future updates, this enum will be extended to preserve some
414 * level of backwards-compatibility despite changes to internal
415 * details (for example, the preferred masking scheme for blinded
416 * keys).
417 *
418 * Values are hardened.
419 */
421 // Version 1.
422 kOtcryptoLibVersion1 = 0x7f4,
424
425/**
426 * Struct to represent the configuration of a blinded key.
427 */
428typedef struct otcrypto_key_config {
429 // Crypto library version for this key.
431 // Mode for which the key usage is intended.
432 otcrypto_key_mode_t key_mode;
433 // Length in bytes of the unblinded form of this key.
434 uint32_t key_length;
435 // Whether the hardware key manager should produce this key.
436 // If this is set to `true`, the keyblob must be exactly 8 words long, where
437 // the first word is the version and the remaining 7 words are the salt.
438 hardened_bool_t hw_backed;
439 // Whether the key can be exported (always false if `hw_backed` is true).
440 hardened_bool_t exportable;
441 // Key security level.
442 otcrypto_key_security_level_t security_level;
444
445/**
446 * Struct to handle unmasked key type.
447 */
449 // Mode for which the key usage is intended.
450 otcrypto_key_mode_t key_mode;
451 // Key length in bytes.
452 uint32_t key_length;
453 // Implementation specific, storage provided by caller.
454 uint32_t *key;
455 // Implementation specific, checksum for this struct.
456 uint32_t checksum;
458
459/**
460 * Struct to handle masked key type.
461 */
462typedef struct otcrypto_blinded_key {
463 // Key configuration information.
464 const otcrypto_key_config_t config;
465 // Length of blinded key material in bytes.
466 const uint32_t keyblob_length;
467 // Implementation specific, storage provided by caller.
468 uint32_t *keyblob;
469 // Implementation specific, checksum for this struct.
470 uint32_t checksum;
472
473/**
474 * Enum to define supported hashing modes.
475 *
476 * Values are hardened.
477 */
478typedef enum otcrypto_hash_mode {
479 // SHA2-256 mode.
480 kOtcryptoHashModeSha256 = 0x69b,
481 // SHA2-384 mode.
482 kOtcryptoHashModeSha384 = 0x7ae,
483 // SHA2-512 mode.
484 kOtcryptoHashModeSha512 = 0x171,
485 // SHA3-224 mode.
486 kOtcryptoHashModeSha3_224 = 0x516,
487 // SHA3-256 mode.
488 kOtcryptoHashModeSha3_256 = 0x2d4,
489 // SHA3-384 mode.
490 kOtcryptoHashModeSha3_384 = 0x267,
491 // SHA3-512 mode.
492 kOtcryptoHashModeSha3_512 = 0x44d,
493 // Shake128 mode.
494 kOtcryptoHashXofModeShake128 = 0x5d8,
495 // Shake256 mode.
496 kOtcryptoHashXofModeShake256 = 0x34a,
497 // cShake128 mode.
498 kOtcryptoHashXofModeCshake128 = 0x0bd,
499 // cShake256 mode.
500 kOtcryptoHashXofModeCshake256 = 0x4e2,
502
503/**
504 * Container for a hash digest.
505 */
506typedef struct otcrypto_hash_digest {
507 // Digest type.
509 // Digest data.
510 uint32_t *data;
511 // Digest length in 32-bit words.
512 size_t len;
514
515#ifdef __cplusplus
516} // extern "C"
517#endif // __cplusplus
518
519#endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_INCLUDE_DATATYPES_H_