From owner-svn-src-head@freebsd.org Sat Apr 22 01:06:24 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E58E2D40601; Sat, 22 Apr 2017 01:06:24 +0000 (UTC) (envelope-from des@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 mx1.freebsd.org (Postfix) with ESMTPS id B4F3CDC7; Sat, 22 Apr 2017 01:06:24 +0000 (UTC) (envelope-from des@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v3M16N16097823; Sat, 22 Apr 2017 01:06:23 GMT (envelope-from des@FreeBSD.org) Received: (from des@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v3M16NLH097822; Sat, 22 Apr 2017 01:06:23 GMT (envelope-from des@FreeBSD.org) Message-Id: <201704220106.v3M16NLH097822@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: des set sender to des@FreeBSD.org using -f From: =?UTF-8?Q?Dag-Erling_Sm=c3=b8rgrav?= Date: Sat, 22 Apr 2017 01:06:23 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r317277 - head/sys/crypto/chacha20 X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 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: Sat, 22 Apr 2017 01:06:25 -0000 Author: des Date: Sat Apr 22 01:06:23 2017 New Revision: 317277 URL: https://svnweb.freebsd.org/changeset/base/317277 Log: Fix counter increment in Salsa and ChaCha. In my eagerness to eliminate a branch which is taken once per 2^38 bytes of keystream, I forgot that the state words are in host order. Thus, the counter increment code worked fine on little-endian machines, but not on big-endian ones. Switch to a simpler (branchful) solution. Modified: head/sys/crypto/chacha20/chacha20.c Modified: head/sys/crypto/chacha20/chacha20.c ============================================================================== --- head/sys/crypto/chacha20/chacha20.c Fri Apr 21 23:01:32 2017 (r317276) +++ head/sys/crypto/chacha20/chacha20.c Sat Apr 22 01:06:23 2017 (r317277) @@ -130,7 +130,6 @@ size_t chacha20_encrypt(chacha20_ctx *ctx, const void *vpt, uint8_t *ct, size_t len) { const uint8_t *pt = vpt; - uint64_t ctr; uint32_t mix[16]; uint8_t ks[64]; unsigned int b, i; @@ -157,8 +156,8 @@ chacha20_encrypt(chacha20_ctx *ctx, cons for (i = 0; i < 64 && i < len; ++i) *ct++ = *pt++ ^ ks[i]; } - ctr = le64dec(ctx->state + 12); - le64enc(ctx->state + 12, ++ctr); + if (++ctx->state[12] == 0) + ++ctx->state[13]; } return (len); }