From 7d7b51f744eed028f8481d091c55a068a6a2845a Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Fri, 4 Dec 2009 01:38:29 +0200 Subject: Improved HMAC examples. --- example-hmac.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ example2.c | 212 -------------------------------------------------------- 2 files changed, 215 insertions(+), 212 deletions(-) create mode 100644 example-hmac.c delete mode 100644 example2.c diff --git a/example-hmac.c b/example-hmac.c new file mode 100644 index 0000000..7dc2b4c --- /dev/null +++ b/example-hmac.c @@ -0,0 +1,215 @@ +/* + * Demo on how to use OpenBSD /dev/crypto device. + * + * Author: Michal Ludvig + * http://www.logix.cz/michal + * + * Note: by default OpenBSD doesn't allow using + * /dev/crypto if there is no hardware accelerator + * for a given algorithm. To change this you'll have to + * set cryptodevallowsoft=1 in + * /usr/src/sys/crypto/cryptodev.c and rebuild your kernel. + */ +#include +#include +#include +#include + +#include +//#include +#include "cryptodev.h" + +#define DATA_SIZE 4096 +#define BLOCK_SIZE 16 +#define KEY_SIZE 16 + +static int +test_crypto(int cfd) +{ + struct { + uint8_t in[DATA_SIZE], + encrypted[DATA_SIZE], + decrypted[DATA_SIZE], + iv[BLOCK_SIZE], + key[KEY_SIZE]; + } data; + struct session_op sess; + struct crypt_op cryp; + uint8_t mac[HASH_MAX_LEN]; + uint8_t oldmac[HASH_MAX_LEN]; + uint8_t md5_hmac_out[] = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38"; + uint8_t sha1_out[] = "\x8f\x82\x03\x94\xf9\x53\x35\x18\x20\x45\xda\x24\xf3\x4d\xe5\x2b\xf8\xbc\x34\x32"; + int i; + + memset(&sess, 0, sizeof(sess)); + memset(&cryp, 0, sizeof(cryp)); + + /* Use the garbage that is on the stack :-) */ + /* memset(&data, 0, sizeof(data)); */ + + /* SHA1 plain test */ + memset(mac, 0, sizeof(mac)); + + sess.cipher = 0; + sess.mac = CRYPTO_SHA1; + if (ioctl(cfd, CIOCGSESSION, &sess)) { + perror("ioctl(CIOCGSESSION)"); + return 1; + } + + cryp.ses = sess.ses; + cryp.len = sizeof("what do ya want for nothing?")-1; + cryp.src = "what do ya want for nothing?"; + cryp.mac = mac; + cryp.op = COP_ENCRYPT; + if (ioctl(cfd, CIOCCRYPT, &cryp)) { + perror("ioctl(CIOCCRYPT)"); + return 1; + } + + if (memcmp(mac, sha1_out, 20)!=0) { + printf("mac: "); + for (i=0;i - * http://www.logix.cz/michal - * - * Note: by default OpenBSD doesn't allow using - * /dev/crypto if there is no hardware accelerator - * for a given algorithm. To change this you'll have to - * set cryptodevallowsoft=1 in - * /usr/src/sys/crypto/cryptodev.c and rebuild your kernel. - */ -#include -#include -#include -#include - -#include -//#include -#include "cryptodev.h" - -#define DATA_SIZE 4096 -#define BLOCK_SIZE 16 -#define KEY_SIZE 16 - -static int -test_crypto(int cfd) -{ - struct { - uint8_t in[DATA_SIZE], - encrypted[DATA_SIZE], - decrypted[DATA_SIZE], - iv[BLOCK_SIZE], - key[KEY_SIZE]; - } data; - struct session_op sess; - struct crypt_op cryp; - uint8_t mac[HASH_MAX_LEN]; - uint8_t oldmac[HASH_MAX_LEN]; - uint8_t def_out[] = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38"; - uint8_t def_out_hash[] = "\x8f\x82\x03\x94\xf9\x53\x35\x18\x20\x45\xda\x24\xf3\x4d\xe5\x2b\xf8\xbc\x34\x32"; - int i; - - memset(&sess, 0, sizeof(sess)); - memset(&cryp, 0, sizeof(cryp)); - - /* Use the garbage that is on the stack :-) */ - /* memset(&data, 0, sizeof(data)); */ - - memset(mac, 0, sizeof(mac)); - - sess.cipher = 0; - sess.mac = CRYPTO_SHA1; - if (ioctl(cfd, CIOCGSESSION, &sess)) { - perror("ioctl(CIOCGSESSION)"); - return 1; - } - - cryp.ses = sess.ses; - cryp.len = sizeof("what do ya want for nothing?")-1; - cryp.src = "what do ya want for nothing?"; - cryp.mac = mac; - cryp.op = COP_ENCRYPT; - if (ioctl(cfd, CIOCCRYPT, &cryp)) { - perror("ioctl(CIOCCRYPT)"); - return 1; - } - - if (memcmp(mac, def_out_hash, 20)!=0) { - printf("mac: "); - for (i=0;i