2 Copyright (c) 2013 Eugene Crosser
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.
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:
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.
17 2. Altered source versions must be plainly marked as such, and must
18 not be misrepresented as being the original software.
20 3. This notice may not be removed or altered from any source
29 #include "crypto_if.h"
31 static const char *gnu_init(void)
33 (void)gcry_check_version(GCRYPT_VERSION);
34 gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
35 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
39 static unsigned long gnu_encrypt(void *key, int keylen, void *iv,
40 void *pt, void *ct, int tlen)
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;
59 static unsigned long gnu_decrypt(void *key, int keylen, void *iv,
60 void *ct, void *pt, int tlen)
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;
79 static unsigned long gnu_hash(void *pt, int tlen, void *tag, int *taglen)
84 unsigned int dlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
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);
91 memcpy(tag, gcry_md_read(hd, GCRY_MD_SHA1), dlen);
97 static unsigned long gnu_hmac(void *key, int keylen, void *pt, int tlen,
98 void *tag, int *taglen)
103 unsigned int dlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
105 return (unsigned long)gcry_error_from_errno(ENOMEM);
106 if ((err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_SECURE |
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);
113 memcpy(tag, gcry_md_read(hd, GCRY_MD_SHA1), dlen);
119 static const char *gnu_errstr(unsigned long err)
121 return gcry_strerror((gcry_error_t)err);
124 struct crypto_interface gnu_crypto_if = {
126 .encrypt = gnu_encrypt,
127 .decrypt = gnu_decrypt,
130 .errstr = gnu_errstr,