if pkgname == "runtime" {
continue
}
- if pkgname == "crypto/internal/nistec/fiat" {
+ if pkgname == "crypto/internal/fips/nistec/fiat" {
continue // golang.org/issue/49372
}
if e.Val(dwarf.AttrStmtList) == nil {
import (
"crypto/internal/boring"
- "crypto/internal/nistec"
+ "crypto/internal/fips/nistec"
"crypto/internal/randutil"
"errors"
"internal/byteorder"
"crypto/internal/bigmod"
"crypto/internal/boring"
"crypto/internal/boring/bbig"
- "crypto/internal/nistec"
+ "crypto/internal/fips/nistec"
"crypto/internal/randutil"
"crypto/sha512"
"crypto/subtle"
package elliptic
import (
- "crypto/internal/nistec"
+ "crypto/internal/fips/nistec"
"errors"
"math/big"
)
p224.params = &CurveParams{
Name: "P-224",
BitSize: 224,
- // FIPS 186-4, section D.1.2.2
+ // SP 800-186, Section 3.2.1.2
P: bigFromDecimal("26959946667150639794667015087019630673557916260026308143510066298881"),
N: bigFromDecimal("26959946667150639794667015087019625940457807714424391721682722368061"),
B: bigFromHex("b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4"),
p256.params = &CurveParams{
Name: "P-256",
BitSize: 256,
- // FIPS 186-4, section D.1.2.3
+ // SP 800-186, Section 3.2.1.3
P: bigFromDecimal("115792089210356248762697446949407573530086143415290314195533631308867097853951"),
N: bigFromDecimal("115792089210356248762697446949407573529996955224135760342422259061068512044369"),
B: bigFromHex("5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"),
p384.params = &CurveParams{
Name: "P-384",
BitSize: 384,
- // FIPS 186-4, section D.1.2.4
+ // SP 800-186, Section 3.2.1.4
P: bigFromDecimal("394020061963944792122790401001436138050797392704654" +
"46667948293404245721771496870329047266088258938001861606973112319"),
N: bigFromDecimal("394020061963944792122790401001436138050797392704654" +
p521.params = &CurveParams{
Name: "P-521",
BitSize: 521,
- // FIPS 186-4, section D.1.2.5
+ // SP 800-186, Section 3.2.1.5
P: bigFromDecimal("68647976601306097149819007990813932172694353001433" +
"0540939446345918554318339765605212255964066145455497729631139148" +
"0858037121987999716643812574028291115057151"),
package elliptic
import (
- "crypto/internal/nistec"
+ "crypto/internal/fips/nistec"
"math/big"
)
-module std/crypto/internal/nistec/_asm
+module std/crypto/internal/fips/nistec/_asm
go 1.24
)
func main() {
- Package("crypto/internal/nistec")
+ Package("crypto/internal/fips/nistec")
ConstraintExpr("!purego")
p256MovCond()
p256NegCond()
--- /dev/null
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package nistec_test
+
+import (
+ "crypto/internal/fips/nistec"
+ "crypto/rand"
+ "testing"
+)
+
+type nistPoint[T any] interface {
+ Bytes() []byte
+ SetGenerator() T
+ SetBytes([]byte) (T, error)
+ Add(T, T) T
+ Double(T) T
+ ScalarMult(T, []byte) (T, error)
+ ScalarBaseMult([]byte) (T, error)
+}
+
+func BenchmarkScalarMult(b *testing.B) {
+ b.Run("P224", func(b *testing.B) {
+ benchmarkScalarMult(b, nistec.NewP224Point().SetGenerator(), 28)
+ })
+ b.Run("P256", func(b *testing.B) {
+ benchmarkScalarMult(b, nistec.NewP256Point().SetGenerator(), 32)
+ })
+ b.Run("P384", func(b *testing.B) {
+ benchmarkScalarMult(b, nistec.NewP384Point().SetGenerator(), 48)
+ })
+ b.Run("P521", func(b *testing.B) {
+ benchmarkScalarMult(b, nistec.NewP521Point().SetGenerator(), 66)
+ })
+}
+
+func benchmarkScalarMult[P nistPoint[P]](b *testing.B, p P, scalarSize int) {
+ scalar := make([]byte, scalarSize)
+ rand.Read(scalar)
+ b.ReportAllocs()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ p.ScalarMult(p, scalar)
+ }
+}
+
+func BenchmarkScalarBaseMult(b *testing.B) {
+ b.Run("P224", func(b *testing.B) {
+ benchmarkScalarBaseMult(b, nistec.NewP224Point().SetGenerator(), 28)
+ })
+ b.Run("P256", func(b *testing.B) {
+ benchmarkScalarBaseMult(b, nistec.NewP256Point().SetGenerator(), 32)
+ })
+ b.Run("P384", func(b *testing.B) {
+ benchmarkScalarBaseMult(b, nistec.NewP384Point().SetGenerator(), 48)
+ })
+ b.Run("P521", func(b *testing.B) {
+ benchmarkScalarBaseMult(b, nistec.NewP521Point().SetGenerator(), 66)
+ })
+}
+
+func benchmarkScalarBaseMult[P nistPoint[P]](b *testing.B, p P, scalarSize int) {
+ scalar := make([]byte, scalarSize)
+ rand.Read(scalar)
+ b.ReportAllocs()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ p.ScalarBaseMult(scalar)
+ }
+}
package fiat_test
import (
- "crypto/internal/nistec/fiat"
+ "crypto/internal/fips/nistec/fiat"
"testing"
)
package fiat
import (
- "crypto/subtle"
+ "crypto/internal/fips/subtle"
"errors"
)
package fiat
import (
- "crypto/subtle"
+ "crypto/internal/fips/subtle"
"errors"
)
package fiat
import (
- "crypto/subtle"
+ "crypto/internal/fips/subtle"
"errors"
)
package fiat
import (
- "crypto/subtle"
+ "crypto/internal/fips/subtle"
"errors"
)
package fiat
import (
- "crypto/subtle"
+ "crypto/internal/fips/subtle"
"errors"
)
package nistec
import (
- "crypto/internal/nistec/fiat"
- "crypto/subtle"
+ "crypto/internal/fips/nistec/fiat"
+ "crypto/internal/fips/subtle"
"errors"
"sync"
)
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-// Package nistec implements the NIST P elliptic curves from FIPS 186-4.
+// Package nistec implements the elliptic curves from NIST SP 800-186.
//
// This package uses fiat-crypto or specialized assembly and Go code for its
// backend field arithmetic (not math/big) and exposes constant-time, heap
package nistec
import (
- "crypto/internal/nistec/fiat"
- "crypto/subtle"
+ "crypto/internal/fips/nistec/fiat"
+ "crypto/internal/fips/subtle"
"errors"
"sync"
)
package nistec
import (
- "crypto/internal/nistec/fiat"
+ "crypto/internal/fips/nistec/fiat"
"sync"
)
package nistec
import (
- "crypto/internal/nistec/fiat"
- "crypto/subtle"
+ "crypto/internal/fips/nistec/fiat"
+ "crypto/internal/fips/subtle"
+ "crypto/internal/fipsdeps/byteorder"
+ "crypto/internal/fipsdeps/cpu"
"errors"
- "internal/byteorder"
- "internal/goarch"
"math/bits"
"sync"
"unsafe"
return nil, errors.New("invalid scalar length")
}
- s[0] = byteorder.BeUint64(x[24:])
- s[1] = byteorder.BeUint64(x[16:])
- s[2] = byteorder.BeUint64(x[8:])
- s[3] = byteorder.BeUint64(x[:])
+ s[0] = byteorder.BEUint64(x[24:])
+ s[1] = byteorder.BEUint64(x[16:])
+ s[2] = byteorder.BEUint64(x[8:])
+ s[3] = byteorder.BEUint64(x[:])
// Ensure s is in the range [0, ord(G)-1]. Since 2 * ord(G) > 2²⁵⁶, we can
// just conditionally subtract ord(G), keeping the result if it doesn't
func (s *p256OrdElement) Bytes() []byte {
var out [32]byte
- byteorder.BePutUint64(out[24:], s[0])
- byteorder.BePutUint64(out[16:], s[1])
- byteorder.BePutUint64(out[8:], s[2])
- byteorder.BePutUint64(out[:], s[3])
+ byteorder.BEPutUint64(out[24:], s[0])
+ byteorder.BEPutUint64(out[16:], s[1])
+ byteorder.BEPutUint64(out[8:], s[2])
+ byteorder.BEPutUint64(out[:], s[3])
return out[:]
}
func init() {
p256GeneratorTablesPtr := unsafe.Pointer(&p256PrecomputedEmbed)
- if goarch.BigEndian {
+ if cpu.BigEndian {
var newTable [43 * 32 * 2 * 4]uint64
for i, x := range (*[43 * 32 * 2 * 4][8]byte)(p256GeneratorTablesPtr) {
- newTable[i] = byteorder.LeUint64(x[:])
+ newTable[i] = byteorder.LEUint64(x[:])
}
p256GeneratorTablesPtr = unsafe.Pointer(&newTable)
}
package nistec
import (
+ "crypto/internal/fipsdeps/byteorder"
"errors"
- "internal/byteorder"
"math/bits"
"runtime"
"unsafe"
}
func bytesToLimbs(l *[4]uint64, b *[32]byte) {
- l[0] = byteorder.BeUint64(b[24:])
- l[1] = byteorder.BeUint64(b[16:])
- l[2] = byteorder.BeUint64(b[8:])
- l[3] = byteorder.BeUint64(b[:])
+ l[0] = byteorder.BEUint64(b[24:])
+ l[1] = byteorder.BEUint64(b[16:])
+ l[2] = byteorder.BEUint64(b[8:])
+ l[3] = byteorder.BEUint64(b[:])
}
func p256LittleToBig(b *[32]byte, l *p256Element) {
}
func limbsToBytes(b *[32]byte, l *[4]uint64) {
- byteorder.BePutUint64(b[24:], l[0])
- byteorder.BePutUint64(b[16:], l[1])
- byteorder.BePutUint64(b[8:], l[2])
- byteorder.BePutUint64(b[:], l[3])
+ byteorder.BEPutUint64(b[24:], l[0])
+ byteorder.BEPutUint64(b[16:], l[1])
+ byteorder.BEPutUint64(b[8:], l[2])
+ byteorder.BEPutUint64(b[:], l[3])
}
// p256Add sets res = x + y.
if runtime.GOARCH == "s390x" {
var newTable [43 * 32 * 2 * 4]uint64
for i, x := range (*[43 * 32 * 2 * 4][8]byte)(p256PrecomputedPtr) {
- newTable[i] = byteorder.LeUint64(x[:])
+ newTable[i] = byteorder.LEUint64(x[:])
}
p256PrecomputedPtr = unsafe.Pointer(&newTable)
}
import (
"bytes"
- "crypto/internal/nistec/fiat"
+ "crypto/internal/fips/nistec/fiat"
"fmt"
"testing"
)
package nistec
import (
- "crypto/internal/nistec/fiat"
- "crypto/subtle"
+ "crypto/internal/fips/nistec/fiat"
+ "crypto/internal/fips/subtle"
"errors"
"sync"
)
package nistec
import (
- "crypto/internal/nistec/fiat"
- "crypto/subtle"
+ "crypto/internal/fips/nistec/fiat"
+ "crypto/internal/fips/subtle"
"errors"
"sync"
)
//go:build (amd64 || arm64) && !purego
-package nistec_test
+package fipstest
import (
"bytes"
"crypto/elliptic"
- "crypto/internal/nistec"
+ "crypto/internal/fips/nistec"
"math/big"
"testing"
)
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package nistec_test
+package fipstest
import (
"bytes"
"crypto/elliptic"
"crypto/internal/cryptotest"
- "crypto/internal/nistec"
+ "crypto/internal/fips/nistec"
"fmt"
"math/big"
"math/rand"
"testing"
)
-func TestAllocations(t *testing.T) {
+func TestNISTECAllocations(t *testing.T) {
cryptotest.SkipTestAllocations(t)
t.Run("P224", func(t *testing.T) {
if allocs := testing.AllocsPerRun(10, func() {
t.Fatal(err)
}
}
-
-func BenchmarkScalarMult(b *testing.B) {
- b.Run("P224", func(b *testing.B) {
- benchmarkScalarMult(b, nistec.NewP224Point().SetGenerator(), 28)
- })
- b.Run("P256", func(b *testing.B) {
- benchmarkScalarMult(b, nistec.NewP256Point().SetGenerator(), 32)
- })
- b.Run("P384", func(b *testing.B) {
- benchmarkScalarMult(b, nistec.NewP384Point().SetGenerator(), 48)
- })
- b.Run("P521", func(b *testing.B) {
- benchmarkScalarMult(b, nistec.NewP521Point().SetGenerator(), 66)
- })
-}
-
-func benchmarkScalarMult[P nistPoint[P]](b *testing.B, p P, scalarSize int) {
- scalar := make([]byte, scalarSize)
- rand.Read(scalar)
- b.ReportAllocs()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- p.ScalarMult(p, scalar)
- }
-}
-
-func BenchmarkScalarBaseMult(b *testing.B) {
- b.Run("P224", func(b *testing.B) {
- benchmarkScalarBaseMult(b, nistec.NewP224Point().SetGenerator(), 28)
- })
- b.Run("P256", func(b *testing.B) {
- benchmarkScalarBaseMult(b, nistec.NewP256Point().SetGenerator(), 32)
- })
- b.Run("P384", func(b *testing.B) {
- benchmarkScalarBaseMult(b, nistec.NewP384Point().SetGenerator(), 48)
- })
- b.Run("P521", func(b *testing.B) {
- benchmarkScalarBaseMult(b, nistec.NewP521Point().SetGenerator(), 66)
- })
-}
-
-func benchmarkScalarBaseMult[P nistPoint[P]](b *testing.B, p P, scalarSize int) {
- scalar := make([]byte, scalarSize)
- rand.Read(scalar)
- b.ReportAllocs()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- p.ScalarBaseMult(scalar)
- }
-}
< crypto/internal/fips/ssh
< crypto/internal/fips/tls12
< crypto/internal/fips/tls13
+ < crypto/internal/fips/nistec/fiat
+ < crypto/internal/fips/nistec
< FIPS;
FIPS < crypto/internal/fips/check/checktest;
crypto/internal/fips/alias, math/rand/v2,
crypto/subtle, embed
< crypto/internal/randutil
- < crypto/internal/nistec/fiat
- < crypto/internal/nistec
< crypto/internal/edwards25519/field
< crypto/internal/edwards25519;