aes.h 2.4 KB

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