]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacMd5.c
1. Add new API supports for PEM & X509 key retrieving & verification;
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacMd5.c
CommitLineData
a8c44645 1/** @file\r
2 HMAC-MD5 Wrapper Implementation over OpenSSL.\r
3\r
4Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "InternalCryptLib.h"\r
16#include <openssl/hmac.h>\r
17\r
18/**\r
19 Retrieves the size, in bytes, of the context buffer required for HMAC-MD5 operations.\r
20\r
21 @return The size, in bytes, of the context buffer required for HMAC-MD5 operations.\r
22\r
23**/\r
24UINTN\r
25EFIAPI\r
26HmacMd5GetContextSize (\r
27 VOID\r
28 )\r
29{\r
30 //\r
31 // Retrieves the OpenSSL HMAC-MD5 Context Size\r
32 //\r
33 return (UINTN)(sizeof (HMAC_CTX));\r
34}\r
35\r
36/**\r
37 Initializes user-supplied memory pointed by HmacMd5Context as HMAC-MD5 context for\r
38 subsequent use.\r
39\r
40 If HmacMd5Context is NULL, then ASSERT().\r
41\r
42 @param[out] HmacMd5Context Pointer to HMAC-MD5 context being initialized.\r
43 @param[in] Key Pointer to the user-supplied key.\r
44 @param[in] KeySize Key size in bytes.\r
45\r
46 @retval TRUE HMAC-MD5 context initialization succeeded.\r
47 @retval FALSE HMAC-MD5 context initialization failed.\r
48\r
49**/\r
50BOOLEAN\r
51EFIAPI\r
52HmacMd5Init (\r
53 OUT VOID *HmacMd5Context,\r
54 IN CONST UINT8 *Key,\r
55 IN UINTN KeySize\r
56 )\r
57{\r
58 //\r
59 // ASSERT if HmacMd5Context is NULL.\r
60 //\r
61 ASSERT (HmacMd5Context != NULL);\r
62\r
63 //\r
64 // OpenSSL HMAC-MD5 Context Initialization\r
65 //\r
66 HMAC_CTX_init (HmacMd5Context);\r
67 HMAC_Init_ex (HmacMd5Context, Key, (UINT32) KeySize, EVP_md5(), NULL);\r
68\r
69 return TRUE;\r
70}\r
71\r
72/**\r
73 Makes a copy of an existing HMAC-MD5 context.\r
74\r
75 If HmacMd5Context is NULL, then ASSERT().\r
76 If NewHmacMd5Context is NULL, then ASSERT().\r
77\r
78 @param[in] HmacMd5Context Pointer to HMAC-MD5 context being copied.\r
79 @param[out] NewHmacMd5Context Pointer to new HMAC-MD5 context.\r
80\r
81 @retval TRUE HMAC-MD5 context copy succeeded.\r
82 @retval FALSE HMAC-MD5 context copy failed.\r
83\r
84**/\r
85BOOLEAN\r
86EFIAPI\r
87HmacMd5Duplicate (\r
88 IN CONST VOID *HmacMd5Context,\r
89 OUT VOID *NewHmacMd5Context\r
90 )\r
91{\r
4a567c96 92 //\r
93 // ASSERT if HmacMd5Context or NewHmacMd5Context is NULL.\r
94 //\r
95 ASSERT (HmacMd5Context != NULL);\r
96 ASSERT (NewHmacMd5Context != NULL);\r
97 \r
a8c44645 98 CopyMem (NewHmacMd5Context, HmacMd5Context, sizeof (HMAC_CTX));\r
99\r
100 return TRUE;\r
101}\r
102\r
103/**\r
104 Digests the input data and updates HMAC-MD5 context.\r
105\r
106 This function performs HMAC-MD5 digest on a data buffer of the specified size.\r
107 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
108 HMAC-MD5 context should be already correctly intialized by HmacMd5Init(), and should not be\r
109 finalized by HmacMd5Final(). Behavior with invalid context is undefined.\r
110\r
111 If HmacMd5Context is NULL, then ASSERT().\r
112\r
113 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.\r
114 @param[in] Data Pointer to the buffer containing the data to be digested.\r
115 @param[in] DataSize Size of Data buffer in bytes.\r
116\r
117 @retval TRUE HMAC-MD5 data digest succeeded.\r
118 @retval FALSE HMAC-MD5 data digest failed.\r
119\r
120**/\r
121BOOLEAN\r
122EFIAPI\r
123HmacMd5Update (\r
124 IN OUT VOID *HmacMd5Context,\r
125 IN CONST VOID *Data,\r
126 IN UINTN DataSize\r
127 )\r
128{\r
129 //\r
130 // ASSERT if HmacMd5Context is NULL\r
131 //\r
132 ASSERT (HmacMd5Context != NULL);\r
133\r
134 //\r
135 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL\r
136 //\r
137 if (Data == NULL) {\r
138 ASSERT (DataSize == 0);\r
139 }\r
140\r
141 //\r
142 // OpenSSL HMAC-MD5 digest update\r
143 //\r
144 HMAC_Update (HmacMd5Context, Data, DataSize);\r
145\r
146 return TRUE;\r
147}\r
148\r
149/**\r
150 Completes computation of the HMAC-MD5 digest value.\r
151\r
152 This function completes HMAC-MD5 digest computation and retrieves the digest value into\r
153 the specified memory. After this function has been called, the HMAC-MD5 context cannot\r
154 be used again.\r
155 HMAC-MD5 context should be already correctly intialized by HmacMd5Init(), and should not be\r
156 finalized by HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.\r
157\r
158 If HmacMd5Context is NULL, then ASSERT().\r
159 If HmacValue is NULL, then ASSERT().\r
160\r
161 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.\r
162 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD5 digest\r
163 value (16 bytes).\r
164\r
165 @retval TRUE HMAC-MD5 digest computation succeeded.\r
166 @retval FALSE HMAC-MD5 digest computation failed.\r
167\r
168**/\r
169BOOLEAN\r
170EFIAPI\r
171HmacMd5Final (\r
172 IN OUT VOID *HmacMd5Context,\r
173 OUT UINT8 *HmacValue\r
174 )\r
175{\r
176 UINT32 Length;\r
177\r
178 //\r
179 // ASSERT if HmacMd5Context is NULL or HmacValue is NULL\r
180 //\r
181 ASSERT (HmacMd5Context != NULL);\r
182 ASSERT (HmacValue != NULL);\r
183\r
184 //\r
185 // OpenSSL HMAC-MD5 digest finalization\r
186 //\r
187 HMAC_Final (HmacMd5Context, HmacValue, &Length);\r
188 HMAC_CTX_cleanup (HmacMd5Context);\r
189\r
190 return TRUE;\r
191}\r