]> www.average.org Git - pam_pcsc_cr.git/blob - crypto.c
282a713d494517afef007f57212df40691b264ca
[pam_pcsc_cr.git] / crypto.c
1 #include <assert.h>
2 #include "crypto.h"
3 #include "crypto_if.h"
4
5 extern struct crypto_interface ossl_crypto_if;
6 extern struct crypto_interface tom_crypto_if;
7
8 static struct crypto_interface *ifs[] = {
9 #ifdef HAVE_OPENSSL
10         &ossl_crypto_if,
11 #endif
12 #ifdef HAVE_TOMCRYPT
13         &tom_crypto_if,
14 #endif
15         (void*)0,
16 };
17 #define MAX_IF (sizeof(ifs)/sizeof(struct crypto_interface *)-2)
18
19 static int which = 0;
20
21 int select_crypto_if(int ifno)
22 {
23         if (ifno < 0 || ifno > MAX_IF) return -1;
24         which = ifno;
25         return 0;
26 }
27
28 static unsigned char iv[16] = {0};
29
30 unsigned long encrypt(void *key, int keylen, void *pt, void *ct, int tlen)
31 {
32         assert(keylen == 16);
33         return ifs[which]->encrypt(key, keylen, iv, pt, ct, tlen);
34 }
35
36 unsigned long decrypt(void *key, int keylen, void *ct, void *pt, int tlen)
37 {
38         assert(keylen == 16);
39         return ifs[which]->decrypt(key, keylen, iv, ct, pt, tlen);
40 }
41
42 unsigned long hash(void *pt, int tlen, void *tag, int *taglen)
43 {
44         assert(*taglen == 20);
45         return ifs[which]->hash(pt, tlen, tag, taglen);
46 }
47
48 unsigned long hmac(void *key, int keylen, void *pt, int tlen, void *tag, int *taglen)
49 {
50         assert(keylen == 20);
51         assert(*taglen == 20);
52         return ifs[which]->hmac(key, keylen, pt, tlen, tag, taglen);
53 }
54
55 const char *crypto_errstr(unsigned long err)
56 {
57         return ifs[which]->errstr(err);
58 }