Skip to content

Commit

Permalink
Cast STORE64H/STORE64L argument to ulong64
Browse files Browse the repository at this point in the history
Avoids undefined behaviour with right shift greater than 32 bits
  • Loading branch information
mkj committed May 29, 2020
1 parent 19c6e79 commit fb6ecbb
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/headers/tomcrypt_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
((ulong32)((y)[0] & 255)); } while(0)

#define STORE64L(x, y) \
do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
(y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
(y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
do { (y)[7] = (unsigned char)(((ulong64)(x)>>56)&255); (y)[6] = (unsigned char)(((ulong64)(x)>>48)&255); \
(y)[5] = (unsigned char)(((ulong64)(x)>>40)&255); (y)[4] = (unsigned char)(((ulong64)(x)>>32)&255); \
(y)[3] = (unsigned char)(((ulong64)(x)>>24)&255); (y)[2] = (unsigned char)(((ulong64)(x)>>16)&255); \
(y)[1] = (unsigned char)(((ulong64)(x)>>8)&255); (y)[0] = (unsigned char)((ulong64)(x)&255); } while(0)

#define LOAD64L(x, y) \
do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
Expand All @@ -43,10 +43,10 @@
((ulong32)((y)[3] & 255)); } while(0)

#define STORE64H(x, y) \
do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
(y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
(y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
(y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
do { (y)[0] = (unsigned char)(((ulong64)(x)>>56)&255); (y)[1] = (unsigned char)(((ulong64)(x)>>48)&255); \
(y)[2] = (unsigned char)(((ulong64)(x)>>40)&255); (y)[3] = (unsigned char)(((ulong64)(x)>>32)&255); \
(y)[4] = (unsigned char)(((ulong64)(x)>>24)&255); (y)[5] = (unsigned char)(((ulong64)(x)>>16)&255); \
(y)[6] = (unsigned char)(((ulong64)(x)>>8)&255); (y)[7] = (unsigned char)((ulong64)(x)&255); } while(0)

#define LOAD64H(x, y) \
do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
Expand Down Expand Up @@ -125,10 +125,10 @@ asm __volatile__ ( \
#else

#define STORE64H(x, y) \
do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
(y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
(y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
(y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
do { (y)[0] = (unsigned char)(((ulong64)(x)>>56)&255); (y)[1] = (unsigned char)(((ulong64)(x)>>48)&255); \
(y)[2] = (unsigned char)(((ulong64)(x)>>40)&255); (y)[3] = (unsigned char)(((ulong64)(x)>>32)&255); \
(y)[4] = (unsigned char)(((ulong64)(x)>>24)&255); (y)[5] = (unsigned char)(((ulong64)(x)>>16)&255); \
(y)[6] = (unsigned char)(((ulong64)(x)>>8)&255); (y)[7] = (unsigned char)((ulong64)(x)&255); } while(0)

#define LOAD64H(x, y) \
do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
Expand All @@ -147,10 +147,10 @@ do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
do { XMEMCPY(&(x), y, 4); } while(0)

#define STORE64L(x, y) \
do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
(y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
(y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
do { (y)[7] = (unsigned char)(((ulong64)(x))>>56)&255); (y)[6] = (unsigned char)((((ulong64)(x))>>48)&255); \
(y)[5] = (unsigned char)(((ulong64)(x))>>40)&255); (y)[4] = (unsigned char)((((ulong64)(x))>>32)&255); \
(y)[3] = (unsigned char)(((ulong64)(x))>>24)&255); (y)[2] = (unsigned char)((((ulong64)(x))>>16)&255); \
(y)[1] = (unsigned char)(((ulong64)(x))>>8)&255); (y)[0] = (unsigned char)(((ulong64)(x))&255); } while(0)

#define LOAD64L(x, y) \
do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
Expand Down Expand Up @@ -187,10 +187,10 @@ do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
((ulong32)((y)[0] & 255)); } while(0)

#define STORE64L(x, y) \
do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
(y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
(y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
do { (y)[7] = (unsigned char)((((ulong64)(x))>>56)&255); (y)[6] = (unsigned char)((((ulong64)(x))>>48)&255); \
(y)[5] = (unsigned char)((((ulong64)(x))>>40)&255); (y)[4] = (unsigned char)((((ulong64)(x))>>32)&255); \
(y)[3] = (unsigned char)((((ulong64)(x))>>24)&255); (y)[2] = (unsigned char)((((ulong64)(x))>>16)&255); \
(y)[1] = (unsigned char)((((ulong64)(x))>>8)&255); (y)[0] = (unsigned char)(((ulong64)(x))&255); } while(0)

#define LOAD64L(x, y) \
do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
Expand All @@ -207,10 +207,10 @@ do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
do { XMEMCPY(&(x), y, 4); } while(0)

#define STORE64H(x, y) \
do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
(y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
(y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
(y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
do { (y)[0] = (unsigned char)(((ulong64)(x)>>56)&255); (y)[1] = (unsigned char)(((ulong64)(x)>>48)&255); \
(y)[2] = (unsigned char)(((ulong64)(x)>>40)&255); (y)[3] = (unsigned char)(((ulong64)(x)>>32)&255); \
(y)[4] = (unsigned char)(((ulong64)(x)>>24)&255); (y)[5] = (unsigned char)(((ulong64)(x)>>16)&255); \
(y)[6] = (unsigned char)(((ulong64)(x)>>8)&255); (y)[7] = (unsigned char)((ulong64)(x)&255); } while(0)

#define LOAD64H(x, y) \
do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
Expand Down

0 comments on commit fb6ecbb

Please sign in to comment.