TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptAes.c
Go to the documentation of this file.
1
9#include "InternalCryptLib.h"
10#include <openssl/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 AES_KEY is allocated.
27 //
28 return (UINTN)(2 * sizeof (AES_KEY));
29}
30
51BOOLEAN
52EFIAPI
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
76 if (AES_set_decrypt_key (Key, (UINT32)KeyLength, AesKey + 1) != 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 AES_KEY *AesKey;
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 AesKey = (AES_KEY *)AesContext;
135 CopyMem (IvecBuffer, Ivec, AES_BLOCK_SIZE);
136
137 //
138 // Perform AES data encryption with CBC mode
139 //
140 AES_cbc_encrypt (Input, Output, (UINT32)InputSize, AesKey, IvecBuffer, AES_ENCRYPT);
141
142 return TRUE;
143}
144
172BOOLEAN
173EFIAPI
175 IN VOID *AesContext,
176 IN CONST UINT8 *Input,
177 IN UINTN InputSize,
178 IN CONST UINT8 *Ivec,
179 OUT UINT8 *Output
180 )
181{
182 AES_KEY *AesKey;
183 UINT8 IvecBuffer[AES_BLOCK_SIZE];
184
185 //
186 // Check input parameters.
187 //
188 if ((AesContext == NULL) || (Input == NULL) || ((InputSize % AES_BLOCK_SIZE) != 0)) {
189 return FALSE;
190 }
191
192 if ((Ivec == NULL) || (Output == NULL) || (InputSize > INT_MAX)) {
193 return FALSE;
194 }
195
196 AesKey = (AES_KEY *)AesContext;
197 CopyMem (IvecBuffer, Ivec, AES_BLOCK_SIZE);
198
199 //
200 // Perform AES data decryption with CBC mode
201 //
202 AES_cbc_encrypt (Input, Output, (UINT32)InputSize, AesKey + 1, IvecBuffer, AES_DECRYPT);
203
204 return TRUE;
205}
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