25
25
*
26
26
* -----
27
27
*
28
- * Parts of this software are based on Ledger Nano SDK
29
- *
30
- * (c) 2017 Ledger
28
+ * Parts of this software are based on the Ledger Bitcoin Wallet App
29
+ *
30
+ * Ledger App - Bitcoin Wallet
31
+ * (c) 2016-2019 Ledger
31
32
*
32
- * Licensed under the Apache License, Version 2.0 (the "License");
33
- * you may not use this file except in compliance with the License.
34
- * You may obtain a copy of the License at
33
+ * Licensed under the Apache License, Version 2.0 (the "License");
34
+ * you may not use this file except in compliance with the License.
35
+ * You may obtain a copy of the License at
35
36
*
36
- * http://www.apache.org/licenses/LICENSE-2.0
37
+ * http://www.apache.org/licenses/LICENSE-2.0
37
38
*
38
- * Unless required by applicable law or agreed to in writing, software
39
- * distributed under the License is distributed on an "AS IS" BASIS,
40
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41
- * See the License for the specific language governing permissions and
42
- * limitations under the License.
39
+ * Unless required by applicable law or agreed to in writing, software
40
+ * distributed under the License is distributed on an "AS IS" BASIS,
41
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42
+ * See the License for the specific language governing permissions and
43
+ * limitations under the License.
43
44
******************************************************************************/
44
45
45
46
#include "utils/base58.h"
54
55
55
56
#include "utils/utils.h"
56
57
58
+ ////////////////////////////////////////////////////////////////////////////////
59
+ #define MAX_ENC_INPUT_SIZE 120
60
+
57
61
////////////////////////////////////////////////////////////////////////////////
58
62
static const uint8_t BASE58ALPHABET [] = {
59
63
'1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' ,
@@ -63,71 +67,64 @@ static const uint8_t BASE58ALPHABET[] = {
63
67
};
64
68
65
69
////////////////////////////////////////////////////////////////////////////////
66
- uint8_t encodeBase58 (uint8_t * in ,
67
- size_t inSize ,
68
- uint8_t * out ,
69
- size_t maxOutSize ) {
70
- uint8_t tmp [164 ];
71
- uint8_t buffer [164 ];
72
- size_t j ;
73
- size_t startAt ;
70
+ // src: https://github.com/LedgerHQ/ledger-app-btc/blob/master/src/btchip_base58.c#L79
71
+ int btchip_encode_base58 (const unsigned char * in , size_t length ,
72
+ unsigned char * out , size_t * outlen ) {
73
+ unsigned char buffer [MAX_ENC_INPUT_SIZE * 138 / 100 + 1 ] = {0 };
74
+ size_t i = 0 , j ;
75
+ size_t startAt , stopAt ;
74
76
size_t zeroCount = 0 ;
77
+ size_t outputSize ;
75
78
76
- if (inSize > sizeof ( tmp ) ) {
77
- return 0 ;
79
+ if (length > MAX_ENC_INPUT_SIZE ) {
80
+ return -1 ;
78
81
}
79
82
80
- bytecpy (tmp , in , inSize );
81
- while ((zeroCount < inSize ) && (tmp [zeroCount ] == 0U )) {
83
+ while ((zeroCount < length ) && (in [zeroCount ] == 0 )) {
82
84
++ zeroCount ;
83
85
}
84
86
85
- j = 2 * inSize ;
86
- startAt = zeroCount ;
87
- while (startAt < inSize ) {
88
- size_t remainder = 0 ;
89
- size_t divLoop ;
90
-
91
- for (divLoop = startAt ; divLoop < inSize ; divLoop ++ ) {
92
- size_t digit256 = (size_t )(tmp [divLoop ] & 0xff );
93
- size_t tmpDiv = remainder * 256 + digit256 ;
94
- tmp [divLoop ] = (uint8_t )(tmpDiv / 58 );
95
- remainder = (tmpDiv % 58 );
87
+ outputSize = (length - zeroCount ) * 138 / 100 + 1 ;
88
+ stopAt = outputSize - 1 ;
89
+ for (startAt = zeroCount ; startAt < length ; startAt ++ ) {
90
+ int carry = in [startAt ];
91
+ for (j = outputSize - 1 ; (int )j >= 0 ; j -- ) {
92
+ carry += 256 * buffer [j ];
93
+ buffer [j ] = carry % 58 ;
94
+ carry /= 58 ;
95
+
96
+ if (j <= stopAt - 1 && carry == 0 ) {
97
+ break ;
98
+ }
96
99
}
97
-
98
- if (tmp [startAt ] == 0 ) {
99
- ++ startAt ;
100
- }
101
-
102
- buffer [-- j ] = (uint8_t )BASE58ALPHABET [remainder ];
100
+ stopAt = j ;
103
101
}
104
102
105
- while ((j < (2 * inSize )) && (buffer [j ] == BASE58ALPHABET [0 ])) {
106
- ++ j ;
103
+ j = 0 ;
104
+ while (j < outputSize && buffer [j ] == 0 ) {
105
+ j += 1 ;
107
106
}
108
107
109
- while (zeroCount -- > 0 ) {
110
- buffer [-- j ] = BASE58ALPHABET [0 ];
108
+ if (* outlen < zeroCount + outputSize - j ) {
109
+ * outlen = zeroCount + outputSize - j ;
110
+ return -1 ;
111
111
}
112
112
113
- inSize = 2 * inSize - j ;
114
- if (maxOutSize < inSize ) {
115
- explicit_bzero (out , sizeof (out ));
116
- return 0 ;
117
- }
113
+ os_memset (out , BASE58ALPHABET [0 ], zeroCount );
118
114
119
- bytecpy (out , (buffer + j ), inSize );
115
+ i = zeroCount ;
116
+ while (j < outputSize ) {
117
+ out [i ++ ] = BASE58ALPHABET [buffer [j ++ ]];
118
+ }
119
+ * outlen = i ;
120
120
121
- return inSize ;
121
+ return 0 ;
122
122
}
123
123
124
124
////////////////////////////////////////////////////////////////////////////////
125
- uint16_t encodeBase58PublicKey (uint8_t * in ,
126
- size_t inSize ,
127
- uint8_t * out ,
128
- size_t outSize ,
129
- uint16_t version ,
130
- uint8_t alreadyHashed ) {
125
+ int encodeBase58PublicKey (uint8_t * in , size_t inSize ,
126
+ uint8_t * out , size_t outSize ,
127
+ uint16_t version , uint8_t alreadyHashed ) {
131
128
uint8_t temp [inSize + 4 ];
132
129
uint8_t checksum [HASH_32_LEN ];
133
130
size_t versionSize = (version > 255U ? 2 : 1 );
@@ -153,5 +150,5 @@ uint16_t encodeBase58PublicKey(uint8_t *in,
153
150
154
151
bytecpy (& temp [ripeLength ], checksum , 4 );
155
152
156
- return encodeBase58 (temp , ripeLength + 4 , out , outSize );
153
+ return btchip_encode_base58 (temp , ripeLength + 4 , out , & outSize );
157
154
}
0 commit comments