Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3226641imm; Fri, 19 Oct 2018 07:15:11 -0700 (PDT) X-Google-Smtp-Source: ACcGV62i2+AMJyoKMyNhnSfGEWogkT25oU281MtnfsrFc7sCUQmOPm/kONr05qz9AfwRSjh/yFSv X-Received: by 2002:aa7:8252:: with SMTP id e18-v6mr34967499pfn.164.1539958511269; Fri, 19 Oct 2018 07:15:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539958511; cv=none; d=google.com; s=arc-20160816; b=KN6ofFnGQvRfaRrriBk/oVtFVMXC/HG/rmFJYjX50KbNJeHw7nN9rj++FRif5MsPvM s6Dx06EN8q7Vwg9YU2XpkPcpCqQQsGAHrgZTHnOhBJyt8L5GHURiBgDI92WiLsSdXVa1 B3szCo3PtI3hHVMqW7XnKMQI+iGr8APm7a1w8j3OwMSLzt6WwNoTNh96zz9iHmPhrHWq Byd/TrfJhSVhBMh7Ti419RUcAEc7J0hWH4xp5OX7P2+Da5wf3w74dV5erSZe7zRF9EMn RaPEa80e+S3wlzI9LPY2nfkHILZIwx4/ARTRFYEFGpN/39CRpvIsMHAfLGZ0iZmf1PGz 3jWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature; bh=l3f+Ww9h8FY+AielolXxFK472lMmil5XO+Su6/BhXBk=; b=IHzt960x1dzyiNEK9sQr8T5X07W1eLOOuEjU8MOv3bcEVe30gafmFPm0muBr/0Lso/ bbFha3cTI+89hYaIH8wi0Ra5ivJ8w7WSTSHqQfYz4AMseTgIt0u7Wp1kEVZ+o9lJv/Iq aPgtR9x84WuB1UmkaYYVz6GzOth+VJKSpwH8kCeqhtwSALpGRzxfDMb7e3QKwYxVI6GB A9pJOx12hGqUBWecrlNYp4qisHDsOXS6Y5lShucQ9TkLRKjv6gEfri716eUbRzmRzzo0 9Y9R+QEjQATcjf03TS1jWbfJIxxHGiIULSZMy0I8dhE5wxYnN1MqKn2WU+eUu1vrOtNe Xlpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="UrgKOh2/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c3-v6si18229234pld.3.2018.10.19.07.14.55; Fri, 19 Oct 2018 07:15:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="UrgKOh2/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727620AbeJSWTU (ORCPT + 99 others); Fri, 19 Oct 2018 18:19:20 -0400 Received: from mail-io1-f68.google.com ([209.85.166.68]:41250 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727459AbeJSWTU (ORCPT ); Fri, 19 Oct 2018 18:19:20 -0400 Received: by mail-io1-f68.google.com with SMTP id q4-v6so23092295iob.8 for ; Fri, 19 Oct 2018 07:13:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=l3f+Ww9h8FY+AielolXxFK472lMmil5XO+Su6/BhXBk=; b=UrgKOh2/U3khpvFoBF3DEVczuR9dj0q5qS6fqogVFF+3CrlDPb4hzVxVymm52Xbycc 9jriIv3wYOIbkfqbkAC8QNylWDErmwBbV5mKN4G21lh+v2nzTRah+BD2BtN7FcDDHgTr Clq9/70bT93geux22j6e27l2vZnTtcfzklVEc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=l3f+Ww9h8FY+AielolXxFK472lMmil5XO+Su6/BhXBk=; b=Eu1MK4D3srY6B+TFzpBlWE9/5sFu6dK81PB4Yse2OXg2QOh3kCmdoXSmOh8fMcbMgg OvceoubbqSPC0cRHakI7TOG5QUEK9ujx5NiTpPlECbPeML3XVjCR+Bwt2gT5GnxLHlZJ 8nWlWYRlXWqPznrSSTc+/ddrmB/ZM20KQJ7v/DWhBgVbylMt5FLH3LnTYNv340XUr+lh u1e66rPKBWLYghKqg42CNpKAX8+d1Hi7Bt5DYsXmvyy8SO4SUvrmx6vFLIp4vzHuQ02C yoi937qcg8arv6xVLbUZAm/k7IvHz8KAMGBPQBN3kpCZT1Y5+5Q2ic35CEutb4jF1M5m k3Pw== X-Gm-Message-State: AGRZ1gLbA/Steb1iSjFnmdZ/7ZY5GEgbDH8ocDRC7SgCIGCTrPLf0lxY qvGK0pT/hRJmoX5LN42oX3Tpf/u+er2XTmp/Z13N8g== X-Received: by 2002:a6b:be83:: with SMTP id o125-v6mr2979329iof.173.1539958381127; Fri, 19 Oct 2018 07:13:01 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a6b:5910:0:0:0:0:0 with HTTP; Fri, 19 Oct 2018 07:13:00 -0700 (PDT) In-Reply-To: <20181015175424.97147-2-ebiggers@kernel.org> References: <20181015175424.97147-1-ebiggers@kernel.org> <20181015175424.97147-2-ebiggers@kernel.org> From: Ard Biesheuvel Date: Fri, 19 Oct 2018 22:13:00 +0800 Message-ID: Subject: Re: [RFC PATCH v2 01/12] crypto: chacha20-generic - add HChaCha20 library function To: Eric Biggers Cc: "open list:HARDWARE RANDOM NUMBER GENERATOR CORE" , "Jason A . Donenfeld" , Greg Kaiser , Herbert Xu , Samuel Neves , Michael Halcrow , Linux Kernel Mailing List , linux-fscrypt@vger.kernel.org, Tomer Ashur , linux-arm-kernel , Paul Crowley Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 16 October 2018 at 01:54, Eric Biggers wrote: > From: Eric Biggers > > Refactor the unkeyed permutation part of chacha20_block() into its own > function, then add hchacha20_block() which is the ChaCha equivalent of > HSalsa20 and is an intermediate step towards XChaCha20 (see > https://cr.yp.to/snuffle/xsalsa-20081128.pdf). HChaCha20 skips the > final addition of the initial state, and outputs only certain words of > the state. It should not be used for streaming directly. > > Signed-off-by: Eric Biggers Reviewed-by: Ard Biesheuvel > --- > include/crypto/chacha20.h | 2 ++ > lib/chacha20.c | 50 ++++++++++++++++++++++++++++++++++----- > 2 files changed, 46 insertions(+), 6 deletions(-) > > diff --git a/include/crypto/chacha20.h b/include/crypto/chacha20.h > index f76302d99e2be..fbec4e6a87890 100644 > --- a/include/crypto/chacha20.h > +++ b/include/crypto/chacha20.h > @@ -19,6 +19,8 @@ struct chacha20_ctx { > }; > > void chacha20_block(u32 *state, u8 *stream); > +void hchacha20_block(const u32 *in, u32 *out); > + > void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv); > int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key, > unsigned int keysize); > diff --git a/lib/chacha20.c b/lib/chacha20.c > index d907fec6a9ed1..6a484e16171d1 100644 > --- a/lib/chacha20.c > +++ b/lib/chacha20.c > @@ -1,5 +1,5 @@ > /* > - * ChaCha20 256-bit cipher algorithm, RFC7539 > + * The "hash function" used as the core of the ChaCha20 stream cipher (RFC7539) > * > * Copyright (C) 2015 Martin Willi > * > @@ -16,14 +16,10 @@ > #include > #include > > -void chacha20_block(u32 *state, u8 *stream) > +static void chacha20_permute(u32 *x) > { > - u32 x[16]; > int i; > > - for (i = 0; i < ARRAY_SIZE(x); i++) > - x[i] = state[i]; > - > for (i = 0; i < 20; i += 2) { > x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 16); > x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 16); > @@ -65,6 +61,25 @@ void chacha20_block(u32 *state, u8 *stream) > x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 7); > x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 7); > } > +} > + > +/** > + * chacha20_block - generate one keystream block and increment block counter > + * @state: input state matrix (16 32-bit words) > + * @stream: output keystream block (64 bytes) > + * > + * This is the ChaCha20 core, a function from 64-byte strings to 64-byte > + * strings. The caller has already converted the endianness of the input. This > + * function also handles incrementing the block counter in the input matrix. > + */ > +void chacha20_block(u32 *state, u8 *stream) > +{ > + u32 x[16]; > + int i; > + > + memcpy(x, state, 64); > + > + chacha20_permute(x); > > for (i = 0; i < ARRAY_SIZE(x); i++) > put_unaligned_le32(x[i] + state[i], &stream[i * sizeof(u32)]); > @@ -72,3 +87,26 @@ void chacha20_block(u32 *state, u8 *stream) > state[12]++; > } > EXPORT_SYMBOL(chacha20_block); > + > +/** > + * hchacha20_block - abbreviated ChaCha20 core, for XChaCha20 > + * @in: input state matrix (16 32-bit words) > + * @out: output (8 32-bit words) > + * > + * HChaCha20 is the ChaCha equivalent of HSalsa20 and is an intermediate step > + * towards XChaCha20 (see https://cr.yp.to/snuffle/xsalsa-20081128.pdf). > + * HChaCha20 skips the final addition of the initial state, and outputs only > + * certain words of the state. It should not be used for streaming directly. > + */ > +void hchacha20_block(const u32 *in, u32 *out) > +{ > + u32 x[16]; > + > + memcpy(x, in, 64); > + > + chacha20_permute(x); > + > + memcpy(&out[0], &x[0], 16); > + memcpy(&out[4], &x[12], 16); > +} > +EXPORT_SYMBOL(hchacha20_block); > -- > 2.19.1.331.ge82ca0e54c-goog > > > _______________________________________________ > linux-arm-kernel mailing list > linux-arm-kernel@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-arm-kernel