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