]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha256.c
Add CryptoPkg (from UDK2010.UP3)
[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 <Library/BaseLib.h>
16 #include <Library/DebugLib.h>
17
18 #include <Library/BaseCryptLib.h>
19 #include <openssl/sha.h>
20
21
22 /**
23 Retrieves the size, in bytes, of the context buffer required for SHA-256 operations.
24
25 @return The size, in bytes, of the context buffer required for SHA-256 operations.
26
27 **/
28 UINTN
29 EFIAPI
30 Sha256GetContextSize (
31 VOID
32 )
33 {
34 //
35 // Retrieves OpenSSL SHA-256 Context Size
36 //
37 return (UINTN)(sizeof (SHA256_CTX));
38 }
39
40
41 /**
42 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
43 subsequent use.
44
45 If Sha256Context is NULL, then ASSERT().
46
47 @param[in, out] Sha256Context Pointer to SHA-256 Context being initialized.
48
49 @retval TRUE SHA-256 context initialization succeeded.
50 @retval FALSE SHA-256 context initialization failed.
51
52 **/
53 BOOLEAN
54 EFIAPI
55 Sha256Init (
56 IN OUT VOID *Sha256Context
57 )
58 {
59 //
60 // ASSERT if Sha256Context is NULL
61 //
62 ASSERT (Sha256Context != NULL);
63
64 //
65 // OpenSSL SHA-256 Context Initialization
66 //
67 return (BOOLEAN) (SHA256_Init ((SHA256_CTX *)Sha256Context));
68 }
69
70
71 /**
72 Performs SHA-256 digest on a data buffer of the specified length. This function can
73 be called multiple times to compute the digest of long or discontinuous data streams.
74
75 If Sha256Context is NULL, then ASSERT().
76
77 @param[in, out] Sha256Context Pointer to the SHA-256 context.
78 @param[in] Data Pointer to the buffer containing the data to be hashed.
79 @param[in] DataLength Length of Data buffer in bytes.
80
81 @retval TRUE SHA-256 data digest succeeded.
82 @retval FALSE Invalid SHA-256 context. After Sha256Final function has been called, the
83 SHA-256 context cannot be reused.
84
85 **/
86 BOOLEAN
87 EFIAPI
88 Sha256Update (
89 IN OUT VOID *Sha256Context,
90 IN CONST VOID *Data,
91 IN UINTN DataLength
92 )
93 {
94 //
95 // ASSERT if Sha256Context is NULL
96 //
97 ASSERT (Sha256Context != NULL);
98
99 //
100 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL
101 //
102 if (Data == NULL) {
103 ASSERT (DataLength == 0);
104 }
105
106 //
107 // OpenSSL SHA-256 Hash Update
108 //
109 return (BOOLEAN) (SHA256_Update ((SHA256_CTX *)Sha256Context, Data, DataLength));
110 }
111
112
113 /**
114 Completes SHA-256 hash computation and retrieves the digest value into the specified
115 memory. After this function has been called, the SHA-256 context cannot be used again.
116
117 If Sha256Context is NULL, then ASSERT().
118 If HashValue is NULL, then ASSERT().
119
120 @param[in, out] Sha256Context Pointer to SHA-256 context
121 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
122 value (32 bytes).
123
124 @retval TRUE SHA-256 digest computation succeeded.
125 @retval FALSE SHA-256 digest computation failed.
126
127 **/
128 BOOLEAN
129 EFIAPI
130 Sha256Final (
131 IN OUT VOID *Sha256Context,
132 OUT UINT8 *HashValue
133 )
134 {
135 //
136 // ASSERT if Sha256Context is NULL or HashValue is NULL
137 //
138 ASSERT (Sha256Context != NULL);
139 ASSERT (HashValue != NULL);
140
141 //
142 // OpenSSL SHA-256 Hash Finalization
143 //
144 return (BOOLEAN) (SHA256_Final (HashValue, (SHA256_CTX *)Sha256Context));
145 }