Re: ssh-ed25519 implementations

Ron Frederick <ronf@timeheart.net> Fri, 12 May 2017 06:39 UTC

Return-Path: <bounces-ietf-ssh-owner-secsh-tyoxbijeg7-archive=lists.ietf.org@NetBSD.org>
X-Original-To: ietfarch-secsh-tyoxbijeg7-archive@ietfa.amsl.com
Delivered-To: ietfarch-secsh-tyoxbijeg7-archive@ietfa.amsl.com
Received: from localhost (localhost [127.0.0.1]) by ietfa.amsl.com (Postfix) with ESMTP id 630D7129C06 for <ietfarch-secsh-tyoxbijeg7-archive@ietfa.amsl.com>; Thu, 11 May 2017 23:39:02 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -4.089
X-Spam-Level:
X-Spam-Status: No, score=-4.089 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_MED=-2.3, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001, T_DKIM_INVALID=0.01, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
Authentication-Results: ietfa.amsl.com (amavisd-new); dkim=fail (1024-bit key) reason="fail (body has been altered)" header.d=timeheart.net
Received: from mail.ietf.org ([4.31.198.44]) by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Sbi9gca5kIqd for <ietfarch-secsh-tyoxbijeg7-archive@ietfa.amsl.com>; Thu, 11 May 2017 23:38:58 -0700 (PDT)
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ietfa.amsl.com (Postfix) with ESMTPS id B943B12EB1E for <secsh-tyoxbijeg7-archive@lists.ietf.org>; Thu, 11 May 2017 23:34:26 -0700 (PDT)
Received: by mail.netbsd.org (Postfix, from userid 605) id 417998559E; Fri, 12 May 2017 06:34:26 +0000 (UTC)
Delivered-To: ietf-ssh@netbsd.org
Received: by mail.netbsd.org (Postfix, from userid 1347) id D61F88559D; Fri, 12 May 2017 06:34:25 +0000 (UTC)
Received: from localhost (localhost [127.0.0.1]) by mail.netbsd.org (Postfix) with ESMTP id 5C09484D9A for <ietf-ssh@netbsd.org>; Fri, 12 May 2017 02:27:36 +0000 (UTC)
X-Virus-Scanned: amavisd-new at netbsd.org
Authentication-Results: mail.netbsd.org (amavisd-new); dkim=pass (1024-bit key) header.d=timeheart.net
Received: from mail.netbsd.org ([127.0.0.1]) by localhost (mail.netbsd.org [127.0.0.1]) (amavisd-new, port 10025) with ESMTP id zzOGLpc4UpnN for <ietf-ssh@netbsd.org>; Fri, 12 May 2017 02:27:34 +0000 (UTC)
Received: from mail-pf0-x22e.google.com (mail-pf0-x22e.google.com [IPv6:2607:f8b0:400e:c00::22e]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.netbsd.org (Postfix) with ESMTPS id A30BB84DD0 for <ietf-ssh@netbsd.org>; Fri, 12 May 2017 02:27:33 +0000 (UTC)
Received: by mail-pf0-x22e.google.com with SMTP id n23so17721575pfb.2 for <ietf-ssh@netbsd.org>; Thu, 11 May 2017 19:27:33 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=timeheart.net; s=mail; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=ptbK9m7gtynEgdKDlQy+PclvKEzbeRRAQ42uJINpoTU=; b=LBhnnzFTaOw9dVA8R9z0TxkEwhk4gEZAO5/HE7wPY5yYfQz0R0XafBg2JZpoYHiBjG 7MjWFFUfXLfdoQDry4qprgvAwZyYb0A9220SWlNpGQWudgehnhnbKoI2Z9JjQyJpPQH3 uxKKgx4Rr3MyQojvbm1EhO/JsdJUHnVbznKXU=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=ptbK9m7gtynEgdKDlQy+PclvKEzbeRRAQ42uJINpoTU=; b=UUv7/JuThFojMUfL1AZRRlbWjx454DA7mhknb55qXjHti/wcliVbpR3G/1HyAjGF8s XjexeZ4wzGJPq97fB7aKNlFuRoSIiDl8tWQjwRroVLyxo/FWmSlEWkQKQskK6IhxJddh rR6F2DvDw6C3f7WJRM1r1IvE2LAvMviCECG0X3NWx6pumtbj83MkZoIFP/46wepP4f9z aBbaD+ClytmxU2/u4al0OshKyjkYxZUhg7NUQPhLhi46s9j5ny+TJqBCEcEeDceSuxpS zKj8hwyLKnfEQkjZXQR2tbxfP1//MHOKU6MAHvDKdGE0V+PqJglMbE5cmtldBA+XJQCn 9LEA==
X-Gm-Message-State: AODbwcCr8/HzerLiF8hXGokZ793YP039tpJgiLYENGjnvIuTk+g/Gb+w +B5+z8OtYP/8zWA2pA780A==
X-Received: by 10.99.178.11 with SMTP id x11mr1863019pge.68.1494556053230; Thu, 11 May 2017 19:27:33 -0700 (PDT)
Received: from ?IPv6:2601:647:4282:2200:cfa:5497:d692:7eeb? ([2601:647:4282:2200:cfa:5497:d692:7eeb]) by smtp.gmail.com with ESMTPSA id w23sm2155910pfl.133.2017.05.11.19.27.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 May 2017 19:27:32 -0700 (PDT)
From: Ron Frederick <ronf@timeheart.net>
Message-Id: <6047C877-67DE-404F-8FBD-5B2C19D16EA6@timeheart.net>
Content-Type: multipart/alternative; boundary="Apple-Mail=_0C05DEBB-4A4A-4E87-B290-B93427D3E747"
Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\))
Subject: Re: ssh-ed25519 implementations
Date: Thu, 11 May 2017 19:27:30 -0700
In-Reply-To: <36528.1494509552@eng-mail01.juniper.net>
Cc: Eric Rescorla <ekr@rtfm.com>, Brian Smith <brian@briansmith.org>, denis bider <denisbider.ietf@gmail.com>, Simon Tatham <anakin@pobox.com>, "ietf-ssh@NetBSD.org" <ietf-ssh@NetBSD.org>, "curdle@ietf.org" <curdle@ietf.org>
To: "Mark D. Baushke" <mdb@juniper.net>
References: <76FD0F39-1F3D-4476-A3D8-D4C942C2EFD1@juniper.net> <CABcZeBNYUV=-azoZzZjnNtCEu3K0A-THHN2mt02V65oihbbrXw@mail.gmail.com> <36528.1494509552@eng-mail01.juniper.net>
X-Mailer: Apple Mail (2.3273)
Sender: ietf-ssh-owner@NetBSD.org
List-Id: ietf-ssh.NetBSD.org
Precedence: list
List-Unsubscribe: <mailto:majordomo@NetBSD.org?subject=Unsubscribe%20ietf-ssh&body=unsubscribe%20ietf-ssh>

