23 #ifndef ASCON_UTILITY_H
24 #define ASCON_UTILITY_H
64 (
char *out,
size_t outlen,
const unsigned char *in,
size_t inlen,
83 (
unsigned char *out,
size_t outlen,
const char *in,
size_t inlen);
89 #define ASCON_NO_STL 1
92 #if !defined(ASCON_NO_STL) || defined(ASCON_SUITE_DOC)
131 explicit byte_array(
size_t size,
unsigned char value = 0);
135 if (p && (--(p->ref)) == 0)
144 if (p && (--(p->ref)) == 0)
151 unsigned char &operator[](
size_t pos);
152 const unsigned char &operator[](
size_t pos)
const;
154 inline size_t size()
const {
return p ? p->size : 0; }
155 inline size_t capacity()
const {
return p ? p->capacity : 0; }
156 inline bool empty()
const {
return !p || p->size == 0; }
158 inline unsigned char *
data()
168 inline const unsigned char *
data()
const {
return p ? p->data : 0; }
170 void reserve(
size_t size);
171 void resize(
size_t size);
175 if (p && (--(p->ref)) == 0)
180 void push_back(
unsigned char value);
183 inline bool operator==(
const byte_array &other)
const
185 return cmp(other) == 0;
187 inline bool operator!=(
const byte_array &other)
const
189 return cmp(other) != 0;
191 inline bool operator<(
const byte_array &other)
const
193 return cmp(other) < 0;
195 inline bool operator<=(
const byte_array &other)
const
197 return cmp(other) <= 0;
199 inline bool operator>(
const byte_array &other)
const
201 return cmp(other) > 0;
203 inline bool operator>=(
const byte_array &other)
const
205 return cmp(other) >= 0;
208 typedef unsigned char *iterator;
209 typedef const unsigned char *const_iterator;
211 inline iterator begin() {
return data(); }
212 inline iterator end() {
return data() + size(); }
213 inline const_iterator begin()
const {
return data(); }
214 inline const_iterator end()
const {
return data() + size(); }
215 inline const_iterator cbegin()
const {
return data(); }
216 inline const_iterator cend()
const {
return data() + size(); }
219 struct byte_array_private
226 inline byte_array_private(
size_t reserve)
230 ,
data(
new unsigned char [reserve])
233 inline ~byte_array_private() {
delete[]
data; }
236 mutable byte_array_private *p;
238 void detach(
size_t capacity = 0)
const;
246 #if !defined(ARDUINO) || defined(ASCON_SUITE_DOC)
262 static inline byte_array bytes_from_hex(
const char *str,
size_t len)
280 static inline byte_array bytes_from_hex(
const char *str)
282 return bytes_from_hex(str, str ? ::strlen(str) : 0);
285 #if !defined(ASCON_NO_STL) || defined(ASCON_SUITE_DOC)
297 static inline std::string bytes_to_hex
298 (
const unsigned char *in,
size_t len,
bool upper_case =
false)
300 char out[len * 2U + 1U];
302 (out,
sizeof(out), in, len, upper_case ? 1 : 0);
303 return std::string(out);
315 static inline std::string bytes_to_hex
316 (
const byte_array &in,
bool upper_case =
false)
318 size_t len = in.size();
319 char out[len * 2U + 1U];
321 (out,
sizeof(out), in.data(), len, upper_case ? 1 : 0);
322 return std::string(out);
333 static inline byte_array bytes_from_hex(
const std::string &str)
335 return bytes_from_hex(str.data(), str.size());
342 #elif defined(ARDUINO)
349 static inline String bytes_to_hex
350 (
const unsigned char *in,
size_t len,
bool upper_case =
false)
352 char out[len * 2U + 1U];
354 (out,
sizeof(out), in, len, upper_case ? 1 : 0);
358 static inline String bytes_to_hex
359 (
const byte_array &in,
bool upper_case =
false)
361 size_t len = in.size();
362 char out[len * 2U + 1U];
364 (out,
sizeof(out), in.data(), len, upper_case ? 1 : 0);
368 static inline byte_array bytes_from_hex(
const char *str,
size_t len)
378 static inline byte_array bytes_from_hex(
const char *str)
380 return bytes_from_hex(str, str ? ::strlen(str) : 0);
383 static inline byte_array bytes_from_hex(
const String &str)
385 return bytes_from_hex(str.c_str(), str.length());
std::vector< unsigned char > byte_array
C++ type for an array of bytes.
unsigned char data[8]
[snippet_key]
void ascon_clean(void *buf, unsigned size)
Cleans a buffer that contains sensitive material.
int ascon_bytes_from_hex(unsigned char *out, size_t outlen, const char *in, size_t inlen)
Converts a hexadecimal string into an array of bytes.
int ascon_bytes_to_hex(char *out, size_t outlen, const unsigned char *in, size_t inlen, int upper_case)
Converts an array of bytes into a hexadecimal string.