Debian Codegraph @

crt/s2n/tests/testlib/s2n_hex_testlib.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <string.h>
17
18 #include "error/s2n_errno.h"
19 #include "stuffer/s2n_stuffer.h"
20 #include "testlib/s2n_testlib.h"
21 #include "utils/s2n_safety.h"
22
23 S2N_RESULT s2n_stuffer_alloc_from_hex(struct s2n_stuffer *bytes_out, const char *hex_cstr)
24 {
25 RESULT_ENSURE_REF(bytes_out);
26 RESULT_ENSURE_REF(hex_cstr);
27
28 DEFER_CLEANUP(struct s2n_stuffer hex = { 0 }, s2n_stuffer_free);
29 /* Copying the hex into heap memory to handle the 'const' isn't exactly efficient,
30 * but for a testlib method it is sufficient.
31 */
32 RESULT_GUARD_POSIX(s2n_stuffer_alloc(&hex, strlen(hex_cstr)));
33 RESULT_GUARD_POSIX(s2n_stuffer_write_str(&hex, hex_cstr));
34
35 uint32_t bytes_size = strlen(hex_cstr) / 2;
36 RESULT_GUARD_POSIX(s2n_stuffer_alloc(bytes_out, bytes_size));
37 RESULT_GUARD(s2n_stuffer_read_hex(&hex, &bytes_out->blob));
38 RESULT_ENSURE(s2n_stuffer_data_available(&hex) == 0, S2N_ERR_BAD_HEX);
39 RESULT_GUARD_POSIX(s2n_stuffer_skip_write(bytes_out, bytes_size));
40 return S2N_RESULT_OK;
41 }
42
43 S2N_RESULT s2n_blob_alloc_from_hex_with_whitespace(struct s2n_blob *bytes_out, const char *hex_cstr)
44 {
45 RESULT_ENSURE_REF(bytes_out);
46 RESULT_ENSURE_REF(hex_cstr);
47
48 DEFER_CLEANUP(struct s2n_stuffer hex_in = { 0 }, s2n_stuffer_free);
49 RESULT_GUARD_POSIX(s2n_stuffer_alloc(&hex_in, strlen(hex_cstr)));
50 for (size_t i = 0; i < strlen(hex_cstr); i++) {
51 if (hex_cstr[i] == ' ') {
52 continue;
53 }
54 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&hex_in, hex_cstr[i]));
55 }
56 uint32_t hex_in_size = s2n_stuffer_data_available(&hex_in);
57
58 RESULT_GUARD_POSIX(s2n_alloc(bytes_out, hex_in_size / 2));
59 RESULT_GUARD(s2n_stuffer_read_hex(&hex_in, bytes_out));
60 RESULT_ENSURE(s2n_stuffer_data_available(&hex_in) == 0, S2N_ERR_BAD_HEX);
61 return S2N_RESULT_OK;
62 }