]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacMd5.c
CryptoPkg/BaseCryptLib: remove HmacXxxGetContextSize interface
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacMd5.c
1 /** @file
2 HMAC-MD5 Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2010 - 2020, 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 Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD5 use.
14
15 @return Pointer to the HMAC_CTX context that has been initialized.
16 If the allocations fails, HmacMd5New() returns NULL.
17
18 **/
19 VOID *
20 EFIAPI
21 HmacMd5New (
22 VOID
23 )
24 {
25 //
26 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
27 //
28 return (VOID *) HMAC_CTX_new ();
29 }
30
31 /**
32 Release the specified HMAC_CTX context.
33
34 @param[in] HmacMd5Ctx Pointer to the HMAC_CTX context to be released.
35
36 **/
37 VOID
38 EFIAPI
39 HmacMd5Free (
40 IN VOID *HmacMd5Ctx
41 )
42 {
43 //
44 // Free OpenSSL HMAC_CTX Context
45 //
46 HMAC_CTX_free ((HMAC_CTX *)HmacMd5Ctx);
47 }
48
49 /**
50 Set user-supplied key for subsequent use. It must be done before any
51 calling to HmacMd5Update().
52
53 If HmacMd5Context is NULL, then return FALSE.
54
55 @param[out] HmacMd5Context Pointer to HMAC-MD5 context.
56 @param[in] Key Pointer to the user-supplied key.
57 @param[in] KeySize Key size in bytes.
58
59 @retval TRUE Key is set successfully.
60 @retval FALSE Key is set unsuccessfully.
61
62 **/
63 BOOLEAN
64 EFIAPI
65 HmacMd5SetKey (
66 OUT VOID *HmacMd5Context,
67 IN CONST UINT8 *Key,
68 IN UINTN KeySize
69 )
70 {
71 //
72 // Check input parameters.
73 //
74 if (HmacMd5Context == NULL || KeySize > INT_MAX) {
75 return FALSE;
76 }
77
78 if (HMAC_Init_ex ((HMAC_CTX *)HmacMd5Context, Key, (UINT32) KeySize, EVP_md5(), NULL) != 1) {
79 return FALSE;
80 }
81
82 return TRUE;
83 }
84
85 /**
86 Makes a copy of an existing HMAC-MD5 context.
87
88 If HmacMd5Context is NULL, then return FALSE.
89 If NewHmacMd5Context is NULL, then return FALSE.
90
91 @param[in] HmacMd5Context Pointer to HMAC-MD5 context being copied.
92 @param[out] NewHmacMd5Context Pointer to new HMAC-MD5 context.
93
94 @retval TRUE HMAC-MD5 context copy succeeded.
95 @retval FALSE HMAC-MD5 context copy failed.
96
97 **/
98 BOOLEAN
99 EFIAPI
100 HmacMd5Duplicate (
101 IN CONST VOID *HmacMd5Context,
102 OUT VOID *NewHmacMd5Context
103 )
104 {
105 //
106 // Check input parameters.
107 //
108 if (HmacMd5Context == NULL || NewHmacMd5Context == NULL) {
109 return FALSE;
110 }
111
112 if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacMd5Context, (HMAC_CTX *)HmacMd5Context) != 1) {
113 return FALSE;
114 }
115
116 return TRUE;
117 }
118
119 /**
120 Digests the input data and updates HMAC-MD5 context.
121
122 This function performs HMAC-MD5 digest on a data buffer of the specified size.
123 It can be called multiple times to compute the digest of long or discontinuous data streams.
124 HMAC-MD5 context should be initialized by HmacMd5New(), and should not be finalized by
125 HmacMd5Final(). Behavior with invalid context is undefined.
126
127 If HmacMd5Context is NULL, then return FALSE.
128
129 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
130 @param[in] Data Pointer to the buffer containing the data to be digested.
131 @param[in] DataSize Size of Data buffer in bytes.
132
133 @retval TRUE HMAC-MD5 data digest succeeded.
134 @retval FALSE HMAC-MD5 data digest failed.
135
136 **/
137 BOOLEAN
138 EFIAPI
139 HmacMd5Update (
140 IN OUT VOID *HmacMd5Context,
141 IN CONST VOID *Data,
142 IN UINTN DataSize
143 )
144 {
145 //
146 // Check input parameters.
147 //
148 if (HmacMd5Context == NULL) {
149 return FALSE;
150 }
151
152 //
153 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
154 //
155 if (Data == NULL && DataSize != 0) {
156 return FALSE;
157 }
158
159 //
160 // OpenSSL HMAC-MD5 digest update
161 //
162 if (HMAC_Update ((HMAC_CTX *)HmacMd5Context, Data, DataSize) != 1) {
163 return FALSE;
164 }
165
166 return TRUE;
167 }
168
169 /**
170 Completes computation of the HMAC-MD5 digest value.
171
172 This function completes HMAC-MD5 digest computation and retrieves the digest value into
173 the specified memory. After this function has been called, the HMAC-MD5 context cannot
174 be used again.
175 HMAC-MD5 context should be initialized by HmacMd5New(), and should not be finalized by
176 HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.
177
178 If HmacMd5Context is NULL, then return FALSE.
179 If HmacValue is NULL, then return FALSE.
180
181 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
182 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD5 digest
183 value (16 bytes).
184
185 @retval TRUE HMAC-MD5 digest computation succeeded.
186 @retval FALSE HMAC-MD5 digest computation failed.
187
188 **/
189 BOOLEAN
190 EFIAPI
191 HmacMd5Final (
192 IN OUT VOID *HmacMd5Context,
193 OUT UINT8 *HmacValue
194 )
195 {
196 UINT32 Length;
197
198 //
199 // Check input parameters.
200 //
201 if (HmacMd5Context == NULL || HmacValue == NULL) {
202 return FALSE;
203 }
204
205 //
206 // OpenSSL HMAC-MD5 digest finalization
207 //
208 if (HMAC_Final ((HMAC_CTX *)HmacMd5Context, HmacValue, &Length) != 1) {
209 return FALSE;
210 }
211 if (HMAC_CTX_reset ((HMAC_CTX *)HmacMd5Context) != 1) {
212 return FALSE;
213 }
214
215 return TRUE;
216 }