2 Copyright (c) 2013 Eugene Crosser
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.
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:
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.
17 2. Altered source versions must be plainly marked as such, and must
18 not be misrepresented as being the original software.
20 3. This notice may not be removed or altered from any source
33 #define printh(x) printh_f(#x, x, sizeof(x))
34 void printh_f(char *p, unsigned char *x, size_t l)
38 for (i=0; i<l; i++) printf(" %02x", x[i]);
42 int test_enc_dec(int iface1, int iface2)
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};
51 printf("%d -> %d\n", iface1, iface2);
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));
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));
68 if (memcmp(pt, re, sizeof(pt))) {
75 int test_sha(int iface)
77 unsigned char sha1[20];
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};
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));
91 printf("%d: len=%d ", iface, shalen);
93 if (memcmp(sha1, sstd, sizeof(sstd))) {
100 int test_hmac(int iface)
102 unsigned char hmac1[20];
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};
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),
115 printf("hash error: %s\n", crypto_errstr(err));
118 printf("%d: len=%d ", iface, hmaclen);
120 if (memcmp(hmac1, hstd, sizeof(hstd))) {
127 int main(int argc, char *argv[])
133 for (numifs = 0; (name = crypto_init(numifs)); numifs++)
134 printf("%d: %s\n", numifs, name);
135 printf("Testing %d interfaces\n\n", numifs);
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;