]> www.average.org Git - pam_pcsc_cr.git/commitdiff
cleaner crypto init
authorEugene Crosser <crosser@average.org>
Thu, 31 Oct 2013 07:13:19 +0000 (11:13 +0400)
committerEugene Crosser <crosser@average.org>
Thu, 31 Oct 2013 07:13:19 +0000 (11:13 +0400)
crypto.c
crypto.h
crypto_if.h
ossl_crypto.c
test_crypto.c
tom_crypto.c

index b524f8ad24d3631f02fe8b980d625afe9dcb0c64..003cc0f82da5a8f9dd51ef9abc74eab7b74caf5d 100644 (file)
--- a/crypto.c
+++ b/crypto.c
@@ -28,10 +28,10 @@ int select_crypto_if(int ifno)
        return 0;
 }
 
        return 0;
 }
 
-const char *if_name(int ifno)
+const char *crypto_init(int ifno)
 {
 {
-       if (ifno < 0 || ifno > MAX_IF) return "invalid index";
-       return ifs[ifno]->name;
+       if (ifno < 0 || ifno > MAX_IF) return (const char *)0;
+       return ifs[ifno]->init();
 }
 
 static unsigned char iv[16] = {0};
 }
 
 static unsigned char iv[16] = {0};
index f0f709d9a3e6e42af45eb38e5850bacfce924dad..c24cce07a76f7a6d910d675baeccff6b49bcc2ed 100644 (file)
--- a/crypto.h
+++ b/crypto.h
@@ -2,7 +2,7 @@
 #define _CRYPTO_H
 
 int select_crypto_if(int ifno);
 #define _CRYPTO_H
 
 int select_crypto_if(int ifno);
-const char *if_name(int ifno);
+const char *crypto_init(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);
 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 c24d6a3630106c6f63ec516fff1814b25036f8c9..2fb4519cba6971e956639f5284f7f0b6209247ff 100644 (file)
