From nobody Sat Feb 12 22:58:34 2022 X-Original-To: dev-commits-src-all@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 C607119BE5E8; Sat, 12 Feb 2022 22:58:34 +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 4Jx5WG31y7z4Rg9; Sat, 12 Feb 2022 22:58:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644706714; 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=ntSwIJ+6nurILUoyijEcHivrv/TftpYTCEFE2WGeuuQ=; b=N0miv9+yVzai2QaWph+4gmqZHAXt9//6vqvNqyxB18Te3zFv5yta2t2VVyK3RccBF9Qwbm 561ZznpGsHLYdXuV1x6ve4UznzbjeW/cTc3ckApyX3qSuYqOhw1SsG1LGfVi8kOj8WC8hH ov6EyBE1kTFwd8rROGaTn7hdUg0JjK8fm7fiEy7YSFZimr1t5gc9RpfOl5/Gurc4+w9e9T IBlHo82AfuveUeWVhd6YDbSJ1afbLST1zET1ToR4EN+cdjiv2m/5drzj5fo8/voNmLJYWw 7xNzeB9Mubo5Z2PU9pz1s41tPTV5N0vGFDsycrPZ0O+9pTQ9eYHOn3lab8w3wA== 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 3A62010791; Sat, 12 Feb 2022 22:58:34 +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 21CMwY3o037857; Sat, 12 Feb 2022 22:58:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 21CMwYR7037856; Sat, 12 Feb 2022 22:58:34 GMT (envelope-from git) Date: Sat, 12 Feb 2022 22:58:34 GMT Message-Id: <202202122258.21CMwYR7037856@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: 7e4be228aa36 - stable/12 - Embedded chacha: Add 0-bit iv + 128-bit counter mode List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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: 7e4be228aa36fb6be214c166971c33fb2f24dcb3 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644706714; 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=ntSwIJ+6nurILUoyijEcHivrv/TftpYTCEFE2WGeuuQ=; b=SCUtf7bqinGeWiuXE6gaMFwIcIhxPRJuAEQe2XdWI99G2bZKAY1sXOUMT5iQmWBejvIZeM OlcYPGNDj0oTj5/0faYAEjLvZbmQ82B6byk5X0e3pFz8xlZnQsqhgL3C9mX+MCPcI6YToI gIBh0+jm4sO70h1TkvYANwPPHpqN7TQoH209Aa2a5BkBAuftj55Gk5XUiNR3eqTGYkxYZl psTWkzosOMT5BwyJ7KYseimwjE7JZTlKUKslAViPyP29jYsp/PpHrDK7SHkFVqDzkysZUv zn8GLF1u5b+OW/MJb6PY2F1yqXaCSLdQyy8n5UrQwlNspWk+nS4paARuneBrSg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1644706714; a=rsa-sha256; cv=none; b=wXQvXPA1cxuW6a7JQZyR7fj86WRdKypH2PiWbitFohJzuye0h0Y+Ce4dGgvPk78LZIicOQ Bn/vTJvjOGuwZxf1U8AG7ON+RTL1T6ULJlC7oPDNmLNT1Zakh1Jig6tpI1Oq6xklRSXcEs 99Ya/vT4Y841hZpzy2hL/2efoYRkny+N5IzdIsk3C+siGuUTReLD60ZLd3xERDZPnem1KR 6VLUQdp4S2wwGAexdxIw8WQNsyFXehd5MI0ddCqR2Sc4JeFNntFjtQk102xFY4eUF8ajHQ fpFh28JJV4NUljJlM9d0Va4z3cD6o6W9QSAf62YXR71CvSEfgqmTno7jjwhVRw== 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=7e4be228aa36fb6be214c166971c33fb2f24dcb3 commit 7e4be228aa36fb6be214c166971c33fb2f24dcb3 Author: Conrad Meyer AuthorDate: 2019-03-01 23:30:23 +0000 Commit: David E. O'Brien CommitDate: 2022-02-12 22:27:58 +0000 Embedded chacha: Add 0-bit iv + 128-bit counter mode This mode might be suitable for a Fortuna keystream primitive. (cherry picked from commit 7d93ab5e35d97eb23cb4772be4d431a782a65395) --- sys/crypto/chacha20/chacha.c | 33 +++++++++++++++++++++++++++++++++ sys/crypto/chacha20/chacha.h | 11 ++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/sys/crypto/chacha20/chacha.c b/sys/crypto/chacha20/chacha.c index ac4eef222115..d696307d1a7f 100644 --- a/sys/crypto/chacha20/chacha.c +++ b/sys/crypto/chacha20/chacha.c @@ -84,12 +84,32 @@ chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits) LOCAL void chacha_ivsetup(chacha_ctx *x, const u8 *iv, const u8 *counter) { +#ifndef CHACHA_NONCE0_CTR128 x->input[12] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 0); x->input[13] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 4); x->input[14] = U8TO32_LITTLE(iv + 0); x->input[15] = U8TO32_LITTLE(iv + 4); +#else + // CHACHA_STATELEN + (void)iv; + x->input[12] = U8TO32_LITTLE(counter + 0); + x->input[13] = U8TO32_LITTLE(counter + 4); + x->input[14] = U8TO32_LITTLE(counter + 8); + x->input[15] = U8TO32_LITTLE(counter + 12); +#endif } +#ifdef CHACHA_NONCE0_CTR128 +LOCAL void +chacha_ctrsave(const chacha_ctx *x, u8 *counter) +{ + U32TO8_LITTLE(counter + 0, x->input[12]); + U32TO8_LITTLE(counter + 4, x->input[13]); + U32TO8_LITTLE(counter + 8, x->input[14]); + U32TO8_LITTLE(counter + 12, x->input[15]); +} +#endif + LOCAL void chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u32 bytes) { @@ -190,7 +210,16 @@ chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u32 bytes) j12 = PLUSONE(j12); if (!j12) { j13 = PLUSONE(j13); +#ifndef CHACHA_NONCE0_CTR128 /* stopping at 2^70 bytes per nonce is user's responsibility */ +#else + if (!j13) { + j14 = PLUSONE(j14); + if (!j14) { + j15 = PLUSONE(j15); + } + } +#endif } U32TO8_LITTLE(c + 0,x0); @@ -216,6 +245,10 @@ chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u32 bytes) } x->input[12] = j12; x->input[13] = j13; +#ifdef CHACHA_NONCE0_CTR128 + x->input[14] = j14; + x->input[15] = j15; +#endif return; } bytes -= 64; diff --git a/sys/crypto/chacha20/chacha.h b/sys/crypto/chacha20/chacha.h index 73548331cc85..32262b04d568 100644 --- a/sys/crypto/chacha20/chacha.h +++ b/sys/crypto/chacha20/chacha.h @@ -26,10 +26,19 @@ Public domain. #define LOCAL #endif +#ifdef CHACHA_NONCE0_CTR128 +#define CHACHA_UNUSED __unused +#else +#define CHACHA_UNUSED +#endif + LOCAL void chacha_keysetup(struct chacha_ctx *x, const u_char *k, u_int kbits); -LOCAL void chacha_ivsetup(struct chacha_ctx *x, const u_char *iv, const u_char *ctr); +LOCAL void chacha_ivsetup(struct chacha_ctx *x, const u_char *iv CHACHA_UNUSED, + const u_char *ctr); LOCAL void chacha_encrypt_bytes(struct chacha_ctx *x, const u_char *m, u_char *c, u_int bytes); +#undef CHACHA_UNUSED + #endif /* CHACHA_H */