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