]> www.average.org Git - pam_pcsc_cr.git/blob - gnu_crypto.c
colorize svg picture
[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(const void *key, const int keylen, void *iv,
40                         const void *pt, void *ct, const 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(const void *key, const int keylen, void *iv,
60                         const void *ct, void *pt, const 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(const void *pt, const int tlen,
80                         void *tag, int *taglen)
81 {
82         gcry_error_t err;
83         gcry_md_hd_t hd;
84
85         unsigned int dlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
86         if (*taglen < dlen)
87                 return (unsigned long)gcry_error_from_errno(ENOMEM);
88         if ((err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_SECURE)))
89                 return (unsigned long)err;
90         gcry_md_write(hd, pt, tlen);
91         gcry_md_final(hd);
92         memcpy(tag, gcry_md_read(hd, GCRY_MD_SHA1), dlen);
93         gcry_md_close(hd);
94         *taglen = dlen;
95         return 0UL;
96 }
97
98 static unsigned long gnu_hmac(const void *key, const int keylen,
99                         const void *pt, const int tlen,
100                         void *tag, int *taglen)
101 {
102         gcry_error_t err;
103         gcry_md_hd_t hd;
104
105         unsigned int dlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
106         if (*taglen < dlen)
107                 return (unsigned long)gcry_error_from_errno(ENOMEM);
108         if ((err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_SECURE |
109                                                         GCRY_MD_FLAG_HMAC)))
110                 return (unsigned long)err;
111         if ((err = gcry_md_setkey(hd, key, keylen)))
112                 return (unsigned long)err;
113         gcry_md_write(hd, pt, tlen);
114         gcry_md_final(hd);
115         memcpy(tag, gcry_md_read(hd, GCRY_MD_SHA1), dlen);
116         gcry_md_close(hd);
117         *taglen = dlen;
118         return 0UL;
119 }
120
121 static const char *gnu_errstr(const unsigned long err)
122 {
123         return gcry_strerror((gcry_error_t)err);
124 }
125
126 struct crypto_interface gnu_crypto_if = {
127         .init           = gnu_init,
128         .encrypt        = gnu_encrypt,
129         .decrypt        = gnu_decrypt,
130         .hash           = gnu_hash,
131         .hmac           = gnu_hmac,
132         .errstr         = gnu_errstr,
133 };