ASCON Suite
utility.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef ASCON_UTILITY_H
24 #define ASCON_UTILITY_H
25 
31 #include <stddef.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
43 void ascon_clean(void *buf, unsigned size);
44 
64  (char *out, size_t outlen, const unsigned char *in, size_t inlen,
65  int upper_case);
66 
83  (unsigned char *out, size_t outlen, const char *in, size_t inlen);
84 
85 #ifdef __cplusplus
86 }
87 
88 #if defined(ARDUINO)
89 #define ASCON_NO_STL 1
90 #endif
91 
92 #if !defined(ASCON_NO_STL) || defined(ASCON_SUITE_DOC)
93 
94 #include <vector>
95 #include <string.h>
96 
97 namespace ascon
98 {
99 
109  typedef std::vector<unsigned char> byte_array;
110 }
111 
112 #else /* ASCON_NO_STL */
113 
114 #include <string.h>
115 
116 namespace ascon
117 {
118 
119 class byte_array
120 {
121 public:
122  inline byte_array() : p(0) {}
123 
124  inline byte_array(const byte_array &other)
125  : p(other.p)
126  {
127  if (p)
128  ++(p->ref);
129  }
130 
131  explicit byte_array(size_t size, unsigned char value = 0);
132 
133  inline ~byte_array()
134  {
135  if (p && (--(p->ref)) == 0)
136  delete p;
137  }
138 
139  inline byte_array &operator=(const byte_array &other)
140  {
141  if (p != other.p) {
142  if (other.p)
143  ++(other.p->ref);
144  if (p && (--(p->ref)) == 0)
145  delete p;
146  p = other.p;
147  }
148  return *this;
149  }
150 
151  unsigned char &operator[](size_t pos);
152  const unsigned char &operator[](size_t pos) const;
153 
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; }
157 
158  inline unsigned char *data()
159  {
160  if (p) {
161  if (p->ref > 1)
162  detach();
163  return p->data;
164  } else {
165  return 0;
166  }
167  }
168  inline const unsigned char *data() const { return p ? p->data : 0; }
169 
170  void reserve(size_t size);
171  void resize(size_t size);
172 
173  inline void clear()
174  {
175  if (p && (--(p->ref)) == 0)
176  delete p;
177  p = 0;
178  }
179 
180  void push_back(unsigned char value);
181  void pop_back();
182 
183  inline bool operator==(const byte_array &other) const
184  {
185  return cmp(other) == 0;
186  }
187  inline bool operator!=(const byte_array &other) const
188  {
189  return cmp(other) != 0;
190  }
191  inline bool operator<(const byte_array &other) const
192  {
193  return cmp(other) < 0;
194  }
195  inline bool operator<=(const byte_array &other) const
196  {
197  return cmp(other) <= 0;
198  }
199  inline bool operator>(const byte_array &other) const
200  {
201  return cmp(other) > 0;
202  }
203  inline bool operator>=(const byte_array &other) const
204  {
205  return cmp(other) >= 0;
206  }
207 
208  typedef unsigned char *iterator;
209  typedef const unsigned char *const_iterator;
210 
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(); }
217 
218 private:
219  struct byte_array_private
220  {
221  size_t ref;
222  size_t size;
223  size_t capacity;
224  unsigned char *data;
225 
226  inline byte_array_private(size_t reserve)
227  : ref(1)
228  , size(0)
229  , capacity(reserve)
230  , data(new unsigned char [reserve])
231  {
232  }
233  inline ~byte_array_private() { delete[] data; }
234  };
235 
236  mutable byte_array_private *p;
237 
238  void detach(size_t capacity = 0) const;
239  int cmp(const byte_array &other) const;
240 };
241 
242 } /* namespace ascon */
243 
244 #endif /* ASCON_NO_STL */
245 
246 #if !defined(ARDUINO) || defined(ASCON_SUITE_DOC)
247 
248 #include <string>
249 
250 namespace ascon
251 {
252 
262 static inline byte_array bytes_from_hex(const char *str, size_t len)
263 {
264  byte_array vec(len / 2);
265  int result = ::ascon_bytes_from_hex(vec.data(), vec.size(), str, len);
266  if (result != -1)
267  return vec;
268  else
269  return byte_array();
270 }
271 
280 static inline byte_array bytes_from_hex(const char *str)
281 {
282  return bytes_from_hex(str, str ? ::strlen(str) : 0);
283 }
284 
285 #if !defined(ASCON_NO_STL) || defined(ASCON_SUITE_DOC)
286 
297 static inline std::string bytes_to_hex
298  (const unsigned char *in, size_t len, bool upper_case = false)
299 {
300  char out[len * 2U + 1U];
302  (out, sizeof(out), in, len, upper_case ? 1 : 0);
303  return std::string(out);
304 }
305 
315 static inline std::string bytes_to_hex
316  (const byte_array &in, bool upper_case = false)
317 {
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);
323 }
324 
333 static inline byte_array bytes_from_hex(const std::string &str)
334 {
335  return bytes_from_hex(str.data(), str.size());
336 }
337 
338 #endif /* !ASCON_NO_STL */
339 
340 } /* namespace ascon */
341 
342 #elif defined(ARDUINO)
343 
344 #include <WString.h>
345 
346 namespace ascon
347 {
348 
349 static inline String bytes_to_hex
350  (const unsigned char *in, size_t len, bool upper_case = false)
351 {
352  char out[len * 2U + 1U];
354  (out, sizeof(out), in, len, upper_case ? 1 : 0);
355  return String(out);
356 }
357 
358 static inline String bytes_to_hex
359  (const byte_array &in, bool upper_case = false)
360 {
361  size_t len = in.size();
362  char out[len * 2U + 1U];
364  (out, sizeof(out), in.data(), len, upper_case ? 1 : 0);
365  return String(out);
366 }
367 
368 static inline byte_array bytes_from_hex(const char *str, size_t len)
369 {
370  byte_array vec(len / 2);
371  int result = ::ascon_bytes_from_hex(vec.data(), vec.size(), str, len);
372  if (result != -1)
373  return vec;
374  else
375  return byte_array();
376 }
377 
378 static inline byte_array bytes_from_hex(const char *str)
379 {
380  return bytes_from_hex(str, str ? ::strlen(str) : 0);
381 }
382 
383 static inline byte_array bytes_from_hex(const String &str)
384 {
385  return bytes_from_hex(str.c_str(), str.length());
386 }
387 
388 } /* namespace ascon */
389 
390 #endif /* ARDUINO */
391 
392 #endif /* _cplusplus */
393 
394 #endif
Definition: hash.h:211
std::vector< unsigned char > byte_array
C++ type for an array of bytes.
Definition: utility.h:109
unsigned char data[8]
[snippet_key]
Definition: snippets.c:14
void ascon_clean(void *buf, unsigned size)
Cleans a buffer that contains sensitive material.
Definition: ascon-clean.c:38
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.
Definition: ascon-hex.c:49
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.
Definition: ascon-hex.c:26