1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include "hitls_build.h"
17 #ifdef HITLS_CRYPTO_MD5
18
19 #include "securec.h"
20 #include "crypt_errno.h"
21 #include "crypt_utils.h"
22 #include "bsl_err_internal.h"
23 #include "md5_core.h"
24 #include "crypt_md5.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif /* __cpluscplus */
29
30 /* F, G, H and I are basic MD5 functions. */
31 #define F(x, y, z) (((x) & (y)) | ((~(x)) & (z)))
32 #define G(x, y, z) (((x) & (z)) | ((y) & (~(z))))
33 #define H(x, y, z) ((x) ^ (y) ^ (z))
34 #define I(x, y, z) ((y) ^ ((x) | (~(z))))
35
36 #define FF(a, b, c, d, x, s, ac) \
37 do { \
38 (a) += F((b), (c), (d)) + (x) + (ac); \
39 (a) = ROTL32((a), (s)); \
40 (a) += (b); \
41 } while (0)
42
43 #define GG(a, b, c, d, x, s, ac) \
44 do { \
45 (a) += G((b), (c), (d)) + (x) + (ac); \
46 (a) = ROTL32((a), (s)); \
47 (a) += (b); \
48 } while (0)
49
50 #define HH(a, b, c, d, x, s, ac) \
51 do { \
52 (a) += H((b), (c), (d)) + (x) + (ac); \
53 (a) = ROTL32((a), (s)); \
54 (a) += (b); \
55 } while (0)
56
57 #define II(a, b, c, d, x, s, ac) \
58 do { \
59 (a) += I((b), (c), (d)) + (x) + (ac); \
60 (a) = ROTL32((a), (s)); \
61 (a) += (b); \
62 } while (0)
63
64 /* Constants for MD5_Compress routine. */
65 #define S11 7
66 #define S12 12
67 #define S13 17
68 #define S14 22
69 #define S21 5
70 #define S22 9
71 #define S23 14
72 #define S24 20
73 #define S31 4
74 #define S32 11
75 #define S33 16
76 #define S34 23
77 #define S41 6
78 #define S42 10
79 #define S43 15
80 #define S44 21
81
82 static const uint32_t T[64] = {
83 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE,
84 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
85 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE,
86 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
87
88 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA,
89 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8,
90 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED,
91 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
92
93 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C,
94 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
95 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05,
96 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
97
98 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039,
99 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
100 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1,
101 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391,
102 };
103
104 /* see RFC1321 chapter 3.4 Step 4 https://www.rfc-editor.org/rfc/rfc1321 */
MD5_Compress(uint32_t state[4],const uint8_t * data,uint32_t blockCnt)105 void MD5_Compress(uint32_t state[4], const uint8_t *data, uint32_t blockCnt)
106 {
107 uint32_t w[16] = {0};
108 const uint8_t *input = data;
109 uint32_t count = blockCnt;
110
111 while (count > 0) {
112 /* convert data to 32 bits for calculation */
113 w[0] = GET_UINT32_LE(input, 0);
114 w[1] = GET_UINT32_LE(input, 4);
115 w[2] = GET_UINT32_LE(input, 8);
116 w[3] = GET_UINT32_LE(input, 12);
117 w[4] = GET_UINT32_LE(input, 16);
118 w[5] = GET_UINT32_LE(input, 20);
119 w[6] = GET_UINT32_LE(input, 24);
120 w[7] = GET_UINT32_LE(input, 28);
121 w[8] = GET_UINT32_LE(input, 32);
122 w[9] = GET_UINT32_LE(input, 36);
123 w[10] = GET_UINT32_LE(input, 40);
124 w[11] = GET_UINT32_LE(input, 44);
125 w[12] = GET_UINT32_LE(input, 48);
126 w[13] = GET_UINT32_LE(input, 52);
127 w[14] = GET_UINT32_LE(input, 56);
128 w[15] = GET_UINT32_LE(input, 60);
129
130 uint32_t a = state[0];
131 uint32_t b = state[1];
132 uint32_t c = state[2];
133 uint32_t d = state[3];
134
135 FF(a, b, c, d, w[0], S11, T[0]);
136 FF(d, a, b, c, w[1], S12, T[1]);
137 FF(c, d, a, b, w[2], S13, T[2]);
138 FF(b, c, d, a, w[3], S14, T[3]);
139 FF(a, b, c, d, w[4], S11, T[4]);
140 FF(d, a, b, c, w[5], S12, T[5]);
141 FF(c, d, a, b, w[6], S13, T[6]);
142 FF(b, c, d, a, w[7], S14, T[7]);
143 FF(a, b, c, d, w[8], S11, T[8]);
144 FF(d, a, b, c, w[9], S12, T[9]);
145 FF(c, d, a, b, w[10], S13, T[10]);
146 FF(b, c, d, a, w[11], S14, T[11]);
147 FF(a, b, c, d, w[12], S11, T[12]);
148 FF(d, a, b, c, w[13], S12, T[13]);
149 FF(c, d, a, b, w[14], S13, T[14]);
150 FF(b, c, d, a, w[15], S14, T[15]);
151
152 GG(a, b, c, d, w[1], S21, T[16]);
153 GG(d, a, b, c, w[6], S22, T[17]);
154 GG(c, d, a, b, w[11], S23, T[18]);
155 GG(b, c, d, a, w[0], S24, T[19]);
156 GG(a, b, c, d, w[5], S21, T[20]);
157 GG(d, a, b, c, w[10], S22, T[21]);
158 GG(c, d, a, b, w[15], S23, T[22]);
159 GG(b, c, d, a, w[4], S24, T[23]);
160 GG(a, b, c, d, w[9], S21, T[24]);
161 GG(d, a, b, c, w[14], S22, T[25]);
162 GG(c, d, a, b, w[3], S23, T[26]);
163 GG(b, c, d, a, w[8], S24, T[27]);
164 GG(a, b, c, d, w[13], S21, T[28]);
165 GG(d, a, b, c, w[2], S22, T[29]);
166 GG(c, d, a, b, w[7], S23, T[30]);
167 GG(b, c, d, a, w[12], S24, T[31]);
168
169 HH(a, b, c, d, w[5], S31, T[32]);
170 HH(d, a, b, c, w[8], S32, T[33]);
171 HH(c, d, a, b, w[11], S33, T[34]);
172 HH(b, c, d, a, w[14], S34, T[35]);
173 HH(a, b, c, d, w[1], S31, T[36]);
174 HH(d, a, b, c, w[4], S32, T[37]);
175 HH(c, d, a, b, w[7], S33, T[38]);
176 HH(b, c, d, a, w[10], S34, T[39]);
177 HH(a, b, c, d, w[13], S31, T[40]);
178 HH(d, a, b, c, w[0], S32, T[41]);
179 HH(c, d, a, b, w[3], S33, T[42]);
180 HH(b, c, d, a, w[6], S34, T[43]);
181 HH(a, b, c, d, w[9], S31, T[44]);
182 HH(d, a, b, c, w[12], S32, T[45]);
183 HH(c, d, a, b, w[15], S33, T[46]);
184 HH(b, c, d, a, w[2], S34, T[47]);
185
186 II(a, b, c, d, w[0], S41, T[48]);
187 II(d, a, b, c, w[7], S42, T[49]);
188 II(c, d, a, b, w[14], S43, T[50]);
189 II(b, c, d, a, w[5], S44, T[51]);
190 II(a, b, c, d, w[12], S41, T[52]);
191 II(d, a, b, c, w[3], S42, T[53]);
192 II(c, d, a, b, w[10], S43, T[54]);
193 II(b, c, d, a, w[1], S44, T[55]);
194 II(a, b, c, d, w[8], S41, T[56]);
195 II(d, a, b, c, w[15], S42, T[57]);
196 II(c, d, a, b, w[6], S43, T[58]);
197 II(b, c, d, a, w[13], S44, T[59]);
198 II(a, b, c, d, w[4], S41, T[60]);
199 II(d, a, b, c, w[11], S42, T[61]);
200 II(c, d, a, b, w[2], S43, T[62]);
201 II(b, c, d, a, w[9], S44, T[63]);
202
203 state[0] += a;
204 state[1] += b;
205 state[2] += c;
206 state[3] += d;
207
208 input += CRYPT_MD5_BLOCKSIZE;
209 count--;
210 }
211 }
212 #ifdef __cplusplus
213 }
214 #endif /* __cpluscplus */
215
216 #endif // HITLS_CRYPTO_MD5
217