TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptAes.c
Go to the documentation of this file.
1
9#include "InternalCryptLib.h"
10#include <mbedtls/aes.h>
11
19EFIAPI
21 VOID
22 )
23{
24 //
25 // AES uses different key contexts for encryption and decryption, so here memory
26 // for 2 copies of mbedtls_aes_context is allocated.
27 //
28 return (UINTN)(2 * sizeof (mbedtls_aes_context));
29}
30
51BOOLEAN
52EFIAPI
54 OUT VOID *AesContext,
55 IN CONST UINT8 *Key,
56 IN UINTN KeyLength
57 )
58{
59 mbedtls_aes_context *AesCtx;
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 AesCtx = (mbedtls_aes_context *)AesContext;
72 if (mbedtls_aes_setkey_enc (AesCtx, Key, (UINT32)KeyLength) != 0) {
73 return FALSE;
74 }
75
76 if (mbedtls_aes_setkey_dec (AesCtx + 1, Key, (UINT32)KeyLength) != 0) {
77 return FALSE;
78 }
79
80 return TRUE;
81}
82
110BOOLEAN
111EFIAPI
113 IN VOID *AesContext,
114 IN CONST UINT8 *Input,
115 IN UINTN InputSize,
116 IN CONST UINT8 *Ivec,
117 OUT UINT8 *Output
118 )
119{
120 mbedtls_aes_context *AesCtx;
121 UINT8 IvecBuffer[AES_BLOCK_SIZE];
122
123 //
124 // Check input parameters.
125 //
126 if ((AesContext == NULL) || (Input == NULL) || ((InputSize % AES_BLOCK_SIZE) != 0)) {
127 return FALSE;
128 }
129
130 if ((Ivec == NULL) || (Output == NULL) || (InputSize > INT_MAX)) {
131 return FALSE;
132 }
133
134 AesCtx = (mbedtls_aes_context *)AesContext;
135 CopyMem (IvecBuffer, Ivec, AES_BLOCK_SIZE);
136
137 //
138 // Perform AES data encryption with CBC mode
139 //
140 if (mbedtls_aes_crypt_cbc (
141 AesCtx,
142 MBEDTLS_AES_ENCRYPT,
143 (UINT32)InputSize,
144 IvecBuffer,
145 Input,
146 Output
147 ) != 0)
148 {
149 return FALSE;
150 } else {
151 return TRUE;
152 }
153}
154
182BOOLEAN
183EFIAPI
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 mbedtls_aes_context *AesCtx;
193 UINT8 IvecBuffer[AES_BLOCK_SIZE];
194
195 //
196 // Check input parameters.
197 //
198 if ((AesContext == NULL) || (Input == NULL) || ((InputSize % AES_BLOCK_SIZE) != 0)) {
199 return FALSE;
200 }
201
202 if ((Ivec == NULL) || (Output == NULL) || (InputSize > INT_MAX)) {
203 return FALSE;
204 }
205
206 AesCtx = (mbedtls_aes_context *)AesContext;
207 CopyMem (IvecBuffer, Ivec, AES_BLOCK_SIZE);
208
209 //
210 // Perform AES data encryption with CBC mode
211 //
212 if (mbedtls_aes_crypt_cbc (
213 AesCtx + 1,
214 MBEDTLS_AES_DECRYPT,
215 (UINT32)InputSize,
216 IvecBuffer,
217 Input,
218 Output
219 ) != 0)
220 {
221 return FALSE;
222 } else {
223 return TRUE;
224 }
225}
UINT64 UINTN
#define AES_BLOCK_SIZE
Definition: BaseCryptLib.h:69
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI AesInit(OUT VOID *AesContext, IN CONST UINT8 *Key, IN UINTN KeyLength)
Definition: CryptAes.c:53
UINTN EFIAPI AesGetContextSize(VOID)
Definition: CryptAes.c:20
BOOLEAN EFIAPI AesCbcEncrypt(IN VOID *AesContext, IN CONST UINT8 *Input, IN UINTN InputSize, IN CONST UINT8 *Ivec, OUT UINT8 *Output)
Definition: CryptAes.c:112
BOOLEAN EFIAPI AesCbcDecrypt(IN VOID *AesContext, IN CONST UINT8 *Input, IN UINTN InputSize, IN CONST UINT8 *Ivec, OUT UINT8 *Output)
Definition: CryptAes.c:174
VOID EFIAPI Input(IN CHAR16 *Prompt OPTIONAL, OUT CHAR16 *InStr, IN UINTN StrLen)
Definition: EdbSupportUI.c:187
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284