]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacMd5.c
19e9fbeae66fe9ab9a105f9249d7d93b8b8d3f12
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacMd5.c
1 /** @file
2 HMAC-MD5 Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "InternalCryptLib.h"
10 #include <openssl/hmac.h>
11
12 //
13 // NOTE: OpenSSL redefines the size of HMAC_CTX at crypto/hmac/hmac_lcl.h
14 // #define HMAC_MAX_MD_CBLOCK_SIZE 144
15 //
16 #define HMAC_MD5_CTX_SIZE (sizeof(void *) * 4 + sizeof(unsigned int) + \
17 sizeof(unsigned char) * 144)
18
19 /**
20 Retrieves the size, in bytes, of the context buffer required for HMAC-MD5 operations.
21 (NOTE: This API is deprecated.
22 Use HmacMd5New() / HmacMd5Free() for HMAC-MD5 Context operations.)
23
24 @return The size, in bytes, of the context buffer required for HMAC-MD5 operations.
25
26 **/
27 UINTN
28 EFIAPI
29 HmacMd5GetContextSize (
30 VOID
31 )
32 {
33 //
34 // Retrieves the OpenSSL HMAC-MD5 Context Size
35 // NOTE: HMAC_CTX object was made opaque in openssl-1.1.x, here we just use the
36 // fixed size as a workaround to make this API work for compatibility.
37 // We should retire HmacMd5GetContextSize() in future, and use HmacMd5New()
38 // and HmacMd5Free() for context allocation and release.
39 //
40 return (UINTN) HMAC_MD5_CTX_SIZE;
41 }
42
43 /**
44 Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD5 use.
45
46 @return Pointer to the HMAC_CTX context that has been initialized.
47 If the allocations fails, HmacMd5New() returns NULL.
48
49 **/
50 VOID *
51 EFIAPI
52 HmacMd5New (
53 VOID
54 )
55 {
56 //
57 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
58 //
59 return (VOID *) HMAC_CTX_new ();
60 }
61
62 /**
63 Release the specified HMAC_CTX context.
64
65 @param[in] HmacMd5Ctx Pointer to the HMAC_CTX context to be released.
66
67 **/
68 VOID
69 EFIAPI
70 HmacMd5Free (
71 IN VOID *HmacMd5Ctx
72 )
73 {
74 //
75 // Free OpenSSL HMAC_CTX Context
76 //
77 HMAC_CTX_free ((HMAC_CTX *)HmacMd5Ctx);
78 }
79
80 /**
81 Initializes user-supplied memory pointed by HmacMd5Context as HMAC-MD5 context for
82 subsequent use.
83
84 If HmacMd5Context is NULL, then return FALSE.
85
86 @param[out] HmacMd5Context Pointer to HMAC-MD5 context being initialized.
87 @param[in] Key Pointer to the user-supplied key.
88 @param[in] KeySize Key size in bytes.
89
90 @retval TRUE HMAC-MD5 context initialization succeeded.
91 @retval FALSE HMAC-MD5 context initialization failed.
92
93 **/
94 BOOLEAN
95 EFIAPI
96 HmacMd5Init (
97 OUT VOID *HmacMd5Context,
98 IN CONST UINT8 *Key,
99 IN UINTN KeySize
100 )
101 {
102 //
103 // Check input parameters.
104 //
105 if (HmacMd5Context == NULL || KeySize > INT_MAX) {
106 return FALSE;
107 }
108
109 //
110 // OpenSSL HMAC-MD5 Context Initialization
111 //
112 memset(HmacMd5Context, 0, HMAC_MD5_CTX_SIZE);
113 if (HMAC_CTX_reset ((HMAC_CTX *)HmacMd5Context) != 1) {
114 return FALSE;
115 }
116 if (HMAC_Init_ex ((HMAC_CTX *)HmacMd5Context, Key, (UINT32) KeySize, EVP_md5(), NULL) != 1) {
117 return FALSE;
118 }
119
120 return TRUE;
121 }
122
123 /**
124 Makes a copy of an existing HMAC-MD5 context.
125
126 If HmacMd5Context is NULL, then return FALSE.
127 If NewHmacMd5Context is NULL, then return FALSE.
128
129 @param[in] HmacMd5Context Pointer to HMAC-MD5 context being copied.
130 @param[out] NewHmacMd5Context Pointer to new HMAC-MD5 context.
131
132 @retval TRUE HMAC-MD5 context copy succeeded.
133 @retval FALSE HMAC-MD5 context copy failed.
134
135 **/
136 BOOLEAN
137 EFIAPI
138 HmacMd5Duplicate (
139 IN CONST VOID *HmacMd5Context,
140 OUT VOID *NewHmacMd5Context
141 )
142 {
143 //
144 // Check input parameters.
145 //
146 if (HmacMd5Context == NULL || NewHmacMd5Context == NULL) {
147 return FALSE;
148 }
149
150 if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacMd5Context, (HMAC_CTX *)HmacMd5Context) != 1) {
151 return FALSE;
152 }
153
154 return TRUE;
155 }
156
157 /**
158 Digests the input data and updates HMAC-MD5 context.
159
160 This function performs HMAC-MD5 digest on a data buffer of the specified size.
161 It can be called multiple times to compute the digest of long or discontinuous data streams.
162 HMAC-MD5 context should be already correctly initialized by HmacMd5Init(), and should not be
163 finalized by HmacMd5Final(). Behavior with invalid context is undefined.
164
165 If HmacMd5Context is NULL, then return FALSE.
166
167 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
168 @param[in] Data Pointer to the buffer containing the data to be digested.
169 @param[in] DataSize Size of Data buffer in bytes.
170
171 @retval TRUE HMAC-MD5 data digest succeeded.
172 @retval FALSE HMAC-MD5 data digest failed.
173
174 **/
175 BOOLEAN
176 EFIAPI
177 HmacMd5Update (
178 IN OUT VOID *HmacMd5Context,
179 IN CONST VOID *Data,
180 IN UINTN DataSize
181 )
182 {
183 //
184 // Check input parameters.
185 //
186 if (HmacMd5Context == NULL) {
187 return FALSE;
188 }
189
190 //
191 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
192 //
193 if (Data == NULL && DataSize != 0) {
194 return FALSE;
195 }
196
197 //
198 // OpenSSL HMAC-MD5 digest update
199 //
200 if (HMAC_Update ((HMAC_CTX *)HmacMd5Context, Data, DataSize) != 1) {
201 return FALSE;
202 }
203
204 return TRUE;
205 }
206
207 /**
208 Completes computation of the HMAC-MD5 digest value.
209
210 This function completes HMAC-MD5 digest computation and retrieves the digest value into
211 the specified memory. After this function has been called, the HMAC-MD5 context cannot
212 be used again.
213 HMAC-MD5 context should be already correctly initialized by HmacMd5Init(), and should not be
214 finalized by HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.
215
216 If HmacMd5Context is NULL, then return FALSE.
217 If HmacValue is NULL, then return FALSE.
218
219 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
220 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD5 digest
221 value (16 bytes).
222
223 @retval TRUE HMAC-MD5 digest computation succeeded.
224 @retval FALSE HMAC-MD5 digest computation failed.
225
226 **/
227 BOOLEAN
228 EFIAPI
229 HmacMd5Final (
230 IN OUT VOID *HmacMd5Context,
231 OUT UINT8 *HmacValue
232 )
233 {
234 UINT32 Length;
235
236 //
237 // Check input parameters.
238 //
239 if (HmacMd5Context == NULL || HmacValue == NULL) {
240 return FALSE;
241 }
242
243 //
244 // OpenSSL HMAC-MD5 digest finalization
245 //
246 if (HMAC_Final ((HMAC_CTX *)HmacMd5Context, HmacValue, &Length) != 1) {
247 return FALSE;
248 }
249 if (HMAC_CTX_reset ((HMAC_CTX *)HmacMd5Context) != 1) {
250 return FALSE;
251 }
252
253 return TRUE;
254 }