4 #include <openssl/err.h>
5 #include <openssl/sha.h>
6 #include <openssl/evp.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 unsigned char hkey[16];
24 if (EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(),
25 NULL, key, keylen, 5, hkey, NULL) != 16) return 1UL;
26 if (!EVP_EncryptInit(&ctx, EVP_aes_128_cbc(), hkey, iv))
27 return ERR_get_error();
28 if (!EVP_EncryptUpdate(&ctx, ct, &outlen1, pt, tlen))
29 return ERR_get_error();
30 if (!EVP_EncryptFinal(&ctx, ct + outlen1, &outlen2))
31 return ERR_get_error();
32 if (outlen1 + outlen2 != tlen) {
33 printf("enc tlen =%d outlen1=%d outlen2=%d\n",
34 tlen, outlen1, outlen2);
40 static unsigned long ossl_decrypt(void *key, int keylen, void *iv,
41 void *ct, void *pt, int tlen)
45 unsigned char hkey[16];
47 if (EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(),
48 NULL, key, keylen, 5, hkey, NULL) != 16) return 1UL;
49 if (!EVP_DecryptInit(&ctx, EVP_aes_128_cbc(), hkey, iv))
50 return ERR_get_error();
51 if (!EVP_DecryptUpdate(&ctx, ct, &outlen1, pt, tlen))
52 return ERR_get_error();
53 if (!EVP_DecryptFinal(&ctx, ct + outlen1, &outlen2))
54 return ERR_get_error();
55 if (outlen1 + outlen2 != tlen) {
56 printf("dec tlen =%d outlen1=%d outlen2=%d\n",
57 tlen, outlen1, outlen2);
63 static unsigned long ossl_hash(void *pt, int tlen, void *tag, int *taglen)
67 if (!SHA1_Init(&sctx)) return ERR_get_error();
68 if (!SHA1_Update(&sctx, pt, tlen)) return ERR_get_error();
69 if (!SHA1_Final(tag, &sctx)) return ERR_get_error();
74 static unsigned long ossl_hmac(void *pt, int tlen, void *key, int keylen,
75 void *tag, int *taglen)
77 if (!HMAC(EVP_sha1(), key, keylen, pt, tlen,
78 tag, (unsigned int *)taglen))
79 return ERR_get_error();
83 static const char *ossl_errstr(unsigned long err)
85 return ERR_error_string(err, NULL);
88 struct crypto_interface ossl_crypto_if = {
90 .encrypt = ossl_encrypt,
91 .decrypt = ossl_decrypt,
94 .errstr = ossl_errstr,