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