1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-29 10:58:21 +00:00

cleanup coding style

This commit is contained in:
Pavol Rusnak 2015-04-13 18:19:33 +02:00
parent 6ec585fcee
commit 21d0bb437a
9 changed files with 97 additions and 132 deletions

View File

@ -26,7 +26,7 @@
#include <sys/types.h>
#include "base58.h"
#include "sha2.h"
#include "macro_utils.h"
#include "macros.h"
static const int8_t b58digits_map[] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
@ -184,8 +184,6 @@ bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
return true;
}
#include <stdio.h>
int base58_encode_check(const uint8_t *data, int datalen, char *str, int strsize)
{
if (datalen > 128) {
@ -197,10 +195,9 @@ int base58_encode_check(const uint8_t *data, int datalen, char *str, int strsize
sha256_Raw(data, datalen, hash);
sha256_Raw(hash, 32, hash);
size_t res = strsize;
bool fSuccess = b58enc(str, &res, buf, datalen + 4);
bool success = b58enc(str, &res, buf, datalen + 4);
MEMSET_BZERO(buf, sizeof(buf));
return fSuccess ? res : 0;
return success ? res : 0;
}
int base58_decode_check(const char *str, uint8_t *data, int datalen)

View File

@ -26,7 +26,7 @@
#include <assert.h>
#include "bignum.h"
#include "secp256k1.h"
#include "macro_utils.h"
#include "macros.h"
inline uint32_t read_be(const uint8_t *data)
{
@ -253,8 +253,7 @@ void bn_multiply(const bignum256 *k, bignum256 *x, const bignum256 *prime)
for (i = 0; i < 9; i++) {
x->val[i] = res[i];
}
MEMSET_BZERO(res,sizeof(res));
MEMSET_BZERO(res, sizeof(res));
}
// input x can be any normalized number that fits (0 <= x < 2^270).
@ -312,10 +311,8 @@ void bn_sqrt(bignum256 *x, const bignum256 *prime)
}
bn_mod(&res, prime);
memcpy(x, &res, sizeof(bignum256));
MEMSET_BZERO(&res, sizeof(res));
MEMSET_BZERO(&p, sizeof(p));
}
#if ! USE_INVERSE_FAST
@ -425,7 +422,7 @@ void bn_inverse(bignum256 *x, const bignum256 *prime)
// max(u,v) <= 2^k (*) see comment at end of loop
// gcd(u,v) = 1
// {odd,even} = {&us, &vr}
// odd->a[0] and odd->a[8] are odd
// odd->a[0] and odd->a[8] are odd
// even->a[0] or even->a[8] is even
//
// first u/v are large and r/s small
@ -622,7 +619,7 @@ void bn_inverse(bignum256 *x, const bignum256 *prime)
}
x->val[i] = temp32;
// Let's wipe all temp buffers.
// let's wipe all temp buffers
MEMSET_BZERO(pp, sizeof(pp));
MEMSET_BZERO(&us, sizeof(us));
MEMSET_BZERO(&vr, sizeof(vr));

76
bip32.c
View File

