Skip to content

Commit

Permalink
running flipper fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
xtruan committed Mar 7, 2023
1 parent f3280c2 commit 57986fd
Show file tree
Hide file tree
Showing 158 changed files with 20,053 additions and 16,321 deletions.
8 changes: 4 additions & 4 deletions crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ SRCS += sha2.c
SRCS += sha3.c
SRCS += hasher.c
SRCS += aes/aescrypt.c aes/aeskey.c aes/aestab.c aes/aes_modes.c
SRCS += ed25519-donna/curve25519-donna-32bit.c ed25519-donna/curve25519-donna-helpers.c ed25519-donna/modm-donna-32bit.c
SRCS += ed25519-donna/ed25519-donna-basepoint-table.c ed25519-donna/ed25519-donna-32bit-tables.c ed25519-donna/ed25519-donna-impl-base.c
SRCS += ed25519-donna/ed25519.c ed25519-donna/curve25519-donna-scalarmult-base.c ed25519-donna/ed25519-sha3.c ed25519-donna/ed25519-keccak.c
SRCS += ed25519_donna/curve25519-donna-32bit.c ed25519_donna/curve25519-donna-helpers.c ed25519_donna/modm-donna-32bit.c
SRCS += ed25519_donna/ed25519_donna-basepoint-table.c ed25519_donna/ed25519_donna-32bit-tables.c ed25519_donna/ed25519_donna-impl-base.c
SRCS += ed25519_donna/ed25519.c ed25519_donna/curve25519-donna-scalarmult-base.c ed25519_donna/ed25519-sha3.c ed25519_donna/ed25519-keccak.c
SRCS += monero/base58.c
SRCS += monero/serialize.c
SRCS += monero/xmr.c
Expand Down Expand Up @@ -177,7 +177,7 @@ secp256k1-zkp.o:
$(CC) $(CFLAGS) -Wno-unused-function $(ZKP_CFLAGS) -fPIC -I$(ZKP_PATH) -I$(ZKP_PATH)/src -c $(ZKP_PATH)/src/secp256k1.c -o secp256k1-zkp.o

