diff options
author | Nick Mathewson <nickm@torproject.org> | 2014-09-28 20:39:09 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2014-09-28 20:41:05 -0400 |
commit | 6129ff320e6510a453922dba01163824923bc782 (patch) | |
tree | 59867adf2fffea55fa8d6ff8e8d46d20476fb668 /src/ext/ed25519/ref10/sc_reduce.c | |
parent | 6b155dc1a6c7c7bd345514a31288c260e4588216 (diff) | |
download | tor-6129ff320e6510a453922dba01163824923bc782.tar.gz tor-6129ff320e6510a453922dba01163824923bc782.zip |
Use SHL{8,32,64} in ed25519/ref10 to avoid left-shifting negative values
This helps us avoid undefined behavior. It's based on a patch from teor,
except that I wrote a perl script to regenerate the patch:
#!/usr/bin/perl -p -w -i
BEGIN { %vartypes = (); }
if (/^[{}]/) {
%vartypes = ();
}
if (/^ *crypto_int(\d+) +([a-zA-Z_][_a-zA-Z0-9]*)/) {
$vartypes{$2} = $1;
} elsif (/^ *(?:signed +)char +([a-zA-Z_][_a-zA-Z0-9]*)/) {
$vartypes{$1} = '8';
}
# This fixes at most one shift per line. But that's all the code does.
if (/([a-zA-Z_][a-zA-Z_0-9]*) *<< *(\d+)/) {
$v = $1;
if (exists $vartypes{$v}) {
s/$v *<< *(\d+)/SHL$vartypes{$v}($v,$1)/;
}
}
# remove extra parenthesis
s/\(SHL64\((.*)\)\)/SHL64\($1\)/;
s/\(SHL32\((.*)\)\)/SHL32\($1\)/;
s/\(SHL8\((.*)\)\)/SHL8\($1\)/;
Diffstat (limited to 'src/ext/ed25519/ref10/sc_reduce.c')
-rw-r--r-- | src/ext/ed25519/ref10/sc_reduce.c | 112 |
1 files changed, 56 insertions, 56 deletions
diff --git a/src/ext/ed25519/ref10/sc_reduce.c b/src/ext/ed25519/ref10/sc_reduce.c index d01f5a5737..c5afa53741 100644 --- a/src/ext/ed25519/ref10/sc_reduce.c +++ b/src/ext/ed25519/ref10/sc_reduce.c @@ -124,18 +124,18 @@ void sc_reduce(unsigned char *s) s11 -= s18 * 683901; s18 = 0; - carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21; - carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21; - carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21; - carry12 = (s12 + (1<<20)) >> 21; s13 += carry12; s12 -= carry12 << 21; - carry14 = (s14 + (1<<20)) >> 21; s15 += carry14; s14 -= carry14 << 21; - carry16 = (s16 + (1<<20)) >> 21; s17 += carry16; s16 -= carry16 << 21; + carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= SHL64(carry6,21); + carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= SHL64(carry8,21); + carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= SHL64(carry10,21); + carry12 = (s12 + (1<<20)) >> 21; s13 += carry12; s12 -= SHL64(carry12,21); + carry14 = (s14 + (1<<20)) >> 21; s15 += carry14; s14 -= SHL64(carry14,21); + carry16 = (s16 + (1<<20)) >> 21; s17 += carry16; s16 -= SHL64(carry16,21); - carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21; - carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21; - carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21; - carry13 = (s13 + (1<<20)) >> 21; s14 += carry13; s13 -= carry13 << 21; - carry15 = (s15 + (1<<20)) >> 21; s16 += carry15; s15 -= carry15 << 21; + carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= SHL64(carry7,21); + carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= SHL64(carry9,21); + carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= SHL64(carry11,21); + carry13 = (s13 + (1<<20)) >> 21; s14 += carry13; s13 -= SHL64(carry13,21); + carry15 = (s15 + (1<<20)) >> 21; s16 += carry15; s15 -= SHL64(carry15,21); s5 += s17 * 666643; s6 += s17 * 470296; @@ -185,19 +185,19 @@ void sc_reduce(unsigned char *s) s5 -= s12 * 683901; s12 = 0; - carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= carry0 << 21; - carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= carry2 << 21; - carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= carry4 << 21; - carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21; - carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21; - carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21; + carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= SHL64(carry0,21); + carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= SHL64(carry2,21); + carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= SHL64(carry4,21); + carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= SHL64(carry6,21); + carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= SHL64(carry8,21); + carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= SHL64(carry10,21); - carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= carry1 << 21; - carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= carry3 << 21; - carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= carry5 << 21; - carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21; - carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21; - carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21; + carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= SHL64(carry1,21); + carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= SHL64(carry3,21); + carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= SHL64(carry5,21); + carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= SHL64(carry7,21); + carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= SHL64(carry9,21); + carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= SHL64(carry11,21); s0 += s12 * 666643; s1 += s12 * 470296; @@ -207,18 +207,18 @@ void sc_reduce(unsigned char *s) s5 -= s12 * 683901; s12 = 0; - carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21; - carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21; - carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21; - carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21; - carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21; - carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21; - carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21; - carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21; - carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21; - carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21; - carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21; - carry11 = s11 >> 21; s12 += carry11; s11 -= carry11 << 21; + carry0 = s0 >> 21; s1 += carry0; s0 -= SHL64(carry0,21); + carry1 = s1 >> 21; s2 += carry1; s1 -= SHL64(carry1,21); + carry2 = s2 >> 21; s3 += carry2; s2 -= SHL64(carry2,21); + carry3 = s3 >> 21; s4 += carry3; s3 -= SHL64(carry3,21); + carry4 = s4 >> 21; s5 += carry4; s4 -= SHL64(carry4,21); + carry5 = s5 >> 21; s6 += carry5; s5 -= SHL64(carry5,21); + carry6 = s6 >> 21; s7 += carry6; s6 -= SHL64(carry6,21); + carry7 = s7 >> 21; s8 += carry7; s7 -= SHL64(carry7,21); + carry8 = s8 >> 21; s9 += carry8; s8 -= SHL64(carry8,21); + carry9 = s9 >> 21; s10 += carry9; s9 -= SHL64(carry9,21); + carry10 = s10 >> 21; s11 += carry10; s10 -= SHL64(carry10,21); + carry11 = s11 >> 21; s12 += carry11; s11 -= SHL64(carry11,21); s0 += s12 * 666643; s1 += s12 * 470296; @@ -228,47 +228,47 @@ void sc_reduce(unsigned char *s) s5 -= s12 * 683901; s12 = 0; - carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21; - carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21; - carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21; - carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21; - carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21; - carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21; - carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21; - carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21; - carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21; - carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21; - carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21; + carry0 = s0 >> 21; s1 += carry0; s0 -= SHL64(carry0,21); + carry1 = s1 >> 21; s2 += carry1; s1 -= SHL64(carry1,21); + carry2 = s2 >> 21; s3 += carry2; s2 -= SHL64(carry2,21); + carry3 = s3 >> 21; s4 += carry3; s3 -= SHL64(carry3,21); + carry4 = s4 >> 21; s5 += carry4; s4 -= SHL64(carry4,21); + carry5 = s5 >> 21; s6 += carry5; s5 -= SHL64(carry5,21); + carry6 = s6 >> 21; s7 += carry6; s6 -= SHL64(carry6,21); + carry7 = s7 >> 21; s8 += carry7; s7 -= SHL64(carry7,21); + carry8 = s8 >> 21; s9 += carry8; s8 -= SHL64(carry8,21); + carry9 = s9 >> 21; s10 += carry9; s9 -= SHL64(carry9,21); + carry10 = s10 >> 21; s11 += carry10; s10 -= SHL64(carry10,21); s[0] = s0 >> 0; s[1] = s0 >> 8; - s[2] = (s0 >> 16) | (s1 << 5); + s[2] = (s0 >> 16) | SHL64(s1,5); s[3] = s1 >> 3; s[4] = s1 >> 11; - s[5] = (s1 >> 19) | (s2 << 2); + s[5] = (s1 >> 19) | SHL64(s2,2); s[6] = s2 >> 6; - s[7] = (s2 >> 14) | (s3 << 7); + s[7] = (s2 >> 14) | SHL64(s3,7); s[8] = s3 >> 1; s[9] = s3 >> 9; - s[10] = (s3 >> 17) | (s4 << 4); + s[10] = (s3 >> 17) | SHL64(s4,4); s[11] = s4 >> 4; s[12] = s4 >> 12; - s[13] = (s4 >> 20) | (s5 << 1); + s[13] = (s4 >> 20) | SHL64(s5,1); s[14] = s5 >> 7; - s[15] = (s5 >> 15) | (s6 << 6); + s[15] = (s5 >> 15) | SHL64(s6,6); s[16] = s6 >> 2; s[17] = s6 >> 10; - s[18] = (s6 >> 18) | (s7 << 3); + s[18] = (s6 >> 18) | SHL64(s7,3); s[19] = s7 >> 5; s[20] = s7 >> 13; s[21] = s8 >> 0; s[22] = s8 >> 8; - s[23] = (s8 >> 16) | (s9 << 5); + s[23] = (s8 >> 16) | SHL64(s9,5); s[24] = s9 >> 3; s[25] = s9 >> 11; - s[26] = (s9 >> 19) | (s10 << 2); + s[26] = (s9 >> 19) | SHL64(s10,2); s[27] = s10 >> 6; - s[28] = (s10 >> 14) | (s11 << 7); + s[28] = (s10 >> 14) | SHL64(s11,7); s[29] = s11 >> 1; s[30] = s11 >> 9; s[31] = s11 >> 17; |