8 static const char *gnu_init(void)
10 (void)gcry_check_version(GCRYPT_VERSION);
11 gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
12 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
16 static unsigned long gnu_encrypt(void *key, int keylen, void *iv,
17 void *pt, void *ct, int tlen)
22 if ((err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128,
23 GCRY_CIPHER_MODE_CBC, 0)))
24 return (unsigned long)err;
25 if ((err = gcry_cipher_setkey(hd, key, keylen)))
26 return (unsigned long)err;
27 if ((err = gcry_cipher_setiv(hd, iv, keylen)))
28 return (unsigned long)err;
29 if ((err = gcry_cipher_encrypt(hd, ct, tlen, pt, tlen)))
30 return (unsigned long)err;
31 if ((err = gcry_cipher_reset(hd)))
32 return (unsigned long)err;
36 static unsigned long gnu_decrypt(void *key, int keylen, void *iv,
37 void *ct, void *pt, int tlen)
42 if ((err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128,
43 GCRY_CIPHER_MODE_CBC, 0)))
44 return (unsigned long)err;
45 if ((err = gcry_cipher_setkey(hd, key, keylen)))
46 return (unsigned long)err;
47 if ((err = gcry_cipher_setiv(hd, iv, keylen)))
48 return (unsigned long)err;
49 if ((err = gcry_cipher_decrypt(hd, pt, tlen, ct, tlen)))
50 return (unsigned long)err;
51 if ((err = gcry_cipher_reset(hd)))
52 return (unsigned long)err;
56 static unsigned long gnu_hash(void *pt, int tlen, void *tag, int *taglen)
61 unsigned int dlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
63 return (unsigned long)gcry_error_from_errno(ENOMEM);
64 if ((err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_SECURE)))
65 return (unsigned long)err;
66 gcry_md_write(hd, pt, tlen);
68 memcpy(tag, gcry_md_read(hd, GCRY_MD_SHA1), dlen);
74 static unsigned long gnu_hmac(void *key, int keylen, void *pt, int tlen,
75 void *tag, int *taglen)
80 unsigned int dlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
82 return (unsigned long)gcry_error_from_errno(ENOMEM);
83 if ((err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_SECURE |
85 return (unsigned long)err;
86 if ((err = gcry_md_setkey(hd, key, keylen)))
87 return (unsigned long)err;
88 gcry_md_write(hd, pt, tlen);
90 memcpy(tag, gcry_md_read(hd, GCRY_MD_SHA1), dlen);
96 static const char *gnu_errstr(unsigned long err)
98 return gcry_strerror((gcry_error_t)err);
101 struct crypto_interface gnu_crypto_if = {
103 .encrypt = gnu_encrypt,
104 .decrypt = gnu_decrypt,
107 .errstr = gnu_errstr,