Annotation of embedaddon/strongswan/src/libipsec/tests/suites/test_chapoly.c, revision 1.1.1.1

1.1       misho       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 <ip_packet.h>
                     19: #include <esp_packet.h>
                     20: 
                     21: static iv_gen_t *ivgen;
                     22: 
                     23: METHOD(aead_t, get_iv_gen, iv_gen_t*,
                     24:        aead_t *this)
                     25: {
                     26:        return ivgen;
                     27: }
                     28: 
                     29: METHOD(iv_gen_t, get_iv, bool,
                     30:        iv_gen_t *this, uint64_t seq, size_t size, uint8_t *buffer)
                     31: {
                     32:        if (size != 8)
                     33:        {
                     34:                return FALSE;
                     35:        }
                     36:        memcpy(buffer, "\x10\x11\x12\x13\x14\x15\x16\x17", 8);
                     37:        return TRUE;
                     38: }
                     39: 
                     40: METHOD(iv_gen_t, allocate_iv, bool,
                     41:        iv_gen_t *this, uint64_t seq, size_t size, chunk_t *chunk)
                     42: {
                     43:        if (size != 8)
                     44:        {
                     45:                return FALSE;
                     46:        }
                     47:        *chunk = chunk_alloc(size);
                     48:        return get_iv(this, seq, chunk->len, chunk->ptr);
                     49: }
                     50: 
                     51: /**
                     52:  * Appendix A draft-ietf-ipsecme-chacha20-poly1305-06
                     53:  */
                     54: START_TEST(test_chapoly)
                     55: {
                     56:        host_t *src, *dst;
                     57:        ip_packet_t *icmp;
                     58:        esp_packet_t *esp;
                     59:        esp_context_t *ctx;
                     60:        chunk_t data, exp;
                     61:        uint32_t seq = 0;
                     62: 
                     63:        icmp = ip_packet_create(chunk_clone(chunk_from_chars(
                     64:                                                                0x45,0x00,0x00,0x54,0xa6,0xf2,0x00,0x00,
                     65:                                                                0x40,0x01,0xe7,0x78,0xc6,0x33,0x64,0x05,
                     66:                                                                0xc0,0x00,0x02,0x05,0x08,0x00,0x5b,0x7a,
                     67:                                                                0x3a,0x08,0x00,0x00,0x55,0x3b,0xec,0x10,
                     68:                                                                0x00,0x07,0x36,0x27,0x08,0x09,0x0a,0x0b,
                     69:                                                                0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,
                     70:                                                                0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,
                     71:                                                                0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,
                     72:                                                                0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,
                     73:                                                                0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,
                     74:                                                                0x34,0x35,0x36,0x37)));
                     75:        ck_assert(icmp);
                     76: 
                     77:        src = host_create_from_string("203.0.113.153", 0);
                     78:        dst = host_create_from_string("203.0.113.5", 0);
                     79:        esp = esp_packet_create_from_payload(src, dst, icmp);
                     80: 
                     81:        ctx = esp_context_create(ENCR_CHACHA20_POLY1305, chunk_from_chars(
                     82:                                                                0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
                     83:                                                                0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
                     84:                                                                0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
                     85:                                                                0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
                     86:                                                                0xa0,0xa1,0xa2,0xa3),
                     87:                                                         AUTH_UNDEFINED, chunk_empty, FALSE);
                     88:        while (seq != 4)
                     89:        {
                     90:                ck_assert(ctx->next_seqno(ctx, &seq));
                     91:        }
                     92:        INIT(ivgen,
                     93:                .get_iv = _get_iv,
                     94:                .allocate_iv = _allocate_iv,
                     95:                .destroy = (void*)free,
                     96:        );
                     97:        ctx->get_aead(ctx)->get_iv_gen = _get_iv_gen;
                     98:        ck_assert(esp->encrypt(esp, ctx, htonl(0x01020304)) == SUCCESS);
                     99: 
                    100:        data = esp->packet.get_data(&esp->packet);
                    101:        exp = chunk_from_chars(0x01,0x02,0x03,0x04,0x00,0x00,0x00,0x05,
                    102:                                                   0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
                    103:                                                   0x24,0x03,0x94,0x28,0xb9,0x7f,0x41,0x7e,
                    104:                                                   0x3c,0x13,0x75,0x3a,0x4f,0x05,0x08,0x7b,
                    105:                                                   0x67,0xc3,0x52,0xe6,0xa7,0xfa,0xb1,0xb9,
                    106:                                                   0x82,0xd4,0x66,0xef,0x40,0x7a,0xe5,0xc6,
                    107:                                                   0x14,0xee,0x80,0x99,0xd5,0x28,0x44,0xeb,
                    108:                                                   0x61,0xaa,0x95,0xdf,0xab,0x4c,0x02,0xf7,
                    109:                                                   0x2a,0xa7,0x1e,0x7c,0x4c,0x4f,0x64,0xc9,
                    110:                                                   0xbe,0xfe,0x2f,0xac,0xc6,0x38,0xe8,0xf3,
                    111:                                                   0xcb,0xec,0x16,0x3f,0xac,0x46,0x9b,0x50,
                    112:                                                   0x27,0x73,0xf6,0xfb,0x94,0xe6,0x64,0xda,
                    113:                                                   0x91,0x65,0xb8,0x28,0x29,0xf6,0x41,0xe0,
                    114:                                                   0x76,0xAA,0xA8,0x26,0x6B,0x7F,0xB0,0xF7,
                    115:                                                   0xB1,0x1B,0x36,0x99,0x07,0xE1,0xAD,0x43);
                    116:        ck_assert_msg(chunk_equals(data, exp), "got %B\nexp %B", &data, &exp);
                    117: 
                    118:        esp->destroy(esp);
                    119:        ctx->destroy(ctx);
                    120:        ivgen->destroy(ivgen);
                    121: }
                    122: END_TEST
                    123: 
                    124: Suite *chapoly_suite_create()
                    125: {
                    126:        Suite *s;
                    127:        TCase *tc;
                    128: 
                    129:        s = suite_create("chapoly");
                    130: 
                    131:        tc = tcase_create("ChaCha20Poly1305 ESP encryption");
                    132:        tcase_add_test(tc, test_chapoly);
                    133:        suite_add_tcase(s, tc);
                    134: 
                    135:        return s;
                    136: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>