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