]> www.average.org Git - pam_pcsc_cr.git/blob - gnu_crypto.c
1b6092b661dcb1a672b7e8301b2eb48991d39665
[pam_pcsc_cr.git] / gnu_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 <errno.h>
28 #include <gcrypt.h>
29 #include "crypto_if.h"
30
31 static const char *gnu_init(void)
32 {
33         (void)gcry_check_version(GCRYPT_VERSION);
34         gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
35         gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
36         return "gcrypt";
37 }
38
39 static unsigned long gnu_encrypt(void *key, int keylen, void *iv,
40                         void *pt, void *ct, int tlen)
41 {
42         gcry_error_t err;
43         gcry_cipher_hd_t hd;
44
45         if ((err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128,
46                                         GCRY_CIPHER_MODE_CBC, 0)))
47                 return (unsigned long)err;
48         if ((err = gcry_cipher_setkey(hd, key, keylen)))
49                 return (unsigned long)err;
50         if ((err = gcry_cipher_setiv(hd, iv, keylen)))
51                 return (unsigned long)err;
52         if ((err = gcry_cipher_encrypt(hd, ct, tlen, pt, tlen)))
53                 return (unsigned long)err;
54         if ((err = gcry_cipher_reset(hd)))
55                 return (unsigned long)err;
56         return 0UL;
57 }
58
59 static unsigned long gnu_decrypt(void *key, int keylen, void *iv,
60                         void *ct, void *pt, int tlen)
61 {
62         gcry_error_t err;
63         gcry_cipher_hd_t hd;
64
65         if ((err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128,
66                                         GCRY_CIPHER_MODE_CBC, 0)))
67                 return (unsigned long)err;
68         if ((err = gcry_cipher_setkey(hd, key, keylen)))
69                 return (unsigned long)err;
70         if ((err = gcry_cipher_setiv(hd, iv, keylen)))
71                 return (unsigned long)err;
72         if ((err = gcry_cipher_decrypt(hd, pt, tlen, ct, tlen)))
73                 return (unsigned long)err;
74         if ((err = gcry_cipher_reset(hd)))
75                 return (unsigned long)err;
76         return 0UL;
77 }
78
79 static unsigned long gnu_hash(void *pt, int tlen, void *tag, int *taglen)
80 {
81         gcry_error_t err;
82         gcry_md_hd_t hd;
83
84         unsigned int dlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
85         if (*taglen < dlen)
86                 return (unsigned long)gcry_error_from_errno(ENOMEM);
87         if ((err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_SECURE)))
88                 return (unsigned long)err;
89         gcry_md_write(hd, pt, tlen);
90         gcry_md_final(hd);
91         memcpy(tag, gcry_md_read(hd, GCRY_MD_SHA1), dlen);
92         gcry_md_close(hd);
93         *taglen = dlen;
94         return 0UL;
95 }
96
97 static unsigned long gnu_hmac(void *key, int keylen, void *pt, int tlen,
98                         void *tag, int *taglen)
99 {
100         gcry_error_t err;
101         gcry_md_hd_t hd;
102
103         unsigned int dlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
104         if (*taglen < dlen)
105                 return (unsigned long)gcry_error_from_errno(ENOMEM);
106         if ((err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_SECURE |
107                                                         GCRY_MD_FLAG_HMAC)))
108                 return (unsigned long)err;
109         if ((err = gcry_md_setkey(hd, key, keylen)))
110                 return (unsigned long)err;
111         gcry_md_write(hd, pt, tlen);
112         gcry_md_final(hd);
113         memcpy(tag, gcry_md_read(hd, GCRY_MD_SHA1), dlen);
114         gcry_md_close(hd);
115         *taglen = dlen;
116         return 0UL;
117 }
118
119 static const char *gnu_errstr(unsigned long err)
120 {
121         return gcry_strerror((gcry_error_t)err);
122 }
123
124 struct crypto_interface gnu_crypto_if = {
125         .init           = gnu_init,
126         .encrypt        = gnu_encrypt,
127         .decrypt        = gnu_decrypt,
128         .hash           = gnu_hash,
129         .hmac           = gnu_hmac,
130         .errstr         = gnu_errstr,
131 };