Line data Source code
1 : /**
2 : * The MIT License (MIT)
3 : *
4 : * Copyright (c) 2021 RSK Labs Ltd
5 : *
6 : * Permission is hereby granted, free of charge, to any person obtaining a copy
7 : * of this software and associated documentation files (the "Software"), to
8 : * deal in the Software without restriction, including without limitation the
9 : * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 : * sell copies of the Software, and to permit persons to whom the Software is
11 : * furnished to do so, subject to the following conditions:
12 : *
13 : * The above copyright notice and this permission notice shall be included in
14 : * all copies or substantial portions of the Software.
15 : *
16 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 : * IN THE SOFTWARE.
23 : */
24 :
25 : #include <string.h>
26 : #include <stdbool.h>
27 : #include "memutil.h"
28 : #include "pathAuth.h"
29 :
30 : /* Paths that require authorization
31 : m/44'/0'/0'/0/0 (BTC)
32 : m/44'/1'/0'/0/0 (tBTC)
33 : */
34 : const char authPaths[][SINGLE_PATH_SIZE_BYTES] = {
35 : "\x05\x2c\x00\x00\x80\x00\x00\x00\x80\x00\x00\x00\x80\x00\x00\x00\x00\x00"
36 : "\x00\x00\x00", // BTC
37 : "\x05\x2c\x00\x00\x80\x01\x00\x00\x80\x00\x00\x00\x80\x00\x00\x00\x00\x00"
38 : "\x00\x00\x00" // tBTC
39 : };
40 :
41 : /* Paths that don't require authorization
42 : m/44'/137'/0'/0/0 (RSK)
43 : m/44'/137'/1'/0/0 (MST)
44 : m/44'/1'/1'/0/0 (tRSK)
45 : m/44'/1'/2'/0/0 (tMST)
46 : */
47 : const char noAuthPaths[][SINGLE_PATH_SIZE_BYTES] = {
48 : "\x05\x2c\x00\x00\x80\x89\x00\x00\x80\x00\x00\x00\x80\x00\x00\x00\x00\x00"
49 : "\x00\x00\x00", // RSK
50 : "\x05\x2c\x00\x00\x80\x89\x00\x00\x80\x01\x00\x00\x80\x00\x00\x00\x00\x00"
51 : "\x00\x00\x00", // MST
52 : "\x05\x2c\x00\x00\x80\x01\x00\x00\x80\x01\x00\x00\x80\x00\x00\x00\x00\x00"
53 : "\x00\x00\x00", // tRSK
54 : "\x05\x2c\x00\x00\x80\x01\x00\x00\x80\x02\x00\x00\x80\x00\x00\x00\x00\x00"
55 : "\x00\x00\x00", // tMST
56 : };
57 :
58 : // Derivation-path-lexicographically (and statically) ordered binary paths
59 : // These need to be updated if paths change
60 : // Each element indexes paths on the above arrays as follows:
61 : // Most significant byte indicates authPaths (0) or noAuthPaths (1)
62 : // Least significant byte indicates index on the array in question
63 : const int ordered_paths[TOTAL_AUTHORIZED_PATHS] = {
64 : 0x0000, // BTC
65 : 0x0001, // tBTC
66 : 0x0102, // tRSK
67 : 0x0103, // tMST
68 : 0x0100, // RSK
69 : 0x0101, // MST
70 : };
71 :
72 : // Return true if the *path is inside the authPaths array, false otherwhise
73 : // this means this path require authorization and validations.
74 144 : bool pathRequireAuth(unsigned char *path) {
75 : char cmpbuf[sizeof(authPaths[0])];
76 258 : for (unsigned int i = 0; i < sizeof(authPaths) / sizeof(authPaths[0]);
77 114 : i++) {
78 : // Dont memcmp flash to RAM
79 460 : SAFE_MEMMOVE(cmpbuf,
80 : sizeof(cmpbuf),
81 : MEMMOVE_ZERO_OFFSET,
82 : authPaths[i],
83 : sizeof(authPaths[i]),
84 : MEMMOVE_ZERO_OFFSET,
85 : sizeof(cmpbuf),
86 : { return false; });
87 230 : if (!memcmp(path, cmpbuf, sizeof(cmpbuf)))
88 116 : return true;
89 : }
90 28 : return false;
91 : }
92 :
93 : // Return true if the *path is inside the noAuthPaths array, false otherwhise
94 : // This means this path can be used to sign any hash, and does not require
95 : // authorization
96 28 : bool pathDontRequireAuth(unsigned char *path) {
97 : char cmpbuf[sizeof(noAuthPaths[0])];
98 100 : for (unsigned int i = 0; i < sizeof(noAuthPaths) / sizeof(noAuthPaths[0]);
99 72 : i++) {
100 : // Dont memcmp flash to RAM
101 176 : SAFE_MEMMOVE(cmpbuf,
102 : sizeof(cmpbuf),
103 : MEMMOVE_ZERO_OFFSET,
104 : noAuthPaths[i],
105 : sizeof(noAuthPaths[i]),
106 : MEMMOVE_ZERO_OFFSET,
107 : sizeof(cmpbuf),
108 : { return false; });
109 88 : if (!memcmp(path, cmpbuf, sizeof(cmpbuf)))
110 16 : return true;
111 : }
112 12 : return false;
113 : }
114 :
115 0 : const char *get_ordered_path(unsigned int index) {
116 0 : if (ordered_paths[index] & 0xFF00) {
117 : // No auth path
118 0 : return noAuthPaths[ordered_paths[index] & 0xFF];
119 : } else {
120 : // Auth path
121 0 : return authPaths[ordered_paths[index] & 0xFF];
122 : }
123 : }
|