clean:
rm -f *.o aes/*.o chacha20poly1305/*.o ed25519-donna/*.o monero/*.o
rm -f *.o aes/*.o chacha20poly1305/*.o ed25519_donna/*.o monero/*.o
rm -f tests/*.o tests/test_check tests/test_speed tests/test_openssl tests/libtrezor-crypto.so tests/aestst
rm -f tools/*.o tools/xpubaddrgen tools/mktable tools/bip39bruteforce
rm -f fuzzer/*.o fuzzer/fuzzer
Expand Down
2 changes: 1 addition & 1 deletion crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ Distibuted under MIT License.
- SHA1/SHA2: http://www.aarongifford.com/computers/sha.html
- SHA3: https://github.com/rhash/RHash
- Curve25519: https://github.com/agl/curve25519-donna
- Ed25519: https://github.com/floodyberry/ed25519-donna
- Ed25519: https://github.com/floodyberry/ed25519_donna
- Chacha20: https://github.com/wg/c20p1305
- Poly1305: https://github.com/floodyberry/poly1305-donna
102 changes: 49 additions & 53 deletions crypto/address.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,70 +25,66 @@
#include "bignum.h"

size_t address_prefix_bytes_len(uint32_t address_type) {
if (address_type <= 0xFF) return 1;
if (address_type <= 0xFFFF) return 2;
if (address_type <= 0xFFFFFF) return 3;
return 4;
if(address_type <= 0xFF) return 1;
if(address_type <= 0xFFFF) return 2;
if(address_type <= 0xFFFFFF) return 3;
return 4;
}

void address_write_prefix_bytes(uint32_t address_type, uint8_t *out) {
if (address_type > 0xFFFFFF) *(out++) = address_type >> 24;
if (address_type > 0xFFFF) *(out++) = (address_type >> 16) & 0xFF;
if (address_type > 0xFF) *(out++) = (address_type >> 8) & 0xFF;
*(out++) = address_type & 0xFF;
void address_write_prefix_bytes(uint32_t address_type, uint8_t* out) {
if(address_type > 0xFFFFFF) *(out++) = address_type >> 24;
if(address_type > 0xFFFF) *(out++) = (address_type >> 16) & 0xFF;
if(address_type > 0xFF) *(out++) = (address_type >> 8) & 0xFF;
*(out++) = address_type & 0xFF;
}

bool address_check_prefix(const uint8_t *addr, uint32_t address_type) {
if (address_type <= 0xFF) {
return address_type == (uint32_t)(addr[0]);
}
if (address_type <= 0xFFFF) {
return address_type == (((uint32_t)addr[0] << 8) | ((uint32_t)addr[1]));
}
if (address_type <= 0xFFFFFF) {
return address_type == (((uint32_t)addr[0] << 16) |
((uint32_t)addr[1] << 8) | ((uint32_t)addr[2]));
}
return address_type ==
(((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) |
((uint32_t)addr[2] << 8) | ((uint32_t)addr[3]));
bool address_check_prefix(const uint8_t* addr, uint32_t address_type) {
if(address_type <= 0xFF) {
return address_type == (uint32_t)(addr[0]);
}
if(address_type <= 0xFFFF) {
return address_type == (((uint32_t)addr[0] << 8) | ((uint32_t)addr[1]));
}
if(address_type <= 0xFFFFFF) {
return address_type ==
(((uint32_t)addr[0] << 16) | ((uint32_t)addr[1] << 8) | ((uint32_t)addr[2]));
}
return address_type == (((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) |
((uint32_t)addr[2] << 8) | ((uint32_t)addr[3]));
}

#if USE_ETHEREUM
#include "sha3.h"

void ethereum_address_checksum(const uint8_t *addr, char *address, bool rskip60,
uint64_t chain_id) {
const char *hex = "0123456789abcdef";
address[0] = '0';
address[1] = 'x';
for (int i = 0; i < 20; i++) {
address[2 + i * 2] = hex[(addr[i] >> 4) & 0xF];
address[2 + i * 2 + 1] = hex[addr[i] & 0xF];
}
address[42] = 0;

SHA3_CTX ctx = {0};
uint8_t hash[32] = {0};
keccak_256_Init(&ctx);
if (rskip60) {
char prefix[16] = {0};
int prefix_size = bn_format_uint64(chain_id, NULL, "0x", 0, 0, false, 0,
prefix, sizeof(prefix));
keccak_Update(&ctx, (const uint8_t *)prefix, prefix_size);
}
keccak_Update(&ctx, (const uint8_t *)(address + 2), 40);
keccak_Final(&ctx, hash);
void ethereum_address_checksum(const uint8_t* addr, char* address, bool rskip60, uint64_t chain_id) {
const char* hex = "0123456789abcdef";
address[0] = '0';
address[1] = 'x';
for(int i = 0; i < 20; i++) {
address[2 + i * 2] = hex[(addr[i] >> 4) & 0xF];
address[2 + i * 2 + 1] = hex[addr[i] & 0xF];
}
address[42] = 0;

for (int i = 0; i < 20; i++) {
if ((hash[i] & 0x80) && address[2 + i * 2] >= 'a' &&
address[2 + i * 2] <= 'f') {
address[2 + i * 2] -= 0x20;
SHA3_CTX ctx = {0};
uint8_t hash[32] = {0};
keccak_256_Init(&ctx);
if(rskip60) {
char prefix[16] = {0};
int prefix_size =
bn_format_uint64(chain_id, NULL, "0x", 0, 0, false, 0, prefix, sizeof(prefix));
keccak_Update(&ctx, (const uint8_t*)prefix, prefix_size);
}
if ((hash[i] & 0x08) && address[2 + i * 2 + 1] >= 'a' &&
address[2 + i * 2 + 1] <= 'f') {
address[2 + i * 2 + 1] -= 0x20;
keccak_Update(&ctx, (const uint8_t*)(address + 2), 40);
keccak_Final(&ctx, hash);

for(int i = 0; i < 20; i++) {
if((hash[i] & 0x80) && address[2 + i * 2] >= 'a' && address[2 + i * 2] <= 'f') {
address[2 + i * 2] -= 0x20;
}
if((hash[i] & 0x08) && address[2 + i * 2 + 1] >= 'a' && address[2 + i * 2 + 1] <= 'f') {
address[2 + i * 2 + 1] -= 0x20;
}
}
}
}
#endif
7 changes: 3 additions & 4 deletions crypto/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
#include "options.h"

size_t address_prefix_bytes_len(uint32_t address_type);
void address_write_prefix_bytes(uint32_t address_type, uint8_t *out);
bool address_check_prefix(const uint8_t *addr, uint32_t address_type);
void address_write_prefix_bytes(uint32_t address_type, uint8_t* out);
bool address_check_prefix(const uint8_t* addr, uint32_t address_type);
#if USE_ETHEREUM
void ethereum_address_checksum(const uint8_t *addr, char *address, bool rskip60,
uint64_t chain_id);
void ethereum_address_checksum(const uint8_t* addr, char* address, bool rskip60, uint64_t chain_id);
#endif

#endif
Loading

0 comments on commit 57986fd

Please sign in to comment.