Hi Mark,

On May 11, 2017, at 6:32 AM, Mark D. Baushke <mdb@juniper.net> wrote:
> Hi Eric & Ron & Brian & Simon,
> 
> Given input from folks so far, I think it would be better if both
> Curve25519 and Curve448 continued to use the "mpint" format for K when
> generating a hash even though this is not what RFC7748 suggests.
> 
> Would it make sense to include the following text to the end of section
> 2.1 of https://tools.ietf.org/html/draft-ietf-curdle-ssh-curves-04 ?
> 
>    When performing the X25519 or X448 operations, the integer values
>    there will be encoded into byte strings by doing a fix-length
>    unsigned litle-endian conversion, per [RFC7748]. It is only later
>    when these byte strings are then passed to the ECDH code in SSH that
>    the bytes are re-interpreted as a fixed-length unsigned big-endian
>    integer value K, and then later that K value is encoded as a
>    variable-length signed "mpint" before being fed to the hash
>    algorithm used for key generation.
> 
> to help clarify the differences between RFC7748 and what is happening in
> SSH?
> 
> Much of this text is borrowed from what Ron Frederick has written to me,
> any remaining confusion is my fault.
> 
> I think that the above text should help clear up the confusion that Eric
> noted in this section of code.
> 
> If there are no problems with this text, I will release the -05 draft
> with it.


