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(const void *key, const int keylen, void *iv,
40 const void *pt, void *ct, const 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(const void *key, const int keylen, void *iv,
60 const void *ct, void *pt, const 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(const void *pt, const int tlen,
80 void *tag, int *taglen)
85 unsigned int dlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
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);
92 memcpy(tag, gcry_md_read(hd, GCRY_MD_SHA1), dlen);
98 static unsigned long gnu_hmac(const void *key, const int keylen,
99 const void *pt, const int tlen,
100 void *tag, int *taglen)
105 unsigned int dlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
107 return (unsigned long)gcry_error_from_errno(ENOMEM);
108 if ((err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_SECURE |
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);
115 memcpy(tag, gcry_md_read(hd, GCRY_MD_SHA1), dlen);
121 static const char *gnu_errstr(const unsigned long err)
123 return gcry_strerror((gcry_error_t)err);
126 struct crypto_interface gnu_crypto_if = {
128 .encrypt = gnu_encrypt,
129 .decrypt = gnu_decrypt,
132 .errstr = gnu_errstr,