summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile12
-rw-r--r--examples/cipher.c230
-rw-r--r--examples/hmac.c210
-rw-r--r--examples/ncr.c1255
-rw-r--r--examples/pk.c1129
-rw-r--r--examples/speed.c183
6 files changed, 1956 insertions, 1063 deletions
diff --git a/examples/Makefile b/examples/Makefile
index 100cc490600..9911100263e 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -1,19 +1,13 @@
CC = gcc
CFLAGS = -Wall -g -O2
-progs := cipher hmac ncr pk speed
+progs := ncr pk speed
all: $(progs)
-cipher: cipher.c
- $(CC) $(CFLAGS) $< -o $@
-
speed: speed.c
$(CC) $(CFLAGS) $< -o $@
-hmac: hmac.c
- $(CC) $(CFLAGS) $< -o $@
-
ncr: ncr.c
$(CC) $(CFLAGS) $< -o $@
@@ -23,9 +17,7 @@ pk: pk.c
check: $(progs)
./ncr
./pk
- ./cipher
- ./hmac
./speed
clean:
- rm -f *.o *~ hmac cipher ncr pk speed \ No newline at end of file
+ rm -f *.o *~ $(progs)
diff --git a/examples/cipher.c b/examples/cipher.c
deleted file mode 100644
index 52b4996f4a6..00000000000
--- a/examples/cipher.c
+++ /dev/null
@@ -1,230 +0,0 @@
-/*
- * Demo on how to use /dev/crypto device for ciphering.
- *
- * Placed under public domain.
- *
- */
-#include <stdint.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <sys/ioctl.h>
-#include "../cryptodev.h"
-
-#define DATA_SIZE 4096
-#define BLOCK_SIZE 16
-#define KEY_SIZE 16
-
-static int
-test_crypto(int cfd)
-{
- uint8_t plaintext[DATA_SIZE];
- uint8_t ciphertext[DATA_SIZE];
- uint8_t iv[BLOCK_SIZE];
- uint8_t key[KEY_SIZE];
-
- struct session_op sess;
- struct crypt_op cryp;
-
- memset(&sess, 0, sizeof(sess));
- memset(&cryp, 0, sizeof(cryp));
-
- memset(plaintext, 0x15, sizeof(plaintext));
- memset(key, 0x33, sizeof(key));
- memset(iv, 0x03, sizeof(iv));
-
- /* Get crypto session for AES128 */
- sess.cipher = CRYPTO_AES_CBC;
- sess.keylen = KEY_SIZE;
- sess.key = key;
- if (ioctl(cfd, CIOCGSESSION, &sess)) {
- perror("ioctl(CIOCGSESSION)");
- return 1;
- }
-
- /* Encrypt data.in to data.encrypted */
- cryp.ses = sess.ses;
- cryp.len = sizeof(plaintext);
- cryp.src = plaintext;
- cryp.dst = ciphertext;
- cryp.iv = iv;
- cryp.op = COP_ENCRYPT;
- if (ioctl(cfd, CIOCCRYPT, &cryp)) {
- perror("ioctl(CIOCCRYPT)");
- return 1;
- }
-
- if (ioctl(cfd, CIOCGSESSION, &sess)) {
- perror("ioctl(CIOCGSESSION)");
- return 1;
- }
-
- /* Decrypt data.encrypted to data.decrypted */
- cryp.ses = sess.ses;
- cryp.len = sizeof(plaintext);
- cryp.src = ciphertext;
- cryp.dst = ciphertext;
- cryp.iv = iv;
- cryp.op = COP_DECRYPT;
- if (ioctl(cfd, CIOCCRYPT, &cryp)) {
- perror("ioctl(CIOCCRYPT)");
- return 1;
- }
-
- /* Verify the result */
- if (memcmp(plaintext, ciphertext, sizeof(plaintext)) != 0) {
- fprintf(stderr,
- "FAIL: Decrypted data are different from the input data.\n");
- return 1;
- } else
- printf("Test passed\n");
-
- /* Finish crypto session */
- if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
- perror("ioctl(CIOCFSESSION)");
- return 1;
- }
-
- return 0;
-}
-
-static int test_aes(int cfd)
-{
- uint8_t plaintext1[BLOCK_SIZE];
- uint8_t ciphertext1[BLOCK_SIZE] = { 0xdf, 0x55, 0x6a, 0x33, 0x43, 0x8d, 0xb8, 0x7b, 0xc4, 0x1b, 0x17, 0x52, 0xc5, 0x5e, 0x5e, 0x49 };
- uint8_t iv1[BLOCK_SIZE];
- uint8_t key1[KEY_SIZE] = { 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- uint8_t plaintext2[BLOCK_SIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 };
- uint8_t ciphertext2[BLOCK_SIZE] = { 0xb7, 0x97, 0x2b, 0x39, 0x41, 0xc4, 0x4b, 0x90, 0xaf, 0xa7, 0xb2, 0x64, 0xbf, 0xba, 0x73, 0x87 };
- uint8_t iv2[BLOCK_SIZE];
- uint8_t key2[KEY_SIZE];
-
- struct session_op sess;
- struct crypt_op cryp;
-
- memset(&sess, 0, sizeof(sess));
- memset(&cryp, 0, sizeof(cryp));
-
- memset(plaintext1, 0x0, sizeof(plaintext1));
- memset(iv1, 0x0, sizeof(iv1));
-
- /* Get crypto session for AES128 */
- sess.cipher = CRYPTO_AES_CBC;
- sess.keylen = KEY_SIZE;
- sess.key = key1;
- if (ioctl(cfd, CIOCGSESSION, &sess)) {
- perror("ioctl(CIOCGSESSION)");
- return 1;
- }
-
- /* Encrypt data.in to data.encrypted */
- cryp.ses = sess.ses;
- cryp.len = sizeof(plaintext1);
- cryp.src = plaintext1;
- cryp.dst = plaintext1;
- cryp.iv = iv1;
- cryp.op = COP_ENCRYPT;
- if (ioctl(cfd, CIOCCRYPT, &cryp)) {
- perror("ioctl(CIOCCRYPT)");
- return 1;
- }
-
- /* Verify the result */
- if (memcmp(plaintext1, ciphertext1, sizeof(plaintext1)) != 0) {
- fprintf(stderr,
- "FAIL: Decrypted data are different from the input data.\n");
- return 1;
- }
-
- /* Test 2 */
-
- memset(key2, 0x0, sizeof(key2));
- memset(iv2, 0x0, sizeof(iv2));
-
- /* Get crypto session for AES128 */
- sess.cipher = CRYPTO_AES_CBC;
- sess.keylen = KEY_SIZE;
- sess.key = key2;
- if (ioctl(cfd, CIOCGSESSION, &sess)) {
- perror("ioctl(CIOCGSESSION)");
- return 1;
- }
-
- /* Encrypt data.in to data.encrypted */
- cryp.ses = sess.ses;
- cryp.len = sizeof(plaintext2);
- cryp.src = plaintext2;
- cryp.dst = plaintext2;
- cryp.iv = iv2;
- cryp.op = COP_ENCRYPT;
- if (ioctl(cfd, CIOCCRYPT, &cryp)) {
- perror("ioctl(CIOCCRYPT)");
- return 1;
- }
-
- /* Verify the result */
- if (memcmp(plaintext2, ciphertext2, sizeof(plaintext2)) != 0) {
- fprintf(stderr,
- "FAIL: Decrypted data are different from the input data.\n");
- return 1;
- }
-
- printf("AES Test passed\n");
-
- /* Finish crypto session */
- if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
- perror("ioctl(CIOCFSESSION)");
- return 1;
- }
-
- return 0;
-}
-
-int
-main()
-{
- int fd = -1, cfd = -1;
-
- /* Open the crypto device */
- fd = open("/dev/crypto", O_RDWR, 0);
- if (fd < 0) {
- perror("open(/dev/crypto)");
- return 1;
- }
-
- /* Clone file descriptor */
- if (ioctl(fd, CRIOGET, &cfd)) {
- perror("ioctl(CRIOGET)");
- return 1;
- }
-
- /* Set close-on-exec (not really neede here) */
- if (fcntl(cfd, F_SETFD, 1) == -1) {
- perror("fcntl(F_SETFD)");
- return 1;
- }
-
- /* Run the test itself */
- if (test_aes(cfd))
- return 1;
-
- if (test_crypto(cfd))
- return 1;
-
- /* Close cloned descriptor */
- if (close(cfd)) {
- perror("close(cfd)");
- return 1;
- }
-
- /* Close the original descriptor */
- if (close(fd)) {
- perror("close(fd)");
- return 1;
- }
-
- return 0;
-}
-
diff --git a/examples/hmac.c b/examples/hmac.c
deleted file mode 100644
index c54d7419a34..00000000000
--- a/examples/hmac.c
+++ /dev/null
@@ -1,210 +0,0 @@
-/*
- * Demo on how to use /dev/crypto device for HMAC.
- *
- * Placed under public domain.
- *
- */
-#include <stdint.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <sys/ioctl.h>
-#include "../cryptodev.h"
-
-#define DATA_SIZE 4096
-#define BLOCK_SIZE 16
-#define KEY_SIZE 16
-#define SHA1_HASH_LEN 20
-
-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[AALG_MAX_RESULT_LEN];
- uint8_t oldmac[AALG_MAX_RESULT_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<SHA1_HASH_LEN;i++) {
- printf("%.2x", (uint8_t)mac[i]);
- }
- puts("\n");
- fprintf(stderr, "HASH test 1: failed\n");
- } else {
- fprintf(stderr, "HASH test 1: passed\n");
- }
-
- /* MD5-HMAC test */
- memset(mac, 0, sizeof(mac));
-
- sess.cipher = 0;
- sess.mackey = (uint8_t*)"Jefe";
- sess.mackeylen = 4;
- sess.mac = CRYPTO_MD5_HMAC;
- 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, md5_hmac_out, 16)!=0) {
- printf("mac: ");
- for (i=0;i<SHA1_HASH_LEN;i++) {
- printf("%.2x", (uint8_t)mac[i]);
- }
- puts("\n");
- fprintf(stderr, "HMAC test 1: failed\n");
- } else {
- fprintf(stderr, "HMAC test 1: passed\n");
- }
-
- /* Hash and encryption in one step test */
- sess.cipher = CRYPTO_AES_CBC;
- sess.mac = CRYPTO_SHA1_HMAC;
- sess.keylen = KEY_SIZE;
- sess.key = data.key;
- sess.mackeylen = 16;
- sess.mackey = (uint8_t*)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
- if (ioctl(cfd, CIOCGSESSION, &sess)) {
- perror("ioctl(CIOCGSESSION)");
- return 1;
- }
-
- /* Encrypt data.in to data.encrypted */
- cryp.ses = sess.ses;
- cryp.len = sizeof(data.in);
- cryp.src = data.in;
- cryp.dst = data.encrypted;
- cryp.iv = data.iv;
- cryp.mac = mac;
- cryp.op = COP_ENCRYPT;
- if (ioctl(cfd, CIOCCRYPT, &cryp)) {
- perror("ioctl(CIOCCRYPT)");
- return 1;
- }
-
- memcpy(oldmac, mac, sizeof(mac));
-
- /* Decrypt data.encrypted to data.decrypted */
- cryp.src = data.encrypted;
- cryp.dst = data.decrypted;
- cryp.op = COP_DECRYPT;
- if (ioctl(cfd, CIOCCRYPT, &cryp)) {
- perror("ioctl(CIOCCRYPT)");
- return 1;
- }
-
- /* Verify the result */
- if (memcmp(data.in, data.decrypted, sizeof(data.in)) != 0) {
- fprintf(stderr,
- "FAIL: Decrypted data are different from the input data.\n");
- return 1;
- } else
- printf("Crypt Test: passed\n");
-
- if (memcmp(mac, oldmac, 20) != 0) {
- fprintf(stderr,
- "FAIL: Hash in decrypted data different than in encrypted.\n");
- return 1;
- } else
- printf("HMAC Test 2: passed\n");
-
- /* Finish crypto session */
- if (ioctl(cfd, CIOCFSESSION, &sess.ses)) {
- perror("ioctl(CIOCFSESSION)");
- return 1;
- }
-
- return 0;
-}
-
-int
-main()
-{
- int fd = -1, cfd = -1;
-
- /* Open the crypto device */
- fd = open("/dev/crypto", O_RDWR, 0);
- if (fd < 0) {
- perror("open(/dev/crypto)");
- return 1;
- }
-
- /* Clone file descriptor */
- if (ioctl(fd, CRIOGET, &cfd)) {
- perror("ioctl(CRIOGET)");
- return 1;
- }
-
- /* Set close-on-exec (not really neede here) */
- if (fcntl(cfd, F_SETFD, 1) == -1) {
- perror("fcntl(F_SETFD)");
- return 1;
- }
-
- /* Run the test itself */
- if (test_crypto(cfd))
- return 1;
-
- /* Close cloned descriptor */
- if (close(cfd)) {
- perror("close(cfd)");
- return 1;
- }
-
- /* Close the original descriptor */
- if (close(fd)) {
- perror("close(fd)");
- return 1;
- }
-
- return 0;
-}
diff --git a/examples/ncr.c b/examples/ncr.c
index 9a75a996e58..9691fea5dc1 100644
--- a/examples/ncr.c
+++ b/examples/ncr.c
@@ -13,11 +13,18 @@
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
#include "../ncr.h"
#include <stdlib.h>
#define DATA_SIZE 4096
+#define ALIGN_NL __attribute__((aligned(NLA_ALIGNTO)))
+
+#define ALG_AES_CBC "cbc(aes)"
+#define ALG_AES_ECB "ecb(aes)"
+
static void randomize_data(uint8_t * data, size_t data_size)
{
int i;
@@ -33,11 +40,42 @@ int i;
static int
test_ncr_key(int cfd)
{
- struct ncr_key_generate_st kgen;
+ struct __attribute__((packed)) {
+ struct ncr_key_generate f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_AES_CBC)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ struct nlattr bits_head ALIGN_NL;
+ uint32_t bits ALIGN_NL;
+ } kgen;
+ struct __attribute__((packed)) {
+ struct ncr_key_get_info f;
+ /* This union is only here to stop gcc from complaining about
+ aliasing. */
+ union {
+ unsigned char __reserve[DATA_SIZE];
+ struct nlattr first_header;
+ } u ALIGN_NL;
+ } kinfo;
+ struct nlattr *nla;
ncr_key_t key;
- struct ncr_key_data_st keydata;
+ struct __attribute__((packed)) {
+ struct ncr_key_import f;
+ struct nlattr id_head ALIGN_NL;
+ uint8_t id[2] ALIGN_NL;
+ struct nlattr type_head ALIGN_NL;
+ uint32_t type ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_AES_CBC)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ } kimport;
+ struct ncr_key_export kexport;
uint8_t data[KEY_DATA_SIZE];
uint8_t data_bak[KEY_DATA_SIZE];
+ uint16_t *attr_p;
+ int got_algo, got_flags, got_type;
fprintf(stdout, "Tests on Keys:\n");
@@ -51,23 +89,32 @@ test_ncr_key(int cfd)
memcpy(data_bak, data, sizeof(data));
/* convert it to key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &key)) {
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
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;
-
- keydata.key = key;
- keydata.idata = data;
- keydata.idata_size = sizeof(data);
-
- if (ioctl(cfd, NCRIO_KEY_IMPORT, &keydata)) {
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.input_size = sizeof(kimport);
+ kimport.f.key = key;
+ kimport.f.data = data;
+ kimport.f.data_size = sizeof(data);
+ kimport.id_head.nla_len = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'a';
+ kimport.id[1] = 'b';
+ kimport.type_head.nla_len = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.algo_head.nla_len = NLA_HDRLEN + sizeof(kimport.algo);
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kimport.algo, ALG_AES_CBC);
+ kimport.flags_head.nla_len = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE;
+
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_IMPORT)");
return 1;
@@ -76,20 +123,14 @@ test_ncr_key(int cfd)
/* now try to read it */
fprintf(stdout, "\tKey export...\n");
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = key;
- keydata.idata = data;
- keydata.idata_size = sizeof(data);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = key;
+ kexport.buffer = data;
+ kexport.buffer_size = sizeof(data);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ if (ioctl(cfd, NCRIO_KEY_EXPORT, &kexport) != sizeof(data)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_IMPORT)");
- return 1;
- }
-
- if (keydata.idata_size != sizeof(data)) {
- fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- fprintf(stderr, "data returned but differ!\n");
+ perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
@@ -112,37 +153,46 @@ test_ncr_key(int cfd)
fprintf(stdout, "\tKey import...\n");
/* convert it to key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &key)) {
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- kgen.desc = key;
- kgen.params.algorithm = NCR_ALG_AES_CBC;
- kgen.params.keyflags = NCR_KEY_FLAG_EXPORTABLE;
- kgen.params.params.secret.bits = 128; /* 16 bytes */
-
+ memset(&kgen.f, 0, sizeof(kgen.f));
+ kgen.f.input_size = sizeof(kgen);
+ kgen.f.key = key;
+ kgen.algo_head.nla_len = NLA_HDRLEN + sizeof(kgen.algo);
+ kgen.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kgen.algo, ALG_AES_CBC);
+ kgen.flags_head.nla_len = NLA_HDRLEN + sizeof(kgen.flags);
+ kgen.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kgen.flags = NCR_KEY_FLAG_EXPORTABLE;
+ kgen.bits_head.nla_len = NLA_HDRLEN + sizeof(kgen.bits);
+ kgen.bits_head.nla_type = NCR_ATTR_SECRET_KEY_BITS;
+ kgen.bits = 128; /* 16 bytes */
+
if (ioctl(cfd, NCRIO_KEY_GENERATE, &kgen)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_IMPORT)");
+ perror("ioctl(NCRIO_KEY_GENERATE)");
return 1;
}
memset(data, 0, sizeof(data));
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = key;
- keydata.idata = data;
- keydata.idata_size = sizeof(data);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = key;
+ kexport.buffer = data;
+ kexport.buffer_size = sizeof(data);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ if (ioctl(cfd, NCRIO_KEY_EXPORT, &kexport) != sizeof(data)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_IMPORT)");
+ perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
- if (keydata.idata_size == 0 || (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[4] == 0)) {
+ if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[4] == 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
fprintf(stderr, "Generated key: %.2x.%.2x.%.2x.%.2x.%.2x.%.2x.%.2x.%.2x."
"%.2x.%.2x.%.2x.%.2x.%.2x.%.2x.%.2x.%.2x\n", data[0], data[1],
@@ -152,6 +202,92 @@ test_ncr_key(int cfd)
return 1;
}
+ memset(&kinfo.f, 0, sizeof(kinfo.f));
+ kinfo.f.output_size = sizeof(kinfo);
+ kinfo.f.key = key;
+ nla = &kinfo.u.first_header;
+ nla->nla_type = NCR_ATTR_WANTED_ATTRS;
+ attr_p = (uint16_t *)((char *)nla + NLA_HDRLEN);
+ *attr_p++ = NCR_ATTR_ALGORITHM;
+ *attr_p++ = NCR_ATTR_KEY_FLAGS;
+ *attr_p++ = NCR_ATTR_KEY_TYPE;
+ nla->nla_len = (char *)attr_p - (char *)nla;
+ kinfo.f.input_size = (char *)attr_p - (char *)&kinfo;
+
+ if (ioctl(cfd, NCRIO_KEY_GET_INFO, &kinfo)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_KEY_GET_INFO)");
+ return 1;
+ }
+
+ got_algo = got_flags = got_type = 0;
+ if (kinfo.f.output_size <
+ (char *)&kinfo.u.first_header - (char *)&kinfo) {
+ fprintf(stderr, "No nlattr returned\n");
+ return 1;
+ }
+ nla = &kinfo.u.first_header;
+ for (;;) {
+ void *data;
+
+ if (nla->nla_len >
+ kinfo.f.output_size - ((char *)nla - (char *)&kinfo)) {
+ fprintf(stderr, "Attributes overflow\n");
+ return 1;
+ }
+ data = (char *)nla + NLA_HDRLEN;
+ switch (nla->nla_type) {
+ case NCR_ATTR_ALGORITHM:
+ if (nla->nla_len < NLA_HDRLEN + 1) {
+ fprintf(stderr, "Attribute too small\n");
+ return 1;
+ }
+ if (((char *)data)[nla->nla_len - NLA_HDRLEN - 1]
+ != 0) {
+ fprintf(stderr, "NUL missing\n");
+ return 1;
+ }
+ if (strcmp(data, ALG_AES_CBC) != 0) {
+ fprintf(stderr, "Unexpected algorithm\n");
+ return 1;
+ }
+ got_algo++;
+ break;
+ case NCR_ATTR_KEY_FLAGS:
+ if (nla->nla_len < NLA_HDRLEN + sizeof(uint32_t)) {
+ fprintf(stderr, "Attribute too small\n");
+ return 1;
+ }
+ if (*(uint32_t *)data != NCR_KEY_FLAG_EXPORTABLE) {
+ fprintf(stderr, "Unexpected key flags\n");
+ return 1;
+ }
+ got_flags++;
+ break;
+ case NCR_ATTR_KEY_TYPE:
+ if (nla->nla_len < NLA_HDRLEN + sizeof(uint32_t)) {
+ fprintf(stderr, "Attribute too small\n");
+ return 1;
+ }
+ if (*(uint32_t *)data != NCR_KEY_TYPE_SECRET) {
+ fprintf(stderr, "Unexpected key type\n");
+ return 1;
+ }
+ got_type++;
+ break;
+ }
+
+ if (NLA_ALIGN(nla->nla_len) + NLA_HDRLEN >
+ kinfo.f.output_size - ((char *)nla - (char *)&kinfo))
+ break;
+ nla = (struct nlattr *)((char *)nla + NLA_ALIGN(nla->nla_len));
+ }
+ if (got_algo != 1 || got_flags != 1 || got_type != 1) {
+ fprintf(stderr, "Unexpected attrs - %d, %d, %d\n", got_algo,
+ got_flags, got_type);
+ return 1;
+ }
+
if (ioctl(cfd, NCRIO_KEY_DEINIT, &key)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_DEINIT)");
@@ -162,32 +298,41 @@ test_ncr_key(int cfd)
* try to export it.
*/
fprintf(stdout, "\tKey protection of non-exportable keys...\n");
- if (ioctl(cfd, NCRIO_KEY_INIT, &key)) {
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- kgen.desc = key;
- kgen.params.algorithm = NCR_ALG_AES_CBC;
- kgen.params.keyflags = 0;
- kgen.params.params.secret.bits = 128; /* 16 bytes */
-
+ memset(&kgen.f, 0, sizeof(kgen.f));
+ kgen.f.input_size = sizeof(kgen);
+ kgen.f.key = key;
+ kgen.algo_head.nla_len = NLA_HDRLEN + sizeof(kgen.algo);
+ kgen.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kgen.algo, ALG_AES_CBC);
+ kgen.flags_head.nla_len = NLA_HDRLEN + sizeof(kgen.flags);
+ kgen.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kgen.flags = 0;
+ kgen.bits_head.nla_len = NLA_HDRLEN + sizeof(kgen.flags);
+ kgen.bits_head.nla_type = NCR_ATTR_SECRET_KEY_BITS;
+ kgen.bits = 128; /* 16 bytes */
+
if (ioctl(cfd, NCRIO_KEY_GENERATE, &kgen)) {
- perror("ioctl(NCRIO_KEY_IMPORT)");
+ perror("ioctl(NCRIO_KEY_GENERATE)");
return 1;
}
memset(data, 0, sizeof(data));
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = key;
- keydata.idata = data;
- keydata.idata_size = sizeof(data);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = key;
+ kexport.buffer = data;
+ kexport.buffer_size = sizeof(data);
/* try to get the output data - should fail */
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)==0) {
+ if (ioctl(cfd, NCRIO_KEY_EXPORT, &kexport) >= 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
fprintf(stderr, "Data were exported, but shouldn't be!\n");
return 1;
@@ -208,10 +353,31 @@ test_ncr_key(int cfd)
static int
test_ncr_wrap_key(int cfd)
{
- int i;
+ int i, ret;
ncr_key_t key, key2;
- struct ncr_key_data_st keydata;
- struct ncr_key_wrap_st kwrap;
+ struct __attribute__((packed)) {
+ struct ncr_key_import f;
+ struct nlattr id_head ALIGN_NL;
+ uint8_t id[2] ALIGN_NL;
+ struct nlattr type_head ALIGN_NL;
+ uint32_t type ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_AES_CBC)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ } kimport;
+ struct __attribute__((packed)) {
+ struct ncr_key_wrap f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(NCR_WALG_AES_RFC3394)] ALIGN_NL;
+ } kwrap;
+ struct __attribute__((packed)) {
+ struct ncr_key_unwrap f;
+ struct nlattr wrap_algo_head ALIGN_NL;
+ char wrap_algo[sizeof(NCR_WALG_AES_RFC3394)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ } kunwrap;
uint8_t data[WRAPPED_KEY_DATA_SIZE];
int data_size;
@@ -224,83 +390,106 @@ test_ncr_wrap_key(int cfd)
fprintf(stdout, "\tKey Wrap test...\n");
/* convert it to key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &key)) {
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
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|NCR_KEY_FLAG_WRAPPABLE;
-
- keydata.key = key;
- keydata.idata = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F";
- keydata.idata_size = 16;
-
- if (ioctl(cfd, NCRIO_KEY_IMPORT, &keydata)) {
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.input_size = sizeof(kimport);
+ kimport.f.key = key;
+ kimport.f.data = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F";
+ kimport.f.data_size = 16;
+ kimport.id_head.nla_len = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'a';
+ kimport.id[1] = 'b';
+ kimport.type_head.nla_len = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.algo_head.nla_len = NLA_HDRLEN + sizeof(kimport.algo);
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kimport.algo, ALG_AES_CBC);
+ kimport.flags_head.nla_len = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPING|NCR_KEY_FLAG_UNWRAPPING;
+
+ ret = ioctl(cfd, NCRIO_KEY_IMPORT, &kimport);
+ if (geteuid() == 0 && ret) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_IMPORT)");
return 1;
}
+ if (geteuid() != 0) {
+ /* cannot test further */
+ fprintf(stdout, "\t(Wrapping test not completed. Run as root)\n");
+ return 0;
+ }
/* convert it to key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &key2)) {
+ key2 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key2 == -1) {
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- keydata.key_id[0] = 'b';
- keydata.key_id[2] = 'a';
- keydata.key_id_size = 2;
- keydata.type = NCR_KEY_TYPE_SECRET;
- keydata.algorithm = NCR_ALG_AES_CBC;
- keydata.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
-
- keydata.key = key2;
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.input_size = sizeof(kimport);
+ kimport.f.key = key2;
#define DKEY "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"
- keydata.idata = DKEY;
- keydata.idata_size = 16;
-
- if (ioctl(cfd, NCRIO_KEY_IMPORT, &keydata)) {
+ kimport.f.data = DKEY;
+ kimport.f.data_size = 16;
+ kimport.id_head.nla_len = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'b';
+ kimport.id[1] = 'a';
+ kimport.type_head.nla_len = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.algo_head.nla_len = NLA_HDRLEN + sizeof(kimport.algo);
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kimport.algo, ALG_AES_CBC);
+ kimport.flags_head.nla_len = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
+
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_IMPORT)");
return 1;
}
/* now try wrapping key2 using key */
- memset(&kwrap, 0, sizeof(kwrap));
- kwrap.algorithm = NCR_WALG_AES_RFC3394;
- kwrap.keytowrap = key2;
- kwrap.key = key;
- kwrap.io = data;
- kwrap.io_size = sizeof(data);
-
- if (ioctl(cfd, NCRIO_KEY_WRAP, &kwrap)) {
+ memset(&kwrap.f, 0, sizeof(kwrap.f));
+ kwrap.f.input_size = sizeof(kwrap);
+ kwrap.f.wrapping_key = key;
+ kwrap.f.source_key = key2;
+ kwrap.f.buffer = data;
+ kwrap.f.buffer_size = sizeof(data);
+ kwrap.algo_head.nla_len = NLA_HDRLEN + sizeof(kwrap.algo);
+ kwrap.algo_head.nla_type = NCR_ATTR_WRAPPING_ALGORITHM;
+ strcpy(kwrap.algo, NCR_WALG_AES_RFC3394);
+
+ data_size = ioctl(cfd, NCRIO_KEY_WRAP, &kwrap);
+ if (data_size < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_WRAP)");
return 1;
}
-
- data_size = kwrap.io_size;
- if (kwrap.io_size != 24 || memcmp(data,
+ if (data_size != 24 || memcmp(data,
"\x1F\xA6\x8B\x0A\x81\x12\xB4\x47\xAE\xF3\x4B\xD8\xFB\x5A\x7B\x82\x9D\x3E\x86\x23\x71\xD2\xCF\xE5", 24) != 0) {
fprintf(stderr, "Wrapped data do not match.\n");
- fprintf(stderr, "Data[%d]: ",(int) kwrap.io_size);
- for(i=0;i<kwrap.io_size;i++)
+ fprintf(stderr, "Data[%d]: ",(int) data_size);
+ for(i=0;i<data_size;i++)
fprintf(stderr, "%.2x:", data[i]);
fprintf(stderr, "\n");
return 1;
}
-
-
-
/* test unwrapping */
fprintf(stdout, "\tKey Unwrap test...\n");
@@ -310,19 +499,26 @@ test_ncr_wrap_key(int cfd)
return 1;
}
- if (ioctl(cfd, NCRIO_KEY_INIT, &key2)) {
+ key2 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key2 == -1) {
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- memset(&kwrap, 0, sizeof(kwrap));
- kwrap.algorithm = NCR_WALG_AES_RFC3394;
- kwrap.keytowrap = key2;
- kwrap.key = key;
- kwrap.io = data;
- kwrap.io_size = data_size;
-
- if (ioctl(cfd, NCRIO_KEY_UNWRAP, &kwrap)) {
+ memset(&kunwrap.f, 0, sizeof(kunwrap.f));
+ kunwrap.f.input_size = sizeof(kunwrap);
+ kunwrap.f.wrapping_key = key;
+ kunwrap.f.dest_key = key2;
+ kunwrap.f.data = data;
+ kunwrap.f.data_size = data_size;
+ kunwrap.wrap_algo_head.nla_len = NLA_HDRLEN + sizeof(kunwrap.wrap_algo);
+ kunwrap.wrap_algo_head.nla_type = NCR_ATTR_WRAPPING_ALGORITHM;
+ strcpy(kunwrap.wrap_algo, NCR_WALG_AES_RFC3394);
+ kunwrap.flags_head.nla_len = NLA_HDRLEN + sizeof(kunwrap.flags);
+ kunwrap.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kunwrap.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
+
+ if (ioctl(cfd, NCRIO_KEY_UNWRAP, &kunwrap)) {
perror("ioctl(NCRIO_KEY_UNWRAP)");
return 1;
}
@@ -360,7 +556,129 @@ test_ncr_wrap_key(int cfd)
#endif
return 0;
+}
+/* check whether wrapping of long keys is not allowed with
+ * shorted wrapping keys */
+static int
+test_ncr_wrap_key2(int cfd)
+{
+ int ret;
+ ncr_key_t key, key2;
+ struct __attribute__((packed)) {
+ struct ncr_key_import f;
+ struct nlattr id_head ALIGN_NL;
+ uint8_t id[2] ALIGN_NL;
+ struct nlattr type_head ALIGN_NL;
+ uint32_t type ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_AES_CBC)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ } kimport;
+ struct __attribute__((packed)) {
+ struct ncr_key_wrap f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(NCR_WALG_AES_RFC3394)] ALIGN_NL;
+ } kwrap;
+ uint8_t data[WRAPPED_KEY_DATA_SIZE];
+
+ /* test 1: generate a key in userspace import it
+ * to kernel via data and export it.
+ */
+
+ fprintf(stdout, "\tKey Wrap test II...\n");
+
+ if (geteuid() != 0) {
+ /* cannot test further */
+ fprintf(stdout, "\t(Wrapping test not completed. Run as root)\n");
+ return 0;
+ }
+
+ /* convert it to key */
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
+ perror("ioctl(NCRIO_KEY_INIT)");
+ return 1;
+ }
+
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.input_size = sizeof(kimport);
+ kimport.f.key = key;
+ kimport.f.data = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F";
+ kimport.f.data_size = 16;
+ kimport.id_head.nla_len = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'a';
+ kimport.id[1] = 'b';
+ kimport.type_head.nla_len = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.algo_head.nla_len = NLA_HDRLEN + sizeof(kimport.algo);
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kimport.algo, ALG_AES_CBC);
+ kimport.flags_head.nla_len = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPING|NCR_KEY_FLAG_UNWRAPPING;
+
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_KEY_IMPORT)");
+ return 1;
+ }
+
+
+ /* convert it to key */
+ key2 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key2 == -1) {
+ perror("ioctl(NCRIO_KEY_INIT)");
+ return 1;
+ }
+
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.input_size = sizeof(kimport);
+ kimport.f.key = key2;
+ kimport.f.data = "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF";
+ kimport.f.data_size = 32;
+ kimport.id_head.nla_len = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'b';
+ kimport.id[1] = 'a';
+ kimport.type_head.nla_len = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.algo_head.nla_len = NLA_HDRLEN + sizeof(kimport.algo);
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kimport.algo, ALG_AES_CBC);
+ kimport.flags_head.nla_len = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
+
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_KEY_IMPORT)");
+ return 1;
+ }
+
+ /* now try wrapping key2 using key */
+ memset(&kwrap.f, 0, sizeof(kwrap.f));
+ kwrap.f.input_size = sizeof(kwrap);
+ kwrap.f.wrapping_key = key;
+ kwrap.f.source_key = key2;
+ kwrap.f.buffer = data;
+ kwrap.f.buffer_size = sizeof(data);
+ kwrap.algo_head.nla_len = NLA_HDRLEN + sizeof(kwrap.algo);
+ kwrap.algo_head.nla_type = NCR_ATTR_WRAPPING_ALGORITHM;
+ strcpy(kwrap.algo, NCR_WALG_AES_RFC3394);
+
+ ret = ioctl(cfd, NCRIO_KEY_WRAP, &kwrap);
+ if (ret >= 0) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ /* wrapping shouldn't have been allowed */
+ return 1;
+ }
+
+ return 0;
}
static int
@@ -368,8 +686,20 @@ test_ncr_store_wrap_key(int cfd)
{
int i;
ncr_key_t key2;
- struct ncr_key_data_st keydata;
- struct ncr_key_storage_wrap_st kwrap;
+ struct __attribute__((packed)) {
+ struct ncr_key_import f;
+ struct nlattr id_head ALIGN_NL;
+ uint8_t id[2] ALIGN_NL;
+ struct nlattr type_head ALIGN_NL;
+ uint32_t type ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_AES_CBC)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ } kimport;
+ struct ncr_key_export kexport;
+ struct ncr_key_storage_wrap kwrap;
+ struct ncr_key_storage_unwrap kunwrap;
uint8_t data[DATA_SIZE];
int data_size;
@@ -382,24 +712,33 @@ test_ncr_store_wrap_key(int cfd)
fprintf(stdout, "\tKey Storage wrap test...\n");
/* convert it to key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &key2)) {
+ key2 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key2 == -1) {
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- keydata.key_id[0] = 'b';
- keydata.key_id[2] = 'a';
- keydata.key_id_size = 2;
- keydata.type = NCR_KEY_TYPE_SECRET;
- keydata.algorithm = NCR_ALG_AES_CBC;
- keydata.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
-
- keydata.key = key2;
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.input_size = sizeof(kimport);
+ kimport.f.key = key2;
#define DKEY "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"
- keydata.idata = DKEY;
- keydata.idata_size = 16;
-
- if (ioctl(cfd, NCRIO_KEY_IMPORT, &keydata)) {
+ kimport.f.data = DKEY;
+ kimport.f.data_size = 16;
+ kimport.id_head.nla_len = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'b';
+ kimport.id[1] = 'a';
+ kimport.type_head.nla_len = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.algo_head.nla_len = NLA_HDRLEN + sizeof(kimport.algo);
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kimport.algo, ALG_AES_CBC);
+ kimport.flags_head.nla_len = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
+
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_IMPORT)");
return 1;
@@ -407,18 +746,18 @@ test_ncr_store_wrap_key(int cfd)
/* now try wrapping key2 using key */
memset(&kwrap, 0, sizeof(kwrap));
- kwrap.keytowrap = key2;
- kwrap.io = data;
- kwrap.io_size = sizeof(data);
+ kwrap.key = key2;
+ kwrap.buffer = data;
+ kwrap.buffer_size = sizeof(data);
- if (ioctl(cfd, NCRIO_KEY_STORAGE_WRAP, &kwrap)) {
+ data_size = ioctl(cfd, NCRIO_KEY_STORAGE_WRAP, &kwrap);
+ if (data_size < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_STORAGE_WRAP)");
return 1;
}
/* test unwrapping */
- data_size = kwrap.io_size;
fprintf(stdout, "\tKey Storage Unwrap test...\n");
/* reset key2 */
@@ -428,38 +767,38 @@ test_ncr_store_wrap_key(int cfd)
return 1;
}
- if (ioctl(cfd, NCRIO_KEY_INIT, &key2)) {
+ key2 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key2 == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- memset(&kwrap, 0, sizeof(kwrap));
- kwrap.keytowrap = key2;
- kwrap.io = data;
- kwrap.io_size = data_size;
+ memset(&kunwrap, 0, sizeof(kunwrap));
+ kunwrap.key = key2;
+ kunwrap.data = data;
+ kunwrap.data_size = data_size;
- if (ioctl(cfd, NCRIO_KEY_STORAGE_UNWRAP, &kwrap)) {
+ if (ioctl(cfd, NCRIO_KEY_STORAGE_UNWRAP, &kunwrap)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_STORAGE_UNWRAP)");
return 1;
}
/* now export the unwrapped */
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = key2;
- keydata.idata = data;
- keydata.idata_size = sizeof(data);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = key2;
+ kexport.buffer = data;
+ kexport.buffer_size = sizeof(data);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ data_size = ioctl(cfd, NCRIO_KEY_EXPORT, &kexport);
+ if (data_size != 16) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_IMPORT)");
+ perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
-
- data_size = keydata.idata_size;
- if (data_size != 16 || memcmp(data, DKEY, 16) != 0) {
+ if (memcmp(data, DKEY, 16) != 0) {
fprintf(stderr, "Unwrapped data do not match.\n");
fprintf(stderr, "Data[%d]: ", (int) data_size);
for(i=0;i<data_size;i++)
@@ -509,56 +848,91 @@ static int
test_ncr_aes(int cfd)
{
ncr_key_t key;
- struct ncr_key_data_st keydata;
+ struct __attribute__((packed)) {
+ struct ncr_key_import f;
+ struct nlattr id_head ALIGN_NL;
+ uint8_t id[2] ALIGN_NL;
+ struct nlattr type_head ALIGN_NL;
+ uint32_t type ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_AES_ECB)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ } kimport;
uint8_t data[KEY_DATA_SIZE];
int i, j;
- struct ncr_session_once_op_st nop;
- int data_size;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_AES_ECB)] ALIGN_NL;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr output_head ALIGN_NL;
+ struct ncr_session_output_buffer output ALIGN_NL;
+ } op;
+ size_t data_size;
/* convert it to key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &key)) {
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
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<sizeof(aes_vectors)/sizeof(aes_vectors[0]);i++) {
- keydata.key = key;
- keydata.idata = (void*)aes_vectors[i].key;
- keydata.idata_size = 16;
- if (ioctl(cfd, NCRIO_KEY_IMPORT, &keydata)) {
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.input_size = sizeof(kimport);
+ kimport.f.key = key;
+ kimport.f.data = aes_vectors[i].key;
+ kimport.f.data_size = 16;
+ kimport.id_head.nla_len = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'a';
+ kimport.id[1] = 'b';
+ kimport.type_head.nla_len = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.algo_head.nla_len = NLA_HDRLEN + sizeof(kimport.algo);
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kimport.algo, ALG_AES_ECB);
+ kimport.flags_head.nla_len = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE;
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_IMPORT)");
return 1;
}
/* encrypt */
- memset(&nop, 0, sizeof(nop));
- nop.init.algorithm = NCR_ALG_AES_ECB;
- nop.init.key = key;
- nop.init.op = NCR_OP_ENCRYPT;
- nop.op.data.udata.input = (void*)aes_vectors[i].plaintext;
- nop.op.data.udata.input_size = 16;
- nop.op.data.udata.output = data;
- nop.op.data.udata.output_size = sizeof(data);
- nop.op.type = NCR_DIRECT_DATA;
-
- if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) {
+ memset(&op.f, 0, sizeof(op.f));
+ op.f.input_size = sizeof(op);
+ op.f.op = NCR_OP_ENCRYPT;
+ op.algo_head.nla_len = NLA_HDRLEN + sizeof(op.algo);
+ op.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(op.algo, ALG_AES_ECB);
+ op.key_head.nla_len = NLA_HDRLEN + sizeof(op.key);
+ op.key_head.nla_type = NCR_ATTR_KEY;
+ op.key = key;
+ op.input_head.nla_len = NLA_HDRLEN + sizeof(op.input);
+ op.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ op.input.data = aes_vectors[i].plaintext;
+ op.input.data_size = 16;
+ op.output_head.nla_len = NLA_HDRLEN + sizeof(op.output);
+ op.output_head.nla_type = NCR_ATTR_UPDATE_OUTPUT_BUFFER;
+ op.output.buffer = data;
+ op.output.buffer_size = sizeof(data);
+ op.output.result_size_ptr = &data_size;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &op)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_ONCE)");
return 1;
}
-
- data_size = nop.op.data.udata.output_size;
/* verify */
if (data_size != 16 || memcmp(data, aes_vectors[i].ciphertext, 16) != 0) {
@@ -580,34 +954,55 @@ test_ncr_aes(int cfd)
fprintf(stdout, "Tests on AES Decryption\n");
for (i=0;i<sizeof(aes_vectors)/sizeof(aes_vectors[0]);i++) {
- keydata.key = key;
- keydata.idata = (void*)aes_vectors[i].key;
- keydata.idata_size = 16;
- if (ioctl(cfd, NCRIO_KEY_IMPORT, &keydata)) {
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.input_size = sizeof(kimport);
+ kimport.f.key = key;
+ kimport.f.data = aes_vectors[i].key;
+ kimport.f.data_size = 16;
+ kimport.id_head.nla_len = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'a';
+ kimport.id[1] = 'b';
+ kimport.type_head.nla_len = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.algo_head.nla_len = NLA_HDRLEN + sizeof(kimport.algo);
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kimport.algo, ALG_AES_CBC);
+ kimport.flags_head.nla_len = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE;
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_IMPORT)");
return 1;
}
/* decrypt */
- memset(&nop, 0, sizeof(nop));
- nop.init.algorithm = NCR_ALG_AES_ECB;
- nop.init.key = key;
- nop.init.op = NCR_OP_DECRYPT;
- nop.op.data.udata.input = (void*)aes_vectors[i].ciphertext;
- nop.op.data.udata.input_size = 16;
- nop.op.data.udata.output = data;
- nop.op.data.udata.output_size = sizeof(data);
- nop.op.type = NCR_DIRECT_DATA;
-
- if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) {
+ memset(&op.f, 0, sizeof(op.f));
+ op.f.input_size = sizeof(op);
+ op.f.op = NCR_OP_DECRYPT;
+ op.algo_head.nla_len = NLA_HDRLEN + sizeof(op.algo);
+ op.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(op.algo, ALG_AES_ECB);
+ op.key_head.nla_len = NLA_HDRLEN + sizeof(op.key);
+ op.key_head.nla_type = NCR_ATTR_KEY;
+ op.key = key;
+ op.input_head.nla_len = NLA_HDRLEN + sizeof(op.input);
+ op.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ op.input.data = aes_vectors[i].ciphertext;
+ op.input.data_size = 16;
+ op.output_head.nla_len = NLA_HDRLEN + sizeof(op.output);
+ op.output_head.nla_type = NCR_ATTR_UPDATE_OUTPUT_BUFFER;
+ op.output.buffer = data;
+ op.output.buffer_size = sizeof(data);
+ op.output.result_size_ptr = &data_size;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &op)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_ONCE)");
return 1;
}
-
- data_size = nop.op.data.udata.output_size;
-
if (data_size != 16 || memcmp(data, aes_vectors[i].plaintext, 16) != 0) {
fprintf(stderr, "AES test vector %d failed!\n", i);
@@ -633,8 +1028,7 @@ test_ncr_aes(int cfd)
}
struct hash_vectors_st {
- const char* name;
- ncr_algorithm_t algorithm;
+ const char* algorithm;
const uint8_t* key; /* if hmac */
int key_size;
const uint8_t* plaintext;
@@ -644,8 +1038,7 @@ struct hash_vectors_st {
ncr_crypto_op_t op;
} hash_vectors[] = {
{
- .name = "SHA1",
- .algorithm = NCR_ALG_SHA1,
+ .algorithm = "sha1",
.key = NULL,
.plaintext = (uint8_t*)"what do ya want for nothing?",
.plaintext_size = sizeof("what do ya want for nothing?")-1,
@@ -654,8 +1047,7 @@ struct hash_vectors_st {
.op = NCR_OP_SIGN,
},
{
- .name = "HMAC-MD5",
- .algorithm = NCR_ALG_HMAC_MD5,
+ .algorithm = "hmac(md5)",
.key = (uint8_t*)"Jefe",
.key_size = 4,
.plaintext = (uint8_t*)"what do ya want for nothing?",
@@ -666,8 +1058,7 @@ struct hash_vectors_st {
},
/* from rfc4231 */
{
- .name = "HMAC-SHA224",
- .algorithm = NCR_ALG_HMAC_SHA2_224,
+ .algorithm = "hmac(sha224)",
.key = (uint8_t*)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
.key_size = 20,
.plaintext = (uint8_t*)"Hi There",
@@ -677,8 +1068,7 @@ struct hash_vectors_st {
.op = NCR_OP_SIGN,
},
{
- .name = "HMAC-SHA256",
- .algorithm = NCR_ALG_HMAC_SHA2_256,
+ .algorithm = "hmac(sha256)",
.key = (uint8_t*)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
.key_size = 20,
.plaintext = (uint8_t*)"Hi There",
@@ -688,8 +1078,7 @@ struct hash_vectors_st {
.op = NCR_OP_SIGN,
},
{
- .name = "HMAC-SHA384",
- .algorithm = NCR_ALG_HMAC_SHA2_384,
+ .algorithm = "hmac(sha384)",
.key = (uint8_t*)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
.key_size = 20,
.plaintext = (uint8_t*)"Hi There",
@@ -699,8 +1088,7 @@ struct hash_vectors_st {
.op = NCR_OP_SIGN,
},
{
- .name = "HMAC-SHA512",
- .algorithm = NCR_ALG_HMAC_SHA2_512,
+ .algorithm = "hmac(sha512)",
.key = (uint8_t*)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
.key_size = 20,
.plaintext = (uint8_t*)"Hi There",
@@ -718,62 +1106,102 @@ static int
test_ncr_hash(int cfd)
{
ncr_key_t key;
- struct ncr_key_data_st keydata;
+ struct __attribute__((packed)) {
+ struct ncr_key_import f;
+ struct nlattr id_head ALIGN_NL;
+ uint8_t id[2] ALIGN_NL;
+ struct nlattr type_head ALIGN_NL;
+ uint32_t type ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[128] ALIGN_NL;
+ } kimport;
uint8_t data[HASH_DATA_SIZE];
- int i, j, data_size;
- struct ncr_session_once_op_st nop;
+ int i, j;
+ size_t data_size;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr output_head ALIGN_NL;
+ struct ncr_session_output_buffer output ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[128] ALIGN_NL;
+ } op;
/* convert it to key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &key)) {
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
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 Hashes\n");
for (i=0;i<sizeof(hash_vectors)/sizeof(hash_vectors[0]);i++) {
+ size_t algo_size;
- fprintf(stdout, "\t%s:\n", hash_vectors[i].name);
+ algo_size = strlen(hash_vectors[i].algorithm) + 1;
+ fprintf(stdout, "\t%s:\n", hash_vectors[i].algorithm);
/* import key */
if (hash_vectors[i].key != NULL) {
- keydata.key = key;
- keydata.idata = (void*)hash_vectors[i].key;
- keydata.idata_size = hash_vectors[i].key_size;
- if (ioctl(cfd, NCRIO_KEY_IMPORT, &keydata)) {
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.key = key;
+ kimport.f.data = hash_vectors[i].key;
+ kimport.f.data_size = hash_vectors[i].key_size;
+ kimport.id_head.nla_len
+ = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'a';
+ kimport.id[1] = 'b';
+ kimport.type_head.nla_len
+ = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.flags_head.nla_len
+ = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE;
+ kimport.algo_head.nla_len = NLA_HDRLEN + algo_size;
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ memcpy(kimport.algo, hash_vectors[i].algorithm,
+ algo_size);
+ kimport.f.input_size
+ = kimport.algo + algo_size - (char *)&kimport;
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_IMPORT)");
return 1;
}
}
- /* encrypt */
- memset(&nop, 0, sizeof(nop));
- nop.init.algorithm = hash_vectors[i].algorithm;
- if (hash_vectors[i].key != NULL)
- nop.init.key = key;
- nop.init.op = hash_vectors[i].op;
- nop.op.data.udata.input = (void*)hash_vectors[i].plaintext;
- nop.op.data.udata.input_size = hash_vectors[i].plaintext_size;
- nop.op.data.udata.output = data;
- nop.op.data.udata.output_size = sizeof(data);
- nop.op.type = NCR_DIRECT_DATA;
-
- if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) {
+ memset(&op.f, 0, sizeof(op.f));
+ op.f.op = hash_vectors[i].op;
+ op.key_head.nla_len = NLA_HDRLEN + sizeof(op.key);
+ op.key_head.nla_type = NCR_ATTR_KEY;
+ op.key = hash_vectors[i].key != NULL ? key : NCR_KEY_INVALID;
+ op.input_head.nla_len = NLA_HDRLEN + sizeof(op.input);
+ op.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ op.input.data = hash_vectors[i].plaintext;
+ op.input.data_size = hash_vectors[i].plaintext_size;
+ op.output_head.nla_len = NLA_HDRLEN + sizeof(op.output);
+ op.output_head.nla_type = NCR_ATTR_FINAL_OUTPUT_BUFFER;
+ op.output.buffer = data;
+ op.output.buffer_size = sizeof(data);
+ op.output.result_size_ptr = &data_size;
+ op.algo_head.nla_len = NLA_HDRLEN + algo_size;
+ op.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ memcpy(op.algo, hash_vectors[i].algorithm, algo_size);
+ op.f.input_size = op.algo + algo_size - (char *)&op;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &op)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_ONCE)");
return 1;
}
-
- data_size = nop.op.data.udata.output_size;
-
if (data_size != hash_vectors[i].output_size ||
memcmp(data, hash_vectors[i].output, hash_vectors[i].output_size) != 0) {
@@ -799,94 +1227,353 @@ test_ncr_hash(int cfd)
}
static int
+test_ncr_hash_clone(int cfd)
+{
+ ncr_key_t key;
+ struct __attribute__((packed)) {
+ struct ncr_key_import f;
+ struct nlattr id_head ALIGN_NL;
+ uint8_t id[2] ALIGN_NL;
+ struct nlattr type_head ALIGN_NL;
+ uint32_t type ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[128] ALIGN_NL;
+ } kimport;
+ uint8_t data[HASH_DATA_SIZE];
+ const struct hash_vectors_st *hv;
+ int j;
+ size_t data_size;
+ struct __attribute__((packed)) {
+ struct ncr_session_init f;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[128] ALIGN_NL;
+ } kinit;
+ struct __attribute__((packed)) {
+ struct ncr_session_update f;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ } kupdate;
+ struct __attribute__((packed)) {
+ struct ncr_session_final f;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr output_head ALIGN_NL;
+ struct ncr_session_output_buffer output ALIGN_NL;
+ } kfinal;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr clone_head ALIGN_NL;
+ uint32_t clone ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr output_head ALIGN_NL;
+ struct ncr_session_output_buffer output ALIGN_NL;
+ } kclone;
+ ncr_session_t ses;
+
+ /* convert it to key */
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
+ perror("ioctl(NCRIO_KEY_INIT)");
+ return 1;
+ }
+
+ fprintf(stdout, "Tests of hash cloning\n");
+ for (hv = hash_vectors;
+ hv < hash_vectors + sizeof(hash_vectors) / sizeof(hash_vectors[0]);
+ hv++) {
+ size_t algo_size;
+
+ algo_size = strlen(hv->algorithm) + 1;
+ fprintf(stdout, "\t%s:\n", hv->algorithm);
+ /* import key */
+ if (hv->key != NULL) {
+
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.key = key;
+ kimport.f.data = hv->key;
+ kimport.f.data_size = hv->key_size;
+ kimport.id_head.nla_len
+ = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'a';
+ kimport.id[1] = 'b';
+ kimport.type_head.nla_len
+ = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.flags_head.nla_len
+ = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE;
+ kimport.algo_head.nla_len = NLA_HDRLEN + algo_size;
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ memcpy(kimport.algo, hv->algorithm, algo_size);
+ kimport.f.input_size
+ = kimport.algo + algo_size - (char *)&kimport;
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_KEY_IMPORT)");
+ return 1;
+ }
+ }
+
+ /* Initialize a session */
+ memset(&kinit.f, 0, sizeof(kinit.f));
+ kinit.f.op = hv->op;
+ kinit.key_head.nla_len = NLA_HDRLEN + sizeof(kinit.key);
+ kinit.key_head.nla_type = NCR_ATTR_KEY;
+ kinit.key = hv->key != NULL ? key : NCR_KEY_INVALID;
+ kinit.algo_head.nla_len = NLA_HDRLEN + algo_size;
+ kinit.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ memcpy(kinit.algo, hv->algorithm, algo_size);
+ kinit.f.input_size = kinit.algo + algo_size - (char *)&kinit;
+
+ ses = ioctl(cfd, NCRIO_SESSION_INIT, &kinit);
+ if (ses < 0) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_SESSION_INIT)");
+ return 1;
+ }
+
+ /* Submit half of the data */
+ memset(&kupdate.f, 0, sizeof(kupdate.f));
+ kupdate.f.input_size = sizeof(kupdate);
+ kupdate.f.ses = ses;
+ kupdate.input_head.nla_len = NLA_HDRLEN + sizeof(kupdate.input);
+ kupdate.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ kupdate.input.data = hv->plaintext;
+ kupdate.input.data_size = hv->plaintext_size / 2;
+
+ if (ioctl(cfd, NCRIO_SESSION_UPDATE, &kupdate)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_SESSION_UPDATE)");
+ return 1;
+ }
+
+ /* Clone a session, submit the other half, verify. */
+ memset(&kclone.f, 0, sizeof(kclone.f));
+ kclone.f.input_size = sizeof(kclone);
+ kclone.f.op = hv->op;
+ kclone.clone_head.nla_len = NLA_HDRLEN + sizeof(kclone.clone);
+ kclone.clone_head.nla_type = NCR_ATTR_SESSION_CLONE_FROM;
+ kclone.clone = ses;
+ kclone.input_head.nla_len = NLA_HDRLEN + sizeof(kclone.input);
+ kclone.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ kclone.input.data = hv->plaintext + hv->plaintext_size / 2;
+ kclone.input.data_size
+ = hv->plaintext_size - hv->plaintext_size / 2;
+ kclone.output_head.nla_len = NLA_HDRLEN + sizeof(kclone.output);
+ kclone.output_head.nla_type = NCR_ATTR_FINAL_OUTPUT_BUFFER;
+ kclone.output.buffer = data;
+ kclone.output.buffer_size = sizeof(data);
+ kclone.output.result_size_ptr = &data_size;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &kclone)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_SESSION_ONCE)");
+ return 1;
+ }
+
+ if (data_size != hv->output_size
+ || memcmp(data, hv->output, hv->output_size) != 0) {
+ fprintf(stderr, "HASH test vector %td failed!\n",
+ hv - hash_vectors);
+
+ fprintf(stderr, "Output[%zu]: ", data_size);
+ for(j = 0; j < data_size; j++)
+ fprintf(stderr, "%.2x:", (int)data[j]);
+ fprintf(stderr, "\n");
+
+ fprintf(stderr, "Expected[%d]: ", hv->output_size);
+ for (j = 0; j < hv->output_size; j++)
+ fprintf(stderr, "%.2x:", (int)hv->output[j]);
+ fprintf(stderr, "\n");
+ return 1;
+ }
+
+ /* Submit the other half to the original session, verify. */
+ memset(&kfinal.f, 0, sizeof(kfinal.f));
+ kfinal.f.input_size = sizeof(kfinal);
+ kfinal.f.ses = ses;
+ kfinal.input_head.nla_len = NLA_HDRLEN + sizeof(kfinal.input);
+ kfinal.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ kfinal.input.data = hv->plaintext + hv->plaintext_size / 2;
+ kfinal.input.data_size
+ = hv->plaintext_size - hv->plaintext_size / 2;
+ kfinal.output_head.nla_len = NLA_HDRLEN + sizeof(kfinal.output);
+ kfinal.output_head.nla_type = NCR_ATTR_FINAL_OUTPUT_BUFFER;
+ kfinal.output.buffer = data;
+ kfinal.output.buffer_size = sizeof(data);
+ kfinal.output.result_size_ptr = &data_size;
+
+ if (ioctl(cfd, NCRIO_SESSION_FINAL, &kfinal)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_SESSION_FINAL)");
+ return 1;
+ }
+
+ if (data_size != hv->output_size
+ || memcmp(data, hv->output, hv->output_size) != 0) {
+ fprintf(stderr, "HASH test vector %td failed!\n",
+ hv - hash_vectors);
+
+ fprintf(stderr, "Output[%zu]: ", data_size);
+ for(j = 0; j < data_size; j++)
+ fprintf(stderr, "%.2x:", (int)data[j]);
+ fprintf(stderr, "\n");
+
+ fprintf(stderr, "Expected[%d]: ", hv->output_size);
+ for (j = 0; j < hv->output_size; j++)
+ fprintf(stderr, "%.2x:", (int)hv->output[j]);
+ fprintf(stderr, "\n");
+ return 1;
+ }
+ }
+
+ fprintf(stdout, "\n");
+
+ return 0;
+
+}
+
+static int
test_ncr_hash_key(int cfd)
{
ncr_key_t key;
- struct ncr_key_data_st keydata;
+ ncr_session_t ses;
+ struct __attribute__((packed)) {
+ struct ncr_key_import f;
+ struct nlattr id_head ALIGN_NL;
+ uint8_t id[2] ALIGN_NL;
+ struct nlattr type_head ALIGN_NL;
+ uint32_t type ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[128] ALIGN_NL;
+ } kimport;
uint8_t data[HASH_DATA_SIZE];
- int j, data_size;
- struct ncr_session_op_st op;
- struct ncr_session_st op_init;
+ int j;
+ size_t data_size, algo_size;
+ struct __attribute__((packed)) {
+ struct ncr_session_init f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[128] ALIGN_NL;
+ } op_init;
+ struct __attribute__((packed)) {
+ struct ncr_session_update f;
+ struct nlattr data_head ALIGN_NL;
+ struct ncr_session_input_data data ALIGN_NL;
+ } op_up_data;
+ struct __attribute__((packed)) {
+ struct ncr_session_update f;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key;
+ } op_up_key;
+ struct __attribute__((packed)) {
+ struct ncr_session_final f;
+ struct nlattr output_head ALIGN_NL;
+ struct ncr_session_output_buffer output ALIGN_NL;
+ } op_final;
const uint8_t *output = (void*)"\xe2\xd7\x2c\x2e\x14\xad\x97\xc8\xd2\xdb\xce\xd8\xb3\x52\x9f\x1c\xb3\x2c\x5c\xec";
/* convert it to key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &key)) {
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
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 Hashes of Keys\n");
- fprintf(stdout, "\t%s:\n", hash_vectors[0].name);
+ fprintf(stdout, "\t%s:\n", hash_vectors[0].algorithm);
+ algo_size = strlen(hash_vectors[0].algorithm) + 1;
/* import key */
- keydata.key = key;
- keydata.idata = (void*)hash_vectors[0].plaintext;
- keydata.idata_size = hash_vectors[0].plaintext_size;
- if (ioctl(cfd, NCRIO_KEY_IMPORT, &keydata)) {
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.key = key;
+ kimport.f.data = hash_vectors[0].plaintext;
+ kimport.f.data_size = hash_vectors[0].plaintext_size;
+ kimport.id_head.nla_len = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'a';
+ kimport.id[1] = 'b';
+ kimport.type_head.nla_len = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.flags_head.nla_len = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE;
+ kimport.algo_head.nla_len = NLA_HDRLEN + algo_size;
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ memcpy(kimport.algo, hash_vectors[0].algorithm, algo_size);
+ kimport.f.input_size = kimport.algo + algo_size - (char *)&kimport;
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_IMPORT)");
return 1;
}
- /* encrypt */
- memset(&op_init, 0, sizeof(op_init));
- op_init.algorithm = hash_vectors[0].algorithm;
- op_init.op = hash_vectors[0].op;
+ memset(&op_init.f, 0, sizeof(op_init.f));
+ op_init.f.op = hash_vectors[0].op;
+ op_init.algo_head.nla_len = NLA_HDRLEN + algo_size;
+ op_init.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ memcpy(op_init.algo, hash_vectors[0].algorithm, algo_size);
+ op_init.f.input_size = op_init.algo + algo_size - (char *)&op_init;
- if (ioctl(cfd, NCRIO_SESSION_INIT, &op_init)) {
+ ses = ioctl(cfd, NCRIO_SESSION_INIT, &op_init);
+ if (ses < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_INIT)");
return 1;
}
- memset(&op, 0, sizeof(op));
- op.ses = op_init.ses;
- op.data.udata.input = (void*)hash_vectors[0].plaintext;
- op.data.udata.input_size = hash_vectors[0].plaintext_size;
- op.data.udata.output = NULL;
- op.data.udata.output_size = 0;
- op.type = NCR_DIRECT_DATA;
+ memset(&op_up_data.f, 0, sizeof(op_up_data.f));
+ op_up_data.f.input_size = sizeof(op_up_data);
+ op_up_data.f.ses = ses;
+ op_up_data.data_head.nla_len = NLA_HDRLEN + sizeof(op_up_data.data);
+ op_up_data.data_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ op_up_data.data.data = hash_vectors[0].plaintext;
+ op_up_data.data.data_size = hash_vectors[0].plaintext_size;
- if (ioctl(cfd, NCRIO_SESSION_UPDATE, &op)) {
+ if (ioctl(cfd, NCRIO_SESSION_UPDATE, &op_up_data)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_UPDATE)");
return 1;
}
- memset(&op, 0, sizeof(op));
- op.ses = op_init.ses;
- op.data.kdata.input = key;
- op.data.kdata.output = NULL;
- op.data.kdata.output_size = 0;
- op.type = NCR_KEY_DATA;
+ memset(&op_up_key.f, 0, sizeof(op_up_key.f));
+ op_up_key.f.input_size = sizeof(op_up_key);
+ op_up_key.f.ses = ses;
+ op_up_key.key_head.nla_len = NLA_HDRLEN + sizeof(op_up_key.key);
+ op_up_key.key_head.nla_type = NCR_ATTR_UPDATE_INPUT_KEY_AS_DATA;
+ op_up_key.key = key;
- if (ioctl(cfd, NCRIO_SESSION_UPDATE, &op)) {
+ if (ioctl(cfd, NCRIO_SESSION_UPDATE, &op_up_key)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_UPDATE)");
return 1;
}
- op.data.udata.input = NULL;
- op.data.udata.input_size = 0;
- op.data.udata.output = data;
- op.data.udata.output_size = sizeof(data);
- op.type = NCR_DIRECT_DATA;
+ memset(&op_final.f, 0, sizeof(op_final.f));
+ op_final.f.input_size = sizeof(op_final);
+ op_final.f.ses = ses;
+ op_final.output_head.nla_len = NLA_HDRLEN + sizeof(op_final.output);
+ op_final.output_head.nla_type = NCR_ATTR_FINAL_OUTPUT_BUFFER;
+ op_final.output.buffer = data;
+ op_final.output.buffer_size = sizeof(data);
+ op_final.output.result_size_ptr = &data_size;
- if (ioctl(cfd, NCRIO_SESSION_FINAL, &op)) {
+ if (ioctl(cfd, NCRIO_SESSION_FINAL, &op_final)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_FINAL)");
return 1;
}
- data_size = op.data.udata.output_size;
-
if (data_size != hash_vectors[0].output_size ||
memcmp(data, output, hash_vectors[0].output_size) != 0) {
@@ -933,12 +1620,18 @@ main()
if (test_ncr_hash(fd))
return 1;
+ if (test_ncr_hash_clone(fd))
+ return 1;
+
if (test_ncr_hash_key(fd))
return 1;
if (test_ncr_wrap_key(fd))
return 1;
+ if (test_ncr_wrap_key2(fd))
+ return 1;
+
if (test_ncr_store_wrap_key(fd))
return 1;
diff --git a/examples/pk.c b/examples/pk.c
index 3102a3b1abd..5ccb73e5205 100644
--- a/examples/pk.c
+++ b/examples/pk.c
@@ -4,6 +4,7 @@
* Placed under public domain.
*
*/
+#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -11,8 +12,10 @@
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
+#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <linux/netlink.h>
#include "../ncr.h"
#include <stdlib.h>
#include <gnutls/gnutls.h>
@@ -23,6 +26,16 @@
#define DATA_SIZE 4096
+#define ALIGN_NL __attribute__((aligned(NLA_ALIGNTO)))
+
+#define SIGNATURE_HASH "sha1"
+#define SIGNATURE_HASH_SIZE 20
+
+#define ALG_AES_CBC "cbc(aes)"
+#define ALG_DH "dh"
+#define ALG_DSA "dsa"
+#define ALG_RSA "rsa"
+
static void
print_hex_datum (gnutls_datum_t * dat)
{
@@ -307,16 +320,32 @@ const char dh_params_txt[] = "-----BEGIN DH PARAMETERS-----\n"\
static int test_ncr_dh(int cfd)
{
-struct ncr_key_generate_st kgen;
+struct __attribute__((packed)) {
+ struct ncr_key_generate_pair f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_DH)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ unsigned char buffer[DATA_SIZE] ALIGN_NL;
+} kgen;
+struct nlattr *nla;
ncr_key_t private1, public1, public2, private2;
ncr_key_t z1, z2;
int ret;
gnutls_datum g, p, params;
gnutls_dh_params_t dhp;
unsigned char y1[1024], y2[1024];
-size_t y1_size, y2_size;
-struct ncr_key_data_st keydata;
-struct ncr_key_derivation_params_st kderive;
+ssize_t y1_size, y2_size;
+struct ncr_key_export kexport;
+struct __attribute__((packed)) {
+ struct ncr_key_derive f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(NCR_DERIVE_DH)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ struct nlattr public_head ALIGN_NL;
+ unsigned char public[DATA_SIZE] ALIGN_NL;
+} kderive;
fprintf(stdout, "Tests on DH key exchange:");
fflush(stdout);
@@ -346,157 +375,201 @@ struct ncr_key_derivation_params_st kderive;
}
/* generate a DH key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &private1)) {
+ private1 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (private1 == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- if (ioctl(cfd, NCRIO_KEY_INIT, &public1)) {
+ public1 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (public1 == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- memset(&kgen, 0, sizeof(kgen));
- kgen.desc = private1;
- kgen.desc2 = public1;
- kgen.params.algorithm = NCR_ALG_DH;
- kgen.params.keyflags = NCR_KEY_FLAG_EXPORTABLE;
- kgen.params.params.dh.p = p.data;
- kgen.params.params.dh.p_size = p.size;
- kgen.params.params.dh.g = g.data;
- kgen.params.params.dh.g_size = g.size;
+ memset(&kgen.f, 0, sizeof(kgen.f));
+ kgen.f.private_key = private1;
+ kgen.f.public_key = public1;
+ kgen.algo_head.nla_len = NLA_HDRLEN + sizeof(kgen.algo);
+ kgen.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kgen.algo, ALG_DH);
+ kgen.flags_head.nla_len = NLA_HDRLEN + sizeof(kgen.flags);
+ kgen.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kgen.flags = NCR_KEY_FLAG_EXPORTABLE;
+ nla = (struct nlattr *)kgen.buffer;
+ nla->nla_len = NLA_HDRLEN + p.size;
+ nla->nla_type = NCR_ATTR_DH_PRIME;
+ memcpy((char *)nla + NLA_HDRLEN, p.data, p.size);
+ nla = (struct nlattr *)((char *)nla + NLA_ALIGN(nla->nla_len));
+ nla->nla_len = NLA_HDRLEN + g.size;
+ nla->nla_type = NCR_ATTR_DH_BASE;
+ memcpy((char *)nla + NLA_HDRLEN, g.data, g.size);
+ nla = (struct nlattr *)((char *)nla + NLA_ALIGN(nla->nla_len));
+ kgen.f.input_size = (char *)nla - (char *)&kgen;
+ assert(kgen.f.input_size <= sizeof(kgen));
if (ioctl(cfd, NCRIO_KEY_GENERATE_PAIR, &kgen)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_GENERATE)");
+ perror("ioctl(NCRIO_KEY_GENERATE_PAIR)");
return 1;
}
/* generate another DH key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &private2)) {
+ private2 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (private2 == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- if (ioctl(cfd, NCRIO_KEY_INIT, &public2)) {
+ public2 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (public2 == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- memset(&kgen, 0, sizeof(kgen));
- kgen.desc = private2;
- kgen.desc2 = public2;
- kgen.params.algorithm = NCR_ALG_DH;
- kgen.params.keyflags = NCR_KEY_FLAG_EXPORTABLE;
- kgen.params.params.dh.p = p.data;
- kgen.params.params.dh.p_size = p.size;
- kgen.params.params.dh.g = g.data;
- kgen.params.params.dh.g_size = g.size;
+ memset(&kgen.f, 0, sizeof(kgen.f));
+ kgen.f.private_key = private2;
+ kgen.f.public_key = public2;
+ kgen.algo_head.nla_len = NLA_HDRLEN + sizeof(kgen.algo);
+ kgen.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kgen.algo, ALG_DH);
+ kgen.flags_head.nla_len = NLA_HDRLEN + sizeof(kgen.flags);
+ kgen.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kgen.flags = NCR_KEY_FLAG_EXPORTABLE;
+ nla = (struct nlattr *)kgen.buffer;
+ nla->nla_len = NLA_HDRLEN + p.size;
+ nla->nla_type = NCR_ATTR_DH_PRIME;
+ memcpy((char *)nla + NLA_HDRLEN, p.data, p.size);
+ nla = (struct nlattr *)((char *)nla + NLA_ALIGN(nla->nla_len));
+ nla->nla_len = NLA_HDRLEN + g.size;
+ nla->nla_type = NCR_ATTR_DH_BASE;
+ memcpy((char *)nla + NLA_HDRLEN, g.data, g.size);
+ nla = (struct nlattr *)((char *)nla + NLA_ALIGN(nla->nla_len));
+ kgen.f.input_size = (char *)nla - (char *)&kgen;
+ assert(kgen.f.input_size <= sizeof(kgen));
if (ioctl(cfd, NCRIO_KEY_GENERATE_PAIR, &kgen)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_GENERATE)");
+ perror("ioctl(NCRIO_KEY_GENERATE_PAIR)");
return 1;
}
/* export y1=g^x1 */
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = public1;
- keydata.idata = y1;
- keydata.idata_size = sizeof(y1);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = public1;
+ kexport.buffer = y1;
+ kexport.buffer_size = sizeof(y1);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ y1_size = ioctl(cfd, NCRIO_KEY_EXPORT, &kexport);
+ if (y1_size < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
-
- y1_size = keydata.idata_size;
/* export y2=g^x2 */
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = public2;
- keydata.idata = y2;
- keydata.idata_size = sizeof(y2);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = public2;
+ kexport.buffer = y2;
+ kexport.buffer_size = sizeof(y2);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ y2_size = ioctl(cfd, NCRIO_KEY_EXPORT, &kexport);
+ if (y2_size < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
- y2_size = keydata.idata_size;
-
/* z1=y1^x2 */
- if (ioctl(cfd, NCRIO_KEY_INIT, &z1)) {
+ z1 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (z1 == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- memset(&kderive, 0, sizeof(kderive));
- kderive.derive = NCR_DERIVE_DH;
- kderive.newkey = z1;
- kderive.keyflags = NCR_KEY_FLAG_EXPORTABLE;
- kderive.key = private1;
- kderive.params.params.dh.pub = y2;
- kderive.params.params.dh.pub_size = y2_size;
+ memset(&kderive.f, 0, sizeof(kderive.f));
+ kderive.f.input_key = private1;
+ kderive.f.new_key = z1;
+ kderive.algo_head.nla_len = NLA_HDRLEN + sizeof(kderive.algo);
+ kderive.algo_head.nla_type = NCR_ATTR_DERIVATION_ALGORITHM;
+ strcpy(kderive.algo, NCR_DERIVE_DH);
+ kderive.flags_head.nla_len = NLA_HDRLEN + sizeof(kderive.flags);
+ kderive.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kderive.flags = NCR_KEY_FLAG_EXPORTABLE;
+ kderive.public_head.nla_len = NLA_HDRLEN + y2_size;
+ kderive.public_head.nla_type = NCR_ATTR_DH_PUBLIC;
+ memcpy(kderive.public, y2, y2_size);
+ nla = (struct nlattr *)((char *)&kderive.public_head
+ + NLA_ALIGN(kderive.public_head.nla_len));
+ kderive.f.input_size = (char *)nla - (char *)&kderive;
+ assert(kderive.f.input_size <= sizeof(kderive));
if (ioctl(cfd, NCRIO_KEY_DERIVE, &kderive)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_INIT)");
+ perror("ioctl(NCRIO_KEY_DERIVE)");
return 1;
}
/* z2=y2^x1 */
- if (ioctl(cfd, NCRIO_KEY_INIT, &z2)) {
+ z2 = ioctl(cfd, NCRIO_KEY_INIT);
+ if (z2 == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- memset(&kderive, 0, sizeof(kderive));
- kderive.derive = NCR_DERIVE_DH;
- kderive.newkey = z2;
- kderive.keyflags = NCR_KEY_FLAG_EXPORTABLE;
- kderive.key = private2;
- kderive.params.params.dh.pub = y1;
- kderive.params.params.dh.pub_size = y1_size;
+ memset(&kderive.f, 0, sizeof(kderive.f));
+ kderive.f.input_key = private2;
+ kderive.f.new_key = z2;
+ kderive.algo_head.nla_len = NLA_HDRLEN + sizeof(kderive.algo);
+ kderive.algo_head.nla_type = NCR_ATTR_DERIVATION_ALGORITHM;
+ strcpy(kderive.algo, NCR_DERIVE_DH);
+ kderive.flags_head.nla_len = NLA_HDRLEN + sizeof(kderive.flags);
+ kderive.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kderive.flags = NCR_KEY_FLAG_EXPORTABLE;
+ kderive.public_head.nla_len = NLA_HDRLEN + y2_size;
+ kderive.public_head.nla_type = NCR_ATTR_DH_PUBLIC;
+ memcpy(kderive.public, y1, y1_size);
+ nla = (struct nlattr *)((char *)&kderive.public_head
+ + NLA_ALIGN(kderive.public_head.nla_len));
+ kderive.f.input_size = (char *)nla - (char *)&kderive;
+ assert(kderive.f.input_size <= sizeof(kderive));
if (ioctl(cfd, NCRIO_KEY_DERIVE, &kderive)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_INIT)");
+ perror("ioctl(NCRIO_KEY_DERIVE)");
return 1;
}
/* z1==z2 */
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = z1;
- keydata.idata = y1;
- keydata.idata_size = sizeof(y1);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = z1;
+ kexport.buffer = y1;
+ kexport.buffer_size = sizeof(y1);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ y1_size = ioctl(cfd, NCRIO_KEY_EXPORT, &kexport);
+ if (y1_size < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
- y1_size = keydata.idata_size;
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = z2;
- keydata.idata = y2;
- keydata.idata_size = sizeof(y2);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = z2;
+ kexport.buffer = y2;
+ kexport.buffer_size = sizeof(y2);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ y2_size = ioctl(cfd, NCRIO_KEY_EXPORT, &kexport);
+ if (y2_size < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
- y2_size = keydata.idata_size;
if (y1_size == 0 || y1_size != y2_size || memcmp(y1, y2, y1_size) != 0) {
int i;
@@ -524,14 +597,232 @@ struct ncr_key_derivation_params_st kderive;
return 0;
}
+/* check whether wrapping of long keys is not allowed with
+ * shorted wrapping keys */
+static int
+test_ncr_wrap_key3(int cfd)
+{
+ int ret, i;
+ ncr_key_t key;
+ size_t data_size;
+ struct __attribute__((packed)) {
+ struct ncr_key_import f;
+ struct nlattr id_head ALIGN_NL;
+ uint8_t id[2] ALIGN_NL;
+ struct nlattr type_head ALIGN_NL;
+ uint32_t type ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_AES_CBC)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ } kimport;
+ struct __attribute__((packed)) {
+ struct ncr_key_wrap f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(NCR_WALG_AES_RFC5649)] ALIGN_NL;
+ } kwrap;
+ struct __attribute__((packed)) {
+ struct ncr_key_unwrap f;
+ struct nlattr wrap_algo_head ALIGN_NL;
+ char wrap_algo[sizeof(NCR_WALG_AES_RFC5649)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ } kunwrap;
+ struct __attribute__((packed)) {
+ struct ncr_key_generate_pair f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_RSA)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ struct nlattr bits_head ALIGN_NL;
+ uint32_t bits ALIGN_NL;
+ } kgen;
+ ncr_key_t pubkey, privkey;
+ uint8_t data[DATA_SIZE];
+ /* only the first two should be allowed to be wrapped.
+ * the latter shouldn't because it has security level larger
+ * then 128 bits (the size of the wrapping key).
+ */
+ const int sizes[] = {1024, 3248, 5200};
+
+ fprintf(stdout, "Tests on key wrapping (might take long): ");
+ fflush(stdout);
+
+ /* convert it to key */
+ privkey = ioctl(cfd, NCRIO_KEY_INIT);
+ if (privkey == -1) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_KEY_INIT)");
+ return 1;
+ }
+
+ pubkey = ioctl(cfd, NCRIO_KEY_INIT);
+ if (pubkey == -1) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_KEY_INIT)");
+ return 1;
+ }
+
+ if (geteuid() != 0) {
+ /* cannot test further */
+ fprintf(stdout, "\t(Wrapping test not completed. Run as root)\n");
+ return 0;
+ }
+
+ /* make a wrapping key */
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
+ perror("ioctl(NCRIO_KEY_INIT)");
+ return 1;
+ }
+
+ memset(&kimport.f, 0, sizeof(kimport.f));
+ kimport.f.input_size = sizeof(kimport);
+ kimport.f.key = key;
+ kimport.f.data = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F";
+ kimport.f.data_size = 16;
+ kimport.id_head.nla_len = NLA_HDRLEN + sizeof(kimport.id);
+ kimport.id_head.nla_type = NCR_ATTR_KEY_ID;
+ kimport.id[0] = 'a';
+ kimport.id[1] = 'b';
+ kimport.type_head.nla_len = NLA_HDRLEN + sizeof(kimport.type);
+ kimport.type_head.nla_type = NCR_ATTR_KEY_TYPE;
+ kimport.type = NCR_KEY_TYPE_SECRET;
+ kimport.algo_head.nla_len = NLA_HDRLEN + sizeof(kimport.algo);
+ kimport.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kimport.algo, ALG_AES_CBC);
+ kimport.flags_head.nla_len = NLA_HDRLEN + sizeof(kimport.flags);
+ kimport.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kimport.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPING|NCR_KEY_FLAG_UNWRAPPING;
+
+ if (ioctl(cfd, NCRIO_KEY_IMPORT, &kimport)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_KEY_IMPORT)");
+ return 1;
+ }
+
+ for (i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
+
+ fprintf(stdout, ".");
+ fflush(stdout);
+
+ memset(&kgen.f, 0, sizeof(kgen.f));
+ kgen.f.input_size = sizeof(kgen);
+ kgen.f.private_key = privkey;
+ kgen.f.public_key = pubkey;
+ kgen.algo_head.nla_len = NLA_HDRLEN + sizeof(kgen.algo);
+ kgen.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kgen.algo, ALG_RSA);
+ kgen.flags_head.nla_len = NLA_HDRLEN + sizeof(kgen.flags);
+ kgen.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kgen.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
+ kgen.bits_head.nla_len = NLA_HDRLEN + sizeof(kgen.bits);
+ kgen.bits_head.nla_type = NCR_ATTR_RSA_MODULUS_BITS;
+ kgen.bits = sizes[i];
+
+ if (ioctl(cfd, NCRIO_KEY_GENERATE_PAIR, &kgen)) {
+ fprintf(stderr, "Error[%d-%d]: %s:%d\n", i, sizes[i], __func__, __LINE__);
+ perror("ioctl(NCRIO_KEY_GENERATE_PAIR)");
+ return 1;
+ }
+
+ /* now try wrapping key2 using key */
+ memset(&kwrap.f, 0, sizeof(kwrap.f));
+ kwrap.f.input_size = sizeof(kwrap);
+ kwrap.f.wrapping_key = key;
+ kwrap.f.source_key = pubkey;
+ kwrap.f.buffer = data;
+ kwrap.f.buffer_size = sizeof(data);
+ kwrap.algo_head.nla_len = NLA_HDRLEN + sizeof(kwrap.algo);
+ kwrap.algo_head.nla_type = NCR_ATTR_WRAPPING_ALGORITHM;
+ strcpy(kwrap.algo, NCR_WALG_AES_RFC5649);
+
+ ret = ioctl(cfd, NCRIO_KEY_WRAP, &kwrap);
+ if (ret < 0) {
+ fprintf(stderr, "Error[%d-%d]: %s:%d\n", i, sizes[i], __func__, __LINE__);
+ /* wrapping of public key should have been allowed! */
+ return 1;
+ }
+
+ /* now try wrapping private using key */
+ memset(&kwrap.f, 0, sizeof(kwrap.f));
+ kwrap.f.input_size = sizeof(kwrap);
+ kwrap.f.wrapping_key = key;
+ kwrap.f.source_key = privkey;
+ kwrap.f.buffer = data;
+ kwrap.f.buffer_size = sizeof(data);
+ kwrap.algo_head.nla_len = NLA_HDRLEN + sizeof(kwrap.algo);
+ kwrap.algo_head.nla_type = NCR_ATTR_WRAPPING_ALGORITHM;
+ strcpy(kwrap.algo, NCR_WALG_AES_RFC5649);
+
+ ret = ioctl(cfd, NCRIO_KEY_WRAP, &kwrap);
+ if (ret < 0 && i != 2) {
+ fprintf(stderr, "Error[%d-%d]: %s:%d\n", i, sizes[i], __func__, __LINE__);
+ /* wrapping should have been allowed */
+ return 1;
+ } else if (ret >= 0 && i == 2) {
+ fprintf(stderr, "Error[%d-%d]: %s:%d\n", i, sizes[i], __func__, __LINE__);
+ /* wrapping shouldn't have been allowed */
+ return 1;
+ }
+
+ if (ret >= 0) {
+ data_size = ret;
+
+ /* try unwrapping */
+ memset(&kunwrap.f, 0, sizeof(kunwrap.f));
+ kunwrap.f.input_size = sizeof(kunwrap);
+ kunwrap.f.wrapping_key = key;
+ kunwrap.f.dest_key = privkey;
+ kunwrap.f.data = data;
+ kunwrap.f.data_size = data_size;
+ kunwrap.wrap_algo_head.nla_len
+ = NLA_HDRLEN + sizeof(kunwrap.wrap_algo);
+ kunwrap.wrap_algo_head.nla_type
+ = NCR_ATTR_WRAPPING_ALGORITHM;
+ strcpy(kunwrap.wrap_algo, NCR_WALG_AES_RFC5649);
+ kunwrap.flags_head.nla_len
+ = NLA_HDRLEN + sizeof(kunwrap.flags);
+ kunwrap.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kunwrap.flags = 0;
+
+ ret = ioctl(cfd, NCRIO_KEY_UNWRAP, &kunwrap);
+ if (ret) {
+ fprintf(stderr, "Error[%d-%d]: %s:%d\n", i, sizes[i], __func__, __LINE__);
+ return 1;
+ }
+ }
+ fprintf(stdout, "*");
+ fflush(stdout);
+
+ }
+
+ fprintf(stdout, " Success\n");
+ return 0;
+}
+
#define RSA_ENCRYPT_SIZE 32
static int rsa_key_encrypt(int cfd, ncr_key_t privkey, ncr_key_t pubkey, int oaep)
{
- struct ncr_session_once_op_st nop;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_RSA)] ALIGN_NL;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr rsa_head ALIGN_NL;
+ uint32_t rsa ALIGN_NL;
+ struct nlattr oaep_hash_head ALIGN_NL;
+ char oaep_hash[sizeof(SIGNATURE_HASH)] ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr output_head ALIGN_NL;
+ struct ncr_session_output_buffer output ALIGN_NL;
+ } op;
uint8_t data[DATA_SIZE];
uint8_t vdata[RSA_ENCRYPT_SIZE];
- int enc_size;
+ size_t enc_size, dec_size;
fprintf(stdout, "Tests on RSA (%s) key encryption:", (oaep!=0)?"OAEP":"PKCS V1.5");
fflush(stdout);
@@ -540,55 +831,79 @@ static int rsa_key_encrypt(int cfd, ncr_key_t privkey, ncr_key_t pubkey, int oae
memcpy(vdata, data, sizeof(vdata));
/* do encryption */
- memset(&nop, 0, sizeof(nop));
- nop.init.algorithm = NCR_ALG_RSA;
- nop.init.key = pubkey;
+ memset(&op.f, 0, sizeof(op.f));
+ op.f.input_size = sizeof(op);
+ op.f.op = NCR_OP_ENCRYPT;
+ op.algo_head.nla_len = NLA_HDRLEN + sizeof(op.algo);
+ op.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(op.algo, ALG_RSA);
+ op.key_head.nla_len = NLA_HDRLEN + sizeof(op.key);
+ op.key_head.nla_type = NCR_ATTR_KEY;
+ op.key = pubkey;
+ op.rsa_head.nla_len = NLA_HDRLEN + sizeof(op.rsa);
+ op.rsa_head.nla_type = NCR_ATTR_RSA_ENCODING_METHOD;
if (oaep) {
- nop.init.params.params.rsa.type = RSA_PKCS1_OAEP;
- nop.init.params.params.rsa.oaep_hash = NCR_ALG_SHA1;
+ op.rsa = RSA_PKCS1_OAEP;
} else {
- nop.init.params.params.rsa.type = RSA_PKCS1_V1_5;
+ op.rsa = RSA_PKCS1_V1_5;
}
- nop.init.op = NCR_OP_ENCRYPT;
- nop.op.data.udata.input = data;
- nop.op.data.udata.input_size = RSA_ENCRYPT_SIZE;
- nop.op.data.udata.output = data;
- nop.op.data.udata.output_size = sizeof(data);
- nop.op.type = NCR_DIRECT_DATA;
-
- if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) {
+ op.oaep_hash_head.nla_len = NLA_HDRLEN + sizeof(op.oaep_hash);
+ op.oaep_hash_head.nla_type = NCR_ATTR_RSA_OAEP_HASH_ALGORITHM;
+ strcpy(op.oaep_hash, SIGNATURE_HASH); /* Ignored if not using OAEP */
+ op.input_head.nla_len = NLA_HDRLEN + sizeof(op.input);
+ op.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ op.input.data = data;
+ op.input.data_size = RSA_ENCRYPT_SIZE;
+ op.output_head.nla_len = NLA_HDRLEN + sizeof(op.output);
+ op.output_head.nla_type = NCR_ATTR_UPDATE_OUTPUT_BUFFER;
+ op.output.buffer = data;
+ op.output.buffer_size = sizeof(data);
+ op.output.result_size_ptr = &enc_size;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &op)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_ONCE)");
return 1;
}
-
- enc_size = nop.op.data.udata.output_size;
/* decrypt data */
- memset(&nop, 0, sizeof(nop));
- nop.init.algorithm = NCR_ALG_RSA;
- nop.init.key = privkey;
- nop.init.op = NCR_OP_DECRYPT;
+ memset(&op.f, 0, sizeof(op.f));
+ op.f.input_size = sizeof(op);
+ op.f.op = NCR_OP_DECRYPT;
+ op.algo_head.nla_len = NLA_HDRLEN + sizeof(op.algo);
+ op.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(op.algo, ALG_RSA);
+ op.key_head.nla_len = NLA_HDRLEN + sizeof(op.key);
+ op.key_head.nla_type = NCR_ATTR_KEY;
+ op.key = privkey;
+ op.rsa_head.nla_len = NLA_HDRLEN + sizeof(op.rsa);
+ op.rsa_head.nla_type = NCR_ATTR_RSA_ENCODING_METHOD;
if (oaep) {
- nop.init.params.params.rsa.type = RSA_PKCS1_OAEP;
- nop.init.params.params.rsa.oaep_hash = NCR_ALG_SHA1;
+ op.rsa = RSA_PKCS1_OAEP;
} else {
- nop.init.params.params.rsa.type = RSA_PKCS1_V1_5;
+ op.rsa = RSA_PKCS1_V1_5;
}
- nop.op.data.udata.input = data;
- nop.op.data.udata.input_size = enc_size;
- nop.op.data.udata.output = data;
- nop.op.data.udata.output_size = sizeof(data);
- nop.op.type = NCR_DIRECT_DATA;
-
-
- if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) {
+ op.oaep_hash_head.nla_len = NLA_HDRLEN + sizeof(op.oaep_hash);
+ op.oaep_hash_head.nla_type = NCR_ATTR_RSA_OAEP_HASH_ALGORITHM;
+ strcpy(op.oaep_hash, SIGNATURE_HASH); /* Ignored if not using OAEP */
+ op.input_head.nla_len = NLA_HDRLEN + sizeof(op.input);
+ op.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ op.input.data = data;
+ op.input.data_size = enc_size;
+ op.output_head.nla_len = NLA_HDRLEN + sizeof(op.output);
+ op.output_head.nla_type = NCR_ATTR_UPDATE_OUTPUT_BUFFER;
+ op.output.buffer = data;
+ op.output.buffer_size = sizeof(data);
+ op.output.result_size_ptr = &dec_size;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &op)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_ONCE)");
return 1;
}
- if (memcmp(vdata, data, sizeof(vdata)) != 0) {
+ if (dec_size != sizeof(vdata)
+ || memcmp(vdata, data, sizeof(vdata)) != 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
fprintf(stderr, "Decrypted data do not match!\n");
return 1;
@@ -604,61 +919,234 @@ static int rsa_key_encrypt(int cfd, ncr_key_t privkey, ncr_key_t pubkey, int oae
static int rsa_key_sign_verify(int cfd, ncr_key_t privkey, ncr_key_t pubkey, int pss)
{
- struct ncr_session_once_op_st nop;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_RSA)] ALIGN_NL;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr rsa_head ALIGN_NL;
+ uint32_t rsa ALIGN_NL;
+ struct nlattr sign_hash_head ALIGN_NL;
+ char sign_hash[sizeof(SIGNATURE_HASH)] ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr signature_head ALIGN_NL;
+ struct ncr_session_output_buffer signature ALIGN_NL;
+ } ksign;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_RSA)] ALIGN_NL;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr rsa_head ALIGN_NL;
+ uint32_t rsa ALIGN_NL;
+ struct nlattr sign_hash_head ALIGN_NL;
+ char sign_hash[sizeof(SIGNATURE_HASH)] ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr signature_head ALIGN_NL;
+ struct ncr_session_input_data signature ALIGN_NL;
+ } kverify;
uint8_t data[DATA_SIZE];
uint8_t sig[DATA_SIZE];
- int sig_size;
+ size_t sig_size;
+ int ret;
fprintf(stdout, "Tests on RSA (%s) key signature:", (pss!=0)?"PSS":"PKCS V1.5");
fflush(stdout);
memset(data, 0x3, sizeof(data));
- /* sign datad */
- memset(&nop, 0, sizeof(nop));
- nop.init.algorithm = NCR_ALG_RSA;
- nop.init.key = privkey;
- nop.init.params.params.rsa.type = (pss!=0)?RSA_PKCS1_PSS:RSA_PKCS1_V1_5;
- nop.init.params.params.rsa.sign_hash = NCR_ALG_SHA1;
+ /* sign data */
+ memset(&ksign.f, 0, sizeof(ksign.f));
+ ksign.f.input_size = sizeof(ksign);
+ ksign.f.op = NCR_OP_SIGN;
+ ksign.algo_head.nla_len = NLA_HDRLEN + sizeof(ksign.algo);
+ ksign.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(ksign.algo, ALG_RSA);
+ ksign.key_head.nla_len = NLA_HDRLEN + sizeof(ksign.key);
+ ksign.key_head.nla_type = NCR_ATTR_KEY;
+ ksign.key = privkey;
+ ksign.rsa_head.nla_len = NLA_HDRLEN + sizeof(ksign.rsa);
+ ksign.rsa_head.nla_type = NCR_ATTR_RSA_ENCODING_METHOD;
+ ksign.rsa = (pss != 0) ? RSA_PKCS1_PSS : RSA_PKCS1_V1_5;
+ ksign.sign_hash_head.nla_len = NLA_HDRLEN + sizeof(ksign.sign_hash);
+ ksign.sign_hash_head.nla_type = NCR_ATTR_SIGNATURE_HASH_ALGORITHM;
+ strcpy(ksign.sign_hash, SIGNATURE_HASH);
+ ksign.input_head.nla_len = NLA_HDRLEN + sizeof(ksign.input);
+ ksign.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ ksign.input.data = data;
+ ksign.input.data_size = DATA_TO_SIGN;
+ ksign.signature_head.nla_len = NLA_HDRLEN + sizeof(ksign.signature);
+ ksign.signature_head.nla_type = NCR_ATTR_FINAL_OUTPUT_BUFFER;
+ ksign.signature.buffer = sig;
+ ksign.signature.buffer_size = sizeof(sig);
+ ksign.signature.result_size_ptr = &sig_size;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &ksign)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_SESSION_ONCE)");
+ return 1;
+ }
- nop.init.op = NCR_OP_SIGN;
- nop.op.data.udata.input = data;
- nop.op.data.udata.input_size = DATA_TO_SIGN;
- nop.op.data.udata.output = sig;
- nop.op.data.udata.output_size = sizeof(sig);
- nop.op.type = NCR_DIRECT_DATA;
+ /* verify signature */
+ memset(data, 0x3, sizeof(data));
- if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) {
+ memset(&kverify.f, 0, sizeof(kverify.f));
+ kverify.f.input_size = sizeof(kverify);
+ kverify.f.op = NCR_OP_VERIFY;
+ kverify.algo_head.nla_len = NLA_HDRLEN + sizeof(kverify.algo);
+ kverify.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kverify.algo, ALG_RSA);
+ kverify.key_head.nla_len = NLA_HDRLEN + sizeof(kverify.key);
+ kverify.key_head.nla_type = NCR_ATTR_KEY;
+ kverify.key = pubkey;
+ kverify.rsa_head.nla_len = NLA_HDRLEN + sizeof(kverify.rsa);
+ kverify.rsa_head.nla_type = NCR_ATTR_RSA_ENCODING_METHOD;
+ kverify.rsa = (pss != 0) ? RSA_PKCS1_PSS : RSA_PKCS1_V1_5;
+ kverify.sign_hash_head.nla_len = NLA_HDRLEN + sizeof(kverify.sign_hash);
+ kverify.sign_hash_head.nla_type = NCR_ATTR_SIGNATURE_HASH_ALGORITHM;
+ strcpy(kverify.sign_hash, SIGNATURE_HASH);
+ kverify.input_head.nla_len = NLA_HDRLEN + sizeof(kverify.input);
+ kverify.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ kverify.input.data = data;
+ kverify.input.data_size = DATA_TO_SIGN;
+ kverify.signature_head.nla_len = NLA_HDRLEN + sizeof(kverify.signature);
+ kverify.signature_head.nla_type = NCR_ATTR_FINAL_INPUT_DATA;
+ kverify.signature.data = sig;
+ kverify.signature.data_size = sig_size;
+
+ ret = ioctl(cfd, NCRIO_SESSION_ONCE, &kverify);
+ if (ret < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_ONCE)");
return 1;
}
-
- sig_size = nop.op.data.udata.output_size;
- /* verify signature */
- memset(&nop, 0, sizeof(nop));
- nop.init.algorithm = NCR_ALG_RSA;
- nop.init.key = pubkey;
- nop.init.params.params.rsa.type = (pss!=0)?RSA_PKCS1_PSS:RSA_PKCS1_V1_5;
- nop.init.params.params.rsa.sign_hash = NCR_ALG_SHA1;
+ if (ret)
+ fprintf(stdout, " Success\n");
+ else {
+ fprintf(stdout, " Verification Failed!\n");
+ return 1;
+ }
+
+ return 0;
+
+}
+
+static int rsa_key_sign_verify_transparent(int cfd, ncr_key_t privkey,
+ ncr_key_t pubkey, int pss)
+{
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(NCR_ALG_RSA_TRANSPARENT_HASH)] ALIGN_NL;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr rsa_head ALIGN_NL;
+ uint32_t rsa ALIGN_NL;
+ struct nlattr sign_hash_head ALIGN_NL;
+ char sign_hash[sizeof(SIGNATURE_HASH)] ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr signature_head ALIGN_NL;
+ struct ncr_session_output_buffer signature ALIGN_NL;
+ } ksign;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(NCR_ALG_RSA_TRANSPARENT_HASH)] ALIGN_NL;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr rsa_head ALIGN_NL;
+ uint32_t rsa ALIGN_NL;
+ struct nlattr sign_hash_head ALIGN_NL;
+ char sign_hash[sizeof(SIGNATURE_HASH)] ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr signature_head ALIGN_NL;
+ struct ncr_session_input_data signature ALIGN_NL;
+ } kverify;
+ uint8_t data[SIGNATURE_HASH_SIZE];
+ uint8_t sig[DATA_SIZE];
+ size_t sig_size;
+ int ret;
+
+ fprintf(stdout, "Tests on transparent RSA (%s) key signature:",
+ (pss != 0) ? "PSS" : "PKCS V1.5");
+ fflush(stdout);
memset(data, 0x3, sizeof(data));
- nop.init.op = NCR_OP_VERIFY;
- nop.op.data.udata.input = data;
- nop.op.data.udata.input_size = DATA_TO_SIGN;
- nop.op.data.udata.output = sig;
- nop.op.data.udata.output_size = sig_size;
- nop.op.type = NCR_DIRECT_DATA;
+ /* sign data */
+ memset(&ksign.f, 0, sizeof(ksign.f));
+ ksign.f.input_size = sizeof(ksign);
+ ksign.f.op = NCR_OP_SIGN;
+ ksign.algo_head.nla_len = NLA_HDRLEN + sizeof(ksign.algo);
+ ksign.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(ksign.algo, NCR_ALG_RSA_TRANSPARENT_HASH);
+ ksign.key_head.nla_len = NLA_HDRLEN + sizeof(ksign.key);
+ ksign.key_head.nla_type = NCR_ATTR_KEY;
+ ksign.key = privkey;
+ ksign.rsa_head.nla_len = NLA_HDRLEN + sizeof(ksign.rsa);
+ ksign.rsa_head.nla_type = NCR_ATTR_RSA_ENCODING_METHOD;
+ ksign.rsa = (pss != 0) ? RSA_PKCS1_PSS : RSA_PKCS1_V1_5;
+ ksign.sign_hash_head.nla_len = NLA_HDRLEN + sizeof(ksign.sign_hash);
+ ksign.sign_hash_head.nla_type = NCR_ATTR_SIGNATURE_HASH_ALGORITHM;
+ strcpy(ksign.sign_hash, SIGNATURE_HASH);
+ ksign.input_head.nla_len = NLA_HDRLEN + sizeof(ksign.input);
+ ksign.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ ksign.input.data = data;
+ ksign.input.data_size = SIGNATURE_HASH_SIZE;
+ ksign.signature_head.nla_len = NLA_HDRLEN + sizeof(ksign.signature);
+ ksign.signature_head.nla_type = NCR_ATTR_FINAL_OUTPUT_BUFFER;
+ ksign.signature.buffer = sig;
+ ksign.signature.buffer_size = sizeof(sig);
+ ksign.signature.result_size_ptr = &sig_size;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &ksign)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_SESSION_ONCE)");
+ return 1;
+ }
- if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) {
+ /* verify signature */
+ memset(data, 0x3, sizeof(data));
+
+ memset(&kverify.f, 0, sizeof(kverify.f));
+ kverify.f.input_size = sizeof(kverify);
+ kverify.f.op = NCR_OP_VERIFY;
+ kverify.algo_head.nla_len = NLA_HDRLEN + sizeof(kverify.algo);
+ kverify.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kverify.algo, NCR_ALG_RSA_TRANSPARENT_HASH);
+ kverify.key_head.nla_len = NLA_HDRLEN + sizeof(kverify.key);
+ kverify.key_head.nla_type = NCR_ATTR_KEY;
+ kverify.key = pubkey;
+ kverify.rsa_head.nla_len = NLA_HDRLEN + sizeof(kverify.rsa);
+ kverify.rsa_head.nla_type = NCR_ATTR_RSA_ENCODING_METHOD;
+ kverify.rsa = (pss != 0) ? RSA_PKCS1_PSS : RSA_PKCS1_V1_5;
+ kverify.sign_hash_head.nla_len = NLA_HDRLEN + sizeof(kverify.sign_hash);
+ kverify.sign_hash_head.nla_type = NCR_ATTR_SIGNATURE_HASH_ALGORITHM;
+ strcpy(kverify.sign_hash, SIGNATURE_HASH);
+ kverify.input_head.nla_len = NLA_HDRLEN + sizeof(kverify.input);
+ kverify.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ kverify.input.data = data;
+ kverify.input.data_size = SIGNATURE_HASH_SIZE;
+ kverify.signature_head.nla_len = NLA_HDRLEN + sizeof(kverify.signature);
+ kverify.signature_head.nla_type = NCR_ATTR_FINAL_INPUT_DATA;
+ kverify.signature.data = sig;
+ kverify.signature.data_size = sig_size;
+
+ ret = ioctl(cfd, NCRIO_SESSION_ONCE, &kverify);
+ if (ret < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_ONCE)");
return 1;
}
- if (nop.op.err == NCR_SUCCESS)
+ if (ret)
fprintf(stdout, " Success\n");
else {
fprintf(stdout, " Verification Failed!\n");
@@ -666,62 +1154,105 @@ static int rsa_key_sign_verify(int cfd, ncr_key_t privkey, ncr_key_t pubkey, int
}
return 0;
-
}
static int dsa_key_sign_verify(int cfd, ncr_key_t privkey, ncr_key_t pubkey)
{
- struct ncr_session_once_op_st nop;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_DSA)] ALIGN_NL;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr sign_hash_head ALIGN_NL;
+ char sign_hash[sizeof(SIGNATURE_HASH)] ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr signature_head ALIGN_NL;
+ struct ncr_session_output_buffer signature ALIGN_NL;
+ } ksign;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_DSA)] ALIGN_NL;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr sign_hash_head ALIGN_NL;
+ char sign_hash[sizeof(SIGNATURE_HASH)] ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr signature_head ALIGN_NL;
+ struct ncr_session_input_data signature ALIGN_NL;
+ } kverify;
uint8_t data[DATA_SIZE];
uint8_t sig[DATA_SIZE];
- int sig_size;
+ size_t sig_size;
+ int ret;
fprintf(stdout, "Tests on DSA key signature:");
fflush(stdout);
memset(data, 0x3, sizeof(data));
- /* sign datad */
- memset(&nop, 0, sizeof(nop));
- nop.init.algorithm = NCR_ALG_DSA;
- nop.init.key = privkey;
- nop.init.params.params.dsa.sign_hash = NCR_ALG_SHA1;
-
- nop.init.op = NCR_OP_SIGN;
- nop.op.data.udata.input = data;
- nop.op.data.udata.input_size = DATA_TO_SIGN;
- nop.op.data.udata.output = sig;
- nop.op.data.udata.output_size = sizeof(sig);
- nop.op.type = NCR_DIRECT_DATA;
-
- if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) {
+ /* sign data */
+ memset(&ksign.f, 0, sizeof(ksign.f));
+ ksign.f.input_size = sizeof(ksign);
+ ksign.f.op = NCR_OP_SIGN;
+ ksign.algo_head.nla_len = NLA_HDRLEN + sizeof(ksign.algo);
+ ksign.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(ksign.algo, ALG_DSA);
+ ksign.key_head.nla_len = NLA_HDRLEN + sizeof(ksign.key);
+ ksign.key_head.nla_type = NCR_ATTR_KEY;
+ ksign.key = privkey;
+ ksign.sign_hash_head.nla_len = NLA_HDRLEN + sizeof(ksign.sign_hash);
+ ksign.sign_hash_head.nla_type = NCR_ATTR_SIGNATURE_HASH_ALGORITHM;
+ strcpy(ksign.sign_hash, SIGNATURE_HASH);
+ ksign.input_head.nla_len = NLA_HDRLEN + sizeof(ksign.input);
+ ksign.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ ksign.input.data = data;
+ ksign.input.data_size = DATA_TO_SIGN;
+ ksign.signature_head.nla_len = NLA_HDRLEN + sizeof(ksign.signature);
+ ksign.signature_head.nla_type = NCR_ATTR_FINAL_OUTPUT_BUFFER;
+ ksign.signature.buffer = sig;
+ ksign.signature.buffer_size = sizeof(sig);
+ ksign.signature.result_size_ptr = &sig_size;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &ksign)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_ONCE)");
return 1;
}
-
- sig_size = nop.op.data.udata.output_size;
/* verify signature */
- memset(&nop, 0, sizeof(nop));
- nop.init.algorithm = NCR_ALG_DSA;
- nop.init.key = pubkey;
- nop.init.params.params.dsa.sign_hash = NCR_ALG_SHA1;
-
- nop.init.op = NCR_OP_VERIFY;
- nop.op.data.udata.input = data;
- nop.op.data.udata.input_size = DATA_TO_SIGN;
- nop.op.data.udata.output = sig;
- nop.op.data.udata.output_size = sizeof(sig);
- nop.op.type = NCR_DIRECT_DATA;
-
- if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) {
+ memset(&kverify.f, 0, sizeof(kverify.f));
+ kverify.f.input_size = sizeof(kverify);
+ kverify.f.op = NCR_OP_VERIFY;
+ kverify.algo_head.nla_len = NLA_HDRLEN + sizeof(kverify.algo);
+ kverify.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kverify.algo, ALG_DSA);
+ kverify.key_head.nla_len = NLA_HDRLEN + sizeof(kverify.key);
+ kverify.key_head.nla_type = NCR_ATTR_KEY;
+ kverify.key = pubkey;
+ kverify.sign_hash_head.nla_len = NLA_HDRLEN + sizeof(kverify.sign_hash);
+ kverify.sign_hash_head.nla_type = NCR_ATTR_SIGNATURE_HASH_ALGORITHM;
+ strcpy(kverify.sign_hash, SIGNATURE_HASH);
+ kverify.input_head.nla_len = NLA_HDRLEN + sizeof(kverify.input);
+ kverify.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ kverify.input.data = data;
+ kverify.input.data_size = DATA_TO_SIGN;
+ kverify.signature_head.nla_len = NLA_HDRLEN + sizeof(kverify.signature);
+ kverify.signature_head.nla_type = NCR_ATTR_FINAL_INPUT_DATA;
+ kverify.signature.data = sig;
+ kverify.signature.data_size = sizeof(sig);
+
+ ret = ioctl(cfd, NCRIO_SESSION_ONCE, &kverify);
+ if (ret < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_ONCE)");
return 1;
}
- if (nop.op.err == NCR_SUCCESS)
+ if (ret)
fprintf(stdout, " Success\n");
else {
fprintf(stdout, " Verification Failed!\n");
@@ -732,13 +1263,127 @@ static int dsa_key_sign_verify(int cfd, ncr_key_t privkey, ncr_key_t pubkey)
}
+static int dsa_key_sign_verify_transparent(int cfd, ncr_key_t privkey,
+ ncr_key_t pubkey)
+{
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(NCR_ALG_DSA_TRANSPARENT_HASH)] ALIGN_NL;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr sign_hash_head ALIGN_NL;
+ char sign_hash[sizeof(SIGNATURE_HASH)] ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr signature_head ALIGN_NL;
+ struct ncr_session_output_buffer signature ALIGN_NL;
+ } ksign;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(NCR_ALG_DSA_TRANSPARENT_HASH)] ALIGN_NL;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr sign_hash_head ALIGN_NL;
+ char sign_hash[sizeof(SIGNATURE_HASH)] ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr signature_head ALIGN_NL;
+ struct ncr_session_input_data signature ALIGN_NL;
+ } kverify;
+ uint8_t data[SIGNATURE_HASH_SIZE];
+ uint8_t sig[DATA_SIZE];
+ size_t sig_size;
+ int ret;
+
+ fprintf(stdout, "Tests on transparent DSA key signature:");
+ fflush(stdout);
+
+ memset(data, 0x3, sizeof(data));
+
+ /* sign data */
+ memset(&ksign.f, 0, sizeof(ksign.f));
+ ksign.f.input_size = sizeof(ksign);
+ ksign.f.op = NCR_OP_SIGN;
+ ksign.algo_head.nla_len = NLA_HDRLEN + sizeof(ksign.algo);
+ ksign.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(ksign.algo, NCR_ALG_DSA_TRANSPARENT_HASH);
+ ksign.key_head.nla_len = NLA_HDRLEN + sizeof(ksign.key);
+ ksign.key_head.nla_type = NCR_ATTR_KEY;
+ ksign.key = privkey;
+ ksign.sign_hash_head.nla_len = NLA_HDRLEN + sizeof(ksign.sign_hash);
+ ksign.sign_hash_head.nla_type = NCR_ATTR_SIGNATURE_HASH_ALGORITHM;
+ strcpy(ksign.sign_hash, SIGNATURE_HASH);
+ ksign.input_head.nla_len = NLA_HDRLEN + sizeof(ksign.input);
+ ksign.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ ksign.input.data = data;
+ ksign.input.data_size = SIGNATURE_HASH_SIZE;
+ ksign.signature_head.nla_len = NLA_HDRLEN + sizeof(ksign.signature);
+ ksign.signature_head.nla_type = NCR_ATTR_FINAL_OUTPUT_BUFFER;
+ ksign.signature.buffer = sig;
+ ksign.signature.buffer_size = sizeof(sig);
+ ksign.signature.result_size_ptr = &sig_size;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &ksign)) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_SESSION_ONCE)");
+ return 1;
+ }
+
+ /* verify signature */
+ memset(&kverify.f, 0, sizeof(kverify.f));
+ kverify.f.input_size = sizeof(kverify);
+ kverify.f.op = NCR_OP_VERIFY;
+ kverify.algo_head.nla_len = NLA_HDRLEN + sizeof(kverify.algo);
+ kverify.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kverify.algo, NCR_ALG_DSA_TRANSPARENT_HASH);
+ kverify.key_head.nla_len = NLA_HDRLEN + sizeof(kverify.key);
+ kverify.key_head.nla_type = NCR_ATTR_KEY;
+ kverify.key = pubkey;
+ kverify.sign_hash_head.nla_len = NLA_HDRLEN + sizeof(kverify.sign_hash);
+ kverify.sign_hash_head.nla_type = NCR_ATTR_SIGNATURE_HASH_ALGORITHM;
+ strcpy(kverify.sign_hash, SIGNATURE_HASH);
+ kverify.input_head.nla_len = NLA_HDRLEN + sizeof(kverify.input);
+ kverify.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ kverify.input.data = data;
+ kverify.input.data_size = SIGNATURE_HASH_SIZE;
+ kverify.signature_head.nla_len = NLA_HDRLEN + sizeof(kverify.signature);
+ kverify.signature_head.nla_type = NCR_ATTR_FINAL_INPUT_DATA;
+ kverify.signature.data = sig;
+ kverify.signature.data_size = sizeof(sig);
+
+ ret = ioctl(cfd, NCRIO_SESSION_ONCE, &kverify);
+ if (ret < 0) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ perror("ioctl(NCRIO_SESSION_ONCE)");
+ return 1;
+ }
+
+ if (ret)
+ fprintf(stdout, " Success\n");
+ else {
+ fprintf(stdout, " Verification Failed!\n");
+ return 1;
+ }
+
+ return 0;
+}
static int test_ncr_rsa(int cfd)
{
int ret;
- struct ncr_key_generate_st kgen;
+ struct __attribute__((packed)) {
+ struct ncr_key_generate_pair f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_RSA)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ struct nlattr bits_head ALIGN_NL;
+ uint32_t bits ALIGN_NL;
+ } kgen;
ncr_key_t pubkey, privkey;
- struct ncr_key_data_st keydata;
+ struct ncr_key_export kexport;
uint8_t data[DATA_SIZE];
int data_size;
@@ -746,24 +1391,33 @@ static int test_ncr_rsa(int cfd)
fflush(stdout);
/* convert it to key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &privkey)) {
+ privkey = ioctl(cfd, NCRIO_KEY_INIT);
+ if (privkey == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- if (ioctl(cfd, NCRIO_KEY_INIT, &pubkey)) {
+ pubkey = ioctl(cfd, NCRIO_KEY_INIT);
+ if (pubkey == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
memset(&kgen, 0, sizeof(kgen));
- kgen.desc = privkey;
- kgen.desc2 = pubkey;
- kgen.params.algorithm = NCR_ALG_RSA;
- kgen.params.keyflags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
- kgen.params.params.rsa.bits = 1024;
+ kgen.f.input_size = sizeof(kgen);
+ kgen.f.private_key = privkey;
+ kgen.f.public_key = pubkey;
+ kgen.algo_head.nla_len = NLA_HDRLEN + sizeof(kgen.algo);
+ kgen.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kgen.algo, ALG_RSA);
+ kgen.flags_head.nla_len = NLA_HDRLEN + sizeof(kgen.flags);
+ kgen.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kgen.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
+ kgen.bits_head.nla_len = NLA_HDRLEN + sizeof(kgen.bits);
+ kgen.bits_head.nla_type = NCR_ATTR_RSA_MODULUS_BITS;
+ kgen.bits = 1024;
if (ioctl(cfd, NCRIO_KEY_GENERATE_PAIR, &kgen)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
@@ -773,18 +1427,17 @@ static int test_ncr_rsa(int cfd)
/* export the private key */
memset(data, 0, sizeof(data));
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = privkey;
- keydata.idata = data;
- keydata.idata_size = sizeof(data);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = privkey;
+ kexport.buffer = data;
+ kexport.buffer_size = sizeof(data);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ data_size = ioctl(cfd, NCRIO_KEY_EXPORT, &kexport);
+ if (data_size < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
-
- data_size = keydata.idata_size;
ret = privkey_info(data, data_size, 0);
if (ret != 0) {
@@ -795,18 +1448,17 @@ static int test_ncr_rsa(int cfd)
/* export the public key */
memset(data, 0, sizeof(data));
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = pubkey;
- keydata.idata = data;
- keydata.idata_size = sizeof(data);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = pubkey;
+ kexport.buffer = data;
+ kexport.buffer_size = sizeof(data);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ data_size = ioctl(cfd, NCRIO_KEY_EXPORT, &kexport);
+ if (data_size < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_IMPORT)");
+ perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
-
- data_size = keydata.idata_size;
ret = pubkey_info(data, data_size, 0);
if (ret != 0) {
@@ -828,6 +1480,18 @@ static int test_ncr_rsa(int cfd)
return 1;
}
+ ret = rsa_key_sign_verify_transparent(cfd, privkey, pubkey, 1);
+ if (ret != 0) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ return 1;
+ }
+
+ ret = rsa_key_sign_verify_transparent(cfd, privkey, pubkey, 0);
+ if (ret != 0) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ return 1;
+ }
+
ret = rsa_key_encrypt(cfd, privkey, pubkey, 0);
if (ret != 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
@@ -847,9 +1511,19 @@ static int test_ncr_rsa(int cfd)
static int test_ncr_dsa(int cfd)
{
int ret;
- struct ncr_key_generate_st kgen;
+ struct __attribute__((packed)) {
+ struct ncr_key_generate_pair f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_DSA)] ALIGN_NL;
+ struct nlattr flags_head ALIGN_NL;
+ uint32_t flags ALIGN_NL;
+ struct nlattr q_bits_head ALIGN_NL;
+ uint32_t q_bits ALIGN_NL;
+ struct nlattr p_bits_head ALIGN_NL;
+ uint32_t p_bits ALIGN_NL;
+ } kgen;
ncr_key_t pubkey, privkey;
- struct ncr_key_data_st keydata;
+ struct ncr_key_export kexport;
uint8_t data[DATA_SIZE];
int data_size;
@@ -857,25 +1531,36 @@ static int test_ncr_dsa(int cfd)
fflush(stdout);
/* convert it to key */
- if (ioctl(cfd, NCRIO_KEY_INIT, &privkey)) {
+ privkey = ioctl(cfd, NCRIO_KEY_INIT);
+ if (privkey == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- if (ioctl(cfd, NCRIO_KEY_INIT, &pubkey)) {
+ pubkey = ioctl(cfd, NCRIO_KEY_INIT);
+ if (pubkey == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
memset(&kgen, 0, sizeof(kgen));
- kgen.desc = privkey;
- kgen.desc2 = pubkey;
- kgen.params.algorithm = NCR_ALG_DSA;
- kgen.params.keyflags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
- kgen.params.params.dsa.q_bits = 160;
- kgen.params.params.dsa.p_bits = 1024;
+ kgen.f.input_size = sizeof(kgen);
+ kgen.f.private_key = privkey;
+ kgen.f.public_key = pubkey;
+ kgen.algo_head.nla_len = NLA_HDRLEN + sizeof(kgen.algo);
+ kgen.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kgen.algo, ALG_DSA);
+ kgen.flags_head.nla_len = NLA_HDRLEN + sizeof(kgen.flags);
+ kgen.flags_head.nla_type = NCR_ATTR_KEY_FLAGS;
+ kgen.flags = NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE;
+ kgen.q_bits_head.nla_len = NLA_HDRLEN + sizeof(kgen.q_bits);
+ kgen.q_bits_head.nla_type = NCR_ATTR_DSA_Q_BITS;
+ kgen.q_bits = 160;
+ kgen.p_bits_head.nla_len = NLA_HDRLEN + sizeof(kgen.p_bits);
+ kgen.p_bits_head.nla_type = NCR_ATTR_DSA_P_BITS;
+ kgen.p_bits = 1024;
if (ioctl(cfd, NCRIO_KEY_GENERATE_PAIR, &kgen)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
@@ -883,18 +1568,18 @@ static int test_ncr_dsa(int cfd)
return 1;
}
- memset(&keydata, 0, sizeof(keydata));
memset(data, 0, sizeof(data));
- keydata.key = privkey;
- keydata.idata = data;
- keydata.idata_size = sizeof(data);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = privkey;
+ kexport.buffer = data;
+ kexport.buffer_size = sizeof(data);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ data_size = ioctl(cfd, NCRIO_KEY_EXPORT, &kexport);
+ if (data_size < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
- data_size = keydata.idata_size;
ret = privkey_info(data, data_size, 0);
if (ret != 0) {
@@ -905,18 +1590,17 @@ static int test_ncr_dsa(int cfd)
/* export the public key */
memset(data, 0, sizeof(data));
- memset(&keydata, 0, sizeof(keydata));
- keydata.key = pubkey;
- keydata.idata = data;
- keydata.idata_size = sizeof(data);
+ memset(&kexport, 0, sizeof(kexport));
+ kexport.key = pubkey;
+ kexport.buffer = data;
+ kexport.buffer_size = sizeof(data);
- if (ioctl(cfd, NCRIO_KEY_EXPORT, &keydata)) {
+ data_size = ioctl(cfd, NCRIO_KEY_EXPORT, &kexport);
+ if (data_size < 0) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_IMPORT)");
+ perror("ioctl(NCRIO_KEY_EXPORT)");
return 1;
}
-
- data_size = keydata.idata_size;
ret = pubkey_info(data, data_size, 0);
if (ret != 0) {
@@ -932,6 +1616,12 @@ static int test_ncr_dsa(int cfd)
return 1;
}
+ ret = dsa_key_sign_verify_transparent(cfd, privkey, pubkey);
+ if (ret != 0) {
+ fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
+ return 1;
+ }
+
return 0;
}
@@ -961,6 +1651,9 @@ main()
if (test_ncr_dsa(fd))
return 1;
+
+ if (test_ncr_wrap_key3(fd))
+ return 1;
/* Close the original descriptor */
if (close(fd)) {
diff --git a/examples/speed.c b/examples/speed.c
index 5898aaae76e..70a2ed851c3 100644
--- a/examples/speed.c
+++ b/examples/speed.c
@@ -17,6 +17,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <fcntl.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -24,11 +25,15 @@
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/socket.h>
#include <signal.h>
#include <unistd.h>
-#include "../cryptodev.h"
+#include <linux/netlink.h>
#include "../ncr.h"
+#define ALIGN_NL __attribute__((aligned(NLA_ALIGNTO)))
+#define ALG_AES_CBC "cbc(aes)"
+
static double udifftimeval(struct timeval start, struct timeval end)
{
return (double)(end.tv_usec - start.tv_usec) +
@@ -68,56 +73,7 @@ static void value2human(double bytes, double time, double* data, double* speed,c
}
-int encrypt_data(struct session_op *sess, int fdc, int chunksize)
-{
- struct crypt_op cop;
- char *buffer, iv[32];
- static int val = 23;
- struct timeval start, end;
- double total = 0;
- double secs, ddata, dspeed;
- char metric[16];
-
- buffer = malloc(chunksize);
- memset(iv, 0x23, 32);
-
- printf("\tEncrypting in chunks of %d bytes: ", chunksize);
- fflush(stdout);
-
- memset(buffer, val++, chunksize);
-
- must_finish = 0;
- alarm(5);
-
- gettimeofday(&start, NULL);
- do {
- memset(&cop, 0, sizeof(cop));
- cop.ses = sess->ses;
- cop.len = chunksize;
- cop.iv = (unsigned char *)iv;
- cop.op = COP_ENCRYPT;
- cop.flags = 0;
- cop.src = cop.dst = (unsigned char *)buffer;
-
- if (ioctl(fdc, CIOCCRYPT, &cop)) {
- perror("ioctl(CIOCCRYPT)");
- return 1;
- }
- total+=chunksize;
- } while(must_finish==0);
- gettimeofday(&end, NULL);
-
- secs = udifftimeval(start, end)/ 1000000.0;
-
- value2human(total, secs, &ddata, &dspeed, metric);
- printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
- printf ("%.2f %s/sec\n", dspeed, metric);
-
- return 0;
-}
-
-
-int encrypt_data_ncr_direct(int cfd, int algo, int chunksize)
+int encrypt_data_ncr_direct(int cfd, const char *algo, int chunksize)
{
char *buffer, iv[32];
static int val = 23;
@@ -126,23 +82,48 @@ int encrypt_data_ncr_direct(int cfd, int algo, int chunksize)
double secs, ddata, dspeed;
char metric[16];
ncr_key_t key;
- struct ncr_key_generate_st kgen;
- struct ncr_session_once_op_st nop;
-
- if (ioctl(cfd, NCRIO_KEY_INIT, &key)) {
+ struct __attribute__((packed)) {
+ struct ncr_key_generate f;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[sizeof(ALG_AES_CBC)] ALIGN_NL;
+ struct nlattr bits_head ALIGN_NL;
+ uint32_t bits ALIGN_NL;
+ } kgen;
+ struct __attribute__((packed)) {
+ struct ncr_session_once f;
+ struct nlattr key_head ALIGN_NL;
+ uint32_t key ALIGN_NL;
+ struct nlattr input_head ALIGN_NL;
+ struct ncr_session_input_data input ALIGN_NL;
+ struct nlattr output_head ALIGN_NL;
+ struct ncr_session_output_buffer output ALIGN_NL;
+ struct nlattr iv_head ALIGN_NL;
+ struct nlattr algo_head ALIGN_NL;
+ char algo[128] ALIGN_NL;
+ } op;
+ size_t algo_size;
+
+ algo_size = strlen(algo) + 1;
+ key = ioctl(cfd, NCRIO_KEY_INIT);
+ if (key == -1) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_KEY_INIT)");
return 1;
}
- kgen.desc = key;
- kgen.params.algorithm = NCR_ALG_AES_CBC;
- kgen.params.keyflags = NCR_KEY_FLAG_EXPORTABLE;
- kgen.params.params.secret.bits = 128; /* 16 bytes */
-
+ memset(&kgen.f, 0, sizeof(kgen.f));
+ kgen.f.input_size = sizeof(kgen);
+ kgen.f.key = key;
+ kgen.algo_head.nla_len = NLA_HDRLEN + sizeof(kgen.algo);
+ kgen.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ strcpy(kgen.algo, ALG_AES_CBC);
+ kgen.bits_head.nla_len = NLA_HDRLEN + sizeof(kgen.bits);
+ kgen.bits_head.nla_type = NCR_ATTR_SECRET_KEY_BITS;
+ kgen.bits = 128; /* 16 bytes */
+
if (ioctl(cfd, NCRIO_KEY_GENERATE, &kgen)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
- perror("ioctl(NCRIO_KEY_IMPORT)");
+ perror("ioctl(NCRIO_KEY_GENERATE)");
return 1;
}
@@ -160,17 +141,30 @@ int encrypt_data_ncr_direct(int cfd, int algo, int chunksize)
gettimeofday(&start, NULL);
do {
- memset(&nop, 0, sizeof(nop));
- nop.init.algorithm = algo;
- nop.init.key = key;
- nop.init.op = NCR_OP_ENCRYPT;
- nop.op.data.udata.input = buffer;
- nop.op.data.udata.input_size = chunksize;
- nop.op.data.udata.output = buffer;
- nop.op.data.udata.output_size = chunksize;
- nop.op.type = NCR_DIRECT_DATA;
-
- if (ioctl(cfd, NCRIO_SESSION_ONCE, &nop)) {
+ size_t output_size;
+
+ memset(&op.f, 0, sizeof(op.f));
+ op.f.op = NCR_OP_ENCRYPT;
+ op.key_head.nla_len = NLA_HDRLEN + sizeof(op.key);
+ op.key_head.nla_type = NCR_ATTR_KEY;
+ op.key = key;
+ op.input_head.nla_len = NLA_HDRLEN + sizeof(op.input);
+ op.input_head.nla_type = NCR_ATTR_UPDATE_INPUT_DATA;
+ op.input.data = buffer;
+ op.input.data_size = chunksize;
+ op.output_head.nla_len = NLA_HDRLEN + sizeof(op.output);
+ op.output_head.nla_type = NCR_ATTR_UPDATE_OUTPUT_BUFFER;
+ op.output.buffer = buffer;
+ op.output.buffer_size = chunksize;
+ op.output.result_size_ptr = &output_size;
+ op.iv_head.nla_len = NLA_HDRLEN + 0;
+ op.iv_head.nla_type = NCR_ATTR_IV;
+ op.algo_head.nla_len = NLA_HDRLEN + algo_size;
+ op.algo_head.nla_type = NCR_ATTR_ALGORITHM;
+ memcpy(op.algo, algo, algo_size);
+ op.f.input_size = op.algo + algo_size - (char *)&op;
+
+ if (ioctl(cfd, NCRIO_SESSION_ONCE, &op)) {
fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);
perror("ioctl(NCRIO_SESSION_ONCE)");
return 1;
@@ -191,9 +185,7 @@ int encrypt_data_ncr_direct(int cfd, int algo, int chunksize)
int main(void)
{
- int fd, i, fdc = -1;
- struct session_op sess;
- char keybuf[32];
+ int fd, i;
signal(SIGALRM, alarm_handler);
@@ -201,57 +193,20 @@ int main(void)
perror("open()");
return 1;
}
- if (ioctl(fd, CRIOGET, &fdc)) {
- perror("ioctl(CRIOGET)");
- return 1;
- }
-
- fprintf(stderr, "Testing NULL cipher: \n");
- memset(&sess, 0, sizeof(sess));
- sess.cipher = CRYPTO_NULL;
- sess.keylen = 0;
- sess.key = (unsigned char *)keybuf;
- if (ioctl(fdc, CIOCGSESSION, &sess)) {
- perror("ioctl(CIOCGSESSION)");
- return 1;
- }
-
- for (i = 256; i <= (64 * 1024); i *= 2) {
- if (encrypt_data(&sess, fdc, i))
- break;
- }
fprintf(stderr, "\nTesting NCR-DIRECT with NULL cipher: \n");
for (i = 256; i <= (64 * 1024); i *= 2) {
- if (encrypt_data_ncr_direct(fdc, NCR_ALG_NULL, i))
+ if (encrypt_data_ncr_direct(fd, "ecb(cipher_null)", i))
break;
}
- fprintf(stderr, "\nTesting AES-128-CBC cipher: \n");
- memset(&sess, 0, sizeof(sess));
- sess.cipher = CRYPTO_AES_CBC;
- sess.keylen = 16;
- memset(keybuf, 0x42, 16);
- sess.key = (unsigned char *)keybuf;
- if (ioctl(fdc, CIOCGSESSION, &sess)) {
- perror("ioctl(CIOCGSESSION)");
- return 1;
- }
-
- for (i = 256; i <= (64 * 1024); i *= 2) {
- if (encrypt_data(&sess, fdc, i))
- break;
- }
-
fprintf(stderr, "\nTesting NCR-DIRECT with AES-128-CBC cipher: \n");
for (i = 256; i <= (64 * 1024); i *= 2) {
- if (encrypt_data_ncr_direct(fdc, NCR_ALG_AES_CBC, i))
+ if (encrypt_data_ncr_direct(fd, "cbc(aes)", i))
break;
}
-
- close(fdc);
close(fd);
return 0;
}