File: check_concatkdf.c

package info (click to toggle)
cjose 0.6.1%2Bdfsg1-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,456 kB
  • sloc: ansic: 8,734; sh: 4,181; makefile: 95
file content (235 lines) | stat: -rw-r--r-- 7,496 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*!
 *
 */

#include "check_cjose.h"

#include "include/concatkdf_int.h"

#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <arpa/inet.h>
#include <cjose/base64.h>
#include <cjose/error.h>
#include <cjose/header.h>
#include <cjose/util.h>

#include <stdio.h>

static cjose_header_t *_create_otherinfo_header(const uint8_t *apu, const size_t apuLen, const uint8_t *apv, const size_t apvLen, cjose_err *err)
{
    cjose_header_t *result = NULL;
    cjose_header_t *hdr = NULL;
    char *apuB64 = NULL;
    size_t apuB64len = 0;
    char *apvB64 = NULL;
    size_t apvB64len = 0;

    memset(err, 0, sizeof(cjose_err));
    if (NULL != apu && !cjose_base64url_encode(apu, apuLen, &apuB64, &apuB64len, err))
    {
        goto _create_otherinfo_header_finish;
    }
    if (NULL != apv && !cjose_base64url_encode(apv, apvLen, &apvB64, &apvB64len, err))
    {
        goto _create_otherinfo_header_finish;
    }

    hdr = cjose_header_new(err);
    if (NULL == hdr)
    {
        goto _create_otherinfo_header_finish;
    }
    if (!(NULL == apuB64 || cjose_header_set(hdr, CJOSE_HDR_APU, apuB64, err)) ||
        !(NULL == apvB64 || cjose_header_set(hdr, CJOSE_HDR_APV, apvB64, err)))
    {
        goto _create_otherinfo_header_finish;
    }
    result = hdr;
    hdr = NULL;

_create_otherinfo_header_finish:
    cjose_get_dealloc()(apuB64);
    cjose_get_dealloc()(apvB64);
    cjose_header_release(hdr);
    ck_assert(err->code == CJOSE_ERR_NONE);

    return result;
}

static bool _cmp_uint32(uint8_t **actual, uint32_t expected)
{
    uint32_t big_endian_int32 = htonl(expected);

    bool result = (0 == memcmp(*actual, &big_endian_int32, 4));
    (*actual) += 4;
    return result;
}
static bool _cmp_lendata(uint8_t **actual, uint8_t *expected, size_t len)
{
    bool result = _cmp_uint32(actual, len);
    if (result && NULL != expected)
    {
        result = (0 == memcmp(*actual, expected, len));
    }
    (*actual) += len;
    return result;
}

START_TEST(test_cjose_concatkdf_otherinfo_noextra)
{
    cjose_err err;

    cjose_header_t *hdr = cjose_header_new(&err);
    uint8_t *otherinfo = NULL;
    size_t otherinfoLen = 0;
    uint8_t *actual = NULL;

    char *alg = "A256GCM";
    memset(&err, 0, sizeof(cjose_err));
    ck_assert(cjose_concatkdf_create_otherinfo(alg, 256, hdr, &otherinfo, &otherinfoLen, &err));
    actual = otherinfo;
    ck_assert_uint_eq(otherinfoLen, 23);
    ck_assert(_cmp_lendata(&actual, alg, strlen(alg))); // ALG
    ck_assert(_cmp_lendata(&actual, NULL, 0));          // APU
    ck_assert(_cmp_lendata(&actual, NULL, 0));          // APV
    ck_assert(_cmp_uint32(&actual, 256));               // KEYLEN
}
END_TEST

START_TEST(test_cjose_concatkdf_otherinfo_apuapv)
{
    cjose_err err;

    const uint8_t *apu = "expected apu";
    const size_t apuLen = strlen((const char *)apu);
    const uint8_t *apv = "expected apv";
    const size_t apvLen = strlen((const char *)apv);
    cjose_header_t *hdr = _create_otherinfo_header(apu, apuLen, apv, apvLen, &err);
    uint8_t *otherinfo = NULL;
    size_t otherinfoLen = 0;
    uint8_t *actual = NULL;

    char *alg = "A256GCM";
    memset(&err, 0, sizeof(cjose_err));
    ck_assert(cjose_concatkdf_create_otherinfo(alg, 32, hdr, &otherinfo, &otherinfoLen, &err));
    actual = otherinfo;
    ck_assert_uint_eq(otherinfoLen, 47);
    ck_assert(_cmp_lendata(&actual, alg, strlen(alg)));
    ck_assert(_cmp_lendata(&actual, apu, apuLen));
    ck_assert(_cmp_lendata(&actual, apv, apvLen));
    ck_assert(_cmp_uint32(&actual, 32));
}
END_TEST

