Path: blob/main/tests/sys/opencrypto/poly1305_test.c
39534 views
/*-1* Copyright (c) 2018 Conrad Meyer <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/param.h>2728#include <errno.h>29#include <fcntl.h>30#include <string.h>3132#include <atf-c.h>3334/* Be sure to include tree copy rather than system copy. */35#include "cryptodev.h"3637#include "freebsd_test_suite/macros.h"3839struct poly1305_kat {40const char *vector_name;41const char *test_key_hex;42const char *test_msg_hex;43const size_t test_msg_len;4445const char *expected_tag_hex;46};4748static const struct poly1305_kat rfc7539_kats[] = {49{50.vector_name = "RFC 7539 \xc2\xa7 2.5.2",51.test_key_hex = "85:d6:be:78:57:55:6d:33:7f:44:52:fe:42:d5:06:a8"52":01:03:80:8a:fb:0d:b2:fd:4a:bf:f6:af:41:49:f5:1b",53.test_msg_hex =54"43 72 79 70 74 6f 67 72 61 70 68 69 63 20 46 6f "55"72 75 6d 20 52 65 73 65 61 72 63 68 20 47 72 6f "56"75 70",57.test_msg_len = 34,58.expected_tag_hex = "a8:06:1d:c1:30:51:36:c6:c2:2b:8b:af:0c:01:27:a9",59},60{61.vector_name = "RFC 7539 \xc2\xa7 A.3 #1",62.test_key_hex =63"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "64"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ",65.test_msg_hex =66"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "67"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "68"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "69"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ",70.test_msg_len = 64,71.expected_tag_hex = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",72},73{74.vector_name = "RFC 7539 \xc2\xa7 A.3 #2",75.test_key_hex =76"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "77"36 e5 f6 b5 c5 e0 60 70 f0 ef ca 96 22 7a 86 3e ",78.test_msg_hex =79"41 6e 79 20 73 75 62 6d 69 73 73 69 6f 6e 20 74 "80"6f 20 74 68 65 20 49 45 54 46 20 69 6e 74 65 6e "81"64 65 64 20 62 79 20 74 68 65 20 43 6f 6e 74 72 "82"69 62 75 74 6f 72 20 66 6f 72 20 70 75 62 6c 69 "83"63 61 74 69 6f 6e 20 61 73 20 61 6c 6c 20 6f 72 "84"20 70 61 72 74 20 6f 66 20 61 6e 20 49 45 54 46 "85"20 49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 20 "86"6f 72 20 52 46 43 20 61 6e 64 20 61 6e 79 20 73 "87"74 61 74 65 6d 65 6e 74 20 6d 61 64 65 20 77 69 "88"74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 "89"20 6f 66 20 61 6e 20 49 45 54 46 20 61 63 74 69 "90"76 69 74 79 20 69 73 20 63 6f 6e 73 69 64 65 72 "91"65 64 20 61 6e 20 22 49 45 54 46 20 43 6f 6e 74 "92"72 69 62 75 74 69 6f 6e 22 2e 20 53 75 63 68 20 "93"73 74 61 74 65 6d 65 6e 74 73 20 69 6e 63 6c 75 "94"64 65 20 6f 72 61 6c 20 73 74 61 74 65 6d 65 6e "95"74 73 20 69 6e 20 49 45 54 46 20 73 65 73 73 69 "96"6f 6e 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 "97"77 72 69 74 74 65 6e 20 61 6e 64 20 65 6c 65 63 "98"74 72 6f 6e 69 63 20 63 6f 6d 6d 75 6e 69 63 61 "99"74 69 6f 6e 73 20 6d 61 64 65 20 61 74 20 61 6e "100"79 20 74 69 6d 65 20 6f 72 20 70 6c 61 63 65 2c "101"20 77 68 69 63 68 20 61 72 65 20 61 64 64 72 65 "102"73 73 65 64 20 74 6f",103.test_msg_len = 375,104.expected_tag_hex = "36 e5 f6 b5 c5 e0 60 70 f0 ef ca 96 22 7a 86 3e",105},106{107.vector_name = "RFC 7539 \xc2\xa7 A.3 #3",108.test_key_hex =109"36 e5 f6 b5 c5 e0 60 70 f0 ef ca 96 22 7a 86 3e "110"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ",111.test_msg_hex =112"41 6e 79 20 73 75 62 6d 69 73 73 69 6f 6e 20 74 "113"6f 20 74 68 65 20 49 45 54 46 20 69 6e 74 65 6e "114"64 65 64 20 62 79 20 74 68 65 20 43 6f 6e 74 72 "115"69 62 75 74 6f 72 20 66 6f 72 20 70 75 62 6c 69 "116"63 61 74 69 6f 6e 20 61 73 20 61 6c 6c 20 6f 72 "117"20 70 61 72 74 20 6f 66 20 61 6e 20 49 45 54 46 "118"20 49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 20 "119"6f 72 20 52 46 43 20 61 6e 64 20 61 6e 79 20 73 "120"74 61 74 65 6d 65 6e 74 20 6d 61 64 65 20 77 69 "121"74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 "122"20 6f 66 20 61 6e 20 49 45 54 46 20 61 63 74 69 "123"76 69 74 79 20 69 73 20 63 6f 6e 73 69 64 65 72 "124"65 64 20 61 6e 20 22 49 45 54 46 20 43 6f 6e 74 "125"72 69 62 75 74 69 6f 6e 22 2e 20 53 75 63 68 20 "126"73 74 61 74 65 6d 65 6e 74 73 20 69 6e 63 6c 75 "127"64 65 20 6f 72 61 6c 20 73 74 61 74 65 6d 65 6e "128"74 73 20 69 6e 20 49 45 54 46 20 73 65 73 73 69 "129"6f 6e 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 "130"77 72 69 74 74 65 6e 20 61 6e 64 20 65 6c 65 63 "131"74 72 6f 6e 69 63 20 63 6f 6d 6d 75 6e 69 63 61 "132"74 69 6f 6e 73 20 6d 61 64 65 20 61 74 20 61 6e "133"79 20 74 69 6d 65 20 6f 72 20 70 6c 61 63 65 2c "134"20 77 68 69 63 68 20 61 72 65 20 61 64 64 72 65 "135"73 73 65 64 20 74 6f",136.test_msg_len = 375,137.expected_tag_hex = "f3 47 7e 7c d9 54 17 af 89 a6 b8 79 4c 31 0c f0",138},139{140.vector_name = "RFC 7539 \xc2\xa7 A.3 #4",141.test_key_hex =142"1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0 "143"47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0 ",144.test_msg_hex =145"27 54 77 61 73 20 62 72 69 6c 6c 69 67 2c 20 61 "146"6e 64 20 74 68 65 20 73 6c 69 74 68 79 20 74 6f "147"76 65 73 0a 44 69 64 20 67 79 72 65 20 61 6e 64 "148"20 67 69 6d 62 6c 65 20 69 6e 20 74 68 65 20 77 "149"61 62 65 3a 0a 41 6c 6c 20 6d 69 6d 73 79 20 77 "150"65 72 65 20 74 68 65 20 62 6f 72 6f 67 6f 76 65 "151"73 2c 0a 41 6e 64 20 74 68 65 20 6d 6f 6d 65 20 "152"72 61 74 68 73 20 6f 75 74 67 72 61 62 65 2e",153.test_msg_len = 127,154.expected_tag_hex = "45 41 66 9a 7e aa ee 61 e7 08 dc 7c bc c5 eb 62",155},156{157.vector_name = "RFC 7539 \xc2\xa7 A.3 #5",158.test_key_hex =159"02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "160"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ",161.test_msg_hex =162"FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",163.test_msg_len = 16,164.expected_tag_hex = "03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",165},166{167.vector_name = "RFC 7539 \xc2\xa7 A.3 #6",168.test_key_hex =169"02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "170"FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ",171.test_msg_hex =172"02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",173.test_msg_len = 16,174.expected_tag_hex = "03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",175176},177{178.vector_name = "RFC 7539 \xc2\xa7 A.3 #7",179.test_key_hex =180"01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "181"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ",182.test_msg_hex =183"FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF "184"F0 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF "185"11 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",186.test_msg_len = 48,187.expected_tag_hex = "05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",188},189{190.vector_name = "RFC 7539 \xc2\xa7 A.3 #8",191.test_key_hex =192"01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "193"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ",194.test_msg_hex =195"FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF "196"FB FE FE FE FE FE FE FE FE FE FE FE FE FE FE FE "197"01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01",198.test_msg_len = 48,199.expected_tag_hex = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",200},201{202.vector_name = "RFC 7539 \xc2\xa7 A.3 #9",203.test_key_hex =204"02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "205"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ",206.test_msg_hex =207"FD FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",208.test_msg_len = 16,209.expected_tag_hex = "FA FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",210},211{212.vector_name = "RFC 7539 \xc2\xa7 A.3 #10",213.test_key_hex =214"01 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 "215"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ",216.test_msg_hex =217"E3 35 94 D7 50 5E 43 B9 00 00 00 00 00 00 00 00 "218"33 94 D7 50 5E 43 79 CD 01 00 00 00 00 00 00 00 "219"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "220"01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",221.test_msg_len = 64,222.expected_tag_hex = "14 00 00 00 00 00 00 00 55 00 00 00 00 00 00 00",223},224{225.vector_name = "RFC 7539 \xc2\xa7 A.3 #11",226.test_key_hex =227"01 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 "228"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ",229.test_msg_hex =230"E3 35 94 D7 50 5E 43 B9 00 00 00 00 00 00 00 00 "231"33 94 D7 50 5E 43 79 CD 01 00 00 00 00 00 00 00 "232"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",233.test_msg_len = 48,234.expected_tag_hex = "13 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",235},236};237238static void239parse_hex(const struct poly1305_kat *kat, const char *hexstr, void *voutput,240size_t explen)241{242/* Space or colon delimited; may contain a single trailing space;243* length should match exactly.244*/245const char *sep, *it;246size_t sym_len, count;247char hbyte[3], *out;248int res;249250out = voutput;251memset(hbyte, 0, sizeof(hbyte));252253it = hexstr;254count = 0;255while (true) {256sep = strpbrk(it, " :");257if (sep == NULL)258sym_len = strlen(it);259else260sym_len = sep - it;261262ATF_REQUIRE_EQ_MSG(sym_len, 2,263"invalid hex byte '%.*s' in vector %s", (int)sym_len, it,264kat->vector_name);265266memcpy(hbyte, it, 2);267res = sscanf(hbyte, "%hhx", &out[count]);268ATF_REQUIRE_EQ_MSG(res, 1,269"invalid hex byte '%s' in vector %s", hbyte,270kat->vector_name);271272count++;273ATF_REQUIRE_MSG(count <= explen,274"got longer than expected value at %s", kat->vector_name);275276if (sep == NULL)277break;278it = sep;279while (*it == ' ' || *it == ':')280it++;281if (*it == 0)282break;283}284285ATF_REQUIRE_EQ_MSG(count, explen, "got short value at %s",286kat->vector_name);287}288289static void290parse_vector(const struct poly1305_kat *kat,291uint8_t key[__min_size(POLY1305_KEY_LEN)], char *msg,292uint8_t exptag[__min_size(POLY1305_HASH_LEN)])293{294parse_hex(kat, kat->test_key_hex, key, POLY1305_KEY_LEN);295parse_hex(kat, kat->test_msg_hex, msg, kat->test_msg_len);296parse_hex(kat, kat->expected_tag_hex, exptag, POLY1305_HASH_LEN);297}298299static int300get_handle_fd(void)301{302int fd;303304fd = open("/dev/crypto", O_RDWR);305ATF_REQUIRE(fd >= 0);306return (fd);307}308309static int310create_session(int fd, int alg, int crid, const void *key, size_t klen)311{312struct session2_op sop;313314memset(&sop, 0, sizeof(sop));315316sop.mac = alg;317sop.mackey = key;318sop.mackeylen = klen;319sop.crid = crid;320321ATF_REQUIRE_MSG(ioctl(fd, CIOCGSESSION2, &sop) >= 0,322"alg %d keylen %zu, errno=%d (%s)", alg, klen, errno,323strerror(errno));324return (sop.ses);325}326327static void328destroy_session(int fd, int _ses)329{330uint32_t ses;331332ses = _ses;333ATF_REQUIRE_MSG(ioctl(fd, CIOCFSESSION, &ses) >= 0,334"destroy session failed, errno=%d (%s)", errno, strerror(errno));335}336337static void338do_cryptop(int fd, int ses, const void *inp, size_t inlen, void *out)339{340struct crypt_op cop;341342memset(&cop, 0, sizeof(cop));343344cop.ses = ses;345cop.len = inlen;346cop.src = inp;347cop.mac = out;348ATF_CHECK_MSG(ioctl(fd, CIOCCRYPT, &cop) >= 0, "ioctl(CIOCCRYPT)");349}350351static void352test_rfc7539_poly1305_vectors(int crid)353{354uint8_t comptag[POLY1305_HASH_LEN], exptag[POLY1305_HASH_LEN],355key[POLY1305_KEY_LEN], msg[512];356int fd, ses;357size_t i;358359fd = get_handle_fd();360361for (i = 0; i < nitems(rfc7539_kats); i++) {362const struct poly1305_kat *kat;363364kat = &rfc7539_kats[i];365parse_vector(kat, key, msg, exptag);366367ses = create_session(fd, CRYPTO_POLY1305, crid, key, sizeof(key));368369do_cryptop(fd, ses, msg, kat->test_msg_len, comptag);370ATF_CHECK_EQ_MSG(memcmp(comptag, exptag, sizeof(exptag)), 0,371"KAT %s failed:", kat->vector_name);372373destroy_session(fd, ses);374}375}376377ATF_TC(poly1305_vectors);378ATF_TC_HEAD(poly1305_vectors, tc)379{380atf_tc_set_md_var(tc, "require.kmods", "nexus/cryptosoft cryptodev");381}382ATF_TC_BODY(poly1305_vectors, tc)383{384ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);385test_rfc7539_poly1305_vectors(CRYPTO_FLAG_SOFTWARE);386}387388ATF_TP_ADD_TCS(tp)389{390391ATF_TP_ADD_TC(tp, poly1305_vectors);392393return (atf_no_error());394}395396397