]> www.average.org Git - pam_pcsc_cr.git/blob - test_crypto.c
wip on crypto
[pam_pcsc_cr.git] / test_crypto.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include "crypto.h"
9
10 #define printh(x) printh_f(#x, x, sizeof(x))
11 void printh_f(char *p, unsigned char *x, size_t l)
12 {
13         int i;
14         printf("%s:", p);
15         for (i=0; i<l; i++) printf(" %02x", x[i]);
16         printf("\n");
17 }
18
19 int test_enc_dec(int iface1, int iface2)
20 {
21         unsigned long err;
22         unsigned char pt[48] = "the quick brown fox jumps over a lazy dog";
23         unsigned char key[16] = {0x0f,0x65,0xd1,0x3a,0xfe,0xcb,0xc4,0xb9,
24                                 0x52,0xb1,0x60,0xcf,0xe8,0x55,0x6a,0xdd};
25         unsigned char ct[64];
26         unsigned char re[48];
27
28         printf("%d -> %d\n", iface1, iface2);
29         printh(pt);
30         printh(key);
31         if (select_crypto_if(iface1)) return 1;
32         if ((err = encrypt(key, sizeof(key), pt, ct, sizeof(pt)))) {
33                 printf("encrypt error: %s\n", crypto_errstr(err));
34                 return 1;
35         }
36         printh(ct);
37         if (select_crypto_if(iface2)) return 1;
38         if ((err = decrypt(key, sizeof(key), ct, re, sizeof(ct)))) {
39                 printf("decrypt error: %s\n", crypto_errstr(err));
40                 return 1;
41         }
42         printh(re);
43         if (memcmp(pt, re, sizeof(pt))) {
44                 printf("fail\n");
45                 return 1;
46         }
47         return 0;
48 }
49
50 int test_sha(int iface)
51 {
52         unsigned char sha1[20];
53         unsigned long err;
54         int shalen;
55         unsigned char spt[3] = "abc";
56         unsigned char sstd[20] = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,
57                 0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};
58
59         if (select_crypto_if(iface)) return 1;
60         shalen = 20;
61         if ((err = hash(spt, sizeof(spt), sha1, &shalen))) {
62                 printf("hash error: %s\n", crypto_errstr(err));
63                 return 1;
64         }
65         printf("%d: len=%d ", iface, shalen);
66         printh(sha1);
67         if (memcmp(sha1, sstd, sizeof(sstd))) {
68                 printf("fail\n");
69                 return 1;
70         }
71         return 0;
72 }
73
74 int test_hmac(int iface)
75 {
76         unsigned char hmac1[20];
77         unsigned long err;
78         int hmaclen;
79         unsigned char hpt[28] = "what do ya want for nothing?";
80         unsigned char hkey[4] = "Jefe";
81         unsigned char hstd[20] = {0xef,0xfc,0xdf,0x6a,0xe5,0xeb,0x2f,0xa2,
82                 0xd2,0x74,0x16,0xd5,0xf1,0x84,0xdf,0x9c,0x25,0x9a,0x7c,0x79};
83
84         if (select_crypto_if(iface)) return 1;
85         hmaclen = 20;
86         if ((err = hmac(hkey, sizeof(hkey), hpt, sizeof(hpt),
87                                                 hmac1, &hmaclen))) {
88                 printf("hash error: %s\n", crypto_errstr(err));
89                 return 1;
90         }
91         printf("%d: len=%d ", iface, hmaclen);
92         printh(hmac1);
93         if (memcmp(hmac1, hstd, sizeof(hstd))) {
94                 printf("fail\n");
95                 return 1;
96         }
97         return 0;
98 }
99
100 int main(int argc, char *argv[])
101 {
102         int rc, maxrc = 0;
103         int numifs, i, j;
104
105         for (numifs = 0; select_crypto_if(numifs) == 0; numifs++)
106                 printf("%d: %s\n", numifs, if_name(numifs));
107         printf("Testing %d interfaces\n", numifs);
108
109         for (i = 0; i < numifs; i++)
110                 if ((rc = test_sha(i)) > maxrc) maxrc = rc;
111         for (i = 0; i < numifs; i++)
112                 if ((rc = test_hmac(i)) > maxrc) maxrc = rc;
113         for (i = 0; i < numifs; i++) for (j = 0; j < numifs; j++)
114                 if ((rc = test_enc_dec(i,j)) > maxrc) maxrc = rc;
115         return maxrc;
116 }