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