forked from NLnetLabs/simdzone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantics.c
179 lines (161 loc) · 4.47 KB
/
semantics.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* syntax.c -- presentation format syntax test cases
*
* Copyright (c) 2023, NLnet Labs. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#include <assert.h>
#include <limits.h>
#include <stdarg.h>
#include <setjmp.h>
#include <string.h>
#include <stdlib.h>
#include <cmocka.h>
#if !_WIN32
#include <unistd.h>
#endif
#include "zone.h"
#include "diagnostic.h"
#include "tools.h"
static int32_t digest_test_accept_rr(
zone_parser_t *parser,
const zone_name_t *owner,
uint16_t type,
uint16_t class,
uint32_t ttl,
uint16_t rdlength,
const uint8_t *rdata,
void *user_data)
{
(void)parser;
(void)owner;
(void)type;
(void)class;
(void)ttl;
(void)rdlength;
(void)rdata;
(void)user_data;
return 0;
}
static int32_t parse_digest(const char *input)
{
const uint8_t origin[] = { 0 };
zone_parser_t parser;
zone_name_buffer_t name;
zone_rdata_buffer_t rdata;
zone_buffers_t buffers = { 1, &name, &rdata };
zone_options_t options;
memset(&options, 0, sizeof(options));
options.accept.callback = digest_test_accept_rr;
options.origin.octets = origin;
options.origin.length = sizeof(origin);
options.default_ttl = 3600;
options.default_class = 1;
fprintf(stderr, "INPUT: \"%s\"\n", input);
return zone_parse_string(
&parser, &options, &buffers, input, strlen(input), NULL);
}
/*!cmocka */
void ds_digest_lengths(void **state)
{
static const char fmt[] =
"dskey.example.com. 86400 IN DS 60485 5 %c ( %.*s )";
static const char hex_fmt[] =
"dskey.example.com. 86400 CLASS1 TYPE43 \\# %d EC45 05 0%c ( %.*s )";
static const char hex[] =
"0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef";
static const struct {
int algorithm;
int digest_length;
int32_t code;
} tests[] = {
// 0: Reserved
{ 0, 10, ZONE_SUCCESS },
// 1: SHA-1
{ 1, 20, ZONE_SUCCESS },
{ 1, 19, ZONE_SEMANTIC_ERROR },
{ 1, 21, ZONE_SEMANTIC_ERROR },
// 2: SHA-256
{ 2, 32, ZONE_SUCCESS },
{ 2, 31, ZONE_SEMANTIC_ERROR },
{ 2, 33, ZONE_SEMANTIC_ERROR },
// 3: GOST R 34.11-94
{ 3, 32, ZONE_SUCCESS },
{ 3, 31, ZONE_SEMANTIC_ERROR },
{ 3, 33, ZONE_SEMANTIC_ERROR },
// 4: SHA-384
{ 4, 48, ZONE_SUCCESS },
{ 4, 47, ZONE_SEMANTIC_ERROR },
{ 4, 49, ZONE_SEMANTIC_ERROR },
// 5: GOST R 34.10-2012
{ 5, 48, ZONE_SUCCESS },
{ 5, 47, ZONE_SEMANTIC_ERROR },
{ 5, 49, ZONE_SEMANTIC_ERROR },
// 6: SM3
{ 6, 48, ZONE_SUCCESS },
{ 6, 47, ZONE_SEMANTIC_ERROR },
{ 6, 49, ZONE_SEMANTIC_ERROR }
};
(void)state;
int32_t code;
for (size_t i=0, n = sizeof(tests)/sizeof(tests[0]); i < n; i++) {
char buf[512];
const int algo = tests[i].algorithm;
const int len = tests[i].digest_length;
snprintf(buf, sizeof(buf), fmt, algo + 0x30, len * 2, hex);
code = parse_digest(buf);
assert_int_equal(code, tests[i].code);
snprintf(buf, sizeof(buf), hex_fmt, 4 + len, algo + 0x30, len * 2, hex);
code = parse_digest(buf);
assert_int_equal(code, tests[i].code);
}
}
/*!cmocka */
void zonemd_digest_lengths(void **state)
{
static const char fmt[] =
"example.com. 86400 IN ZONEMD 2018031500 1 %c ( %.*s )";
static const char hex_fmt[] =
"example.com. 86400 CLASS1 TYPE63 \\# %d 7848B78C 01 0%c ( %.*s )";
static const char hex[] =
"0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef";
static const struct {
int algorithm;
int digest_length;
int32_t code;
} tests[] = {
// 0: Reserved
{ 0, 10, ZONE_SUCCESS },
// 1: SHA-384
{ 1, 48, ZONE_SUCCESS },
{ 1, 47, ZONE_SEMANTIC_ERROR },
{ 1, 49, ZONE_SEMANTIC_ERROR },
// 2: SHA-512
{ 2, 64, ZONE_SUCCESS },
{ 2, 63, ZONE_SEMANTIC_ERROR },
{ 2, 65, ZONE_SEMANTIC_ERROR }
};
(void)state;
int32_t code;
for (size_t i=0, n = sizeof(tests)/sizeof(tests[0]); i < n; i++) {
char buf[512];
const int algo = tests[i].algorithm;
const int len = tests[i].digest_length;
snprintf(buf, sizeof(buf), fmt, algo + 0x30, len * 2, hex);
code = parse_digest(buf);
assert_int_equal(code, tests[i].code);
snprintf(buf, sizeof(buf), hex_fmt, 6 + len, algo + 0x30, len * 2, hex);
code = parse_digest(buf);
assert_int_equal(code, tests[i].code);
}
}