From: Eugene Crosser Date: Tue, 29 Oct 2013 15:49:29 +0000 (+0400) Subject: crypto interface X-Git-Url: http://www.average.org/gitweb/?p=pam_pcsc_cr.git;a=commitdiff_plain;h=582620961e80e33514b39ab76648be761fd55368;hp=aaa54fb1a4ec04a0f59e47170feb8173514f5ff5 crypto interface --- diff --git a/Makefile.am b/Makefile.am index e83b179..23711ac 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,11 +3,11 @@ AUTOMAKE_OPTIONS = foreign ACLOCAL_AMFLAGS = -I m4 -noinst_HEADERS = pcsc_cr.h token.h +noinst_HEADERS = pcsc_cr.h token.h crypto_if.h crypto.h EXTRA_LTLIBRARIES = libpcsc_cr.la libpcsc_cr_la_SOURCES = crypto.c pcsc_cr.c ykneo.c -EXTRA_libpcsc_cr_la_SOURCES = ossl_crypto.c tom_prypto.c +EXTRA_libpcsc_cr_la_SOURCES = ossl_crypto.c tom_crypto.c libpcsc_cr_la_LIBADD = @CRYPTO_OBJS@ libpcsc_cr_la_DEPENDENCIES = @CRYPTO_OBJS@ diff --git a/crypto.c b/crypto.c index ab6a3c4..83ba4ef 100644 --- a/crypto.c +++ b/crypto.c @@ -1,30 +1,28 @@ -#include -#include +#include "crypto.h" +#include "crypto_if.h" -//int main() -//{ -// EVP_CIPHER_CTX ctx; -// unsigned char key[32] = {0}; -// unsigned char iv[16] = {0}; -// unsigned char in[16] = {0}; -// unsigned char out[32]; /* at least one block longer than in[] */ -// int outlen1, outlen2; -// -// EVP_EncryptInit(&ctx, EVP_aes_256_cbc(), key, iv); -// EVP_EncryptUpdate(&ctx, out, &outlen1, in, sizeof(in)); -// EVP_EncryptFinal(&ctx, out + outlen1, &outlen2); -// -// printf("ciphertext length: %d\n", outlen1 + outlen2); -// -// return 0; -//} +extern struct crypto_interface ossl_crypto_if; +extern struct crypto_interface tom_crypto_if; -// result = HMAC(EVP_sha256(), key, 999, data, 888, NULL, NULL); -// EVP_MD * +static struct crypto_interface *active = &ossl_crypto_if; + +int encrypt(void *pt, int ptlen, void *key, int keylen, void *ct, int *ctlen) +{ + return active->encrypt(pt, ptlen, key, keylen, ct, ctlen); +} + +int decrypt(void *ct, int ctlen, void *key, int keylen, void *pt, int *ptlen) +{ + return active->decrypt(ct, ctlen, key, keylen, pt, ptlen); +} + +int hash(void *pt, int ptlen, void *tag, int *taglen) +{ + return active->hash(pt, ptlen, tag, taglen); +} + +int hmac(void *pt, int ptlen, void *key, int keylen, void *tag, int *taglen) +{ + return active->hmac(pt, ptlen, key, keylen, tag, taglen); +} -// HMAC_CTX hctx; -// HMAC_CTX_init(&hctx); -// if (HMAC_Init(&hctx, key, keylen, EVP_sha1())) success; -// if (HMAC_Update(&hctx, data, datalen)) success; -// if (HMAC_Final(&hctx, &digest, &digestlen)) success -// HMAC_CTX_cleanup(&hctx); diff --git a/crypto_if.h b/crypto_if.h new file mode 100644 index 0000000..488a0e6 --- /dev/null +++ b/crypto_if.h @@ -0,0 +1,15 @@ +#ifndef _CRYPTO_IF_H +#define _CRYPTO_IF_H + +struct crypto_interface { + char *name; + int (*encrypt)(void *pt, int ptlen, void *key, int keylen, + void *ct, int *ctlen); + int (*decrypt)(void *ct, int ctlen, void *key, int keylen, + void *pt, int *ptlen); + int (*hash)(void *pt, int ptlen, void *tag, int *taglen); + int (*hmac)(void *ct, int ctlen, void *key, int keylen, + void *tag, int *taglen); +}; + +#endif diff --git a/ossl_crypto.c b/ossl_crypto.c index e69de29..61c6504 100644 --- a/ossl_crypto.c +++ b/ossl_crypto.c @@ -0,0 +1,53 @@ +#include +#include + +#include "crypto_if.h" + +static int ossl_encrypt(void *pt, int ptlen, void *key, int keylen, + void *ct, int *ctlen) +{ + EVP_CIPHER_CTX ctx; + unsigned char iv[16] = {0}; + int outlen1, outlen2; + + EVP_EncryptInit(&ctx, EVP_aes_256_cbc(), key, iv); + EVP_EncryptUpdate(&ctx, ct, &outlen1, pt, ptlen); + EVP_EncryptFinal(&ctx, ct + outlen1, &outlen2); + if (outlen1 + outlen2 > *ctlen) return -1; + *ctlen = outlen1 + outlen2; + + return 0; +} + +static int ossl_decrypt() +{ + return 0; +} + +static int ossl_hash() +{ + return 0; +} + +static int ossl_hmac() +{ + return 0; +} + +// result = HMAC(EVP_sha256(), key, 999, data, 888, NULL, NULL); +// EVP_MD * + +// HMAC_CTX hctx; +// HMAC_CTX_init(&hctx); +// if (HMAC_Init(&hctx, key, keylen, EVP_sha1())) success; +// if (HMAC_Update(&hctx, data, datalen)) success; +// if (HMAC_Final(&hctx, &digest, &digestlen)) success +// HMAC_CTX_cleanup(&hctx); + +struct crypto_interface ossl_crypto_if = { + .name = "openssl", + .encrypt = ossl_encrypt, + .decrypt = ossl_decrypt, + .hash = ossl_hash, + .hmac = ossl_hmac, +};