From nobody Mon Feb 14 02:51:58 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 2F6C219482F4; Mon, 14 Feb 2022 02:51:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Jxpf70rgBz4vRm; Mon, 14 Feb 2022 02:51:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644807119; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=e8LfFRyR12EOhdsg/7HRj8lsLuNxPY9c3055g9HFcQU=; b=raqwEkVBkuW2S+p1rbg0HTvE5H61scrZhDoDaTOs6JBLxHt733UnD5nySnKnFNA2bWY8Aq EwJ53xDvdDNO2KF0XXaASQBagbW7CQBhfQpQ1bcUaTOn0m+0o6xowQBjYTjDqQGqiWyFWu usCvTiWtCFfGM6JGQw98YnsReCL4+E+h27megtB+l3y4B6DNek5Vkj/QLMeSrv3YaXx4M4 Z7gXzkoCuDPDFIcKcP7I5LRSnQN1mr562bsrP4pYXqUJwCX7eRolRqAwJYzHxJ3K039H/A 0dPt3Z6b4WpMA3MeY2WRjD5D3zSBIv20EGYXVflqzXZwGZmh3o/fDBo04gY6DA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id F181A26DF8; Mon, 14 Feb 2022 02:51:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 21E2pwRx078456; Mon, 14 Feb 2022 02:51:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 21E2pwKX078455; Mon, 14 Feb 2022 02:51:58 GMT (envelope-from git) Date: Mon, 14 Feb 2022 02:51:58 GMT Message-Id: <202202140251.21E2pwKX078455@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "David E. O'Brien" Subject: git: d4b15220f5f1 - stable/12 - random(4): Add regression tests for uint128 implementation, Chacha CTR List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: obrien X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: d4b15220f5f13edd9b3159034a93a7ca216fa558 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644807119; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=e8LfFRyR12EOhdsg/7HRj8lsLuNxPY9c3055g9HFcQU=; b=MnFqPaitQPvoq2j1tss1+l67kFyWmKoMw3YevpG20rOj+PnL39fPpLlGJvKvFLaVzg9onG jvIR9CxgK/jZycoo1b0K8sbyb0bnasNKu94gv35wxEyO7xnguARkNStAzVFvyg7p24Ie6R N0RakVVsfqhApqi/oj2M9bJ//NLx+NvRYdCUtomQ609sEfmgaFQbZjkJUzfwzz/zW9SEhP B1wkuuVLpBwRQzIVuzSFe60ww7rw3jVwVQJ3NUkFZSfeT3A3yLzlg8gGjvEurJtL3CdwwV OuY9L3VevXAj4Szoa01n6R98EJyC2y2V7db3NjW6HEvEg17OeYPzatBLvZtWjg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1644807119; a=rsa-sha256; cv=none; b=gxZEmkyWXgNJXnVH1PoL8XcKpPYSBOISXG2mbKMp1Shg3wK4rsk+MjS2ORRzAU6hFu8fVP EdQGePihXSTrgNh6fU2BsskRXvbdfIJ4DC89ebQVZYZNj2sbpwHT9LByXH3K1ONd8cCoEo xxI0XAgnlhDOFdg6lloNjPm22V0psmAzLV/0AQPn5aiGS6v+GzBatf6ObLUSfh5c1BEr5n 22G530j0IHzRptspsQLTJXbEDUrurYQXhuuQ5RtyL9TQfOSnMRC20EIp+83RVd4bSgbK7r 1RGh/YT6dQVi1V6lxt9GyhS3+BQd6RihTAcxKZxzxJ1fhI7ibeDAN5RXFnRsMg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/12 has been updated by obrien: URL: https://cgit.FreeBSD.org/src/commit/?id=d4b15220f5f13edd9b3159034a93a7ca216fa558 commit d4b15220f5f13edd9b3159034a93a7ca216fa558 Author: Conrad Meyer AuthorDate: 2019-06-17 14:59:45 +0000 Commit: David E. O'Brien CommitDate: 2022-02-14 02:07:05 +0000 random(4): Add regression tests for uint128 implementation, Chacha CTR Add some basic regression tests to verify behavior of both uint128 implementations at typical boundary conditions, to run on all architectures. Test uint128 increment behavior of Chacha in keystream mode, as used by 'kern.random.use_chacha20_cipher=1' (r344913) to verify assumptions at edge cases. These assumptions are critical to the safety of using Chacha as a PRF in Fortuna (as implemented). (Chacha's use in arc4random is safe regardless of these tests, as it is limited to far less than 4 billion blocks of output in that API.) (cherry picked from commit 403c041316da859b91c048b5daeea61bc366e92f) --- etc/mtree/BSD.tests.dist | 2 + sys/dev/random/hash.c | 6 +- sys/dev/random/hash.h | 2 +- tests/sys/Makefile | 1 + tests/sys/devrandom/Makefile | 26 +++++ tests/sys/devrandom/uint128_test.c | 225 +++++++++++++++++++++++++++++++++++++ 6 files changed, 260 insertions(+), 2 deletions(-) diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index b25dfe78f0a2..d1a841561b97 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -726,6 +726,8 @@ .. .. .. + devrandom + .. dtrace .. fifo diff --git a/sys/dev/random/hash.c b/sys/dev/random/hash.c index 2f3cb4738bc6..113f76e8a041 100644 --- a/sys/dev/random/hash.c +++ b/sys/dev/random/hash.c @@ -37,12 +37,16 @@ __FBSDID("$FreeBSD$"); #else /* !_KERNEL */ #include #include +#include #include +#include +#include #include #include #include #include -#include "unit_test.h" +#define KASSERT(x, y) assert(x) +#define CTASSERT(x) _Static_assert(x, "CTASSERT " #x) #endif /* _KERNEL */ #define CHACHA_EMBED diff --git a/sys/dev/random/hash.h b/sys/dev/random/hash.h index bcc7035a0e4f..e76eaaaf4ccb 100644 --- a/sys/dev/random/hash.h +++ b/sys/dev/random/hash.h @@ -54,7 +54,7 @@ union randomdev_key { struct chacha_ctx chacha; }; -extern bool fortuna_chachamode; +extern bool random_chachamode; void randomdev_hash_init(struct randomdev_hash *); void randomdev_hash_iterate(struct randomdev_hash *, const void *, size_t); diff --git a/tests/sys/Makefile b/tests/sys/Makefile index efc4f801890f..e95d193f7bf9 100644 --- a/tests/sys/Makefile +++ b/tests/sys/Makefile @@ -10,6 +10,7 @@ TESTS_SUBDIRS+= ${_audit} TESTS_SUBDIRS+= auditpipe TESTS_SUBDIRS+= capsicum TESTS_SUBDIRS+= ${_cddl} +TESTS_SUBDIRS+= devrandom TESTS_SUBDIRS+= fifo TESTS_SUBDIRS+= file TESTS_SUBDIRS+= fs diff --git a/tests/sys/devrandom/Makefile b/tests/sys/devrandom/Makefile new file mode 100644 index 000000000000..db9f9d42a470 --- /dev/null +++ b/tests/sys/devrandom/Makefile @@ -0,0 +1,26 @@ +# $FreeBSD$ + +.include + +SDEVRANDOM= ${SRCTOP}/sys/dev/random +.PATH: ${SDEVRANDOM} + +TESTSDIR= ${TESTSBASE}/sys/devrandom +WARNS?= 6 + +CFLAGS+= -I${SRCTOP}/sys + +ATF_TESTS_C+= uint128_test + +# Test Chacha CTR behavior <-> uint128 +LDADD.uint128_test+= ${SDEVRANDOM}/hash.c +LDFLAGS.uint128_test+= -Wno-unused-parameter + +# hash.c deps: +LIBADD.uint128_test+= md # SHA256 +LDADD.uint128_test+= ${SRCTOP}/sys/crypto/rijndael/rijndael-alg-fst.c +LDADD.uint128_test+= ${SRCTOP}/sys/crypto/rijndael/rijndael-api-fst.c +LDFLAGS.uint128_test+= -Wno-cast-align + + +.include diff --git a/tests/sys/devrandom/uint128_test.c b/tests/sys/devrandom/uint128_test.c new file mode 100644 index 000000000000..0e02b227f668 --- /dev/null +++ b/tests/sys/devrandom/uint128_test.c @@ -0,0 +1,225 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2019 Conrad Meyer + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include + +static void +vec_u32_tole128(uint8_t dst[static 16], const uint32_t src[static 4]) +{ + le32enc(dst, src[0]); + le32enc(&dst[4], src[1]); + le32enc(&dst[8], src[2]); + le32enc(&dst[12], src[3]); +} + +static void +le128_to_vec_u32(uint32_t dst[static 4], const uint8_t src[static 16]) +{ + dst[0] = le32dec(src); + dst[1] = le32dec(&src[4]); + dst[2] = le32dec(&src[8]); + dst[3] = le32dec(&src[12]); +} + +static void +formatu128(char buf[static 52], uint128_t x) +{ + uint8_t le128x[16]; + uint32_t vx[4]; + size_t sz, i; + int rc; + + le128enc(le128x, x); + le128_to_vec_u32(vx, le128x); + + sz = 52; + for (i = 0; i < 4; i++) { + rc = snprintf(buf, sz, "0x%x ", vx[i]); + ATF_REQUIRE(rc > 0 && (size_t)rc < sz); + + buf += rc; + sz -= rc; + } + /* Delete last trailing space */ + buf[-1] = '\0'; +} + +static void +u128_check_equality(uint128_t a, uint128_t b, const char *descr) +{ + char fmtbufa[52], fmtbufb[52]; + + formatu128(fmtbufa, a); + formatu128(fmtbufb, b); + + ATF_CHECK_MSG(uint128_equals(a, b), + "Expected: [%s] != Actual: [%s]: %s", fmtbufa, fmtbufb, descr); +} + +ATF_TC_WITHOUT_HEAD(uint128_inc); +ATF_TC_BODY(uint128_inc, tc) +{ + static const struct u128_inc_tc { + uint32_t input[4]; + uint32_t expected[4]; + const char *descr; + } tests[] = { + { + .input = { 0, 0, 0, 0 }, + .expected = { 1, 0, 0, 0 }, + .descr = "0 -> 1", + }, + { + .input = { 1, 0, 0, 0 }, + .expected = { 2, 0, 0, 0 }, + .descr = "0 -> 2", + }, + { + .input = { 0xff, 0, 0, 0 }, + .expected = { 0x100, 0, 0, 0 }, + .descr = "0xff -> 0x100 (byte carry)", + }, + { + .input = { UINT32_MAX, 0, 0, 0 }, + .expected = { 0, 1, 0, 0 }, + .descr = "2^32 - 1 -> 2^32 (word carry)", + }, + { + .input = { UINT32_MAX, UINT32_MAX, 0, 0 }, + .expected = { 0, 0, 1, 0 }, + .descr = "2^64 - 1 -> 2^64 (u128t_word0 carry)", + }, + { + .input = { UINT32_MAX, UINT32_MAX, UINT32_MAX, 0 }, + .expected = { 0, 0, 0, 1 }, + .descr = "2^96 - 1 -> 2^96 (word carry)", + }, + }; + uint8_t inputle[16], expectedle[16]; + uint128_t a; + size_t i; + + for (i = 0; i < nitems(tests); i++) { + vec_u32_tole128(inputle, tests[i].input); + vec_u32_tole128(expectedle, tests[i].expected); + + a = le128dec(inputle); + uint128_increment(&a); + u128_check_equality(le128dec(expectedle), a, tests[i].descr); + } +} + +/* + * Test assumptions about Chacha incrementing counter in the same way as + * uint128.h + */ +ATF_TC_WITHOUT_HEAD(uint128_chacha_ctr); +ATF_TC_BODY(uint128_chacha_ctr, tc) +{ + static const struct u128_chacha_tc { + uint32_t input[4]; + uint32_t expected[4]; + const char *descr; + } tests[] = { + { + .input = { 0, 0, 0, 0 }, + .expected = { 1, 0, 0, 0 }, + .descr = "Single block", + }, + { + .input = { 1, 0, 0, 0 }, + .expected = { 2, 0, 0, 0 }, + .descr = "0 -> 2", + }, + { + .input = { 0xff, 0, 0, 0 }, + .expected = { 0x100, 0, 0, 0 }, + .descr = "0xff -> 0x100 (byte carry)", + }, + { + .input = { UINT32_MAX, 0, 0, 0 }, + .expected = { 0, 1, 0, 0 }, + .descr = "2^32 - 1 -> 2^32 (word carry)", + }, + { + .input = { UINT32_MAX, UINT32_MAX, 0, 0 }, + .expected = { 0, 0, 1, 0 }, + .descr = "2^64 - 1 -> 2^64 (u128t_word0 carry)", + }, + { + .input = { UINT32_MAX, UINT32_MAX, UINT32_MAX, 0 }, + .expected = { 0, 0, 0, 1 }, + .descr = "2^96 - 1 -> 2^96 (word carry)", + }, + }; + union randomdev_key context; + uint8_t inputle[16], expectedle[16], trash[CHACHA_BLOCKLEN]; + uint8_t notrandomkey[RANDOM_KEYSIZE] = { 0 }; + uint128_t a; + size_t i; + + random_chachamode = true; + randomdev_encrypt_init(&context, notrandomkey); + + for (i = 0; i < nitems(tests); i++) { + vec_u32_tole128(inputle, tests[i].input); + vec_u32_tole128(expectedle, tests[i].expected); + + a = le128dec(inputle); + randomdev_keystream(&context, &a, trash, sizeof(trash) / + RANDOM_BLOCKSIZE); + u128_check_equality(le128dec(expectedle), a, tests[i].descr); + } + +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, uint128_inc); + ATF_TP_ADD_TC(tp, uint128_chacha_ctr); + return (atf_no_error()); +}