]> www.average.org Git - pam_pcsc_cr.git/blob - crypto.c
fix serialization and auth test
[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(const 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(const int ifno)
32 {
33         if (ifno < 0 || ifno > MAX_IF) return (const char *)0;
34         return ifs[ifno]->init();
35 }
36
37 #define INITIV {0}
38
39 unsigned long encrypt(const void *key, const int keylen, const void *pt, void *ct, const int tlen)
40 {
41         unsigned char iv[16] = INITIV;
42
43         assert(keylen == 16);
44         return ifs[which]->encrypt(key, keylen, iv, pt, ct, tlen);
45 }
46
47 unsigned long decrypt(const void *key, const int keylen, const void *ct, void *pt, const int tlen)
48 {
49         unsigned char iv[16] = INITIV;
50
51         assert(keylen == 16);
52         return ifs[which]->decrypt(key, keylen, iv, ct, pt, tlen);
53 }
54
55 unsigned long hash(const void *pt, const int tlen, void *tag, int *taglen)
56 {
57         assert(*taglen == 20);
58         return ifs[which]->hash(pt, tlen, tag, taglen);
59 }
60
61 unsigned long hmac(const void *key, const int keylen, const void *pt, const int tlen, void *tag, int *taglen)
62 {
63         assert(*taglen == 20);
64         return ifs[which]->hmac(key, keylen, pt, tlen, tag, taglen);
65 }
66
67 const char *crypto_errstr(const unsigned long err)
68 {
69         return ifs[which]->errstr(err);
70 }