1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-14 09:20:55 +00:00

fix for formatting

This commit is contained in:
Pavol Rusnak 2013-08-17 14:32:25 +02:00
parent 3645df58e1
commit 1bd0592c26
3 changed files with 24 additions and 24 deletions

32
ecdsa.c
View File

@ -49,7 +49,7 @@ void mod(bignum256 *x, bignum256 const *prime)
if (x->val[i] > prime->val[i]) { if (x->val[i] > prime->val[i]) {
// substract p from x // substract p from x
temp = 0x40000000u; temp = 0x40000000u;
for (i = 0;i < 9; i++) { for (i = 0; i < 9; i++) {
temp += x->val[i] - prime->val[i]; temp += x->val[i] - prime->val[i];
x->val[i] = temp & 0x3FFFFFFF; x->val[i] = temp & 0x3FFFFFFF;
temp >>= 30; temp >>= 30;
@ -68,18 +68,18 @@ void multiply(const bignum256 *k, bignum256 *x, bignum256 const *prime)
uint32_t res[18], coef; uint32_t res[18], coef;
// compute lower half of long multiplication // compute lower half of long multiplication
for (i = 0;i < 9; i++) for (i = 0; i < 9; i++)
{ {
for (j = 0;j <= i; j++) { for (j = 0; j <= i; j++) {
temp += k->val[j] * (uint64_t)x->val[i-j]; temp += k->val[j] * (uint64_t)x->val[i-j];
} }
res[i] = temp & 0x3FFFFFFFu; res[i] = temp & 0x3FFFFFFFu;
temp >>= 30; temp >>= 30;
} }
// compute upper half // compute upper half
for (;i < 17; i++) for (; i < 17; i++)
{ {
for (j = i - 8; j < 9 ;j++) { for (j = i - 8; j < 9 ; j++) {
temp += k->val[j] * (uint64_t)x->val[i-j]; temp += k->val[j] * (uint64_t)x->val[i-j];
} }
res[i] = temp & 0x3FFFFFFFu; res[i] = temp & 0x3FFFFFFFu;
@ -87,7 +87,7 @@ void multiply(const bignum256 *k, bignum256 *x, bignum256 const *prime)
} }
res[17] = temp; res[17] = temp;
// compute modulo p division is only estimated so this may give result greater than prime but not bigger than 2 * prime // compute modulo p division is only estimated so this may give result greater than prime but not bigger than 2 * prime
for (i = 16;i >= 8; i--) { for (i = 16; i >= 8; i--) {
// estimate (res / prime) // estimate (res / prime)
coef = (res[i] >> 16) + (res[i+1] << 14); coef = (res[i] >> 16) + (res[i+1] << 14);
// substract (coef * prime) from res // substract (coef * prime) from res
@ -100,7 +100,7 @@ void multiply(const bignum256 *k, bignum256 *x, bignum256 const *prime)
} }
} }
// store the result // store the result
for (i = 0;i < 9; i++) { for (i = 0; i < 9; i++) {
x->val[i] = res[i]; x->val[i] = res[i];
} }
} }
@ -140,12 +140,12 @@ void inverse(bignum256 *x, bignum256 const *prime)
len2 = 1; len2 = 1;
k = 0; k = 0;
for (;;) { for (;;) {
for (i = 0;i < len1; i++) { for (i = 0; i < len1; i++) {
if (v[i]) break; if (v[i]) break;
} }
if (i == len1) break; if (i == len1) break;
for (;;) { for (;;) {
for (i = 0;i < 30; i++) { for (i = 0; i < 30; i++) {
if (u[0] & (1 << i)) break; if (u[0] & (1 << i)) break;
} }
if (i == 0) break; if (i == 0) break;
@ -167,7 +167,7 @@ void inverse(bignum256 *x, bignum256 const *prime)
k += i; k += i;
} }
for (;;) { for (;;) {
for (i = 0;i < 30; i++) { for (i = 0; i < 30; i++) {
if (v[0] & (1 << i)) break; if (v[0] & (1 << i)) break;
} }
if (i == 0) break; if (i == 0) break;
@ -254,7 +254,7 @@ void inverse(bignum256 *x, bignum256 const *prime)
} }
} }
temp = 1; temp = 1;
for (i = 0;i < 9; i++) { for (i = 0; i < 9; i++) {
temp += 0x3FFFFFFF + prime->val[i] - r[i]; temp += 0x3FFFFFFF + prime->val[i] - r[i];
r[i] = temp & 0x3FFFFFFF; r[i] = temp & 0x3FFFFFFF;
temp >>= 30; temp >>= 30;
@ -276,7 +276,7 @@ void inverse(bignum256 *x, bignum256 const *prime)
temp = r[0] + prime->val[0]; temp = r[0] + prime->val[0];
r[0] = (temp >> 1) & 0x1FFFFFFF; r[0] = (temp >> 1) & 0x1FFFFFFF;
temp >>= 30; temp >>= 30;
for (i = 1;i < 9; i++) { for (i = 1; i < 9; i++) {
temp += r[i] + prime->val[i]; temp += r[i] + prime->val[i];
r[i-1] += (temp & 1) << 29; r[i-1] += (temp & 1) << 29;
r[i] = (temp >> 1) & 0x1FFFFFFF; r[i] = (temp >> 1) & 0x1FFFFFFF;
@ -321,7 +321,7 @@ void point_add(const curve_point *x1, curve_point *x2)
memcpy(&xr, &lambda, sizeof(bignum256)); memcpy(&xr, &lambda, sizeof(bignum256));
multiply(&xr, &xr, &prime256k1); multiply(&xr, &xr, &prime256k1);
temp = 0; temp = 0;
for (i = 0;i < 9; i++) { for (i = 0; i < 9; i++) {
temp += xr.val[i] + 3u * prime256k1.val[i] - x1->x.val[i] - x2->x.val[i]; temp += xr.val[i] + 3u * prime256k1.val[i] - x1->x.val[i] - x2->x.val[i];
xr.val[i] = temp & 0x3FFFFFFF; xr.val[i] = temp & 0x3FFFFFFF;
temp >>= 30; temp >>= 30;
@ -353,7 +353,7 @@ void point_double(curve_point *x)
memcpy(&xr, &lambda, sizeof(bignum256)); memcpy(&xr, &lambda, sizeof(bignum256));
multiply(&xr, &xr, &prime256k1); multiply(&xr, &xr, &prime256k1);
temp = 0; temp = 0;
for (i = 0;i < 9; i++) { for (i = 0; i < 9; i++) {
temp += xr.val[i] + 3u * prime256k1.val[i] - 2u * x->x.val[i]; temp += xr.val[i] + 3u * prime256k1.val[i] - 2u * x->x.val[i];
xr.val[i] = temp & 0x3FFFFFFF; xr.val[i] = temp & 0x3FFFFFFF;
temp >>= 30; temp >>= 30;
@ -465,7 +465,7 @@ void ecdsa_sign(uint8_t *private_key, uint8_t *message, uint32_t len, uint8_t *s
z.val[8] = temp; z.val[8] = temp;
for (;;) { for (;;) {
// generate random number k // generate random number k
for (i = 0;i < 8; i++) { for (i = 0; i < 8; i++) {
k.val[i] = random32() & 0x3FFFFFFF; k.val[i] = random32() & 0x3FFFFFFF;
} }
k.val[8] =random32() & 0xFFFF; k.val[8] =random32() & 0xFFFF;
@ -477,7 +477,7 @@ void ecdsa_sign(uint8_t *private_key, uint8_t *message, uint32_t len, uint8_t *s
// r = (rx mod n) // r = (rx mod n)
mod(&R.x, &order256k1); mod(&R.x, &order256k1);
// if r is zero, we try different k // if r is zero, we try different k
for (i = 0;i < 9; i++) { for (i = 0; i < 9; i++) {
if (R.x.val[i] != 0) break; if (R.x.val[i] != 0) break;
} }
if (i == 9) continue; if (i == 9) continue;

View File

@ -39,10 +39,10 @@ void process_chunk(const uint8_t *chunk, uint32_t *hash)
}; };
uint32_t i, s0, s1, a, b, c, d, e, f, g, h, ch, temp, maj, w[64]; uint32_t i, s0, s1, a, b, c, d, e, f, g, h, ch, temp, maj, w[64];
for (i = 0;i < 16;i++) { for (i = 0; i < 16; i++) {
w[i] = read_be(chunk + 4 * i); w[i] = read_be(chunk + 4 * i);
} }
for (;i < 64;i++) { for (; i < 64; i++) {
s0 = ror(w[i-15], 7) ^ ror(w[i-15], 18) ^ (w[i-15]>>3); s0 = ror(w[i-15], 7) ^ ror(w[i-15], 18) ^ (w[i-15]>>3);
s1 = ror(w[i-2], 17) ^ ror(w[i-2], 19) ^ (w[i-2]>>10); s1 = ror(w[i-2], 17) ^ ror(w[i-2], 19) ^ (w[i-2]>>10);
w[i] = w[i-16] + s0 + w[i-7] + s1; w[i] = w[i-16] + s0 + w[i-7] + s1;
@ -55,7 +55,7 @@ void process_chunk(const uint8_t *chunk, uint32_t *hash)
f = hash[5]; f = hash[5];
g = hash[6]; g = hash[6];
h = hash[7]; h = hash[7];
for (i = 0;i < 64;i++) { for (i = 0; i < 64; i++) {
s1 = ror(e, 6) ^ ror(e, 11) ^ ror(e, 25); s1 = ror(e, 6) ^ ror(e, 11) ^ ror(e, 25);
ch = (e & f) ^ ((~ e) & g); ch = (e & f) ^ ((~ e) & g);
temp = h + s1 + ch + k0[i] + w[i]; temp = h + s1 + ch + k0[i] + w[i];
@ -92,7 +92,7 @@ void sha256(const uint8_t *msg, const uint32_t len, uint8_t *hash)
}; };
uint32_t l = len, i, h[8]; uint32_t l = len, i, h[8];
uint8_t last_chunks[128]; //for storing last 1 or 2 chunks uint8_t last_chunks[128]; //for storing last 1 or 2 chunks
for (i = 0;i < 8; i++) { for (i = 0; i < 8; i++) {
h[i] = h0[i]; h[i] = h0[i];
} }
// process complete message chunks // process complete message chunks
@ -102,13 +102,13 @@ void sha256(const uint8_t *msg, const uint32_t len, uint8_t *hash)
msg += 64; msg += 64;
} }
// process rest of the message // process rest of the message
for (i = 0;i < l; i++) { for (i = 0; i < l; i++) {
last_chunks[i] = msg[i]; last_chunks[i] = msg[i];
} }
// add '1' bit // add '1' bit
last_chunks[i++] = 0x80; last_chunks[i++] = 0x80;
// pad message with zeroes // pad message with zeroes
for (;(i & 63) != 56; i++) { for (; (i & 63) != 56; i++) {
last_chunks[i]=0; last_chunks[i]=0;
} }
// add message length in bits // add message length in bits
@ -121,7 +121,7 @@ void sha256(const uint8_t *msg, const uint32_t len, uint8_t *hash)
process_chunk(last_chunks + 64, h); process_chunk(last_chunks + 64, h);
} }
// write the result // write the result
for (i = 0;i < 8; i++) { for (i = 0; i < 8; i++) {
write_be(hash + 4 * i, h[i]); write_be(hash + 4 * i, h[i]);
} }
} }

2
test.c
View File

@ -61,7 +61,7 @@ int main()
i = buffer[8]; i = buffer[8];
// extract key data // extract key data
if (i > 32) { if (i > 32) {
for (j = 0;j < 32; j++) { for (j = 0; j < 32; j++) {
priv_key[j] = buffer[j + i - 23]; priv_key[j] = buffer[j + i - 23];
} }
} else { } else {