CryptAes.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /** @file
  2. AES Wrapper Implementation over OpenSSL.
  3. Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "InternalCryptLib.h"
  7. #include <openssl/aes.h>
  8. /**
  9. Retrieves the size, in bytes, of the context buffer required for AES operations.
  10. @return The size, in bytes, of the context buffer required for AES operations.
  11. **/
  12. UINTN
  13. EFIAPI
  14. AesGetContextSize (
  15. VOID
  16. )
  17. {
  18. //
  19. // AES uses different key contexts for encryption and decryption, so here memory
  20. // for 2 copies of AES_KEY is allocated.
  21. //
  22. return (UINTN) (2 * sizeof (AES_KEY));
  23. }
  24. /**
  25. Initializes user-supplied memory as AES context for subsequent use.
  26. This function initializes user-supplied memory pointed by AesContext as AES context.
  27. In addition, it sets up all AES key materials for subsequent encryption and decryption
  28. operations.
  29. There are 3 options for key length, 128 bits, 192 bits, and 256 bits.
  30. If AesContext is NULL, then return FALSE.
  31. If Key is NULL, then return FALSE.
  32. If KeyLength is not valid, then return FALSE.
  33. @param[out] AesContext Pointer to AES context being initialized.
  34. @param[in] Key Pointer to the user-supplied AES key.
  35. @param[in] KeyLength Length of AES key in bits.
  36. @retval TRUE AES context initialization succeeded.
  37. @retval FALSE AES context initialization failed.
  38. **/
  39. BOOLEAN
  40. EFIAPI
  41. AesInit (
  42. OUT VOID *AesContext,
  43. IN CONST UINT8 *Key,
  44. IN UINTN KeyLength
  45. )
  46. {
  47. AES_KEY *AesKey;
  48. //
  49. // Check input parameters.
  50. //
  51. if (AesContext == NULL || Key == NULL || (KeyLength != 128 && KeyLength != 192 && KeyLength != 256)) {
  52. return FALSE;
  53. }
  54. //
  55. // Initialize AES encryption & decryption key schedule.
  56. //
  57. AesKey = (AES_KEY *) AesContext;
  58. if (AES_set_encrypt_key (Key, (UINT32) KeyLength, AesKey) != 0) {
  59. return FALSE;
  60. }
  61. if (AES_set_decrypt_key (Key, (UINT32) KeyLength, AesKey + 1) != 0) {
  62. return FALSE;
  63. }
  64. return TRUE;
  65. }
  66. /**
  67. Performs AES encryption on a data buffer of the specified size in ECB mode.
  68. This function performs AES encryption on data buffer pointed by Input, of specified
  69. size of InputSize, in ECB mode.
  70. InputSize must be multiple of block size (16 bytes). This function does not perform
  71. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  72. AesContext should be already correctly initialized by AesInit(). Behavior with
  73. invalid AES context is undefined.
  74. If AesContext is NULL, then return FALSE.
  75. If Input is NULL, then return FALSE.
  76. If InputSize is not multiple of block size (16 bytes), then return FALSE.
  77. If Output is NULL, then return FALSE.
  78. @param[in] AesContext Pointer to the AES context.
  79. @param[in] Input Pointer to the buffer containing the data to be encrypted.
  80. @param[in] InputSize Size of the Input buffer in bytes.
  81. @param[out] Output Pointer to a buffer that receives the AES encryption output.
  82. @retval TRUE AES encryption succeeded.
  83. @retval FALSE AES encryption failed.
  84. **/
  85. BOOLEAN
  86. EFIAPI
  87. AesEcbEncrypt (
  88. IN VOID *AesContext,
  89. IN CONST UINT8 *Input,
  90. IN UINTN InputSize,
  91. OUT UINT8 *Output
  92. )
  93. {
  94. AES_KEY *AesKey;
  95. //
  96. // Check input parameters.
  97. //
  98. if (AesContext == NULL || Input == NULL || (InputSize % AES_BLOCK_SIZE) != 0 || Output == NULL) {
  99. return FALSE;
  100. }
  101. AesKey = (AES_KEY *) AesContext;
  102. //
  103. // Perform AES data encryption with ECB mode (block-by-block)
  104. //
  105. while (InputSize > 0) {
  106. AES_ecb_encrypt (Input, Output, AesKey, AES_ENCRYPT);
  107. Input += AES_BLOCK_SIZE;
  108. Output += AES_BLOCK_SIZE;
  109. InputSize -= AES_BLOCK_SIZE;
  110. }
  111. return TRUE;
  112. }
  113. /**
  114. Performs AES decryption on a data buffer of the specified size in ECB mode.
  115. This function performs AES decryption on data buffer pointed by Input, of specified
  116. size of InputSize, in ECB mode.
  117. InputSize must be multiple of block size (16 bytes). This function does not perform
  118. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  119. AesContext should be already correctly initialized by AesInit(). Behavior with
  120. invalid AES context is undefined.
  121. If AesContext is NULL, then return FALSE.
  122. If Input is NULL, then return FALSE.
  123. If InputSize is not multiple of block size (16 bytes), then return FALSE.
  124. If Output is NULL, then return FALSE.
  125. @param[in] AesContext Pointer to the AES context.
  126. @param[in] Input Pointer to the buffer containing the data to be decrypted.
  127. @param[in] InputSize Size of the Input buffer in bytes.
  128. @param[out] Output Pointer to a buffer that receives the AES decryption output.
  129. @retval TRUE AES decryption succeeded.
  130. @retval FALSE AES decryption failed.
  131. **/
  132. BOOLEAN
  133. EFIAPI
  134. AesEcbDecrypt (
  135. IN VOID *AesContext,
  136. IN CONST UINT8 *Input,
  137. IN UINTN InputSize,
  138. OUT UINT8 *Output
  139. )
  140. {
  141. AES_KEY *AesKey;
  142. //
  143. // Check input parameters.
  144. //
  145. if (AesContext == NULL || Input == NULL || (InputSize % AES_BLOCK_SIZE) != 0 || Output == NULL) {
  146. return FALSE;
  147. }
  148. AesKey = (AES_KEY *) AesContext;
  149. //
  150. // Perform AES data decryption with ECB mode (block-by-block)
  151. //
  152. while (InputSize > 0) {
  153. AES_ecb_encrypt (Input, Output, AesKey + 1, AES_DECRYPT);
  154. Input += AES_BLOCK_SIZE;
  155. Output += AES_BLOCK_SIZE;
  156. InputSize -= AES_BLOCK_SIZE;
  157. }
  158. return TRUE;
  159. }
  160. /**
  161. Performs AES encryption on a data buffer of the specified size in CBC mode.
  162. This function performs AES encryption on data buffer pointed by Input, of specified
  163. size of InputSize, in CBC mode.
  164. InputSize must be multiple of block size (16 bytes). This function does not perform
  165. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  166. Initialization vector should be one block size (16 bytes).
  167. AesContext should be already correctly initialized by AesInit(). Behavior with
  168. invalid AES context is undefined.
  169. If AesContext is NULL, then return FALSE.
  170. If Input is NULL, then return FALSE.
  171. If InputSize is not multiple of block size (16 bytes), then return FALSE.
  172. If Ivec is NULL, then return FALSE.
  173. If Output is NULL, then return FALSE.
  174. @param[in] AesContext Pointer to the AES context.
  175. @param[in] Input Pointer to the buffer containing the data to be encrypted.
  176. @param[in] InputSize Size of the Input buffer in bytes.
  177. @param[in] Ivec Pointer to initialization vector.
  178. @param[out] Output Pointer to a buffer that receives the AES encryption output.
  179. @retval TRUE AES encryption succeeded.
  180. @retval FALSE AES encryption failed.
  181. **/
  182. BOOLEAN
  183. EFIAPI
  184. AesCbcEncrypt (
  185. IN VOID *AesContext,
  186. IN CONST UINT8 *Input,
  187. IN UINTN InputSize,
  188. IN CONST UINT8 *Ivec,
  189. OUT UINT8 *Output
  190. )
  191. {
  192. AES_KEY *AesKey;
  193. UINT8 IvecBuffer[AES_BLOCK_SIZE];
  194. //
  195. // Check input parameters.
  196. //
  197. if (AesContext == NULL || Input == NULL || (InputSize % AES_BLOCK_SIZE) != 0) {
  198. return FALSE;
  199. }
  200. if (Ivec == NULL || Output == NULL || InputSize > INT_MAX) {
  201. return FALSE;
  202. }
  203. AesKey = (AES_KEY *) AesContext;
  204. CopyMem (IvecBuffer, Ivec, AES_BLOCK_SIZE);
  205. //
  206. // Perform AES data encryption with CBC mode
  207. //
  208. AES_cbc_encrypt (Input, Output, (UINT32) InputSize, AesKey, IvecBuffer, AES_ENCRYPT);
  209. return TRUE;
  210. }
  211. /**
  212. Performs AES decryption on a data buffer of the specified size in CBC mode.
  213. This function performs AES decryption on data buffer pointed by Input, of specified
  214. size of InputSize, in CBC mode.
  215. InputSize must be multiple of block size (16 bytes). This function does not perform
  216. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  217. Initialization vector should be one block size (16 bytes).
  218. AesContext should be already correctly initialized by AesInit(). Behavior with
  219. invalid AES context is undefined.
  220. If AesContext is NULL, then return FALSE.
  221. If Input is NULL, then return FALSE.
  222. If InputSize is not multiple of block size (16 bytes), then return FALSE.
  223. If Ivec is NULL, then return FALSE.
  224. If Output is NULL, then return FALSE.
  225. @param[in] AesContext Pointer to the AES context.
  226. @param[in] Input Pointer to the buffer containing the data to be encrypted.
  227. @param[in] InputSize Size of the Input buffer in bytes.
  228. @param[in] Ivec Pointer to initialization vector.
  229. @param[out] Output Pointer to a buffer that receives the AES encryption output.
  230. @retval TRUE AES decryption succeeded.
  231. @retval FALSE AES decryption failed.
  232. **/
  233. BOOLEAN
  234. EFIAPI
  235. AesCbcDecrypt (
  236. IN VOID *AesContext,
  237. IN CONST UINT8 *Input,
  238. IN UINTN InputSize,
  239. IN CONST UINT8 *Ivec,
  240. OUT UINT8 *Output
  241. )
  242. {
  243. AES_KEY *AesKey;
  244. UINT8 IvecBuffer[AES_BLOCK_SIZE];
  245. //
  246. // Check input parameters.
  247. //
  248. if (AesContext == NULL || Input == NULL || (InputSize % AES_BLOCK_SIZE) != 0) {
  249. return FALSE;
  250. }
  251. if (Ivec == NULL || Output == NULL || InputSize > INT_MAX) {
  252. return FALSE;
  253. }
  254. AesKey = (AES_KEY *) AesContext;
  255. CopyMem (IvecBuffer, Ivec, AES_BLOCK_SIZE);
  256. //
  257. // Perform AES data decryption with CBC mode
  258. //
  259. AES_cbc_encrypt (Input, Output, (UINT32) InputSize, AesKey + 1, IvecBuffer, AES_DECRYPT);
  260. return TRUE;
  261. }