1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-10 08:38:07 +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,7 +253,6 @@ 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));
}
@ -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
@ -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));

42
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,12 +53,13 @@ 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)) {
if (bn_is_zero(&a)) { // == 0
failed = true;
} else {
if (!bn_is_less(&a, &order256k1)) { // >= order
failed = true;
}
else if( !bn_is_less(&a, &order256k1)) { // == 0 or >= order
MEMSET_BZERO(&a, sizeof(a));
failed = true;
}
if (failed) {
@ -87,16 +88,12 @@ 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)) { // >= order
failed = true;
}
else
{
if( !bn_is_less(&a, &order256k1)) { // == 0 or >= order
failed = true;
}
// Making sure a is wiped.
MEMSET_BZERO(&a, sizeof(a));
}
@ -142,24 +139,19 @@ int hdnode_private_ckd(HDNode *inout, uint32_t i)
failed = true;
}
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!
// making sure to wipe our memory
MEMSET_BZERO(&a, sizeof(a));
MEMSET_BZERO(&b, sizeof(b));
MEMSET_BZERO(I, sizeof(I));
@ -194,32 +186,26 @@ 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;
}

20
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;
}
@ -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);
@ -809,7 +803,7 @@ 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.
// not as important to clear this one, but we might as well
MEMSET_BZERO(raw, sizeof(raw));
}
@ -821,7 +815,7 @@ 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!
// private keys running around our stack can cause trouble
MEMSET_BZERO(data, sizeof(data));
}
@ -907,7 +901,6 @@ 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));
return res;
}
@ -917,9 +910,7 @@ 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));
return res;
}
@ -959,12 +950,10 @@ int ecdsa_verify_digest(const uint8_t *pub_key, const uint8_t *sig, const uint8_
}
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;
@ -976,6 +965,7 @@ int ecdsa_verify_digest(const uint8_t *pub_key, const uint8_t *sig, const uint8_
MEMSET_BZERO(&r, sizeof(r));
MEMSET_BZERO(&s, sizeof(s));
MEMSET_BZERO(&z, sizeof(z));
// all OK
return result;
}

3
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)
{
@ -57,7 +57,6 @@ void hmac_sha256(const uint8_t *key, const uint32_t keylen, const uint8_t *msg,
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)

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,7 +57,6 @@ 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(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(g, sizeof(g));
}