• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.05
/src/lib/codec/base58/base58.cpp
1
/*
2
* (C) 2018,2020 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include <botan/base58.h>
8

9
#include <botan/bigint.h>
10
#include <botan/exceptn.h>
11
#include <botan/hash.h>
12
#include <botan/internal/ct_utils.h>
13
#include <botan/internal/divide.h>
14
#include <botan/internal/loadstor.h>
15

16
namespace Botan {
17

18
namespace {
19

20
uint32_t sha256_d_checksum(const uint8_t input[], size_t input_length) {
16✔
21
   auto sha256 = HashFunction::create_or_throw("SHA-256");
16✔
22

23
   std::vector<uint8_t> checksum(32);
16✔
24

25
   sha256->update(input, input_length);
16✔
26
   sha256->final(checksum);
16✔
27

28
   sha256->update(checksum);
16✔
29
   sha256->final(checksum);
16✔
30

31
   return load_be<uint32_t>(checksum.data(), 0);
16✔
32
}
32✔
33

34
char lookup_base58_char(uint8_t x) {
333✔
35
   // "123456789 ABCDEFGH JKLMN PQRSTUVWXYZ abcdefghijk mnopqrstuvwxyz"
36
   BOTAN_DEBUG_ASSERT(x < 58);
333✔
37

38
   const auto is_dec_19 = CT::Mask<uint8_t>::is_lte(x, 8);
333✔
39
   const auto is_alpha_AH = CT::Mask<uint8_t>::is_within_range(x, 9, 16);
333✔
40
   const auto is_alpha_JN = CT::Mask<uint8_t>::is_within_range(x, 17, 21);
333✔
41
   const auto is_alpha_PZ = CT::Mask<uint8_t>::is_within_range(x, 22, 32);
333✔
42
   const auto is_alpha_ak = CT::Mask<uint8_t>::is_within_range(x, 33, 43);
333✔
43
   // otherwise in 'm'-'z'
44

45
   const char c_19 = '1' + x;
333✔
46
   const char c_AH = 'A' + (x - 9);
333✔
47
   const char c_JN = 'J' + (x - 17);
333✔
48
   const char c_PZ = 'P' + (x - 22);
333✔
49
   const char c_ak = 'a' + (x - 33);
333✔
50
   const char c_mz = 'm' + (x - 44);
333✔
51

52
   char ret = c_mz;
333✔
53
   ret = is_dec_19.select(c_19, ret);
333✔
54
   ret = is_alpha_AH.select(c_AH, ret);
333✔
55
   ret = is_alpha_JN.select(c_JN, ret);
333✔
56
   ret = is_alpha_PZ.select(c_PZ, ret);
333✔
57
   ret = is_alpha_ak.select(c_ak, ret);
333✔
58

59
   return ret;
333✔
60
}
61

62
std::string base58_encode(BigInt v, size_t leading_zeros) {
26✔
63
   const word radix = 58;
26✔
64

65
   std::string result;
26✔
66
   BigInt q;
26✔
67

68
   while(v.is_nonzero()) {
718✔
69
      word r;
333✔
70
      ct_divide_word(v, radix, q, r);
333✔
71
      result.push_back(lookup_base58_char(static_cast<uint8_t>(r)));
333✔
72
      v.swap(q);
333✔
73
   }
74

75
   for(size_t i = 0; i != leading_zeros; ++i) {
38✔
76
      result.push_back('1');  // 'zero' byte
12✔
77
   }
78

79
   return std::string(result.rbegin(), result.rend());
26✔
80
}
33✔
81

82
template <typename T, typename Z>
83
size_t count_leading_zeros(const T input[], size_t input_length, Z zero) {
45✔
84
   size_t leading_zeros = 0;
45✔
85

86
   while(leading_zeros < input_length && input[leading_zeros] == zero) {
88✔
87
      leading_zeros += 1;
24✔
88
   }
89

90
   return leading_zeros;
91
}
92

93
uint8_t base58_value_of(char input) {
336✔
94
   // "123456789 ABCDEFGH JKLMN PQRSTUVWXYZ abcdefghijk mnopqrstuvwxyz"
95

96
   const uint8_t c = static_cast<uint8_t>(input);
336✔
97

98
   const auto is_dec_19 = CT::Mask<uint8_t>::is_within_range(c, uint8_t('1'), uint8_t('9'));
336✔
99
   const auto is_alpha_AH = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('H'));
336✔
100
   const auto is_alpha_JN = CT::Mask<uint8_t>::is_within_range(c, uint8_t('J'), uint8_t('N'));
336✔
101
   const auto is_alpha_PZ = CT::Mask<uint8_t>::is_within_range(c, uint8_t('P'), uint8_t('Z'));
336✔
102

103
   const auto is_alpha_ak = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('k'));
336✔
104
   const auto is_alpha_mz = CT::Mask<uint8_t>::is_within_range(c, uint8_t('m'), uint8_t('z'));
336✔
105

106
   const uint8_t c_dec_19 = c - uint8_t('1');
336✔
107
   const uint8_t c_AH = c - uint8_t('A') + 9;
336✔
108
   const uint8_t c_JN = c - uint8_t('J') + 17;
336✔
109
   const uint8_t c_PZ = c - uint8_t('P') + 22;
336✔
110

111
   const uint8_t c_ak = c - uint8_t('a') + 33;
336✔
112
   const uint8_t c_mz = c - uint8_t('m') + 44;
336✔
113

114
   uint8_t ret = 0xFF;  // default value
336✔
115

116
   ret = is_dec_19.select(c_dec_19, ret);
336✔
117
   ret = is_alpha_AH.select(c_AH, ret);
336✔
118
   ret = is_alpha_JN.select(c_JN, ret);
336✔
119
   ret = is_alpha_PZ.select(c_PZ, ret);
336✔
120
   ret = is_alpha_ak.select(c_ak, ret);
336✔
121
   ret = is_alpha_mz.select(c_mz, ret);
336✔
122
   return ret;
336✔
123
}
124

125
}  // namespace
126

127
std::string base58_encode(const uint8_t input[], size_t input_length) {
19✔
128
   BigInt v(input, input_length);
19✔
129
   return base58_encode(v, count_leading_zeros(input, input_length, 0));
38✔
130
}
19✔
131

132
std::string base58_check_encode(const uint8_t input[], size_t input_length) {
7✔
133
   BigInt v(input, input_length);
7✔
134
   v <<= 32;
7✔
135
   v += sha256_d_checksum(input, input_length);
7✔
136
   return base58_encode(v, count_leading_zeros(input, input_length, 0));
21✔
137
}
7✔
138

139
std::vector<uint8_t> base58_decode(const char input[], size_t input_length) {
38✔
140
   const size_t leading_zeros = count_leading_zeros(input, input_length, '1');
38✔
141

142
   BigInt v;
38✔
143

144
   for(size_t i = leading_zeros; i != input_length; ++i) {
363✔
145
      const char c = input[i];
336✔
146

147
      if(c == ' ' || c == '\n') {
336✔
148
         continue;
×
149
      }
150

151
      const uint8_t idx = base58_value_of(c);
336✔
152

153
      if(idx == 0xFF) {
336✔
154
         throw Decoding_Error("Invalid base58");
11✔
155
      }
156

157
      v *= 58;
325✔
158
      v += idx;
325✔
159
   }
160

161
   std::vector<uint8_t> output(v.bytes() + leading_zeros);
38✔
162
   v.binary_encode(output.data() + leading_zeros);
27✔
163
   return output;
27✔
164
}
27✔
165

166
std::vector<uint8_t> base58_check_decode(const char input[], size_t input_length) {
10✔
167
   std::vector<uint8_t> dec = base58_decode(input, input_length);
10✔
168

169
   if(dec.size() < 4) {
10✔
170
      throw Decoding_Error("Invalid base58 too short for checksum");
1✔
171
   }
172

173
   const uint32_t computed_checksum = sha256_d_checksum(dec.data(), dec.size() - 4);
9✔
174
   const uint32_t checksum = load_be<uint32_t>(&dec[dec.size() - 4], 0);
9✔
175

176
   if(checksum != computed_checksum) {
9✔
177
      throw Decoding_Error("Invalid base58 checksum");
4✔
178
   }
179

180
   dec.resize(dec.size() - 4);
5✔
181

182
   return dec;
5✔
183
}
5✔
184

185
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc