diff --git a/wolfcrypt/src/sp_arm32.c b/wolfcrypt/src/sp_arm32.c index 7dddcfcb3..2c32fb0ac 100644 --- a/wolfcrypt/src/sp_arm32.c +++ b/wolfcrypt/src/sp_arm32.c @@ -4908,26 +4908,37 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 32); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -5058,26 +5069,37 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 32); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -7173,26 +7195,37 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 64); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -7323,26 +7356,37 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 64); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -8457,26 +8501,37 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } sp_2048_lshift_64(r, norm, y); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -15667,26 +15722,37 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 48); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -15817,26 +15883,37 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 48); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -18732,26 +18809,37 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 96); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -18882,26 +18970,37 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 96); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -20288,26 +20387,37 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } sp_3072_lshift_96(r, norm, y); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -26719,26 +26829,37 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 128); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -26869,26 +26990,37 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 128); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -28547,26 +28679,37 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } sp_4096_lshift_128(r, norm, y); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } diff --git a/wolfcrypt/src/sp_arm64.c b/wolfcrypt/src/sp_arm64.c index 2a2a4df0b..6a012f071 100644 --- a/wolfcrypt/src/sp_arm64.c +++ b/wolfcrypt/src/sp_arm64.c @@ -3226,26 +3226,37 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 64) { c = 60; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 16); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 60; + y = (int)(n >> 60); n <<= 4; c = 60; } else if (c < 4) { - y = n >> 60; + y = (int)(n >> 60); n = e[i--]; c = 4 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 60) & 0xf; + y = (int)((n >> 60) & 0xf); n <<= 4; c -= 4; } @@ -3376,26 +3387,37 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 64) { c = 59; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 16); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 59; + y = (int)(n >> 59); n <<= 5; c = 59; } else if (c < 5) { - y = n >> 59; + y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -4667,26 +4689,37 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 64) { c = 60; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 32); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 60; + y = (int)(n >> 60); n <<= 4; c = 60; } else if (c < 4) { - y = n >> 60; + y = (int)(n >> 60); n = e[i--]; c = 4 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 60) & 0xf; + y = (int)((n >> 60) & 0xf); n <<= 4; c -= 4; } @@ -4817,26 +4850,37 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 64) { c = 59; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 32); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 59; + y = (int)(n >> 59); n <<= 5; c = 59; } else if (c < 5) { - y = n >> 59; + y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -5655,26 +5699,37 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, if (c == 64) { c = 58; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } sp_2048_lshift_32(r, norm, y); for (; i>=0 || c>=6; ) { if (c == 0) { n = e[i--]; - y = n >> 58; + y = (int)(n >> 58); n <<= 6; c = 58; } else if (c < 6) { - y = n >> 58; + y = (int)(n >> 58); n = e[i--]; c = 6 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 58) & 0x3f; + y = (int)((n >> 58) & 0x3f); n <<= 6; c -= 6; } @@ -10567,26 +10622,37 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 64) { c = 60; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 24); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 60; + y = (int)(n >> 60); n <<= 4; c = 60; } else if (c < 4) { - y = n >> 60; + y = (int)(n >> 60); n = e[i--]; c = 4 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 60) & 0xf; + y = (int)((n >> 60) & 0xf); n <<= 4; c -= 4; } @@ -10717,26 +10783,37 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 64) { c = 59; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 24); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 59; + y = (int)(n >> 59); n <<= 5; c = 59; } else if (c < 5) { - y = n >> 59; + y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -12376,26 +12453,37 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 64) { c = 60; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 48); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 60; + y = (int)(n >> 60); n <<= 4; c = 60; } else if (c < 4) { - y = n >> 60; + y = (int)(n >> 60); n = e[i--]; c = 4 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 60) & 0xf; + y = (int)((n >> 60) & 0xf); n <<= 4; c -= 4; } @@ -12526,26 +12614,37 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 64) { c = 59; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 48); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 59; + y = (int)(n >> 59); n <<= 5; c = 59; } else if (c < 5) { - y = n >> 59; + y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -13488,26 +13587,37 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, if (c == 64) { c = 58; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } sp_3072_lshift_48(r, norm, y); for (; i>=0 || c>=6; ) { if (c == 0) { n = e[i--]; - y = n >> 58; + y = (int)(n >> 58); n <<= 6; c = 58; } else if (c < 6) { - y = n >> 58; + y = (int)(n >> 58); n = e[i--]; c = 6 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 58) & 0x3f; + y = (int)((n >> 58) & 0x3f); n <<= 6; c -= 6; } @@ -17307,26 +17417,37 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 64) { c = 60; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 64); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 60; + y = (int)(n >> 60); n <<= 4; c = 60; } else if (c < 4) { - y = n >> 60; + y = (int)(n >> 60); n = e[i--]; c = 4 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 60) & 0xf; + y = (int)((n >> 60) & 0xf); n <<= 4; c -= 4; } @@ -17457,26 +17578,37 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 64) { c = 59; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 64); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 59; + y = (int)(n >> 59); n <<= 5; c = 59; } else if (c < 5) { - y = n >> 59; + y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -18543,26 +18675,37 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, if (c == 64) { c = 58; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } sp_4096_lshift_64(r, norm, y); for (; i>=0 || c>=6; ) { if (c == 0) { n = e[i--]; - y = n >> 58; + y = (int)(n >> 58); n <<= 6; c = 58; } else if (c < 6) { - y = n >> 58; + y = (int)(n >> 58); n = e[i--]; c = 6 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 58) & 0x3f; + y = (int)((n >> 58) & 0x3f); n <<= 6; c -= 6; } diff --git a/wolfcrypt/src/sp_armthumb.c b/wolfcrypt/src/sp_armthumb.c index ac45da14c..97c4ab04c 100644 --- a/wolfcrypt/src/sp_armthumb.c +++ b/wolfcrypt/src/sp_armthumb.c @@ -3202,26 +3202,37 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 32); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -3352,26 +3363,37 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 32); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -4038,26 +4060,37 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 64); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -4188,26 +4221,37 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 64); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -5163,26 +5207,37 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } sp_2048_lshift_64(r, norm, y); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -9002,26 +9057,37 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 48); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -9152,26 +9218,37 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 48); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -9844,26 +9921,37 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 96); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -9994,26 +10082,37 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 96); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -11167,26 +11266,37 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } sp_3072_lshift_96(r, norm, y); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -13731,26 +13841,37 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 128); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -13881,26 +14002,37 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 128); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -15251,26 +15383,37 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } sp_4096_lshift_128(r, norm, y); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } diff --git a/wolfcrypt/src/sp_cortexm.c b/wolfcrypt/src/sp_cortexm.c index 684412586..b993f18a1 100644 --- a/wolfcrypt/src/sp_cortexm.c +++ b/wolfcrypt/src/sp_cortexm.c @@ -3097,26 +3097,37 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 32); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -3247,26 +3258,37 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 32); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -3792,26 +3814,37 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 64); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -3942,26 +3975,37 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 64); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -4915,26 +4959,37 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } sp_2048_lshift_64(r, norm, y); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -7749,26 +7804,37 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 48); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -7899,26 +7965,37 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 48); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -8447,26 +8524,37 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 96); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -8597,26 +8685,37 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 96); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -9766,26 +9865,37 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } sp_3072_lshift_96(r, norm, y); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -11654,26 +11764,37 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e if (c == 32) { c = 28; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 128); for (; i>=0 || c>=4; ) { if (c == 0) { n = e[i--]; - y = n >> 28; + y = (int)(n >> 28); n <<= 4; c = 28; } else if (c < 4) { - y = n >> 28; + y = (int)(n >> 28); n = e[i--]; c = 4 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 28) & 0xf; + y = (int)((n >> 28) & 0xf); n <<= 4; c -= 4; } @@ -11804,26 +11925,37 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 128); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } @@ -13170,26 +13302,37 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, if (c == 32) { c = 27; } - y = (int)(n >> c); - n <<= 32 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 32 - c; + } sp_4096_lshift_128(r, norm, y); for (; i>=0 || c>=5; ) { if (c == 0) { n = e[i--]; - y = n >> 27; + y = (int)(n >> 27); n <<= 5; c = 27; } else if (c < 5) { - y = n >> 27; + y = (int)(n >> 27); n = e[i--]; c = 5 - c; - y |= n >> (32 - c); + y |= (int)(n >> (32 - c)); n <<= c; c = 32 - c; } else { - y = (n >> 27) & 0x1f; + y = (int)((n >> 27) & 0x1f); n <<= 5; c -= 5; } diff --git a/wolfcrypt/src/sp_x86_64.c b/wolfcrypt/src/sp_x86_64.c index a7dee20b5..759d5e639 100644 --- a/wolfcrypt/src/sp_x86_64.c +++ b/wolfcrypt/src/sp_x86_64.c @@ -483,22 +483,35 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 5. */ if ((bits % 5) == 0) { c -= 5; } else { c -= bits % 5; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 16); for (; i>=0 || c>=5; ) { if (c >= 5) { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -512,7 +525,7 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } @@ -679,22 +692,35 @@ static int sp_2048_mod_exp_avx2_16(sp_digit* r, const sp_digit* a, const sp_digi i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 5. */ if ((bits % 5) == 0) { c -= 5; } else { c -= bits % 5; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 16); for (; i>=0 || c>=5; ) { if (c >= 5) { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -708,7 +734,7 @@ static int sp_2048_mod_exp_avx2_16(sp_digit* r, const sp_digit* a, const sp_digi y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } @@ -1090,22 +1116,35 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 5. */ if ((bits % 5) == 0) { c -= 5; } else { c -= bits % 5; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 32); for (; i>=0 || c>=5; ) { if (c >= 5) { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -1119,7 +1158,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } @@ -1288,22 +1327,35 @@ static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digi i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 5. */ if ((bits % 5) == 0) { c -= 5; } else { c -= bits % 5; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 32); for (; i>=0 || c>=5; ) { if (c >= 5) { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -1317,7 +1369,7 @@ static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digi y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } @@ -1936,18 +1988,31 @@ static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 6. */ if ((bits % 6) == 0) { c -= 6; } else { c -= bits % 6; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } sp_2048_lshift_32(r, norm, y); for (; i>=0 || c>=6; ) { if (c == 0) { @@ -1960,12 +2025,12 @@ static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, y = (int)(n >> 58); n = e[i--]; c = 6 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 58) & 0x3f; + y = (int)((n >> 58) & 0x3f); n <<= 6; c -= 6; } @@ -2046,18 +2111,31 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 6. */ if ((bits % 6) == 0) { c -= 6; } else { c -= bits % 6; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } sp_2048_lshift_32(r, norm, y); for (; i>=0 || c>=6; ) { if (c == 0) { @@ -2070,12 +2148,12 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, y = (int)(n >> 58); n = e[i--]; c = 6 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 58) & 0x3f; + y = (int)((n >> 58) & 0x3f); n <<= 6; c -= 6; } @@ -2679,22 +2757,35 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 5. */ if ((bits % 5) == 0) { c -= 5; } else { c -= bits % 5; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 24); for (; i>=0 || c>=5; ) { if (c >= 5) { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -2708,7 +2799,7 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } @@ -2875,22 +2966,35 @@ static int sp_3072_mod_exp_avx2_24(sp_digit* r, const sp_digit* a, const sp_digi i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 5. */ if ((bits % 5) == 0) { c -= 5; } else { c -= bits % 5; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 24); for (; i>=0 || c>=5; ) { if (c >= 5) { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -2904,7 +3008,7 @@ static int sp_3072_mod_exp_avx2_24(sp_digit* r, const sp_digit* a, const sp_digi y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } @@ -3286,22 +3390,35 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 5. */ if ((bits % 5) == 0) { c -= 5; } else { c -= bits % 5; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 48); for (; i>=0 || c>=5; ) { if (c >= 5) { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -3315,7 +3432,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } @@ -3484,22 +3601,35 @@ static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digi i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 5. */ if ((bits % 5) == 0) { c -= 5; } else { c -= bits % 5; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 48); for (; i>=0 || c>=5; ) { if (c >= 5) { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -3513,7 +3643,7 @@ static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digi y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } @@ -4132,18 +4262,31 @@ static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 6. */ if ((bits % 6) == 0) { c -= 6; } else { c -= bits % 6; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } sp_3072_lshift_48(r, norm, y); for (; i>=0 || c>=6; ) { if (c == 0) { @@ -4156,12 +4299,12 @@ static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, y = (int)(n >> 58); n = e[i--]; c = 6 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 58) & 0x3f; + y = (int)((n >> 58) & 0x3f); n <<= 6; c -= 6; } @@ -4242,18 +4385,31 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 6. */ if ((bits % 6) == 0) { c -= 6; } else { c -= bits % 6; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } sp_3072_lshift_48(r, norm, y); for (; i>=0 || c>=6; ) { if (c == 0) { @@ -4266,12 +4422,12 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, y = (int)(n >> 58); n = e[i--]; c = 6 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 58) & 0x3f; + y = (int)((n >> 58) & 0x3f); n <<= 6; c -= 6; } @@ -4930,22 +5086,35 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 5. */ if ((bits % 5) == 0) { c -= 5; } else { c -= bits % 5; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 64); for (; i>=0 || c>=5; ) { if (c >= 5) { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -4959,7 +5128,7 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } @@ -5128,22 +5297,35 @@ static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digi i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 5. */ if ((bits % 5) == 0) { c -= 5; } else { c -= bits % 5; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } XMEMCPY(r, t[y], sizeof(sp_digit) * 64); for (; i>=0 || c>=5; ) { if (c >= 5) { - y = (n >> 59) & 0x1f; + y = (int)((n >> 59) & 0x1f); n <<= 5; c -= 5; } @@ -5157,7 +5339,7 @@ static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digi y = (int)(n >> 59); n = e[i--]; c = 5 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } @@ -5776,18 +5958,31 @@ static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 6. */ if ((bits % 6) == 0) { c -= 6; } else { c -= bits % 6; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } sp_4096_lshift_64(r, norm, y); for (; i>=0 || c>=6; ) { if (c == 0) { @@ -5800,12 +5995,12 @@ static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, y = (int)(n >> 58); n = e[i--]; c = 6 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 58) & 0x3f; + y = (int)((n >> 58) & 0x3f); n <<= 6; c -= 6; } @@ -5886,18 +6081,31 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, i = (bits - 1) / 64; n = e[i--]; + /* Number of bits available in top word. */ c = bits & 63; if (c == 0) { c = 64; } + /* Minus the number of top bits to use so rest is a multiple of 6. */ if ((bits % 6) == 0) { c -= 6; } else { c -= bits % 6; } - y = (int)(n >> c); - n <<= 64 - c; + if (c < 0) { + /* Number of bits in top word is less than number needed. */ + c = -c; + y = (int)(n << c); + n = e[i--]; + y |= (int)(n >> (64 - c)); + n <<= c; + c = 64 - c; + } + else { + y = (int)(n >> c); + n <<= 64 - c; + } sp_4096_lshift_64(r, norm, y); for (; i>=0 || c>=6; ) { if (c == 0) { @@ -5910,12 +6118,12 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, y = (int)(n >> 58); n = e[i--]; c = 6 - c; - y |= n >> (64 - c); + y |= (int)(n >> (64 - c)); n <<= c; c = 64 - c; } else { - y = (n >> 58) & 0x3f; + y = (int)((n >> 58) & 0x3f); n <<= 6; c -= 6; }