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