ASCON Suite
ascon-hex.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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 #include <ascon/utility.h>
24 
26  (char *out, size_t outlen, const unsigned char *in, size_t inlen,
27  int upper_case)
28 {
29  static char const hex_lower[] = "0123456789abcdef";
30  static char const hex_upper[] = "0123456789ABCDEF";
31  const char *hex_chars = upper_case ? hex_upper : hex_lower;
32  size_t posn = 0;
33  if (outlen < (inlen * 2U + 1U)) {
34  if (outlen > 0)
35  out[0] = '\0'; /* For safety in case the caller uses the string */
36  return -1;
37  }
38  while (inlen > 0) {
39  unsigned char ch = *in++;
40  out[posn++] = hex_chars[(ch >> 4) & 0x0F];
41  out[posn++] = hex_chars[ch & 0x0F];
42  --inlen;
43  }
44  out[posn] = '\0';
45  return (int)posn;
46 }
47 
49  (unsigned char *out, size_t outlen, const char *in, size_t inlen)
50 {
51  size_t posn = 0;
52  int value = 0;
53  int nibble = 0;
54  int digit = 0;
55  while (inlen > 0) {
56  char ch = *in++;
57  --inlen;
58  if (ch >= '0' && ch <= '9') {
59  digit = ch - '0';
60  } else if (ch >= 'a' && ch <= 'f') {
61  digit = ch - 'a' + 10;
62  } else if (ch >= 'A' && ch <= 'F') {
63  digit = ch - 'A' + 10;
64  } else if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' ||
65  ch == '\f' || ch == '\v') {
66  continue;
67  } else {
68  return -1;
69  }
70  if (nibble) {
71  if (posn >= outlen) {
72  return -1;
73  }
74  out[posn++] = value | digit;
75  nibble = 0;
76  } else {
77  value = digit << 4;
78  nibble = 1;
79  }
80  }
81  if (nibble) {
82  return -1;
83  }
84  return (int)posn;
85 }
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
System utilities of use to applications that use ASCON.