Source-Changes-HG archive

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

[src/trunk]: src/sys/crypto/rijndael Add XTS mode.



details:   https://anonhg.NetBSD.org/src/rev/9a4c0cd06bf3
branches:  trunk
changeset: 819592:9a4c0cd06bf3
user:      alnsn <alnsn%NetBSD.org@localhost>
date:      Sun Dec 11 00:28:44 2016 +0000

description:
Add XTS mode.

diffstat:

 sys/crypto/rijndael/rijndael-api-fst.c |  51 +++++++++++++++++++++++++++++++--
 sys/crypto/rijndael/rijndael-api-fst.h |   3 +-
 2 files changed, 49 insertions(+), 5 deletions(-)

diffs (117 lines):

diff -r 012dd5b9b036 -r 9a4c0cd06bf3 sys/crypto/rijndael/rijndael-api-fst.c
--- a/sys/crypto/rijndael/rijndael-api-fst.c    Sun Dec 11 00:25:32 2016 +0000
+++ b/sys/crypto/rijndael/rijndael-api-fst.c    Sun Dec 11 00:28:44 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rijndael-api-fst.c,v 1.24 2011/05/14 16:46:55 jmmv Exp $       */
+/*     $NetBSD: rijndael-api-fst.c,v 1.25 2016/12/11 00:28:44 alnsn Exp $      */
 
 /**
  * rijndael-api-fst.c
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rijndael-api-fst.c,v 1.24 2011/05/14 16:46:55 jmmv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rijndael-api-fst.c,v 1.25 2016/12/11 00:28:44 alnsn Exp $");
 
 #include <sys/param.h>
 #ifdef _KERNEL
@@ -52,6 +52,8 @@
 #include <crypto/rijndael/rijndael-alg-fst.h>
 #include <crypto/rijndael/rijndael-api-fst.h>
 
+#define XTS_ALPHA 0x87
+
 static void xor16(uint8_t *d, const uint8_t *a, const uint8_t* b)
 {
        for (size_t i = 0; i < 4; i++) {
@@ -62,6 +64,22 @@
        }
 }
 
+static void
+xts_exponentiate(uint8_t *iv)
+{
+       unsigned int carry = 0;
+
+       for (size_t i = 0; i < 16; i++) {
+               unsigned int msb = iv[i] >> 7;
+
+               iv[i] = (iv[i] << 1) | carry;
+               carry = msb;
+       }
+
+       if (carry != 0)
+               iv[0] ^= XTS_ALPHA;
+}
+
 int
 rijndael_makeKey(keyInstance *key, BYTE direction, int keyLen,
     const char *keyMaterial)
@@ -102,7 +120,8 @@
 int
 rijndael_cipherInit(cipherInstance *cipher, BYTE mode, const char *IV)
 {
-       if ((mode == MODE_ECB) || (mode == MODE_CBC) || (mode == MODE_CFB1)) {
+       if ((mode == MODE_ECB) || (mode == MODE_CBC) ||
+           (mode == MODE_XTS) || (mode == MODE_CFB1)) {
                cipher->mode = mode;
        } else {
                return BAD_CIPHER_MODE;
@@ -153,6 +172,18 @@
                }
                break;
 
+       case MODE_XTS:
+               iv = (u_int8_t *)cipher->IV;
+               for (i = numBlocks; i > 0; i--) {
+                       xor16(block, input, iv);
+                       rijndaelEncrypt(key->rk, key->Nr, block, block);
+                       xor16(outBuffer, block, iv);
+                       xts_exponentiate(iv);
+                       input += 16;
+                       outBuffer += 16;
+               }
+               break;
+
        case MODE_CFB1:
                iv = (u_int8_t *)cipher->IV;
                for (i = numBlocks; i > 0; i--) {
@@ -284,7 +315,19 @@
                }
                break;
 
-    case MODE_CFB1:
+       case MODE_XTS:
+               iv = (u_int8_t *)cipher->IV;
+               for (i = numBlocks; i > 0; i--) {
+                       xor16(block, input, iv);
+                       rijndaelDecrypt(key->rk, key->Nr, block, block);
+                       xor16(outBuffer, block, iv);
+                       xts_exponentiate(iv);
+                       input += 16;
+                       outBuffer += 16;
+               }
+               break;
+
+       case MODE_CFB1:
                iv = (u_int8_t *)cipher->IV;
                for (i = numBlocks; i > 0; i--) {
                        memcpy(outBuffer, input, 16);
diff -r 012dd5b9b036 -r 9a4c0cd06bf3 sys/crypto/rijndael/rijndael-api-fst.h
--- a/sys/crypto/rijndael/rijndael-api-fst.h    Sun Dec 11 00:25:32 2016 +0000
+++ b/sys/crypto/rijndael/rijndael-api-fst.h    Sun Dec 11 00:28:44 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rijndael-api-fst.h,v 1.8 2007/01/21 23:00:08 cbiere Exp $      */
+/*     $NetBSD: rijndael-api-fst.h,v 1.9 2016/12/11 00:28:44 alnsn Exp $       */
 
 /**
  * rijndael-api-fst.h
@@ -48,6 +48,7 @@
 #define     MODE_ECB              1 /*  Are we ciphering in ECB mode?   */
 #define     MODE_CBC              2 /*  Are we ciphering in CBC mode?   */
 #define     MODE_CFB1             3 /*  Are we ciphering in 1-bit CFB mode? */
+#define     MODE_XTS              4 /*  Are we ciphering in XTS mode? */
 #define     TRUE                  1
 #define     FALSE                 0
 #define     BITSPERBLOCK        128 /* Default number of bits in a cipher block */



Home | Main Index | Thread Index | Old Index