Coverage Report

Created: 2024-09-11 06:39

/src/trezor-firmware/crypto/address.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * Copyright (c) 2016 Daira Hopwood
3
 * Copyright (c) 2016 Pavol Rusnak
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining
6
 * a copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
 * OTHER DEALINGS IN THE SOFTWARE.
22
 */
23
24
#include "address.h"
25
#include "bignum.h"
26
27
0
size_t address_prefix_bytes_len(uint32_t address_type) {
28
0
  if (address_type <= 0xFF) return 1;
29
0
  if (address_type <= 0xFFFF) return 2;
30
0
  if (address_type <= 0xFFFFFF) return 3;
31
0
  return 4;
32
0
}
33
34
0
void address_write_prefix_bytes(uint32_t address_type, uint8_t *out) {
35
0
  if (address_type > 0xFFFFFF) *(out++) = address_type >> 24;
36
0
  if (address_type > 0xFFFF) *(out++) = (address_type >> 16) & 0xFF;
37
0
  if (address_type > 0xFF) *(out++) = (address_type >> 8) & 0xFF;
38
0
  *(out++) = address_type & 0xFF;
39
0
}
40
41
0
bool address_check_prefix(const uint8_t *addr, uint32_t address_type) {
42
0
  if (address_type <= 0xFF) {
43
0
    return address_type == (uint32_t)(addr[0]);
44
0
  }
45
0
  if (address_type <= 0xFFFF) {
46
0
    return address_type == (((uint32_t)addr[0] << 8) | ((uint32_t)addr[1]));
47
0
  }
48
0
  if (address_type <= 0xFFFFFF) {
49
0
    return address_type == (((uint32_t)addr[0] << 16) |
50
0
                            ((uint32_t)addr[1] << 8) | ((uint32_t)addr[2]));
51
0
  }
52
0
  return address_type ==
53
0
         (((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) |
54
0
          ((uint32_t)addr[2] << 8) | ((uint32_t)addr[3]));
55
0
}
56
57
#if USE_ETHEREUM
58
#include "sha3.h"
59
60
void ethereum_address_checksum(const uint8_t *addr, char *address, bool rskip60,
61
                               uint64_t chain_id) {
62
  const char *hex = "0123456789abcdef";
63
  address[0] = '0';
64
  address[1] = 'x';
65
  for (int i = 0; i < 20; i++) {
66
    address[2 + i * 2] = hex[(addr[i] >> 4) & 0xF];
67
    address[2 + i * 2 + 1] = hex[addr[i] & 0xF];
68
  }
69
  address[42] = 0;
70
71
  SHA3_CTX ctx = {0};
72
  uint8_t hash[32] = {0};
73
  keccak_256_Init(&ctx);
74
  if (rskip60) {
75
    char prefix[16] = {0};
76
    int prefix_size = bn_format_uint64(chain_id, NULL, "0x", 0, 0, false, 0,
77
                                       prefix, sizeof(prefix));
78
    keccak_Update(&ctx, (const uint8_t *)prefix, prefix_size);
79
  }
80
  keccak_Update(&ctx, (const uint8_t *)(address + 2), 40);
81
  keccak_Final(&ctx, hash);
82
83
  for (int i = 0; i < 20; i++) {
84
    if ((hash[i] & 0x80) && address[2 + i * 2] >= 'a' &&
85
        address[2 + i * 2] <= 'f') {
86
      address[2 + i * 2] -= 0x20;
87
    }
88
    if ((hash[i] & 0x08) && address[2 + i * 2 + 1] >= 'a' &&
89
        address[2 + i * 2 + 1] <= 'f') {
90
      address[2 + i * 2 + 1] -= 0x20;
91
    }
92
  }
93
}
94
#endif