return 0;
}
-const char *if_name(int ifno)
+const char *crypto_init(int ifno)
{
- if (ifno < 0 || ifno > MAX_IF) return "invalid index";
- return ifs[ifno]->name;
+ if (ifno < 0 || ifno > MAX_IF) return (const char *)0;
+ return ifs[ifno]->init();
}
static unsigned char iv[16] = {0};
#define _CRYPTO_H
int select_crypto_if(int ifno);
-const char *if_name(int ifno);
+const char *crypto_init(int ifno);
unsigned long encrypt(void *key, int keylen, void *pt, void *ct, int tlen);
unsigned long decrypt(void *key, int keylen, void *ct, void *pt, int tlen);
unsigned long hash(void *pt, int tlen, void *tag, int *taglen);
#define _CRYPTO_IF_H
struct crypto_interface {
- char *name;
+ const char *(*init)(void);
unsigned long (*encrypt)(void *key, int keylen, void *iv,
void *pt, void *ct, int tlen);
unsigned long (*decrypt)(void *key, int keylen, void *iv,
#include "crypto_if.h"
+static const char *ossl_init(void)
+{
+ ERR_load_crypto_strings();
+ return "openssl";
+}
+
static unsigned long ossl_encrypt(void *key, int keylen, void *iv,
void *pt, void *ct, int tlen)
{
int outlen1, outlen2;
unsigned char hkey[16];
- ERR_load_crypto_strings(); /* FIXME */
-
if (EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(),
NULL, key, keylen, 5, hkey, NULL) != 16) return 1UL;
if (!EVP_EncryptInit(&ctx, EVP_aes_128_cbc(), hkey, iv))
static unsigned long ossl_hmac(void *pt, int tlen, void *key, int keylen,
void *tag, int *taglen)
{
- HMAC_CTX hctx;
-
- HMAC_CTX_init(&hctx);
- if (!HMAC_Init_ex(&hctx, key, keylen, EVP_sha1(), NULL)) return ERR_get_error();
- if (!HMAC_Update(&hctx, pt, tlen)) return ERR_get_error();
- if (!HMAC_Final(&hctx, tag, (unsigned int *)taglen))
- return ERR_get_error();
- HMAC_CTX_cleanup(&hctx);
+ if (!HMAC(EVP_sha1(), key, keylen, pt, tlen,
+ tag, (unsigned int *)taglen))
+ return ERR_get_error();
return 0UL;
}
}
struct crypto_interface ossl_crypto_if = {
- .name = "openssl",
+ .init = ossl_init,
.encrypt = ossl_encrypt,
.decrypt = ossl_decrypt,
.hash = ossl_hash,
printh(pt);
printh(key);
if (select_crypto_if(iface1)) return 1;
+ memset(ct, 0xfe, sizeof(ct));
if ((err = encrypt(key, sizeof(key), pt, ct, sizeof(pt)))) {
printf("encrypt error: %s\n", crypto_errstr(err));
return 1;
}
printh(ct);
if (select_crypto_if(iface2)) return 1;
- if ((err = decrypt(key, sizeof(key), ct, re, sizeof(ct)))) {
+ if ((err = decrypt(key, sizeof(key), ct, re, sizeof(re)))) {
printf("decrypt error: %s\n", crypto_errstr(err));
return 1;
}
{
int rc, maxrc = 0;
int numifs, i, j;
+ const char *name;
- for (numifs = 0; select_crypto_if(numifs) == 0; numifs++)
- printf("%d: %s\n", numifs, if_name(numifs));
- printf("Testing %d interfaces\n", numifs);
+ for (numifs = 0; (name = crypto_init(numifs)); numifs++)
+ printf("%d: %s\n", numifs, name);
+ printf("Testing %d interfaces\n\n", numifs);
for (i = 0; i < numifs; i++)
if ((rc = test_sha(i)) > maxrc) maxrc = rc;
#include "crypto_if.h"
+static const char *tom_init(void)
+{
+ /* nothing to initialize */
+ return "tomcrypt";
+}
+
static unsigned long tom_encrypt(void *key, int keylen, void *iv,
void *pt, void *ct, int tlen)
{
}
struct crypto_interface tom_crypto_if = {
- .name = "tomcrypt",
+ .init = tom_init,
.encrypt = tom_encrypt,
.decrypt = tom_decrypt,
.hash = tom_hash,