]> www.average.org Git - pam_pcsc_cr.git/blob - test_crypto.c
get rid of tokenid altogether
[pam_pcsc_cr.git] / test_crypto.c
1 /*
2 Copyright (c) 2013 Eugene Crosser
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12     1. The origin of this software must not be misrepresented; you must
13     not claim that you wrote the original software. If you use this
14     software in a product, an acknowledgment in the product documentation
15     would be appreciated but is not required.
16
17     2. Altered source versions must be plainly marked as such, and must
18     not be misrepresented as being the original software.
19
20     3. This notice may not be removed or altered from any source
21     distribution.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include "crypto.h"
32
33 #define printh(x) printh_f(#x, x, sizeof(x))
34 void printh_f(char *p, unsigned char *x, size_t l)
35 {
36         int i;
37         printf("%s:", p);
38         for (i=0; i<l; i++) printf(" %02x", x[i]);
39         printf("\n");
40 }
41
42 int test_enc_dec(int iface1, int iface2)
43 {
44         unsigned long err;
45         unsigned char pt[48] = "the quick brown fox jumps over a lazy dog";
46         unsigned char key[16] = {0x0f,0x65,0xd1,0x3a,0xfe,0xcb,0xc4,0xb9,
47                                 0x52,0xb1,0x60,0xcf,0xe8,0x55,0x6a,0xdd};
48         unsigned char ct[64];
49         unsigned char re[48];
50
51         printf("%d -> %d\n", iface1, iface2);
52         printh(pt);
53         printh(key);
54         if (select_crypto_if(iface1)) return 1;
55         memset(ct, 0xfe, sizeof(ct));
56         if ((err = encrypt(key, sizeof(key), pt, ct, sizeof(pt)))) {
57                 printf("encrypt error: %s\n", crypto_errstr(err));
58                 return 1;
59         }
60         printh(ct);
61         if (select_crypto_if(iface2)) return 1;
62         memset(re, 0xab, sizeof(re));
63         if ((err = decrypt(key, sizeof(key), ct, re, sizeof(re)))) {
64                 printf("decrypt error: %s\n", crypto_errstr(err));
65                 return 1;
66         }
67         printh(re);
68         if (memcmp(pt, re, sizeof(pt))) {
69                 printf("fail\n");
70                 return 1;
71         }
72         return 0;
73 }
74
75 int test_sha(int iface)
76 {
77         unsigned char sha1[20];
78         unsigned long err;
79         int shalen;
80         unsigned char spt[3] = "abc";
81         unsigned char sstd[20] = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,
82                 0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};
83
84         if (select_crypto_if(iface)) return 1;
85         memset(sha1, 0, sizeof(sha1));
86         shalen = sizeof(sha1);
87         if ((err = hash(spt, sizeof(spt), sha1, &shalen))) {
88                 printf("hash error: %s\n", crypto_errstr(err));
89                 return 1;
90         }
91         printf("%d: len=%d ", iface, shalen);
92         printh(sha1);
93         if (memcmp(sha1, sstd, sizeof(sstd))) {
94                 printf("fail\n");
95                 return 1;
96         }
97         return 0;
98 }
99
100 int test_hmac(int iface)
101 {
102         unsigned char hmac1[20];
103         unsigned long err;
104         int hmaclen;
105         unsigned char hpt[28] = "what do ya want for nothing?";
106         unsigned char hkey[4] = "Jefe";
107         unsigned char hstd[20] = {0xef,0xfc,0xdf,0x6a,0xe5,0xeb,0x2f,0xa2,
108                 0xd2,0x74,0x16,0xd5,0xf1,0x84,0xdf,0x9c,0x25,0x9a,0x7c,0x79};
109
110         if (select_crypto_if(iface)) return 1;
111         memset(hmac1, 0, sizeof(hmac1));
112         hmaclen = sizeof(hmac1);
113         if ((err = hmac(hkey, sizeof(hkey), hpt, sizeof(hpt),
114                                                 hmac1, &hmaclen))) {
115                 printf("hash error: %s\n", crypto_errstr(err));
116                 return 1;
117         }
118         printf("%d: len=%d ", iface, hmaclen);
119         printh(hmac1);
120         if (memcmp(hmac1, hstd, sizeof(hstd))) {
121                 printf("fail\n");
122                 return 1;
123         }
124         return 0;
125 }
126
127 int main(int argc, char *argv[])
128 {
129         int rc, maxrc = 0;
130         int numifs, i, j;
131         const char *name;
132
133         for (numifs = 0; (name = crypto_init(numifs)); numifs++)
134                 printf("%d: %s\n", numifs, name);
135         printf("Testing %d interfaces\n\n", numifs);
136
137         for (i = 0; i < numifs; i++)
138                 if ((rc = test_sha(i)) > maxrc) maxrc = rc;
139         for (i = 0; i < numifs; i++)
140                 if ((rc = test_hmac(i)) > maxrc) maxrc = rc;
141         for (i = 0; i < numifs; i++) for (j = 0; j < numifs; j++)
142                 if ((rc = test_enc_dec(i,j)) > maxrc) maxrc = rc;
143         return maxrc;
144 }