/src/nss/lib/freebl/md2.c
Line | Count | Source |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | #ifdef FREEBL_NO_DEPEND |
6 | | #include "stubs.h" |
7 | | #endif |
8 | | |
9 | | #include "prerr.h" |
10 | | #include "secerr.h" |
11 | | |
12 | | #include "prtypes.h" |
13 | | |
14 | | #include "blapi.h" |
15 | | |
16 | 0 | #define MD2_DIGEST_LEN 16 |
17 | 7.00M | #define MD2_BUFSIZE 16 |
18 | | #define MD2_X_SIZE 48 /* The X array, [CV | INPUT | TMP VARS] */ |
19 | 0 | #define MD2_CV 0 /* index into X for chaining variables */ |
20 | 46.2M | #define MD2_INPUT 16 /* index into X for input */ |
21 | 22.4M | #define MD2_TMPVARS 32 /* index into X for temporary variables */ |
22 | 1.40M | #define MD2_CHECKSUM_SIZE 16 |
23 | | |
24 | | struct MD2ContextStr { |
25 | | unsigned char checksum[MD2_BUFSIZE]; |
26 | | unsigned char X[MD2_X_SIZE]; |
27 | | PRUint8 unusedBuffer; |
28 | | }; |
29 | | |
30 | | static const PRUint8 MD2S[256] = { |
31 | | 0051, 0056, 0103, 0311, 0242, 0330, 0174, 0001, |
32 | | 0075, 0066, 0124, 0241, 0354, 0360, 0006, 0023, |
33 | | 0142, 0247, 0005, 0363, 0300, 0307, 0163, 0214, |
34 | | 0230, 0223, 0053, 0331, 0274, 0114, 0202, 0312, |
35 | | 0036, 0233, 0127, 0074, 0375, 0324, 0340, 0026, |
36 | | 0147, 0102, 0157, 0030, 0212, 0027, 0345, 0022, |
37 | | 0276, 0116, 0304, 0326, 0332, 0236, 0336, 0111, |
38 | | 0240, 0373, 0365, 0216, 0273, 0057, 0356, 0172, |
39 | | 0251, 0150, 0171, 0221, 0025, 0262, 0007, 0077, |
40 | | 0224, 0302, 0020, 0211, 0013, 0042, 0137, 0041, |
41 | | 0200, 0177, 0135, 0232, 0132, 0220, 0062, 0047, |
42 | | 0065, 0076, 0314, 0347, 0277, 0367, 0227, 0003, |
43 | | 0377, 0031, 0060, 0263, 0110, 0245, 0265, 0321, |
44 | | 0327, 0136, 0222, 0052, 0254, 0126, 0252, 0306, |
45 | | 0117, 0270, 0070, 0322, 0226, 0244, 0175, 0266, |
46 | | 0166, 0374, 0153, 0342, 0234, 0164, 0004, 0361, |
47 | | 0105, 0235, 0160, 0131, 0144, 0161, 0207, 0040, |
48 | | 0206, 0133, 0317, 0145, 0346, 0055, 0250, 0002, |
49 | | 0033, 0140, 0045, 0255, 0256, 0260, 0271, 0366, |
50 | | 0034, 0106, 0141, 0151, 0064, 0100, 0176, 0017, |
51 | | 0125, 0107, 0243, 0043, 0335, 0121, 0257, 0072, |
52 | | 0303, 0134, 0371, 0316, 0272, 0305, 0352, 0046, |
53 | | 0054, 0123, 0015, 0156, 0205, 0050, 0204, 0011, |
54 | | 0323, 0337, 0315, 0364, 0101, 0201, 0115, 0122, |
55 | | 0152, 0334, 0067, 0310, 0154, 0301, 0253, 0372, |
56 | | 0044, 0341, 0173, 0010, 0014, 0275, 0261, 0112, |
57 | | 0170, 0210, 0225, 0213, 0343, 0143, 0350, 0155, |
58 | | 0351, 0313, 0325, 0376, 0073, 0000, 0035, 0071, |
59 | | 0362, 0357, 0267, 0016, 0146, 0130, 0320, 0344, |
60 | | 0246, 0167, 0162, 0370, 0353, 0165, 0113, 0012, |
61 | | 0061, 0104, 0120, 0264, 0217, 0355, 0037, 0032, |
62 | | 0333, 0231, 0215, 0063, 0237, 0021, 0203, 0024 |
63 | | }; |
64 | | |
65 | | SECStatus |
66 | | MD2_Hash(unsigned char *dest, const char *src) |
67 | 0 | { |
68 | 0 | unsigned int len; |
69 | 0 | MD2Context *cx = MD2_NewContext(); |
70 | 0 | if (!cx) { |
71 | 0 | PORT_SetError(PR_OUT_OF_MEMORY_ERROR); |
72 | 0 | return SECFailure; |
73 | 0 | } |
74 | 0 | MD2_Begin(cx); |
75 | 0 | MD2_Update(cx, (const unsigned char *)src, PORT_Strlen(src)); |
76 | 0 | MD2_End(cx, dest, &len, MD2_DIGEST_LEN); |
77 | 0 | MD2_DestroyContext(cx, PR_TRUE); |
78 | 0 | return SECSuccess; |
79 | 0 | } |
80 | | |
81 | | MD2Context * |
82 | | MD2_NewContext(void) |
83 | 50 | { |
84 | 50 | MD2Context *cx = (MD2Context *)PORT_ZAlloc(sizeof(MD2Context)); |
85 | 50 | if (cx == NULL) { |
86 | 0 | PORT_SetError(PR_OUT_OF_MEMORY_ERROR); |
87 | 0 | return NULL; |
88 | 0 | } |
89 | 50 | return cx; |
90 | 50 | } |
91 | | |
92 | | void |
93 | | MD2_DestroyContext(MD2Context *cx, PRBool freeit) |
94 | 50 | { |
95 | 50 | if (freeit) |
96 | 50 | PORT_ZFree(cx, sizeof(*cx)); |
97 | 50 | } |
98 | | |
99 | | void |
100 | | MD2_Begin(MD2Context *cx) |
101 | 50 | { |
102 | 50 | memset(cx, 0, sizeof(*cx)); |
103 | 50 | cx->unusedBuffer = MD2_BUFSIZE; |
104 | 50 | } |
105 | | |
106 | | static void |
107 | | md2_compress(MD2Context *cx) |
108 | 1.40M | { |
109 | 1.40M | int j; |
110 | 1.40M | unsigned char P; |
111 | 1.40M | P = cx->checksum[MD2_CHECKSUM_SIZE - 1]; |
112 | | /* Compute the running checksum, and set the tmp variables to be |
113 | | * CV[i] XOR input[i] |
114 | | */ |
115 | 1.40M | #define CKSUMFN(n) \ |
116 | 22.4M | P = cx->checksum[n] ^ MD2S[cx->X[MD2_INPUT + n] ^ P]; \ |
117 | 22.4M | cx->checksum[n] = P; \ |
118 | 22.4M | cx->X[MD2_TMPVARS + n] = cx->X[n] ^ cx->X[MD2_INPUT + n]; |
119 | 1.40M | CKSUMFN(0); |
120 | 1.40M | CKSUMFN(1); |
121 | 1.40M | CKSUMFN(2); |
122 | 1.40M | CKSUMFN(3); |
123 | 1.40M | CKSUMFN(4); |
124 | 1.40M | CKSUMFN(5); |
125 | 1.40M | CKSUMFN(6); |
126 | 1.40M | CKSUMFN(7); |
127 | 1.40M | CKSUMFN(8); |
128 | 1.40M | CKSUMFN(9); |
129 | 1.40M | CKSUMFN(10); |
130 | 1.40M | CKSUMFN(11); |
131 | 1.40M | CKSUMFN(12); |
132 | 1.40M | CKSUMFN(13); |
133 | 1.40M | CKSUMFN(14); |
134 | 1.40M | CKSUMFN(15); |
135 | | /* The compression function. */ |
136 | 1.40M | #define COMPRESS(n) \ |
137 | 1.20G | P = cx->X[n] ^ MD2S[P]; \ |
138 | 1.20G | cx->X[n] = P; |
139 | 1.40M | P = 0x00; |
140 | 26.6M | for (j = 0; j < 18; j++) { |
141 | 25.2M | COMPRESS(0); |
142 | 25.2M | COMPRESS(1); |
143 | 25.2M | COMPRESS(2); |
144 | 25.2M | COMPRESS(3); |
145 | 25.2M | COMPRESS(4); |
146 | 25.2M | COMPRESS(5); |
147 | 25.2M | COMPRESS(6); |
148 | 25.2M | COMPRESS(7); |
149 | 25.2M | COMPRESS(8); |
150 | 25.2M | COMPRESS(9); |
151 | 25.2M | COMPRESS(10); |
152 | 25.2M | COMPRESS(11); |
153 | 25.2M | COMPRESS(12); |
154 | 25.2M | COMPRESS(13); |
155 | 25.2M | COMPRESS(14); |
156 | 25.2M | COMPRESS(15); |
157 | 25.2M | COMPRESS(16); |
158 | 25.2M | COMPRESS(17); |
159 | 25.2M | COMPRESS(18); |
160 | 25.2M | COMPRESS(19); |
161 | 25.2M | COMPRESS(20); |
162 | 25.2M | COMPRESS(21); |
163 | 25.2M | COMPRESS(22); |
164 | 25.2M | COMPRESS(23); |
165 | 25.2M | COMPRESS(24); |
166 | 25.2M | COMPRESS(25); |
167 | 25.2M | COMPRESS(26); |
168 | 25.2M | COMPRESS(27); |
169 | 25.2M | COMPRESS(28); |
170 | 25.2M | COMPRESS(29); |
171 | 25.2M | COMPRESS(30); |
172 | 25.2M | COMPRESS(31); |
173 | 25.2M | COMPRESS(32); |
174 | 25.2M | COMPRESS(33); |
175 | 25.2M | COMPRESS(34); |
176 | 25.2M | COMPRESS(35); |
177 | 25.2M | COMPRESS(36); |
178 | 25.2M | COMPRESS(37); |
179 | 25.2M | COMPRESS(38); |
180 | 25.2M | COMPRESS(39); |
181 | 25.2M | COMPRESS(40); |
182 | 25.2M | COMPRESS(41); |
183 | 25.2M | COMPRESS(42); |
184 | 25.2M | COMPRESS(43); |
185 | 25.2M | COMPRESS(44); |
186 | 25.2M | COMPRESS(45); |
187 | 25.2M | COMPRESS(46); |
188 | 25.2M | COMPRESS(47); |
189 | 25.2M | P = (P + j) % 256; |
190 | 25.2M | } |
191 | 1.40M | cx->unusedBuffer = MD2_BUFSIZE; |
192 | 1.40M | } |
193 | | |
194 | | void |
195 | | MD2_Update(MD2Context *cx, const unsigned char *input, unsigned int inputLen) |
196 | 1.20k | { |
197 | 1.20k | PRUint32 bytesToConsume; |
198 | | |
199 | | /* Reject corrupt deserialized state: unusedBuffer must never exceed |
200 | | * MD2_BUFSIZE, otherwise the index computation below underflows. */ |
201 | 1.20k | if (cx->unusedBuffer > MD2_BUFSIZE) { |
202 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
203 | 0 | return; |
204 | 0 | } |
205 | | |
206 | | /* Fill the remaining input buffer. */ |
207 | 1.20k | if (cx->unusedBuffer != MD2_BUFSIZE) { |
208 | 1.00k | bytesToConsume = PR_MIN(inputLen, cx->unusedBuffer); |
209 | 1.00k | memcpy(&cx->X[MD2_INPUT + (MD2_BUFSIZE - cx->unusedBuffer)], |
210 | 1.00k | input, bytesToConsume); |
211 | 1.00k | if (cx->unusedBuffer + bytesToConsume >= MD2_BUFSIZE) |
212 | 554 | md2_compress(cx); |
213 | 1.00k | inputLen -= bytesToConsume; |
214 | 1.00k | input += bytesToConsume; |
215 | 1.00k | } |
216 | | |
217 | | /* Iterate over 16-byte chunks of the input. */ |
218 | 1.40M | while (inputLen >= MD2_BUFSIZE) { |
219 | 1.39M | memcpy(&cx->X[MD2_INPUT], input, MD2_BUFSIZE); |
220 | 1.39M | md2_compress(cx); |
221 | 1.39M | inputLen -= MD2_BUFSIZE; |
222 | 1.39M | input += MD2_BUFSIZE; |
223 | 1.39M | } |
224 | | |
225 | | /* Copy any input that remains into the buffer. */ |
226 | 1.20k | if (inputLen) |
227 | 1.04k | memcpy(&cx->X[MD2_INPUT], input, inputLen); |
228 | 1.20k | cx->unusedBuffer = MD2_BUFSIZE - inputLen; |
229 | 1.20k | } |
230 | | |
231 | | void |
232 | | MD2_End(MD2Context *cx, unsigned char *digest, |
233 | | unsigned int *digestLen, unsigned int maxDigestLen) |
234 | 0 | { |
235 | 0 | PRUint8 padStart; |
236 | 0 | if (maxDigestLen < MD2_BUFSIZE) { |
237 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
238 | 0 | return; |
239 | 0 | } |
240 | | /* Reject corrupt deserialized state: unusedBuffer must never exceed |
241 | | * MD2_BUFSIZE, otherwise padStart wraps and the memset below overflows. */ |
242 | 0 | if (cx->unusedBuffer > MD2_BUFSIZE) { |
243 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
244 | 0 | return; |
245 | 0 | } |
246 | 0 | padStart = MD2_BUFSIZE - cx->unusedBuffer; |
247 | 0 | memset(&cx->X[MD2_INPUT + padStart], cx->unusedBuffer, |
248 | 0 | cx->unusedBuffer); |
249 | 0 | md2_compress(cx); |
250 | 0 | memcpy(&cx->X[MD2_INPUT], cx->checksum, MD2_BUFSIZE); |
251 | 0 | md2_compress(cx); |
252 | 0 | *digestLen = MD2_DIGEST_LEN; |
253 | 0 | memcpy(digest, &cx->X[MD2_CV], MD2_DIGEST_LEN); |
254 | 0 | } |
255 | | |
256 | | unsigned int |
257 | | MD2_FlattenSize(MD2Context *cx) |
258 | 50 | { |
259 | 50 | return sizeof(*cx); |
260 | 50 | } |
261 | | |
262 | | SECStatus |
263 | | MD2_Flatten(MD2Context *cx, unsigned char *space) |
264 | 0 | { |
265 | 0 | memcpy(space, cx, sizeof(*cx)); |
266 | 0 | return SECSuccess; |
267 | 0 | } |
268 | | |
269 | | MD2Context * |
270 | | MD2_Resurrect(unsigned char *space, void *arg) |
271 | 0 | { |
272 | 0 | MD2Context *cx = MD2_NewContext(); |
273 | 0 | if (cx) |
274 | 0 | memcpy(cx, space, sizeof(*cx)); |
275 | 0 | return cx; |
276 | 0 | } |
277 | | |
278 | | void |
279 | | MD2_Clone(MD2Context *dest, MD2Context *src) |
280 | 0 | { |
281 | 0 | memcpy(dest, src, sizeof *dest); |
282 | 0 | } |