kernel-netlink: Support extended table IDs for custom routes
[strongswan.git] / src / libcharon / tests / suites / test_message_chapoly.c
1 /*
2 * Copyright (C) 2015 Martin Willi
3 * Copyright (C) 2015 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "test_suite.h"
17
18 #include <encoding/message.h>
19
20 static aead_t *aead;
21
22 static iv_gen_t *ivgen;
23
24 METHOD(keymat_t, get_version, ike_version_t,
25 keymat_t *this)
26 {
27 return IKEV2;
28 }
29
30 METHOD(keymat_t, get_aead, aead_t*,
31 keymat_t *this, bool in)
32 {
33 return aead;
34 }
35
36 METHOD(aead_t, get_iv_gen, iv_gen_t*,
37 aead_t *this)
38 {
39 return ivgen;
40 }
41
42 METHOD(iv_gen_t, get_iv, bool,
43 iv_gen_t *this, uint64_t seq, size_t size, uint8_t *buffer)
44 {
45 if (size != 8)
46 {
47 return FALSE;
48 }
49 memcpy(buffer, "\x10\x11\x12\x13\x14\x15\x16\x17", 8);
50 return TRUE;
51 }
52
53 METHOD(iv_gen_t, allocate_iv, bool,
54 iv_gen_t *this, uint64_t seq, size_t size, chunk_t *chunk)
55 {
56 if (size != 8)
57 {
58 return FALSE;
59 }
60 *chunk = chunk_alloc(size);
61 return get_iv(this, seq, chunk->len, chunk->ptr);
62 }
63
64 /**
65 * Appendix B draft-ietf-ipsecme-chacha20-poly1305-06
66 */
67 START_TEST(test_chacha20poly1305)
68 {
69 uint64_t spii, spir;
70 ike_sa_id_t *id;
71 message_t *m;
72 uint32_t window = htonl(10);
73 chunk_t chunk, exp;
74 keymat_t keymat = {
75 .get_version = _get_version,
76 .create_dh = (void*)return_null,
77 .create_nonce_gen = (void*)return_null,
78 .get_aead = _get_aead,
79 };
80
81 m = message_create(IKEV2, 0);
82 m->set_exchange_type(m, INFORMATIONAL);
83 htoun64(&spii, 0xc0c1c2c3c4c5c6c7);
84 htoun64(&spir, 0xd0d1d2d3d4d5d6d7);
85 id = ike_sa_id_create(IKEV2, spii, spir, FALSE);
86 m->set_ike_sa_id(m, id);
87 id->destroy(id);
88 m->set_source(m, host_create_from_string("1.2.3.4", 4500));
89 m->set_destination(m, host_create_from_string("4.3.2.1", 4500));
90 m->set_message_id(m, 9);
91 m->add_notify(m, TRUE, SET_WINDOW_SIZE, chunk_from_thing(window));
92
93 aead = lib->crypto->create_aead(lib->crypto, ENCR_CHACHA20_POLY1305, 32, 4);
94 ck_assert(aead);
95 ck_assert(aead->set_key(aead, chunk_from_chars(
96 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
97 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
98 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
99 0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
100 0xa0,0xa1,0xa2,0xa3)));
101 INIT(ivgen,
102 .get_iv = _get_iv,
103 .allocate_iv = _allocate_iv,
104 .destroy = (void*)free,
105 );
106 aead->get_iv_gen = _get_iv_gen,
107
108 ck_assert(m->generate(m, &keymat, NULL) == SUCCESS);
109 chunk = m->get_packet_data(m);
110 exp = chunk_from_chars(0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,
111 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,
112 0x2e,0x20,0x25,0x00,0x00,0x00,0x00,0x09,
113 0x00,0x00,0x00,0x45,0x29,0x00,0x00,0x29,
114 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
115 0x61,0x03,0x94,0x70,0x1f,0x8d,0x01,0x7f,
116 0x7c,0x12,0x92,0x48,0x89,0x6b,0x71,0xbf,
117 0xe2,0x52,0x36,0xef,0xd7,0xcd,0xc6,0x70,
118 0x66,0x90,0x63,0x15,0xb2);
119 ck_assert_msg(chunk_equals(chunk, exp), "got %B\nexp %B", &chunk, &exp);
120 ivgen->destroy(ivgen);
121 aead->destroy(aead);
122 m->destroy(m);
123 }
124 END_TEST
125
126 Suite *message_chapoly_suite_create()
127 {
128 Suite *s;
129 TCase *tc;
130
131 s = suite_create("chapoly");
132
133 tc = tcase_create("ChaCha20Poly1305 IKEv2 encryption");
134 tcase_add_test(tc, test_chacha20poly1305);
135 suite_add_tcase(s, tc);
136
137 return s;
138 }