]>
Commit | Line | Data |
---|---|---|
a8c44645 | 1 | /** @file\r |
2 | AES Wrapper Implementation over OpenSSL.\r | |
3 | \r | |
630f67dd | 4 | Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r |
2009f6b4 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
a8c44645 | 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 | |
18 | UINTN\r | |
19 | EFIAPI\r | |
20 | AesGetContextSize (\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 | |
7c342378 | 28 | return (UINTN)(2 * sizeof (AES_KEY));\r |
a8c44645 | 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 | |
6b8ebcb8 | 35 | In addition, it sets up all AES key materials for subsequent encryption and decryption\r |
a8c44645 | 36 | operations.\r |
37 | There are 3 options for key length, 128 bits, 192 bits, and 256 bits.\r | |
38 | \r | |
16d2c32c | 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 | |
a8c44645 | 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 | |
51 | BOOLEAN\r | |
52 | EFIAPI\r | |
53 | AesInit (\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 | |
a8c44645 | 61 | //\r |
16d2c32c | 62 | // Check input parameters.\r |
a8c44645 | 63 | //\r |
7c342378 | 64 | if ((AesContext == NULL) || (Key == NULL) || ((KeyLength != 128) && (KeyLength != 192) && (KeyLength != 256))) {\r |
16d2c32c | 65 | return FALSE;\r |
66 | }\r | |
a8c44645 | 67 | \r |
68 | //\r | |
69 | // Initialize AES encryption & decryption key schedule.\r | |
70 | //\r | |
7c342378 MK |
71 | AesKey = (AES_KEY *)AesContext;\r |
72 | if (AES_set_encrypt_key (Key, (UINT32)KeyLength, AesKey) != 0) {\r | |
a8c44645 | 73 | return FALSE;\r |
74 | }\r | |
7c342378 MK |
75 | \r |
76 | if (AES_set_decrypt_key (Key, (UINT32)KeyLength, AesKey + 1) != 0) {\r | |
a8c44645 | 77 | return FALSE;\r |
78 | }\r | |
7c342378 | 79 | \r |
a8c44645 | 80 | return TRUE;\r |
81 | }\r | |
82 | \r | |
a8c44645 | 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 | |
16d2c32c | 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 | |
a8c44645 | 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 | |
110 | BOOLEAN\r | |
111 | EFIAPI\r | |
112 | AesCbcEncrypt (\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 | |
16d2c32c | 123 | //\r |
124 | // Check input parameters.\r | |
125 | //\r | |
7c342378 | 126 | if ((AesContext == NULL) || (Input == NULL) || ((InputSize % AES_BLOCK_SIZE) != 0)) {\r |
dda39f3a | 127 | return FALSE;\r |
128 | }\r | |
129 | \r | |
7c342378 | 130 | if ((Ivec == NULL) || (Output == NULL) || (InputSize > INT_MAX)) {\r |
16d2c32c | 131 | return FALSE;\r |
132 | }\r | |
a8c44645 | 133 | \r |
7c342378 | 134 | AesKey = (AES_KEY *)AesContext;\r |
a8c44645 | 135 | CopyMem (IvecBuffer, Ivec, AES_BLOCK_SIZE);\r |
136 | \r | |
137 | //\r | |
138 | // Perform AES data encryption with CBC mode\r | |
139 | //\r | |
7c342378 | 140 | AES_cbc_encrypt (Input, Output, (UINT32)InputSize, AesKey, IvecBuffer, AES_ENCRYPT);\r |
a8c44645 | 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 | |
16d2c32c | 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 | |
a8c44645 | 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 | |
172 | BOOLEAN\r | |
173 | EFIAPI\r | |
174 | AesCbcDecrypt (\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 | |
16d2c32c | 184 | \r |
185 | //\r | |
186 | // Check input parameters.\r | |
187 | //\r | |
7c342378 | 188 | if ((AesContext == NULL) || (Input == NULL) || ((InputSize % AES_BLOCK_SIZE) != 0)) {\r |
dda39f3a | 189 | return FALSE;\r |
190 | }\r | |
191 | \r | |
7c342378 | 192 | if ((Ivec == NULL) || (Output == NULL) || (InputSize > INT_MAX)) {\r |
16d2c32c | 193 | return FALSE;\r |
194 | }\r | |
a8c44645 | 195 | \r |
7c342378 | 196 | AesKey = (AES_KEY *)AesContext;\r |
a8c44645 | 197 | CopyMem (IvecBuffer, Ivec, AES_BLOCK_SIZE);\r |
198 | \r | |
199 | //\r | |
200 | // Perform AES data decryption with CBC mode\r | |
201 | //\r | |
7c342378 | 202 | AES_cbc_encrypt (Input, Output, (UINT32)InputSize, AesKey + 1, IvecBuffer, AES_DECRYPT);\r |
a8c44645 | 203 | \r |
204 | return TRUE;\r | |
205 | }\r |