START_TEST(test_cjose_concatkdf_derive_simple)
{
    cjose_err err;
    uint8_t *otherinfo = NULL;
    size_t otherinfoLen = 0;
    uint8_t *derived = NULL;

    const size_t ikmLen = 32;
    uint8_t ikm[] = {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };

    const char *alg
        = "A256GCM";
    const size_t keylen = 32;
    cjose_concatkdf_create_otherinfo(alg, keylen, cjose_header_new(&err), &otherinfo, &otherinfoLen, &err);
    derived = cjose_concatkdf_derive(keylen, ikm, ikmLen, otherinfo, otherinfoLen, &err);
    ck_assert(NULL != derived);

    uint8_t expected[] = {
        0xef, 0x1e, 0xe5, 0x58, 0xb7, 0xa8, 0x60, 0x06,
        0xe1, 0x6b, 0x26, 0x92, 0x5d, 0x14, 0xcc, 0x1b,
        0xa3, 0xbb, 0x4e, 0xcf, 0x0d, 0xf0, 0xb0, 0x49,
        0xaa, 0xc0, 0x3c, 0xef, 0x87, 0x34, 0xbd, 0x20
    };
    ck_assert_bin_eq(derived, expected, keylen);
}
END_TEST

START_TEST(test_cjose_concatkdf_derive_ikm)
{
    cjose_err err;
    uint8_t *otherinfo = NULL;
    size_t otherinfoLen = 0;
    uint8_t *derived = NULL;

    const size_t ikmLen = 32;
    uint8_t ikm[] = {
        0x86, 0x6f, 0x6f, 0xbb, 0x00, 0xf1, 0x7e, 0x2a,
        0x35, 0x34, 0x03, 0x6c, 0x10, 0x24, 0xe1, 0x3c,
        0x5f, 0x9f, 0x3e, 0x32, 0xa0, 0x43, 0xfe, 0x90,
        0x3c, 0x4b, 0x94, 0xf1, 0x62, 0xcc, 0xcd, 0x20
    };

    const char *alg = "A256GCM";
    const size_t keylen = 32;
    cjose_concatkdf_create_otherinfo(alg, keylen, cjose_header_new(&err), &otherinfo, &otherinfoLen, &err);
    derived = cjose_concatkdf_derive(keylen, ikm, ikmLen, otherinfo, otherinfoLen, &err);
    ck_assert(NULL != derived);

    uint8_t expected[] = {
        0x34, 0x85, 0xb0, 0x65, 0x0a, 0xa0, 0x95, 0xcc,
        0xd1, 0xc4, 0xd2, 0x5f, 0x97, 0x23, 0x50, 0x63,
        0x53, 0x77, 0xef, 0x05, 0xaa, 0x22, 0x82, 0x3d,
        0x6a, 0x23, 0x12, 0x39, 0xd2, 0x33, 0x6e, 0x44
    };
    ck_assert_bin_eq(derived, expected, keylen);
}
END_TEST

START_TEST(test_cjose_concatkdf_derive_moreinfo)
{
    cjose_err err;
    uint8_t *otherinfo = NULL;
    size_t otherinfoLen = 0;
    uint8_t *derived = NULL;

    const size_t ikmLen = 32;
    uint8_t ikm[] = {
        0x86, 0x6f, 0x6f, 0xbb, 0x00, 0xf1, 0x7e, 0x2a,
        0x35, 0x34, 0x03, 0x6c, 0x10, 0x24, 0xe1, 0x3c,
        0x5f, 0x9f, 0x3e, 0x32, 0xa0, 0x43, 0xfe, 0x90,
        0x3c, 0x4b, 0x94, 0xf1, 0x62, 0xcc, 0xcd, 0x20
    };

    const char *alg = "A256GCM";
    const size_t keylen = 32;
    cjose_header_t *hdr = _create_otherinfo_header("expected apu", strlen("expected apu"),
                                                   "expected apv", strlen("expected apv"),
                                                   &err);
    cjose_concatkdf_create_otherinfo(alg, keylen, hdr, &otherinfo, &otherinfoLen, &err);
    derived = cjose_concatkdf_derive(keylen, ikm, ikmLen, otherinfo, otherinfoLen, &err);
    ck_assert(NULL != derived);

    uint8_t expected[] = {
        0x2f, 0xc0, 0x7b, 0x68, 0x8d, 0x15, 0x1e, 0x30,
        0x1e, 0xf7, 0xb8, 0x3b, 0xf3, 0x46, 0x7a, 0xf0,
        0x0e, 0x94, 0xac, 0xfc, 0x18, 0xb5, 0xb4, 0xae,
        0x53, 0x81, 0xe0, 0x4a, 0x57, 0x1b, 0x58, 0x65
    };
    ck_assert_bin_eq(derived, expected, keylen);
}
END_TEST
Suite *cjose_concatkdf_suite()
{
    Suite *suite = suite_create("concatkdf");

    TCase *tc_concatkdf = tcase_create("concatkdf");
    tcase_add_test(tc_concatkdf, test_cjose_concatkdf_otherinfo_noextra);
    tcase_add_test(tc_concatkdf, test_cjose_concatkdf_otherinfo_apuapv);
    tcase_add_test(tc_concatkdf, test_cjose_concatkdf_derive_simple);
    tcase_add_test(tc_concatkdf, test_cjose_concatkdf_derive_ikm);
    tcase_add_test(tc_concatkdf, test_cjose_concatkdf_derive_moreinfo);
    suite_add_tcase(suite, tc_concatkdf);

    return suite;
}