1 #include <openssl/err.h>
2 #include <openssl/sha.h>
3 #include <openssl/evp.h>
4 #include <openssl/hmac.h>
8 static unsigned long ossl_encrypt(void *key, int keylen, void *iv,
9 void *pt, void *ct, int tlen)
13 unsigned char hkey[16];
15 if (EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(),
16 NULL, key, keylen, 5, hkey, NULL) != 16) return 1UL;
17 if (!EVP_EncryptInit(&ctx, EVP_aes_128_cbc(), hkey, iv))
18 return ERR_get_error();
19 if (!EVP_EncryptUpdate(&ctx, ct, &outlen1, pt, tlen))
20 return ERR_get_error();
21 if (!EVP_EncryptFinal(&ctx, ct + outlen1, &outlen2))
22 return ERR_get_error();
23 if (outlen1 + outlen2 != tlen) return 1UL;
27 static unsigned long ossl_decrypt(void *key, int keylen, void *iv,
28 void *ct, void *pt, int tlen)
32 unsigned char hkey[16];
34 if (EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(),
35 NULL, key, keylen, 5, hkey, NULL) != 16) return 1UL;
36 if (!EVP_DecryptInit(&ctx, EVP_aes_128_cbc(), hkey, iv))
37 return ERR_get_error();
38 if (!EVP_DecryptUpdate(&ctx, ct, &outlen1, pt, tlen))
39 return ERR_get_error();
40 if (!EVP_DecryptFinal(&ctx, ct + outlen1, &outlen2))
41 return ERR_get_error();
42 if (outlen1 + outlen2 != tlen) return 1UL;
46 static unsigned long ossl_hash(void *pt, int tlen, void *tag, int *taglen)
50 if (!SHA1_Init(&sctx)) return ERR_get_error();
51 if (!SHA1_Update(&sctx, pt, tlen)) return ERR_get_error();
52 if (!SHA1_Final(tag, &sctx)) return ERR_get_error();
57 static unsigned long ossl_hmac(void *pt, int tlen, void *key, int keylen,
58 void *tag, int *taglen)
63 if (!HMAC_Init(&hctx, key, keylen, EVP_sha1())) return ERR_get_error();
64 if (!HMAC_Update(&hctx, pt, tlen)) return ERR_get_error();
65 if (!HMAC_Final(&hctx, tag, (unsigned int *)taglen))
66 return ERR_get_error();
67 HMAC_CTX_cleanup(&hctx);
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,