]> www.average.org Git - pam_pcsc_cr.git/blob - ossl_crypto.c
update README and .svg
[pam_pcsc_cr.git] / ossl_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 <openssl/err.h>
28 #include <openssl/aes.h>
29 #include <openssl/sha.h>
30 #include <openssl/hmac.h>
31
32 #include "crypto_if.h"
33
34 static const char *ossl_init(void)
35 {
36         ERR_load_crypto_strings();
37         return "openssl";
38 }
39
40 static unsigned long ossl_encrypt(const void *key, const int keylen, void *iv,
41                         const void *pt, void *ct, const int tlen)
42 {
43         AES_KEY akey;
44
45         if (AES_set_encrypt_key(key, keylen*8, &akey))
46                 return ERR_get_error();
47         AES_cbc_encrypt(pt, ct, tlen, &akey, iv, AES_ENCRYPT);
48         return 0UL;
49 }
50
51 static unsigned long ossl_decrypt(const void *key, const int keylen, void *iv,
52                         const void *ct, void *pt, const int tlen)
53 {
54         AES_KEY akey;
55
56         if (AES_set_decrypt_key(key, keylen*8, &akey))
57                 return ERR_get_error();
58         AES_cbc_encrypt(ct, pt, tlen, &akey, iv, AES_DECRYPT);
59         return 0UL;
60 }
61
62 static unsigned long ossl_hash(const void *pt, const int tlen,
63                         void *tag, int *taglen)
64 {
65         SHA_CTX sctx;
66
67         if (!SHA1_Init(&sctx)) return ERR_get_error();
68         if (!SHA1_Update(&sctx, pt, tlen)) return ERR_get_error();
69         if (!SHA1_Final(tag, &sctx)) return ERR_get_error();
70         *taglen = SHA_DIGEST_LENGTH;
71         return 0UL;
72 }
73
74 static unsigned long ossl_hmac(const void *key, int const keylen,
75                         const void *pt, const int tlen,
76                         void *tag, int *taglen)
77 {
78 #if 1
79         HMAC_CTX hctx;
80
81         HMAC_CTX_init(&hctx);
82         if (!HMAC_Init_ex(&hctx, key, keylen, EVP_sha1(), NULL))
83                 return ERR_get_error();
84         if (!HMAC_Update(&hctx, pt, tlen)) return ERR_get_error();
85         if (!HMAC_Final(&hctx, tag, (unsigned int *)taglen))
86                 return ERR_get_error();
87         HMAC_CTX_cleanup(&hctx);
88 #else
89         if (HMAC(EVP_sha1(), key, keylen, pt, tlen,
90                                 tag, (unsigned int *)taglen) != tag)
91                 return ERR_get_error();
92 #endif
93         return 0UL;
94 }
95
96 static const char *ossl_errstr(const unsigned long err)
97 {
98         return ERR_error_string(err, NULL);
99 }
100
101 struct crypto_interface ossl_crypto_if = {
102         .init           = ossl_init,
103         .encrypt        = ossl_encrypt,
104         .decrypt        = ossl_decrypt,
105         .hash           = ossl_hash,
106         .hmac           = ossl_hmac,
107         .errstr         = ossl_errstr,
108 };