Software APIs
dif_aes.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_DIF_DIF_AES_H_
6#define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_AES_H_
7
8#include <stdbool.h>
9#include <stdint.h>
10
14
15#include "sw/device/lib/dif/autogen/dif_aes_autogen.h"
16
17#ifdef __cplusplus
18extern "C" {
19#endif // __cplusplus
20
21/**
22 *
23 * @file
24 * @brief <a href="/book/hw/ip/aes/">AES</a> Device Interface Functions
25 *
26 * This API assumes transactional nature of work, where the peripheral is
27 * configured once per message (data consisting of 1..N 128-bit blocks), and
28 * then "de-initialised" when this message has been fully encrypted/decrypted.
29 *
30 * The peripheral is configured through one of the cipher mode "start"
31 * functions:
32 * `dif_aes_start_ecb`, `dif_aes_start_cbc`, ... .
33 *
34 * Then the encryption/decryption data is fed one 128-bit block at the
35 * time through `dif_aes_load_data` function. The cipher mode operation details
36 * are described in the description of above mentioned "start" functions. When
37 * configured in "automatic" operation mode, every "load data" call, will
38 * trigger encryption/decryption. This is not true when in "manual" operation
39 * mode, where encryption/decryption is triggered by explicitly setting the
40 * `aes.TRIGGER.START` flag through `dif_aes_trigger` call.
41 *
42 * When an entire requested message has been processed the internal state of
43 * AES registers must be securely cleared, by calling `dif_aes_end`.
44 *
45 * Please see the following documentation for further information:
46 * https://opentitan.org/book/hw/ip/aes/
47 * https://csrc.nist.gov/csrc/media/publications/fips/197/final/documents/fips-197.pdf
48 * https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf
49 */
50
51/**
52 * A typed representation of the AES key share.
53 *
54 * Two part masked AES key, where XOR operation of these two parts results in
55 * the actual key.
56 */
57typedef struct dif_aes_key_share {
58 /**
59 * One share of the key that when XORed with `share1` results in the actual
60 * key.
61 */
62 uint32_t share0[8];
63 /**
64 * One share of the key that when XORed with `share0` results in the actual
65 * key.
66 */
67 uint32_t share1[8];
69
70/**
71 * A typed representation of the AES Initialisation Vector (IV).
72 */
73typedef struct dif_aes_iv {
74 uint32_t iv[4];
76
77/**
78 * A typed representation of the AES data.
79 */
80typedef struct dif_aes_data {
81 uint32_t data[4];
83
84/**
85 * AES operation.
86 */
87typedef enum dif_aes_operation {
88 /**
89 * AES encryption.
90 */
92 /**
93 * AES decryption.
94 */
97
98/**
99 * AES block cipher mode of operation.
100 */
101typedef enum dif_aes_mode {
102 /**
103 * The Electronic Codebook Mode.
104 * In ECB cipher mode the key must be changed for every new block of data.
105 * This is the only secure way to use ECB cipher mode.
106 *
107 * Note: The ECB cipher mode doesn't use the iv parameter of the
108 * `dif_aes_start` function.
109 *
110 * Note: it is discouraged to use this cipher mode, due to impractical amount
111 * of different keys required to encrypt/decrypt multi-block messages.
112 */
114
115 /**
116 * The Cipher Block Chaining Mode.
117 *
118 * In CBC cipher mode, the same key can be used for all messages, however
119 * new Initialisation Vector (IV) must be generated for any new message. The
120 * following condition must be true:
121 * The IV must be unpredictable (it must not be possible to predict the IV
122 * that will be associated to the plaintext in advance of the generation
123 * of the IV).
124 *
125 * With key length less than 256 bits, the excess portion of the `key` can be
126 * written with any data (preferably random).
127 */
128 kDifAesModeCbc = (1 << 1),
129
130 /**
131 * The Cipher Feedback Mode.
132 *
133 * In CFB cipher mode, the same key can be used for all messages, however
134 * new Initialisation Vector (IV) must be generated for any new message. The
135 * following condition must be true:
136 * The IV must be unpredictable (it must not be possible to predict the IV
137 * that will be associated to the plaintext in advance of the generation
138 * of the IV).
139 *
140 * With key length less than 256 bits, the excess portion of the `key` can be
141 * written with any data (preferably random).
142 */
143 kDifAesModeCfb = (1 << 2),
144
145 /**
146 * The Output Feedback Mode.
147 *
148 * In OFB cipher mode, the same key can be used for all messages, and the
149 * Initialization Vector (IV) need NOT be unpredictable. The following
150 * conditions must be true:
151 * OFB mode requires a unique initialization vector for every message that
152 * is ever encrypted under a given key, across all messages.
153 *
154 * With key length less than 256 bits, the excess portion of the `key` can be
155 * written with any data (preferably random).
156 */
157 kDifAesModeOfb = (1 << 3),
158
159 /**
160 * The Counter Mode.
161 *
162 * In CTR cipher mode, the same key can be used for all messages, if the
163 * following condition is true:
164 * CTR mode requires a unique counter block for each plaintext block that
165 * is ever encrypted under a given key, across all messages.
166 *
167 * With key length less than 256 bits, the excess portion of the `key` can be
168 * written with any data (preferably random).
169 */
170 kDifAesModeCtr = (1 << 4),
171
172 /**
173 * The Galois/Counter Mode.
174 */
175 kDifAesModeGcm = (1 << 5),
177
178/**
179 * AES key length in bits.
180 */
181typedef enum dif_aes_key_length {
182 /**
183 * 128 bit wide AES key.
184 */
186 /**
187 * 192 bit wide AES key.
188 */
189 kDifAesKey192 = (1 << 1),
190 /**
191 * 256 bit wide AES key.
192 */
193 kDifAesKey256 = (1 << 2)
195
196/**
197 * AES manual operation.
198 */
200 /**
201 * AES operates in automatic mode - which means that the encryption/decryption
202 * is automatically triggered on every successful `dif_aes_*_load_data()`.
203 */
205 /**
206 * AES operates in manual mode - which means that the encryption/decryption
207 * is manually triggered by `dif_aes_trigger(kDifAesTriggerStart)`.
208 */
211
212/**
213 * AES key sideloaded.
214 *
215 * Controls whether the AES uses the key provided by the key manager or
216 * software.
217 */
219 /**
220 * The key is provided by software via `dif_aes_key_share_t`.
221 */
223 /**
224 * The key be provided by the key manager.
225 */
228
229/**
230 * AES reseeding rate
231 *
232 * Controls the reseeding rate of the internal pseudo-random number generator
233 * (PRNG) used for masking.
234 */
236 /**
237 * The masking PRNG will be reseed every block.
238 */
240 /**
241 * The masking PRNG will be reseed every 64 blocks.
242 */
244 /**
245 * The masking PRNG will be reseed every 8192 blocks.
246 */
249
250/**
251 * Parameters for an AES transaction.
252 */
253typedef struct dif_aes_transaction {
254 dif_aes_operation_t operation;
255 dif_aes_mode_t mode;
256 dif_aes_key_length_t key_len;
257 dif_aes_manual_operation_t manual_operation;
258 dif_aes_key_provider_t key_provider;
259 dif_aes_mask_reseeding_t mask_reseeding;
260 /**
261 * If true, the internal pseudo-random number generators used for clearing and
262 * masking will be reseeded every time the key changes.
263 */
265 /**
266 * If true, the internal pseudo-random number generator used for masking is
267 * not advancing leading to constant masks.
268 *
269 * NOTE: This should only be used for development purpose (SCA), and is
270 * expected to be removed for the production version.
271 */
273 /**
274 * If true, the module will continuously run encryption/decryption when
275 * configured in manual mode.
276 *
277 * NOTE: This should only be used for power virus testing.
278 */
280 /**
281 * If true, `reseed_on_key_change`, `force_masks` and
282 * `keep_start_trigger_high` will be locked until the device is reset.
283 */
286
287/**
288 * Resets an instance of AES.
289 *
290 * Clears the internal state along with the interface registers.
291 *
292 * @param aes AES state data.
293 * @return The result of the operation.
294 */
297
298/**
299 * Begins an AES transaction in the mode selected by the `transaction->mode`.
300 *
301 * Each call to this function should be sequenced with a call to
302 * `dif_aes_end()`.
303 *
304 * The peripheral must be in IDLE state for this operation to take effect, and
305 * will return `kDifAesBusy` if this condition is not met.
306 *
307 * @param aes AES state data.
308 * @param transaction Configuration data.
309 * @param key Encryption/decryption key when `kDifAesKeySoftwareProvided`, can
310 * be `NULL` otherwise.
311 * @param iv Initialization vector when the mode isn't `kDifAesModeEcb`, can be
312 * `NULL` otherwise.
313 * @return The result of the operation.
314 */
317 const dif_aes_transaction_t *transaction,
318 const dif_aes_key_share_t *key,
319 const dif_aes_iv_t *iv);
320/**
321 * Ends an AES transaction.
322 *
323 * This function must be called at the end of every `dif_aes_<mode>_start`
324 * operation.
325 *
326 * The peripheral must be in IDLE state for this operation to take effect, and
327 * will return `kDifAesEndBusy` if this condition is not met.
328 *
329 * @param aes AES state data.
330 * @return The result of the operation.
331 */
334
335/**
336 * Loads AES Input Data.
337 *
338 * This function will trigger encryption/decryption when configured in
339 * the automatic operation mode.
340 *
341 * The peripheral must be able to accept the input (INPUT_READY set), and
342 * will return `kDifAesLoadDataBusy` if this condition is not met.
343 *
344 * @param aes AES state data.
345 * @param data AES Input Data.
346 * @return The result of the operation.
347 */
350
351/**
352 * Reads AES Output Data.
353 *
354 * The peripheral must have finished previous encryption/decryption operation,
355 * and have valid data in the output registers (OUTPUT_VALID set), and will
356 * return `kDifAesReadOutputInvalid` if this condition is not met.
357 *
358 * @param aes AES state data.
359 * @param data AES Output Data.
360 * @return The result of the operation.
361 */
364
365/**
366 * Puts the AES GCM into the provided phase.
367 *
368 * @param aes AES state data.
369 * @param phase The AES GCM phase we want to enter.
370 * @param num_valid_bytes The number of valid bytes for the AES block.
371 * @return The result of the operation.
372 */
374dif_result_t dif_aes_set_gcm_phase(const dif_aes_t *aes, uint32_t phase,
375 size_t num_valid_bytes);
376
377/**
378 * Loads len(aad) || len(ptx) into the AES-GCM.
379 *
380 * @param aes AES state data.
381 * @param len_ptx Number of plaintext bits.
382 * @param len_aad Number of AAD bits.
383 */
385dif_result_t dif_aes_load_gcm_tag_len(const dif_aes_t *aes, uint64_t len_ptx,
386 uint64_t len_aad);
387
388/**
389 * Process a stream of data containing the plain text and output a stream of
390 * data with the cipher text.
391 *
392 * This function should be used when performance is desired. It requires the
393 * automatic operation mode activated.
394 *
395 * The peripheral must be able to accept the input (INPUT_READY set), and
396 * will return `kDifAesLoadDataBusy` if this condition is not met.
397 *
398 * @param aes AES handle.
399 * @param plain_text AES Input Data.
400 * @param cipher_text AES Output Data.
401 * @param block_amount The amount of blocks to be encrypted.
402 * @return The result of the operation.
403 */
406 const dif_aes_data_t *plain_text,
407 dif_aes_data_t *cipher_text,
408 size_t block_amount);
409
410/**
411 * AES Trigger flags.
412 */
413typedef enum dif_aes_trigger {
414 /**
415 * Trigger encrypt/decrypt.
416 */
418 /**
419 * Clear key, Initialisation Vector/Initial Counter Value and input data.
420 */
422 /**
423 * Clear Output Data registers.
424 */
426 /**
427 * Perform reseed of the internal state.
428 */
431
432/**
433 * Triggers one of `dif_aes_trigger_t` operations.
434 *
435 * All the triggers are applicable to both (automatic and manual) modes, with
436 * the exception of `kDifAesTriggerStart`, which is ignored in automatic mode.
437 *
438 * @param aes AES state data.
439 * @param trigger AES trigger.
440 * @return The result of the operation.
441 */
444
445/**
446 * AES Status flags.
447 */
448typedef enum dif_aes_status {
449 /**
450 * Device is idle.
451 */
453 /**
454 * Device has stalled (only relevant in automatic
455 * operation mode). Output data overwrite
456 * protection.
457 */
459 /**
460 * Output data has been overwritten by the AES unit before the processor
461 * could fully read it. This bit is "sticky" for the entire duration of
462 * the current transaction.
463 */
465 /**
466 * Device output is valid/ready. Denotes a
467 * successful encrypt or decrypt operation.
468 */
470 /**
471 * Device Input Data registers can be written to
472 * (ready to accept new input data).
473 */
475 /**
476 * Fatal alert conditions include i) storage errors in the Control Register,
477 * and ii) if any internal FSM enters an invalid state.
478 */
480 /**
481 * Recoverable alert conditions include update errors in the Control Register.
482 */
485
486/**
487 * Queries the AES status flags.
488 *
489 * @param aes AES state data.
490 * @param flag Status flag to query.
491 * @param[out] set Flag state (set/unset).
492 * @return The result of the operation.
493 */
496 bool *set);
497
498/**
499 * Read the current initialization vector from its register.
500 *
501 * @param aes AES handle.
502 * @param iv The pointer to receive the initialization vector.
503 * @return The result of the operation.
504 */
507
508/**
509 * Loads AES IV.
510 *
511 * This function will load the provided IV into the AES.
512 *
513 * The peripheral must be idle and will return `kDifUnavailable` if this
514 * condition is not met.
515 *
516 * @param aes AES state data.
517 * @param data AES Input Data.
518 * @return The result of the operation.
519 */
522
523#ifdef __cplusplus
524} // extern "C"
525#endif // __cplusplus
526
527#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_AES_H_