xref: /AOO41X/main/sal/workben/t_digest.c (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 
32 #include <rtl/digest.h>
33 
34 static const char *digest_in_MD[] =
35 {
36 	"",
37 	"a",
38 	"abc",
39 	"message digest",
40 	"abcdefghijklmnopqrstuvwxyz",
41 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
42 	"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
43 	NULL,
44 };
45 
46 static const char *digest_out_MD2[]=
47 {
48 	"8350e5a3e24c153df2275c9f80692773",
49 	"32ec01ec4a6dac72c0ab96fb34c0b5d1",
50 	"da853b0d3f88d99b30283a69e6ded6bb",
51 	"ab4f496bfb2a530b219ff33031fe06b0",
52 	"4e8ddff3650292ab5a4108c3aa47940b",
53 	"da33def2a42df13975352846c30338cd",
54 	"d5976f79d83d3a0dc9806c3c66f3efd8",
55 };
56 
57 static const char *digest_out_MD5[]=
58 {
59 	"d41d8cd98f00b204e9800998ecf8427e",
60 	"0cc175b9c0f1b6a831c399e269772661",
61 	"900150983cd24fb0d6963f7d28e17f72",
62 	"f96b697d7cb7938d525a2f31aaf161d0",
63 	"c3fcd3d76192e4007dfb496cca67e13b",
64 	"d174ab98d277d9f5a5611c2c9f419d9f",
65 	"57edf4a22be3c955ac49da2e2107b67a",
66 };
67 
68 static const char *digest_in_SHA[]=
69 {
70 	"abc",
71 	"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
72 	NULL,
73 };
74 
75 static const char *digest_out_SHA_0[]=
76 {
77 	"0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
78 	"d2516ee1acfa5baf33dfc1c471e438449ef134c8",
79 };
80 
81 static const char *digest_out_SHA_1[]=
82 {
83 	"a9993e364706816aba3e25717850c26c9cd0d89d",
84 	"84983e441c3bd26ebaae4aa1f95129e5e54670f1",
85 };
86 
87 static const char *digest_bigout_SHA_0=
88 	"3232affa48628a26653b5aaa44541fd90d690603";
89 
90 static const char *digest_bigout_SHA_1=
91 	"34aa973cd4c4daa4f61eeb2bdbad27316534016f";
92 
93 
94 static const char digest_key_HMAC_MD5_1[] =
95 {
96 	0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
97 	0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
98 	0x00
99 };
100 static const char digest_key_HMAC_MD5_2[] =
101 {
102 	/* "Jefe" */
103 	'J', 'e', 'f', 'e',
104 	0x00
105 };
106 static const unsigned char digest_key_HMAC_MD5_3[] =
107 {
108 	0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
109 	0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
110 	0x00
111 };
112 static const char *digest_key_HMAC_MD5[] =
113 {
114 	(const char*)&digest_key_HMAC_MD5_1,
115 	(const char*)&digest_key_HMAC_MD5_2, /* "Jefe", */
116 	(const char*)&digest_key_HMAC_MD5_3,
117 	NULL
118 };
119 
120 static const unsigned char digest_in_HMAC_MD5_3[] =
121 {
122 	0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
123 	0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
124 	0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
125 	0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
126 	0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
127 	0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
128 	0xDD, 0xDD,
129 	0x00
130 };
131 static const char *digest_in_HMAC_MD5[] =
132 {
133 	"Hi There",
134 	"what do ya want for nothing?",
135 	(const char*)&digest_in_HMAC_MD5_3,
136 	NULL
137 };
138 
139 static const char *digest_out_HMAC_MD5[] =
140 {
141 	"9294727a3638bb1c13f48ef8158bfc9d",
142 	"750c783e6ab0b503eaa86e310a5db738",
143 	"56be34521d144c88dbb8c733f0e8b3f6",
144 	NULL
145 };
146 static const char *digest_out_HMAC_SHA1[] =
147 {
148 	/* unofficial, i.e. not verified */
149 	"675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
150 	"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
151 	"d730594d167e35d5956fd8003d0db3d3f46dc7bb",
152 	NULL
153 };
154 
155 
156 static char *pt (unsigned char *md, int length)
157 {
158 	int i;
159 	static char buf[80];
160 
161 	for (i=0; i<length; i++)
162 		sprintf(&(buf[i*2]),"%02x",md[i]);
163 
164 	return(buf);
165 }
166 
167 int SAL_CALL main (int argc, char **argv)
168 {
169 	const char **P,**R, **Q;
170 	char *p;
171 	int i=1, err=0;
172 
173 	unsigned char md[80];
174 	unsigned char buffer[1000];
175 
176 	rtlDigest Digest;
177 
178 	P=digest_in_MD;
179 	R=digest_out_MD2;
180 	i = 1;
181 	while (*P)
182 	{
183 		rtl_digest_MD2 (*P, strlen(*P), md, sizeof(md));
184 
185 		p=pt (md, RTL_DIGEST_LENGTH_MD2);
186 		if (strcmp (p, *R))
187 		{
188 			printf("error calculating MD2 on '%s'\n",*P);
189 			printf("got %s instead of %s\n",p,*R);
190 			err++;
191 		}
192 		else
193 			printf("test (MD2) %d ok\n",i);
194 		i++;
195 		R++;
196 		P++;
197 	}
198 
199 	P=digest_in_MD;
200 	R=digest_out_MD5;
201 	i=1;
202 	while (*P)
203 	{
204 		rtl_digest_MD5 (*P, strlen(*P), md, sizeof(md));
205 
206 		p=pt (md, RTL_DIGEST_LENGTH_MD5);
207 		if (strcmp (p, *R))
208 		{
209 			printf("error calculating MD5 on '%s'\n",*P);
210 			printf("got %s instead of %s\n",p,*R);
211 			err++;
212 		}
213 		else
214 			printf("test (MD5) %d ok\n",i);
215 		i++;
216 		R++;
217 		P++;
218 	}
219 
220 	P=digest_in_SHA;
221 	R=digest_out_SHA_0;
222 	i=1;
223 	while (*P)
224 	{
225 		rtl_digest_SHA (*P, strlen(*P), md, sizeof(md));
226 
227 		p=pt (md, RTL_DIGEST_LENGTH_SHA);
228 		if (strcmp (p, *R))
229 		{
230 			printf("error calculating SHA-0 on '%s'\n",*P);
231 			printf("got %s instead of %s\n",p,*R);
232 			err++;
233 		}
234 		else
235 			printf("test (SHA-0) %d ok\n",i);
236 		i++;
237 		R++;
238 		P++;
239 	}
240 
241 	memset (buffer, 'a', sizeof(buffer));
242 	R = &digest_bigout_SHA_0;
243 
244 	Digest = rtl_digest_createSHA();
245 	for (i=0; i<1000; i++)
246 		rtl_digest_updateSHA (Digest, buffer, sizeof(buffer));
247 
248 	rtl_digest_getSHA (Digest, md, sizeof(md));
249 	rtl_digest_destroySHA (Digest);
250 
251 	p=pt (md, RTL_DIGEST_LENGTH_SHA);
252 	if (strcmp (p, *R))
253 	{
254 		printf("error calculating SHA-0 on '%s'\n",p);
255 		printf("got %s instead of %s\n",p,*R);
256 		err++;
257 	}
258 	else
259 		printf("test (SHA-0) n ok\n");
260 
261 	P=digest_in_SHA;
262 	R=digest_out_SHA_1;
263 	i=1;
264 	while (*P)
265 	{
266 		rtl_digest_SHA1 (*P, strlen(*P), md, sizeof(md));
267 
268 		p=pt (md, RTL_DIGEST_LENGTH_SHA1);
269 		if (strcmp (p, *R))
270 		{
271 			printf("error calculating SHA-1 on '%s'\n",*P);
272 			printf("got %s instead of %s\n",p,*R);
273 			err++;
274 		}
275 		else
276 			printf("test (SHA-1) %d ok\n",i);
277 		i++;
278 		R++;
279 		P++;
280 	}
281 
282 	memset (buffer, 'a', sizeof(buffer));
283 	R = &digest_bigout_SHA_1;
284 
285 	Digest = rtl_digest_createSHA1();
286 	for (i=0; i<1000; i++)
287 		rtl_digest_updateSHA1 (Digest, buffer, sizeof(buffer));
288 
289 	rtl_digest_getSHA1 (Digest, md, sizeof(md));
290 	rtl_digest_destroySHA1 (Digest);
291 
292 	p=pt (md, RTL_DIGEST_LENGTH_SHA1);
293 	if (strcmp (p, *R))
294 	{
295 		printf("error calculating SHA-1 on '%s'\n",p);
296 		printf("got %s instead of %s\n",p,*R);
297 		err++;
298 	}
299 	else
300 		printf("test (SHA-1) n ok\n");
301 
302 
303 	P=digest_in_HMAC_MD5;
304 	Q=digest_key_HMAC_MD5;
305 	R=digest_out_HMAC_MD5;
306 	Digest = rtl_digest_createHMAC_MD5();
307 	i = 1;
308 	while (*P)
309 	{
310 		rtl_digest_initHMAC_MD5 (Digest, *Q, strlen(*Q));
311 		rtl_digest_updateHMAC_MD5 (Digest, *P, strlen(*P));
312 		rtl_digest_getHMAC_MD5 (Digest, md, sizeof(md));
313 
314 		p=pt (md, RTL_DIGEST_LENGTH_HMAC_MD5);
315 		if (strcmp (p, *R))
316 		{
317 			printf("error calculating HMAC-MD5 on '%s'\n",*P);
318 			printf("got %s instead of %s\n",p,*R);
319 			err++;
320 		}
321 		else
322 			printf("test (HMAC-MD5) %d ok\n",i);
323 		i++;
324 		R++;
325 		P++;
326 		Q++;
327 	}
328 	rtl_digest_destroyHMAC_MD5 (Digest);
329 
330 
331 	P=digest_in_HMAC_MD5;
332 	Q=digest_key_HMAC_MD5;
333 	R=digest_out_HMAC_SHA1;
334 	Digest = rtl_digest_createHMAC_SHA1();
335 	i = 1;
336 	while (*P)
337 	{
338 		rtl_digest_initHMAC_SHA1 (Digest, *Q, strlen(*Q));
339 		rtl_digest_updateHMAC_SHA1 (Digest, *P, strlen(*P));
340 		rtl_digest_getHMAC_SHA1 (Digest, md, sizeof(md));
341 
342 		p=pt (md, RTL_DIGEST_LENGTH_HMAC_SHA1);
343 		if (strcmp (p, *R))
344 		{
345 			printf("error calculating HMAC-SHA-1 on '%s'\n",*P);
346 			printf("got %s instead of %s\n",p,*R);
347 			err++;
348 		}
349 		else
350 			printf("test (HMAC-SHA-1) %d ok\n",i);
351 		i++;
352 		P++;
353 		Q++;
354 		R++;
355 	}
356 	rtl_digest_destroyHMAC_SHA1 (Digest);
357 
358 
359 	P=digest_in_HMAC_MD5;
360 	Q=digest_key_HMAC_MD5;
361 	rtl_digest_PBKDF2 (
362 		md, RTL_DIGEST_LENGTH_MD5, /* [out] derived key     */
363 		Q[1], strlen(Q[1]),        /* [in]  password        */
364 		P[1], strlen(P[1]),        /* [in]  salt            */
365 		1000);                     /* [in]  iteration count */
366 
367 	p=pt (md, RTL_DIGEST_LENGTH_MD5);
368 	if (strcmp (p, "6349e09cb6b8c1485cfa9780ee3264df"))
369 	{
370 		printf("error calculating PBKDF2 on '%s'\n", P[1]);
371 		err++;
372 	}
373 	else
374 		printf("test (PBKDF2) %d ok\n", 1);
375 
376 	return (err);
377 }
378 
379