]>
Commit | Line | Data |
---|---|---|
a8c44645 | 1 | /** @file\r |
2 | HMAC-SHA1 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-SHA1 operations.\r | |
20 | \r | |
21 | @return The size, in bytes, of the context buffer required for HMAC-SHA1 operations.\r | |
22 | \r | |
23 | **/\r | |
24 | UINTN\r | |
25 | EFIAPI\r | |
26 | HmacSha1GetContextSize (\r | |
27 | VOID\r | |
28 | )\r | |
29 | {\r | |
30 | //\r | |
31 | // Retrieves the OpenSSL HMAC-SHA1 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 HmacSha1Context as HMAC-SHA1 context for\r | |
38 | subsequent use.\r | |
39 | \r | |
16d2c32c | 40 | If HmacSha1Context is NULL, then return FALSE.\r |
a8c44645 | 41 | \r |
42 | @param[out] HmacSha1Context Pointer to HMAC-SHA1 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-SHA1 context initialization succeeded.\r | |
47 | @retval FALSE HMAC-SHA1 context initialization failed.\r | |
48 | \r | |
49 | **/\r | |
50 | BOOLEAN\r | |
51 | EFIAPI\r | |
52 | HmacSha1Init (\r | |
53 | OUT VOID *HmacSha1Context,\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 (HmacSha1Context == NULL || KeySize > INT_MAX) {\r |
16d2c32c | 62 | return FALSE;\r |
63 | }\r | |
a8c44645 | 64 | \r |
65 | //\r | |
66 | // OpenSSL HMAC-SHA1 Context Initialization\r | |
67 | //\r | |
68 | HMAC_CTX_init (HmacSha1Context);\r | |
69 | HMAC_Init_ex (HmacSha1Context, Key, (UINT32) KeySize, EVP_sha1(), NULL);\r | |
70 | \r | |
71 | return TRUE;\r | |
72 | }\r | |
73 | \r | |
74 | /**\r | |
75 | Makes a copy of an existing HMAC-SHA1 context.\r | |
76 | \r | |
16d2c32c | 77 | If HmacSha1Context is NULL, then return FALSE.\r |
78 | If NewHmacSha1Context is NULL, then return FALSE.\r | |
a8c44645 | 79 | \r |
80 | @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.\r | |
81 | @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.\r | |
82 | \r | |
83 | @retval TRUE HMAC-SHA1 context copy succeeded.\r | |
84 | @retval FALSE HMAC-SHA1 context copy failed.\r | |
85 | \r | |
86 | **/\r | |
87 | BOOLEAN\r | |
88 | EFIAPI\r | |
89 | HmacSha1Duplicate (\r | |
90 | IN CONST VOID *HmacSha1Context,\r | |
91 | OUT VOID *NewHmacSha1Context\r | |
92 | )\r | |
93 | {\r | |
4a567c96 | 94 | //\r |
16d2c32c | 95 | // Check input parameters.\r |
4a567c96 | 96 | //\r |
16d2c32c | 97 | if (HmacSha1Context == NULL || NewHmacSha1Context == NULL) {\r |
98 | return FALSE;\r | |
99 | }\r | |
4a567c96 | 100 | \r |
a8c44645 | 101 | CopyMem (NewHmacSha1Context, HmacSha1Context, sizeof (HMAC_CTX));\r |
102 | \r | |
103 | return TRUE;\r | |
104 | }\r | |
105 | \r | |
106 | /**\r | |
107 | Digests the input data and updates HMAC-SHA1 context.\r | |
108 | \r | |
109 | This function performs HMAC-SHA1 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 | |
111 | HMAC-SHA1 context should be already correctly intialized by HmacSha1Init(), and should not\r | |
112 | be finalized by HmacSha1Final(). Behavior with invalid context is undefined.\r | |
113 | \r | |
16d2c32c | 114 | If HmacSha1Context is NULL, then return FALSE.\r |
a8c44645 | 115 | \r |
116 | @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 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-SHA1 data digest succeeded.\r | |
121 | @retval FALSE HMAC-SHA1 data digest failed.\r | |
122 | \r | |
123 | **/\r | |
124 | BOOLEAN\r | |
125 | EFIAPI\r | |
126 | HmacSha1Update (\r | |
127 | IN OUT VOID *HmacSha1Context,\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 (HmacSha1Context == 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-SHA1 digest update\r | |
148 | //\r | |
149 | HMAC_Update (HmacSha1Context, Data, DataSize);\r | |
150 | \r | |
151 | return TRUE;\r | |
152 | }\r | |
153 | \r | |
154 | /**\r | |
155 | Completes computation of the HMAC-SHA1 digest value.\r | |
156 | \r | |
157 | This function completes HMAC-SHA1 digest computation and retrieves the digest value into\r | |
158 | the specified memory. After this function has been called, the HMAC-SHA1 context cannot\r | |
159 | be used again.\r | |
160 | HMAC-SHA1 context should be already correctly intialized by HmacSha1Init(), and should\r | |
161 | not be finalized by HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.\r | |
162 | \r | |
16d2c32c | 163 | If HmacSha1Context is NULL, then return FALSE.\r |
164 | If HmacValue is NULL, then return FALSE.\r | |
a8c44645 | 165 | \r |
166 | @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r | |
167 | @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest\r | |
168 | value (20 bytes).\r | |
169 | \r | |
170 | @retval TRUE HMAC-SHA1 digest computation succeeded.\r | |
171 | @retval FALSE HMAC-SHA1 digest computation failed.\r | |
172 | \r | |
173 | **/\r | |
174 | BOOLEAN\r | |
175 | EFIAPI\r | |
176 | HmacSha1Final (\r | |
177 | IN OUT VOID *HmacSha1Context,\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 (HmacSha1Context == NULL || HmacValue == NULL) {\r |
187 | return FALSE;\r | |
188 | }\r | |
a8c44645 | 189 | \r |
190 | //\r | |
191 | // OpenSSL HMAC-SHA1 digest finalization\r | |
192 | //\r | |
193 | HMAC_Final (HmacSha1Context, HmacValue, &Length);\r | |
194 | HMAC_CTX_cleanup (HmacSha1Context);\r | |
195 | \r | |
196 | return TRUE;\r | |
197 | }\r |