diff options
author | Jack Lloyd <[email protected]> | 2017-12-31 14:32:06 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-12-31 14:32:43 -0500 |
commit | 342596073420efc7000a161f69e5a6928d6a2e53 (patch) | |
tree | 42753e232352119aeb2bcab0d111b80452e44a85 /src/lib/pubkey/ed25519/sc_muladd.cpp | |
parent | 987662117c2b98b12fdbe8e8ab3eaf8c93c748e3 (diff) |
Resolve undefined behavior in Ed25519 code
Use multiplications instead of shifting directly.
Diffstat (limited to 'src/lib/pubkey/ed25519/sc_muladd.cpp')
-rw-r--r-- | src/lib/pubkey/ed25519/sc_muladd.cpp | 233 |
1 files changed, 118 insertions, 115 deletions
diff --git a/src/lib/pubkey/ed25519/sc_muladd.cpp b/src/lib/pubkey/ed25519/sc_muladd.cpp index e4dcf3401..83863eeb4 100644 --- a/src/lib/pubkey/ed25519/sc_muladd.cpp +++ b/src/lib/pubkey/ed25519/sc_muladd.cpp @@ -25,6 +25,9 @@ Output: void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c) { + const int64_t X20 = (1 << 20); + const int64_t X21 = (1 << 21); + int64_t a0 = 2097151 & load_3(a); int64_t a1 = 2097151 & (load_4(a + 2) >> 5); int64_t a2 = 2097151 & (load_3(a + 5) >> 2); @@ -134,76 +137,76 @@ void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c) s22 = a11*b11; s23 = 0; - carry0 = (s0 + (1<<20)) >> 21; + carry0 = (s0 + X20) >> 21; s1 += carry0; - s0 -= carry0 << 21; - carry2 = (s2 + (1<<20)) >> 21; + s0 -= carry0 * X21; + carry2 = (s2 + X20) >> 21; s3 += carry2; - s2 -= carry2 << 21; - carry4 = (s4 + (1<<20)) >> 21; + s2 -= carry2 * X21; + carry4 = (s4 + X20) >> 21; s5 += carry4; - s4 -= carry4 << 21; - carry6 = (s6 + (1<<20)) >> 21; + s4 -= carry4 * X21; + carry6 = (s6 + X20) >> 21; s7 += carry6; - s6 -= carry6 << 21; - carry8 = (s8 + (1<<20)) >> 21; + s6 -= carry6 * X21; + carry8 = (s8 + X20) >> 21; s9 += carry8; - s8 -= carry8 << 21; - carry10 = (s10 + (1<<20)) >> 21; + s8 -= carry8 * X21; + carry10 = (s10 + X20) >> 21; s11 += carry10; - s10 -= carry10 << 21; - carry12 = (s12 + (1<<20)) >> 21; + s10 -= carry10 * X21; + carry12 = (s12 + X20) >> 21; s13 += carry12; - s12 -= carry12 << 21; - carry14 = (s14 + (1<<20)) >> 21; + s12 -= carry12 * X21; + carry14 = (s14 + X20) >> 21; s15 += carry14; - s14 -= carry14 << 21; - carry16 = (s16 + (1<<20)) >> 21; + s14 -= carry14 * X21; + carry16 = (s16 + X20) >> 21; s17 += carry16; - s16 -= carry16 << 21; - carry18 = (s18 + (1<<20)) >> 21; + s16 -= carry16 * X21; + carry18 = (s18 + X20) >> 21; s19 += carry18; - s18 -= carry18 << 21; - carry20 = (s20 + (1<<20)) >> 21; + s18 -= carry18 * X21; + carry20 = (s20 + X20) >> 21; s21 += carry20; - s20 -= carry20 << 21; - carry22 = (s22 + (1<<20)) >> 21; + s20 -= carry20 * X21; + carry22 = (s22 + X20) >> 21; s23 += carry22; - s22 -= carry22 << 21; + s22 -= carry22 * X21; - carry1 = (s1 + (1<<20)) >> 21; + carry1 = (s1 + X20) >> 21; s2 += carry1; - s1 -= carry1 << 21; - carry3 = (s3 + (1<<20)) >> 21; + s1 -= carry1 * X21; + carry3 = (s3 + X20) >> 21; s4 += carry3; - s3 -= carry3 << 21; - carry5 = (s5 + (1<<20)) >> 21; + s3 -= carry3 * X21; + carry5 = (s5 + X20) >> 21; s6 += carry5; - s5 -= carry5 << 21; - carry7 = (s7 + (1<<20)) >> 21; + s5 -= carry5 * X21; + carry7 = (s7 + X20) >> 21; s8 += carry7; - s7 -= carry7 << 21; - carry9 = (s9 + (1<<20)) >> 21; + s7 -= carry7 * X21; + carry9 = (s9 + X20) >> 21; s10 += carry9; - s9 -= carry9 << 21; - carry11 = (s11 + (1<<20)) >> 21; + s9 -= carry9 * X21; + carry11 = (s11 + X20) >> 21; s12 += carry11; - s11 -= carry11 << 21; - carry13 = (s13 + (1<<20)) >> 21; + s11 -= carry11 * X21; + carry13 = (s13 + X20) >> 21; s14 += carry13; - s13 -= carry13 << 21; - carry15 = (s15 + (1<<20)) >> 21; + s13 -= carry13 * X21; + carry15 = (s15 + X20) >> 21; s16 += carry15; - s15 -= carry15 << 21; - carry17 = (s17 + (1<<20)) >> 21; + s15 -= carry15 * X21; + carry17 = (s17 + X20) >> 21; s18 += carry17; - s17 -= carry17 << 21; - carry19 = (s19 + (1<<20)) >> 21; + s17 -= carry17 * X21; + carry19 = (s19 + X20) >> 21; s20 += carry19; - s19 -= carry19 << 21; - carry21 = (s21 + (1<<20)) >> 21; + s19 -= carry19 * X21; + carry21 = (s21 + X20) >> 21; s22 += carry21; - s21 -= carry21 << 21; + s21 -= carry21 * X21; s11 += s23 * 666643; s12 += s23 * 470296; @@ -253,40 +256,40 @@ void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c) s11 -= s18 * 683901; s18 = 0; - carry6 = (s6 + (1<<20)) >> 21; + carry6 = (s6 + X20) >> 21; s7 += carry6; - s6 -= carry6 << 21; - carry8 = (s8 + (1<<20)) >> 21; + s6 -= carry6 * X21; + carry8 = (s8 + X20) >> 21; s9 += carry8; - s8 -= carry8 << 21; - carry10 = (s10 + (1<<20)) >> 21; + s8 -= carry8 * X21; + carry10 = (s10 + X20) >> 21; s11 += carry10; - s10 -= carry10 << 21; - carry12 = (s12 + (1<<20)) >> 21; + s10 -= carry10 * X21; + carry12 = (s12 + X20) >> 21; s13 += carry12; - s12 -= carry12 << 21; - carry14 = (s14 + (1<<20)) >> 21; + s12 -= carry12 * X21; + carry14 = (s14 + X20) >> 21; s15 += carry14; - s14 -= carry14 << 21; - carry16 = (s16 + (1<<20)) >> 21; + s14 -= carry14 * X21; + carry16 = (s16 + X20) >> 21; s17 += carry16; - s16 -= carry16 << 21; + s16 -= carry16 * X21; - carry7 = (s7 + (1<<20)) >> 21; + carry7 = (s7 + X20) >> 21; s8 += carry7; - s7 -= carry7 << 21; - carry9 = (s9 + (1<<20)) >> 21; + s7 -= carry7 * X21; + carry9 = (s9 + X20) >> 21; s10 += carry9; - s9 -= carry9 << 21; - carry11 = (s11 + (1<<20)) >> 21; + s9 -= carry9 * X21; + carry11 = (s11 + X20) >> 21; s12 += carry11; - s11 -= carry11 << 21; - carry13 = (s13 + (1<<20)) >> 21; + s11 -= carry11 * X21; + carry13 = (s13 + X20) >> 21; s14 += carry13; - s13 -= carry13 << 21; - carry15 = (s15 + (1<<20)) >> 21; + s13 -= carry13 * X21; + carry15 = (s15 + X20) >> 21; s16 += carry15; - s15 -= carry15 << 21; + s15 -= carry15 * X21; s5 += s17 * 666643; s6 += s17 * 470296; @@ -336,43 +339,43 @@ void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c) s5 -= s12 * 683901; s12 = 0; - carry0 = (s0 + (1<<20)) >> 21; + carry0 = (s0 + X20) >> 21; s1 += carry0; - s0 -= carry0 << 21; - carry2 = (s2 + (1<<20)) >> 21; + s0 -= carry0 * X21; + carry2 = (s2 + X20) >> 21; s3 += carry2; - s2 -= carry2 << 21; - carry4 = (s4 + (1<<20)) >> 21; + s2 -= carry2 * X21; + carry4 = (s4 + X20) >> 21; s5 += carry4; - s4 -= carry4 << 21; - carry6 = (s6 + (1<<20)) >> 21; + s4 -= carry4 * X21; + carry6 = (s6 + X20) >> 21; s7 += carry6; - s6 -= carry6 << 21; - carry8 = (s8 + (1<<20)) >> 21; + s6 -= carry6 * X21; + carry8 = (s8 + X20) >> 21; s9 += carry8; - s8 -= carry8 << 21; - carry10 = (s10 + (1<<20)) >> 21; + s8 -= carry8 * X21; + carry10 = (s10 + X20) >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= carry10 * X21; - carry1 = (s1 + (1<<20)) >> 21; + carry1 = (s1 + X20) >> 21; s2 += carry1; - s1 -= carry1 << 21; - carry3 = (s3 + (1<<20)) >> 21; + s1 -= carry1 * X21; + carry3 = (s3 + X20) >> 21; s4 += carry3; - s3 -= carry3 << 21; - carry5 = (s5 + (1<<20)) >> 21; + s3 -= carry3 * X21; + carry5 = (s5 + X20) >> 21; s6 += carry5; - s5 -= carry5 << 21; - carry7 = (s7 + (1<<20)) >> 21; + s5 -= carry5 * X21; + carry7 = (s7 + X20) >> 21; s8 += carry7; - s7 -= carry7 << 21; - carry9 = (s9 + (1<<20)) >> 21; + s7 -= carry7 * X21; + carry9 = (s9 + X20) >> 21; s10 += carry9; - s9 -= carry9 << 21; - carry11 = (s11 + (1<<20)) >> 21; + s9 -= carry9 * X21; + carry11 = (s11 + X20) >> 21; s12 += carry11; - s11 -= carry11 << 21; + s11 -= carry11 * X21; s0 += s12 * 666643; s1 += s12 * 470296; @@ -384,40 +387,40 @@ void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c) carry0 = s0 >> 21; s1 += carry0; - s0 -= carry0 << 21; + s0 -= carry0 * X21; carry1 = s1 >> 21; s2 += carry1; - s1 -= carry1 << 21; + s1 -= carry1 * X21; carry2 = s2 >> 21; s3 += carry2; - s2 -= carry2 << 21; + s2 -= carry2 * X21; carry3 = s3 >> 21; s4 += carry3; - s3 -= carry3 << 21; + s3 -= carry3 * X21; carry4 = s4 >> 21; s5 += carry4; - s4 -= carry4 << 21; + s4 -= carry4 * X21; carry5 = s5 >> 21; s6 += carry5; - s5 -= carry5 << 21; + s5 -= carry5 * X21; carry6 = s6 >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= carry6 * X21; carry7 = s7 >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= carry7 * X21; carry8 = s8 >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= carry8 * X21; carry9 = s9 >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= carry9 * X21; carry10 = s10 >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= carry10 * X21; carry11 = s11 >> 21; s12 += carry11; - s11 -= carry11 << 21; + s11 -= carry11 * X21; s0 += s12 * 666643; s1 += s12 * 470296; @@ -429,37 +432,37 @@ void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c) carry0 = s0 >> 21; s1 += carry0; - s0 -= carry0 << 21; + s0 -= carry0 * X21; carry1 = s1 >> 21; s2 += carry1; - s1 -= carry1 << 21; + s1 -= carry1 * X21; carry2 = s2 >> 21; s3 += carry2; - s2 -= carry2 << 21; + s2 -= carry2 * X21; carry3 = s3 >> 21; s4 += carry3; - s3 -= carry3 << 21; + s3 -= carry3 * X21; carry4 = s4 >> 21; s5 += carry4; - s4 -= carry4 << 21; + s4 -= carry4 * X21; carry5 = s5 >> 21; s6 += carry5; - s5 -= carry5 << 21; + s5 -= carry5 * X21; carry6 = s6 >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= carry6 * X21; carry7 = s7 >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= carry7 * X21; carry8 = s8 >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= carry8 * X21; carry9 = s9 >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= carry9 * X21; carry10 = s10 >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= carry10 * X21; s[0] = s0 >> 0; s[1] = s0 >> 8; |