From 17c5b2842fa09d592d57a17a46bf9840a7d94d5a Mon Sep 17 00:00:00 2001 From: Joost Rijneveld Date: Wed, 27 Jan 2016 10:18:31 +0100 Subject: [PATCH] Make address masks more explicit and strict --- hash.c | 4 ++-- wots.c | 4 ++-- xmss.c | 24 ++++++++++++------------ xmss_fast.c | 24 ++++++++++++------------ 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/hash.c b/hash.c index b403cb1..ed76207 100644 --- a/hash.c +++ b/hash.c @@ -13,8 +13,8 @@ Public domain. #include -#define SET_KEY_BIT(a,b) (a[15] = (a[15] & 253) | (b << 1)) -#define SET_BLOCK_BIT(a,b) (a[15] = (a[15] & 254) | b) +#define SET_KEY_BIT(a,b) (a[15] = (a[15] & 253) | ((b << 1) & 2)) +#define SET_BLOCK_BIT(a,b) (a[15] = (a[15] & 254) | (b & 1)) #define WOTS_SELECT_KEY(a) (a[15] = (a[15] & 254) | 1) #define WOTS_SELECT_BLOCK(a) (a[15] = (a[15] & 254) | 0) diff --git a/wots.c b/wots.c index 41e3eec..a5c90a9 100644 --- a/wots.c +++ b/wots.c @@ -17,11 +17,11 @@ Public domain. * in the 16byte hash address */ #define SET_HASH_ADDRESS(a, v) {\ - a[15] = (a[15] & 1) | ((v << 1) & 255);\ + a[15] = (a[15] & 1) | ((v << 1) & 254);\ a[14] = (a[14] & 254) | ((v >> 7) & 1);} #define SET_CHAIN_ADDRESS(a, v) {\ - a[14] = (a[14] & 1) | ((v << 1) & 255);\ + a[14] = (a[14] & 1) | ((v << 1) & 254);\ a[13] = (v >> 7) & 255;\ a[12] = (a[12] & 254) | ((v >> 15) & 1);} diff --git a/xmss.c b/xmss.c index 1c77305..df63d54 100644 --- a/xmss.c +++ b/xmss.c @@ -24,23 +24,23 @@ Public domain. * in the 16byte hash address */ #define SET_LAYER_ADDRESS(a, v) {\ - a[6] = (a[6] & 3) | ((v << 2) & 255);\ - a[5] = (a[5] & 252) | ((v >> 6) & 255);} + a[6] = (a[6] & 3) | ((v << 2) & 252);\ + a[5] = (a[5] & 252) | ((v >> 6) & 3);} #define SET_TREE_ADDRESS(a, v) {\ - a[9] = (a[9] & 3) | ((v << 2) & 255);\ + a[9] = (a[9] & 3) | ((v << 2) & 252);\ a[8] = (v >> 6) & 255;\ a[7] = (v >> 14) & 255;\ - a[6] = (a[6] & 252) | ((v >> 22) & 255);} + a[6] = (a[6] & 252) | ((v >> 22) & 3);} #define SET_OTS_BIT(a, b) {\ - a[9] = (a[9] & 253) | (b << 1);} + a[9] = (a[9] & 253) | ((b << 1) & 2);} #define SET_OTS_ADDRESS(a, v) {\ - a[12] = (a[12] & 1) | ((v << 1) & 255);\ + a[12] = (a[12] & 1) | ((v << 1) & 254);\ a[11] = (v >> 7) & 255;\ a[10] = (v >> 15) & 255;\ - a[9] = (a[9] & 254) | ((v >> 23) & 1);} + a[9] = (a[9] & 254) | ((v >> 23) & 1);} #define ZEROISE_OTS_ADDR(a) {\ a[12] = (a[12] & 254);\ @@ -49,7 +49,7 @@ Public domain. a[15] = 0;} #define SET_LTREE_BIT(a, b) {\ - a[9] = (a[9] & 254) | b;} + a[9] = (a[9] & 254) | (b & 1);} #define SET_LTREE_ADDRESS(a, v) {\ a[12] = v & 255;\ @@ -57,10 +57,10 @@ Public domain. a[10] = (v >> 16) & 255;} #define SET_LTREE_TREE_HEIGHT(a, v) {\ - a[13] = (a[13] & 3) | ((v << 2) & 255);} + a[13] = (a[13] & 3) | ((v << 2) & 252);} #define SET_LTREE_TREE_INDEX(a, v) {\ - a[15] = (a[15] & 3) | ((v << 2) & 255);\ + a[15] = (a[15] & 3) | ((v << 2) & 252);\ a[14] = (v >> 6) & 255;\ a[13] = (a[13] & 252) | ((v >> 14) & 3);} @@ -69,11 +69,11 @@ Public domain. a[11] = a[11] & 3;} #define SET_NODE_TREE_HEIGHT(a, v) {\ - a[12] = (a[12] & 3) | ((v << 2) & 255);\ + a[12] = (a[12] & 3) | ((v << 2) & 252);\ a[11] = (a[11] & 252) | ((v >> 6) & 3);} #define SET_NODE_TREE_INDEX(a, v) {\ - a[15] = (a[15] & 3) | ((v << 2) & 255);\ + a[15] = (a[15] & 3) | ((v << 2) & 252);\ a[14] = (v >> 6) & 255;\ a[13] = (v >> 14) & 255;\ a[12] = (a[12] & 252) | ((v >> 22) & 3);} diff --git a/xmss_fast.c b/xmss_fast.c index f833029..dd656d7 100644 --- a/xmss_fast.c +++ b/xmss_fast.c @@ -24,23 +24,23 @@ Public domain. * in the 16byte hash address */ #define SET_LAYER_ADDRESS(a, v) {\ - a[6] = (a[6] & 3) | ((v << 2) & 255);\ - a[5] = (a[5] & 252) | ((v >> 6) & 255);} + a[6] = (a[6] & 3) | ((v << 2) & 252);\ + a[5] = (a[5] & 252) | ((v >> 6) & 3);} #define SET_TREE_ADDRESS(a, v) {\ - a[9] = (a[9] & 3) | ((v << 2) & 255);\ + a[9] = (a[9] & 3) | ((v << 2) & 252);\ a[8] = (v >> 6) & 255;\ a[7] = (v >> 14) & 255;\ - a[6] = (a[6] & 252) | ((v >> 22) & 255);} + a[6] = (a[6] & 252) | ((v >> 22) & 3);} #define SET_OTS_BIT(a, b) {\ - a[9] = (a[9] & 253) | (b << 1);} + a[9] = (a[9] & 253) | ((b << 1) & 2);} #define SET_OTS_ADDRESS(a, v) {\ - a[12] = (a[12] & 1) | ((v << 1) & 255);\ + a[12] = (a[12] & 1) | ((v << 1) & 254);\ a[11] = (v >> 7) & 255;\ a[10] = (v >> 15) & 255;\ - a[9] = (a[9] & 254) | ((v >> 23) & 1);} + a[9] = (a[9] & 254) | ((v >> 23) & 1);} #define ZEROISE_OTS_ADDR(a) {\ a[12] = (a[12] & 254);\ @@ -49,7 +49,7 @@ Public domain. a[15] = 0;} #define SET_LTREE_BIT(a, b) {\ - a[9] = (a[9] & 254) | b;} + a[9] = (a[9] & 254) | (b & 1);} #define SET_LTREE_ADDRESS(a, v) {\ a[12] = v & 255;\ @@ -57,10 +57,10 @@ Public domain. a[10] = (v >> 16) & 255;} #define SET_LTREE_TREE_HEIGHT(a, v) {\ - a[13] = (a[13] & 3) | ((v << 2) & 255);} + a[13] = (a[13] & 3) | ((v << 2) & 252);} #define SET_LTREE_TREE_INDEX(a, v) {\ - a[15] = (a[15] & 3) | ((v << 2) & 255);\ + a[15] = (a[15] & 3) | ((v << 2) & 252);\ a[14] = (v >> 6) & 255;\ a[13] = (a[13] & 252) | ((v >> 14) & 3);} @@ -69,11 +69,11 @@ Public domain. a[11] = a[11] & 3;} #define SET_NODE_TREE_HEIGHT(a, v) {\ - a[12] = (a[12] & 3) | ((v << 2) & 255);\ + a[12] = (a[12] & 3) | ((v << 2) & 252);\ a[11] = (a[11] & 252) | ((v >> 6) & 3);} #define SET_NODE_TREE_INDEX(a, v) {\ - a[15] = (a[15] & 3) | ((v << 2) & 255);\ + a[15] = (a[15] & 3) | ((v << 2) & 252);\ a[14] = (v >> 6) & 255;\ a[13] = (v >> 14) & 255;\ a[12] = (a[12] & 252) | ((v >> 22) & 3);}