4 #include <openssl/err.h>
5 #include <openssl/aes.h>
6 #include <openssl/sha.h>
7 #include <openssl/hmac.h>
11 static const char *ossl_init(void)
13 ERR_load_crypto_strings();
17 static unsigned long ossl_encrypt(void *key, int keylen, void *iv,
18 void *pt, void *ct, int tlen)
22 if (AES_set_encrypt_key(key, keylen*8, &akey))
23 return ERR_get_error();
24 AES_cbc_encrypt(pt, ct, tlen, &akey, iv, AES_ENCRYPT);
28 static unsigned long ossl_decrypt(void *key, int keylen, void *iv,
29 void *ct, void *pt, int tlen)
33 if (AES_set_decrypt_key(key, keylen*8, &akey))
34 return ERR_get_error();
35 AES_cbc_encrypt(ct, pt, tlen, &akey, iv, AES_DECRYPT);
39 static unsigned long ossl_hash(void *pt, int tlen, void *tag, int *taglen)
43 if (!SHA1_Init(&sctx)) return ERR_get_error();
44 if (!SHA1_Update(&sctx, pt, tlen)) return ERR_get_error();
45 if (!SHA1_Final(tag, &sctx)) return ERR_get_error();
46 *taglen = SHA_DIGEST_LENGTH;
50 static unsigned long ossl_hmac(void *key, int keylen, void *pt, int tlen,
51 void *tag, int *taglen)
57 if (!HMAC_Init_ex(&hctx, key, keylen, EVP_sha1(), NULL))
58 return ERR_get_error();
59 if (!HMAC_Update(&hctx, pt, tlen)) return ERR_get_error();
60 if (!HMAC_Final(&hctx, tag, (unsigned int *)taglen))
61 return ERR_get_error();
62 HMAC_CTX_cleanup(&hctx);
64 if (HMAC(EVP_sha1(), key, keylen, pt, tlen,
65 tag, (unsigned int *)taglen) != tag)
66 return ERR_get_error();
71 static const char *ossl_errstr(unsigned long err)
73 return ERR_error_string(err, NULL);
76 struct crypto_interface ossl_crypto_if = {
78 .encrypt = ossl_encrypt,
79 .decrypt = ossl_decrypt,
82 .errstr = ossl_errstr,