Botan 2.19.1
Crypto and TLS for C&
hex.cpp
Go to the documentation of this file.
1/*
2* Hex Encoding and Decoding
3* (C) 2010,2020 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/hex.h>
9#include <botan/mem_ops.h>
10#include <botan/exceptn.h>
11#include <botan/internal/ct_utils.h>
12
13namespace Botan {
14
15namespace {
16
17char hex_encode_nibble(uint8_t n, bool uppercase)
18 {
19 BOTAN_DEBUG_ASSERT(n <= 15);
20
21 const auto in_09 = CT::Mask<uint8_t>::is_lt(n, 10);
22
23 const char c_09 = n + '0';
24 const char c_af = n + (uppercase ? 'A' : 'a') - 10;
25
26 return in_09.select(c_09, c_af);
27 }
28
29}
30
31void hex_encode(char output[],
32 const uint8_t input[],
33 size_t input_length,
34 bool uppercase)
35 {
36 for(size_t i = 0; i != input_length; ++i)
37 {
38 const uint8_t n0 = (input[i] >> 4) & 0xF;
39 const uint8_t n1 = (input[i] ) & 0xF;
40
41 output[2*i ] = hex_encode_nibble(n0, uppercase);
42 output[2*i+1] = hex_encode_nibble(n1, uppercase);
43 }
44 }
45
46std::string hex_encode(const uint8_t input[],
47 size_t input_length,
48 bool uppercase)
49 {
50 std::string output(2 * input_length, 0);
51
52 if(input_length)
53 hex_encode(&output.front(), input, input_length, uppercase);
54
55 return output;
56 }
57
58namespace {
59
60uint8_t hex_char_to_bin(char input)
61 {
62 const uint8_t c = static_cast<uint8_t>(input);
63
64 const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('F'));
65 const auto is_alpha_lower = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('f'));
66 const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('0'), uint8_t('9'));
67
68 const auto is_whitespace = CT::Mask<uint8_t>::is_any_of(c, {
69 uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r')
70 });
71
72 const uint8_t c_upper = c - uint8_t('A') + 10;
73 const uint8_t c_lower = c - uint8_t('a') + 10;
74 const uint8_t c_decim = c - uint8_t('0');
75
76 uint8_t ret = 0xFF; // default value
77
78 ret = is_alpha_upper.select(c_upper, ret);
79 ret = is_alpha_lower.select(c_lower, ret);
80 ret = is_decimal.select(c_decim, ret);
81 ret = is_whitespace.select(0x80, ret);
82
83 return ret;
84 }
85
86}
87
88
89size_t hex_decode(uint8_t output[],
90 const char input[],
91 size_t input_length,
92 size_t& input_consumed,
93 bool ignore_ws)
94 {
95 uint8_t* out_ptr = output;
96 bool top_nibble = true;
97
98 clear_mem(output, input_length / 2);
99
100 for(size_t i = 0; i != input_length; ++i)
101 {
102 const uint8_t bin = hex_char_to_bin(input[i]);
103
104 if(bin >= 0x10)
105 {
106 if(bin == 0x80 && ignore_ws)
107 continue;
108
109 std::string bad_char(1, input[i]);
110 if(bad_char == "\t")
111 bad_char = "\\t";
112 else if(bad_char == "\n")
113 bad_char = "\\n";
114
115 throw Invalid_Argument(
116 std::string("hex_decode: invalid hex character '") +
117 bad_char + "'");
118 }
119
120 if(top_nibble)
121 *out_ptr |= bin << 4;
122 else
123 *out_ptr |= bin;
124
125 top_nibble = !top_nibble;
126 if(top_nibble)
127 ++out_ptr;
128 }
129
130 input_consumed = input_length;
131 size_t written = (out_ptr - output);
132
133 /*
134 * We only got half of a uint8_t at the end; zap the half-written
135 * output and mark it as unread
136 */
137 if(!top_nibble)
138 {
139 *out_ptr = 0;
140 input_consumed -= 1;
141 }
142
143 return written;
144 }
145
146size_t hex_decode(uint8_t output[],
147 const char input[],
148 size_t input_length,
149 bool ignore_ws)
150 {
151 size_t consumed = 0;
152 size_t written = hex_decode(output, input, input_length,
153 consumed, ignore_ws);
154
155 if(consumed != input_length)
156 throw Invalid_Argument("hex_decode: input did not have full bytes");
157
158 return written;
159 }
160
161size_t hex_decode(uint8_t output[],
162 const std::string& input,
163 bool ignore_ws)
164 {
165 return hex_decode(output, input.data(), input.length(), ignore_ws);
166 }
167
169 size_t input_length,
170 bool ignore_ws)
171 {
172 secure_vector<uint8_t> bin(1 + input_length / 2);
173
174 size_t written = hex_decode(bin.data(),
175 input,
176 input_length,
177 ignore_ws);
178
179 bin.resize(written);
180 return bin;
181 }
182
184 bool ignore_ws)
185 {
186 return hex_decode_locked(input.data(), input.size(), ignore_ws);
187 }
188
189std::vector<uint8_t> hex_decode(const char input[],
190 size_t input_length,
191 bool ignore_ws)
192 {
193 std::vector<uint8_t> bin(1 + input_length / 2);
194
195 size_t written = hex_decode(bin.data(),
196 input,
197 input_length,
198 ignore_ws);
199
200 bin.resize(written);
201 return bin;
202 }
203
204std::vector<uint8_t> hex_decode(const std::string& input,
205 bool ignore_ws)
206 {
207 return hex_decode(input.data(), input.size(), ignore_ws);
208 }
209
210}
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
static Mask< T > is_any_of(T v, std::initializer_list< T > accepted)
Definition: ct_utils.h:196
static Mask< T > is_lt(T x, T y)
Definition: ct_utils.h:157
static Mask< T > is_within_range(T v, T l, T u)
Definition: ct_utils.h:186
Definition: alg_id.cpp:13
secure_vector< uint8_t > hex_decode_locked(const char input[], size_t input_length, bool ignore_ws)
Definition: hex.cpp:168
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:31
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:89
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:115