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_reduce.cpp | |
parent | 987662117c2b98b12fdbe8e8ab3eaf8c93c748e3 (diff) |
Resolve undefined behavior in Ed25519 code
Use multiplications instead of shifting directly.
Diffstat (limited to 'src/lib/pubkey/ed25519/sc_reduce.cpp')
-rw-r--r-- | src/lib/pubkey/ed25519/sc_reduce.cpp | 142 |
1 files changed, 73 insertions, 69 deletions
diff --git a/src/lib/pubkey/ed25519/sc_reduce.cpp b/src/lib/pubkey/ed25519/sc_reduce.cpp index 93dcc6de8..5765c7dac 100644 --- a/src/lib/pubkey/ed25519/sc_reduce.cpp +++ b/src/lib/pubkey/ed25519/sc_reduce.cpp @@ -24,6 +24,10 @@ Output: void sc_reduce(uint8_t* s) { + const int64_t X20 = (1 << 20); + const int64_t X21 = (1 << 21); + + int64_t s0 = 2097151 & load_3(s); int64_t s1 = 2097151 & (load_4(s + 2) >> 5); int64_t s2 = 2097151 & (load_3(s + 5) >> 2); @@ -114,40 +118,40 @@ void sc_reduce(uint8_t* s) 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; @@ -197,43 +201,43 @@ void sc_reduce(uint8_t* s) 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; @@ -245,40 +249,40 @@ void sc_reduce(uint8_t* s) 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; @@ -290,37 +294,37 @@ void sc_reduce(uint8_t* s) 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; |