]> git.feebdaed.xyz Git - linuxyz.git/commitdiff
kcrypt results
authorseantywork <seantywork@gmail.com>
Thu, 4 Dec 2025 00:25:23 +0000 (00:25 +0000)
committerseantywork <seantywork@gmail.com>
Thu, 4 Dec 2025 00:25:23 +0000 (00:25 +0000)
kcrypt/2025-1204.xyz.md
kcrypt/cbc/kcrypt.c
kcrypt/gcm/kcrypt.c

index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..51d6fe63ce4102a01c7e90d8fe006f60e1f39679 100644 (file)
@@ -0,0 +1,38 @@
+
+
+# gcm result 
+
+```shell
+[Dec 3 23:59] init kcrypt
+[  +0.000319] original data: this is test data for kcrypt
+[  +0.000013] kcrypt: encryption completed
+[  +0.000001]   - assoc: ."3D."3D."3D."3D
+[  +0.000001]   - cryptogram: NT..\xb9r\xb3\xeaj:.e3.\xa6X..\xbe-x.Z.G\xd2..
+[  +0.000000]   - auth tag: \xfc\xddk.\xe6.x.@\xa7z\xc4S..\xce
+[  +0.000002] kcrypt: decryption completed
+[  +0.000001] authenticated plaintext: this is test data for kcrypt
+[ +37.572556] exit kcrypt
+
+```
+
+
+# cbc result 
+
+
+```shell
+[Dec 4 00:24] init kcrypt
+[  +0.000004] kcrypt: datalen: 28
+[  +0.000001] kcrypt: padded datalen: 32
+[  +0.000036] kcrypt: auth val calculated for nonce + padded data
+[  +0.000001]   - auth val: .T,Z?\xfb\xba.[V8\xda.u.`._7.w^\xd9]k\xb6..G\xc2W#
+[  +0.000001] original data: this is test data for kcrypt
+[  +0.000002] kcrypt: encryption completed
+[  +0.000000]   - cryptogram: .G\xf9.\xb0\xcf.?\xc8.\xf9hc4uu1I\xdb)\xa1'.1.\xc0Y.%.D.
+[  +0.000002] kcrypt: decryption completed
+[  +0.000004] kcrypt: auth val calculated for nonce + padded data after decryption
+[  +0.000001]   - auth val: .T,Z?\xfb\xba.[V8\xda.u.`._7.w^\xd9]k\xb6..G\xc2W#
+[  +0.000001] kcrypt: auth value matched
+[  +0.000001] authenticated plaintext: this is test data for kcrypt
+[ +11.913847] exit kcrypt
+
+```
\ No newline at end of file
index 0cb233062da7234dde63c725f3f2ccc1cbe96007..be4439223fd6bc0e0d785a0c81827f982fd3ee13 100644 (file)
 #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);
@@ -76,11 +74,11 @@ static int run_kcrypt(void) {
         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,
     };
@@ -92,7 +90,7 @@ static int run_kcrypt(void) {
         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,
@@ -100,13 +98,13 @@ static int run_kcrypt(void) {
         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,
@@ -132,7 +130,7 @@ static int run_kcrypt(void) {
     }
     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;
@@ -151,6 +149,7 @@ static int run_kcrypt(void) {
         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);
@@ -159,21 +158,44 @@ static int run_kcrypt(void) {
     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);
@@ -182,14 +204,19 @@ static int run_kcrypt(void) {
         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);
 
@@ -200,35 +227,53 @@ static int run_kcrypt(void) {
         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:
index da93ad288740144037e383c7a27e23a48417a1e6..70b9ea66466af98f6f9d935b204ec4578be92e95 100644 (file)
@@ -9,60 +9,55 @@
 #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);
@@ -81,8 +76,9 @@ static int run_kcrypt(void) {
         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;
@@ -115,12 +111,16 @@ static int run_kcrypt(void) {
     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);
@@ -131,63 +131,69 @@ static int run_kcrypt(void) {
     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: