1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
/*
* Copyright (c) 2026 Egor Tensin <egor@tensin.name>
* This file is part of the "AES tools" project.
* For details, see https://github.com/egor-tensin/aes-tools
* Distributed under the MIT License.
*/
#include <aes/all.h>
#include <stdio.h>
#include <stdlib.h>
AES_StatusCode aes_parse_hex_string(
unsigned char* dest,
const char* src,
size_t numof_bytes,
AES_ErrorDetails* err_details
) {
if (dest == NULL)
return aes_error_null_argument(err_details, "dest");
if (src == NULL)
return aes_error_null_argument(err_details, "src");
char what[30];
snprintf(what, sizeof(what), "a %zu-byte hex string", numof_bytes);
const char* cursor = src;
for (size_t i = 0; i < numof_bytes; ++i) {
int n;
unsigned int byte;
if (sscanf(cursor, "%2x%n", &byte, &n) != 1 || n != 2)
return aes_error_parse(err_details, src, what);
dest[i] = (unsigned char)byte;
cursor += n;
}
if (*cursor != '\0')
return aes_error_parse(err_details, src, what);
return AES_SUCCESS;
}
char* aes_format_block_hex_partial(char* dest, AES_Block block, size_t numof_bytes) {
char* cursor = dest;
AES_ALIGN(unsigned char, 16) bytes[16];
aes_store_block_aligned(bytes, block);
for (size_t i = 0; i < numof_bytes; ++i, cursor += 2)
sprintf(cursor, "%02x", bytes[i]);
return cursor;
}
char* aes_format_block_hex(char* dest, AES_Block block) {
return aes_format_block_hex_partial(dest, block, 16);
}
|