Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev Replace numeric block sizes with symbolic names,



details:   https://anonhg.NetBSD.org/src/rev/0a484e01c8cf
branches:  trunk
changeset: 350027:0a484e01c8cf
user:      alnsn <alnsn%NetBSD.org@localhost>
date:      Mon Jan 02 14:28:29 2017 +0000

description:
Replace numeric block sizes with symbolic names,

No functional change. Requested by mrg a couple of months ago.

diffstat:

 sys/dev/cgd_crypto.c |  62 +++++++++++++++++++++++++++++++--------------------
 sys/dev/cgd_crypto.h |   5 +++-
 2 files changed, 41 insertions(+), 26 deletions(-)

diffs (228 lines):

diff -r bf685eb44151 -r 0a484e01c8cf sys/dev/cgd_crypto.c
--- a/sys/dev/cgd_crypto.c      Mon Jan 02 12:38:16 2017 +0000
+++ b/sys/dev/cgd_crypto.c      Mon Jan 02 14:28:29 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cgd_crypto.c,v 1.14 2016/12/11 00:20:49 alnsn Exp $ */
+/* $NetBSD: cgd_crypto.c,v 1.15 2017/01/02 14:28:29 alnsn Exp $ */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cgd_crypto.c,v 1.14 2016/12/11 00:20:49 alnsn Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cgd_crypto.c,v 1.15 2017/01/02 14:28:29 alnsn Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -199,7 +199,7 @@
 
 struct aes_encdata {
        keyInstance     *ae_key;        /* key for this direction */
-       u_int8_t         ae_iv[16];     /* Initialization Vector */
+       u_int8_t         ae_iv[CGD_AES_BLOCK_SIZE]; /* Initialization Vector */
 };
 
 static void *
@@ -244,8 +244,10 @@
        KASSERT(cipher_ok > 0);
        rijndael_blockEncrypt(&cipher, &apd->ap_enckey,
            blkno_buf, blocksize * 8, iv);
-       if (blocksize > 16)
-               (void)memmove(iv, iv + blocksize - 16, 16);
+       if (blocksize > CGD_AES_BLOCK_SIZE) {
+               (void)memmove(iv, iv + blocksize - CGD_AES_BLOCK_SIZE,
+                   CGD_AES_BLOCK_SIZE);
+       }
 }
 
 static void
@@ -258,7 +260,8 @@
        cipher_ok = rijndael_cipherInit(&cipher, MODE_CBC, ae->ae_iv);
        KASSERT(cipher_ok > 0);
        rijndael_blockEncrypt(&cipher, ae->ae_key, src, len * 8, dst);
-       (void)memcpy(ae->ae_iv, (u_int8_t *)dst + (len - 16), 16);
+       (void)memcpy(ae->ae_iv, (u_int8_t *)dst +
+           (len - CGD_AES_BLOCK_SIZE), CGD_AES_BLOCK_SIZE);
 }
 
 static void
@@ -271,7 +274,8 @@
        cipher_ok = rijndael_cipherInit(&cipher, MODE_CBC, ae->ae_iv);
        KASSERT(cipher_ok > 0);
        rijndael_blockDecrypt(&cipher, ae->ae_key, src, len * 8, dst);
-       (void)memcpy(ae->ae_iv, (const u_int8_t *)src + (len - 16), 16);
+       (void)memcpy(ae->ae_iv, (const u_int8_t *)src +
+           (len - CGD_AES_BLOCK_SIZE), CGD_AES_BLOCK_SIZE);
 }
 
 static void
@@ -281,7 +285,7 @@
        struct aes_privdata     *apd = privdata;
        struct aes_encdata       encd;
 
