-
Notifications
You must be signed in to change notification settings - Fork 1
/
BRBase58.c
187 lines (147 loc) · 6.41 KB
/
BRBase58.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// BRBase58.c
// breadwallet-core
//
// Created by Aaron Voisine on 9/15/15.
// Copyright (c) 2015 breadwallet LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "BRBase58.h"
#include "BRCrypto.h"
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
// base58 and base58check encoding: https://en.bitcoin.it/wiki/Base58Check_encoding
static const char base58chars[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
// returns the number of characters written to str including NULL terminator, or total strLen needed if str is NULL
size_t BRBase58Encode(char *str, size_t strLen, const uint8_t *data, size_t dataLen)
{
size_t i, j, len, zcount = 0;
assert(data != NULL);
while (zcount < dataLen && data && data[zcount] == 0) zcount++; // count leading zeroes
uint8_t buf[(dataLen - zcount)*138/100 + 1]; // log(256)/log(58), rounded up
memset(buf, 0, sizeof(buf));
for (i = zcount; data && i < dataLen; i++) {
uint32_t carry = data[i];
for (j = sizeof(buf); j > 0; j--) {
carry += (uint32_t)buf[j - 1] << 8;
buf[j - 1] = carry % 58;
carry /= 58;
}
var_clean(&carry);
}
i = 0;
while (i < sizeof(buf) && buf[i] == 0) i++; // skip leading zeroes
len = (zcount + sizeof(buf) - i) + 1;
if (str && len <= strLen) {
while (zcount-- > 0) *(str++) = base58chars[0];
while (i < sizeof(buf)) *(str++) = base58chars[buf[i++]];
*str = '\0';
}
mem_clean(buf, sizeof(buf));
return (! str || len <= strLen) ? len : 0;
}
// returns the number of bytes written to data, or total dataLen needed if data is NULL
size_t BRBase58Decode(uint8_t *data, size_t dataLen, const char *str)
{
size_t i = 0, j, len, zcount = 0;
assert(str != NULL);
while (str && *str == base58chars[0]) str++, zcount++; // count leading zeroes
uint8_t buf[(str) ? strlen(str)*733/1000 + 1 : 0]; // log(58)/log(256), rounded up
memset(buf, 0, sizeof(buf));
while (str && *str) {
uint32_t carry = *(const uint8_t *)(str++);
switch (carry) {
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
carry -= '1';
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H':
carry += 9 - 'A';
break;
case 'J': case 'K': case 'L': case 'M': case 'N':
carry += 17 - 'J';
break;
case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y':
case 'Z':
carry += 22 - 'P';
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k':
carry += 33 - 'a';
break;
case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v':
case 'w': case 'x': case 'y': case 'z':
carry += 44 - 'm';
break;
default:
carry = UINT32_MAX;
}
if (carry >= 58) break; // invalid base58 digit
for (j = sizeof(buf); j > 0; j--) {
carry += (uint32_t)buf[j - 1]*58;
buf[j - 1] = carry & 0xff;
carry >>= 8;
}
var_clean(&carry);
}
while (i < sizeof(buf) && buf[i] == 0) i++; // skip leading zeroes
len = zcount + sizeof(buf) - i;
if (data && len <= dataLen) {
if (zcount > 0) memset(data, 0, zcount);
memcpy(&data[zcount], &buf[i], sizeof(buf) - i);
}
mem_clean(buf, sizeof(buf));
return (! data || len <= dataLen) ? len : 0;
}
// returns the number of characters written to str including NULL terminator, or total strLen needed if str is NULL
size_t BRBase58CheckEncode(char *str, size_t strLen, const uint8_t *data, size_t dataLen)
{
size_t len = 0, bufLen = dataLen + 256/8;
uint8_t _buf[(bufLen <= 0x1000) ? bufLen : 0], *buf = (bufLen <= 0x1000) ? _buf : malloc(bufLen);
assert(buf != NULL);
assert(data != NULL || dataLen == 0);
if (data || dataLen == 0) {
memcpy(buf, data, dataLen);
BRSHA256_2(&buf[dataLen], data, dataLen);
len = BRBase58Encode(str, strLen, buf, dataLen + 4);
}
mem_clean(buf, bufLen);
if (buf != _buf) free(buf);
return len;
}
// returns the number of bytes written to data, or total dataLen needed if data is NULL
size_t BRBase58CheckDecode(uint8_t *data, size_t dataLen, const char *str)
{
size_t len, bufLen = (str) ? strlen(str) : 0;
uint8_t md[256/8], _buf[(bufLen <= 0x1000) ? bufLen : 0], *buf = (bufLen <= 0x1000) ? _buf : malloc(bufLen);
assert(str != NULL);
assert(buf != NULL);
len = BRBase58Decode(buf, bufLen, str);
if (len >= 4) {
len -= 4;
BRSHA256_2(md, buf, len);
if (memcmp(&buf[len], md, sizeof(uint32_t)) != 0) len = 0; // verify checksum
if (data && len <= dataLen) memcpy(data, buf, len);
}
else len = 0;
mem_clean(buf, bufLen);
if (buf != _buf) free(buf);
return (! data || len <= dataLen) ? len : 0;
}