• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** @file
2   SHA-256 Digest Wrapper Implementation over OpenSSL.
3 
4 Copyright (c) 2009 - 2016, 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
Sha256GetContextSize(VOID)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 return FALSE.
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
Sha256Init(OUT VOID * Sha256Context)50 Sha256Init (
51   OUT  VOID  *Sha256Context
52   )
53 {
54   //
55   // Check input parameters.
56   //
57   if (Sha256Context == NULL) {
58     return FALSE;
59   }
60 
61   //
62   // OpenSSL SHA-256 Context Initialization
63   //
64   return (BOOLEAN) (SHA256_Init ((SHA256_CTX *) Sha256Context));
65 }
66 
67 /**
68   Makes a copy of an existing SHA-256 context.
69 
70   If Sha256Context is NULL, then return FALSE.
71   If NewSha256Context is NULL, then return FALSE.
72 
73   @param[in]  Sha256Context     Pointer to SHA-256 context being copied.
74   @param[out] NewSha256Context  Pointer to new SHA-256 context.
75 
76   @retval TRUE   SHA-256 context copy succeeded.
77   @retval FALSE  SHA-256 context copy failed.
78 
79 **/
80 BOOLEAN
81 EFIAPI
Sha256Duplicate(IN CONST VOID * Sha256Context,OUT VOID * NewSha256Context)82 Sha256Duplicate (
83   IN   CONST VOID  *Sha256Context,
84   OUT  VOID        *NewSha256Context
85   )
86 {
87   //
88   // Check input parameters.
89   //
90   if (Sha256Context == NULL || NewSha256Context == NULL) {
91     return FALSE;
92   }
93 
94   CopyMem (NewSha256Context, Sha256Context, sizeof (SHA256_CTX));
95 
96   return TRUE;
97 }
98 
99 /**
100   Digests the input data and updates SHA-256 context.
101 
102   This function performs SHA-256 digest on a data buffer of the specified size.
103   It can be called multiple times to compute the digest of long or discontinuous data streams.
104   SHA-256 context should be already correctly initialized by Sha256Init(), and should not be finalized
105   by Sha256Final(). Behavior with invalid context is undefined.
106 
107   If Sha256Context is NULL, then return FALSE.
108 
109   @param[in, out]  Sha256Context  Pointer to the SHA-256 context.
110   @param[in]       Data           Pointer to the buffer containing the data to be hashed.
111   @param[in]       DataSize       Size of Data buffer in bytes.
112 
113   @retval TRUE   SHA-256 data digest succeeded.
114   @retval FALSE  SHA-256 data digest failed.
115 
116 **/
117 BOOLEAN
118 EFIAPI
Sha256Update(IN OUT VOID * Sha256Context,IN CONST VOID * Data,IN UINTN DataSize)119 Sha256Update (
120   IN OUT  VOID        *Sha256Context,
121   IN      CONST VOID  *Data,
122   IN      UINTN       DataSize
123   )
124 {
125   //
126   // Check input parameters.
127   //
128   if (Sha256Context == NULL) {
129     return FALSE;
130   }
131 
132   //
133   // Check invalid parameters, in case that only DataLength was checked in OpenSSL
134   //
135   if (Data == NULL && DataSize != 0) {
136     return FALSE;
137   }
138 
139   //
140   // OpenSSL SHA-256 Hash Update
141   //
142   return (BOOLEAN) (SHA256_Update ((SHA256_CTX *) Sha256Context, Data, DataSize));
143 }
144 
145 /**
146   Completes computation of the SHA-256 digest value.
147 
148   This function completes SHA-256 hash computation and retrieves the digest value into
149   the specified memory. After this function has been called, the SHA-256 context cannot
150   be used again.
151   SHA-256 context should be already correctly initialized by Sha256Init(), and should not be
152   finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.
153 
154   If Sha256Context is NULL, then return FALSE.
155   If HashValue is NULL, then return FALSE.
156 
157   @param[in, out]  Sha256Context  Pointer to the SHA-256 context.
158   @param[out]      HashValue      Pointer to a buffer that receives the SHA-256 digest
159                                   value (32 bytes).
160 
161   @retval TRUE   SHA-256 digest computation succeeded.
162   @retval FALSE  SHA-256 digest computation failed.
163 
164 **/
165 BOOLEAN
166 EFIAPI
Sha256Final(IN OUT VOID * Sha256Context,OUT UINT8 * HashValue)167 Sha256Final (
168   IN OUT  VOID   *Sha256Context,
169   OUT     UINT8  *HashValue
170   )
171 {
172   //
173   // Check input parameters.
174   //
175   if (Sha256Context == NULL || HashValue == NULL) {
176     return FALSE;
177   }
178 
179   //
180   // OpenSSL SHA-256 Hash Finalization
181   //
182   return (BOOLEAN) (SHA256_Final (HashValue, (SHA256_CTX *) Sha256Context));
183 }
184 
185 /**
186   Computes the SHA-256 message digest of a input data buffer.
187 
188   This function performs the SHA-256 message digest of a given data buffer, and places
189   the digest value into the specified memory.
190 
191   If this interface is not supported, then return FALSE.
192 
193   @param[in]   Data        Pointer to the buffer containing the data to be hashed.
194   @param[in]   DataSize    Size of Data buffer in bytes.
195   @param[out]  HashValue   Pointer to a buffer that receives the SHA-256 digest
196                            value (32 bytes).
197 
198   @retval TRUE   SHA-256 digest computation succeeded.
199   @retval FALSE  SHA-256 digest computation failed.
200   @retval FALSE  This interface is not supported.
201 
202 **/
203 BOOLEAN
204 EFIAPI
Sha256HashAll(IN CONST VOID * Data,IN UINTN DataSize,OUT UINT8 * HashValue)205 Sha256HashAll (
206   IN   CONST VOID  *Data,
207   IN   UINTN       DataSize,
208   OUT  UINT8       *HashValue
209   )
210 {
211   //
212   // Check input parameters.
213   //
214   if (HashValue == NULL) {
215     return FALSE;
216   }
217   if (Data == NULL && DataSize != 0) {
218     return FALSE;
219   }
220 
221   //
222   // OpenSSL SHA-256 Hash Computation.
223   //
224   if (SHA256 (Data, DataSize, HashValue) == NULL) {
225     return FALSE;
226   } else {
227     return TRUE;
228   }
229 }
230