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