fd153c3f3144f00001eafbc245c510762130e9ae
[gnulib.git] / tests / test-gc.c
1 /*
2  * Copyright (C) 2005 Free Software Foundation
3  * Written by Simon Josefsson
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <string.h>
26 #include "gc.h"
27
28 int
29 main (int argc, char *argv[])
30 {
31
32   /* Test vectors from RFC 1321. */
33
34   {
35     char *in = "abcdefghijklmnopqrstuvwxyz";
36     size_t inlen = strlen (in);
37     char *expect =
38       "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1\x3b";
39     char out[16];
40
41     /* MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b */
42
43     if (gc_md5 (in, inlen, out) != 0)
44       {
45         printf ("gc_md5 call failed\n");
46         return 1;
47       }
48
49     if (memcmp (out, expect, 16) != 0)
50       {
51         size_t i;
52         printf ("md5 1 missmatch. expected:\n");
53         for (i = 0; i < 16; i++)
54           printf ("%02x ", expect[i] & 0xFF);
55         printf ("\ncomputed:\n");
56         for (i = 0; i < 16; i++)
57           printf ("%02x ", out[i] & 0xFF);
58         printf ("\n");
59         return 1;
60       }
61   }
62
63     /* Test vectors from RFC 2104. */
64
65   {
66     char *key =
67       "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
68     size_t key_len = 16;
69     char *data = "Hi There";
70     size_t data_len = 8;
71     char *digest =
72       "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
73     char out[16];
74
75     /*
76       key =         0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
77       key_len =     16 bytes
78       data =        "Hi There"
79       data_len =    8  bytes
80       digest =      0x9294727a3638bb1c13f48ef8158bfc9d
81     */
82
83     if (gc_hmac_md5 (key, key_len, data, data_len, out) != 0)
84       {
85         printf ("call failure\n");
86         return 1;
87       }
88
89     if (memcmp (digest, out, 16) != 0)
90       {
91         size_t i;
92         printf ("hash 1 missmatch. expected:\n");
93         for (i = 0; i < 16; i++)
94           printf ("%02x ", digest[i] & 0xFF);
95         printf ("\ncomputed:\n");
96         for (i = 0; i < 16; i++)
97           printf ("%02x ", out[i] & 0xFF);
98         printf ("\n");
99         return 1;
100       }
101   }
102
103   return 0;
104 }