Software APIs
aes.c
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 
6 
11 #include "sw/device/lib/crypto/drivers/aes.h"
12 #include "sw/device/lib/crypto/drivers/keymgr.h"
13 #include "sw/device/lib/crypto/impl/aes_gcm/aes_gcm.h"
14 #include "sw/device/lib/crypto/impl/aes_gcm/ghash.h"
15 #include "sw/device/lib/crypto/impl/aes_kwp/aes_kwp.h"
16 #include "sw/device/lib/crypto/impl/integrity.h"
17 #include "sw/device/lib/crypto/impl/keyblob.h"
18 #include "sw/device/lib/crypto/impl/status.h"
20 
21 // Module ID for status codes.
22 #define MODULE_ID MAKE_MODULE_ID('a', 'e', 's')
23 
24 // Check that cipher mode enum from AES driver matches the one from the
25 // top-level API.
26 OT_ASSERT_ENUM_VALUE(kAesCipherModeEcb, (uint32_t)kOtcryptoAesModeEcb);
27 OT_ASSERT_ENUM_VALUE(kAesCipherModeCbc, (uint32_t)kOtcryptoAesModeCbc);
28 OT_ASSERT_ENUM_VALUE(kAesCipherModeCfb, (uint32_t)kOtcryptoAesModeCfb);
29 OT_ASSERT_ENUM_VALUE(kAesCipherModeOfb, (uint32_t)kOtcryptoAesModeOfb);
30 OT_ASSERT_ENUM_VALUE(kAesCipherModeCtr, (uint32_t)kOtcryptoAesModeCtr);
31 
32 // Check GHASH context size against the underlying implementation.
33 static_assert(sizeof(otcrypto_aes_gcm_context_t) >= sizeof(aes_gcm_context_t),
34  "Size of AES-GCM context object for top-level API must be at "
35  "least as large as the context for the "
36  "underlying implementation.");
37 static_assert(alignof(aes_gcm_context_t) >= alignof(uint32_t),
38  "Internal AES-GCM context object must be word-aligned for use "
39  "with `hardened_memcpy`.");
40 static_assert(sizeof(otcrypto_key_config_t) % sizeof(uint32_t) == 0,
41  "Key configuration size should be a multiple of 32 bits");
42 
43 // Ensure the internal AES-GCM context size is a multiple of the word size and
44 // calculate the number of words.
45 static_assert(sizeof(aes_gcm_context_t) % sizeof(uint32_t) == 0,
46  "Internal AES-GCM context object must be a multiple of the word "
47  "size for use with `hardened_memcpy`.");
48 enum {
49  kAesGcmContextNumWords = sizeof(aes_gcm_context_t) / sizeof(uint32_t),
50 };
51 
52 /**
53  * Save an AES-GCM context.
54  *
55  * @param internal_ctx Internal context object to save.
56  * @param[out] api_ctx Resulting API-facing context object.
57  */
58 static inline void gcm_context_save(aes_gcm_context_t *internal_ctx,
59  otcrypto_aes_gcm_context_t *api_ctx) {
60  hardened_memcpy(api_ctx->data, (uint32_t *)internal_ctx,
61  kAesGcmContextNumWords);
62 }
63 
64 /**
65  * Restore an AES-GCM context.
66  *
67  * @param api_ctx API-facing context object to restore from.
68  * @param[out] internal_ctx Resulting internal context object.
69  */
70 static inline void gcm_context_restore(otcrypto_aes_gcm_context_t *api_ctx,
71  aes_gcm_context_t *internal_ctx) {
72  hardened_memcpy((uint32_t *)internal_ctx, api_ctx->data,
73  kAesGcmContextNumWords);
74 }
75 
76 /**
77  * Extract an AES key from the blinded key struct.
78  *
79  * Also performs integrity, mode, and null-pointer checks on the key. This
80  * function is only for basic AES modes; do not use for AES-GCM or AES-KWP keys
81  * since they will fail the mode check.
82  *
83  * @param blinded_key Blinded key struct.
84  * @param aes_mode Block cipher mode.
85  * @param[out] aes_key Destination AES key struct.
86  * @return Result of the operation.
87  */
88 static status_t aes_key_construct(const otcrypto_blinded_key_t *blinded_key,
89  const otcrypto_aes_mode_t aes_mode,
90  aes_key_t *aes_key) {
91  // Key integrity check.
92  if (launder32(integrity_blinded_key_check(blinded_key)) !=
94  return OTCRYPTO_BAD_ARGS;
95  }
96  HARDENED_CHECK_EQ(integrity_blinded_key_check(blinded_key),
98 
99  if (blinded_key->config.hw_backed == kHardenedBoolTrue) {
100  // Call keymgr to sideload the key into AES.
101  keymgr_diversification_t diversification;
102  HARDENED_TRY(
103  keyblob_to_keymgr_diversification(blinded_key, &diversification));
104  HARDENED_TRY(keymgr_generate_key_aes(diversification));
105  aes_key->key_shares[0] = NULL;
106  aes_key->key_shares[1] = NULL;
107  } else if (blinded_key->config.hw_backed == kHardenedBoolFalse) {
108  // Get pointers to the individual shares.
109  uint32_t *share0;
110  uint32_t *share1;
111  HARDENED_TRY(keyblob_to_shares(blinded_key, &share0, &share1));
112  aes_key->key_shares[0] = share0;
113  aes_key->key_shares[1] = share1;
114  } else {
115  return OTCRYPTO_BAD_ARGS;
116  }
117  aes_key->sideload = blinded_key->config.hw_backed;
118 
119  // Check for null pointer.
120  if (blinded_key->keyblob == NULL) {
121  return OTCRYPTO_BAD_ARGS;
122  }
123 
124  // Set the block cipher mode based on the key mode.
125  switch (blinded_key->config.key_mode) {
126  case kOtcryptoKeyModeAesEcb:
127  aes_key->mode = kAesCipherModeEcb;
128  break;
129  case kOtcryptoKeyModeAesCbc:
130  aes_key->mode = kAesCipherModeCbc;
131  break;
132  case kOtcryptoKeyModeAesCfb:
133  aes_key->mode = kAesCipherModeCfb;
134  break;
135  case kOtcryptoKeyModeAesOfb:
136  aes_key->mode = kAesCipherModeOfb;
137  break;
138  case kOtcryptoKeyModeAesCtr:
139  aes_key->mode = kAesCipherModeCtr;
140  break;
141  default:
142  return OTCRYPTO_BAD_ARGS;
143  }
144 
145  // Check that the key mode matches the requested block cipher mode.
146  if (memcmp(&aes_key->mode, &aes_mode, sizeof(aes_key->mode)) != 0) {
147  return OTCRYPTO_BAD_ARGS;
148  }
149  HARDENED_CHECK_EQ(aes_key->mode, aes_mode);
150 
151  // Set the AES key length (in words).
152  aes_key->key_len = keyblob_share_num_words(blinded_key->config);
153 
154  return OTCRYPTO_OK;
155 }
156 
157 /**
158  * Applies the specified mode of AES padding to the block.
159  *
160  * Modifies only positions on and after index `last_block_len` in the
161  * `padded_block`; real input may be written to the initial locations either
162  * before or after calling this function.
163  *
164  * @param padding_mode Padding mode.
165  * @param partial_data_len Length of real input in this block (may be 0).
166  * @param[out] padded_block Destination padded block.
167  * @return Result of the operation.
168  */
169 static status_t aes_padding_apply(otcrypto_aes_padding_t padding_mode,
170  const size_t partial_data_len,
171  aes_block_t *block) {
172  if (partial_data_len >= kAesBlockNumBytes) {
173  return OTCRYPTO_BAD_ARGS;
174  }
175 
176  // Get a byte-sized pointer to the padding start point within the block's
177  // data buffer.
178  char *padding = ((char *)block->data) + partial_data_len;
179 
180  // Pad a full block if the last block is full, otherwise just fill the last
181  // block.
182  size_t padding_len = kAesBlockNumBytes - partial_data_len;
183  hardened_bool_t padding_written = kHardenedBoolFalse;
184  switch (launder32(padding_mode)) {
185  case kOtcryptoAesPaddingPkcs7:
186  // Pads with value same as the number of padding bytes.
187  memset(padding, (uint8_t)padding_len, padding_len);
188  padding_written = kHardenedBoolTrue;
189  break;
190  case kOtcryptoAesPaddingIso9797M2:
191  // Pads with 0x80 (0b10000000), followed by zero bytes.
192  memset(padding, 0x0, padding_len);
193  padding[0] = 0x80;
194  padding_written = kHardenedBoolTrue;
195  break;
196  case kOtcryptoAesPaddingNull:
197  // This routine should not be called if padding is not needed.
198  return OTCRYPTO_RECOV_ERR;
199  default:
200  // Unrecognized padding mode.
201  return OTCRYPTO_BAD_ARGS;
202  }
203  HARDENED_CHECK_EQ(padding_written, kHardenedBoolTrue);
204 
205  return OTCRYPTO_OK;
206 }
207 
208 /**
209  * Return the number of blocks for the input and padding mode.
210  *
211  * @param plaintext_len Length of plaintext in bytes.
212  * @param padding Padding mode.
213  * @returns Number of AES blocks required.
214  */
215 static status_t num_padded_blocks_get(size_t plaintext_len,
216  otcrypto_aes_padding_t padding,
217  size_t *num_blocks) {
218  size_t num_full_blocks = plaintext_len / kAesBlockNumBytes;
219 
220  if (padding == kOtcryptoAesPaddingNull) {
221  // If no padding mode is given, the last block must be full.
222  if (num_full_blocks * kAesBlockNumBytes != plaintext_len) {
223  return OTCRYPTO_BAD_ARGS;
224  }
225 
226  *num_blocks = num_full_blocks;
227  return OTCRYPTO_OK;
228  }
229  HARDENED_CHECK_NE(padding, kOtcryptoAesPaddingNull);
230 
231  // For non-null padding modes, we append a full block of padding if the last
232  // block is full, so the value is always <full blocks> + 1.
233  *num_blocks = num_full_blocks + 1;
234  return OTCRYPTO_OK;
235 }
236 
237 /**
238  * Return the block at index i for the given input and padding mode.
239  *
240  * @param input Input data buffer.
241  * @param padding Padding mode.
242  * @param index Block index.
243  * @param[out] AES block at `index`.
244  * @param input_len Length of cipher input in bytes.
245  * @param padding Padding mode.
246  * @returns Number of AES blocks required.
247  */
248 static status_t get_block(otcrypto_const_byte_buf_t input,
249  otcrypto_aes_padding_t padding, size_t index,
250  aes_block_t *block) {
251  size_t num_full_blocks = input.len / kAesBlockNumBytes;
252 
253  // The index should never be more than `num_full_blocks` + 1, even including
254  // padding.
255  HARDENED_CHECK_LE(index, num_full_blocks + 1);
256 
257  if (launder32(index) < num_full_blocks) {
258  HARDENED_CHECK_LT(index, num_full_blocks);
259  // No need to worry about padding, just copy the data into the output
260  // block.
261  // TODO(#17711) Change to `hardened_memcpy`.
262  memcpy(block->data, &input.data[index * kAesBlockNumBytes],
263  kAesBlockNumBytes);
264  return OTCRYPTO_OK;
265  }
266  HARDENED_CHECK_GE(index, num_full_blocks);
267 
268  // If we get here, this block is the one with padding. It may be a partial
269  // block or an empty block that will be entirely filled with padded bytes.
270  size_t partial_data_len = input.len % kAesBlockNumBytes;
271  memcpy(block->data, &input.data[index * kAesBlockNumBytes], partial_data_len);
272 
273  // Apply padding.
274  HARDENED_TRY(aes_padding_apply(padding, partial_data_len, block));
275  return OTCRYPTO_OK;
276 }
277 
279  size_t plaintext_len, otcrypto_aes_padding_t aes_padding,
280  size_t *padded_len) {
281  size_t padded_nblocks;
282  HARDENED_TRY(
283  num_padded_blocks_get(plaintext_len, aes_padding, &padded_nblocks));
284  *padded_len = padded_nblocks * kAesBlockNumBytes;
285  return OTCRYPTO_OK;
286 }
287 
290  otcrypto_aes_mode_t aes_mode,
291  otcrypto_aes_operation_t aes_operation,
292  otcrypto_const_byte_buf_t cipher_input,
293  otcrypto_aes_padding_t aes_padding,
294  otcrypto_byte_buf_t cipher_output) {
295  // Check for NULL pointers in input pointers and data buffers.
296  if (key == NULL || (aes_mode != kOtcryptoAesModeEcb && iv.data == NULL) ||
297  cipher_input.data == NULL || cipher_output.data == NULL) {
298  return OTCRYPTO_BAD_ARGS;
299  }
300 
301  // Calculate the number of blocks for the input, including the padding for
302  // encryption.
303  size_t input_nblocks;
304  if (aes_operation == kOtcryptoAesOperationEncrypt) {
305  HARDENED_TRY(
306  num_padded_blocks_get(cipher_input.len, aes_padding, &input_nblocks));
307  } else if (aes_operation == kOtcryptoAesOperationDecrypt) {
308  // If the operation is decryption, the input length must be divisible by
309  // the block size.
310  if (launder32(cipher_input.len) % kAesBlockNumBytes != 0) {
311  return OTCRYPTO_BAD_ARGS;
312  }
313  HARDENED_CHECK_EQ(cipher_input.len % kAesBlockNumBytes, 0);
314  input_nblocks = cipher_input.len / kAesBlockNumBytes;
315  }
316 
317  // Check input/output lengths.
318  // - Input length must be nonzero.
319  // - Output length must match number of input blocks.
320  if (cipher_input.len == 0 ||
321  launder32(cipher_output.len) != input_nblocks * kAesBlockNumBytes) {
322  return OTCRYPTO_BAD_ARGS;
323  }
324  HARDENED_CHECK_EQ(cipher_output.len, input_nblocks * kAesBlockNumBytes);
325 
326  // Construct the IV and check its length. ECB mode will ignore the IV, so in
327  // this case it is left uninitialized.
328  aes_block_t aes_iv;
329  if (aes_mode == launder32(kAesCipherModeEcb)) {
330  HARDENED_CHECK_EQ(aes_mode, kAesCipherModeEcb);
331  } else {
332  HARDENED_CHECK_NE(aes_mode, kAesCipherModeEcb);
333 
334  // The IV must be exactly one block long.
335  if (launder32(iv.len) != kAesBlockNumWords) {
336  return OTCRYPTO_BAD_ARGS;
337  }
338  HARDENED_CHECK_EQ(iv.len, kAesBlockNumWords);
339  hardened_memcpy(aes_iv.data, iv.data, kAesBlockNumWords);
340  }
341 
342  // Parse the AES key.
344  HARDENED_TRY(aes_key_construct(key, aes_mode, &aes_key));
345 
346  // Start the operation (encryption or decryption).
347  switch (aes_operation) {
348  case kOtcryptoAesOperationEncrypt:
349  HARDENED_TRY(aes_encrypt_begin(aes_key, &aes_iv));
350  break;
351  case kOtcryptoAesOperationDecrypt:
352  HARDENED_TRY(aes_decrypt_begin(aes_key, &aes_iv));
353  break;
354  default:
355  return OTCRYPTO_BAD_ARGS;
356  }
357 
358  // Perform the cipher operation for all full blocks. The input and output are
359  // offset by `block_offset` number of blocks, where `block_offset` can be 1
360  // or 2. So if unrolled, these loops would look like:
361  //
362  // - block_offset == 1
363  // aes_update(NULL, input[0]);
364  // aes_update(output[0], input[1]);
365  // aes_update(output[1], input[2]);
366  // aes_update(output[2], NULL);
367  //
368  // - block_offset == 2
369  // aes_update(NULL, input[0]);
370  // aes_update(NULL, input[1]);
371  // aes_update(output[0], input[2]); // The HW is processing input[1].
372  // aes_update(output[1], input[3]); // The HW is processing input[2].
373  // aes_update(output[2], NULL);
374  // aes_update(output[3], NULL);
375  //
376  // Using a `block_offset` of 2 allows having 3 blocks in flight which is
377  // beneficial from a hardening and performance point of view:
378  // - Software retrieves Block x-1 from the data output registers.
379  // - Hardware processes Block x.
380  // - Software provides Block x+1 via the data input registers.
381  //
382  // See the AES driver for details.
383  const size_t block_offset = input_nblocks >= 3 ? 2 : 1;
384  aes_block_t block_in;
385  aes_block_t block_out;
386  size_t i;
387 
388  // Provide the first `block_offset` number of input blocks and call the AES
389  // cipher.
390  for (i = 0; launder32(i) < block_offset; ++i) {
391  HARDENED_TRY(get_block(cipher_input, aes_padding, i, &block_in));
392  TRY(aes_update(/*dest=*/NULL, &block_in));
393  }
394  // Check that the loop ran for the correct number of iterations.
395  HARDENED_CHECK_EQ(i, block_offset);
396 
397  // Call the AES cipher while providing new input and copying data to the
398  // output buffer.
399  for (i = block_offset; launder32(i) < input_nblocks; ++i) {
400  HARDENED_TRY(get_block(cipher_input, aes_padding, i, &block_in));
401  TRY(aes_update(&block_out, &block_in));
402  // TODO(#17711) Change to `hardened_memcpy`.
403  memcpy(&cipher_output.data[(i - block_offset) * kAesBlockNumBytes],
404  block_out.data, kAesBlockNumBytes);
405  }
406  // Check that the loop ran for the correct number of iterations.
407  HARDENED_CHECK_EQ(i, input_nblocks);
408 
409  // Retrieve the output from the final `block_offset` blocks (providing no
410  // input).
411  for (i = block_offset; launder32(i) > 0; --i) {
412  HARDENED_TRY(aes_update(&block_out, /*src=*/NULL));
413  // TODO(#17711) Change to `hardened_memcpy`.
414  memcpy(&cipher_output.data[(input_nblocks - i) * kAesBlockNumBytes],
415  block_out.data, kAesBlockNumBytes);
416  }
417  // Check that the loop ran for the correct number of iterations.
418  HARDENED_CHECK_EQ(i, 0);
419 
420  // Deinitialize the AES block and update the IV (in ECB mode, skip the IV).
421  if (aes_mode == launder32(kAesCipherModeEcb)) {
422  HARDENED_TRY(aes_end(NULL));
423  } else {
424  HARDENED_TRY(aes_end(&aes_iv));
425  hardened_memcpy(iv.data, aes_iv.data, kAesBlockNumWords);
426  }
427 
428  // If the key was sideloaded, clear it.
429  if (key->config.hw_backed == kHardenedBoolTrue) {
430  HARDENED_TRY(keymgr_sideload_clear_aes());
431  }
432 
433  return OTCRYPTO_OK;
434 }
435 
436 /**
437  * Construct the underlying AES key for AES-GCM.
438  *
439  * Also performs integrity, mode, and null-pointer checks on the key.
440  *
441  * @param blinded_key Blinded key struct.
442  * @param[out] aes_key Destination AES key struct.
443  * @return Result of the operation.
444  */
445 static status_t aes_gcm_key_construct(const otcrypto_blinded_key_t *blinded_key,
446  aes_key_t *aes_key) {
447  // Key integrity check.
448  if (launder32(integrity_blinded_key_check(blinded_key)) !=
450  return OTCRYPTO_BAD_ARGS;
451  }
452  HARDENED_CHECK_EQ(integrity_blinded_key_check(blinded_key),
454 
455  // Check the key mode.
456  if (launder32((uint32_t)blinded_key->config.key_mode) !=
457  kOtcryptoKeyModeAesGcm) {
458  return OTCRYPTO_BAD_ARGS;
459  }
460  HARDENED_CHECK_EQ(blinded_key->config.key_mode, kOtcryptoKeyModeAesGcm);
461 
462  // Set the mode of the underlying AES key to CTR (since this is the
463  // underlying block cipher mode for GCM).
464  aes_key->mode = kAesCipherModeCtr;
465 
466  // Set the AES key length (in words).
467  aes_key->key_len = keyblob_share_num_words(blinded_key->config);
468 
469  // Check for null pointer.
470  if (blinded_key->keyblob == NULL) {
471  return OTCRYPTO_BAD_ARGS;
472  }
473 
474  if (launder32(blinded_key->config.hw_backed) == kHardenedBoolTrue) {
475  // In this case, we use an implementation-specific representation; the
476  // first "share" is the keyblob and the second share is ignored.
477  if (launder32(blinded_key->keyblob_length) != kKeyblobHwBackedBytes) {
478  return OTCRYPTO_BAD_ARGS;
479  }
480  HARDENED_CHECK_EQ(blinded_key->keyblob_length, kKeyblobHwBackedBytes);
481  aes_key->key_shares[0] = blinded_key->keyblob;
482  aes_key->key_shares[1] = NULL;
483  aes_key->sideload = launder32(kHardenedBoolTrue);
484  } else if (launder32(blinded_key->config.hw_backed) == kHardenedBoolFalse) {
485  HARDENED_CHECK_EQ(blinded_key->config.hw_backed, kHardenedBoolFalse);
486  // Get pointers to the individual shares.
487  uint32_t *share0;
488  uint32_t *share1;
489  HARDENED_TRY(keyblob_to_shares(blinded_key, &share0, &share1));
490  aes_key->key_shares[0] = share0;
491  aes_key->key_shares[1] = share1;
492  aes_key->sideload = launder32(kHardenedBoolFalse);
493  } else {
494  return OTCRYPTO_BAD_ARGS;
495  }
496  HARDENED_CHECK_EQ(aes_key->sideload, blinded_key->config.hw_backed);
497 
498  return OTCRYPTO_OK;
499 }
500 
501 /**
502  * Checks if the given byte-length matches the tag length enum value.
503  *
504  * @param word_len Allocated tag length in 32-bit words.
505  * @param tag_len Tag length enum value.
506  * @return OK if the tag length is acceptable, BAD_ARGS otherwise.
507  */
508 status_t aes_gcm_check_tag_length(size_t word_len,
509  otcrypto_aes_gcm_tag_len_t tag_len) {
510  size_t bit_len = 0;
511  switch (launder32(tag_len)) {
512  case kOtcryptoAesGcmTagLen128:
513  HARDENED_CHECK_EQ(tag_len, kOtcryptoAesGcmTagLen128);
514  bit_len = 128;
515  break;
516  case kOtcryptoAesGcmTagLen96:
517  HARDENED_CHECK_EQ(tag_len, kOtcryptoAesGcmTagLen96);
518  bit_len = 96;
519  break;
520  case kOtcryptoAesGcmTagLen64:
521  HARDENED_CHECK_EQ(tag_len, kOtcryptoAesGcmTagLen64);
522  bit_len = 64;
523  break;
524  case kOtcryptoAesGcmTagLen32:
525  HARDENED_CHECK_EQ(tag_len, kOtcryptoAesGcmTagLen32);
526  bit_len = 32;
527  break;
528  default:
529  // Invalid tag length.
530  return OTCRYPTO_BAD_ARGS;
531  }
532  HARDENED_CHECK_GT(bit_len, 0);
533  HARDENED_CHECK_EQ(bit_len % 32, 0);
534 
535  if (launder32(word_len) != bit_len / 32) {
536  return OTCRYPTO_BAD_ARGS;
537  }
538  HARDENED_CHECK_EQ(word_len, bit_len / 32);
539 
540  // Extra hardening checks; the word length must be nonzero and at most the
541  // size of an AES block.
542  HARDENED_CHECK_LT(0, word_len);
543  HARDENED_CHECK_LE(word_len, kAesBlockNumWords);
544 
545  return OTCRYPTO_OK;
546 }
547 
548 /**
549  * Actuate the key manager to generate a sideloaded AES-GCM key.
550  *
551  * The AES driver will not load or clear sideloaded keys by itself, so we need
552  * to do it separately at each stage of the AES-GCM operation.
553  *
554  * Sideloaded keys are stored in the `aes_key_t` struct in a format specific to
555  * this AES-GCM implementation: the first "key share" is the diversification
556  * data as it would be stored in a blinded keyblob, and the second "share" is
557  * completely ignored.
558  *
559  * If the key is not a sideloaded key, this function does nothing.
560  *
561  * @param key Key to load.
562  * @return OK or errror.
563  */
564 static status_t load_key_if_sideloaded(const aes_key_t key) {
565  if (launder32(key.sideload) == kHardenedBoolFalse) {
566  return OTCRYPTO_OK;
567  } else if (key.sideload != kHardenedBoolTrue) {
568  return OTCRYPTO_BAD_ARGS;
569  }
571  keymgr_diversification_t diversification;
572  HARDENED_TRY(keyblob_buffer_to_keymgr_diversification(
573  key.key_shares[0], kOtcryptoKeyModeAesGcm, &diversification));
574  return keymgr_generate_key_aes(diversification);
575 }
576 
577 /**
578  * Clear the sideload slot if the AES key was sideloaded.
579  *
580  * It is important to clear the sideload slot before returning to the caller so
581  * that other applications can't access the key in between operations.
582  *
583  * If the key is not a sideloaded key, this function does nothing.
584  *
585  * @param key Key that was possibly loaded.
586  * @return OK or errror.
587  */
588 static status_t clear_key_if_sideloaded(const aes_key_t key) {
589  if (launder32(key.sideload) == kHardenedBoolFalse) {
591  return OTCRYPTO_OK;
592  } else if (launder32(key.sideload) != kHardenedBoolTrue) {
593  return OTCRYPTO_BAD_ARGS;
594  }
596  return keymgr_sideload_clear_aes();
597 }
598 
600  otcrypto_const_byte_buf_t plaintext,
604  otcrypto_byte_buf_t ciphertext,
605  otcrypto_word32_buf_t auth_tag) {
606  // Check for NULL pointers in input pointers and required-nonzero-length data
607  // buffers.
608  if (key == NULL || iv.data == NULL || auth_tag.data == NULL) {
609  return OTCRYPTO_BAD_ARGS;
610  }
611 
612  // Conditionally check for null pointers in data buffers that may be
613  // 0-length.
614  if ((aad.len != 0 && aad.data == NULL) ||
615  (ciphertext.len != 0 && ciphertext.data == NULL) ||
616  (plaintext.len != 0 && plaintext.data == NULL)) {
617  return OTCRYPTO_BAD_ARGS;
618  }
619 
620  // Ensure the plaintext and ciphertext lengths match.
621  if (launder32(ciphertext.len) != plaintext.len) {
622  return OTCRYPTO_BAD_ARGS;
623  }
624  HARDENED_CHECK_EQ(ciphertext.len, plaintext.len);
625 
626  // Check the tag length.
627  HARDENED_TRY(aes_gcm_check_tag_length(auth_tag.len, tag_len));
628 
629  // Construct the AES key.
631  HARDENED_TRY(aes_gcm_key_construct(key, &aes_key));
632  HARDENED_TRY(load_key_if_sideloaded(aes_key));
633 
634  // Call the core encryption operation.
635  HARDENED_TRY(aes_gcm_encrypt(aes_key, iv.len, iv.data, plaintext.len,
636  plaintext.data, aad.len, aad.data, auth_tag.len,
637  auth_tag.data, ciphertext.data));
638 
639  HARDENED_TRY(clear_key_if_sideloaded(aes_key));
640  return OTCRYPTO_OK;
641 }
642 
644  const otcrypto_blinded_key_t *key, otcrypto_const_byte_buf_t ciphertext,
647  otcrypto_byte_buf_t plaintext, hardened_bool_t *success) {
648  // Check for NULL pointers in input pointers and required-nonzero-length data
649  // buffers.
650  if (key == NULL || iv.data == NULL || auth_tag.data == NULL) {
651  return OTCRYPTO_BAD_ARGS;
652  }
653 
654  // Conditionally check for null pointers in data buffers that may be
655  // 0-length.
656  if ((aad.len != 0 && aad.data == NULL) ||
657  (ciphertext.len != 0 && ciphertext.data == NULL) ||
658  (plaintext.len != 0 && plaintext.data == NULL)) {
659  return OTCRYPTO_BAD_ARGS;
660  }
661 
662  // Construct the AES key.
664  HARDENED_TRY(aes_gcm_key_construct(key, &aes_key));
665  HARDENED_TRY(load_key_if_sideloaded(aes_key));
666 
667  // Ensure the plaintext and ciphertext lengths match.
668  if (launder32(ciphertext.len) != plaintext.len) {
669  return OTCRYPTO_BAD_ARGS;
670  }
671  HARDENED_CHECK_EQ(ciphertext.len, plaintext.len);
672 
673  // Check the tag length.
674  HARDENED_TRY(aes_gcm_check_tag_length(auth_tag.len, tag_len));
675 
676  // Call the core decryption operation.
677  HARDENED_TRY(aes_gcm_decrypt(aes_key, iv.len, iv.data, ciphertext.len,
678  ciphertext.data, aad.len, aad.data, auth_tag.len,
679  auth_tag.data, plaintext.data, success));
680 
681  HARDENED_TRY(clear_key_if_sideloaded(aes_key));
682  return OTCRYPTO_OK;
683 }
684 
688  if (key == NULL || key->keyblob == NULL || iv.data == NULL || ctx == NULL) {
689  return OTCRYPTO_BAD_ARGS;
690  }
691 
692  // Construct the AES key.
694  HARDENED_TRY(aes_gcm_key_construct(key, &aes_key));
695  HARDENED_TRY(load_key_if_sideloaded(aes_key));
696 
697  // Call the internal init operation.
698  aes_gcm_context_t internal_ctx;
699  HARDENED_TRY(aes_gcm_encrypt_init(aes_key, iv.len, iv.data, &internal_ctx));
700 
701  // Save the context and clear the key if needed.
702  gcm_context_save(&internal_ctx, ctx);
703  HARDENED_TRY(clear_key_if_sideloaded(internal_ctx.key));
704  return OTCRYPTO_OK;
705 }
706 
710  if (key == NULL || key->keyblob == NULL || iv.data == NULL || ctx == NULL) {
711  return OTCRYPTO_BAD_ARGS;
712  }
713 
714  // Construct the AES key.
716  HARDENED_TRY(aes_gcm_key_construct(key, &aes_key));
717  HARDENED_TRY(load_key_if_sideloaded(aes_key));
718 
719  // Call the internal init operation.
720  aes_gcm_context_t internal_ctx;
721  HARDENED_TRY(aes_gcm_decrypt_init(aes_key, iv.len, iv.data, &internal_ctx));
722 
723  // Save the context and clear the key if needed.
724  gcm_context_save(&internal_ctx, ctx);
725  HARDENED_TRY(clear_key_if_sideloaded(internal_ctx.key));
726  return OTCRYPTO_OK;
727 }
728 
731  if (ctx == NULL || aad.data == NULL) {
732  return OTCRYPTO_BAD_ARGS;
733  }
734 
735  if (aad.len == 0) {
736  // Nothing to do.
737  return OTCRYPTO_OK;
738  }
739 
740  // Restore the AES-GCM context object and load the key if needed.
741  aes_gcm_context_t internal_ctx;
742  gcm_context_restore(ctx, &internal_ctx);
743  HARDENED_TRY(load_key_if_sideloaded(internal_ctx.key));
744 
745  // Call the internal update operation.
746  HARDENED_TRY(aes_gcm_update_aad(&internal_ctx, aad.len, aad.data));
747 
748  // Save the context and clear the key if needed.
749  gcm_context_save(&internal_ctx, ctx);
750  HARDENED_TRY(clear_key_if_sideloaded(internal_ctx.key));
751  return OTCRYPTO_OK;
752 }
753 
756  otcrypto_byte_buf_t output, size_t *output_bytes_written) {
757  if (ctx == NULL || input.data == NULL || output.data == NULL ||
758  output_bytes_written == NULL) {
759  return OTCRYPTO_BAD_ARGS;
760  }
761  *output_bytes_written = 0;
762 
763  if (input.len == 0) {
764  // Nothing to do.
765  return OTCRYPTO_OK;
766  }
767 
768  // Restore the AES-GCM context object and load the key if needed.
769  aes_gcm_context_t internal_ctx;
770  gcm_context_restore(ctx, &internal_ctx);
771  HARDENED_TRY(load_key_if_sideloaded(internal_ctx.key));
772 
773  // The output buffer must be long enough to hold all full blocks that will
774  // exist after `input` is added.
775  size_t partial_block_len = internal_ctx.input_len % kAesBlockNumBytes;
776  if (input.len > UINT32_MAX - partial_block_len) {
777  return OTCRYPTO_BAD_ARGS;
778  }
779  size_t min_output_blocks =
780  (partial_block_len + input.len) / kAesBlockNumBytes;
781  size_t min_output_len = min_output_blocks * kAesBlockNumBytes;
782  if (output.len < min_output_len) {
783  return OTCRYPTO_BAD_ARGS;
784  }
785 
786  // Call the internal update operation.
787  HARDENED_TRY(aes_gcm_update_encrypted_data(
788  &internal_ctx, input.len, input.data, output_bytes_written, output.data));
789 
790  // Save the context and clear the key if needed.
791  gcm_context_save(&internal_ctx, ctx);
792  HARDENED_TRY(clear_key_if_sideloaded(internal_ctx.key));
793  return OTCRYPTO_OK;
794 }
795 
798  otcrypto_byte_buf_t ciphertext, size_t *ciphertext_bytes_written,
799  otcrypto_word32_buf_t auth_tag) {
800  if (ctx == NULL || ciphertext_bytes_written == NULL ||
801  auth_tag.data == NULL) {
802  return OTCRYPTO_BAD_ARGS;
803  }
804  if (ciphertext.len != 0 && ciphertext.data == NULL) {
805  return OTCRYPTO_BAD_ARGS;
806  }
807  *ciphertext_bytes_written = 0;
808 
809  // Check the tag length.
810  HARDENED_TRY(aes_gcm_check_tag_length(auth_tag.len, tag_len));
811 
812  // Restore the AES-GCM context object and load the key if needed.
813  aes_gcm_context_t internal_ctx;
814  gcm_context_restore(ctx, &internal_ctx);
815  HARDENED_TRY(load_key_if_sideloaded(internal_ctx.key));
816 
817  // If the partial block is nonempty, the output must be at least as long as
818  // the partial block.
819  size_t partial_block_len = internal_ctx.input_len % kAesBlockNumBytes;
820  if (ciphertext.len < partial_block_len) {
821  return OTCRYPTO_BAD_ARGS;
822  }
823 
824  // Call the internal final operation.
825  HARDENED_TRY(aes_gcm_encrypt_final(&internal_ctx, auth_tag.len, auth_tag.data,
826  ciphertext_bytes_written,
827  ciphertext.data));
828 
829  // Clear the context and the key if needed.
830  hardened_memshred(ctx->data, ARRAYSIZE(ctx->data));
831  HARDENED_TRY(clear_key_if_sideloaded(internal_ctx.key));
832  return OTCRYPTO_OK;
833 }
834 
838  size_t *plaintext_bytes_written, hardened_bool_t *success) {
839  if (ctx == NULL || plaintext_bytes_written == NULL || auth_tag.data == NULL ||
840  success == NULL) {
841  return OTCRYPTO_BAD_ARGS;
842  }
843  if (plaintext.len != 0 && plaintext.data == NULL) {
844  return OTCRYPTO_BAD_ARGS;
845  }
846  *plaintext_bytes_written = 0;
847  *success = kHardenedBoolFalse;
848 
849  // Check the tag length.
850  HARDENED_TRY(aes_gcm_check_tag_length(auth_tag.len, tag_len));
851 
852  // Restore the AES-GCM context object and load the key if needed.
853  aes_gcm_context_t internal_ctx;
854  gcm_context_restore(ctx, &internal_ctx);
855  HARDENED_TRY(load_key_if_sideloaded(internal_ctx.key));
856 
857  // If the partial block is nonempty, the output must be at least as long as
858  // the partial block.
859  size_t partial_block_len = internal_ctx.input_len % kAesBlockNumBytes;
860  if (plaintext.len < partial_block_len) {
861  return OTCRYPTO_BAD_ARGS;
862  }
863 
864  // Call the internal final operation.
865  HARDENED_TRY(aes_gcm_decrypt_final(&internal_ctx, auth_tag.len, auth_tag.data,
866  plaintext_bytes_written, plaintext.data,
867  success));
868 
869  // Clear the context and the key if needed.
870  hardened_memshred(ctx->data, ARRAYSIZE(ctx->data));
871  HARDENED_TRY(clear_key_if_sideloaded(internal_ctx.key));
872  return OTCRYPTO_OK;
873 }
874 
876  const otcrypto_key_config_t config, size_t *wrapped_num_words) {
877  // Check that the total wrapped key length will fit in 32 bits.
878  size_t config_num_words = sizeof(otcrypto_key_config_t) / sizeof(uint32_t);
879  if (keyblob_num_words(config) > UINT32_MAX - config_num_words - 2) {
880  return OTCRYPTO_BAD_ARGS;
881  }
882 
883  // A wrapped key includes:
884  // - The full key configuration
885  // - The key checksum (32 bits)
886  // - The keyblob length (in words) as a 32-bit word
887  // - The keyblob
888  *wrapped_num_words = config_num_words + 2 + keyblob_num_words(config);
889 
890  // We need to add 64 bits for the AES-KWP prefix.
891  *wrapped_num_words += 2;
892 
893  // The number of words needs to be rounded up to the next multiple of 64 bits.
894  if (*wrapped_num_words % 2 == 1) {
895  *wrapped_num_words += 1;
896  }
897 
898  return OTCRYPTO_OK;
899 }
900 
901 /**
902  * Extract an AES-KWP key encryption key from the blinded key struct.
903  *
904  * Also checks the key's integrity and mode.
905  *
906  * @param key_kek Blinded key encryption key.
907  * @param[out] aes_key Destination AES key struct.
908  * @return Result of the operation.
909  */
910 static status_t aes_kwp_key_construct(const otcrypto_blinded_key_t *key_kek,
911  aes_key_t *aes_key) {
912  // Key integrity check.
913  if (launder32(integrity_blinded_key_check(key_kek)) != kHardenedBoolTrue) {
914  return OTCRYPTO_BAD_ARGS;
915  }
916  HARDENED_CHECK_EQ(integrity_blinded_key_check(key_kek), kHardenedBoolTrue);
917 
918  // Check the key mode.
919  if (launder32((uint32_t)key_kek->config.key_mode) != kOtcryptoKeyModeAesKwp) {
920  return OTCRYPTO_BAD_ARGS;
921  }
922  HARDENED_CHECK_EQ(key_kek->config.key_mode, kOtcryptoKeyModeAesKwp);
923 
924  // Set the mode of the underlying AES key to ECB (since this is the
925  // underlying block cipher mode for KWP).
926  aes_key->mode = kAesCipherModeEcb;
927 
928  if (key_kek->config.hw_backed == kHardenedBoolTrue) {
929  // Call keymgr to sideload the key into AES.
930  keymgr_diversification_t diversification;
931  HARDENED_TRY(keyblob_to_keymgr_diversification(key_kek, &diversification));
932  HARDENED_TRY(keymgr_generate_key_aes(diversification));
933  } else if (key_kek->config.hw_backed != kHardenedBoolFalse) {
934  return OTCRYPTO_BAD_ARGS;
935  }
936  aes_key->sideload = key_kek->config.hw_backed;
937 
938  // Set the AES key length (in words).
939  aes_key->key_len = keyblob_share_num_words(key_kek->config);
940 
941  // Get pointers to the individual shares.
942  uint32_t *share0;
943  uint32_t *share1;
944  HARDENED_TRY(keyblob_to_shares(key_kek, &share0, &share1));
945  aes_key->key_shares[0] = share0;
946  aes_key->key_shares[1] = share1;
947 
948  return OTCRYPTO_OK;
949 }
950 
952  const otcrypto_blinded_key_t *key_to_wrap,
953  const otcrypto_blinded_key_t *key_kek, otcrypto_word32_buf_t wrapped_key) {
954  if (key_to_wrap == NULL || key_to_wrap->keyblob == NULL || key_kek == NULL ||
955  key_kek->keyblob == NULL || wrapped_key.data == NULL) {
956  return OTCRYPTO_BAD_ARGS;
957  }
958 
959  // Check the integrity of the key material we are wrapping.
960  if (launder32(integrity_blinded_key_check(key_to_wrap)) !=
962  return OTCRYPTO_BAD_ARGS;
963  }
964  HARDENED_CHECK_EQ(integrity_blinded_key_check(key_to_wrap),
966 
967  // Check the length of the output buffer.
968  size_t exp_len;
969  HARDENED_TRY(otcrypto_aes_kwp_wrapped_len(key_to_wrap->config, &exp_len));
970  if (wrapped_key.len != exp_len) {
971  return OTCRYPTO_BAD_ARGS;
972  }
973 
974  // Check the integrity/lengths/mode of the key encryption key, and construct
975  // an internal AES key.
976  aes_key_t kek;
977  HARDENED_TRY(aes_kwp_key_construct(key_kek, &kek));
978 
979  // Check the keyblob length.
980  uint32_t keyblob_words = keyblob_num_words(key_to_wrap->config);
981  if (key_to_wrap->keyblob_length != keyblob_words * sizeof(uint32_t)) {
982  return OTCRYPTO_BAD_ARGS;
983  }
984 
985  // Check that the configuration is aligned.
986  if (misalignment32_of((uintptr_t)&key_to_wrap->config) != 0) {
987  return OTCRYPTO_BAD_ARGS;
988  }
989 
990  // Create the plaintext by copying the key configuration, checksum, keyblob
991  // length, and keyblob into a buffer.
992  uint32_t config_words = sizeof(otcrypto_key_config_t) / sizeof(uint32_t);
993  size_t plaintext_num_words = config_words + 2 + keyblob_words;
994  uint32_t plaintext[plaintext_num_words];
995  hardened_memcpy(plaintext, (uint32_t *)&key_to_wrap->config, config_words);
996  plaintext[config_words] = key_to_wrap->checksum;
997  plaintext[config_words + 1] = keyblob_words;
998  hardened_memcpy(plaintext + config_words + 2, key_to_wrap->keyblob,
999  keyblob_words);
1000 
1001  // Wrap the key.
1002  return aes_kwp_wrap(kek, plaintext, sizeof(plaintext), wrapped_key.data);
1003 }
1004 
1006  otcrypto_const_word32_buf_t wrapped_key,
1007  const otcrypto_blinded_key_t *key_kek, hardened_bool_t *success,
1008  otcrypto_blinded_key_t *unwrapped_key) {
1009  *success = kHardenedBoolFalse;
1010 
1011  if (wrapped_key.data == NULL || key_kek == NULL || key_kek->keyblob == NULL ||
1012  success == NULL || unwrapped_key == NULL ||
1013  unwrapped_key->keyblob == NULL) {
1014  return OTCRYPTO_BAD_ARGS;
1015  }
1016 
1017  // Check the integrity/lengths/mode of the key encryption key, and construct
1018  // an internal AES key.
1019  aes_key_t kek;
1020  HARDENED_TRY(aes_kwp_key_construct(key_kek, &kek));
1021 
1022  // Check that the configuration is aligned.
1023  if (misalignment32_of((uintptr_t)&unwrapped_key->config) != 0) {
1024  return OTCRYPTO_BAD_ARGS;
1025  }
1026 
1027  // Unwrap the key.
1028  uint32_t plaintext[wrapped_key.len];
1029  HARDENED_TRY(aes_kwp_unwrap(kek, wrapped_key.data,
1030  wrapped_key.len * sizeof(uint32_t), success,
1031  plaintext));
1032 
1033  if (launder32(*success) != kHardenedBoolTrue) {
1034  *success = kHardenedBoolFalse;
1035  return OTCRYPTO_OK;
1036  }
1038 
1039  // Set back to false while we check other conditions.
1040  *success = kHardenedBoolFalse;
1041 
1042  // Extract the key configuration.
1043  uint32_t config_words = sizeof(otcrypto_key_config_t) / sizeof(uint32_t);
1044  hardened_memcpy((uint32_t *)&unwrapped_key->config, plaintext, config_words);
1045 
1046  // Extract the checksum and keyblob length.
1047  unwrapped_key->checksum = plaintext[config_words];
1048  uint32_t keyblob_words = plaintext[config_words + 1];
1049  if (keyblob_words != keyblob_num_words(unwrapped_key->config)) {
1050  *success = kHardenedBoolFalse;
1051  return OTCRYPTO_OK;
1052  }
1053 
1054  // Extract the keyblob.
1055  if (unwrapped_key->keyblob_length != keyblob_words * sizeof(uint32_t)) {
1056  return OTCRYPTO_BAD_ARGS;
1057  }
1058  hardened_memcpy(unwrapped_key->keyblob, plaintext + config_words + 2,
1059  keyblob_words);
1060 
1061  // Finally, check the integrity of the key material we unwrapped.
1062  *success = integrity_blinded_key_check(unwrapped_key);
1063  return OTCRYPTO_OK;
1064 }