]> www.average.org Git - pam_pcsc_cr.git/blob - test_crypto.c
eprint don't need \n in the format
[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         memset(ct, 0xfe, sizeof(ct));
33         if ((err = encrypt(key, sizeof(key), pt, ct, sizeof(pt)))) {
34                 printf("encrypt error: %s\n", crypto_errstr(err));
35                 return 1;
36         }
37         printh(ct);
38         if (select_crypto_if(iface2)) return 1;
39         memset(re, 0xab, sizeof(re));
40         if ((err = decrypt(key, sizeof(key), ct, re, sizeof(re)))) {
41                 printf("decrypt error: %s\n", crypto_errstr(err));
42                 return 1;
43         }
44         printh(re);
45         if (memcmp(pt, re, sizeof(pt))) {
46                 printf("fail\n");
47                 return 1;
48         }
49         return 0;
50 }
51
52 int test_sha(int iface)
53 {
54         unsigned char sha1[20];
55         unsigned long err;
56         int shalen;
57         unsigned char spt[3] = "abc";
58         unsigned char sstd[20] = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,
59                 0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};
60
61         if (select_crypto_if(iface)) return 1;
62         memset(sha1, 0, sizeof(sha1));
63         shalen = sizeof(sha1);
64         if ((err = hash(spt, sizeof(spt), sha1, &shalen))) {
65                 printf("hash error: %s\n", crypto_errstr(err));
66                 return 1;
67         }
68         printf("%d: len=%d ", iface, shalen);
69         printh(sha1);
70         if (memcmp(sha1, sstd, sizeof(sstd))) {
71                 printf("fail\n");
72                 return 1;
73         }
74         return 0;
75 }
76
77 int test_hmac(int iface)
78 {
79         unsigned char hmac1[20];
80         unsigned long err;
81         int hmaclen;
82         unsigned char hpt[28] = "what do ya want for nothing?";
83         unsigned char hkey[4] = "Jefe";
84         unsigned char hstd[20] = {0xef,0xfc,0xdf,0x6a,0xe5,0xeb,0x2f,0xa2,
85                 0xd2,0x74,0x16,0xd5,0xf1,0x84,0xdf,0x9c,0x25,0x9a,0x7c,0x79};
86
87         if (select_crypto_if(iface)) return 1;
88         memset(hmac1, 0, sizeof(hmac1));
89         hmaclen = sizeof(hmac1);
90         if ((err = hmac(hkey, sizeof(hkey), hpt, sizeof(hpt),
91                                                 hmac1, &hmaclen))) {
92                 printf("hash error: %s\n", crypto_errstr(err));
93                 return 1;
94         }
95         printf("%d: len=%d ", iface, hmaclen);
96         printh(hmac1);
97         if (memcmp(hmac1, hstd, sizeof(hstd))) {
98                 printf("fail\n");
99                 return 1;
100         }
101         return 0;
102 }
103
104 int main(int argc, char *argv[])
105 {
106         int rc, maxrc = 0;
107         int numifs, i, j;
108         const char *name;
109
110         for (numifs = 0; (name = crypto_init(numifs)); numifs++)
111                 printf("%d: %s\n", numifs, name);
112         printf("Testing %d interfaces\n\n", numifs);
113
114         for (i = 0; i < numifs; i++)
115                 if ((rc = test_sha(i)) > maxrc) maxrc = rc;
116         for (i = 0; i < numifs; i++)
117                 if ((rc = test_hmac(i)) > maxrc) maxrc = rc;
118         for (i = 0; i < numifs; i++) for (j = 0; j < numifs; j++)
119                 if ((rc = test_enc_dec(i,j)) > maxrc) maxrc = rc;
120         return maxrc;
121 }