I just took a look at the updated draft. Here’s a copy of the text in section 2.1 and some inline suggestions:

2.1 <https://tools.ietf.org/html/draft-ietf-curdle-ssh-curves-05#section-2.1>.  Shared Secret Encoding

   The following step differs from [RFC5656 <https://tools.ietf.org/html/rfc5656>], which uses a different
   conversion.  This is not intended to modify that text generally, but
   only to be applicable to the scope of the mechanism described in this
   document.

   The shared secret, K, is defined in [RFC4253 <https://tools.ietf.org/html/rfc4253>] as a multiple precision
   integer (mpint).  Curve25519/448 outputs a binary string X, which is

[Ron] RFC 4253 actually says that K is “encoded as an mpint”. I’d suggest saying “The shared secret, K, is defined in [RFC4253] as an integer."

   the 32 or 56 byte point obtained by scalar multiplication of the
   other side's public key and the local private key scalar.  The 32 or
   56 bytes of X are converted into K by interpreting the bytes as an
   unsigned fixed-length integer encoded in network byte order.  This
   conversion follows the normal "mpint" process as described in section <https://tools.ietf.org/html/rfc4251#section-5>
   5 of [RFC4251] <https://tools.ietf.org/html/rfc4251#section-5>.

[Ron] There are actually two conversions here, from the byte string X to the integer K, and then from K back to a byte string to be fed to the hash used for key generation. This text makes it sound like the conversion from X to K should follow the “mpint” conversion, but that’s not the case. The sentence about how to convert X into the integer K looks good, but I would drop the last sentence here and begin a new paragraph which discusses the second conversion from the integer K back to a byte string for insertion into the hash, such as:

The integer K is then fed along with other data to the key exchange method’s hash function to generate encryption keys. During this process, [RFC4253] specifies that K should be encoded as an “mpint” as described in section 5 of [RFC4251]. This conversion may result in a value which varies from the fixed-length byte string X as follows:

This new paragraph can replace the next paragraph below.

   To clarify a corner-case in this conversion, when X is encoded as an
   mpint K, in order to calculate the exchange hash, it may vary as
   follows:

   o  Trim all leading zero-bytes of X.  If X is all zero-bytes, then
      the key exchange MUST fail.

[Ron] Are you sure this is necessary? I don’t see any mention of this restriction in RFC 4253. If the integer value of K is 0, it can still be encoded as a valid mpInt.

   o  If the high bit of X is set, the mpint format requires a zero byte
      to be prepended.

   o  The length of the encoded K may not be the same as the original
      length of X due to trimming or prepending zero-bytes as needed for
      "mpint" format.

[Ron] Actually, after doing all of this, the mpint conversion also requires the that resulting byte string be preceded by a 4-byte big-endian length value. So, even if the X value has no bytes removed or prepended, it will be 4 bytes longer than it was originally. There’s no mention of this length here.

   Or, as pseudo code:

                 k := x;
                 while (k.length() > 0 && k[0] == 0) k = k[1:];
                 assert(k.length() > 0);
                 if 0 != (k[0] & 0x80) k = '\0' .. k;

                                 Figure 1

   When performing the X25519 or X448 operations, the integer values
   there will be encoded into byte strings by doing a fix-length
   unsigned litle-endian conversion, per [RFC7748 <https://tools.ietf.org/html/rfc7748>].  It is only later
   when these byte strings are then passed to the ECDH code in SSH that
   the bytes are re-interpreted as a fixed-length unsigned big-endian
   integer value K, and then later that K value is encoded as a
   variable-length signed "mpint" before being fed to the hash algorithm
   used for key generation.

[Ron] With the changes I have proposed above, I’m not sure this last paragraph is needed. Alternately, I’d think about working some of this text into the two paragraphs above that discuss the conversion from X to K and then from K to an mpint. Also, “fix-length” here should be “fixed-length”. I think it is better to cover this point before diving into the details of how the “mpint” conversion is different from the original X byte string.
-- 
Ron Frederick
ronf@timeheart.net