| 1 |
// Copyright 2025 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package mldsa |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto/internal/fips140" |
| 10 |
_ "crypto/internal/fips140/check" |
| 11 |
"crypto/internal/fips140/sha256" |
| 12 |
"errors" |
| 13 |
"sync" |
| 14 |
) |
| 15 |
|
| 16 |
func fipsPCT(priv *PrivateKey) { |
| 17 |
fips140.PCT("ML-DSA sign and verify PCT", func() error { |
| 18 |
μ := make([]byte, 64) |
| 19 |
sig, err := SignExternalMuDeterministic(priv, μ) |
| 20 |
if err != nil { |
| 21 |
return err |
| 22 |
} |
| 23 |
return VerifyExternalMu(priv.PublicKey(), μ, sig) |
| 24 |
}) |
| 25 |
} |
| 26 |
|
| 27 |
var fipsSelfTest = sync.OnceFunc(func() { |
| 28 |
fips140.CAST("ML-DSA-44", fips140CAST) |
| 29 |
}) |
| 30 |
|
| 31 |
// fips140CAST covers all rejection sampling paths, as recommended by IG 10.3.A, |
| 32 |
// and as tested by TestCASTRejectionPaths. It tests only one parameter set as |
| 33 |
// allowed by Note26. It tests the modified version of Algorithm 7 and 8 with a |
| 34 |
// fixed mu/μ, as allowed by IG 10.3.A, Resolution 15. It compares sk and not |
| 35 |
// pk, because H(pk) is part of sk, as allowed by the same Resolution. It |
| 36 |
// compares the results with hashes instead of values, to avoid embedding several |
| 37 |
// kilobytes of test vectors in every binary, as allowed by GeneralNote7. |
| 38 |
func fips140CAST() error { |
| 39 |
// From https://pages.nist.gov/ACVP/draft-celi-acvp-ml-dsa.html#table-1. |
| 40 |
var seed = &[32]byte{ |
| 41 |
0x5c, 0x62, 0x4f, 0xcc, 0x18, 0x62, 0x45, 0x24, |
| 42 |
0x52, 0xd0, 0xc6, 0x65, 0x84, 0x0d, 0x82, 0x37, |
| 43 |
0xf4, 0x31, 0x08, 0xe5, 0x49, 0x9e, 0xdc, 0xdc, |
| 44 |
0x10, 0x8f, 0xbc, 0x49, 0xd5, 0x96, 0xe4, 0xb7, |
| 45 |
} |
| 46 |
var μ = &[64]byte{ |
| 47 |
0x2a, 0xd1, 0xc7, 0x2b, 0xb0, 0xfc, 0xbe, 0x28, |
| 48 |
0x09, 0x9c, 0xe8, 0xbd, 0x2e, 0xd8, 0x36, 0xdf, |
| 49 |
0xeb, 0xe5, 0x20, 0xaa, 0xd3, 0x8f, 0xba, 0xc6, |
| 50 |
0x6e, 0xf7, 0x85, 0xa3, 0xcf, 0xb1, 0x0f, 0xb4, |
| 51 |
0x19, 0x32, 0x7f, 0xa5, 0x78, 0x18, 0xee, 0x4e, |
| 52 |
0x37, 0x18, 0xda, 0x4b, 0xe4, 0x8d, 0x24, 0xb5, |
| 53 |
0x9a, 0x20, 0x8f, 0x88, 0x07, 0x27, 0x1f, 0xdb, |
| 54 |
0x7e, 0xda, 0x6e, 0x60, 0x14, 0x1b, 0xd2, 0x63, |
| 55 |
} |
| 56 |
var skHash = []byte{ |
| 57 |
0x29, 0x37, 0x49, 0x51, 0xcb, 0x2b, 0xc3, 0xcd, |
| 58 |
0xa7, 0x31, 0x5c, 0xe7, 0xf0, 0xab, 0x99, 0xc7, |
| 59 |
0xd2, 0xd6, 0x52, 0x92, 0xe6, 0xc5, 0x15, 0x6e, |
| 60 |
0x8a, 0xa6, 0x2a, 0xc1, 0x4b, 0x14, 0x12, 0xaf, |
| 61 |
} |
| 62 |
var sigHash = []byte{ |
| 63 |
0xdc, 0xc7, 0x1a, 0x42, 0x1b, 0xc6, 0xff, 0xaf, |
| 64 |
0xb7, 0xdf, 0x0c, 0x7f, 0x6d, 0x01, 0x8a, 0x19, |
| 65 |
0xad, 0xa1, 0x54, 0xd1, 0xe2, 0xee, 0x36, 0x0e, |
| 66 |
0xd5, 0x33, 0xce, 0xcd, 0x5d, 0xc9, 0x80, 0xad, |
| 67 |
} |
| 68 |
priv := newPrivateKey(seed, params44) |
| 69 |
H := sha256.New() |
| 70 |
H.Write(TestingOnlyPrivateKeySemiExpandedBytes(priv)) |
| 71 |
if !bytes.Equal(H.Sum(nil), skHash) { |
| 72 |
return errors.New("unexpected private key hash") |
| 73 |
} |
| 74 |
var random [32]byte |
| 75 |
sig := signInternal(priv, μ, &random) |
| 76 |
H.Reset() |
| 77 |
H.Write(sig) |
| 78 |
if !bytes.Equal(H.Sum(nil), sigHash) { |
| 79 |
return errors.New("unexpected signature hash") |
| 80 |
} |
| 81 |
return verifyInternal(priv.PublicKey(), μ, sig) |
| 82 |
} |