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