]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacMd5.c
Update CryptoPkg for new ciphers (HMAC, Block Cipher, etc) supports.
[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
92 CopyMem (NewHmacMd5Context, HmacMd5Context, sizeof (HMAC_CTX));\r
93\r
94 return TRUE;\r
95}\r
96\r
97/**\r
98 Digests the input data and updates HMAC-MD5 context.\r
99\r
100 This function performs HMAC-MD5 digest on a data buffer of the specified size.\r
101 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
102 HMAC-MD5 context should be already correctly intialized by HmacMd5Init(), and should not be\r
103 finalized by HmacMd5Final(). Behavior with invalid context is undefined.\r
104\r
105 If HmacMd5Context is NULL, then ASSERT().\r
106\r
107 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.\r
108 @param[in] Data Pointer to the buffer containing the data to be digested.\r
109 @param[in] DataSize Size of Data buffer in bytes.\r
110\r
111 @retval TRUE HMAC-MD5 data digest succeeded.\r
112 @retval FALSE HMAC-MD5 data digest failed.\r
113\r
114**/\r
115BOOLEAN\r
116EFIAPI\r
117HmacMd5Update (\r
118 IN OUT VOID *HmacMd5Context,\r
119 IN CONST VOID *Data,\r
120 IN UINTN DataSize\r
121 )\r
122{\r
123 //\r
124 // ASSERT if HmacMd5Context is NULL\r
125 //\r
126 ASSERT (HmacMd5Context != NULL);\r
127\r
128 //\r
129 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL\r
130 //\r
131 if (Data == NULL) {\r
132 ASSERT (DataSize == 0);\r
133 }\r
134\r
135 //\r
136 // OpenSSL HMAC-MD5 digest update\r
137 //\r
138 HMAC_Update (HmacMd5Context, Data, DataSize);\r
139\r
140 return TRUE;\r
141}\r
142\r
143/**\r
144 Completes computation of the HMAC-MD5 digest value.\r
145\r
146 This function completes HMAC-MD5 digest computation and retrieves the digest value into\r
147 the specified memory. After this function has been called, the HMAC-MD5 context cannot\r
148 be used again.\r
149 HMAC-MD5 context should be already correctly intialized by HmacMd5Init(), and should not be\r
150 finalized by HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.\r
151\r
152 If HmacMd5Context is NULL, then ASSERT().\r
153 If HmacValue is NULL, then ASSERT().\r
154\r
155 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.\r
156 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD5 digest\r
157 value (16 bytes).\r
158\r
159 @retval TRUE HMAC-MD5 digest computation succeeded.\r
160 @retval FALSE HMAC-MD5 digest computation failed.\r
161\r
162**/\r
163BOOLEAN\r
164EFIAPI\r
165HmacMd5Final (\r
166 IN OUT VOID *HmacMd5Context,\r
167 OUT UINT8 *HmacValue\r
168 )\r
169{\r
170 UINT32 Length;\r
171\r
172 //\r
173 // ASSERT if HmacMd5Context is NULL or HmacValue is NULL\r
174 //\r
175 ASSERT (HmacMd5Context != NULL);\r
176 ASSERT (HmacValue != NULL);\r
177\r
178 //\r
179 // OpenSSL HMAC-MD5 digest finalization\r
180 //\r
181 HMAC_Final (HmacMd5Context, HmacValue, &Length);\r
182 HMAC_CTX_cleanup (HmacMd5Context);\r
183\r
184 return TRUE;\r
185}\r