]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptMd5.c
Update CryptoPkg for new ciphers (HMAC, Block Cipher, etc) supports.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptMd5.c
CommitLineData
97f98500
HT
1/** @file\r
2 MD5 Digest Wrapper Implementation over OpenSSL.\r
3\r
4Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT 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/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
25UINTN\r
26EFIAPI\r
27Md5GetContextSize (\r
28 VOID\r
29 )\r
30{\r
31 //\r
32 // Retrieves the OpenSSL MD5 Context Size\r
33 //\r
34 return (UINTN)(sizeof (MD5_CTX));\r
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 ASSERT().\r
43\r
a8c44645 44 @param[out] Md5Context Pointer to MD5 context being initialized.\r
97f98500
HT
45\r
46 @retval TRUE MD5 context initialization succeeded.\r
47 @retval FALSE MD5 context initialization failed.\r
48\r
49**/\r
50BOOLEAN\r
51EFIAPI\r
52Md5Init (\r
a8c44645 53 OUT VOID *Md5Context\r
97f98500
HT
54 )\r
55{\r
56 //\r
57 // ASSERT if Md5Context is NULL.\r
58 //\r
59 ASSERT (Md5Context != NULL);\r
60\r
61 //\r
62 // OpenSSL MD5 Context Initialization\r
63 //\r
64 return (BOOLEAN) (MD5_Init ((MD5_CTX *)Md5Context));\r
65}\r
66\r
a8c44645 67/**\r
68 Makes a copy of an existing MD5 context.\r
69\r
70 If Md5Context is NULL, then ASSERT().\r
71 If NewMd5Context is NULL, then ASSERT().\r
72\r
73 @param[in] Md5Context Pointer to MD5 context being copied.\r
74 @param[out] NewMd5Context Pointer to new MD5 context.\r
75\r
76 @retval TRUE MD5 context copy succeeded.\r
77 @retval FALSE MD5 context copy failed.\r
78\r
79**/\r
80BOOLEAN\r
81EFIAPI\r
82Md5Duplicate (\r
83 IN CONST VOID *Md5Context,\r
84 OUT VOID *NewMd5Context\r
85 )\r
86{\r
87 CopyMem (NewMd5Context, Md5Context, sizeof (MD5_CTX));\r
88\r
89 return TRUE;\r
90}\r
97f98500
HT
91\r
92/**\r
a8c44645 93 Digests the input data and updates MD5 context.\r
94\r
95 This function performs MD5 digest on a data buffer of the specified size.\r
96 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
97 MD5 context should be already correctly intialized by Md5Init(), and should not be finalized\r
98 by Md5Final(). Behavior with invalid context is undefined.\r
97f98500
HT
99\r
100 If Md5Context is NULL, then ASSERT().\r
101\r
102 @param[in, out] Md5Context Pointer to the MD5 context.\r
103 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
a8c44645 104 @param[in] DataSize Size of Data buffer in bytes.\r
97f98500
HT
105\r
106 @retval TRUE MD5 data digest succeeded.\r
a8c44645 107 @retval FALSE MD5 data digest failed.\r
97f98500
HT
108\r
109**/\r
110BOOLEAN\r
111EFIAPI\r
112Md5Update (\r
113 IN OUT VOID *Md5Context,\r
114 IN CONST VOID *Data,\r
a8c44645 115 IN UINTN DataSize\r
97f98500
HT
116 )\r
117{\r
118 //\r
119 // ASSERT if Md5Context is NULL\r
120 //\r
121 ASSERT (Md5Context != NULL);\r
122\r
123 //\r
124 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL\r
125 //\r
126 if (Data == NULL) {\r
a8c44645 127 ASSERT (DataSize == 0);\r
97f98500
HT
128 }\r
129\r
130 //\r
131 // OpenSSL MD5 Hash Update\r
132 //\r
a8c44645 133 return (BOOLEAN) (MD5_Update ((MD5_CTX *)Md5Context, Data, DataSize));\r
97f98500
HT
134}\r
135\r
97f98500 136/**\r
a8c44645 137 Completes computation of the MD5 digest value.\r
138\r
139 This function completes MD5 hash computation and retrieves the digest value into\r
140 the specified memory. After this function has been called, the MD5 context cannot\r
141 be used again.\r
142 MD5 context should be already correctly intialized by Md5Init(), and should not be\r
143 finalized by Md5Final(). Behavior with invalid MD5 context is undefined.\r
97f98500
HT
144\r
145 If Md5Context is NULL, then ASSERT().\r
146 If HashValue is NULL, then ASSERT().\r
147\r
a8c44645 148 @param[in, out] Md5Context Pointer to the MD5 context.\r
97f98500
HT
149 @param[out] HashValue Pointer to a buffer that receives the MD5 digest\r
150 value (16 bytes).\r
151\r
152 @retval TRUE MD5 digest computation succeeded.\r
153 @retval FALSE MD5 digest computation failed.\r
154\r
155**/\r
156BOOLEAN\r
157EFIAPI\r
158Md5Final (\r
159 IN OUT VOID *Md5Context,\r
160 OUT UINT8 *HashValue\r
161 )\r
162{\r
163 //\r
164 // ASSERT if Md5Context is NULL or HashValue is NULL\r
165 //\r
166 ASSERT (Md5Context != NULL);\r
167 ASSERT (HashValue != NULL);\r
168\r
169 //\r
170 // OpenSSL MD5 Hash Finalization\r
171 //\r
172 return (BOOLEAN) (MD5_Final (HashValue, (MD5_CTX *)Md5Context));\r
173}\r