]> www.average.org Git - pam_pcsc_cr.git/blob - ossl_crypto.c
reinit iv before every op
[pam_pcsc_cr.git] / ossl_crypto.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4 #include <openssl/err.h>
5 #include <openssl/aes.h>
6 #include <openssl/sha.h>
7 #include <openssl/hmac.h>
8
9 #include "crypto_if.h"
10
11 static const char *ossl_init(void)
12 {
13         ERR_load_crypto_strings();
14         return "openssl";
15 }
16
17 static unsigned long ossl_encrypt(void *key, int keylen, void *iv,
18                         void *pt, void *ct, int tlen)
19 {
20         AES_KEY akey;
21
22         if (AES_set_encrypt_key(key, keylen*8, &akey))
23                 return ERR_get_error();
24         AES_cbc_encrypt(pt, ct, tlen, &akey, iv, AES_ENCRYPT);
25         return 0UL;
26 }
27
28 static unsigned long ossl_decrypt(void *key, int keylen, void *iv,
29                         void *ct, void *pt, int tlen)
30 {
31         AES_KEY akey;
32
33         if (AES_set_decrypt_key(key, keylen*8, &akey))
34                 return ERR_get_error();
35         AES_cbc_encrypt(ct, pt, tlen, &akey, iv, AES_DECRYPT);
36         return 0UL;
37 }
38
39 static unsigned long ossl_hash(void *pt, int tlen, void *tag, int *taglen)
40 {
41         SHA_CTX sctx;
42
43         if (!SHA1_Init(&sctx)) return ERR_get_error();
44         if (!SHA1_Update(&sctx, pt, tlen)) return ERR_get_error();
45         if (!SHA1_Final(tag, &sctx)) return ERR_get_error();
46         *taglen = SHA_DIGEST_LENGTH;
47         return 0UL;
48 }
49
50 static unsigned long ossl_hmac(void *pt, int tlen, void *key, int keylen,
51                         void *tag, int *taglen)
52 {
53         if (!HMAC(EVP_sha1(), key, keylen, pt, tlen,
54                                 tag, (unsigned int *)taglen))
55                         return ERR_get_error();
56         return 0UL;
57 }
58
59 static const char *ossl_errstr(unsigned long err)
60 {
61         return ERR_error_string(err, NULL);
62 }
63
64 struct crypto_interface ossl_crypto_if = {
65         .init           = ossl_init,
66         .encrypt        = ossl_encrypt,
67         .decrypt        = ossl_decrypt,
68         .hash           = ossl_hash,
69         .hmac           = ossl_hmac,
70         .errstr         = ossl_errstr,
71 };