Skip to content
Commits on Source (2)
......@@ -12,13 +12,23 @@
* Check /proc/cpuinfo flags for "aes" to see if you have it.
*/
/* This may not be high enough.
* 0x10000003 1.0.0b fails
* 0x1000105fL 1.0.1e works.
*/
#define CMAC_VERSION_CUTOFF 0x10000003
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <openssl/opensslv.h>
#include <openssl/err.h>
#if OPENSSL_VERSION_NUMBER > CMAC_VERSION_CUTOFF
#include <openssl/cmac.h>
#include <openssl/hmac.h>
#endif
#include <openssl/evp.h>
#include <openssl/md5.h>
#include <openssl/rand.h>
......@@ -50,14 +60,20 @@ int NUM = 1000000;
#define MAX_KEY_LENGTH 64
EVP_MD_CTX *ctx;
#if OPENSSL_VERSION_NUMBER > CMAC_VERSION_CUTOFF
CMAC_CTX *cmac;
HMAC_CTX *hmac;
#endif
static void ssl_init(void)
{
ERR_load_crypto_strings();
OpenSSL_add_all_digests();
ctx = EVP_MD_CTX_new();
#if OPENSSL_VERSION_NUMBER > CMAC_VERSION_CUTOFF
cmac = CMAC_CTX_new();
hmac = HMAC_CTX_new();
#endif
}
static unsigned int SSL_Digest(
......@@ -96,6 +112,7 @@ static unsigned int SSL_DigestSlow(
return len;
}
#if OPENSSL_VERSION_NUMBER > CMAC_VERSION_CUTOFF
static size_t SSL_CMAC(
const EVP_CIPHER *cipher, /* cipher algorithm */
uint8_t *key, /* key pointer */
......@@ -105,13 +122,28 @@ static size_t SSL_CMAC(
) {
unsigned char answer[EVP_MAX_MD_SIZE];
size_t len;
CMAC_resume(cmac);
CMAC_Init(cmac, key, keylength, cipher, NULL);
CMAC_Update(cmac, pkt, pktlength);
CMAC_Final(cmac, answer, &len);
return len;
}
static size_t SSL_HMAC(
const EVP_MD *digest, /* digest algorithm */
uint8_t *key, /* key pointer */
int keylength, /* key size */
uint8_t *pkt, /* packet pointer */
int pktlength /* packet length */
) {
unsigned char answer[EVP_MAX_MD_SIZE];
unsigned int len;
HMAC_Init_ex(hmac, key, keylength, digest, NULL);
HMAC_Update(hmac, pkt, pktlength);
HMAC_Final(hmac, answer, &len);
return len;
}
#endif
static void DoDigest(
const char *name, /* type of digest */
uint8_t *key, /* key pointer */
......@@ -151,6 +183,7 @@ static void DoDigest(
printf("\n");
}
#if OPENSSL_VERSION_NUMBER > CMAC_VERSION_CUTOFF
static void DoCMAC(
const char *name, /* name of cipher */
const EVP_CIPHER *cipher,
......@@ -173,12 +206,37 @@ static void DoCMAC(
}
clock_gettime(CLOCK_MONOTONIC, &stop);
fast = (stop.tv_sec-start.tv_sec)*1E9 + (stop.tv_nsec-start.tv_nsec);
printf("%10s %2d %2d %2lu %6.0f %6.3f",
printf("%10s %2d %2d %2lu %6.0f %6.3f\n",
name, keylength, pktlength, digestlength, fast/NUM, fast/1E9);
printf("\n");
}
static void DoHMAC(
const char *name, /* name of cipher */
uint8_t *key, /* key pointer */
int keylength, /* key length */
uint8_t *pkt, /* packet pointer */
int pktlength /* packet length */
)
{
int type = OBJ_sn2nid(name);
const EVP_MD *digest = EVP_get_digestbynid(type);
struct timespec start, stop;
int i;
double fast;
unsigned long digestlength = 0;
if (NULL == digest) return;
clock_gettime(CLOCK_MONOTONIC, &start);
for (i = 0; i < NUM; i++) {
digestlength = SSL_HMAC(digest, key, keylength, pkt, pktlength);
}
clock_gettime(CLOCK_MONOTONIC, &stop);
fast = (stop.tv_sec-start.tv_sec)*1E9 + (stop.tv_nsec-start.tv_nsec);
printf("%10s %2d %2d %2lu %6.0f %6.3f\n",
name, keylength, pktlength, digestlength, fast/NUM, fast/1E9);
}
#endif
int main(int argc, char *argv[])
......@@ -217,6 +275,7 @@ int main(int argc, char *argv[])
DoDigest("RIPEMD160", key, 20, packet, PACKET_LENGTH);
DoDigest("RIPEMD160", key, 32, packet, PACKET_LENGTH);
#if OPENSSL_VERSION_NUMBER > CMAC_VERSION_CUTOFF
printf("\n");
printf("# KL=key length, PL=packet length, CL=CMAC length\n");
printf("# CMAC KL PL CL ns/op sec/run\n");
......@@ -229,6 +288,18 @@ int main(int argc, char *argv[])
DoCMAC("CAM-192", EVP_camellia_192_cbc(), key, 24, packet, PACKET_LENGTH);
DoCMAC("CAM-256", EVP_camellia_256_cbc(), key, 32, packet, PACKET_LENGTH);
printf("\n");
printf("# KL=key length, PL=packet length, CL=HMAC length\n");
printf("# HMAC KL PL CL ns/op sec/run\n");
DoHMAC("MD5", key, 8, packet, PACKET_LENGTH);
DoHMAC("SHA1", key, 16, packet, PACKET_LENGTH);
DoHMAC("SHA256", key, 16, packet, PACKET_LENGTH);
DoHMAC("SHA256", key, 20, packet, PACKET_LENGTH);
DoHMAC("SHA512", key, 16, packet, PACKET_LENGTH);
DoHMAC("SHA512", key, 32, packet, PACKET_LENGTH);
#endif
return 0;
}