]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c
3fdef064f93a4e80ffcfc79674b685aaa1962c6b
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacSha256.c
1 /** @file
2 HMAC-SHA256 Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2016 - 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-SHA256 use.
14
15 @return Pointer to the HMAC_CTX context that has been initialized.
16 If the allocations fails, HmacSha256New() returns NULL.
17
18 **/
19 VOID *
20 EFIAPI
21 HmacSha256New (
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] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
35
36 **/
37 VOID
38 EFIAPI
39 HmacSha256Free (
40 IN VOID *HmacSha256Ctx
41 )
42 {
43 //
44 // Free OpenSSL HMAC_CTX Context
45 //
46 HMAC_CTX_free ((HMAC_CTX *)HmacSha256Ctx);
47 }
48
49 /**
50 Set user-supplied key for subsequent use. It must be done before any
51 calling to HmacSha256Update().
52
53 If HmacSha256Context is NULL, then return FALSE.
54
55 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.
56 @param[in] Key Pointer to the user-supplied key.
57 @param[in] KeySize Key size in bytes.
58
59 @retval TRUE The Key is set successfully.
60 @retval FALSE The Key is set unsuccessfully.
61
62 **/
63 BOOLEAN
64 EFIAPI
65 HmacSha256SetKey (
66 OUT VOID *HmacSha256Context,
67 IN CONST UINT8 *Key,
68 IN UINTN KeySize
69 )
70 {
71 //
72 // Check input parameters.
73 //
74 if (HmacSha256Context == NULL || KeySize > INT_MAX) {
75 return FALSE;
76 }
77
78 if (HMAC_Init_ex ((HMAC_CTX *)HmacSha256Context, Key, (UINT32) KeySize, EVP_sha256(), NULL) != 1) {
79 return FALSE;
80 }
81
82 return TRUE;
83 }
84
85 /**
86 Makes a copy of an existing HMAC-SHA256 context.
87
88 If HmacSha256Context is NULL, then return FALSE.
89 If NewHmacSha256Context is NULL, then return FALSE.
90
91 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
92 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
93
94 @retval TRUE HMAC-SHA256 context copy succeeded.
95 @retval FALSE HMAC-SHA256 context copy failed.
96
97 **/
98 BOOLEAN
99 EFIAPI
100 HmacSha256Duplicate (
101 IN CONST VOID *HmacSha256Context,
102 OUT VOID *NewHmacSha256Context
103 )
104 {
105 //
106 // Check input parameters.
107 //
108 if (HmacSha256Context == NULL || NewHmacSha256Context == NULL) {
109 return FALSE;
110 }
111
112 if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha256Context, (HMAC_CTX *)HmacSha256Context) != 1) {
113 return FALSE;
114 }
115
116 return TRUE;
117 }
118
119 /**
120 Digests the input data and updates HMAC-SHA256 context.
121
122 This function performs HMAC-SHA256 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-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
125 by HmacSha256Final(). Behavior with invalid context is undefined.
126
127 If HmacSha256Context is NULL, then return FALSE.
128
129 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 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-SHA256 data digest succeeded.
134 @retval FALSE HMAC-SHA256 data digest failed.
135
136 **/
137 BOOLEAN
138 EFIAPI
139 HmacSha256Update (
140 IN OUT VOID *HmacSha256Context,
141 IN CONST VOID *Data,
142 IN UINTN DataSize
143 )
144 {
145 //
146 // Check input parameters.
147 //
148 if (HmacSha256Context == 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-SHA256 digest update
161 //
162 if (HMAC_Update ((HMAC_CTX *)HmacSha256Context, Data, DataSize) != 1) {
163 return FALSE;
164 }
165
166 return TRUE;
167 }
168
169 /**
170 Completes computation of the HMAC-SHA256 digest value.
171
172 This function completes HMAC-SHA256 hash computation and retrieves the digest value into
173 the specified memory. After this function has been called, the HMAC-SHA256 context cannot
174 be used again.
175 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
176 by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
177
178 If HmacSha256Context is NULL, then return FALSE.
179 If HmacValue is NULL, then return FALSE.
180
181 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
182 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
183 value (32 bytes).
184
185 @retval TRUE HMAC-SHA256 digest computation succeeded.
186 @retval FALSE HMAC-SHA256 digest computation failed.
187
188 **/
189 BOOLEAN
190 EFIAPI
191 HmacSha256Final (
192 IN OUT VOID *HmacSha256Context,
193 OUT UINT8 *HmacValue
194 )
195 {
196 UINT32 Length;
197
198 //
199 // Check input parameters.
200 //
201 if (HmacSha256Context == NULL || HmacValue == NULL) {
202 return FALSE;
203 }
204
205 //
206 // OpenSSL HMAC-SHA256 digest finalization
207 //
208 if (HMAC_Final ((HMAC_CTX *)HmacSha256Context, HmacValue, &Length) != 1) {
209 return FALSE;
210 }
211 if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) {
212 return FALSE;
213 }
214
215 return TRUE;
216 }