Skip to content

Commit

Permalink
Merge pull request #2 from vzsg/negative-shift
Browse files Browse the repository at this point in the history
Fix undefined behavior by replacing some shifts and typecasts
  • Loading branch information
vzsg authored Oct 4, 2017
2 parents 06a1b45 + cdfe62f commit 0d2cfe8
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 214 deletions.
196 changes: 98 additions & 98 deletions Sources/CEd25519/fe.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,34 +318,34 @@ void fe_frombytes(fe h, const unsigned char *s) {

carry9 = (h9 + (int64_t) (1 << 24)) >> 25;
h0 += carry9 * 19;
h9 -= carry9 << 25;
h9 -= carry9 * (1 << 25);
carry1 = (h1 + (int64_t) (1 << 24)) >> 25;
h2 += carry1;
h1 -= carry1 << 25;
h1 -= carry1 * (1 << 25);
carry3 = (h3 + (int64_t) (1 << 24)) >> 25;
h4 += carry3;
h3 -= carry3 << 25;
h3 -= carry3 * (1 << 25);
carry5 = (h5 + (int64_t) (1 << 24)) >> 25;
h6 += carry5;
h5 -= carry5 << 25;
h5 -= carry5 * (1 << 25);
carry7 = (h7 + (int64_t) (1 << 24)) >> 25;
h8 += carry7;
h7 -= carry7 << 25;
h7 -= carry7 * (1 << 25);
carry0 = (h0 + (int64_t) (1 << 25)) >> 26;
h1 += carry0;
h0 -= carry0 << 26;
h0 -= carry0 * (1 << 26);
carry2 = (h2 + (int64_t) (1 << 25)) >> 26;
h3 += carry2;
h2 -= carry2 << 26;
h2 -= carry2 * (1 << 26);
carry4 = (h4 + (int64_t) (1 << 25)) >> 26;
h5 += carry4;
h4 -= carry4 << 26;
h4 -= carry4 * (1 << 26);
carry6 = (h6 + (int64_t) (1 << 25)) >> 26;
h7 += carry6;
h6 -= carry6 << 26;
h6 -= carry6 * (1 << 26);
carry8 = (h8 + (int64_t) (1 << 25)) >> 26;
h9 += carry8;
h8 -= carry8 << 26;
h8 -= carry8 * (1 << 26);

h[0] = (int32_t) h0;
h[1] = (int32_t) h1;
Expand Down Expand Up @@ -711,46 +711,46 @@ void fe_mul(fe h, const fe f, const fe g) {

carry0 = (h0 + (int64_t) (1 << 25)) >> 26;
h1 += carry0;
h0 -= carry0 << 26;
h0 -= carry0 * (1 << 26);
carry4 = (h4 + (int64_t) (1 << 25)) >> 26;
h5 += carry4;
h4 -= carry4 << 26;
h4 -= carry4 * (1 << 26);

carry1 = (h1 + (int64_t) (1 << 24)) >> 25;
h2 += carry1;
h1 -= carry1 << 25;
h1 -= carry1 * (1 << 25);
carry5 = (h5 + (int64_t) (1 << 24)) >> 25;
h6 += carry5;
h5 -= carry5 << 25;
h5 -= carry5 * (1 << 25);

carry2 = (h2 + (int64_t) (1 << 25)) >> 26;
h3 += carry2;
h2 -= carry2 << 26;
h2 -= carry2 * (1 << 26);
carry6 = (h6 + (int64_t) (1 << 25)) >> 26;
h7 += carry6;
h6 -= carry6 << 26;
h6 -= carry6 * (1 << 26);

carry3 = (h3 + (int64_t) (1 << 24)) >> 25;
h4 += carry3;
h3 -= carry3 << 25;
h3 -= carry3 * (1 << 25);
carry7 = (h7 + (int64_t) (1 << 24)) >> 25;
h8 += carry7;
h7 -= carry7 << 25;
h7 -= carry7 * (1 << 25);

carry4 = (h4 + (int64_t) (1 << 25)) >> 26;
h5 += carry4;
h4 -= carry4 << 26;
h4 -= carry4 * (1 << 26);
carry8 = (h8 + (int64_t) (1 << 25)) >> 26;
h9 += carry8;
h8 -= carry8 << 26;
h8 -= carry8 * (1 << 26);

carry9 = (h9 + (int64_t) (1 << 24)) >> 25;
h0 += carry9 * 19;
h9 -= carry9 << 25;
h9 -= carry9 * (1 << 25);

carry0 = (h0 + (int64_t) (1 << 25)) >> 26;
h1 += carry0;
h0 -= carry0 << 26;
h0 -= carry0 * (1 << 26);

h[0] = (int32_t) h0;
h[1] = (int32_t) h1;
Expand Down Expand Up @@ -808,17 +808,17 @@ void fe_mul121666(fe h, fe f) {
int64_t carry8;
int64_t carry9;

carry9 = (h9 + (int64_t) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 << 25;
carry1 = (h1 + (int64_t) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 << 25;
carry3 = (h3 + (int64_t) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 << 25;
carry5 = (h5 + (int64_t) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 << 25;
carry7 = (h7 + (int64_t) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 << 25;
carry9 = (h9 + (int64_t) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 * (1 << 25);
carry1 = (h1 + (int64_t) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 * (1 << 25);
carry3 = (h3 + (int64_t) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 * (1 << 25);
carry5 = (h5 + (int64_t) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 * (1 << 25);
carry7 = (h7 + (int64_t) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 * (1 << 25);

carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26;
carry2 = (h2 + (int64_t) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 << 26;
carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26;
carry6 = (h6 + (int64_t) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26;
carry8 = (h8 + (int64_t) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26;
carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 * (1 << 26);
carry2 = (h2 + (int64_t) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 * (1 << 26);
carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 * (1 << 26);
carry6 = (h6 + (int64_t) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 * (1 << 26);
carry8 = (h8 + (int64_t) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 * (1 << 26);

h[0] = (int32_t) h0;
h[1] = (int32_t) h1;
Expand Down Expand Up @@ -1080,40 +1080,40 @@ void fe_sq(fe h, const fe f) {
int64_t carry9;
carry0 = (h0 + (int64_t) (1 << 25)) >> 26;
h1 += carry0;
h0 -= carry0 << 26;
h0 -= carry0 * (1 << 26);
carry4 = (h4 + (int64_t) (1 << 25)) >> 26;
h5 += carry4;
h4 -= carry4 << 26;
h4 -= carry4 * (1 << 26);
carry1 = (h1 + (int64_t) (1 << 24)) >> 25;
h2 += carry1;
h1 -= carry1 << 25;
h1 -= carry1 * (1 << 25);
carry5 = (h5 + (int64_t) (1 << 24)) >> 25;
h6 += carry5;
h5 -= carry5 << 25;
h5 -= carry5 * (1 << 25);
carry2 = (h2 + (int64_t) (1 << 25)) >> 26;
h3 += carry2;
h2 -= carry2 << 26;
h2 -= carry2 * (1 << 26);
carry6 = (h6 + (int64_t) (1 << 25)) >> 26;
h7 += carry6;
h6 -= carry6 << 26;
h6 -= carry6 * (1 << 26);
carry3 = (h3 + (int64_t) (1 << 24)) >> 25;
h4 += carry3;
h3 -= carry3 << 25;
h3 -= carry3 * (1 << 25);
carry7 = (h7 + (int64_t) (1 << 24)) >> 25;
h8 += carry7;
h7 -= carry7 << 25;
h7 -= carry7 * (1 << 25);
carry4 = (h4 + (int64_t) (1 << 25)) >> 26;
h5 += carry4;
h4 -= carry4 << 26;
h4 -= carry4 * (1 << 26);
carry8 = (h8 + (int64_t) (1 << 25)) >> 26;
h9 += carry8;
h8 -= carry8 << 26;
h8 -= carry8 * (1 << 26);
carry9 = (h9 + (int64_t) (1 << 24)) >> 25;
h0 += carry9 * 19;
h9 -= carry9 << 25;
h9 -= carry9 * (1 << 25);
carry0 = (h0 + (int64_t) (1 << 25)) >> 26;
h1 += carry0;
h0 -= carry0 << 26;
h0 -= carry0 * (1 << 26);
h[0] = (int32_t) h0;
h[1] = (int32_t) h1;
h[2] = (int32_t) h2;
Expand Down Expand Up @@ -1253,40 +1253,40 @@ void fe_sq2(fe h, const fe f) {
h9 += h9;
carry0 = (h0 + (int64_t) (1 << 25)) >> 26;
h1 += carry0;
h0 -= carry0 << 26;
h0 -= carry0 * (1 << 26);
carry4 = (h4 + (int64_t) (1 << 25)) >> 26;
h5 += carry4;
h4 -= carry4 << 26;
h4 -= carry4 * (1 << 26);
carry1 = (h1 + (int64_t) (1 << 24)) >> 25;
h2 += carry1;
h1 -= carry1 << 25;
h1 -= carry1 * (1 << 25);
carry5 = (h5 + (int64_t) (1 << 24)) >> 25;
h6 += carry5;
h5 -= carry5 << 25;
h5 -= carry5 * (1 << 25);
carry2 = (h2 + (int64_t) (1 << 25)) >> 26;
h3 += carry2;
h2 -= carry2 << 26;
h2 -= carry2 * (1 << 26);
carry6 = (h6 + (int64_t) (1 << 25)) >> 26;
h7 += carry6;
h6 -= carry6 << 26;
h6 -= carry6 * (1 << 26);
carry3 = (h3 + (int64_t) (1 << 24)) >> 25;
h4 += carry3;
h3 -= carry3 << 25;
h3 -= carry3 * (1 << 25);
carry7 = (h7 + (int64_t) (1 << 24)) >> 25;
h8 += carry7;
h7 -= carry7 << 25;
h7 -= carry7 * (1 << 25);
carry4 = (h4 + (int64_t) (1 << 25)) >> 26;
h5 += carry4;
h4 -= carry4 << 26;
h4 -= carry4 * (1 << 26);
carry8 = (h8 + (int64_t) (1 << 25)) >> 26;
h9 += carry8;
h8 -= carry8 << 26;
h8 -= carry8 * (1 << 26);
carry9 = (h9 + (int64_t) (1 << 24)) >> 25;
h0 += carry9 * 19;
h9 -= carry9 << 25;
h9 -= carry9 * (1 << 25);
carry0 = (h0 + (int64_t) (1 << 25)) >> 26;
h1 += carry0;
h0 -= carry0 << 26;
h0 -= carry0 * (1 << 26);
h[0] = (int32_t) h0;
h[1] = (int32_t) h1;
h[2] = (int32_t) h2;
Expand Down Expand Up @@ -1421,33 +1421,33 @@ void fe_tobytes(unsigned char *s, const fe h) {
/* Goal: Output h-2^255 q, which is between 0 and 2^255-20. */
carry0 = h0 >> 26;
h1 += carry0;
h0 -= carry0 << 26;
h0 -= carry0 * (1 << 26);
carry1 = h1 >> 25;
h2 += carry1;
h1 -= carry1 << 25;
h1 -= carry1 * (1 << 25);
carry2 = h2 >> 26;
h3 += carry2;
h2 -= carry2 << 26;
h2 -= carry2 * (1 << 26);
carry3 = h3 >> 25;
h4 += carry3;
h3 -= carry3 << 25;
h3 -= carry3 * (1 << 25);
carry4 = h4 >> 26;
h5 += carry4;
h4 -= carry4 << 26;
h4 -= carry4 * (1 << 26);
carry5 = h5 >> 25;
h6 += carry5;
h5 -= carry5 << 25;
h5 -= carry5 * (1 << 25);
carry6 = h6 >> 26;
h7 += carry6;
h6 -= carry6 << 26;
h6 -= carry6 * (1 << 26);
carry7 = h7 >> 25;
h8 += carry7;
h7 -= carry7 << 25;
h7 -= carry7 * (1 << 25);
carry8 = h8 >> 26;
h9 += carry8;
h8 -= carry8 << 26;
h8 -= carry8 * (1 << 26);
carry9 = h9 >> 25;
h9 -= carry9 << 25;
h9 -= carry9 * (1 << 25);

/* h10 = carry9 */
/*
Expand All @@ -1456,36 +1456,36 @@ void fe_tobytes(unsigned char *s, const fe h) {
evidently 2^255 h10-2^255 q = 0.
Goal: Output h0+...+2^230 h9.
*/
s[0] = (unsigned char) (h0 >> 0);
s[1] = (unsigned char) (h0 >> 8);
s[2] = (unsigned char) (h0 >> 16);
s[3] = (unsigned char) ((h0 >> 24) | (h1 << 2));
s[4] = (unsigned char) (h1 >> 6);
s[5] = (unsigned char) (h1 >> 14);
s[6] = (unsigned char) ((h1 >> 22) | (h2 << 3));
s[7] = (unsigned char) (h2 >> 5);
s[8] = (unsigned char) (h2 >> 13);
s[9] = (unsigned char) ((h2 >> 21) | (h3 << 5));
s[10] = (unsigned char) (h3 >> 3);
s[11] = (unsigned char) (h3 >> 11);
s[12] = (unsigned char) ((h3 >> 19) | (h4 << 6));
s[13] = (unsigned char) (h4 >> 2);
s[14] = (unsigned char) (h4 >> 10);
s[15] = (unsigned char) (h4 >> 18);
s[16] = (unsigned char) (h5 >> 0);
s[17] = (unsigned char) (h5 >> 8);
s[18] = (unsigned char) (h5 >> 16);
s[19] = (unsigned char) ((h5 >> 24) | (h6 << 1));
s[20] = (unsigned char) (h6 >> 7);
s[21] = (unsigned char) (h6 >> 15);
s[22] = (unsigned char) ((h6 >> 23) | (h7 << 3));
s[23] = (unsigned char) (h7 >> 5);
s[24] = (unsigned char) (h7 >> 13);
s[25] = (unsigned char) ((h7 >> 21) | (h8 << 4));
s[26] = (unsigned char) (h8 >> 4);
s[27] = (unsigned char) (h8 >> 12);
s[28] = (unsigned char) ((h8 >> 20) | (h9 << 6));
s[29] = (unsigned char) (h9 >> 2);
s[30] = (unsigned char) (h9 >> 10);
s[31] = (unsigned char) (h9 >> 18);
s[0] = (unsigned char) ((uint32_t) h0 >> 0);
s[1] = (unsigned char) ((uint32_t) h0 >> 8);
s[2] = (unsigned char) ((uint32_t) h0 >> 16);
s[3] = (unsigned char) (((uint32_t) h0 >> 24) | ((uint32_t) h1 << 2));
s[4] = (unsigned char) ((uint32_t) h1 >> 6);
s[5] = (unsigned char) ((uint32_t) h1 >> 14);
s[6] = (unsigned char) (((uint32_t) h1 >> 22) | ((uint32_t) h2 << 3));
s[7] = (unsigned char) ((uint32_t) h2 >> 5);
s[8] = (unsigned char) ((uint32_t) h2 >> 13);
s[9] = (unsigned char) (((uint32_t) h2 >> 21) | ((uint32_t) h3 << 5));
s[10] = (unsigned char) ((uint32_t) h3 >> 3);
s[11] = (unsigned char) ((uint32_t) h3 >> 11);
s[12] = (unsigned char) (((uint32_t) h3 >> 19) | ((uint32_t) h4 << 6));
s[13] = (unsigned char) ((uint32_t) h4 >> 2);
s[14] = (unsigned char) ((uint32_t) h4 >> 10);
s[15] = (unsigned char) ((uint32_t) h4 >> 18);
s[16] = (unsigned char) ((uint32_t) h5 >> 0);
s[17] = (unsigned char) ((uint32_t) h5 >> 8);
s[18] = (unsigned char) ((uint32_t) h5 >> 16);
s[19] = (unsigned char) (((uint32_t) h5 >> 24) | ((uint32_t) h6 << 1));
s[20] = (unsigned char) ((uint32_t) h6 >> 7);
s[21] = (unsigned char) ((uint32_t) h6 >> 15);
s[22] = (unsigned char) (((uint32_t) h6 >> 23) | ((uint32_t) h7 << 3));
s[23] = (unsigned char) ((uint32_t) h7 >> 5);
s[24] = (unsigned char) ((uint32_t) h7 >> 13);
s[25] = (unsigned char) (((uint32_t) h7 >> 21) | ((uint32_t) h8 << 4));
s[26] = (unsigned char) ((uint32_t) h8 >> 4);
s[27] = (unsigned char) ((uint32_t) h8 >> 12);
s[28] = (unsigned char) (((uint32_t) h8 >> 20) | ((uint32_t) h9 << 6));
s[29] = (unsigned char) ((uint32_t) h9 >> 2);
s[30] = (unsigned char) ((uint32_t) h9 >> 10);
s[31] = (unsigned char) ((uint32_t) h9 >> 18);
}
2 changes: 1 addition & 1 deletion Sources/CEd25519/ge.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ static void cmov(ge_precomp *t, const ge_precomp *u, unsigned char b) {
static void select(ge_precomp *t, int pos, signed char b) {
ge_precomp minust;
unsigned char bnegative = negative(b);
unsigned char babs = b - (((-bnegative) & b) << 1);
unsigned char babs = b - (((-bnegative) & b) * 2);
fe_1(t->yplusx);
fe_1(t->yminusx);
fe_0(t->xy2d);
Expand Down
Loading

0 comments on commit 0d2cfe8

Please sign in to comment.