-       (void)memcpy(encd.ae_iv, iv, 16);
+       (void)memcpy(encd.ae_iv, iv, CGD_AES_BLOCK_SIZE);
        switch (dir) {
        case CGD_CIPHER_ENCRYPT:
                encd.ae_key = &apd->ap_enckey;
@@ -358,7 +362,7 @@
        cipher_ok = rijndael_cipherInit(&cipher, MODE_XTS, ae->ae_iv);
        KASSERT(cipher_ok > 0);
        rijndael_blockEncrypt(&cipher, ae->ae_key, src, len * 8, dst);
-       (void)memcpy(ae->ae_iv, cipher.IV, 16);
+       (void)memcpy(ae->ae_iv, cipher.IV, CGD_AES_BLOCK_SIZE);
 }
 
 static void
@@ -371,7 +375,7 @@
        cipher_ok = rijndael_cipherInit(&cipher, MODE_XTS, ae->ae_iv);
        KASSERT(cipher_ok > 0);
        rijndael_blockDecrypt(&cipher, ae->ae_key, src, len * 8, dst);
-       (void)memcpy(ae->ae_iv, cipher.IV, 16);
+       (void)memcpy(ae->ae_iv, cipher.IV, CGD_AES_BLOCK_SIZE);
 }
 
 static void
@@ -381,7 +385,7 @@
        struct aes_privdata     *apd = privdata;
        struct aes_encdata       encd;
 
-       (void)memcpy(encd.ae_iv, iv, 16);
+       (void)memcpy(encd.ae_iv, iv, CGD_AES_BLOCK_SIZE);
        switch (dir) {
        case CGD_CIPHER_ENCRYPT:
                encd.ae_key = &apd->ap_enckey;
@@ -410,7 +414,7 @@
        des_key_schedule        *ce_key1;
        des_key_schedule        *ce_key2;
        des_key_schedule        *ce_key3;
-       u_int8_t                ce_iv[8];
+       u_int8_t                ce_iv[CGD_3DES_BLOCK_SIZE];
 };
 
 static void *
@@ -455,13 +459,15 @@
     const char *blkno_buf, size_t blocksize, int dir)
 {
        struct  c3des_privdata *cp = privdata;
-       char    zero_iv[8];
+       char    zero_iv[CGD_3DES_BLOCK_SIZE];
 
        memset(zero_iv, 0, sizeof(zero_iv));
        des_ede3_cbc_encrypt(blkno_buf, iv, blocksize,
            cp->cp_key1, cp->cp_key2, cp->cp_key3, (des_cblock *)zero_iv, 1);
-       if (blocksize > 8)
-               (void)memmove(iv, iv + blocksize - 8, 8);
+       if (blocksize > CGD_3DES_BLOCK_SIZE) {
+               (void)memmove(iv, iv + blocksize - CGD_3DES_BLOCK_SIZE,
+                   CGD_3DES_BLOCK_SIZE);
+       }
 }
 
 static void
@@ -471,7 +477,8 @@
 
        des_ede3_cbc_encrypt(src, dst, len, *ce->ce_key1, *ce->ce_key2,
            *ce->ce_key3, (des_cblock *)ce->ce_iv, 1);
-       (void)memcpy(ce->ce_iv, (const u_int8_t *)dst + (len - 8), 8);
+       (void)memcpy(ce->ce_iv, (const u_int8_t *)dst +
+           (len - CGD_3DES_BLOCK_SIZE), CGD_3DES_BLOCK_SIZE);
 }
 
 static void
@@ -481,7 +488,8 @@
 
        des_ede3_cbc_encrypt(src, dst, len, *ce->ce_key1, *ce->ce_key2,
            *ce->ce_key3, (des_cblock *)ce->ce_iv, 0);
-       (void)memcpy(ce->ce_iv, (const u_int8_t *)src + (len - 8), 8);
+       (void)memcpy(ce->ce_iv, (const u_int8_t *)src +
+           (len - CGD_3DES_BLOCK_SIZE), CGD_3DES_BLOCK_SIZE);
 }
 
 static void
@@ -491,7 +499,7 @@
        struct  c3des_privdata *cp = privdata;
        struct  c3des_encdata ce;
 