@ -31,7 +31,7 @@
#include "sha2.h"
#include "ripemd160.h"
#include "base58.h"
#include "macro_utils.h"
#include "macros.h"
int hdnode_from_xpub(uint32_t depth, uint32_t fingerprint, uint32_t child_num, const uint8_t *chain_code, const uint8_t *public_key, HDNode *out)
{
@ -53,15 +53,16 @@ int hdnode_from_xprv(uint32_t depth, uint32_t fingerprint, uint32_t child_num, c
bn_read_be(private_key, &a);
bool failed = false;
if (bn_is_zero(&a)) {
failed = true;
}
else if( !bn_is_less(&a, &order256k1)) { // == 0 or >= order
MEMSET_BZERO(&a,sizeof(a));
if (bn_is_zero(&a)) { // == 0
failed = true;
} else {
if (!bn_is_less(&a, &order256k1)) { // >= order
failed = true;
}
MEMSET_BZERO(&a, sizeof(a));
}
if(failed) {
if (failed) {
return 0;
}
@ -87,25 +88,21 @@ int hdnode_from_seed(const uint8_t *seed, int seed_len, HDNode *out)
bn_read_be(out->private_key, &a);
bool failed = false;
if (bn_is_zero(&a)) {
if (bn_is_zero(&a)) { // == 0
failed = true;
}
else
{
if( !bn_is_less(&a, &order256k1)) { // == 0 or >= order
} else {
if (!bn_is_less(&a, &order256k1)) { // >= order
failed = true;
}
// Making sure a is wiped.
MEMSET_BZERO(&a,sizeof(a));
MEMSET_BZERO(&a, sizeof(a));
}
if(!failed) {
if (!failed) {
memcpy(out->chain_code, I + 32, 32);
hdnode_fill_public_key(out);
}
MEMSET_BZERO(I,sizeof(I));
MEMSET_BZERO(I, sizeof(I));
return failed ? 0 : 1;
}
@ -141,30 +138,25 @@ int hdnode_private_ckd(HDNode *inout, uint32_t i)
if (!bn_is_less(&b, &order256k1)) { // >= order
failed = true;
}
if(!failed) {
if (!failed) {
bn_addmod(&a, &b, &order256k1);
if (bn_is_zero(&a)) {
failed = true;
}
}
if(!failed)
{
if (!failed) {
inout->depth++;
inout->child_num = i;
bn_write_be(&a, inout->private_key);
hdnode_fill_public_key(inout);
}
// Making sure to wipe our memory!
MEMSET_BZERO(&a,sizeof(a));
MEMSET_BZERO(&b,sizeof(b));
MEMSET_BZERO(I,sizeof(I));
MEMSET_BZERO(fingerprint,sizeof(fingerprint));
MEMSET_BZERO(data,sizeof(data));
// making sure to wipe our memory
MEMSET_BZERO(&a, sizeof(a));
MEMSET_BZERO(&b, sizeof(b));
MEMSET_BZERO(I, sizeof(I));
MEMSET_BZERO(fingerprint, sizeof(fingerprint));
MEMSET_BZERO(data, sizeof(data));
return failed ? 0 : 1;
}
@ -194,43 +186,37 @@ int hdnode_public_ckd(HDNode *inout, uint32_t i)
failed = true;
}
if(!failed)
{
if (!failed) {
hmac_sha512(inout->chain_code, 32, data, sizeof(data), I);
memcpy(inout->chain_code, I + 32, 32);
bn_read_be(I, &c);
if (!bn_is_less(&c, &order256k1)) { // >= order
failed = true;
}
}
if(!failed)
{
if (!failed) {
scalar_multiply(&c, &b); // b = c * G
point_add(&a, &b); // b = a + b
if (!ecdsa_validate_pubkey(&b)) {
failed = true;
}
}
if(!failed)
{
if (!failed) {
inout->public_key[0] = 0x02 | (b.y.val[0] & 0x01);
bn_write_be(&b.x, inout->public_key + 1);
inout->depth++;
inout->child_num = i;
}
// Wipe all stack data.
MEMSET_BZERO(data,sizeof(data));
MEMSET_BZERO(I,sizeof(I));
MEMSET_BZERO(fingerprint,sizeof(fingerprint));
MEMSET_BZERO(&a,sizeof(a));
MEMSET_BZERO(&b,sizeof(b));
MEMSET_BZERO(&c,sizeof(c));
MEMSET_BZERO(data, sizeof(data));
MEMSET_BZERO(I, sizeof(I));
MEMSET_BZERO(fingerprint, sizeof(fingerprint));
MEMSET_BZERO(&a, sizeof(a));
MEMSET_BZERO(&b, sizeof(b));
MEMSET_BZERO(&c, sizeof(c));
return failed ? 0 : 1;
}

70
ecdsa.c
View File

@ -33,7 +33,7 @@
#include "hmac.h"
#include "ecdsa.h"
#include "base58.h"
#include "macro_utils.h"
#include "macros.h"
// Set cp2 = cp1
void point_copy(const curve_point *cp1, curve_point *cp2)
@ -260,7 +260,6 @@ static void point_jacobian_add(const curve_point *p1, jacobian_curve_point *p2)
* = r * (h^2*x2 - x3) - h^3*y2
*/
/* h = x1*z2^2 - x2
* r = y1*z2^3 - y2
* x3 = r^2 - h^3 - 2*h^2*x2
@ -348,7 +347,6 @@ static void point_jacobian_double(jacobian_curve_point *p) {
* = m * (xy^2 - x3) - y^4
*/
/* m = 3/2*x*x
* x3 = m^2 - 2*xy^2
* y3 = m*(xy^2 - x3) - 8y^4
@ -656,7 +654,6 @@ int ecdsa_sign(const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, ui
uint8_t hash[32];
sha256_Raw(msg, msg_len, hash);
int res = ecdsa_sign_digest(priv_key, hash, sig, pby);
MEMSET_BZERO(hash, sizeof(hash));
return res;
@ -670,9 +667,7 @@ int ecdsa_sign_double(const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_
sha256_Raw(msg, msg_len, hash);
sha256_Raw(hash, 32, hash);
int res = ecdsa_sign_digest(priv_key, hash, sig, pby);
MEMSET_BZERO(hash, sizeof(hash));
return res;
}
@ -701,7 +696,7 @@ int ecdsa_sign_digest(const uint8_t *priv_key, const uint8_t *digest, uint8_t *s
}
#endif
if(result == 0) {
if (result == 0) {
// compute k*G
scalar_multiply(&k, &R);
if (pby) {
@ -716,7 +711,7 @@ int ecdsa_sign_digest(const uint8_t *priv_key, const uint8_t *digest, uint8_t *s
}
}
if(result == 0) {
if (result == 0) {
bn_inverse(&k, &order256k1);
bn_read_be(priv_key, da);
bn_multiply(&R.x, da, &order256k1);
@ -734,8 +729,7 @@ int ecdsa_sign_digest(const uint8_t *priv_key, const uint8_t *digest, uint8_t *s
}
}
if(result == 0)
{
if (result == 0) {
// if S > order/2 => S = -S
if (bn_is_less(&order256k1_half, &k)) {
bn_subtract(&order256k1, &k, &k);
@ -748,9 +742,9 @@ int ecdsa_sign_digest(const uint8_t *priv_key, const uint8_t *digest, uint8_t *s
bn_write_be(&k, sig + 32);
}
MEMSET_BZERO(&k,sizeof(k));
MEMSET_BZERO(&z,sizeof(z));
MEMSET_BZERO(&R,sizeof(R));
MEMSET_BZERO(&k, sizeof(k));
MEMSET_BZERO(&z, sizeof(z));
MEMSET_BZERO(&R, sizeof(R));
return 0;
}
@ -764,8 +758,8 @@ void ecdsa_get_public_key33(const uint8_t *priv_key, uint8_t *pub_key)
scalar_multiply(&k, &R);
pub_key[0] = 0x02 | (R.y.val[0] & 0x01);
bn_write_be(&R.x, pub_key + 1);
MEMSET_BZERO(&R,sizeof(R));
MEMSET_BZERO(&k,sizeof(k));
MEMSET_BZERO(&R, sizeof(R));
MEMSET_BZERO(&k, sizeof(k));
}
void ecdsa_get_public_key65(const uint8_t *priv_key, uint8_t *pub_key)
@ -779,8 +773,8 @@ void ecdsa_get_public_key65(const uint8_t *priv_key, uint8_t *pub_key)
pub_key[0] = 0x04;
bn_write_be(&R.x, pub_key + 1);
bn_write_be(&R.y, pub_key + 33);
MEMSET_BZERO(&R,sizeof(R));
MEMSET_BZERO(&k,sizeof(k));
MEMSET_BZERO(&R, sizeof(R));
MEMSET_BZERO(&k, sizeof(k));
}
void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash)
@ -794,7 +788,7 @@ void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash)
sha256_Raw(pub_key, 33, h); // expecting compressed format
}
ripemd160(h, 32, pubkeyhash);
MEMSET_BZERO(h,sizeof(h));
MEMSET_BZERO(h, sizeof(h));
}
void ecdsa_get_address_raw(const uint8_t *pub_key, uint8_t version, uint8_t *addr_raw)
@ -809,8 +803,8 @@ void ecdsa_get_address(const uint8_t *pub_key, uint8_t version, char *addr, int
ecdsa_get_address_raw(pub_key, version, raw);
base58_encode_check(raw, 21, addr, addrsize);
// Not as important to clear this one, but we might as well.
MEMSET_BZERO(raw,sizeof(raw));
// not as important to clear this one, but we might as well
MEMSET_BZERO(raw, sizeof(raw));
}
void ecdsa_get_wif(const uint8_t *priv_key, uint8_t version, char *wif, int wifsize)
@ -821,8 +815,8 @@ void ecdsa_get_wif(const uint8_t *priv_key, uint8_t version, char *wif, int wifs
data[33] = 0x01;
base58_encode_check(data, 34, wif, wifsize);
// This private keys running around our stack can cause trouble!
MEMSET_BZERO(data,sizeof(data));
// private keys running around our stack can cause trouble
MEMSET_BZERO(data, sizeof(data));
}
int ecdsa_address_decode(const char *addr, uint8_t *out)
@ -907,8 +901,7 @@ int ecdsa_verify(const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg,
uint8_t hash[32];
sha256_Raw(msg, msg_len, hash);
int res = ecdsa_verify_digest(pub_key, sig, hash);
MEMSET_BZERO(hash,sizeof(hash));
MEMSET_BZERO(hash, sizeof(hash));
return res;
}
@ -917,10 +910,8 @@ int ecdsa_verify_double(const uint8_t *pub_key, const uint8_t *sig, const uint8_
uint8_t hash[32];
sha256_Raw(msg, msg_len, hash);
sha256_Raw(hash, 32, hash);
int res = ecdsa_verify_digest(pub_key, sig, hash);
MEMSET_BZERO(hash,sizeof(hash));
MEMSET_BZERO(hash, sizeof(hash));
return res;
}
@ -958,24 +949,23 @@ int ecdsa_verify_digest(const uint8_t *pub_key, const uint8_t *sig, const uint8_
scalar_multiply(&z, &res);
}
if(result == 0) {
// both pub and res can be infinity, can have y = 0 OR can be equal -> false negative
point_multiply(&s, &pub, &pub);
point_add(&pub, &res);
bn_mod(&(res.x), &order256k1);
// signature does not match
if (result == 0) {
// both pub and res can be infinity, can have y = 0 OR can be equal -> false negative
point_multiply(&s, &pub, &pub);
point_add(&pub, &res);
bn_mod(&(res.x), &order256k1);
// signature does not match
if (!bn_is_equal(&res.x, &r)) {
result = 5;
}
}
MEMSET_BZERO(&pub,sizeof(pub));
MEMSET_BZERO(&res,sizeof(res));
MEMSET_BZERO(&r,sizeof(r));
MEMSET_BZERO(&s,sizeof(s));
MEMSET_BZERO(&z,sizeof(z));
MEMSET_BZERO(&pub, sizeof(pub));
MEMSET_BZERO(&res, sizeof(res));
MEMSET_BZERO(&r, sizeof(r));
MEMSET_BZERO(&s, sizeof(s));
MEMSET_BZERO(&z, sizeof(z));
// all OK
return result;
}

15
hmac.c
View File

@ -25,7 +25,7 @@
#include "hmac.h"
#include "sha2.h"
#include "macro_utils.h"
#include "macros.h"
void hmac_sha256(const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t *hmac)
{
@ -54,10 +54,9 @@ void hmac_sha256(const uint8_t *key, const uint32_t keylen, const uint8_t *msg,
sha256_Update(&ctx, o_key_pad, SHA256_BLOCK_LENGTH);
sha256_Update(&ctx, buf, SHA256_DIGEST_LENGTH);
sha256_Final(hmac, &ctx);
MEMSET_BZERO(buf,sizeof(buf));
MEMSET_BZERO(o_key_pad,sizeof(o_key_pad));
MEMSET_BZERO(i_key_pad,sizeof(i_key_pad));
MEMSET_BZERO(buf, sizeof(buf));
MEMSET_BZERO(o_key_pad, sizeof(o_key_pad));
MEMSET_BZERO(i_key_pad, sizeof(i_key_pad));
}
void hmac_sha512(const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t *hmac)
@ -88,7 +87,7 @@ void hmac_sha512(const uint8_t *key, const uint32_t keylen, const uint8_t *msg,
sha512_Update(&ctx, buf, SHA512_DIGEST_LENGTH);
sha512_Final(hmac, &ctx);
MEMSET_BZERO(buf,sizeof(buf));
MEMSET_BZERO(o_key_pad,sizeof(o_key_pad));
MEMSET_BZERO(i_key_pad,sizeof(i_key_pad));
MEMSET_BZERO(buf, sizeof(buf));
MEMSET_BZERO(o_key_pad, sizeof(o_key_pad));
MEMSET_BZERO(i_key_pad, sizeof(i_key_pad));
}

View File

@ -1,8 +0,0 @@
#if !defined( _MACRO_UTILS_H )
#define _MACRO_UTILS_H
#define MEMSET_BZERO(p,l) memset((p), 0, (l))
#endif

6
macros.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __MACROS_H__
#define __MACROS_H__
#define MEMSET_BZERO(p,l) memset((p), 0, (l))
#endif

View File

@ -24,7 +24,7 @@
#include <string.h>
#include "pbkdf2.h"
#include "hmac.h"
#include "macro_utils.h"
#include "macros.h"
void pbkdf2_hmac_sha256(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32_t iterations, uint8_t *key, int keylen, void (*progress_callback)(uint32_t current, uint32_t total))
{
@ -57,8 +57,7 @@ void pbkdf2_hmac_sha256(const uint8_t *pass, int passlen, uint8_t *salt, int sal
memcpy(key + HMACLEN * (i - 1), f, HMACLEN);
}
}
MEMSET_BZERO(f,sizeof(f));
MEMSET_BZERO(f, sizeof(f));
MEMSET_BZERO(g, sizeof(g));
}
@ -93,7 +92,6 @@ void pbkdf2_hmac_sha512(const uint8_t *pass, int passlen, uint8_t *salt, int sal
memcpy(key + HMACLEN * (i - 1), f, HMACLEN);
}
}
MEMSET_BZERO(f,sizeof(f));
MEMSET_BZERO(f, sizeof(f));
MEMSET_BZERO(g, sizeof(g));
}