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