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