Path: blob/main/tests/sys/opencrypto/blake2_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/*27* Derived from blake2b-test.c and blake2s-test.c:28*29* BLAKE2 reference source code package - optimized C implementations30*31* Written in 2012 by Samuel Neves <[email protected]>32*33* To the extent possible under law, the author(s) have dedicated all copyright34* and related and neighboring rights to this software to the public domain35* worldwide. This software is distributed without any warranty.36*37* You should have received a copy of the CC0 Public Domain Dedication along with38* this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.39*/4041#include <sys/param.h>4243#include <errno.h>44#include <fcntl.h>45#include <string.h>4647#include <atf-c.h>4849/* Be sure to include tree copy rather than system copy. */50#include "cryptodev.h"5152#include "freebsd_test_suite/macros.h"5354#include <blake2.h>55#include "blake2-kat.h"5657static uint8_t key2b[BLAKE2B_KEYBYTES];58static uint8_t key2s[BLAKE2S_KEYBYTES];59static uint8_t katbuf[KAT_LENGTH];6061static void62initialize_constant_buffers(void)63{64size_t i;6566for (i = 0; i < sizeof(key2b); i++)67key2b[i] = (uint8_t)i;68for (i = 0; i < sizeof(key2s); i++)69key2s[i] = (uint8_t)i;70for (i = 0; i < sizeof(katbuf); i++)71katbuf[i] = (uint8_t)i;72}7374static int75lookup_crid(int fd, const char *devname)76{77struct crypt_find_op find;7879find.crid = -1;80strlcpy(find.name, devname, sizeof(find.name));81ATF_REQUIRE(ioctl(fd, CIOCFINDDEV, &find) != -1);82return (find.crid);83}8485static int86get_handle_fd(void)87{88int fd;8990fd = open("/dev/crypto", O_RDWR);91ATF_REQUIRE(fd >= 0);92return (fd);93}9495static int96create_session(int fd, int alg, int crid, const void *key, size_t klen)97{98struct session2_op sop;99100memset(&sop, 0, sizeof(sop));101102sop.mac = alg;103sop.mackey = key;104sop.mackeylen = klen;105sop.crid = crid;106107ATF_REQUIRE_MSG(ioctl(fd, CIOCGSESSION2, &sop) >= 0,108"alg %d keylen %zu, errno=%d (%s)", alg, klen, errno,109strerror(errno));110return (sop.ses);111}112113static void114do_cryptop(int fd, int ses, size_t inlen, void *out)115{116struct crypt_op cop;117118memset(&cop, 0, sizeof(cop));119120cop.ses = ses;121cop.len = inlen;122cop.src = katbuf;123cop.mac = out;124ATF_CHECK_MSG(ioctl(fd, CIOCCRYPT, &cop) >= 0, "ioctl(CIOCCRYPT)");125}126127static void128test_blake2b_vectors(const char *devname)129{130uint8_t hash[BLAKE2B_OUTBYTES];131int crid, fd, ses;132size_t i;133134initialize_constant_buffers();135fd = get_handle_fd();136crid = lookup_crid(fd, devname);137ses = create_session(fd, CRYPTO_BLAKE2B, crid, key2b, sizeof(key2b));138139for (i = 0; i < sizeof(katbuf); i++) {140do_cryptop(fd, ses, i, hash);141ATF_CHECK_EQ_MSG(142memcmp(hash, blake2b_keyed_kat[i], sizeof(hash)),1430,144"different at %zu", i);145}146}147148static void149test_blake2s_vectors(const char *devname)150{151uint8_t hash[BLAKE2S_OUTBYTES];152int crid, fd, ses;153size_t i;154155initialize_constant_buffers();156fd = get_handle_fd();157crid = lookup_crid(fd, devname);158ses = create_session(fd, CRYPTO_BLAKE2S, crid, key2s, sizeof(key2s));159160for (i = 0; i < sizeof(katbuf); i++) {161do_cryptop(fd, ses, i, hash);162ATF_CHECK_EQ_MSG(163memcmp(hash, blake2s_keyed_kat[i], sizeof(hash)),1640,165"different at %zu", i);166}167}168169ATF_TC(blake2b_vectors);170ATF_TC_HEAD(blake2b_vectors, tc)171{172atf_tc_set_md_var(tc, "require.kmods", "nexus/cryptosoft cryptodev");173}174ATF_TC_BODY(blake2b_vectors, tc)175{176ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);177test_blake2b_vectors("cryptosoft0");178}179180ATF_TC(blake2s_vectors);181ATF_TC_HEAD(blake2s_vectors, tc)182{183atf_tc_set_md_var(tc, "require.kmods", "nexus/cryptosoft cryptodev");184}185ATF_TC_BODY(blake2s_vectors, tc)186{187ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);188test_blake2s_vectors("cryptosoft0");189}190191#if defined(__i386__) || defined(__amd64__)192ATF_TC(blake2b_vectors_x86);193ATF_TC_HEAD(blake2b_vectors_x86, tc)194{195atf_tc_set_md_var(tc, "require.kmods", "nexus/blake2 cryptodev");196}197ATF_TC_BODY(blake2b_vectors_x86, tc)198{199ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);200test_blake2b_vectors("blaketwo0");201}202203ATF_TC(blake2s_vectors_x86);204ATF_TC_HEAD(blake2s_vectors_x86, tc)205{206atf_tc_set_md_var(tc, "require.kmods", "nexus/blake2 cryptodev");207}208ATF_TC_BODY(blake2s_vectors_x86, tc)209{210ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);211test_blake2s_vectors("blaketwo0");212}213#endif214215ATF_TP_ADD_TCS(tp)216{217218ATF_TP_ADD_TC(tp, blake2b_vectors);219ATF_TP_ADD_TC(tp, blake2s_vectors);220#if defined(__i386__) || defined(__amd64__)221ATF_TP_ADD_TC(tp, blake2b_vectors_x86);222ATF_TP_ADD_TC(tp, blake2s_vectors_x86);223#endif224225return (atf_no_error());226}227228229