]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha1.c
27526bcd404a834a438818dd59cfa285935ec271
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptSha1.c
1 /** @file
2 SHA-1 Digest Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2009 - 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/sha.h>
17
18
19 /**
20 Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
21
22 @return The size, in bytes, of the context buffer required for SHA-1 hash operations.
23
24 **/
25 UINTN
26 EFIAPI
27 Sha1GetContextSize (
28 VOID
29 )
30 {
31 //
32 // Retrieves OpenSSL SHA Context Size
33 //
34 return (UINTN)(sizeof (SHA_CTX));
35 }
36
37 /**
38 Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
39 subsequent use.
40
41 If Sha1Context is NULL, then ASSERT().
42
43 @param[out] Sha1Context Pointer to SHA-1 context being initialized.
44
45 @retval TRUE SHA-1 context initialization succeeded.
46 @retval FALSE SHA-1 context initialization failed.
47
48 **/
49 BOOLEAN
50 EFIAPI
51 Sha1Init (
52 OUT VOID *Sha1Context
53 )
54 {
55 //
56 // ASSERT if Sha1Context is NULL
57 //
58 ASSERT (Sha1Context != NULL);
59
60 //
61 // OpenSSL SHA-1 Context Initialization
62 //
63 return (BOOLEAN) (SHA1_Init ((SHA_CTX *)Sha1Context));
64 }
65
66 /**
67 Makes a copy of an existing SHA-1 context.
68
69 If Sha1Context is NULL, then ASSERT().
70 If NewSha1Context is NULL, then ASSERT().
71
72 @param[in] Sha1Context Pointer to SHA-1 context being copied.
73 @param[out] NewSha1Context Pointer to new SHA-1 context.
74
75 @retval TRUE SHA-1 context copy succeeded.
76 @retval FALSE SHA-1 context copy failed.
77
78 **/
79 BOOLEAN
80 EFIAPI
81 Sha1Duplicate (
82 IN CONST VOID *Sha1Context,
83 OUT VOID *NewSha1Context
84 )
85 {
86 //
87 // ASSERT if Sha1Context or NewSha1Context is NULL.
88 //
89 ASSERT (Sha1Context != NULL);
90 ASSERT (NewSha1Context != NULL);
91
92 CopyMem (NewSha1Context, Sha1Context, sizeof (SHA_CTX));
93
94 return TRUE;
95 }
96
97 /**
98 Digests the input data and updates SHA-1 context.
99
100 This function performs SHA-1 digest on a data buffer of the specified size.
101 It can be called multiple times to compute the digest of long or discontinuous data streams.
102 SHA-1 context should be already correctly intialized by Sha1Init(), and should not be finalized
103 by Sha1Final(). Behavior with invalid context is undefined.
104
105 If Sha1Context is NULL, then ASSERT().
106
107 @param[in, out] Sha1Context Pointer to the SHA-1 context.
108 @param[in] Data Pointer to the buffer containing the data to be hashed.
109 @param[in] DataSize Size of Data buffer in bytes.
110
111 @retval TRUE SHA-1 data digest succeeded.
112 @retval FALSE SHA-1 data digest failed.
113
114 **/
115 BOOLEAN
116 EFIAPI
117 Sha1Update (
118 IN OUT VOID *Sha1Context,
119 IN CONST VOID *Data,
120 IN UINTN DataSize
121 )
122 {
123 //
124 // ASSERT if Sha1Context is NULL
125 //
126 ASSERT (Sha1Context != NULL);
127
128 //
129 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL
130 //
131 if (Data == NULL) {
132 ASSERT (DataSize == 0);
133 }
134
135 //
136 // OpenSSL SHA-1 Hash Update
137 //
138 return (BOOLEAN) (SHA1_Update ((SHA_CTX *)Sha1Context, Data, DataSize));
139 }
140
141 /**
142 Completes computation of the SHA-1 digest value.
143
144 This function completes SHA-1 hash computation and retrieves the digest value into
145 the specified memory. After this function has been called, the SHA-1 context cannot
146 be used again.
147 SHA-1 context should be already correctly intialized by Sha1Init(), and should not be
148 finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
149
150 If Sha1Context is NULL, then ASSERT().
151 If HashValue is NULL, then ASSERT().
152
153 @param[in, out] Sha1Context Pointer to the SHA-1 context.
154 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
155 value (20 bytes).
156
157 @retval TRUE SHA-1 digest computation succeeded.
158 @retval FALSE SHA-1 digest computation failed.
159
160 **/
161 BOOLEAN
162 EFIAPI
163 Sha1Final (
164 IN OUT VOID *Sha1Context,
165 OUT UINT8 *HashValue
166 )
167 {
168 //
169 // ASSERT if Sha1Context is NULL or HashValue is NULL
170 //
171 ASSERT (Sha1Context != NULL);
172 ASSERT (HashValue != NULL);
173
174 //
175 // OpenSSL SHA-1 Hash Finalization
176 //
177 return (BOOLEAN) (SHA1_Final (HashValue, (SHA_CTX *)Sha1Context));
178 }