]> www.average.org Git - pam_pcsc_cr.git/blob - ossl_crypto.c
try to preserve auth file ownership
[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(void *key, int keylen, void *iv,
41                         void *pt, void *ct, 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(void *key, int keylen, void *iv,
52                         void *ct, void *pt, 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(void *pt, int tlen, void *tag, int *taglen)
63 {
64         SHA_CTX sctx;
65
66         if (!SHA1_Init(&sctx)) return ERR_get_error();
67         if (!SHA1_Update(&sctx, pt, tlen)) return ERR_get_error();
68         if (!SHA1_Final(tag, &sctx)) return ERR_get_error();
69         *taglen = SHA_DIGEST_LENGTH;
70         return 0UL;
71 }
72
73 static unsigned long ossl_hmac(void *key, int keylen, void *pt, int tlen,
74                         void *tag, int *taglen)
75 {
76 #if 1
77         HMAC_CTX hctx;
78
79         HMAC_CTX_init(&hctx);
80         if (!HMAC_Init_ex(&hctx, key, keylen, EVP_sha1(), NULL))
81                 return ERR_get_error();
82         if (!HMAC_Update(&hctx, pt, tlen)) return ERR_get_error();
83         if (!HMAC_Final(&hctx, tag, (unsigned int *)taglen))
84                 return ERR_get_error();
85         HMAC_CTX_cleanup(&hctx);
86 #else
87         if (HMAC(EVP_sha1(), key, keylen, pt, tlen,
88                                 tag, (unsigned int *)taglen) != tag)
89                 return ERR_get_error();
90 #endif
91         return 0UL;
92 }
93
94 static const char *ossl_errstr(unsigned long err)
95 {
96         return ERR_error_string(err, NULL);
97 }
98
99 struct crypto_interface ossl_crypto_if = {
100         .init           = ossl_init,
101         .encrypt        = ossl_encrypt,
102         .decrypt        = ossl_decrypt,
103         .hash           = ossl_hash,
104         .hmac           = ossl_hmac,
105         .errstr         = ossl_errstr,
106 };