uboot_aes.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_BLOCK_LENGTH bytes.
  19. */
  20. enum {
  21. AES_STATECOLS = 4, /* columns in the state & expanded key */
  22. AES128_KEYCOLS = 4, /* columns in a key for aes128 */
  23. AES192_KEYCOLS = 6, /* columns in a key for aes128 */
  24. AES256_KEYCOLS = 8, /* columns in a key for aes128 */
  25. AES128_ROUNDS = 10, /* rounds in encryption for aes128 */
  26. AES192_ROUNDS = 12, /* rounds in encryption for aes192 */
  27. AES256_ROUNDS = 14, /* rounds in encryption for aes256 */
  28. AES128_KEY_LENGTH = 128 / 8,
  29. AES192_KEY_LENGTH = 192 / 8,
  30. AES256_KEY_LENGTH = 256 / 8,
  31. AES128_EXPAND_KEY_LENGTH = 4 * AES_STATECOLS * (AES128_ROUNDS + 1),
  32. AES192_EXPAND_KEY_LENGTH = 4 * AES_STATECOLS * (AES192_ROUNDS + 1),
  33. AES256_EXPAND_KEY_LENGTH = 4 * AES_STATECOLS * (AES256_ROUNDS + 1),
  34. AES_BLOCK_LENGTH = 128 / 8,
  35. };
  36. /**
  37. * aes_expand_key() - Expand the AES key
  38. *
  39. * Expand a key into a key schedule, which is then used for the other
  40. * operations.
  41. *
  42. * @key Key
  43. * @key_size Size of the key (in bits)
  44. * @expkey Buffer to place expanded key, AES_EXPAND_KEY_LENGTH
  45. */
  46. void aes_expand_key(u8 *key, u32 key_size, u8 *expkey);
  47. /**
  48. * aes_encrypt() - Encrypt single block of data with AES 128
  49. *
  50. * @key_size Size of the aes key (in bits)
  51. * @in Input data
  52. * @expkey Expanded key to use for encryption (from aes_expand_key())
  53. * @out Output data
  54. */
  55. void aes_encrypt(u32 key_size, u8 *in, u8 *expkey, u8 *out);
  56. /**
  57. * aes_decrypt() - Decrypt single block of data with AES 128
  58. *
  59. * @key_size Size of the aes key (in bits)
  60. * @in Input data
  61. * @expkey Expanded key to use for decryption (from aes_expand_key())
  62. * @out Output data
  63. */
  64. void aes_decrypt(u32 key_size, u8 *in, u8 *expkey, u8 *out);
  65. /**
  66. * Apply chain data to the destination using EOR
  67. *
  68. * Each array is of length AES_BLOCK_LENGTH.
  69. *
  70. * @cbc_chain_data Chain data
  71. * @src Source data
  72. * @dst Destination data, which is modified here
  73. */
  74. void aes_apply_cbc_chain_data(u8 *cbc_chain_data, u8 *src, u8 *dst);
  75. /**
  76. * aes_cbc_encrypt_blocks() - Encrypt multiple blocks of data with AES CBC.
  77. *
  78. * @key_size Size of the aes key (in bits)
  79. * @key_exp Expanded key to use
  80. * @iv Initialization vector
  81. * @src Source data to encrypt
  82. * @dst Destination buffer
  83. * @num_aes_blocks Number of AES blocks to encrypt
  84. */
  85. void aes_cbc_encrypt_blocks(u32 key_size, u8 *key_exp, u8 *iv, u8 *src, u8 *dst,
  86. u32 num_aes_blocks);
  87. /**
  88. * Decrypt multiple blocks of data with AES CBC.
  89. *
  90. * @key_size Size of the aes key (in bits)
  91. * @key_exp Expanded key to use
  92. * @iv Initialization vector
  93. * @src Source data to decrypt
  94. * @dst Destination buffer
  95. * @num_aes_blocks Number of AES blocks to decrypt
  96. */
  97. void aes_cbc_decrypt_blocks(u32 key_size, u8 *key_exp, u8 *iv, u8 *src, u8 *dst,
  98. u32 num_aes_blocks);
  99. #endif /* _AES_REF_H_ */