Annotation of embedaddon/strongswan/src/libstrongswan/tests/suites/test_prf_plus.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (C) 2019 Tobias Brunner
! 3: * HSR Hochschule fuer Technik Rapperswil
! 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 <crypto/prf_plus.h>
! 19:
! 20: static struct {
! 21: chunk_t key;
! 22: chunk_t seed;
! 23: chunk_t iterations[10];
! 24: } counter_data[] = {
! 25: { .key = chunk_from_chars(0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,
! 26: 0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,
! 27: 0x0b,0x0b,0x0b,0x0b),
! 28: .seed = chunk_from_chars(0x48,0x69,0x20,0x54,0x68,0x65,0x72,0x65),
! 29: .iterations = {
! 30: chunk_from_chars(0xb9,0xbd,0xc0),
! 31: chunk_from_chars(0x89,0x88,0xb4,0xc2,0xb7,0x5a),
! 32: chunk_from_chars(0xa9,0x3e,0x59,0x6a,0xc8,0x42,0x05),
! 33: chunk_from_chars(0xfa,0x2d,0xdd,0xe1,0xbf,0x7a,0x25,0x72,
! 34: 0x06,0x7b,0x00,0xe1,0x4b,0x23,0x77,0x32),
! 35: chunk_from_chars(0x83,0x05,0x09,0x98,0x1a,0xd2,0xf9,0x4a),
! 36: chunk_from_chars(0x8c,0x32,0xa4,0x7d,0xaa,0x22,0x55,0xb6),
! 37: chunk_from_chars(0x60,0xc4,0x36,0x34,0x7a,0xe7,0x56,0xa6,
! 38: 0xed,0xc0,0x23,0x47,0x7d,0x80),
! 39: chunk_from_chars(0x95,0x90,0xe6,0x82,0xf6,0x1d,0x9c,0x04,
! 40: 0xb0,0x6b,0x4a,0xd9,0x71,0xa3,0x4c,0x81,
! 41: 0x47,0xfa,0x66,0x79),
! 42: chunk_from_chars(0x2f,0xf1,0x43,0x4b,0x93,0xc7,0x22,0xb3,
! 43: 0x2e,0x12,0xf4,0x88,0x32,0xeb,0xc1,0x5c,
! 44: 0xe2,0x36,0x9c,0xe7,0x1f,0xe9,0xb7,0xb8,
! 45: 0x1e,0x57,0x04,0xc1,0x4d,0x0f,0x52,0x80,
! 46: 0xa6,0xec,0x62,0x6e,0x99,0x2d,0x7a,0x9f),
! 47: },
! 48: },
! 49: };
! 50:
! 51: START_TEST(test_vectors_counter)
! 52: {
! 53: prf_plus_t *prf_plus;
! 54: prf_t *prf;
! 55: chunk_t *iter = counter_data[_i].iterations, out;
! 56:
! 57: prf = lib->crypto->create_prf(lib->crypto, PRF_HMAC_SHA2_256);
! 58: ck_assert(prf->set_key(prf, counter_data[_i].key));
! 59: prf_plus = prf_plus_create(prf, TRUE, counter_data[_i].seed);
! 60: while (iter->ptr)
! 61: {
! 62: ck_assert(prf_plus->allocate_bytes(prf_plus, iter->len, &out));
! 63: ck_assert_chunk_eq(*iter, out);
! 64: chunk_free(&out);
! 65: iter++;
! 66: }
! 67: prf_plus->destroy(prf_plus);
! 68: prf->destroy(prf);
! 69: }
! 70: END_TEST
! 71:
! 72: START_TEST(test_wrap)
! 73: {
! 74: prf_plus_t *prf_plus;
! 75: prf_t *prf;
! 76: u_char buf[32];
! 77: int i;
! 78:
! 79: prf = lib->crypto->create_prf(lib->crypto, PRF_HMAC_SHA2_256);
! 80: ck_assert(prf->set_key(prf, counter_data[0].key));
! 81: prf_plus = prf_plus_create(prf, TRUE, counter_data[0].seed);
! 82: for (i = 1; i < 256; i++)
! 83: {
! 84: ck_assert(prf_plus->get_bytes(prf_plus, sizeof(buf), buf));
! 85: }
! 86: ck_assert(!prf_plus->get_bytes(prf_plus, sizeof(buf), buf));
! 87: prf_plus->destroy(prf_plus);
! 88: prf->destroy(prf);
! 89: }
! 90: END_TEST
! 91:
! 92: static struct {
! 93: chunk_t key;
! 94: chunk_t seed;
! 95: chunk_t iterations[10];
! 96: } classic_data[] = {
! 97: { .key = chunk_from_chars(0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,
! 98: 0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,
! 99: 0x0b,0x0b,0x0b,0x0b),
! 100: .seed = chunk_from_chars(0x48,0x69,0x20,0x54,0x68,0x65,0x72,0x65),
! 101: .iterations = {
! 102: chunk_from_chars(0xb0,0x34,0x4c),
! 103: chunk_from_chars(0x61,0xd8,0xdb,0x38,0x53,0x5c),
! 104: chunk_from_chars(0xa8,0xaf,0xce,0xaf,0x0b,0xf1,0x2b),
! 105: chunk_from_chars(0x88,0x1d,0xc2,0x00,0xc9,0x83,0x3d,0xa7,
! 106: 0x26,0xe9,0x37,0x6c,0x2e,0x32,0xcf,0xf7),
! 107: chunk_from_chars(0xd0,0x9a,0xe2,0x4b,0x3a,0x83,0xff,0xd4),
! 108: chunk_from_chars(0xb1,0xef,0xa5,0x94,0x5c,0xc5,0xed,0x85),
! 109: chunk_from_chars(0xb0,0xb2,0xcc,0x56,0xfc,0xf7,0x5d,0x23,
! 110: 0xa0,0xa3,0x4c,0xa4,0xdb,0xff,),
! 111: chunk_from_chars(0xea,0xfd,0xaa,0x6a,0x3b,0xf4,0x11,0x34,
! 112: 0x24,0xe4,0x50,0x2d,0xf9,0x7a,0x76,0x93,
! 113: 0x24,0xf6,0x11,0x24),
! 114: chunk_from_chars(0x24,0x3b,0x99,0x6e,0x7d,0x0f,0x35,0x99,
! 115: 0x88,0x79,0x73,0x6b,0xdb,0x70,0x65,0x9a,
! 116: 0x6e,0xfa,0xd2,0x39,0x94,0x10,0xe6,0xce,
! 117: 0x80,0x45,0x6e,0xb6,0x07,0x07,0x8f,0xe1,
! 118: 0xc4,0x7c,0x6b,0x5e,0x81,0x65,0x47,0x8a),
! 119: },
! 120: },
! 121: };
! 122:
! 123: START_TEST(test_vectors_classic)
! 124: {
! 125: prf_plus_t *prf_plus;
! 126: prf_t *prf;
! 127: chunk_t *iter = classic_data[_i].iterations, out;
! 128:
! 129: prf = lib->crypto->create_prf(lib->crypto, PRF_HMAC_SHA2_256);
! 130: ck_assert(prf->set_key(prf, classic_data[_i].key));
! 131: prf_plus = prf_plus_create(prf, FALSE, classic_data[_i].seed);
! 132: while (iter->ptr)
! 133: {
! 134: ck_assert(prf_plus->allocate_bytes(prf_plus, iter->len, &out));
! 135: ck_assert_chunk_eq(*iter, out);
! 136: chunk_free(&out);
! 137: iter++;
! 138: }
! 139: prf_plus->destroy(prf_plus);
! 140: prf->destroy(prf);
! 141: }
! 142: END_TEST
! 143:
! 144: Suite *prf_plus_suite_create()
! 145: {
! 146: Suite *s;
! 147: TCase *tc;
! 148:
! 149: s = suite_create("prf_plus");
! 150:
! 151: tc = tcase_create("counter");
! 152: tcase_add_loop_test(tc, test_vectors_counter, 0, countof(counter_data));
! 153: tcase_add_test(tc, test_wrap);
! 154: suite_add_tcase(s, tc);
! 155:
! 156: tc = tcase_create("no counter");
! 157: tcase_add_loop_test(tc, test_vectors_classic, 0, countof(classic_data));
! 158: suite_add_tcase(s, tc);
! 159:
! 160: return s;
! 161: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>