From: Eugene Crosser Date: Thu, 31 Oct 2013 07:13:19 +0000 (+0400) Subject: cleaner crypto init X-Git-Url: http://www.average.org/gitweb/?p=pam_pcsc_cr.git;a=commitdiff_plain;h=724570ad4aaaa5eb67fe0e808d638321d522eba7;hp=8c8a47cd542e60381773fe23f2075aa5b896be4f cleaner crypto init --- diff --git a/crypto.c b/crypto.c index b524f8a..003cc0f 100644 --- a/crypto.c +++ b/crypto.c @@ -28,10 +28,10 @@ int select_crypto_if(int ifno) 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}; diff --git a/crypto.h b/crypto.h index f0f709d..c24cce0 100644 --- a/crypto.h +++ b/crypto.h @@ -2,7 +2,7 @@ #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); diff --git a/crypto_if.h b/crypto_if.h index c24d6a3..2fb4519 100644 --- a/crypto_if.h +++ b/crypto_if.h @@ -2,7 +2,7 @@ #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, diff --git a/ossl_crypto.c b/ossl_crypto.c index 4fdbcf3..4b475a4 100644 --- a/ossl_crypto.c +++ b/ossl_crypto.c @@ -8,6 +8,12 @@ #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) { @@ -15,8 +21,6 @@ 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)) @@ -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) { - 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; } @@ -87,7 +86,7 @@ static const char *ossl_errstr(unsigned long err) } struct crypto_interface ossl_crypto_if = { - .name = "openssl", + .init = ossl_init, .encrypt = ossl_encrypt, .decrypt = ossl_decrypt, .hash = ossl_hash, diff --git a/test_crypto.c b/test_crypto.c index fe762cf..3654fa2 100644 --- a/test_crypto.c +++ b/test_crypto.c @@ -29,13 +29,14 @@ int test_enc_dec(int iface1, int iface2) 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 = 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; } @@ -101,10 +102,11 @@ int main(int argc, char *argv[]) { 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; diff --git a/tom_crypto.c b/tom_crypto.c index d774333..fa4fc5d 100644 --- a/tom_crypto.c +++ b/tom_crypto.c @@ -5,6 +5,12 @@ #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) { @@ -66,7 +72,7 @@ static const char *tom_errstr(unsigned long err) } struct crypto_interface tom_crypto_if = { - .name = "tomcrypt", + .init = tom_init, .encrypt = tom_encrypt, .decrypt = tom_decrypt, .hash = tom_hash,