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