uboot_aes.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. * (C) Copyright 2010 - 2011 NVIDIA Corporation <www.nvidia.com>
  5. */
  6. #ifndef _AES_REF_H_
  7. #define _AES_REF_H_
  8. #ifdef USE_HOSTCC
  9. /* Define compat stuff for use in fw_* tools. */
  10. typedef unsigned char u8;
  11. typedef unsigned int u32;
  12. #define debug(...) do {} while (0)
  13. #endif
  14. /*
  15. * AES encryption library, with small code size, supporting only 128-bit AES
  16. *
  17. * AES is a stream cipher which works a block at a time, with each block
  18. * in this case being AES_KEY_LENGTH bytes.
  19. */
  20. enum {
  21. AES_STATECOLS = 4, /* columns in the state & expanded key */
  22. AES_KEYCOLS = 4, /* columns in a key */
  23. AES_ROUNDS = 10, /* rounds in encryption */
  24. AES_KEY_LENGTH = 128 / 8,
  25. AES_EXPAND_KEY_LENGTH = 4 * AES_STATECOLS * (AES_ROUNDS + 1),
  26. };
  27. /**
  28. * aes_expand_key() - Expand the AES key
  29. *
  30. * Expand a key into a key schedule, which is then used for the other
  31. * operations.
  32. *
  33. * @key Key, of length AES_KEY_LENGTH bytes
  34. * @expkey Buffer to place expanded key, AES_EXPAND_KEY_LENGTH
  35. */
  36. void aes_expand_key(u8 *key, u8 *expkey);
  37. /**
  38. * aes_encrypt() - Encrypt single block of data with AES 128
  39. *
  40. * @in Input data
  41. * @expkey Expanded key to use for encryption (from aes_expand_key())
  42. * @out Output data
  43. */
  44. void aes_encrypt(u8 *in, u8 *expkey, u8 *out);
  45. /**
  46. * aes_decrypt() - Decrypt single block of data with AES 128
  47. *
  48. * @in Input data
  49. * @expkey Expanded key to use for decryption (from aes_expand_key())
  50. * @out Output data
  51. */
  52. void aes_decrypt(u8 *in, u8 *expkey, u8 *out);
  53. /**
  54. * Apply chain data to the destination using EOR
  55. *
  56. * Each array is of length AES_KEY_LENGTH.
  57. *
  58. * @cbc_chain_data Chain data
  59. * @src Source data
  60. * @dst Destination data, which is modified here
  61. */
  62. void aes_apply_cbc_chain_data(u8 *cbc_chain_data, u8 *src, u8 *dst);
  63. /**
  64. * aes_cbc_encrypt_blocks() - Encrypt multiple blocks of data with AES CBC.
  65. *
  66. * @key_exp Expanded key to use
  67. * @iv Initialization vector
  68. * @src Source data to encrypt
  69. * @dst Destination buffer
  70. * @num_aes_blocks Number of AES blocks to encrypt
  71. */
  72. void aes_cbc_encrypt_blocks(u8 *key_exp, u8 *iv, u8 *src, u8 *dst,
  73. u32 num_aes_blocks);
  74. /**
  75. * Decrypt multiple blocks of data with AES CBC.
  76. *
  77. * @key_exp Expanded key to use
  78. * @iv Initialization vector
  79. * @src Source data to decrypt
  80. * @dst Destination buffer
  81. * @num_aes_blocks Number of AES blocks to decrypt
  82. */
  83. void aes_cbc_decrypt_blocks(u8 *key_exp, u8 *iv, u8 *src, u8 *dst,
  84. u32 num_aes_blocks);
  85. #endif /* _AES_REF_H_ */