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