[lamps] Re: Seed as private key for ML-DSA and ML-KEM

Phillip Hallam-Baker <phill@hallambaker.com> Wed, 21 August 2024 22:44 UTC

Return-Path: <hallam@gmail.com>
X-Original-To: spasm@ietfa.amsl.com
Delivered-To: spasm@ietfa.amsl.com
Received: from localhost (localhost [127.0.0.1]) by ietfa.amsl.com (Postfix) with ESMTP id 8C2B5C17C8B0 for <spasm@ietfa.amsl.com>; Wed, 21 Aug 2024 15:44:59 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -6.654
X-Spam-Level:
X-Spam-Status: No, score=-6.654 tagged_above=-999 required=5 tests=[AC_DIV_BONANZA=0.001, BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_ZEN_BLOCKED_OPENDNS=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01, URIBL_DBL_BLOCKED_OPENDNS=0.001, URIBL_ZEN_BLOCKED_OPENDNS=0.001] autolearn=ham autolearn_force=no
Received: from mail.ietf.org ([50.223.129.194]) by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id zf5Y9SZR92Lc for <spasm@ietfa.amsl.com>; Wed, 21 Aug 2024 15:44:55 -0700 (PDT)
Received: from mail-oa1-f44.google.com (mail-oa1-f44.google.com [209.85.160.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by ietfa.amsl.com (Postfix) with ESMTPS id 9B083C17C8B3 for <spasm@ietf.org>; Wed, 21 Aug 2024 15:44:55 -0700 (PDT)
Received: by mail-oa1-f44.google.com with SMTP id 586e51a60fabf-26ff21d82e4so113788fac.2 for <spasm@ietf.org>; Wed, 21 Aug 2024 15:44:55 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724280295; x=1724885095; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZJh6fwxZc/C2zUc7vbSoTSu7fkZTiyUyNeuOj0emjx4=; b=b6xPEz3F7BL3VZgnh+PGkg7GF9GxnTcT6aagfSFJcx1hQNBN3hfJGcUMViSALI5oid nmybw7wHdyEVm7qb0X7+XiQyi9yjHokFoOQ7PLPJXb/CGLhBZQKrFSN/vIMVZvB+Xgtu w5Lk2ADlIB2XHxkrcXHVy6wAX8zbn/adElpqKKD8oUIki7lIaI8X+Bxb0ruUU3ZEIrNK xyX5XotMAmJUIkgkrsdgzdgeLc5nEX51XBKb2Lxl6wJHRYIOSR0yeglqdoAf4+bPomrG IHAXvOxSO5hSshITA9M0jkF53Li9Bp5Gym9nB3vdpAQ9bXYOYZ1pUPV0nIbGkLZa4iVL k1/Q==
X-Forwarded-Encrypted: i=1; AJvYcCWOzywbYmgsn9tTTo6mV3SJksQjCCB01VL1pNforS6LKZ51uo4OXmHTwca97P3NZqTrFZCrxg==@ietf.org
X-Gm-Message-State: AOJu0YyVSjYgMF6CpF5H7+A0qu1GGZfMa+D3uMuSpOkaNzdlxLnSbAGj gCiDmSels1rnCYYLUGDrxSkyJY68o5Y+qLUwI4s4N5yqCPC6KaLB+7KpO5kJpVdW1ye+CxEil6B FjNVbS7I/vvQVjBWbBY4id7Ywh8g=
X-Google-Smtp-Source: AGHT+IGvvlMADJZWtyGGZ/07+nkrXjbVEKSvKFcx4f8ufIvA3Rvhsvgjw9HGPBu5fVz20m1Xc3ZsWYKKqb/0UeS0PWg=
X-Received: by 2002:a05:6871:5223:b0:270:1498:6a36 with SMTP id 586e51a60fabf-2737ef54d6dmr4445253fac.29.1724280294557; Wed, 21 Aug 2024 15:44:54 -0700 (PDT)
MIME-Version: 1.0
References: <CAMjbhoUOLfcRHT12ubSsPMEnDGT=UJUCvy34VX+qJYmyoFbscg@mail.gmail.com> <CAF8qwaCb8BJLqZQ__9+QsQ1+dWw426BwS4J+vQkUwfjWhdevnA@mail.gmail.com> <CAEEbLAZzhV4GjKRZschE1Sf+OkScJ5jvaMNFCuGCMtZ2g4yQ7Q@mail.gmail.com> <CAMm+Lwg9KJ5OD_5gB8eAq5LGD1T7bTF7qz=_7O5B45fqWOfqvw@mail.gmail.com> <CH0PR11MB5444429A14162FDB153D402AC18E2@CH0PR11MB5444.namprd11.prod.outlook.com> <CAMm+Lwhh8MmASxYjzkM=NHwM0ygQ24gGtcpE7RAUXEna+xY=fw@mail.gmail.com> <CAEEbLAacR6ymdf7=EY50wXt7K8w7YUz3H0NuyjX=oFHK7kopNw@mail.gmail.com>
In-Reply-To: <CAEEbLAacR6ymdf7=EY50wXt7K8w7YUz3H0NuyjX=oFHK7kopNw@mail.gmail.com>
From: Phillip Hallam-Baker <phill@hallambaker.com>
Date: Wed, 21 Aug 2024 18:44:42 -0400
Message-ID: <CAMm+LwhX7oNERAkreEtO5oENj8kdtmVsrtn59=oBa38CHabQwA@mail.gmail.com>
To: Sophie Schmieg <sschmieg@google.com>
Content-Type: multipart/alternative; boundary="000000000000734ebf0620394add"
Message-ID-Hash: SIK567YBSFCPGNNUVTDXQRQKO2GOHC4X
X-Message-ID-Hash: SIK567YBSFCPGNNUVTDXQRQKO2GOHC4X
X-MailFrom: hallam@gmail.com
X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-spasm.ietf.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header
CC: "Scott Fluhrer (sfluhrer)" <sfluhrer@cisco.com>, David Benjamin <davidben@chromium.org>, Bas Westerbaan <bas@cloudflare.com>, LAMPS <spasm@ietf.org>
X-Mailman-Version: 3.3.9rc4
Precedence: list
Subject: [lamps] Re: Seed as private key for ML-DSA and ML-KEM
List-Id: This is the mail list for the LAMPS Working Group <spasm.ietf.org>
Archived-At: <https://mailarchive.ietf.org/arch/msg/spasm/LZJtO3vsRPMFi52U17OyXiFOOHo>
List-Archive: <https://mailarchive.ietf.org/arch/browse/spasm>
List-Help: <mailto:spasm-request@ietf.org?subject=help>
List-Owner: <mailto:spasm-owner@ietf.org>
List-Post: <mailto:spasm@ietf.org>
List-Subscribe: <mailto:spasm-join@ietf.org>
List-Unsubscribe: <mailto:spasm-leave@ietf.org>

Every time I get COVID, I seem to end up discussing RSA keygen with you. I
probably missed a step in the explanation.


It is quite a while since I wrote the code. I was very tempted to do the
optimization you suggest. However, as you point out, you need two seeds,
not one. I rejected it after being pointed to a NIST recommendation for
generating RSA key pairs.

So from the spec, I have

HKDF-Extract(salt, IKM) -> PRK
HKDF-Expand(PRK, info, L) -> OKM
info = alg + param + keyname.UTF8()


So basically, I set up a HKDF using the seed and a salt. Then use it to
churn out the parameters I need which in this case have tags 'p' and 'q'.

So having generated an initial p, you find the closest prime equal or
larger and then do that for q.


One option would be the partial optimization for p but not for q.

If we were going for the full optimization, I would generate random IKM to
generate p as above and then generate a series of q candidates from the
HKDF using the tag 'q0', 'q1',...'q<n>' until we get a prime. Then I would
insert the prime into the UDF string using the JSON-B integer encoding.

I agree it is probably unnecessary but since some of my devices are
Raspberry Pi nanos and ESP32 like things, avoiding the need to do RSA
keygen on them might actually be a win.



On Wed, Aug 21, 2024 at 5:32 PM Sophie Schmieg <sschmieg@google.com> wrote:

> This is now very off-topic, but if you sample both p and q from the same
> seed you either would make your keygen take a lot longer (you now need to
> find seeds that produce two large primes without rejecting, expected number
> of iterations (ln(n/2))^2 instead of 2*ln(n/2)), or you would still have to
> rejection sample when expanding the seed.
>
> On Wed, Aug 21, 2024 at 1:43 PM Phillip Hallam-Baker <
> phill@hallambaker.com> wrote:
>
>> Thanks, Scott. While I would like to avoid RSA and DH, it turns out that
>> there are still a surprising number of applications which require them. And
>> if like me, your scheme is designed to connect all a user's devices
>> together and squirt the credentials they need into them, it turns out RSA
>> is still required.
>>
>>
>> Generate from a single seed appears to be what I implemented:
>>
>>
>> https://www.ietf.org/archive/id/draft-hallambaker-mesh-udf-18.html#name-rsa-key-pairs
>>
>> If I was going to implement the keygen optimization Sophie suggested, I
>> would do it by means of an additional salt to be applied to one seed but
>> not the other.
>>
>>
>> On Wed, Aug 21, 2024 at 4:24 PM Scott Fluhrer (sfluhrer) <
>> sfluhrer@cisco.com> wrote:
>>
>>> Actually, if we’re going to keep on using RSA (“the height of 50 year
>>> old cryptography”), I would recommend not using separate seeds for p and q,
>>> as it leaves open a possible implementation flaw.
>>>
>>>
>>>
>>> What I believe has happened in the past is that when the implementation
>>> sampled the randomness for ‘p’, the entropy was lousy, but when it sampled
>>> the randomness for ‘q’, it got better.  What happens in that case, if we
>>> generate a number of keys this way, that the keys will often have common
>>> ‘p’ values, but distinct ‘q’ values.  I believe that was exactly what
>>> happened in the ‘Mind your P’s and Q’s’ paper by Nadia.
>>>
>>>
>>>
>>> Because of this, both primes should be selected from the same seed –
>>> that way, if entropy is lousy, you’ll pick the same RSA key repeatedly –
>>> admittedly, not great, but better than the previous scenario.
>>>
>>>
>>>
>>> *From:* Phillip Hallam-Baker <phill@hallambaker.com>
>>> *Sent:* Wednesday, August 21, 2024 3:53 PM
>>> *To:* Sophie Schmieg <sschmieg=40google.com@dmarc.ietf.org>
>>> *Cc:* David Benjamin <davidben@chromium.org>; Bas Westerbaan <bas=
>>> 40cloudflare.com@dmarc.ietf.org>; LAMPS <spasm@ietf.org>
>>> *Subject:* [lamps] Re: Seed as private key for ML-DSA and ML-KEM
>>>
>>>
>>>
>>> +1
>>>
>>>
>>>
>>> If we were likely to do a lot of RSA, this is vastly superior to the p/q
>>> approach because it prevents the kleptography attacks Motti Yung described.
>>> If you generate p in the normal fashion, you can generate q in such fashion
>>> that the high bits of the modulus leak the (encrypted) seed. An alternative
>>> approach is to use this scheme to compress the modulus
>>>
>>>
>>>
>>> And being reminded of that, I think we have a security reason to insist
>>> on representing the private key as the seed as opposed to the expanded
>>> private key because it means we block an ML variant of Motti's attacks. I
>>> managed to get C19 a few weeks back and still not quite up to thinking what
>>> those attacks might be but why spend time thinking about a possible attack
>>> we can easily avoid?
>>>
>>>
>>>
>>> So it has to be 4, we should reject 1 and 2 on security grounds so 3 is
>>> irrelevant.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Wed, Aug 21, 2024 at 3:24 PM Sophie Schmieg <sschmieg=
>>> 40google.com@dmarc.ietf.org> wrote:
>>>
>>> For completeness sake, and not to argue for it, you can extend the
>>> private key as seeds paradigm to RSA as well, without destroying
>>> performance, by rejection sampling seeds for p and q until they are both
>>> prime, and serialize the private key as seed_p || seed_q. It would give you
>>> a 64 byte RSA private key, and the expansion would skip over the most
>>> expensive part of RSA keygen without sacrificing the security
>>> gains, assuming you redo the primality check of p and q.
>>>
>>> But practically speaking, I would prefer to not touch RSA serialization.
>>>
>>>
>>>
>>> On Wed, Aug 21, 2024 at 11:04 AM David Benjamin <davidben@chromium.org>
>>> wrote:
>>>
>>> I'm also sold on option 4.
>>>
>>> On Wed, Aug 21, 2024, 11:50 Bas Westerbaan <bas=
>>> 40cloudflare.com@dmarc.ietf.org> wrote:
>>>
>>> Hi all,
>>>
>>>
>>>
>>> NIST allows two formats in which private keys are stored ML-DSA and
>>> ML-KEM.
>>>
>>>
>>>
>>> 1. Seed. 32 bytes for ML-DSA; 64 bytes for ML-KEM.
>>>
>>> 2. Expanded private key. Multiple kilobytes depending on instance.
>>>
>>>
>>>
>>> An expanded private key is obtained from the seed by calling the
>>> KeyGen_internal function.
>>>
>>>
>>>
>>> In contrast to RSA, key generation for these algorithms is very fast. In
>>> fact, if you use spinning disks, then using a seed is probably faster than
>>> the expanded private key.
>>>
>>>
>>>
>>> Another advantage is that we do not need to worry about private key
>>> validation. NIST specified a few checks to perform, but there are more we
>>> could do (eg. whether the decoded coefficients of \hat{s} are bounded.)
>>>
>>>
>>>
>>> Of course a big advantage is storage space: the seeds are much smaller.
>>>
>>>
>>>
>>> Now, how do we want to proceed? I see a few options.
>>>
>>>
>>>
>>> 1. Ignore the seed as private key.
>>>
>>>
>>>
>>> 2. Allow both seed and the expanded private key.
>>>
>>>
>>>
>>> 3. Assign separate algorithm for seed-as-private-key.
>>>
>>>
>>>
>>> 4. Switch to seed as private key only.
>>>
>>>
>>>
>>> I prefer 4, and would otherwise go for 1.
>>>
>>>
>>>
>>> The downside of 2 is that it adds complexity without the gain of
>>> simplifying verification. If one only cares about size savings, then one
>>> can use seed-as-private-key without needing a portable format for it.
>>>
>>>
>>>
>>> So I'd prefer 4 and 1 second.
>>>
>>>
>>>
>>> Best,
>>>
>>>
>>>
>>>  Bas
>>>
>>> _______________________________________________
>>> Spasm mailing list -- spasm@ietf.org
>>> To unsubscribe send an email to spasm-leave@ietf.org
>>>
>>> _______________________________________________
>>> Spasm mailing list -- spasm@ietf.org
>>> To unsubscribe send an email to spasm-leave@ietf.org
>>>
>>>
>>>
>>>
>>> --
>>>
>>>
>>> Sophie Schmieg | Information Security Engineer | ISE Crypto |
>>> sschmieg@google.com
>>>
>>>
>>>
>>> _______________________________________________
>>> Spasm mailing list -- spasm@ietf.org
>>> To unsubscribe send an email to spasm-leave@ietf.org
>>>
>>>
>
> --
>
> Sophie Schmieg | Information Security Engineer | ISE Crypto |
> sschmieg@google.com
>
>