From 4b08e60b009c56c595f862ce382a4b11f264170c Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Wed, 16 Jun 2010 10:38:14 +0200 Subject: Added test functionality for HMAC and hashes (SHA-1, MD5) --- examples/new.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 174 insertions(+), 2 deletions(-) diff --git a/examples/new.c b/examples/new.c index 6709bb1..2bbe675 100644 --- a/examples/new.c +++ b/examples/new.c @@ -599,7 +599,7 @@ struct aes_vectors_st { }, }; -/* Key wrapping */ +/* AES cipher */ static int test_ncr_aes(int cfd) { @@ -799,7 +799,7 @@ test_ncr_aes(int cfd) for(j=0;j<16;j++) fprintf(stderr, "%.2x:", (int)aes_vectors[i].plaintext[j]); fprintf(stderr, "\n"); -// return 1; + return 1; } } @@ -810,6 +810,175 @@ test_ncr_aes(int cfd) } +struct hash_vectors_st { + ncr_algorithm_t algorithm; + const uint8_t* key; /* if hmac */ + int key_size; + const uint8_t* plaintext; + int plaintext_size; + const uint8_t* output; + int output_size; + ncr_crypto_op_t op; +} hash_vectors[] = { + { + .algorithm = NCR_ALG_SHA1, + .key = NULL, + .plaintext = "what do ya want for nothing?", + .plaintext_size = sizeof("what do ya want for nothing?")-1, + .output = "\x8f\x82\x03\x94\xf9\x53\x35\x18\x20\x45\xda\x24\xf3\x4d\xe5\x2b\xf8\xbc\x34\x32", + .output_size = 20, + .op = NCR_OP_DIGEST, + }, + { + .algorithm = NCR_ALG_HMAC_MD5, + .key = "Jefe", + .key_size = 4, + .plaintext = "what do ya want for nothing?", + .plaintext_size = sizeof("what do ya want for nothing?")-1, + .output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38", + .output_size = 16, + .op = NCR_OP_MAC, + }, +}; + + +/* SHA1 and other hashes */ +static int +test_ncr_hash(int cfd) +{ + struct ncr_data_init_st dinit; + struct ncr_key_generate_st kgen; + ncr_key_t key; + struct ncr_key_data_st keydata; + struct ncr_data_st kdata; + ncr_data_t dd, dd2; + uint8_t data[KEY_DATA_SIZE]; + int i, j; + struct ncr_session_once_op_st nop; + + dinit.max_object_size = KEY_DATA_SIZE; + dinit.flags = NCR_DATA_FLAG_EXPORTABLE; + dinit.initial_data = NULL; + dinit.initial_data_size = 0; + + if (ioctl(cfd, NCRIO_DATA_INIT, &dinit)) { + fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__); + perror("ioctl(NCRIO_DATA_INIT)"); + return 1; + } + + dd = dinit.desc; + + if (ioctl(cfd, NCRIO_DATA_INIT, &dinit)) { + fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__); + perror("ioctl(NCRIO_DATA_INIT)"); + return 1; + } + + dd2 = dinit.desc; + + /* convert it to key */ + if (ioctl(cfd, NCRIO_KEY_INIT, &key)) { + perror("ioctl(NCRIO_KEY_INIT)"); + return 1; + } + + keydata.key_id[0] = 'a'; + keydata.key_id[2] = 'b'; + keydata.key_id_size = 2; + keydata.type = NCR_KEY_TYPE_SECRET; + keydata.algorithm = NCR_ALG_AES_CBC; + keydata.flags = NCR_KEY_FLAG_EXPORTABLE; + + + fprintf(stdout, "Tests on AES Encryption\n"); + for (i=0;i