#include <crypto/hash.h>
#include <crypto/hmac.h>
-
+#define MAX_MSG_LEN 128
+#define AES_BLOCKLEN 16
+#define CBC_IVLEN 16
+#define CBC_KEYLEN 32
+#define CBC_AUTHKEYLEN 32
+#define CBC_AUTHLEN 32
static int hmac_sha256(unsigned char *key, size_t key_size, unsigned char *ikm, size_t ikm_len, unsigned char *okm, size_t okm_len){
struct crypto_shash *tfm;
struct shash_desc *shash;
int ret = -1;
-
tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
if (IS_ERR(tfm)) {
printk("hmac: crypto_alloc_shash failed\n");
return -1;
}
-
ret = crypto_shash_setkey(tfm, key, key_size);
if (ret) {
printk("hmac: crypto_ahash_setkey failed: %d", ret);
goto failed;
}
-
shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),GFP_KERNEL);
if (!shash) {
ret = -ENOMEM;
printk("hmac: zalloc: %d\n", ret);
goto failed;
- }
-
+ }
shash->tfm = tfm;
-
ret = crypto_shash_digest(shash, ikm, ikm_len, okm);
-
kfree(shash);
failed:
crypto_free_shash(tfm);
return ret;
-
}
static int run_kcrypt(void) {
-
+ char print_message[MAX_MSG_LEN] = {0};
char *test_data = "this is test data for kcrypt";
int test_datalen = 0;
int test_datapaddedlen = 0;
- int aes_blocklen = 16;
- int aes_cbc_noncelen = 16;
- int aes_cbc_authtrunclen = 32;
+ int aes_blocklen = AES_BLOCKLEN;
+ int aes_cbc_noncelen = CBC_IVLEN;
+ int aes_cbc_authlen = CBC_AUTHLEN;
+ size_t buffer_size = MAX_MSG_LEN;
int padtest = 0;
struct crypto_skcipher *tfm = NULL;
struct skcipher_request *req = NULL;
u8 *buffer = NULL;
u8 *buffer2 = NULL;
- size_t buffer_size = 1024;
- u8 *bp = NULL, *bp_end = NULL;
+ u8 *bp = NULL, *bp_end = NULL, *bp_print = NULL;
struct scatterlist sg = { 0 };
struct scatterlist sg2 = { 0 };
DECLARE_CRYPTO_WAIT(wait);
test_datapaddedlen = test_datalen + (aes_blocklen - padtest);
}
- u8 nonce[16] = {
+ u8 nonce[CBC_IVLEN] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
- u8 nonce_org[16] = {
+ u8 nonce_org[CBC_IVLEN] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0xaa, 0xbb,
};
- u8 auth_key[32] = {
+ u8 auth_key[CBC_AUTHKEYLEN] = {
0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
0x11, 0x22,
};
- u8 auth_val[32] = {
+ u8 auth_val[CBC_AUTHLEN] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
- u8 auth_val_org[32] = {
+ u8 auth_val_org[CBC_AUTHLEN] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, crypto_req_done, &wait);
- err = crypto_skcipher_setkey(tfm, key, 32);
+ err = crypto_skcipher_setkey(tfm, key, CBC_KEYLEN);
if (err != 0) {
printk("kcrypt: set key failed: %d.\n", err);
goto kcrypt_end;
printk("kcrypt: buffer alloc failed \n");
goto kcrypt_end;
}
+
memset(buffer, 0, buffer_size);
memset(buffer2, 0, buffer_size);
get_random_bytes(nonce, aes_cbc_noncelen);
sg_init_one(&sg2, buffer2, buffer_size);
memcpy(buffer, nonce, aes_cbc_noncelen);
memcpy(buffer + aes_cbc_noncelen, test_data, test_datalen);
- err = hmac_sha256(auth_key, 32, buffer, aes_cbc_noncelen + test_datapaddedlen, auth_val, aes_cbc_authtrunclen);
+
+ err = hmac_sha256(auth_key, CBC_AUTHKEYLEN, buffer, aes_cbc_noncelen + test_datapaddedlen, auth_val, aes_cbc_authlen);
if(err != 0){
- printk("kxfrm: hmac failed: %d\n", err);
+ printk("kcrypt: hmac failed: %d\n", err);
goto kcrypt_end;
}
- memcpy(auth_val_org, auth_val, aes_cbc_authtrunclen);
+
+ printk("kcrypt: auth val calculated for nonce + padded data\n");
+
+ memcpy(auth_val_org, auth_val, aes_cbc_authlen);
memset(buffer, 0, buffer_size);
memcpy(buffer, test_data, test_datalen);
+
+ memset(print_message, 0, MAX_MSG_LEN);
+ bp_print = print_message;
+ bp = auth_val_org;
+ bp_end = bp + aes_cbc_authlen;
+ while (bp != bp_end) {
+ *bp_print = isprint(*bp) ? *bp : '.';
+ bp++;
+ bp_print++;
+ }
+ printk(" - auth val: %s\n", print_message);
+ bp_print = NULL;
+ memset(print_message, 0, MAX_MSG_LEN);
+
+ bp_print = print_message;
bp = buffer;
- bp_end = bp + test_datapaddedlen;
- printk("original data: ");
+ bp_end = bp + test_datalen;
+
while (bp != bp_end) {
- printk("%c\n", isprint(*bp) ? *bp : '.');
+ *bp_print = isprint(*bp) ? *bp : '.';
bp++;
+ bp_print++;
}
+ printk("original data: %s\n", print_message);
+ bp_print = NULL;
+ memset(print_message, 0, MAX_MSG_LEN);
skcipher_request_set_crypt(req, &sg, &sg2, test_datapaddedlen, nonce);
err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
goto kcrypt_end;
}
- printk("cryptogram: ");
+ printk("kcrypt: encryption completed\n");
+ bp_print = print_message;
bp = buffer2;
bp_end = bp + test_datapaddedlen;
while (bp != bp_end) {
- printk("%c\n", isprint(*bp) ? *bp : '.');
+ *bp_print = isprint(*bp) ? *bp : '.';
bp++;
+ bp_print++;
}
+ printk(" - cryptogram: %s\n", print_message);
+ bp_print = NULL;
+ memset(print_message, 0, MAX_MSG_LEN);
memcpy(nonce, nonce_org, aes_cbc_noncelen);
printk("kcrypt: decrypt: %d\n", err);
goto kcrypt_end;
}
+
+ printk("kcrypt: decryption completed\n");
+
memcpy(nonce, nonce_org, aes_cbc_noncelen);
memcpy(buffer2, buffer, buffer_size);
memset(buffer, 0, buffer_size);
- memcpy(buffer + aes_cbc_noncelen, buffer2, test_datapaddedlen);
memcpy(buffer, nonce, aes_cbc_noncelen);
- err = hmac_sha256(auth_key, 32, buffer, aes_cbc_noncelen + test_datapaddedlen, auth_val, aes_cbc_authtrunclen);
+ memcpy(buffer + aes_cbc_noncelen, buffer2, test_datapaddedlen);
+ err = hmac_sha256(auth_key, CBC_AUTHKEYLEN, buffer, aes_cbc_noncelen + test_datapaddedlen, auth_val, aes_cbc_authlen);
if(err != 0){
- printk("kxfrm: hmac failed: %d\n", err);
+ printk("kcrypt: hmac failed: %d\n", err);
goto kcrypt_end;
}
- printk("authenticated plaintext: ");
- bp = buffer + aes_cbc_noncelen;
- bp_end = bp + test_datapaddedlen;
-
+ printk("kcrypt: auth val calculated for nonce + padded data after decryption\n");
+ memset(print_message, 0, MAX_MSG_LEN);
+ bp_print = print_message;
+ bp = auth_val;
+ bp_end = bp + aes_cbc_authlen;
while (bp != bp_end) {
- printk("%c\n", isprint(*bp) ? *bp : '.');
+ *bp_print = isprint(*bp) ? *bp : '.';
bp++;
+ bp_print++;
}
+ printk(" - auth val: %s\n", print_message);
+ bp_print = NULL;
+ memset(print_message, 0, MAX_MSG_LEN);
-
- int cmpres = memcmp(auth_val, auth_val_org, sizeof(u8) * aes_cbc_noncelen);
+ int cmpres = memcmp(auth_val, auth_val_org, aes_cbc_noncelen);
if(cmpres != 0){
- printk("auth value not matched: %d\n", cmpres);
+ printk("kcrypt: auth value not matched: %d\n", cmpres);
+ goto kcrypt_end;
} else {
- printk("auth value matched\n");
+ printk("kcrypt: auth value matched\n");
}
+ bp_print = print_message;
+ bp = buffer + aes_cbc_noncelen;
+ bp_end = bp + test_datalen;
+
+ while (bp != bp_end) {
+ *bp_print = isprint(*bp) ? *bp : '.';
+ bp++;
+ bp_print++;
+ }
+ printk("authenticated plaintext: %s\n", print_message);
kcrypt_end:
#include <linux/string.h>
#include <linux/ip.h>
-
-
-
-
+#define MAX_MSG_LEN 128
+#define GCM_ASSOCLEN 16
+#define GCM_TAGLEN 16
+#define GCM_NONCE_SALTLEN 4
+#define GCM_NONCE_BODYLEN 8
+#define GCM_NONCELEN GCM_NONCE_SALTLEN + GCM_NONCE_BODYLEN
+#define GCM_KEY_BODYLEN 32
+#define GCM_KEY_SALTLEN 4
+#define GCM_KEYLEN GCM_KEY_BODYLEN + GCM_KEY_SALTLEN
static int run_kcrypt(void) {
-
-
+ char print_messge[MAX_MSG_LEN] = {0};
char *test_data = "this is test data for kcrypt";
int test_datalen = 0;
- int aes_gcm_assoclen = 16;
- int aes_gcm_taglen = 16;
- int nonce_saltlen = 4;
- test_datalen = strlen(test_data);
+ int aes_gcm_assoclen = GCM_ASSOCLEN;
+ int aes_gcm_taglen = GCM_TAGLEN;
+ int nonce_saltlen = GCM_NONCE_SALTLEN;
+ size_t buffer_size = MAX_MSG_LEN;
struct crypto_aead *tfm = NULL;
struct aead_request *req = NULL;
u8 *buffer = NULL;
u8 *buffer2 = NULL;
- size_t buffer_size = 1024;
- u8 *bp = NULL, *bp_end = NULL;
+ u8 *bp = NULL, *bp_end = NULL, *bp_print = NULL;
struct scatterlist sg = { 0 };
struct scatterlist sg2 = { 0 };
DECLARE_CRYPTO_WAIT(wait);
+ test_datalen = strlen(test_data);
- u8 assoc_msg[16] = {
+ u8 assoc_msg[GCM_ASSOCLEN] = {
0x11, 0x22, 0x33, 0x44,
0x11, 0x22, 0x33, 0x44,
0x11, 0x22, 0x33, 0x44,
0x11, 0x22, 0x33, 0x44,
};
- u8 nonce[12] = {
- 0xcc, 0xdd, 0xee, 0xff,
+ u8 nonce[GCM_NONCELEN] = {
+ 0x99, 0x99, 0x99, 0x99,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
- u8 key[36] = {
+ u8 key[GCM_KEYLEN] = {
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0xaa, 0xbb,
- 0xcc, 0xdd, 0xee, 0xff,
- };
-/*
- u8 key[36] = {
- 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
- 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
- 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
- 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
- 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
- 0xaa, 0xbb,
+ 0x00, 0x00, 0x00, 0x00,
};
-*/
+
int err = -1;
tfm = crypto_alloc_aead("rfc4106(gcm(aes))", 0, 0);
goto kcrypt_end;
}
- get_random_bytes(nonce + 4, 8);
- err = crypto_aead_setkey(tfm, key, 36);
+ memcpy(key + GCM_KEY_BODYLEN, nonce, nonce_saltlen);
+ get_random_bytes(nonce + nonce_saltlen, GCM_NONCE_BODYLEN);
+ err = crypto_aead_setkey(tfm, key, GCM_KEYLEN);
if (err != 0) {
printk("kcrypt: set key failed: %d.\n", err);
goto kcrypt_end;
bp = buffer + aes_gcm_assoclen;
bp_end = bp + test_datalen;
- printk("original data: ");
+ memset(print_messge, 0, MAX_MSG_LEN);
+ bp_print = print_messge;
while (bp != bp_end) {
- printk("%c\n", isprint(*bp) ? *bp : '.');
+ *bp_print = isprint(*bp) ? *bp : '.';
bp++;
+ bp_print++;
}
-
+ printk("original data: %s\n", print_messge);
+ bp_print = NULL;
+ memset(print_messge, 0, MAX_MSG_LEN);
sg_init_one(&sg, buffer, buffer_size);
sg_init_one(&sg2, buffer2, buffer_size);
aead_request_set_crypt(req, &sg, &sg2, test_datalen, nonce + nonce_saltlen);
aead_request_set_ad(req, aes_gcm_assoclen);
- // aead_request_set_ad(req, 0);
-
err = crypto_wait_req(crypto_aead_encrypt(req), &wait);
if (err != 0) {
printk("kcrypt: encrypt: failed: %d.\n", err);
goto kcrypt_end;
}
+
+ printk("kcrypt: encryption completed\n");
+
+ bp_print = print_messge;
memcpy(buffer2, assoc_msg, aes_gcm_assoclen);
- printk("assoc: ");
bp = buffer2;
bp_end = bp + aes_gcm_assoclen;
while (bp != bp_end) {
- printk("%c\n", isprint(*bp) ? *bp : '.');
+ *bp_print = isprint(*bp) ? *bp : '.';
bp++;
+ bp_print++;
}
- printk("cryptogram: ");
+ printk(" - assoc: %s\n", print_messge);
+ bp_print = NULL;
+ memset(print_messge, 0, MAX_MSG_LEN);
+
+ bp_print = print_messge;
bp_end += test_datalen;
while (bp != bp_end) {
- printk("%c\n", isprint(*bp) ? *bp : '.');
+ *bp_print = isprint(*bp) ? *bp : '.';
bp++;
+ bp_print++;
}
+ printk(" - cryptogram: %s\n", print_messge);
+ bp_print = NULL;
+ memset(print_messge, 0, MAX_MSG_LEN);
- printk("authentication tag: ");
+ bp_print = print_messge;
bp_end += aes_gcm_taglen;
while (bp != bp_end) {
- printk("%c\n", isprint(*bp) ? *bp : '.');
+ *bp_print = isprint(*bp) ? *bp : '.';
bp++;
+ bp_print++;
}
+ printk(" - auth tag: %s\n", print_messge);
+ bp_print = NULL;
+ memset(print_messge, 0, MAX_MSG_LEN);
aead_request_set_crypt(req, &sg2, &sg, test_datalen + aes_gcm_taglen, nonce + nonce_saltlen);
aead_request_set_ad(req, aes_gcm_taglen);
- /*
- err = crypto_aead_setkey(tfm, key, 36);
- if (err != 0) {
- printk("kcrypt: setkey again: %d.\n", err);
- goto kcrypt_end;
- }
- */
-
err = crypto_wait_req(crypto_aead_decrypt(req), &wait);
if (err != 0) {
printk("kcrypt: decrypt: %d\n", err);
goto kcrypt_end;
}
+ printk("kcrypt: decryption completed\n");
-
- printk("authenticated plaintext: ");
+ bp_print = print_messge;
bp = buffer + aes_gcm_assoclen;
bp_end = bp + test_datalen;
-
while (bp != bp_end) {
- printk("%c\n", isprint(*bp) ? *bp : '.');
+ *bp_print = isprint(*bp) ? *bp : '.';
bp++;
+ bp_print++;
}
-
+ printk("authenticated plaintext: %s\n", print_messge);
kcrypt_end: