crypto: Adding Hash-Encrypt-Hash(HEH)
Subsystems (2)
cover crypto: Adding Hash-Encrypt-Hash(HEH)
This patchset implements HEH, which is currently specified by the following Internet Draft:
https://tools.ietf.org/html/draft-cope-heh-00
This patchset is a request for comments, and should not be merged at this time. We would like to wait for further comments on the Internet Draft before merging this patchset.
Thanks
RFC PATCH 1/7 crypto: skcipher adding skciper_walk_virt_init
Adding skcipher_walk_virt_init to allow a skciper_walk to specify length and input/output sg. Provides similar funcationalty to blkcipher_walk_init
--- crypto/skcipher.c | 32 +++++++++++++++++++++++--------- include/crypto/internal/skcipher.h | 4 ++++ 2 files changed, 27 insertions(+), 9 deletions(-)
crypto/skcipher.c +23 -9
@@ -447,16 +447,19 @@ static int skcipher_walk_first(struct skcipher_walk *walk) }static int skcipher_walk_skcipher (struct skcipher_walk *walk, -struct skcipher_request *req) +struct skcipher_request *req, +struct scatterlist *src, +struct scatterlist *dst, +unsigned int len) {struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); -scatterwalk_start (&walk->in, req->src); -scatterwalk_start (&walk->out, req->dst); +scatterwalk_start (&walk->in, src); +scatterwalk_start (&walk->out, dst); - walk->in.sg = req->src; - walk->out.sg = req->dst; - walk->total = req->cryptlen; + walk->in.sg = src; + walk->out.sg = dst; + walk->total = len; walk->iv = req->iv; walk->oiv = req->iv;
@@ -474,17 +477,27 @@ static int skcipher_walk_skcipher(struct skcipher_walk *walk,int skcipher_walk_virt (struct skcipher_walk *walk,struct skcipher_request *req,bool atomic) { +return skcipher_walk_virt_init (walk, req, atomic, req->src, req->dst, + req->cryptlen); +} +EXPORT_SYMBOL_GPL (skcipher_walk_virt); + +int skcipher_walk_virt_init (struct skcipher_walk *walk, +struct skcipher_request *req,bool atomic, +struct scatterlist *src,struct scatterlist *dst, +unsigned int len) +{int err; walk->flags &= ~SKCIPHER_WALK_PHYS ; - err =skcipher_walk_skcipher (walk, req); + err =skcipher_walk_skcipher (walk, req, src, dst, len); walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0 ;return err; } -EXPORT_SYMBOL_GPL (skcipher_walk_virt); +EXPORT_SYMBOL_GPL (skcipher_walk_virt_init);void skcipher_walk_atomise (struct skcipher_walk *walk) {
@@ -499,7 +512,8 @@ int skcipher_walk_async(struct skcipher_walk *walk,INIT_LIST_HEAD (&walk->buffers); -return skcipher_walk_skcipher (walk, req); +return skcipher_walk_skcipher (walk, req, req->src, req->dst, + req->cryptlen); }EXPORT_SYMBOL_GPL (skcipher_walk_async);
include/crypto/internal/skcipher.h +4 -0
@@ -144,6 +144,10 @@ int skcipher_walk_done(struct skcipher_walk *walk, int err);int skcipher_walk_virt (struct skcipher_walk *walk,struct skcipher_request *req,bool atomic); +int skcipher_walk_virt_init (struct skcipher_walk *walk, +struct skcipher_request *req, +bool atomic,struct scatterlist *src, +struct scatterlist *dst,unsigned int len);void skcipher_walk_atomise (struct skcipher_walk *walk);int skcipher_walk_async (struct skcipher_walk *walk,struct skcipher_request *req);
Signature · 1 line
2.8.0.rc3.226.g39d4020
RFC PATCH 2/7 crypto: gf128mul - Refactor gf128 overflow macros
In reply to Alex CopeRename and clean up the overflow macros. Their usage is more general than the name suggested.
--- crypto/gf128mul.c | 68 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 27 deletions(-)
crypto/gf128mul.c +41 -27
@@ -88,33 +88,47 @@ q(0xf8), q(0xf9), q(0xfa), q(0xfb), q(0xfc), q(0xfd), q(0xfe), q(0xff) \ } -/* Given the value i in 0..255 as the byte overflow when a field element -in GHASH is multiplied by x^8, this function will return the values that -are generated in the lo 16-bit word of the field value by applying the -modular polynomial. The values lo_byte and hi_byte are returned via the -macro xp_fun(lo_byte, hi_byte) so that the values can be assembled into -memory as required by a suitable definition of this macro operating on -the table above -*/ - -#define xx (p, q) 0x##p##q +/* +* Given a value i in 0..255 as the byte overflow when a field element +* in GF(2^128) is multiplied by x^8, the following macro returns the +* 16-bit value that must be XOR-ed into the low-degree end of the +* product to reduce it modulo the irreducible polynomial x^128 + x^7 + +* x^2 + x + 1. +* +* There are two versions of the macro, and hence two tables: one for +* the "be" convention where the highest-order bit is the coefficient of +* the highest-degree polynomial term, and one for the "le" convention +* where the highest-order bit is the coefficient of the lowest-degree +* polynomial term. In both cases the values are stored in CPU byte +* endianness such that the coefficients are ordered consistently across +* bytes, i.e. in the "be" table bits 15..0 of the stored value +* correspond to the coefficients of x^15..x^0, and in the "le" table +* bits 15..0 correspond to the coefficients of x^0..x^15. +* +* Therefore, provided that the appropriate byte endianness conversions +* are done by the multiplication functions (and these must be in place +* anyway to support both little endian and big endian CPUs), the "be" +* table can be used for multiplications of both "bbe" and "ble" +* elements, and the "le" table can be used for multiplications of both +* "lle" and "lbe" elements. +*/ -#define xda_bbe (i) ( \ - (i & 0x80 ? xx(43, 80) : 0) ^ (i & 0x40 ? xx(21, c0) : 0) ^ \ - (i & 0x20 ? xx(10, e0) : 0) ^ (i & 0x10 ? xx(08, 70) : 0) ^ \ - (i & 0x08 ? xx(04, 38) : 0) ^ (i & 0x04 ? xx(02, 1c) : 0) ^ \ - (i & 0x02 ? xx(01, 0e) : 0) ^ (i & 0x01 ? xx(00, 87) : 0) \ +#define xda_be (i) ( \ + (i & 0x80 ? 0x4380 : 0) ^ (i & 0x40 ? 0x21c0 : 0) ^ \ + (i & 0x20 ? 0x10e0 : 0) ^ (i & 0x10 ? 0x0870 : 0) ^ \ + (i & 0x08 ? 0x0438 : 0) ^ (i & 0x04 ? 0x021c : 0) ^ \ + (i & 0x02 ? 0x010e : 0) ^ (i & 0x01 ? 0x0087 : 0) \ ) -#define xda_lle (i) ( \ - (i & 0x80 ? xx(e1, 00) : 0) ^ (i & 0x40 ? xx(70, 80) : 0) ^ \ - (i & 0x20 ? xx(38, 40) : 0) ^ (i & 0x10 ? xx(1c, 20) : 0) ^ \ - (i & 0x08 ? xx(0e, 10) : 0) ^ (i & 0x04 ? xx(07, 08) : 0) ^ \ - (i & 0x02 ? xx(03, 84) : 0) ^ (i & 0x01 ? xx(01, c2) : 0) \ +#define xda_le (i) ( \ + (i & 0x80 ? 0xe100 : 0) ^ (i & 0x40 ? 0x7080 : 0) ^ \ + (i & 0x20 ? 0x3840 : 0) ^ (i & 0x10 ? 0x1c20 : 0) ^ \ + (i & 0x08 ? 0x0e10 : 0) ^ (i & 0x04 ? 0x0708 : 0) ^ \ + (i & 0x02 ? 0x0384 : 0) ^ (i & 0x01 ? 0x01c2 : 0) \ ) -static const u16 gf128mul_table_lle[256 ] =gf128mul_dat (xda_lle); -static const u16 gf128mul_table_bbe[256 ] =gf128mul_dat (xda_bbe); +static const u16 gf128mul_table_le[256 ] =gf128mul_dat (xda_le); +static const u16 gf128mul_table_be[256 ] =gf128mul_dat (xda_be);/* These functions multiply a field element by x, by x^4 and by x^8 * in the polynomial field representation. It uses 32-bit word operations
@@ -126,7 +140,7 @@ static void gf128mul_x_lle(be128 *r, const be128 *x) {u64 a =be64_to_cpu (x->a);u64 b =be64_to_cpu (x->b); -u64 _tt = gf128mul_table_lle[(b <<7 ) &0xff ]; +u64 _tt = gf128mul_table_le[(b <<7 ) &0xff ]; r->b =cpu_to_be64 ((b >>1 ) | (a <<63 )); r->a =cpu_to_be64 ((a >>1 ) ^ (_tt <<48 ));
@@ -136,7 +150,7 @@ static void gf128mul_x_bbe(be128 *r, const be128 *x) {u64 a =be64_to_cpu (x->a);u64 b =be64_to_cpu (x->b); -u64 _tt = gf128mul_table_bbe[a >>63 ]; +u64 _tt = gf128mul_table_be[a >>63 ]; r->a =cpu_to_be64 ((a <<1 ) | (b >>63 )); r->b =cpu_to_be64 ((b <<1 ) ^ _tt);
@@ -146,7 +160,7 @@ void gf128mul_x_ble(be128 *r, const be128 *x) {u64 a =le64_to_cpu (x->a);u64 b =le64_to_cpu (x->b); -u64 _tt = gf128mul_table_bbe[b >>63 ]; +u64 _tt = gf128mul_table_be[b >>63 ]; r->a =cpu_to_le64 ((a <<1 ) ^ _tt); r->b =cpu_to_le64 ((b <<1 ) | (a >>63 ));
@@ -157,7 +171,7 @@ static void gf128mul_x8_lle(be128 *x) {u64 a =be64_to_cpu (x->a);u64 b =be64_to_cpu (x->b); -u64 _tt = gf128mul_table_lle[b &0xff ]; +u64 _tt = gf128mul_table_le[b &0xff ]; x->b =cpu_to_be64 ((b >>8 ) | (a <<56 )); x->a =cpu_to_be64 ((a >>8 ) ^ (_tt <<48 ));
@@ -167,7 +181,7 @@ static void gf128mul_x8_bbe(be128 *x) {u64 a =be64_to_cpu (x->a);u64 b =be64_to_cpu (x->b); -u64 _tt = gf128mul_table_bbe[a >>56 ]; +u64 _tt = gf128mul_table_be[a >>56 ]; x->a =cpu_to_be64 ((a <<8 ) | (b >>56 )); x->b =cpu_to_be64 ((b <<8 ) ^ _tt);
Signature · 1 line
2.8.0.rc3.226.g39d4020
RFC PATCH 3/7 crypto: gf128mul - Add ble multiplication functions
In reply to Alex CopeAdding ble multiplication to GF128mul, and fixing up comments.
The ble multiplication functions multiply GF(2^128) elements in the ble format. This format is preferable because the bits within each byte map to polynomial coefficients in the natural order (lowest order bit = coefficient of lowest degree polynomial term), and the bytes are stored in little endian order which matches the endianness of most modern CPUs.
These new functions will be used by the HEH algorithm.
--- crypto/gf128mul.c | 99 ++++++++++++++++++++++++++++++++++++++++++++--- include/crypto/gf128mul.h | 45 +++++++++++---------- 2 files changed, 117 insertions(+), 27 deletions(-)
crypto/gf128mul.c +93 -6
@@ -44,7 +44,7 @@--------------------------------------------------------------------------- Issue 31/01/2006 -This file provides fast multiplication in GF(128) as required by several +This file provides fast multiplication in GF(2^128) as required by several cryptographic authentication modes */
@@ -130,9 +130,10 @@static const u16 gf128mul_table_le[256 ] =gf128mul_dat (xda_le);static const u16 gf128mul_table_be[256 ] =gf128mul_dat (xda_be); -/* These functions multiply a field element by x, by x^4 and by x^8 -* in the polynomial field representation. It uses 32-bit word operations -* to gain speed but compensates for machine endianess and hence works +/* +* The following functions multiply a field element by x or by x^8 in +* the polynomial field representation. They use 64-bit word operations +* to gain speed but compensate for machine endianness and hence work * correctly on both styles of machine. */
@@ -187,6 +188,16 @@ static void gf128mul_x8_bbe(be128 *x) x->b =cpu_to_be64 ((b <<8 ) ^ _tt); } +static void gf128mul_x8_ble (be128 *x) +{ +u64 a =le64_to_cpu (x->b); +u64 b =le64_to_cpu (x->a); +u64 _tt = gf128mul_table_be[a >>56 ]; + + x->b =cpu_to_le64 ((a <<8 ) | (b >>56 )); + x->a =cpu_to_le64 ((b <<8 ) ^ _tt); +} +void gf128mul_lle (be128 *r,const be128 *b) {be128 p[8 ];
@@ -263,9 +274,48 @@ void gf128mul_bbe(be128 *r, const be128 *b) }EXPORT_SYMBOL (gf128mul_bbe); +void gf128mul_ble (be128 *r,const be128 *b) +{ +be128 p[8 ]; +int i; + + p[0 ] = *r; +for (i =0 ; i <7 ; ++i) +gf128mul_x_ble ((be128 *)&p[i +1 ], (be128 *)&p[i]); + +memset (r,0 ,sizeof (*r)); +for (i =0 ;;) { +u8 ch = ((u8 *)b)[15 - i]; + +if (ch &0x80 ) +be128_xor (r, r, &p[7 ]); +if (ch &0x40 ) +be128_xor (r, r, &p[6 ]); +if (ch &0x20 ) +be128_xor (r, r, &p[5 ]); +if (ch &0x10 ) +be128_xor (r, r, &p[4 ]); +if (ch &0x08 ) +be128_xor (r, r, &p[3 ]); +if (ch &0x04 ) +be128_xor (r, r, &p[2 ]); +if (ch &0x02 ) +be128_xor (r, r, &p[1 ]); +if (ch &0x01 ) +be128_xor (r, r, &p[0 ]); + +if (++i >=16 ) +break ; + +gf128mul_x8_ble (r); + } +} +EXPORT_SYMBOL (gf128mul_ble); + +/* This version uses 64k bytes of table space. A 16 byte buffer has to be multiplied by a 16 byte key -value in GF(128). If we consider a GF(128) value in +value in GF(2^128). If we consider a GF(2^128) value in the buffer's lowest byte, we can construct a table of the 256 16 byte values that result from the 256 values of this byte. This requires 4096 bytes. But we also
@@ -399,7 +449,7 @@ EXPORT_SYMBOL(gf128mul_64k_bbe);/* This version uses 4k bytes of table space. A 16 byte buffer has to be multiplied by a 16 byte key -value in GF(128). If we consider a GF(128) value in a +value in GF(2^128). If we consider a GF(2^128) value in a single byte, we can construct a table of the 256 16 byte values that result from the 256 values of this byte. This requires 4096 bytes. If we take the highest byte in
@@ -457,6 +507,28 @@ struct gf128mul_4k *gf128mul_init_4k_bbe(const be128 *g) }EXPORT_SYMBOL (gf128mul_init_4k_bbe); +struct gf128mul_4k *gf128mul_init_4k_ble (const be128 *g) +{ +struct gf128mul_4k *t; +int j, k; + + t =kzalloc (sizeof (*t),GFP_KERNEL ); +if (!t) + goto out; + + t->t[1 ] = *g; +for (j =1 ; j <=64 ; j <<=1 ) +gf128mul_x_ble (&t->t[j + j], &t->t[j]); + +for (j =2 ; j <256 ; j += j) +for (k =1 ; k < j; ++k) +be128_xor (&t->t[j + k], &t->t[j], &t->t[k]); + +out: +return t; +} +EXPORT_SYMBOL (gf128mul_init_4k_ble); +void gf128mul_4k_lle (be128 *a,struct gf128mul_4k *t) {u8 *ap = (u8 *)a;
@@ -487,5 +559,20 @@ void gf128mul_4k_bbe(be128 *a, struct gf128mul_4k *t) }EXPORT_SYMBOL (gf128mul_4k_bbe); +void gf128mul_4k_ble (be128 *a,struct gf128mul_4k *t) +{ +u8 *ap = (u8 *)a; +be128 r[1 ]; +int i =15 ; + + *r = t->t[ap[15 ]]; +while (i--) { +gf128mul_x8_ble (r); +be128_xor (r, r, &t->t[ap[i]]); + } + *a = *r; +} +EXPORT_SYMBOL (gf128mul_4k_ble); +MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("Functions for multiplying elements of GF(2^128)" );
include/crypto/gf128mul.h +24 -21
127 lines not on this page · raw message
Signature · 1 line
2.8.0.rc3.226.g39d4020
RFC PATCH 4/7 crypto: shash - Add crypto_grab_shash() and crypto_spawn_shash_alg()
In reply to Alex CopeAnalogous to crypto_grab_skcipher() and crypto_spawn_skcipher_alg(), these are useful for algorithms that need to use a shash sub-algorithm, possibly in addition to other sub-algorithms.
--- crypto/shash.c | 8 ++++++++ include/crypto/internal/hash.h | 8 ++++++++ 2 files changed, 16 insertions(+)
crypto/shash.c +8 -0
@@ -536,6 +536,14 @@ void shash_free_instance(struct crypto_instance *inst) }EXPORT_SYMBOL_GPL (shash_free_instance); +int crypto_grab_shash (struct crypto_shash_spawn *spawn, +const char *name,u32 type,u32 mask) +{ + spawn->base.frontend = &crypto_shash_type; +return crypto_grab_spawn (&spawn->base, name, type, mask); +} +EXPORT_SYMBOL_GPL (crypto_grab_shash); +int crypto_init_shash_spawn (struct crypto_shash_spawn *spawn,struct shash_alg *alg,struct crypto_instance *inst)
include/crypto/internal/hash.h +8 -0
@@ -99,6 +99,8 @@ int shash_register_instance(struct crypto_template *tmpl, struct shash_instance *inst);void shash_free_instance (struct crypto_instance *inst); +int crypto_grab_shash (struct crypto_shash_spawn *spawn, +const char *name,u32 type,u32 mask);int crypto_init_shash_spawn (struct crypto_shash_spawn *spawn,struct shash_alg *alg,struct crypto_instance *inst);
@@ -108,6 +110,12 @@ static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)crypto_drop_spawn (&spawn->base); } +static inline struct shash_alg *crypto_spawn_shash_alg ( +struct crypto_shash_spawn *spawn) +{ + return container_of(spawn->base.alg, struct shash_alg, base); +} +struct shash_alg *shash_attr_alg (struct rtattr *rta,u32 type,u32 mask);int shash_ahash_update (struct ahash_request *req,struct shash_desc *desc);
Signature · 1 line
2.8.0.rc3.226.g39d4020
RFC PATCH 5/7 crypto: heh - Add Hash Encrypt Hash(HEH) algorithm
In reply to Alex CopeHash Encrypt Hash (HEH) is a proposed block cipher mode of operation which extends the strong pseudo-random permutation property of block ciphers (e.g. AES) to arbitrary length input strings. This provides a stronger notion of security than existing block cipher modes of operation (e.g. CBC, CTR, XTS), though it is less performant. It uses two keyed invertible hash functions with a layer of ECB encryption applied in-between.
This patch adds HEH as a skcipher. Support for HEH as an AEAD is not yet implemented.
HEH will use existing accelerated ecb(block_cipher) implementation for the encrypt step if available. Accelerated versions of the hash step are planned but not yet implemented.
HEH will be used for filename encryption in ext4 and f2fs.
The algorithm is currently specified by the following Internet Draft:
https://tools.ietf.org/html/draft-cope-heh-00
--- crypto/Kconfig | 17 ++ crypto/Makefile | 1 + crypto/heh.c | 814 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 832 insertions(+) create mode 100644 crypto/heh.c
crypto/Kconfig +17 -0
@@ -316,6 +316,23 @@ config CRYPTO_CBC CBC: Cipher Block Chaining mode This block cipher algorithm is required for IPSec. +config CRYPTO_HEH + tristate "HEH support" + select CRYPTO_CMAC + select CRYPTO_ECB + select CRYPTO_GF128MUL + select CRYPTO_MANAGER + help + HEH: Hash Encrypt Hash mode + HEH is a proposed block cipher mode of operation which extends the + strong pseudo-random permutation (SPRP) property of block ciphers to + arbitrary-length input strings. This provides a stronger notion of + security than existing block cipher modes of operation (e.g. CBC, CTR, + XTS), though it is less performant. Applications include disk + encryption and encryption of file names and contents. Currently, this + implementation only provides a symmetric cipher interface, so it can't + yet be used as an AEAD. + config CRYPTO_CTR tristate "CTR support" select CRYPTO_BLKCIPHER
crypto/Makefile +1 -0
@@ -78,6 +78,7 @@ obj-$(CONFIG_CRYPTO_TGR192) += tgr192.o obj-$(CONFIG_CRYPTO_GF128MUL) += gf128mul.o obj-$(CONFIG_CRYPTO_ECB) += ecb.o obj-$(CONFIG_CRYPTO_CBC) += cbc.o +obj-$(CONFIG_CRYPTO_HEH) += heh.o obj-$(CONFIG_CRYPTO_PCBC) += pcbc.o obj-$(CONFIG_CRYPTO_CTS) += cts.o obj-$(CONFIG_CRYPTO_LRW) += lrw.o
crypto/heh.c new file +814 -0
814 lines not on this page · raw message
Signature · 1 line
2.8.0.rc3.226.g39d4020
RFC PATCH 6/7 crypto: testmgr - Add test vectors for HEH
In reply to Alex CopeAdding test vectors from
https://tools.ietf.org/html/draft-cope-heh-00
--- crypto/testmgr.c | 15 ++++ crypto/testmgr.h | 226 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+)
crypto/testmgr.c +15 -0
@@ -3481,6 +3481,21 @@ static const struct alg_test_desc alg_test_descs[] = { } } }, { + .alg = "heh(aes)", + .test = alg_test_skcipher, + .suite = { + .cipher = { + .enc = { + .vecs = aes_heh_enc_tv_template, + .count = AES_HEH_ENC_TEST_VECTORS + }, + .dec = { + .vecs = aes_heh_dec_tv_template, + .count = AES_HEH_DEC_TEST_VECTORS + } + } + } + }, { .alg = "hmac(crc32)", .test = alg_test_hash, .suite = {
crypto/testmgr.h +226 -0
238 lines not on this page · raw message
Signature · 1 line
2.8.0.rc3.226.g39d4020