]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacMd5.c
1eff5c4978b57caec59a2ffad15bd391dfbcc158
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacMd5.c
1 /** @file
2 HMAC-MD5 Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2010, 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 /**
19 Retrieves the size, in bytes, of the context buffer required for HMAC-MD5 operations.
20
21 @return The size, in bytes, of the context buffer required for HMAC-MD5 operations.
22
23 **/
24 UINTN
25 EFIAPI
26 HmacMd5GetContextSize (
27 VOID
28 )
29 {
30 //
31 // Retrieves the OpenSSL HMAC-MD5 Context Size
32 //
33 return (UINTN)(sizeof (HMAC_CTX));
34 }
35
36 /**
37 Initializes user-supplied memory pointed by HmacMd5Context as HMAC-MD5 context for
38 subsequent use.
39
40 If HmacMd5Context is NULL, then ASSERT().
41
42 @param[out] HmacMd5Context Pointer to HMAC-MD5 context being initialized.
43 @param[in] Key Pointer to the user-supplied key.
44 @param[in] KeySize Key size in bytes.
45
46 @retval TRUE HMAC-MD5 context initialization succeeded.
47 @retval FALSE HMAC-MD5 context initialization failed.
48
49 **/
50 BOOLEAN
51 EFIAPI
52 HmacMd5Init (
53 OUT VOID *HmacMd5Context,
54 IN CONST UINT8 *Key,
55 IN UINTN KeySize
56 )
57 {
58 //
59 // ASSERT if HmacMd5Context is NULL.
60 //
61 ASSERT (HmacMd5Context != NULL);
62
63 //
64 // OpenSSL HMAC-MD5 Context Initialization
65 //
66 HMAC_CTX_init (HmacMd5Context);
67 HMAC_Init_ex (HmacMd5Context, Key, (UINT32) KeySize, EVP_md5(), NULL);
68
69 return TRUE;
70 }
71
72 /**
73 Makes a copy of an existing HMAC-MD5 context.
74
75 If HmacMd5Context is NULL, then ASSERT().
76 If NewHmacMd5Context is NULL, then ASSERT().
77
78 @param[in] HmacMd5Context Pointer to HMAC-MD5 context being copied.
79 @param[out] NewHmacMd5Context Pointer to new HMAC-MD5 context.
80
81 @retval TRUE HMAC-MD5 context copy succeeded.
82 @retval FALSE HMAC-MD5 context copy failed.
83
84 **/
85 BOOLEAN
86 EFIAPI
87 HmacMd5Duplicate (
88 IN CONST VOID *HmacMd5Context,
89 OUT VOID *NewHmacMd5Context
90 )
91 {
92 CopyMem (NewHmacMd5Context, HmacMd5Context, sizeof (HMAC_CTX));
93
94 return TRUE;
95 }
96
97 /**
98 Digests the input data and updates HMAC-MD5 context.
99
100 This function performs HMAC-MD5 digest on a data buffer of the specified size.
101 It can be called multiple times to compute the digest of long or discontinuous data streams.
102 HMAC-MD5 context should be already correctly intialized by HmacMd5Init(), and should not be
103 finalized by HmacMd5Final(). Behavior with invalid context is undefined.
104
105 If HmacMd5Context is NULL, then ASSERT().
106
107 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
108 @param[in] Data Pointer to the buffer containing the data to be digested.
109 @param[in] DataSize Size of Data buffer in bytes.
110
111 @retval TRUE HMAC-MD5 data digest succeeded.
112 @retval FALSE HMAC-MD5 data digest failed.
113
114 **/
115 BOOLEAN
116 EFIAPI
117 HmacMd5Update (
118 IN OUT VOID *HmacMd5Context,
119 IN CONST VOID *Data,
120 IN UINTN DataSize
121 )
122 {
123 //
124 // ASSERT if HmacMd5Context is NULL
125 //
126 ASSERT (HmacMd5Context != NULL);
127
128 //
129 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL
130 //
131 if (Data == NULL) {
132 ASSERT (DataSize == 0);
133 }
134
135 //
136 // OpenSSL HMAC-MD5 digest update
137 //
138 HMAC_Update (HmacMd5Context, Data, DataSize);
139
140 return TRUE;
141 }
142
143 /**
144 Completes computation of the HMAC-MD5 digest value.
145
146 This function completes HMAC-MD5 digest computation and retrieves the digest value into
147 the specified memory. After this function has been called, the HMAC-MD5 context cannot
148 be used again.
149 HMAC-MD5 context should be already correctly intialized by HmacMd5Init(), and should not be
150 finalized by HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.
151
152 If HmacMd5Context is NULL, then ASSERT().
153 If HmacValue is NULL, then ASSERT().
154
155 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
156 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD5 digest
157 value (16 bytes).
158
159 @retval TRUE HMAC-MD5 digest computation succeeded.
160 @retval FALSE HMAC-MD5 digest computation failed.
161
162 **/
163 BOOLEAN
164 EFIAPI
165 HmacMd5Final (
166 IN OUT VOID *HmacMd5Context,
167 OUT UINT8 *HmacValue
168 )
169 {
170 UINT32 Length;
171
172 //
173 // ASSERT if HmacMd5Context is NULL or HmacValue is NULL
174 //
175 ASSERT (HmacMd5Context != NULL);
176 ASSERT (HmacValue != NULL);
177
178 //
179 // OpenSSL HMAC-MD5 digest finalization
180 //
181 HMAC_Final (HmacMd5Context, HmacValue, &Length);
182 HMAC_CTX_cleanup (HmacMd5Context);
183
184 return TRUE;
185 }