]> www.average.org Git - pam_pcsc_cr.git/commitdiff
wip on crypto
authorEugene Crosser <crosser@average.org>
Wed, 30 Oct 2013 23:02:23 +0000 (03:02 +0400)
committerEugene Crosser <crosser@average.org>
Wed, 30 Oct 2013 23:02:23 +0000 (03:02 +0400)
crypto.c
crypto.h
ossl_crypto.c
pcsc_cr.c
test_cr.c
test_crypto.c
tom_crypto.c
ykneo.c

index 282a713d494517afef007f57212df40691b264ca..b524f8ad24d3631f02fe8b980d625afe9dcb0c64 100644 (file)
--- a/crypto.c
+++ b/crypto.c
@@ -1,3 +1,6 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 #include <assert.h>
 #include "crypto.h"
 #include "crypto_if.h"
@@ -25,6 +28,12 @@ int select_crypto_if(int ifno)
        return 0;
 }
 
+const char *if_name(int ifno)
+{
+       if (ifno < 0 || ifno > MAX_IF) return "invalid index";
+       return ifs[ifno]->name;
+}
+
 static unsigned char iv[16] = {0};
 
 unsigned long encrypt(void *key, int keylen, void *pt, void *ct, int tlen)
@@ -47,7 +56,6 @@ unsigned long hash(void *pt, int tlen, void *tag, int *taglen)
 
 unsigned long hmac(void *key, int keylen, void *pt, int tlen, void *tag, int *taglen)
 {
-       assert(keylen == 20);
        assert(*taglen == 20);
        return ifs[which]->hmac(key, keylen, pt, tlen, tag, taglen);
 }
index 06d7cd5be72f9001e227f5e47da10bad54b554c6..f0f709d9a3e6e42af45eb38e5850bacfce924dad 100644 (file)
--- a/crypto.h
+++ b/crypto.h
@@ -2,6 +2,7 @@
 #define _CRYPTO_H
 
 int select_crypto_if(int ifno);
+const char *if_name(int ifno);
 unsigned long encrypt(void *key, int keylen, void *pt, void *ct, int tlen);
 unsigned long decrypt(void *key, int keylen, void *ct, void *pt, int tlen);
 unsigned long hash(void *pt, int tlen, void *tag, int *taglen);
index a7fc515b5db6f5642c77fd756f8f28d7d286799f..4fdbcf3df134c0a73930a0a46a2c4acde50f9687 100644 (file)
@@ -1,3 +1,6 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 #include <openssl/err.h>
 #include <openssl/sha.h>
 #include <openssl/evp.h>
@@ -12,6 +15,8 @@ static unsigned long ossl_encrypt(void *key, int keylen, void *iv,
        int outlen1, outlen2;
        unsigned char hkey[16];
 
+       ERR_load_crypto_strings(); /* FIXME */
+
        if (EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(),
                        NULL, key, keylen, 5, hkey, NULL) != 16) return 1UL;
        if (!EVP_EncryptInit(&ctx, EVP_aes_128_cbc(), hkey, iv))
@@ -20,7 +25,11 @@ static unsigned long ossl_encrypt(void *key, int keylen, void *iv,
                return ERR_get_error();
        if (!EVP_EncryptFinal(&ctx, ct + outlen1, &outlen2))
                return ERR_get_error();
-       if (outlen1 + outlen2 != tlen) return 1UL;
+       if (outlen1 + outlen2 != tlen) {
+               printf("enc tlen =%d outlen1=%d outlen2=%d\n",
+                       tlen, outlen1, outlen2);
+               // return 1UL;
+       }
        return 0UL;
 }
 
@@ -39,7 +48,11 @@ static unsigned long ossl_decrypt(void *key, int keylen, void *iv,
                return ERR_get_error();
        if (!EVP_DecryptFinal(&ctx, ct + outlen1, &outlen2))
                return ERR_get_error();
-       if (outlen1 + outlen2 != tlen) return 1UL;
+       if (outlen1 + outlen2 != tlen) {
+               printf("dec tlen =%d outlen1=%d outlen2=%d\n",
+                       tlen, outlen1, outlen2);
+               // return 1UL;
+       }
        return 0UL;
 }
 
