TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptAeadAesGcm.c
Go to the documentation of this file.
1
12#include "InternalCryptLib.h"
13#include <mbedtls/gcm.h>
14
39BOOLEAN
40EFIAPI
42 IN CONST UINT8 *Key,
43 IN UINTN KeySize,
44 IN CONST UINT8 *Iv,
45 IN UINTN IvSize,
46 IN CONST UINT8 *AData,
47 IN UINTN ADataSize,
48 IN CONST UINT8 *DataIn,
49 IN UINTN DataInSize,
50 OUT UINT8 *TagOut,
51 IN UINTN TagSize,
52 OUT UINT8 *DataOut,
53 OUT UINTN *DataOutSize
54 )
55{
56 mbedtls_gcm_context Ctx;
57 INT32 Ret;
58
59 if (DataInSize > INT_MAX) {
60 return FALSE;
61 }
62
63 if (ADataSize > INT_MAX) {
64 return FALSE;
65 }
66
67 if (IvSize != 12) {
68 return FALSE;
69 }
70
71 switch (KeySize) {
72 case 16:
73 case 24:
74 case 32:
75 break;
76 default:
77 return FALSE;
78 }
79
80 if ((TagSize != 12) && (TagSize != 13) && (TagSize != 14) && (TagSize != 15) && (TagSize != 16)) {
81 return FALSE;
82 }
83
84 if (DataOutSize != NULL) {
85 if ((*DataOutSize > INT_MAX) || (*DataOutSize < DataInSize)) {
86 return FALSE;
87 }
88 }
89
90 mbedtls_gcm_init (&Ctx);
91
92 Ret = mbedtls_gcm_setkey (&Ctx, MBEDTLS_CIPHER_ID_AES, Key, (UINT32)(KeySize * 8));
93 if (Ret != 0) {
94 return FALSE;
95 }
96
97 Ret = mbedtls_gcm_crypt_and_tag (
98 &Ctx,
99 MBEDTLS_GCM_ENCRYPT,
100 (UINT32)DataInSize,
101 Iv,
102 (UINT32)IvSize,
103 AData,
104 (UINT32)ADataSize,
105 DataIn,
106 DataOut,
107 TagSize,
108 TagOut
109 );
110 mbedtls_gcm_free (&Ctx);
111 if (Ret != 0) {
112 return FALSE;
113 }
114
115 if (DataOutSize != NULL) {
116 *DataOutSize = DataInSize;
117 }
118
119 return TRUE;
120}
121
147BOOLEAN
148EFIAPI
150 IN CONST UINT8 *Key,
151 IN UINTN KeySize,
152 IN CONST UINT8 *Iv,
153 IN UINTN IvSize,
154 IN CONST UINT8 *AData,
155 IN UINTN ADataSize,
156 IN CONST UINT8 *DataIn,
157 IN UINTN DataInSize,
158 IN CONST UINT8 *Tag,
159 IN UINTN TagSize,
160 OUT UINT8 *DataOut,
161 OUT UINTN *DataOutSize
162 )
163{
164 mbedtls_gcm_context Ctx;
165 INT32 Ret;
166
167 if (DataInSize > INT_MAX) {
168 return FALSE;
169 }
170
171 if (ADataSize > INT_MAX) {
172 return FALSE;
173 }
174
175 if (IvSize != 12) {
176 return FALSE;
177 }
178
179 switch (KeySize) {
180 case 16:
181 case 24:
182 case 32:
183 break;
184 default:
185 return FALSE;
186 }
187
188 if ((TagSize != 12) && (TagSize != 13) && (TagSize != 14) && (TagSize != 15) && (TagSize != 16)) {
189 return FALSE;
190 }
191
192 if (DataOutSize != NULL) {
193 if ((*DataOutSize > INT_MAX) || (*DataOutSize < DataInSize)) {
194 return FALSE;
195 }
196 }
197
198 mbedtls_gcm_init (&Ctx);
199
200 Ret = mbedtls_gcm_setkey (&Ctx, MBEDTLS_CIPHER_ID_AES, Key, (UINT32)(KeySize * 8));
201 if (Ret != 0) {
202 return FALSE;
203 }
204
205 Ret = mbedtls_gcm_auth_decrypt (
206 &Ctx,
207 (UINT32)DataInSize,
208 Iv,
209 (UINT32)IvSize,
210 AData,
211 (UINT32)ADataSize,
212 Tag,
213 (UINT32)TagSize,
214 DataIn,
215 DataOut
216 );
217 mbedtls_gcm_free (&Ctx);
218 if (Ret != 0) {
219 return FALSE;
220 }
221
222 if (DataOutSize != NULL) {
223 *DataOutSize = DataInSize;
224 }
225
226 return TRUE;
227}
UINT64 UINTN
BOOLEAN EFIAPI AeadAesGcmDecrypt(IN CONST UINT8 *Key, IN UINTN KeySize, IN CONST UINT8 *Iv, IN UINTN IvSize, IN CONST UINT8 *AData, IN UINTN ADataSize, IN CONST UINT8 *DataIn, IN UINTN DataInSize, IN CONST UINT8 *Tag, IN UINTN TagSize, OUT UINT8 *DataOut, OUT UINTN *DataOutSize)
BOOLEAN EFIAPI AeadAesGcmEncrypt(IN CONST UINT8 *Key, IN UINTN KeySize, IN CONST UINT8 *Iv, IN UINTN IvSize, IN CONST UINT8 *AData, IN UINTN ADataSize, IN CONST UINT8 *DataIn, IN UINTN DataInSize, OUT UINT8 *TagOut, IN UINTN TagSize, OUT UINT8 *DataOut, OUT UINTN *DataOutSize)
#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