]> www.average.org Git - pam_pcsc_cr.git/blob - crypto.c
replace README_CR with svg pic
[pam_pcsc_cr.git] / crypto.c
1 /*
2 Copyright (c) 2013 Eugene Crosser
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12     1. The origin of this software must not be misrepresented; you must
13     not claim that you wrote the original software. If you use this
14     software in a product, an acknowledgment in the product documentation
15     would be appreciated but is not required.
16
17     2. Altered source versions must be plainly marked as such, and must
18     not be misrepresented as being the original software.
19
20     3. This notice may not be removed or altered from any source
21     distribution.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <assert.h>
28 #include "crypto.h"
29 #include "crypto_if.h"
30
31 extern struct crypto_interface ossl_crypto_if;
32 extern struct crypto_interface tom_crypto_if;
33 extern struct crypto_interface gnu_crypto_if;
34
35 static struct crypto_interface *ifs[] = {
36 #ifdef HAVE_OPENSSL
37         &ossl_crypto_if,
38 #endif
39 #ifdef HAVE_TOMCRYPT
40         &tom_crypto_if,
41 #endif
42 #ifdef HAVE_GCRYPT
43         &gnu_crypto_if,
44 #endif
45         (struct crypto_interface *)0,
46 };
47 #define MAX_IF (sizeof(ifs)/sizeof(struct crypto_interface *)-2)
48
49 static int which = 0;
50
51 int select_crypto_if(const int ifno)
52 {
53         if (ifno < 0 || ifno > MAX_IF) return -1;
54         which = ifno;
55         return 0;
56 }
57
58 const char *crypto_init(const int ifno)
59 {
60         if (ifno < 0 || ifno > MAX_IF) return (const char *)0;
61         return ifs[ifno]->init();
62 }
63
64 #define INITIV {0}
65
66 unsigned long encrypt(const void *key, const int keylen, const void *pt, void *ct, const int tlen)
67 {
68         unsigned char iv[16] = INITIV;
69
70         assert(keylen == 16);
71         return ifs[which]->encrypt(key, keylen, iv, pt, ct, tlen);
72 }
73
74 unsigned long decrypt(const void *key, const int keylen, const void *ct, void *pt, const int tlen)
75 {
76         unsigned char iv[16] = INITIV;
77
78         assert(keylen == 16);
79         return ifs[which]->decrypt(key, keylen, iv, ct, pt, tlen);
80 }
81
82 unsigned long hash(const void *pt, const int tlen, void *tag, int *taglen)
83 {
84         assert(*taglen == 20);
85         return ifs[which]->hash(pt, tlen, tag, taglen);
86 }
87
88 unsigned long hmac(const void *key, const int keylen, const void *pt, const int tlen, void *tag, int *taglen)
89 {
90         assert(*taglen == 20);
91         return ifs[which]->hmac(key, keylen, pt, tlen, tag, taglen);
92 }
93
94 const char *crypto_errstr(const unsigned long err)
95 {
96         return ifs[which]->errstr(err);
97 }