@@ -50,7 +63,7 @@ static unsigned long ossl_hash(void *pt, int tlen, void *tag, int *taglen)
        if (!SHA1_Init(&sctx)) return ERR_get_error();
        if (!SHA1_Update(&sctx, pt, tlen)) return ERR_get_error();
        if (!SHA1_Final(tag, &sctx)) return ERR_get_error();
-       *taglen = 160;
+       *taglen = 20;
        return 0UL;
 }
 
@@ -60,7 +73,7 @@ static unsigned long ossl_hmac(void *pt, int tlen, void *key, int keylen,
        HMAC_CTX hctx;
 
        HMAC_CTX_init(&hctx);
-       if (!HMAC_Init(&hctx, key, keylen, EVP_sha1())) return ERR_get_error();
+       if (!HMAC_Init_ex(&hctx, key, keylen, EVP_sha1(), NULL)) return ERR_get_error();
        if (!HMAC_Update(&hctx, pt, tlen)) return ERR_get_error();
        if (!HMAC_Final(&hctx, tag, (unsigned int *)taglen))
                return ERR_get_error();
index 03a05d090583ce962d5217dda8f294a66a69141f..52a18e7d9dbcf275f0ab675c96fc3f28856847f0 100644 (file)
--- a/pcsc_cr.c
+++ b/pcsc_cr.c
@@ -1,3 +1,6 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 #include <stdio.h>
 #include <string.h>
 #include <alloca.h>
index 9415406d5ef62135ed8f56cc1ae843f03146c6c7..43991d1e5ce18b7f5c0a5ace2bc97c885116531d 100644 (file)
--- a/test_cr.c
+++ b/test_cr.c
@@ -1,3 +1,6 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
index 695c4fe249fbe0178f434973ef82a397d36fac77..fe762cf62158720a886394f74942705bd2acf80b 100644 (file)
@@ -1,20 +1,13 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include "crypto.h"
 
-unsigned char pt[48] = "the quick brown fox jumps over a lazy dog";
-unsigned char key[16] = {
-0x0f,0x65,0xd1,0x3a,0xfe,0xcb,0xc4,0xb9,0x52,0xb1,0x60,0xcf,0xe8,0x55,0x6a,0xdd
-};
-
-static void usage(const char const *cmd)
-{
-       fprintf(stderr, "usage: %s\n", cmd);
-}
-
-#define printh(p,x) printh_f(p, x, sizeof(x))
+#define printh(x) printh_f(#x, x, sizeof(x))
 void printh_f(char *p, unsigned char *x, size_t l)
 {
        int i;
@@ -23,23 +16,101 @@ void printh_f(char *p, unsigned char *x, size_t l)
        printf("\n");
 }
 
-int main(int argc, char *argv[])
+int test_enc_dec(int iface1, int iface2)
 {
        unsigned long err;
-       unsigned char ct1[48], re1[48];
-       unsigned char sha1[20], sha2[20];
-       unsigned char hmac1[20], hmac2[20];
-
-       printf("source: %s\n", pt);
-       printh("source", pt);
-       printh("key", key);
-       if (select_crypto_if(0)) return 1;
-       if (err = encrypt(key, sizeof(key), pt, ct1, sizeof(pt)))
+       unsigned char pt[48] = "the quick brown fox jumps over a lazy dog";
+       unsigned char key[16] = {0x0f,0x65,0xd1,0x3a,0xfe,0xcb,0xc4,0xb9,
+                               0x52,0xb1,0x60,0xcf,0xe8,0x55,0x6a,0xdd};
+       unsigned char ct[64];
+       unsigned char re[48];
+
+       printf("%d -> %d\n", iface1, iface2);
+       printh(pt);
+       printh(key);
+       if (select_crypto_if(iface1)) return 1;
+       if ((err = encrypt(key, sizeof(key), pt, ct, sizeof(pt)))) {
                printf("encrypt error: %s\n", crypto_errstr(err));
-       printh("ct1", ct1);
-       if (err = decrypt(key, sizeof(key), ct1, re1, sizeof(re1)))
+               return 1;
+       }
+       printh(ct);
+       if (select_crypto_if(iface2)) return 1;
+       if ((err = decrypt(key, sizeof(key), ct, re, sizeof(ct)))) {
                printf("decrypt error: %s\n", crypto_errstr(err));
-       printh("re1", re1);
-       if (select_crypto_if(1)) return 1;
+               return 1;
+       }
+       printh(re);
+       if (memcmp(pt, re, sizeof(pt))) {
+               printf("fail\n");
+               return 1;
+       }
+       return 0;
+}
+
+int test_sha(int iface)
+{
+       unsigned char sha1[20];
+       unsigned long err;
+       int shalen;
+       unsigned char spt[3] = "abc";
+       unsigned char sstd[20] = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,
+               0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};
+
+       if (select_crypto_if(iface)) return 1;
+       shalen = 20;
+       if ((err = hash(spt, sizeof(spt), sha1, &shalen))) {
+               printf("hash error: %s\n", crypto_errstr(err));
+               return 1;
+       }
+       printf("%d: len=%d ", iface, shalen);
+       printh(sha1);
+       if (memcmp(sha1, sstd, sizeof(sstd))) {
+               printf("fail\n");
+               return 1;
+       }
        return 0;
 }