-       (void)memcpy(ce.ce_iv, iv, 8);
+       (void)memcpy(ce.ce_iv, iv, CGD_3DES_BLOCK_SIZE);
        ce.ce_key1 = &cp->cp_key1;
        ce.ce_key2 = &cp->cp_key2;
        ce.ce_key3 = &cp->cp_key3;
@@ -517,7 +525,7 @@
 
 struct bf_encdata {
        BF_KEY          *be_key;
-       u_int8_t         be_iv[8];
+       u_int8_t         be_iv[CGD_BF_BLOCK_SIZE];
 };
 
 static void *
@@ -554,12 +562,14 @@
     const char *blkno_buf, size_t blocksize, int dir)
 {
        struct  bf_privdata *bp = privdata;
-       char    zero_iv[8];
+       char    zero_iv[CGD_BF_BLOCK_SIZE];
 
        memset(zero_iv, 0, sizeof(zero_iv));
        BF_cbc_encrypt(blkno_buf, iv, blocksize, &bp->bp_key, zero_iv, 1);
-       if (blocksize > 8)
-               (void)memmove(iv, iv + blocksize - 8, 8);
+       if (blocksize > CGD_BF_BLOCK_SIZE) {
+               (void)memmove(iv, iv + blocksize - CGD_BF_BLOCK_SIZE,
+                   CGD_BF_BLOCK_SIZE);
+       }
 }
 
 static void
@@ -568,7 +578,8 @@
        struct  bf_encdata *be = privdata;
 
        BF_cbc_encrypt(src, dst, len, be->be_key, be->be_iv, 1);
-       (void)memcpy(be->be_iv, (u_int8_t *)dst + (len - 8), 8);
+       (void)memcpy(be->be_iv, (u_int8_t *)dst +
+           (len - CGD_BF_BLOCK_SIZE), CGD_BF_BLOCK_SIZE);
 }
 
 static void
@@ -577,7 +588,8 @@
        struct  bf_encdata *be = privdata;
 
        BF_cbc_encrypt(src, dst, len, be->be_key, be->be_iv, 0);
-       (void)memcpy(be->be_iv, (const u_int8_t *)src + (len - 8), 8);
+       (void)memcpy(be->be_iv, (const u_int8_t *)src +
+           (len - CGD_BF_BLOCK_SIZE), CGD_BF_BLOCK_SIZE);
 }
 
 static void
@@ -587,7 +599,7 @@
        struct  bf_privdata *bp = privdata;
        struct  bf_encdata be;
 
-       (void)memcpy(be.be_iv, iv, 8);
+       (void)memcpy(be.be_iv, iv, CGD_BF_BLOCK_SIZE);
        be.be_key = &bp->bp_key;
        switch (dir) {
        case CGD_CIPHER_ENCRYPT:
diff -r bf685eb44151 -r 0a484e01c8cf sys/dev/cgd_crypto.h
--- a/sys/dev/cgd_crypto.h      Mon Jan 02 12:38:16 2017 +0000
+++ b/sys/dev/cgd_crypto.h      Mon Jan 02 14:28:29 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cgd_crypto.h,v 1.9 2016/12/11 00:20:49 alnsn Exp $ */
+/* $NetBSD: cgd_crypto.h,v 1.10 2017/01/02 14:28:29 alnsn Exp $ */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -35,6 +35,9 @@
 #ifdef _KERNEL
 #define CGD_CIPHER_DECRYPT     1
 #define CGD_CIPHER_ENCRYPT     2
+#define CGD_AES_BLOCK_SIZE     16
+#define CGD_3DES_BLOCK_SIZE    8
+#define CGD_BF_BLOCK_SIZE      8
 
 typedef void *(cfunc_init)(size_t, const void *, size_t *);
 typedef void  (cfunc_destroy)(void *);



Home | Main Index | Thread Index | Old Index