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