+
+int test_hmac(int iface)
+{
+       unsigned char hmac1[20];
+       unsigned long err;
+       int hmaclen;
+       unsigned char hpt[28] = "what do ya want for nothing?";
+       unsigned char hkey[4] = "Jefe";
+       unsigned char hstd[20] = {0xef,0xfc,0xdf,0x6a,0xe5,0xeb,0x2f,0xa2,
+               0xd2,0x74,0x16,0xd5,0xf1,0x84,0xdf,0x9c,0x25,0x9a,0x7c,0x79};
+
+       if (select_crypto_if(iface)) return 1;
+       hmaclen = 20;
+       if ((err = hmac(hkey, sizeof(hkey), hpt, sizeof(hpt),
+                                               hmac1, &hmaclen))) {
+               printf("hash error: %s\n", crypto_errstr(err));
+               return 1;
+       }
+       printf("%d: len=%d ", iface, hmaclen);
+       printh(hmac1);
+       if (memcmp(hmac1, hstd, sizeof(hstd))) {
+               printf("fail\n");
+               return 1;
+       }
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       int rc, maxrc = 0;
+       int numifs, i, j;
+
+       for (numifs = 0; select_crypto_if(numifs) == 0; numifs++)
+               printf("%d: %s\n", numifs, if_name(numifs));
+       printf("Testing %d interfaces\n", numifs);
+
+       for (i = 0; i < numifs; i++)
+               if ((rc = test_sha(i)) > maxrc) maxrc = rc;
+       for (i = 0; i < numifs; i++)
+               if ((rc = test_hmac(i)) > maxrc) maxrc = rc;
+       for (i = 0; i < numifs; i++) for (j = 0; j < numifs; j++)
+               if ((rc = test_enc_dec(i,j)) > maxrc) maxrc = rc;
+       return maxrc;
+}
index 97a18e19fafcab5169b3963a7310a1befccd3388..d77433384a34515ca92b1a2aa9369a2ed36af09c 100644 (file)
@@ -1,3 +1,6 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 #include <tomcrypt.h>
 
 #include "crypto_if.h"
@@ -50,7 +53,6 @@ static unsigned long tom_hmac(void *key, int keylen,
        int index, rc;
        unsigned long ltaglen = *taglen;
 
-       if (keylen != 20) return CRYPT_INVALID_KEYSIZE;
        if ((index = register_hash(&sha1_desc)) == -1)
                return CRYPT_INVALID_HASH;
        rc = hmac_memory(index, key, keylen, pt, tlen, tag, &ltaglen);
diff --git a/ykneo.c b/ykneo.c
index 2510e18d307ab3f68a3d27a1f50f784c677eae38..5a153a545dd3af4c1d9131d0bb3138465394f029 100644 (file)
--- a/ykneo.c
+++ b/ykneo.c
@@ -1,3 +1,6 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 #include <stdlib.h>
 #include <string.h>
 #include <alloca.h>