From owner-svn-src-head@freebsd.org Fri Mar 1 23:30:24 2019 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 745861505AC7; Fri, 1 Mar 2019 23:30:24 +0000 (UTC) (envelope-from cem@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 1778C97CD3; Fri, 1 Mar 2019 23:30:24 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 0370323BD6; Fri, 1 Mar 2019 23:30:24 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x21NUNI2036502; Fri, 1 Mar 2019 23:30:23 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x21NUNMW036500; Fri, 1 Mar 2019 23:30:23 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201903012330.x21NUNMW036500@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Fri, 1 Mar 2019 23:30:23 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r344714 - head/sys/crypto/chacha20 X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: head/sys/crypto/chacha20 X-SVN-Commit-Revision: 344714 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 1778C97CD3 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.96 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,0]; NEURAL_HAM_SHORT(-0.96)[-0.960,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US] X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Mar 2019 23:30:24 -0000 Author: cem Date: Fri Mar 1 23:30:23 2019 New Revision: 344714 URL: https://svnweb.freebsd.org/changeset/base/344714 Log: Embedded chacha: Add 0-bit iv + 128-bit counter mode This mode might be suitable for a Fortuna keystream primitive. Reviewed by: markm Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D19410 Modified: head/sys/crypto/chacha20/chacha.c head/sys/crypto/chacha20/chacha.h Modified: head/sys/crypto/chacha20/chacha.c ============================================================================== --- head/sys/crypto/chacha20/chacha.c Fri Mar 1 22:51:45 2019 (r344713) +++ head/sys/crypto/chacha20/chacha.c Fri Mar 1 23:30:23 2019 (r344714) @@ -84,13 +84,33 @@ 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) { u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; @@ -192,7 +212,16 @@ chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u 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); @@ -218,6 +247,10 @@ chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u } x->input[12] = j12; x->input[13] = j13; +#ifdef CHACHA_NONCE0_CTR128 + x->input[14] = j14; + x->input[15] = j15; +#endif return; } bytes -= 64; Modified: head/sys/crypto/chacha20/chacha.h ============================================================================== --- head/sys/crypto/chacha20/chacha.h Fri Mar 1 22:51:45 2019 (r344713) +++ head/sys/crypto/chacha20/chacha.h Fri Mar 1 23:30:23 2019 (r344714) @@ -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 */