@@ -2,7 +2,7 @@
 #define _CRYPTO_IF_H
 
 struct crypto_interface {
 #define _CRYPTO_IF_H
 
 struct crypto_interface {
-       char *name;
+       const char *(*init)(void);
        unsigned long (*encrypt)(void *key, int keylen, void *iv,
                                void *pt, void *ct, int tlen);
        unsigned long (*decrypt)(void *key, int keylen, void *iv,
        unsigned long (*encrypt)(void *key, int keylen, void *iv,
                                void *pt, void *ct, int tlen);
        unsigned long (*decrypt)(void *key, int keylen, void *iv,
index 4fdbcf3df134c0a73930a0a46a2c4acde50f9687..4b475a4fcbe807d0e0d0894027873764861dba12 100644 (file)
@@ -8,6 +8,12 @@
 
 #include "crypto_if.h"
 
 
 #include "crypto_if.h"
 
+static const char *ossl_init(void)
+{
+       ERR_load_crypto_strings();
+       return "openssl";
+}
+
 static unsigned long ossl_encrypt(void *key, int keylen, void *iv,
                        void *pt, void *ct, int tlen)
 {
 static unsigned long ossl_encrypt(void *key, int keylen, void *iv,
                        void *pt, void *ct, int tlen)
 {
@@ -15,8 +21,6 @@ static unsigned long ossl_encrypt(void *key, int keylen, void *iv,
        int outlen1, outlen2;
        unsigned char hkey[16];
 
        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))
        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))
@@ -70,14 +74,9 @@ static unsigned long ossl_hash(void *pt, int tlen, void *tag, int *taglen)
 static unsigned long ossl_hmac(void *pt, int tlen, void *key, int keylen,
                        void *tag, int *taglen)
 {
 static unsigned long ossl_hmac(void *pt, int tlen, void *key, int keylen,
                        void *tag, int *taglen)
 {
-       HMAC_CTX hctx;
-
-       HMAC_CTX_init(&hctx);
-       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();
-       HMAC_CTX_cleanup(&hctx);
+       if (!HMAC(EVP_sha1(), key, keylen, pt, tlen,
+                               tag, (unsigned int *)taglen))
+                       return ERR_get_error();
        return 0UL;
 }
 
        return 0UL;
 }
 
@@ -87,7 +86,7 @@ static const char *ossl_errstr(unsigned long err)
 }
 
 struct crypto_interface ossl_crypto_if = {
 }
 
 struct crypto_interface ossl_crypto_if = {
-       .name           = "openssl",
+       .init           = ossl_init,
        .encrypt        = ossl_encrypt,
        .decrypt        = ossl_decrypt,
        .hash           = ossl_hash,
        .encrypt        = ossl_encrypt,
        .decrypt        = ossl_decrypt,
        .hash           = ossl_hash,
index fe762cf62158720a886394f74942705bd2acf80b..3654fa287259108a326f9ccc46290882f82ed9bd 100644 (file)
@@ -29,13 +29,14 @@ int test_enc_dec(int iface1, int iface2)
        printh(pt);
        printh(key);
        if (select_crypto_if(iface1)) return 1;
        printh(pt);
        printh(key);
        if (select_crypto_if(iface1)) return 1;
+       memset(ct, 0xfe, sizeof(ct));
        if ((err = encrypt(key, sizeof(key), pt, ct, sizeof(pt)))) {
                printf("encrypt error: %s\n", crypto_errstr(err));
                return 1;
        }
        printh(ct);
        if (select_crypto_if(iface2)) return 1;
        if ((err = encrypt(key, sizeof(key), pt, ct, sizeof(pt)))) {
                printf("encrypt error: %s\n", crypto_errstr(err));
                return 1;
        }
        printh(ct);
        if (select_crypto_if(iface2)) return 1;
-       if ((err = decrypt(key, sizeof(key), ct, re, sizeof(ct)))) {
+       if ((err = decrypt(key, sizeof(key), ct, re, sizeof(re)))) {
                printf("decrypt error: %s\n", crypto_errstr(err));
                return 1;
        }
                printf("decrypt error: %s\n", crypto_errstr(err));
                return 1;
        }
@@ -101,10 +102,11 @@ int main(int argc, char *argv[])
 {
        int rc, maxrc = 0;
        int numifs, i, j;
 {
        int rc, maxrc = 0;
        int numifs, i, j;
+       const char *name;
 
 
-       for (numifs = 0; select_crypto_if(numifs) == 0; numifs++)
-               printf("%d: %s\n", numifs, if_name(numifs));
-       printf("Testing %d interfaces\n", numifs);
+       for (numifs = 0; (name = crypto_init(numifs)); numifs++)
+               printf("%d: %s\n", numifs, name);
+       printf("Testing %d interfaces\n\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_sha(i)) > maxrc) maxrc = rc;
index d77433384a34515ca92b1a2aa9369a2ed36af09c..fa4fc5dabb0ae3d10a53c5e4753c6210a8cf2447 100644 (file)
@@ -5,6 +5,12 @@
 
 #include "crypto_if.h"
 
 
 #include "crypto_if.h"
 
+static const char *tom_init(void)
+{
+       /* nothing to initialize */
+       return "tomcrypt";
+}
+
 static unsigned long tom_encrypt(void *key, int keylen, void *iv,
                        void *pt, void *ct, int tlen)
 {
 static unsigned long tom_encrypt(void *key, int keylen, void *iv,
                        void *pt, void *ct, int tlen)
 {
@@ -66,7 +72,7 @@ static const char *tom_errstr(unsigned long err)
 }
 
 struct crypto_interface tom_crypto_if = {
 }
 
 struct crypto_interface tom_crypto_if = {
-       .name           = "tomcrypt",
+       .init           = tom_init,
        .encrypt        = tom_encrypt,
        .decrypt        = tom_decrypt,
        .hash           = tom_hash,
        .encrypt        = tom_encrypt,
        .decrypt        = tom_decrypt,
        .hash           = tom_hash,