Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdce021fa6 | ||
|
|
d9474cc485 | ||
|
|
7626c50256 | ||
|
|
0471e7969e | ||
|
|
6435c37ab6 | ||
|
|
7d695da948 | ||
|
|
5b7f841599 | ||
|
|
0b316e7ee4 | ||
|
|
55aec0a86b | ||
|
|
5f2de1a9f1 | ||
|
|
45460e0797 | ||
|
|
d37c95e27d | ||
|
|
e2ffffe738 | ||
|
|
60e158ad30 | ||
|
|
1b77d23dc8 | ||
|
|
3872a2bc69 | ||
|
|
9f72eccdf6 | ||
|
|
8f405a4109 | ||
|
|
bb41b3dbe6 | ||
|
|
e04e721997 | ||
|
|
b315afd69a | ||
|
|
d4a85f4749 | ||
|
|
6f39c5226a | ||
|
|
4c4d20b72c | ||
|
|
e3e62d9601 | ||
|
|
5adb68b3e6 | ||
|
|
9beb694f97 | ||
|
|
e5306b2f3e | ||
|
|
d2fe592df1 | ||
|
|
d59570d1a3 | ||
|
|
4aab0d9d6a | ||
|
|
a1c0d99298 | ||
|
|
3c7c86938f | ||
|
|
533fb3f7e4 | ||
|
|
abbc44d451 | ||
|
|
e052873987 | ||
|
|
b61cf853a8 | ||
|
|
9c2cd33e8d | ||
|
|
890731877d | ||
|
|
ffd87b4878 | ||
|
|
4e7a7384ec | ||
|
|
b25012b37b | ||
|
|
6c195c8a97 | ||
|
|
f717e29698 | ||
|
|
e7c36ccb47 | ||
|
|
0fb843a472 | ||
|
|
e3d1254f1e | ||
|
|
a1ce0fee12 | ||
|
|
a749d17980 | ||
|
|
7ee59707ff | ||
|
|
44decbfe70 | ||
|
|
b8a14a8d65 | ||
|
|
9d9d507896 | ||
|
|
fd0b90d21f | ||
|
|
b9e53593a6 | ||
|
|
cc0e4fc1d4 | ||
|
|
a8e9237a21 | ||
|
|
03ca0dcccb | ||
|
|
8400f4a938 | ||
|
|
81c6cb34a8 | ||
|
|
982eaa62df | ||
|
|
159944f128 | ||
|
|
a408498e55 | ||
|
|
cab0f71854 | ||
|
|
2f26647a79 | ||
|
|
e08b06753d | ||
|
|
7d0074ccc6 | ||
|
|
506e022208 | ||
|
|
7dacc380ba | ||
|
|
19acf81bd7 | ||
|
|
3a1c6b4b61 | ||
|
|
f4602e4040 | ||
|
|
4e0068c009 | ||
|
|
e79546e28b | ||
|
|
f91f7a7c31 | ||
|
|
2df4153a03 | ||
|
|
bcf6a849ef | ||
|
|
b4f2b62076 | ||
|
|
79ec3a51fc | ||
|
|
122a78f140 | ||
|
|
c0531f9c34 | ||
|
|
0997000b45 | ||
|
|
017a1aaa2d | ||
|
|
cf29fa96f8 | ||
|
|
0b7aa0cfb0 | ||
|
|
1faea2975c | ||
|
|
627cb894b6 | ||
|
|
dca4914afe | ||
|
|
1336e21bd6 | ||
|
|
2beaa59a3c | ||
|
|
66c3d8ce71 | ||
|
|
ddb4e80c6a | ||
|
|
f4d47b0db5 | ||
|
|
96dc232fbd | ||
|
|
8c9ba31836 | ||
|
|
559e062ce8 | ||
|
|
5307a0ce6d | ||
|
|
9d779377cf | ||
|
|
8f580defa0 | ||
|
|
a4d1237429 | ||
|
|
b8d8dae13d | ||
|
|
f5a2eabcab | ||
|
|
44ecf3af99 | ||
|
|
ef5341b706 | ||
|
|
b999374650 | ||
|
|
c247dead11 | ||
|
|
1fda73153f | ||
|
|
1b4c3d2e8c | ||
|
|
b903b535d3 | ||
|
|
459a9db11b | ||
|
|
74e709ad8a | ||
|
|
b3790b8d91 | ||
|
|
1dc4269656 | ||
|
|
97bf787255 | ||
|
|
952517d181 | ||
|
|
c6fce02826 | ||
|
|
0ae49b8145 | ||
|
|
3bf9d2afd4 | ||
|
|
9bab96736c | ||
|
|
4f9f0ca9fc | ||
|
|
eac7cf0d78 | ||
|
|
18228cd6f1 | ||
|
|
73f6362471 | ||
|
|
ebc8e46315 | ||
|
|
e944286e33 | ||
|
|
78a1fd7893 | ||
|
|
ac58737d25 | ||
|
|
9dbbcf002b | ||
|
|
9f6bf8449a | ||
|
|
aae6e61070 | ||
|
|
9c1aa6af88 | ||
|
|
88199028d7 | ||
|
|
3f311e442e | ||
|
|
1f7c62cd66 | ||
|
|
a5f804889e | ||
|
|
958cde86ef | ||
|
|
51f005cfd4 | ||
|
|
7c358664da | ||
|
|
0091fc8e7c | ||
|
|
388684e50b | ||
|
|
953e809589 | ||
|
|
18f770732f | ||
|
|
2ebaafcdf5 | ||
|
|
4bc0711281 | ||
|
|
c96bba2ced | ||
|
|
6b13eef40b | ||
|
|
959f8f3db0 | ||
|
|
769bcd6997 | ||
|
|
d0a798f774 | ||
|
|
acbcbef23f | ||
|
|
376eb14006 | ||
|
|
b369b723c8 | ||
|
|
6b853fbea3 | ||
|
|
49bf5b80c8 | ||
|
|
24852b6b3f | ||
|
|
bbc689cf5c | ||
|
|
7292932d45 | ||
|
|
f66f74b0a4 | ||
|
|
b0784b7bfb | ||
|
|
911360c8a4 | ||
|
|
89ff08d67c | ||
|
|
e47973b1c1 | ||
|
|
9290511cd2 | ||
|
|
fa5273e461 | ||
|
|
a8ea4be81f | ||
|
|
71d3a4cfdb | ||
|
|
8929309228 | ||
|
|
4a75ba54c2 | ||
|
|
b4f1988a35 | ||
|
|
7042ebcbe0 | ||
|
|
3e90321ac7 | ||
|
|
8c4e668694 | ||
|
|
6018723c74 | ||
|
|
71ed71b4fa | ||
|
|
750a45fe5e | ||
|
|
36b172546b | ||
|
|
80ea76eb17 | ||
|
|
c17aa50fbd | ||
|
|
7cfb9161e8 | ||
|
|
b61b08db44 | ||
|
|
6c21748958 | ||
|
|
adef4cc1a8 | ||
|
|
a0819fbb02 | ||
|
|
42ee18b963 | ||
|
|
9e92970a1e | ||
|
|
c9da6b9a40 | ||
|
|
b35ab4fde0 | ||
|
|
bcb0f91bbc | ||
|
|
7eace71069 | ||
|
|
620dfbc770 | ||
|
|
82942cf1d8 | ||
|
|
0484c26df7 | ||
|
|
38ed1bc0ec | ||
|
|
38a0b5da75 | ||
|
|
bf5f14f545 | ||
|
|
b2d3a6a4b4 |
@@ -2,20 +2,17 @@
|
||||
|
||||
[](https://pkg.go.dev/golang.org/x/crypto)
|
||||
|
||||
This repository holds supplementary Go cryptography libraries.
|
||||
|
||||
## Download/Install
|
||||
|
||||
The easiest way to install is to run `go get -u golang.org/x/crypto/...`. You
|
||||
can also manually git clone the repository to `$GOPATH/src/golang.org/x/crypto`.
|
||||
This repository holds supplementary Go cryptography packages.
|
||||
|
||||
## Report Issues / Send Patches
|
||||
|
||||
This repository uses Gerrit for code changes. To learn how to submit changes to
|
||||
this repository, see https://golang.org/doc/contribute.html.
|
||||
this repository, see https://go.dev/doc/contribute.
|
||||
|
||||
The git repository is https://go.googlesource.com/crypto.
|
||||
|
||||
The main issue tracker for the crypto repository is located at
|
||||
https://github.com/golang/go/issues. Prefix your issue with "x/crypto:" in the
|
||||
https://go.dev/issues. Prefix your issue with "x/crypto:" in the
|
||||
subject line, so it is easy to find.
|
||||
|
||||
Note that contributions to the cryptography package receive additional scrutiny
|
||||
|
||||
+46
-56
@@ -31,12 +31,11 @@ import (
|
||||
"crypto/x509/pkix"
|
||||
"encoding/asn1"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -353,6 +352,10 @@ func (c *Client) authorize(ctx context.Context, typ, val string) (*Authorization
|
||||
if _, err := c.Discover(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.dir.AuthzURL == "" {
|
||||
// Pre-Authorization is unsupported
|
||||
return nil, errPreAuthorizationNotSupported
|
||||
}
|
||||
|
||||
type authzID struct {
|
||||
Type string `json:"type"`
|
||||
@@ -467,7 +470,7 @@ func (c *Client) WaitAuthorization(ctx context.Context, url string) (*Authorizat
|
||||
// while waiting for a final authorization status.
|
||||
d := retryAfter(res.Header.Get("Retry-After"))
|
||||
if d == 0 {
|
||||
// Given that the fastest challenges TLS-SNI and HTTP-01
|
||||
// Given that the fastest challenges TLS-ALPN and HTTP-01
|
||||
// require a CA to make at least 1 network round trip
|
||||
// and most likely persist a challenge state,
|
||||
// this default delay seems reasonable.
|
||||
@@ -514,7 +517,11 @@ func (c *Client) Accept(ctx context.Context, chal *Challenge) (*Challenge, error
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res, err := c.post(ctx, nil, chal.URI, json.RawMessage("{}"), wantStatus(
|
||||
payload := json.RawMessage("{}")
|
||||
if len(chal.Payload) != 0 {
|
||||
payload = chal.Payload
|
||||
}
|
||||
res, err := c.post(ctx, nil, chal.URI, payload, wantStatus(
|
||||
http.StatusOK, // according to the spec
|
||||
http.StatusAccepted, // Let's Encrypt: see https://goo.gl/WsJ7VT (acme-divergences.md)
|
||||
))
|
||||
@@ -564,50 +571,28 @@ func (c *Client) HTTP01ChallengePath(token string) string {
|
||||
}
|
||||
|
||||
// TLSSNI01ChallengeCert creates a certificate for TLS-SNI-01 challenge response.
|
||||
// Always returns an error.
|
||||
//
|
||||
// Deprecated: This challenge type is unused in both draft-02 and RFC versions of the ACME spec.
|
||||
func (c *Client) TLSSNI01ChallengeCert(token string, opt ...CertOption) (cert tls.Certificate, name string, err error) {
|
||||
ka, err := keyAuth(c.Key.Public(), token)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, "", err
|
||||
}
|
||||
b := sha256.Sum256([]byte(ka))
|
||||
h := hex.EncodeToString(b[:])
|
||||
name = fmt.Sprintf("%s.%s.acme.invalid", h[:32], h[32:])
|
||||
cert, err = tlsChallengeCert([]string{name}, opt)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, "", err
|
||||
}
|
||||
return cert, name, nil
|
||||
// Deprecated: This challenge type was only present in pre-standardized ACME
|
||||
// protocol drafts and is insecure for use in shared hosting environments.
|
||||
func (c *Client) TLSSNI01ChallengeCert(token string, opt ...CertOption) (tls.Certificate, string, error) {
|
||||
return tls.Certificate{}, "", errPreRFC
|
||||
}
|
||||
|
||||
// TLSSNI02ChallengeCert creates a certificate for TLS-SNI-02 challenge response.
|
||||
// Always returns an error.
|
||||
//
|
||||
// Deprecated: This challenge type is unused in both draft-02 and RFC versions of the ACME spec.
|
||||
func (c *Client) TLSSNI02ChallengeCert(token string, opt ...CertOption) (cert tls.Certificate, name string, err error) {
|
||||
b := sha256.Sum256([]byte(token))
|
||||
h := hex.EncodeToString(b[:])
|
||||
sanA := fmt.Sprintf("%s.%s.token.acme.invalid", h[:32], h[32:])
|
||||
|
||||
ka, err := keyAuth(c.Key.Public(), token)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, "", err
|
||||
}
|
||||
b = sha256.Sum256([]byte(ka))
|
||||
h = hex.EncodeToString(b[:])
|
||||
sanB := fmt.Sprintf("%s.%s.ka.acme.invalid", h[:32], h[32:])
|
||||
|
||||
cert, err = tlsChallengeCert([]string{sanA, sanB}, opt)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, "", err
|
||||
}
|
||||
return cert, sanA, nil
|
||||
// Deprecated: This challenge type was only present in pre-standardized ACME
|
||||
// protocol drafts and is insecure for use in shared hosting environments.
|
||||
func (c *Client) TLSSNI02ChallengeCert(token string, opt ...CertOption) (tls.Certificate, string, error) {
|
||||
return tls.Certificate{}, "", errPreRFC
|
||||
}
|
||||
|
||||
// TLSALPN01ChallengeCert creates a certificate for TLS-ALPN-01 challenge response.
|
||||
// Servers can present the certificate to validate the challenge and prove control
|
||||
// over a domain name. For more details on TLS-ALPN-01 see
|
||||
// https://tools.ietf.org/html/draft-shoemaker-acme-tls-alpn-00#section-3
|
||||
// over an identifier (either a DNS name or the textual form of an IPv4 or IPv6
|
||||
// address). For more details on TLS-ALPN-01 see
|
||||
// https://www.rfc-editor.org/rfc/rfc8737 and https://www.rfc-editor.org/rfc/rfc8738
|
||||
//
|
||||
// The token argument is a Challenge.Token value.
|
||||
// If a WithKey option is provided, its private part signs the returned cert,
|
||||
@@ -615,9 +600,13 @@ func (c *Client) TLSSNI02ChallengeCert(token string, opt ...CertOption) (cert tl
|
||||
// If no WithKey option is provided, a new ECDSA key is generated using P-256 curve.
|
||||
//
|
||||
// The returned certificate is valid for the next 24 hours and must be presented only when
|
||||
// the server name in the TLS ClientHello matches the domain, and the special acme-tls/1 ALPN protocol
|
||||
// the server name in the TLS ClientHello matches the identifier, and the special acme-tls/1 ALPN protocol
|
||||
// has been specified.
|
||||
func (c *Client) TLSALPN01ChallengeCert(token, domain string, opt ...CertOption) (cert tls.Certificate, err error) {
|
||||
//
|
||||
// Validation requests for IP address identifiers will use the reverse DNS form in the server name
|
||||
// in the TLS ClientHello since the SNI extension is not supported for IP addresses.
|
||||
// See RFC 8738 Section 6 for more information.
|
||||
func (c *Client) TLSALPN01ChallengeCert(token, identifier string, opt ...CertOption) (cert tls.Certificate, err error) {
|
||||
ka, err := keyAuth(c.Key.Public(), token)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, err
|
||||
@@ -647,7 +636,7 @@ func (c *Client) TLSALPN01ChallengeCert(token, domain string, opt ...CertOption)
|
||||
}
|
||||
tmpl.ExtraExtensions = append(tmpl.ExtraExtensions, acmeExtension)
|
||||
newOpt = append(newOpt, WithTemplate(tmpl))
|
||||
return tlsChallengeCert([]string{domain}, newOpt)
|
||||
return tlsChallengeCert(identifier, newOpt)
|
||||
}
|
||||
|
||||
// popNonce returns a nonce value previously stored with c.addNonce
|
||||
@@ -701,7 +690,7 @@ func (c *Client) addNonce(h http.Header) {
|
||||
}
|
||||
|
||||
func (c *Client) fetchNonce(ctx context.Context, url string) (string, error) {
|
||||
r, err := http.NewRequest("HEAD", url, nil)
|
||||
r, err := http.NewRequestWithContext(ctx, "HEAD", url, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -765,11 +754,15 @@ func defaultTLSChallengeCertTemplate() *x509.Certificate {
|
||||
}
|
||||
}
|
||||
|
||||
// tlsChallengeCert creates a temporary certificate for TLS-SNI challenges
|
||||
// with the given SANs and auto-generated public/private key pair.
|
||||
// The Subject Common Name is set to the first SAN to aid debugging.
|
||||
// tlsChallengeCert creates a temporary certificate for TLS-ALPN challenges
|
||||
// for the given identifier, using an auto-generated public/private key pair.
|
||||
//
|
||||
// If the provided identifier is a domain name, it will be used as a DNS type SAN and for the
|
||||
// subject common name. If the provided identifier is an IP address it will be used as an IP type
|
||||
// SAN.
|
||||
//
|
||||
// To create a cert with a custom key pair, specify WithKey option.
|
||||
func tlsChallengeCert(san []string, opt []CertOption) (tls.Certificate, error) {
|
||||
func tlsChallengeCert(identifier string, opt []CertOption) (tls.Certificate, error) {
|
||||
var key crypto.Signer
|
||||
tmpl := defaultTLSChallengeCertTemplate()
|
||||
for _, o := range opt {
|
||||
@@ -793,9 +786,12 @@ func tlsChallengeCert(san []string, opt []CertOption) (tls.Certificate, error) {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
}
|
||||
tmpl.DNSNames = san
|
||||
if len(san) > 0 {
|
||||
tmpl.Subject.CommonName = san[0]
|
||||
|
||||
if ip := net.ParseIP(identifier); ip != nil {
|
||||
tmpl.IPAddresses = []net.IP{ip}
|
||||
} else {
|
||||
tmpl.DNSNames = []string{identifier}
|
||||
tmpl.Subject.CommonName = identifier
|
||||
}
|
||||
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, key.Public(), key)
|
||||
@@ -808,11 +804,5 @@ func tlsChallengeCert(san []string, opt []CertOption) (tls.Certificate, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// encodePEM returns b encoded as PEM with block of type typ.
|
||||
func encodePEM(typ string, b []byte) []byte {
|
||||
pb := &pem.Block{Type: typ, Bytes: b}
|
||||
return pem.EncodeToMemory(pb)
|
||||
}
|
||||
|
||||
// timeNow is time.Now, except in tests which can mess with it.
|
||||
var timeNow = time.Now
|
||||
|
||||
+87
-111
@@ -9,20 +9,18 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -254,6 +252,57 @@ func TestAuthorizeValid(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeUnsupported(t *testing.T) {
|
||||
const (
|
||||
nonce = "https://example.com/acme/new-nonce"
|
||||
reg = "https://example.com/acme/new-acct"
|
||||
order = "https://example.com/acme/new-order"
|
||||
revoke = "https://example.com/acme/revoke-cert"
|
||||
keychange = "https://example.com/acme/key-change"
|
||||
metaTerms = "https://example.com/acme/terms/2017-5-30"
|
||||
metaWebsite = "https://www.example.com/"
|
||||
metaCAA = "example.com"
|
||||
)
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Replay-Nonce", "nonce")
|
||||
if r.Method == http.MethodHead {
|
||||
return
|
||||
}
|
||||
switch r.URL.Path {
|
||||
case "/": // Directory
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprintf(w, `{
|
||||
"newNonce": %q,
|
||||
"newAccount": %q,
|
||||
"newOrder": %q,
|
||||
"revokeCert": %q,
|
||||
"keyChange": %q,
|
||||
"meta": {
|
||||
"termsOfService": %q,
|
||||
"website": %q,
|
||||
"caaIdentities": [%q],
|
||||
"externalAccountRequired": true
|
||||
}
|
||||
}`, nonce, reg, order, revoke, keychange, metaTerms, metaWebsite, metaCAA)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
case "/acme/new-authz":
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
client := &Client{Key: testKey, DirectoryURL: ts.URL}
|
||||
dir, err := client.Discover(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dir.AuthzURL != "" {
|
||||
t.Fatalf("expected AuthzURL to be empty, got %q", dir.AuthzURL)
|
||||
}
|
||||
if _, err := client.Authorize(context.Background(), "example.com"); !errors.Is(err, errPreAuthorizationNotSupported) {
|
||||
t.Errorf("expected err to indicate pre-authorization is unsupported, got %+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWaitAuthorization(t *testing.T) {
|
||||
t.Run("wait loop", func(t *testing.T) {
|
||||
var count int
|
||||
@@ -640,71 +689,6 @@ func TestLinkHeader(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTLSSNI01ChallengeCert(t *testing.T) {
|
||||
const (
|
||||
token = "evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA"
|
||||
// echo -n <token.testKeyECThumbprint> | shasum -a 256
|
||||
san = "dbbd5eefe7b4d06eb9d1d9f5acb4c7cd.a27d320e4b30332f0b6cb441734ad7b0.acme.invalid"
|
||||
)
|
||||
|
||||
tlscert, name, err := newTestClient().TLSSNI01ChallengeCert(token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if n := len(tlscert.Certificate); n != 1 {
|
||||
t.Fatalf("len(tlscert.Certificate) = %d; want 1", n)
|
||||
}
|
||||
cert, err := x509.ParseCertificate(tlscert.Certificate[0])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(cert.DNSNames) != 1 || cert.DNSNames[0] != san {
|
||||
t.Fatalf("cert.DNSNames = %v; want %q", cert.DNSNames, san)
|
||||
}
|
||||
if cert.DNSNames[0] != name {
|
||||
t.Errorf("cert.DNSNames[0] != name: %q vs %q", cert.DNSNames[0], name)
|
||||
}
|
||||
if cn := cert.Subject.CommonName; cn != san {
|
||||
t.Errorf("cert.Subject.CommonName = %q; want %q", cn, san)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTLSSNI02ChallengeCert(t *testing.T) {
|
||||
const (
|
||||
token = "evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA"
|
||||
// echo -n evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA | shasum -a 256
|
||||
sanA = "7ea0aaa69214e71e02cebb18bb867736.09b730209baabf60e43d4999979ff139.token.acme.invalid"
|
||||
// echo -n <token.testKeyECThumbprint> | shasum -a 256
|
||||
sanB = "dbbd5eefe7b4d06eb9d1d9f5acb4c7cd.a27d320e4b30332f0b6cb441734ad7b0.ka.acme.invalid"
|
||||
)
|
||||
|
||||
tlscert, name, err := newTestClient().TLSSNI02ChallengeCert(token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if n := len(tlscert.Certificate); n != 1 {
|
||||
t.Fatalf("len(tlscert.Certificate) = %d; want 1", n)
|
||||
}
|
||||
cert, err := x509.ParseCertificate(tlscert.Certificate[0])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
names := []string{sanA, sanB}
|
||||
if !reflect.DeepEqual(cert.DNSNames, names) {
|
||||
t.Fatalf("cert.DNSNames = %v;\nwant %v", cert.DNSNames, names)
|
||||
}
|
||||
sort.Strings(cert.DNSNames)
|
||||
i := sort.SearchStrings(cert.DNSNames, name)
|
||||
if i >= len(cert.DNSNames) || cert.DNSNames[i] != name {
|
||||
t.Errorf("%v doesn't have %q", cert.DNSNames, name)
|
||||
}
|
||||
if cn := cert.Subject.CommonName; cn != sanA {
|
||||
t.Errorf("CommonName = %q; want %q", cn, sanA)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTLSALPN01ChallengeCert(t *testing.T) {
|
||||
const (
|
||||
token = "evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA"
|
||||
@@ -757,10 +741,11 @@ func TestTLSALPN01ChallengeCert(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTLSChallengeCertOpt(t *testing.T) {
|
||||
key, err := rsa.GenerateKey(rand.Reader, 512)
|
||||
key, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
domain := "example.com"
|
||||
tmpl := &x509.Certificate{
|
||||
SerialNumber: big.NewInt(2),
|
||||
Subject: pkix.Name{Organization: []string{"Test"}},
|
||||
@@ -769,52 +754,43 @@ func TestTLSChallengeCertOpt(t *testing.T) {
|
||||
opts := []CertOption{WithKey(key), WithTemplate(tmpl)}
|
||||
|
||||
client := newTestClient()
|
||||
cert1, _, err := client.TLSSNI01ChallengeCert("token", opts...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cert2, _, err := client.TLSSNI02ChallengeCert("token", opts...)
|
||||
cert, err := client.TLSALPN01ChallengeCert("token", domain, opts...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i, tlscert := range []tls.Certificate{cert1, cert2} {
|
||||
// verify generated cert private key
|
||||
tlskey, ok := tlscert.PrivateKey.(*rsa.PrivateKey)
|
||||
if !ok {
|
||||
t.Errorf("%d: tlscert.PrivateKey is %T; want *rsa.PrivateKey", i, tlscert.PrivateKey)
|
||||
continue
|
||||
}
|
||||
if tlskey.D.Cmp(key.D) != 0 {
|
||||
t.Errorf("%d: tlskey.D = %v; want %v", i, tlskey.D, key.D)
|
||||
}
|
||||
// verify generated cert public key
|
||||
x509Cert, err := x509.ParseCertificate(tlscert.Certificate[0])
|
||||
if err != nil {
|
||||
t.Errorf("%d: %v", i, err)
|
||||
continue
|
||||
}
|
||||
tlspub, ok := x509Cert.PublicKey.(*rsa.PublicKey)
|
||||
if !ok {
|
||||
t.Errorf("%d: x509Cert.PublicKey is %T; want *rsa.PublicKey", i, x509Cert.PublicKey)
|
||||
continue
|
||||
}
|
||||
if tlspub.N.Cmp(key.N) != 0 {
|
||||
t.Errorf("%d: tlspub.N = %v; want %v", i, tlspub.N, key.N)
|
||||
}
|
||||
// verify template option
|
||||
sn := big.NewInt(2)
|
||||
if x509Cert.SerialNumber.Cmp(sn) != 0 {
|
||||
t.Errorf("%d: SerialNumber = %v; want %v", i, x509Cert.SerialNumber, sn)
|
||||
}
|
||||
org := []string{"Test"}
|
||||
if !reflect.DeepEqual(x509Cert.Subject.Organization, org) {
|
||||
t.Errorf("%d: Subject.Organization = %+v; want %+v", i, x509Cert.Subject.Organization, org)
|
||||
}
|
||||
for _, v := range x509Cert.DNSNames {
|
||||
if !strings.HasSuffix(v, ".acme.invalid") {
|
||||
t.Errorf("%d: invalid DNSNames element: %q", i, v)
|
||||
}
|
||||
// verify generated cert private key
|
||||
tlskey, ok := cert.PrivateKey.(*rsa.PrivateKey)
|
||||
if !ok {
|
||||
t.Fatalf("tlscert.PrivateKey is %T; want *rsa.PrivateKey", cert.PrivateKey)
|
||||
}
|
||||
if tlskey.D.Cmp(key.D) != 0 {
|
||||
t.Errorf("tlskey.D = %v; want %v", tlskey.D, key.D)
|
||||
}
|
||||
// verify generated cert public key
|
||||
x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tlspub, ok := x509Cert.PublicKey.(*rsa.PublicKey)
|
||||
if !ok {
|
||||
t.Fatalf("x509Cert.PublicKey is %T; want *rsa.PublicKey", x509Cert.PublicKey)
|
||||
}
|
||||
if tlspub.N.Cmp(key.N) != 0 {
|
||||
t.Errorf("tlspub.N = %v; want %v", tlspub.N, key.N)
|
||||
}
|
||||
// verify template option
|
||||
sn := big.NewInt(2)
|
||||
if x509Cert.SerialNumber.Cmp(sn) != 0 {
|
||||
t.Errorf("SerialNumber = %v; want %v", x509Cert.SerialNumber, sn)
|
||||
}
|
||||
org := []string{"Test"}
|
||||
if !reflect.DeepEqual(x509Cert.Subject.Organization, org) {
|
||||
t.Errorf("Subject.Organization = %+v; want %+v", x509Cert.Subject.Organization, org)
|
||||
}
|
||||
for _, v := range x509Cert.DNSNames {
|
||||
if v != domain {
|
||||
t.Errorf("invalid DNSNames element: %q", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+31
-42
@@ -134,7 +134,8 @@ type Manager struct {
|
||||
// RenewBefore optionally specifies how early certificates should
|
||||
// be renewed before they expire.
|
||||
//
|
||||
// If zero, they're renewed 30 days before expiration.
|
||||
// If zero, they're renewed at the lesser of 30 days or
|
||||
// 1/3 of the certificate lifetime.
|
||||
RenewBefore time.Duration
|
||||
|
||||
// Client is used to perform low-level operations, such as account registration
|
||||
@@ -247,10 +248,6 @@ func (m *Manager) TLSConfig() *tls.Config {
|
||||
// If GetCertificate is used directly, instead of via Manager.TLSConfig, package users will
|
||||
// also have to add acme.ALPNProto to NextProtos for tls-alpn-01, or use HTTPHandler for http-01.
|
||||
func (m *Manager) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
||||
if m.Prompt == nil {
|
||||
return nil, errors.New("acme/autocert: Manager.Prompt not set")
|
||||
}
|
||||
|
||||
name := hello.ServerName
|
||||
if name == "" {
|
||||
return nil, errors.New("acme/autocert: missing server name")
|
||||
@@ -292,6 +289,10 @@ func (m *Manager) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate,
|
||||
}
|
||||
|
||||
// regular domain
|
||||
if err := m.hostPolicy()(ctx, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ck := certKey{
|
||||
domain: strings.TrimSuffix(name, "."), // golang.org/issue/18114
|
||||
isRSA: !supportsECDSA(hello),
|
||||
@@ -305,9 +306,6 @@ func (m *Manager) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate,
|
||||
}
|
||||
|
||||
// first-time
|
||||
if err := m.hostPolicy()(ctx, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cert, err = m.createCert(ctx, ck)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -463,7 +461,7 @@ func (m *Manager) cert(ctx context.Context, ck certKey) (*tls.Certificate, error
|
||||
leaf: cert.Leaf,
|
||||
}
|
||||
m.state[ck] = s
|
||||
m.startRenew(ck, s.key, s.leaf.NotAfter)
|
||||
m.startRenew(ck, s.key, s.leaf.NotBefore, s.leaf.NotAfter)
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
@@ -566,24 +564,21 @@ func encodeECDSAKey(w io.Writer, key *ecdsa.PrivateKey) error {
|
||||
// If the domain is already being verified, it waits for the existing verification to complete.
|
||||
// Either way, createCert blocks for the duration of the whole process.
|
||||
func (m *Manager) createCert(ctx context.Context, ck certKey) (*tls.Certificate, error) {
|
||||
// TODO: maybe rewrite this whole piece using sync.Once
|
||||
state, err := m.certState(ck)
|
||||
state, owner, err := m.certState(ck)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// state may exist if another goroutine is already working on it
|
||||
// in which case just wait for it to finish
|
||||
if !state.locked {
|
||||
// If another goroutine is already working on this state, wait for it
|
||||
// to finish by taking the read lock
|
||||
if !owner {
|
||||
state.RLock()
|
||||
defer state.RUnlock()
|
||||
return state.tlscert()
|
||||
}
|
||||
|
||||
// We are the first; state is locked.
|
||||
// Unblock the readers when domain ownership is verified
|
||||
// and we got the cert or the process failed.
|
||||
// We are the first to work on this certKey, so state is write-locked.
|
||||
// Unblock the readers when our work is complete.
|
||||
defer state.Unlock()
|
||||
state.locked = false
|
||||
|
||||
der, leaf, err := m.authorizedCert(ctx, state.key, ck)
|
||||
if err != nil {
|
||||
@@ -609,14 +604,19 @@ func (m *Manager) createCert(ctx context.Context, ck certKey) (*tls.Certificate,
|
||||
}
|
||||
state.cert = der
|
||||
state.leaf = leaf
|
||||
m.startRenew(ck, state.key, state.leaf.NotAfter)
|
||||
m.startRenew(ck, state.key, state.leaf.NotBefore, state.leaf.NotAfter)
|
||||
return state.tlscert()
|
||||
}
|
||||
|
||||
// certState returns a new or existing certState.
|
||||
// If a new certState is returned, state.exist is false and the state is locked.
|
||||
// certState returns a new or existing certState along with a boolean
|
||||
// indicating whether the caller is the owner of the state.
|
||||
//
|
||||
// The owner of the state is responsible for performing the ACME work and
|
||||
// must unlock the state's write lock when done. Non-owner callers should
|
||||
// wait on the state's read lock for the owner to finish.
|
||||
//
|
||||
// The returned error is non-nil only in the case where a new state could not be created.
|
||||
func (m *Manager) certState(ck certKey) (*certState, error) {
|
||||
func (m *Manager) certState(ck certKey) (*certState, bool, error) {
|
||||
m.stateMu.Lock()
|
||||
defer m.stateMu.Unlock()
|
||||
if m.state == nil {
|
||||
@@ -624,7 +624,7 @@ func (m *Manager) certState(ck certKey) (*certState, error) {
|
||||
}
|
||||
// existing state
|
||||
if state, ok := m.state[ck]; ok {
|
||||
return state, nil
|
||||
return state, false, nil
|
||||
}
|
||||
|
||||
// new locked state
|
||||
@@ -638,16 +638,13 @@ func (m *Manager) certState(ck certKey) (*certState, error) {
|
||||
key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
state := &certState{
|
||||
key: key,
|
||||
locked: true,
|
||||
}
|
||||
state := &certState{key: key}
|
||||
state.Lock() // will be unlocked by m.certState caller
|
||||
m.state[ck] = state
|
||||
return state, nil
|
||||
return state, true, nil
|
||||
}
|
||||
|
||||
// authorizedCert starts the domain ownership verification process and requests a new cert upon success.
|
||||
@@ -907,7 +904,7 @@ func httpTokenCacheKey(tokenPath string) string {
|
||||
//
|
||||
// The key argument is a certificate private key.
|
||||
// The exp argument is the cert expiration time (NotAfter).
|
||||
func (m *Manager) startRenew(ck certKey, key crypto.Signer, exp time.Time) {
|
||||
func (m *Manager) startRenew(ck certKey, key crypto.Signer, notBefore, notAfter time.Time) {
|
||||
m.renewalMu.Lock()
|
||||
defer m.renewalMu.Unlock()
|
||||
if m.renewal[ck] != nil {
|
||||
@@ -919,7 +916,7 @@ func (m *Manager) startRenew(ck certKey, key crypto.Signer, exp time.Time) {
|
||||
}
|
||||
dr := &domainRenewal{m: m, ck: ck, key: key}
|
||||
m.renewal[ck] = dr
|
||||
dr.start(exp)
|
||||
dr.start(notBefore, notAfter)
|
||||
}
|
||||
|
||||
// stopRenew stops all currently running cert renewal timers.
|
||||
@@ -1027,13 +1024,6 @@ func (m *Manager) hostPolicy() HostPolicy {
|
||||
return defaultHostPolicy
|
||||
}
|
||||
|
||||
func (m *Manager) renewBefore() time.Duration {
|
||||
if m.RenewBefore > renewJitter {
|
||||
return m.RenewBefore
|
||||
}
|
||||
return 720 * time.Hour // 30 days
|
||||
}
|
||||
|
||||
func (m *Manager) now() time.Time {
|
||||
if m.nowFunc != nil {
|
||||
return m.nowFunc()
|
||||
@@ -1044,10 +1034,9 @@ func (m *Manager) now() time.Time {
|
||||
// certState is ready when its mutex is unlocked for reading.
|
||||
type certState struct {
|
||||
sync.RWMutex
|
||||
locked bool // locked for read/write
|
||||
key crypto.Signer // private key for cert
|
||||
cert [][]byte // DER encoding
|
||||
leaf *x509.Certificate // parsed cert[0]; always non-nil if cert != nil
|
||||
key crypto.Signer // private key for cert
|
||||
cert [][]byte // DER encoding
|
||||
leaf *x509.Certificate // parsed cert[0]; always non-nil if cert != nil
|
||||
}
|
||||
|
||||
// tlscert creates a tls.Certificate from s.key and s.cert.
|
||||
|
||||
@@ -201,7 +201,7 @@ func TestGetCertificate(t *testing.T) {
|
||||
prepare: func(t *testing.T, man *Manager, s *acmetest.CAServer) {
|
||||
man.Prompt = nil
|
||||
},
|
||||
expectError: "Manager.Prompt not set",
|
||||
expectError: "missing Manager.Prompt",
|
||||
},
|
||||
{
|
||||
name: "trailingDot",
|
||||
@@ -513,6 +513,33 @@ func TestGetCertificate_failedAttempt(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetCertificate_concurrent guards against a data race on certState
|
||||
// fields between the goroutine performing the ACME work for a domain and
|
||||
// the other goroutines waiting on the result. See golang/go#80119.
|
||||
func TestGetCertificate_concurrent(t *testing.T) {
|
||||
// An unreachable directory URL makes the owner goroutine fail
|
||||
// quickly inside createCert, so its writes overlap with the
|
||||
// non-owner goroutines reaching the same code path.
|
||||
m := &Manager{
|
||||
Prompt: AcceptTOS,
|
||||
Client: &acme.Client{DirectoryURL: "http://127.0.0.1:1/"},
|
||||
}
|
||||
defer m.stopRenew()
|
||||
|
||||
start := make(chan struct{})
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 64; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-start
|
||||
m.GetCertificate(clientHelloInfo(exampleDomain, algECDSA))
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestRevokeFailedAuthz(t *testing.T) {
|
||||
ca := acmetest.NewCAServer(t)
|
||||
// Make the authz unfulfillable on the client side, so it will be left
|
||||
@@ -561,9 +588,9 @@ func TestHTTPHandlerDefaultFallback(t *testing.T) {
|
||||
{"GET", "http://example.org/foo?a=b", 302, "https://example.org/foo?a=b"},
|
||||
{"GET", "http://example.org:80/foo?a=b", 302, "https://example.org:443/foo?a=b"},
|
||||
{"GET", "http://example.org:80/foo%20bar", 302, "https://example.org:443/foo%20bar"},
|
||||
{"GET", "http://[2602:d1:xxxx::c60a]:1234", 302, "https://[2602:d1:xxxx::c60a]:443/"},
|
||||
{"GET", "http://[2602:d1:xxxx::c60a]", 302, "https://[2602:d1:xxxx::c60a]/"},
|
||||
{"GET", "http://[2602:d1:xxxx::c60a]/foo?a=b", 302, "https://[2602:d1:xxxx::c60a]/foo?a=b"},
|
||||
{"GET", "http://[2602:d1:abcd::c60a]:1234", 302, "https://[2602:d1:abcd::c60a]:443/"},
|
||||
{"GET", "http://[2602:d1:abcd::c60a]", 302, "https://[2602:d1:abcd::c60a]/"},
|
||||
{"GET", "http://[2602:d1:abcd::c60a]/foo?a=b", 302, "https://[2602:d1:abcd::c60a]/foo?a=b"},
|
||||
{"HEAD", "http://example.org", 302, "https://example.org/"},
|
||||
{"HEAD", "http://example.org/foo", 302, "https://example.org/foo"},
|
||||
{"HEAD", "http://example.org/foo/bar/", 302, "https://example.org/foo/bar/"},
|
||||
@@ -619,7 +646,7 @@ func TestCache(t *testing.T) {
|
||||
PrivateKey: ecdsaKey,
|
||||
}
|
||||
|
||||
rsaKey, err := rsa.GenerateKey(rand.Reader, 512)
|
||||
rsaKey, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -694,7 +721,7 @@ func TestValidCert(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key3, err := rsa.GenerateKey(rand.Reader, 512)
|
||||
key3, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ func (ca *CAServer) URL() string {
|
||||
return ca.url
|
||||
}
|
||||
|
||||
// Roots returns a pool cointaining the CA root.
|
||||
// Roots returns a pool containing the CA root.
|
||||
func (ca *CAServer) Roots() *x509.CertPool {
|
||||
if ca.url == "" {
|
||||
panic("Roots called before Start")
|
||||
@@ -239,7 +239,8 @@ type discovery struct {
|
||||
}
|
||||
|
||||
type discoveryMeta struct {
|
||||
ExternalAccountRequired bool `json:"externalAccountRequired,omitempty"`
|
||||
Terms string `json:"termsOfService,omitempty"`
|
||||
ExternalAccountRequired bool `json:"externalAccountRequired,omitempty"`
|
||||
}
|
||||
|
||||
type challenge struct {
|
||||
@@ -281,6 +282,7 @@ func (ca *CAServer) handle(w http.ResponseWriter, r *http.Request) {
|
||||
NewAccount: ca.serverURL("/new-account"),
|
||||
NewOrder: ca.serverURL("/new-order"),
|
||||
Meta: discoveryMeta{
|
||||
Terms: ca.serverURL("/terms"),
|
||||
ExternalAccountRequired: ca.eabRequired,
|
||||
},
|
||||
}
|
||||
@@ -308,7 +310,7 @@ func (ca *CAServer) handle(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err := decodePayload(&req, r.Body); err != nil {
|
||||
ca.httpErrorf(w, http.StatusBadRequest, err.Error())
|
||||
ca.httpErrorf(w, http.StatusBadRequest, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -328,7 +330,7 @@ func (ca *CAServer) handle(w http.ResponseWriter, r *http.Request) {
|
||||
Identifiers []struct{ Value string }
|
||||
}
|
||||
if err := decodePayload(&req, r.Body); err != nil {
|
||||
ca.httpErrorf(w, http.StatusBadRequest, err.Error())
|
||||
ca.httpErrorf(w, http.StatusBadRequest, "%v", err)
|
||||
return
|
||||
}
|
||||
ca.mu.Lock()
|
||||
@@ -352,7 +354,7 @@ func (ca *CAServer) handle(w http.ResponseWriter, r *http.Request) {
|
||||
defer ca.mu.Unlock()
|
||||
o, err := ca.storedOrder(strings.TrimPrefix(r.URL.Path, "/orders/"))
|
||||
if err != nil {
|
||||
ca.httpErrorf(w, http.StatusBadRequest, err.Error())
|
||||
ca.httpErrorf(w, http.StatusBadRequest, "%v", err)
|
||||
return
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(o); err != nil {
|
||||
@@ -412,7 +414,7 @@ func (ca *CAServer) handle(w http.ResponseWriter, r *http.Request) {
|
||||
orderID := strings.TrimPrefix(r.URL.Path, "/new-cert/")
|
||||
o, err := ca.storedOrder(orderID)
|
||||
if err != nil {
|
||||
ca.httpErrorf(w, http.StatusBadRequest, err.Error())
|
||||
ca.httpErrorf(w, http.StatusBadRequest, "%v", err)
|
||||
return
|
||||
}
|
||||
if o.Status != acme.StatusReady {
|
||||
@@ -427,7 +429,7 @@ func (ca *CAServer) handle(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := base64.RawURLEncoding.DecodeString(req.CSR)
|
||||
csr, err := x509.ParseCertificateRequest(b)
|
||||
if err != nil {
|
||||
ca.httpErrorf(w, http.StatusBadRequest, err.Error())
|
||||
ca.httpErrorf(w, http.StatusBadRequest, "%v", err)
|
||||
return
|
||||
}
|
||||
// Issue the certificate.
|
||||
@@ -449,7 +451,7 @@ func (ca *CAServer) handle(w http.ResponseWriter, r *http.Request) {
|
||||
defer ca.mu.Unlock()
|
||||
o, err := ca.storedOrder(strings.TrimPrefix(r.URL.Path, "/issued-cert/"))
|
||||
if err != nil {
|
||||
ca.httpErrorf(w, http.StatusBadRequest, err.Error())
|
||||
ca.httpErrorf(w, http.StatusBadRequest, "%v", err)
|
||||
return
|
||||
}
|
||||
if o.Status != acme.StatusValid {
|
||||
@@ -782,15 +784,3 @@ func decodePayload(v interface{}, r io.Reader) error {
|
||||
func challengeToken(domain, challType string, authzID int) string {
|
||||
return fmt.Sprintf("token-%s-%s-%d", domain, challType, authzID)
|
||||
}
|
||||
|
||||
func unique(a []string) []string {
|
||||
seen := make(map[string]bool)
|
||||
var res []string
|
||||
for _, s := range a {
|
||||
if s != "" && !seen[s] {
|
||||
seen[s] = true
|
||||
res = append(res, s)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -124,32 +123,13 @@ func (ln *listener) Close() error {
|
||||
return ln.tcpListener.Close()
|
||||
}
|
||||
|
||||
func homeDir() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
||||
}
|
||||
if h := os.Getenv("HOME"); h != "" {
|
||||
return h
|
||||
}
|
||||
return "/"
|
||||
}
|
||||
|
||||
func cacheDir() string {
|
||||
const base = "golang-autocert"
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
return filepath.Join(homeDir(), "Library", "Caches", base)
|
||||
case "windows":
|
||||
for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} {
|
||||
if v := os.Getenv(ev); v != "" {
|
||||
return filepath.Join(v, base)
|
||||
}
|
||||
}
|
||||
// Worst case:
|
||||
return filepath.Join(homeDir(), base)
|
||||
cache, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
// Fall back to the root directory.
|
||||
cache = "/.cache"
|
||||
}
|
||||
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
|
||||
return filepath.Join(xdg, base)
|
||||
}
|
||||
return filepath.Join(homeDir(), ".cache", base)
|
||||
|
||||
return filepath.Join(cache, base)
|
||||
}
|
||||
|
||||
+19
-17
@@ -11,9 +11,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// renewJitter is the maximum deviation from Manager.RenewBefore.
|
||||
const renewJitter = time.Hour
|
||||
|
||||
// domainRenewal tracks the state used by the periodic timers
|
||||
// renewing a single domain's cert.
|
||||
type domainRenewal struct {
|
||||
@@ -30,13 +27,13 @@ type domainRenewal struct {
|
||||
// defined by the certificate expiration time exp.
|
||||
//
|
||||
// If the timer is already started, calling start is a noop.
|
||||
func (dr *domainRenewal) start(exp time.Time) {
|
||||
func (dr *domainRenewal) start(notBefore, notAfter time.Time) {
|
||||
dr.timerMu.Lock()
|
||||
defer dr.timerMu.Unlock()
|
||||
if dr.timer != nil {
|
||||
return
|
||||
}
|
||||
dr.timer = time.AfterFunc(dr.next(exp), dr.renew)
|
||||
dr.timer = time.AfterFunc(dr.next(notBefore, notAfter), dr.renew)
|
||||
}
|
||||
|
||||
// stop stops the cert renewal timer and waits for any in-flight calls to renew
|
||||
@@ -79,7 +76,7 @@ func (dr *domainRenewal) renew() {
|
||||
// TODO: rotate dr.key at some point?
|
||||
next, err := dr.do(ctx)
|
||||
if err != nil {
|
||||
next = renewJitter / 2
|
||||
next = time.Hour / 2
|
||||
next += time.Duration(pseudoRand.int63n(int64(next)))
|
||||
}
|
||||
testDidRenewLoop(next, err)
|
||||
@@ -107,8 +104,8 @@ func (dr *domainRenewal) do(ctx context.Context) (time.Duration, error) {
|
||||
// a race is likely unavoidable in a distributed environment
|
||||
// but we try nonetheless
|
||||
if tlscert, err := dr.m.cacheGet(ctx, dr.ck); err == nil {
|
||||
next := dr.next(tlscert.Leaf.NotAfter)
|
||||
if next > dr.m.renewBefore()+renewJitter {
|
||||
next := dr.next(tlscert.Leaf.NotBefore, tlscert.Leaf.NotAfter)
|
||||
if next > 0 {
|
||||
signer, ok := tlscert.PrivateKey.(crypto.Signer)
|
||||
if ok {
|
||||
state := &certState{
|
||||
@@ -139,18 +136,23 @@ func (dr *domainRenewal) do(ctx context.Context) (time.Duration, error) {
|
||||
return 0, err
|
||||
}
|
||||
dr.updateState(state)
|
||||
return dr.next(leaf.NotAfter), nil
|
||||
return dr.next(leaf.NotBefore, leaf.NotAfter), nil
|
||||
}
|
||||
|
||||
func (dr *domainRenewal) next(expiry time.Time) time.Duration {
|
||||
d := expiry.Sub(dr.m.now()) - dr.m.renewBefore()
|
||||
// add a bit of randomness to renew deadline
|
||||
n := pseudoRand.int63n(int64(renewJitter))
|
||||
d -= time.Duration(n)
|
||||
if d < 0 {
|
||||
return 0
|
||||
// next returns the wait time before the next renewal should start.
|
||||
// If manager.RenewBefore is set, it uses that capped at 30 days,
|
||||
// otherwise it uses a default of 1/3 of the cert lifetime.
|
||||
// It builds in a jitter of 10% of the renew threshold, capped at 1 hour.
|
||||
func (dr *domainRenewal) next(notBefore, notAfter time.Time) time.Duration {
|
||||
threshold := min(notAfter.Sub(notBefore)/3, 30*24*time.Hour)
|
||||
if dr.m.RenewBefore > 0 {
|
||||
threshold = min(dr.m.RenewBefore, 30*24*time.Hour)
|
||||
}
|
||||
return d
|
||||
maxJitter := min(threshold/10, time.Hour)
|
||||
jitter := pseudoRand.int63n(int64(maxJitter))
|
||||
renewAt := notAfter.Add(-(threshold - time.Duration(jitter)))
|
||||
renewWait := renewAt.Sub(dr.m.now())
|
||||
return max(0, renewWait)
|
||||
}
|
||||
|
||||
var testDidRenewLoop = func(next time.Duration, err error) {}
|
||||
|
||||
@@ -17,27 +17,60 @@ import (
|
||||
|
||||
func TestRenewalNext(t *testing.T) {
|
||||
now := time.Now()
|
||||
man := &Manager{
|
||||
RenewBefore: 7 * 24 * time.Hour,
|
||||
nowFunc: func() time.Time { return now },
|
||||
}
|
||||
defer man.stopRenew()
|
||||
nowFn := func() time.Time { return now }
|
||||
tt := []struct {
|
||||
expiry time.Time
|
||||
min, max time.Duration
|
||||
name string
|
||||
renewBefore time.Duration // arg to Manager
|
||||
// leaf cert validity
|
||||
notBefore time.Time
|
||||
validFor time.Duration
|
||||
// wait time
|
||||
waitMin, waitMax time.Duration
|
||||
}{
|
||||
{now.Add(90 * 24 * time.Hour), 83*24*time.Hour - renewJitter, 83 * 24 * time.Hour},
|
||||
{now.Add(time.Hour), 0, 1},
|
||||
{now, 0, 1},
|
||||
{now.Add(-time.Hour), 0, 1},
|
||||
{"default renewal, 1h cert, valid",
|
||||
0, now, time.Hour, 40 * time.Minute, 50 * time.Minute},
|
||||
{"default renewal, 1h cert, should renew",
|
||||
0, now.Add(-50 * time.Minute), time.Hour, 0, 0},
|
||||
{"default renewal, 1h cert, expired",
|
||||
0, now.Add(-400 * 24 * time.Hour), time.Hour, 0, 0},
|
||||
{"default renewal, 6d cert, valid",
|
||||
0, now, 6 * 24 * time.Hour, 4 * 24 * time.Hour, (4*24 + 1) * time.Hour},
|
||||
{"default renewal, 6d cert, should renew",
|
||||
0, now.Add(-5 * 24 * time.Hour), 6 * 24 * time.Hour, 0, 0},
|
||||
{"default renewal, 6d cert, expired",
|
||||
0, now.Add(-400 * 24 * time.Hour), 6 * 24 * time.Hour, 0, 0},
|
||||
{"default renewal, 90d cert, valid",
|
||||
0, now, 90 * 24 * time.Hour, 60 * 24 * time.Hour, (60*24 + 1) * time.Hour},
|
||||
{"default renewal, 90d cert, should renew",
|
||||
0, now.Add(-70 * 24 * time.Hour), 90 * 24 * time.Hour, 0, 0},
|
||||
{"default renewal, 90d cert, expired",
|
||||
0, now.Add(-400 * 24 * time.Hour), 90 * 24 * time.Hour, 0, 0},
|
||||
{"default renewal, 398d cert, valid",
|
||||
0, now, 398 * 24 * time.Hour, (368 * 24) * time.Hour, (368*24 + 1) * time.Hour},
|
||||
{"default renewal, 398d cert, should renew",
|
||||
0, now.Add(-378 * 24 * time.Hour), 398 * 24 * time.Hour, 0, 0},
|
||||
{"default renewal, 398d cert, expired",
|
||||
0, now.Add(-400 * 24 * time.Hour), 398 * 24 * time.Hour, 0, 0},
|
||||
{"7d renewal, 90d cert, valid",
|
||||
7 * 24 * time.Hour, now, 90 * 24 * time.Hour, 83 * 24 * time.Hour, (83*24 + 1) * time.Hour},
|
||||
{"7d renewal, 90d cert, should not renew",
|
||||
7 * 24 * time.Hour, now.Add(-70 * 24 * time.Hour), 90 * 24 * time.Hour, 13 * 24 * time.Hour, (13*24 + 1) * time.Hour},
|
||||
{"7d renewal, 90d cert, should renew",
|
||||
7 * 24 * time.Hour, now.Add(-85 * 24 * time.Hour), 90 * 24 * time.Hour, 0, 0},
|
||||
{"7d renewal, 90d cert, expired",
|
||||
7 * 24 * time.Hour, now.Add(-400 * 24 * time.Hour), 90 * 24 * time.Hour, 0, 0},
|
||||
}
|
||||
|
||||
dr := &domainRenewal{m: man}
|
||||
for i, test := range tt {
|
||||
next := dr.next(test.expiry)
|
||||
if next < test.min || test.max < next {
|
||||
t.Errorf("%d: next = %v; want between %v and %v", i, next, test.min, test.max)
|
||||
}
|
||||
for _, test := range tt {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
dr := &domainRenewal{m: &Manager{RenewBefore: test.renewBefore, nowFunc: nowFn}}
|
||||
defer dr.m.stopRenew()
|
||||
|
||||
next := dr.next(test.notBefore, test.notBefore.Add(test.validFor))
|
||||
if next < test.waitMin || next > test.waitMax {
|
||||
t.Errorf("expected wait time: %v <= %v <= %v", test.waitMin, next, test.waitMax)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,9 +180,6 @@ func TestRenewFromCacheAlreadyRenewed(t *testing.T) {
|
||||
|
||||
// set internal state to an almost expired cert
|
||||
oldCert := ca.LeafCert(exampleDomain, "ECDSA", now.Add(-2*time.Hour), now.Add(time.Minute))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldLeaf, err := validCert(exampleCertKey, oldCert.Certificate, oldCert.PrivateKey.(crypto.Signer), now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -239,7 +269,7 @@ func TestRenewFromCacheAlreadyRenewed(t *testing.T) {
|
||||
}
|
||||
|
||||
// trigger renew
|
||||
man.startRenew(exampleCertKey, s.key, s.leaf.NotAfter)
|
||||
man.startRenew(exampleCertKey, s.key, s.leaf.NotBefore, s.leaf.NotAfter)
|
||||
<-renewed
|
||||
func() {
|
||||
man.renewalMu.Lock()
|
||||
|
||||
+4
-7
@@ -66,7 +66,7 @@ func (c *Client) retryTimer() *retryTimer {
|
||||
// The n argument is always bounded between 1 and 30.
|
||||
// The returned value is always greater than 0.
|
||||
func defaultBackoff(n int, r *http.Request, res *http.Response) time.Duration {
|
||||
const max = 10 * time.Second
|
||||
const maxVal = 10 * time.Second
|
||||
var jitter time.Duration
|
||||
if x, err := rand.Int(rand.Reader, big.NewInt(1000)); err == nil {
|
||||
// Set the minimum to 1ms to avoid a case where
|
||||
@@ -86,10 +86,7 @@ func defaultBackoff(n int, r *http.Request, res *http.Response) time.Duration {
|
||||
n = 30
|
||||
}
|
||||
d := time.Duration(1<<uint(n-1))*time.Second + jitter
|
||||
if d > max {
|
||||
return max
|
||||
}
|
||||
return d
|
||||
return min(d, maxVal)
|
||||
}
|
||||
|
||||
// retryAfter parses a Retry-After HTTP header value,
|
||||
@@ -131,7 +128,7 @@ func wantStatus(codes ...int) resOkay {
|
||||
func (c *Client) get(ctx context.Context, url string, ok resOkay) (*http.Response, error) {
|
||||
retry := c.retryTimer()
|
||||
for {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -231,7 +228,7 @@ func (c *Client) postNoRetry(ctx context.Context, key crypto.Signer, url string,
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, bytes.NewReader(b))
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
+1
-1
@@ -92,7 +92,7 @@ func jwsEncodeJSON(claimset interface{}, key crypto.Signer, kid KeyID, nonce, ur
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
phead := base64.RawURLEncoding.EncodeToString([]byte(phJSON))
|
||||
phead := base64.RawURLEncoding.EncodeToString(phJSON)
|
||||
var payload string
|
||||
if val, ok := claimset.(string); ok {
|
||||
payload = val
|
||||
|
||||
@@ -0,0 +1,809 @@
|
||||
// Copyright 2025 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 acme_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/acme"
|
||||
)
|
||||
|
||||
const (
|
||||
// pebbleModVersion is the module version used for Pebble and Pebble's
|
||||
// challenge test server. It is ignored if `-pebble-local-dir` is provided.
|
||||
pebbleModVersion = "v2.7.0"
|
||||
// startingPort is the first port number used for binding interface
|
||||
// addresses. Each call to takeNextPort() will increment a port number
|
||||
// starting at this value.
|
||||
startingPort = 5555
|
||||
)
|
||||
|
||||
var (
|
||||
pebbleLocalDir = flag.String(
|
||||
"pebble-local-dir",
|
||||
"",
|
||||
"Local Pebble to use, instead of fetching from source",
|
||||
)
|
||||
nextPort atomic.Uint32
|
||||
)
|
||||
|
||||
func init() {
|
||||
nextPort.Store(startingPort)
|
||||
}
|
||||
|
||||
func TestWithPebble(t *testing.T) {
|
||||
// We want to use process groups w/ syscall.Kill, and the acme package
|
||||
// is very platform-agnostic, so skip on non-Linux.
|
||||
if runtime.GOOS != "linux" {
|
||||
t.Skip("skipping pebble tests on non-linux OS")
|
||||
}
|
||||
|
||||
if testing.Short() {
|
||||
t.Skip("skipping pebble tests in short mode")
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
challSrv func(*environment) (challengeServer, string)
|
||||
}{
|
||||
{
|
||||
name: "TLSALPN01-Issuance",
|
||||
challSrv: func(env *environment) (challengeServer, string) {
|
||||
bindAddr := fmt.Sprintf(":%d", env.config.TLSPort)
|
||||
return newChallTLSServer(bindAddr), bindAddr
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
name: "HTTP01-Issuance",
|
||||
challSrv: func(env *environment) (challengeServer, string) {
|
||||
bindAddr := fmt.Sprintf(":%d", env.config.HTTPPort)
|
||||
return newChallHTTPServer(bindAddr), bindAddr
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := startPebbleEnvironment(t, nil)
|
||||
challSrv, challSrvAddr := tt.challSrv(&env)
|
||||
challSrv.Run()
|
||||
|
||||
t.Cleanup(func() {
|
||||
challSrv.Shutdown()
|
||||
})
|
||||
|
||||
waitForServer(t, challSrvAddr)
|
||||
testIssuance(t, &env, challSrv)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// challengeServer abstracts over the details of running a challenge response
|
||||
// server for some supported acme.Challenge type. Responses are provisioned
|
||||
// during the test issuance process to be presented to the ACME server's
|
||||
// validation authority.
|
||||
type challengeServer interface {
|
||||
Run()
|
||||
Shutdown() error
|
||||
Supported(chal *acme.Challenge) bool
|
||||
Provision(client *acme.Client, ident acme.AuthzID, chal *acme.Challenge) error
|
||||
}
|
||||
|
||||
// challTLSServer is a simple challenge response server that listens for TLS
|
||||
// connections on a specific port and if they are TLS-ALPN-01 challenge
|
||||
// requests, completes the handshake using the configured challenge response
|
||||
// certificate for the SNI value provided.
|
||||
type challTLSServer struct {
|
||||
*http.Server
|
||||
// mu protects challCerts.
|
||||
mu sync.RWMutex
|
||||
// challCerts is a map from SNI domain name to challenge response certificate.
|
||||
challCerts map[string]*tls.Certificate
|
||||
}
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc8737#section-4
|
||||
const acmeTLSAlpnProtocol = "acme-tls/1"
|
||||
|
||||
func newChallTLSServer(address string) *challTLSServer {
|
||||
challServer := &challTLSServer{Server: &http.Server{
|
||||
Addr: address,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 5 * time.Second,
|
||||
}, challCerts: make(map[string]*tls.Certificate)}
|
||||
|
||||
// Configure the server to support the TLS-ALPN-01 challenge protocol
|
||||
// and to use a callback for selecting the handshake certificate.
|
||||
challServer.Server.TLSConfig = &tls.Config{
|
||||
NextProtos: []string{acmeTLSAlpnProtocol},
|
||||
GetCertificate: challServer.getCertificate,
|
||||
}
|
||||
|
||||
return challServer
|
||||
}
|
||||
|
||||
func (c *challTLSServer) Shutdown() error {
|
||||
log.Printf("challTLSServer: shutting down")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10)
|
||||
defer cancel()
|
||||
return c.Server.Shutdown(ctx)
|
||||
}
|
||||
|
||||
func (c *challTLSServer) Run() {
|
||||
go func() {
|
||||
// Note: certFile and keyFile are empty because our config uses a
|
||||
// GetCertificate callback.
|
||||
if err := c.Server.ListenAndServeTLS("", ""); err != nil {
|
||||
if !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Printf("challTLSServer error: %v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *challTLSServer) Supported(chal *acme.Challenge) bool {
|
||||
return chal.Type == "tls-alpn-01"
|
||||
}
|
||||
|
||||
func (c *challTLSServer) Provision(client *acme.Client, ident acme.AuthzID, chal *acme.Challenge) error {
|
||||
respCert, err := client.TLSALPN01ChallengeCert(chal.Token, ident.Value)
|
||||
if err != nil {
|
||||
return fmt.Errorf("challTLSServer: failed to generate challlenge response cert for %s: %w",
|
||||
ident.Value, err)
|
||||
}
|
||||
|
||||
log.Printf("challTLSServer: setting challenge response certificate for %s", ident.Value)
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.challCerts[ident.Value] = &respCert
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *challTLSServer) getCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
||||
// Verify the request looks like a TLS-ALPN-01 challenge request.
|
||||
if len(clientHello.SupportedProtos) != 1 || clientHello.SupportedProtos[0] != acmeTLSAlpnProtocol {
|
||||
return nil, fmt.Errorf(
|
||||
"challTLSServer: non-TLS-ALPN-01 challenge request received with SupportedProtos: %s",
|
||||
clientHello.SupportedProtos)
|
||||
}
|
||||
|
||||
serverName := clientHello.ServerName
|
||||
|
||||
// TLS-ALPN-01 challenge requests for IP addresses are encoded in the SNI
|
||||
// using the reverse-DNS notation. See RFC 8738 Section 6:
|
||||
// https://www.rfc-editor.org/rfc/rfc8738.html#section-6
|
||||
if strings.HasSuffix(serverName, ".in-addr.arpa") {
|
||||
serverName = strings.TrimSuffix(serverName, ".in-addr.arpa")
|
||||
parts := strings.Split(serverName, ".")
|
||||
for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 {
|
||||
parts[i], parts[j] = parts[j], parts[i]
|
||||
}
|
||||
serverName = strings.Join(parts, ".")
|
||||
}
|
||||
|
||||
log.Printf("challTLSServer: selecting certificate for request from %s for %s",
|
||||
clientHello.Conn.RemoteAddr(), serverName)
|
||||
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
cert := c.challCerts[serverName]
|
||||
if cert == nil {
|
||||
return nil, fmt.Errorf("challTLSServer: no challenge response certificate configured for %s", serverName)
|
||||
}
|
||||
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
// challHTTPServer is a simple challenge response server that listens for HTTP
|
||||
// connections on a specific port and if they are HTTP-01 challenge requests,
|
||||
// serves the challenge response key authorization.
|
||||
type challHTTPServer struct {
|
||||
*http.Server
|
||||
// mu protects challMap
|
||||
mu sync.RWMutex
|
||||
// challMap is a mapping from request path to response body.
|
||||
challMap map[string]string
|
||||
}
|
||||
|
||||
func newChallHTTPServer(address string) *challHTTPServer {
|
||||
challServer := &challHTTPServer{
|
||||
Server: &http.Server{
|
||||
Addr: address,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 5 * time.Second,
|
||||
},
|
||||
challMap: make(map[string]string),
|
||||
}
|
||||
|
||||
challServer.Server.Handler = challServer
|
||||
|
||||
return challServer
|
||||
}
|
||||
|
||||
func (c *challHTTPServer) Supported(chal *acme.Challenge) bool {
|
||||
return chal.Type == "http-01"
|
||||
}
|
||||
|
||||
func (c *challHTTPServer) Provision(client *acme.Client, ident acme.AuthzID, chall *acme.Challenge) error {
|
||||
path := client.HTTP01ChallengePath(chall.Token)
|
||||
body, err := client.HTTP01ChallengeResponse(chall.Token)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to generate HTTP-01 challenge response for %v challenge %s token %s: %w",
|
||||
ident, chall.URI, chall.Token, err)
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
log.Printf("challHTTPServer: setting challenge response for %s", path)
|
||||
c.challMap[path] = body
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *challHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("challHTTPServer: handling %s to %s from %s", r.Method, r.URL.Path, r.RemoteAddr)
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
response, exists := c.challMap[r.URL.Path]
|
||||
|
||||
if !exists {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write([]byte(response))
|
||||
}
|
||||
|
||||
func (c *challHTTPServer) Shutdown() error {
|
||||
log.Printf("challHTTPServer: shutting down")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
return c.Server.Shutdown(ctx)
|
||||
}
|
||||
|
||||
func (c *challHTTPServer) Run() {
|
||||
go func() {
|
||||
if err := c.Server.ListenAndServe(); err != nil {
|
||||
if !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Printf("challHTTPServer error: %v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func testIssuance(t *testing.T, env *environment, challSrv challengeServer) {
|
||||
t.Helper()
|
||||
|
||||
// Bound the total issuance process by a timeout of 60 seconds.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Create a new ACME account.
|
||||
client := env.client
|
||||
acct, err := client.Register(ctx, &acme.Account{}, acme.AcceptTOS)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to register account: %v", err)
|
||||
}
|
||||
if acct.Status != acme.StatusValid {
|
||||
t.Fatalf("expected new account status to be valid, got %v", acct.Status)
|
||||
}
|
||||
log.Printf("registered account: %s", acct.URI)
|
||||
|
||||
// Create a new order for some example identifiers
|
||||
identifiers := []acme.AuthzID{
|
||||
{
|
||||
Type: "dns",
|
||||
Value: "example.com",
|
||||
},
|
||||
{
|
||||
Type: "dns",
|
||||
Value: "www.example.com",
|
||||
},
|
||||
{
|
||||
Type: "ip",
|
||||
Value: "127.0.0.1",
|
||||
},
|
||||
}
|
||||
order, err := client.AuthorizeOrder(ctx, identifiers)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create order for %v: %v", identifiers, err)
|
||||
}
|
||||
if order.Status != acme.StatusPending {
|
||||
t.Fatalf("expected new order status to be pending, got %v", order.Status)
|
||||
}
|
||||
orderURL := order.URI
|
||||
log.Printf("created order: %v", orderURL)
|
||||
|
||||
// For each pending authz provision a supported challenge type's response
|
||||
// with the test challenge server, and tell the ACME server to verify it.
|
||||
for _, authzURL := range order.AuthzURLs {
|
||||
authz, err := client.GetAuthorization(ctx, authzURL)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get order %s authorization %s: %v",
|
||||
orderURL, authzURL, err)
|
||||
}
|
||||
|
||||
if authz.Status != acme.StatusPending {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, challenge := range authz.Challenges {
|
||||
if challenge.Status != acme.StatusPending || !challSrv.Supported(challenge) {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := challSrv.Provision(client, authz.Identifier, challenge); err != nil {
|
||||
t.Fatalf("failed to provision challenge %s: %v", challenge.URI, err)
|
||||
}
|
||||
|
||||
_, err = client.Accept(ctx, challenge)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to accept order %s challenge %s: %v",
|
||||
orderURL, challenge.URI, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for the order to become ready for finalization.
|
||||
order, err = client.WaitOrder(ctx, order.URI)
|
||||
if err != nil {
|
||||
var orderErr *acme.OrderError
|
||||
if errors.Is(err, orderErr) {
|
||||
t.Fatalf("failed to wait for order %s: %s: %s", orderURL, err, orderErr.Problem)
|
||||
} else {
|
||||
t.Fatalf("failed to wait for order %s: %s", orderURL, err)
|
||||
}
|
||||
}
|
||||
if order.Status != acme.StatusReady {
|
||||
t.Fatalf("expected order %s status to be ready, got %v",
|
||||
orderURL, order.Status)
|
||||
}
|
||||
|
||||
// Generate a certificate keypair and a CSR for the order identifiers.
|
||||
certKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to generate certificate key: %v", err)
|
||||
}
|
||||
var dnsNames []string
|
||||
var ipAddresses []net.IP
|
||||
for _, ident := range identifiers {
|
||||
switch ident.Type {
|
||||
case "dns":
|
||||
dnsNames = append(dnsNames, ident.Value)
|
||||
case "ip":
|
||||
ipAddresses = append(ipAddresses, net.ParseIP(ident.Value))
|
||||
default:
|
||||
t.Fatalf("unsupported identifier type: %s", ident.Type)
|
||||
}
|
||||
}
|
||||
csrDer, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{
|
||||
DNSNames: dnsNames,
|
||||
IPAddresses: ipAddresses,
|
||||
}, certKey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create CSR: %v", err)
|
||||
}
|
||||
|
||||
// Finalize the order by creating a certificate with our CSR.
|
||||
chain, _, err := client.CreateOrderCert(ctx, order.FinalizeURL, csrDer, true)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to finalize order %s with finalize URL %s: %v",
|
||||
orderURL, order.FinalizeURL, err)
|
||||
}
|
||||
|
||||
// Split the chain into the leaf and any intermediates.
|
||||
leaf := chain[0]
|
||||
intermediatesDER := chain[1:]
|
||||
leafCert, err := x509.ParseCertificate(leaf)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse order %s leaf certificate: %v", orderURL, err)
|
||||
}
|
||||
intermediates := x509.NewCertPool()
|
||||
for i, intermediateDER := range intermediatesDER {
|
||||
intermediate, err := x509.ParseCertificate(intermediateDER)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse intermediate %d: %v", i, err)
|
||||
}
|
||||
intermediates.AddCert(intermediate)
|
||||
}
|
||||
|
||||
// Verify there is a valid path from the leaf certificate to Pebble's
|
||||
// issuing root using the provided intermediate certificates.
|
||||
roots, err := env.RootCert()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get Pebble issuer root certs: %v", err)
|
||||
}
|
||||
paths, err := leafCert.Verify(x509.VerifyOptions{
|
||||
Intermediates: intermediates,
|
||||
Roots: roots,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to verify order %s leaf certificate: %v", orderURL, err)
|
||||
}
|
||||
log.Printf("verified %d path(s) from issued leaf certificate to Pebble root CA", len(paths))
|
||||
|
||||
// Also verify that the leaf cert is valid for each of the DNS names
|
||||
// and IP addresses from our order's identifiers.
|
||||
for _, name := range dnsNames {
|
||||
if err := leafCert.VerifyHostname(name); err != nil {
|
||||
t.Fatalf("failed to verify order %s leaf certificate for order DNS name %s: %v",
|
||||
orderURL, name, err)
|
||||
}
|
||||
}
|
||||
for _, ip := range ipAddresses {
|
||||
if err := leafCert.VerifyHostname(ip.String()); err != nil {
|
||||
t.Fatalf("failed to verify order %s leaf certificate for order IP address %s: %v",
|
||||
orderURL, ip, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type environment struct {
|
||||
config *environmentConfig
|
||||
client *acme.Client
|
||||
}
|
||||
|
||||
// RootCert returns the Pebble CA's primary issuing hierarchy root certificate.
|
||||
// This is generated randomly at each startup and can be used to verify
|
||||
// certificate chains issued by Pebble's ACME interface. Note that this
|
||||
// is separate from the static root certificate used by the Pebble ACME
|
||||
// HTTPS interface.
|
||||
func (e *environment) RootCert() (*x509.CertPool, error) {
|
||||
// NOTE: in the future we may want to consider the alternative chains
|
||||
// returned as Link alternative headers.
|
||||
rootURL := fmt.Sprintf("https://%s/roots/0", e.config.pebbleConfig.ManagementListenAddress)
|
||||
resp, err := e.client.HTTPClient.Get(rootURL)
|
||||
if err != nil || resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("failed to GET Pebble root CA from %s: %v", rootURL, err)
|
||||
}
|
||||
|
||||
roots := x509.NewCertPool()
|
||||
rootPEM, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse Pebble root CA PEM: %v", err)
|
||||
}
|
||||
rootDERBlock, _ := pem.Decode(rootPEM)
|
||||
rootCA, err := x509.ParseCertificate(rootDERBlock.Bytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse Pebble root CA DER: %v", err)
|
||||
}
|
||||
roots.AddCert(rootCA)
|
||||
|
||||
return roots, nil
|
||||
}
|
||||
|
||||
// environmentConfig describes the Pebble configuration, and configuration
|
||||
// shared between pebble and pebble-challtestsrv.
|
||||
type environmentConfig struct {
|
||||
pebbleConfig
|
||||
dnsPort uint32
|
||||
}
|
||||
|
||||
// defaultConfig returns an environmentConfig populated with default values.
|
||||
// The provided pebbleDir is used to specify certificate/private key paths
|
||||
// for the HTTPS ACME interface.
|
||||
func defaultConfig(pebbleDir string) environmentConfig {
|
||||
return environmentConfig{
|
||||
pebbleConfig: pebbleConfig{
|
||||
ListenAddress: fmt.Sprintf("127.0.0.1:%d", takeNextPort()),
|
||||
ManagementListenAddress: fmt.Sprintf("127.0.0.1:%d", takeNextPort()),
|
||||
HTTPPort: takeNextPort(),
|
||||
TLSPort: takeNextPort(),
|
||||
Certificate: fmt.Sprintf("%s/test/certs/localhost/cert.pem", pebbleDir),
|
||||
PrivateKey: fmt.Sprintf("%s/test/certs/localhost/key.pem", pebbleDir),
|
||||
OCSPResponderURL: "",
|
||||
ExternalAccountBindingRequired: false,
|
||||
ExternalAccountMACKeys: make(map[string]string),
|
||||
DomainBlocklist: []string{"blocked-domain.example"},
|
||||
Profiles: map[string]struct {
|
||||
Description string
|
||||
ValidityPeriod uint64
|
||||
}{
|
||||
"default": {
|
||||
Description: "default profile",
|
||||
ValidityPeriod: 3600,
|
||||
},
|
||||
},
|
||||
RetryAfter: struct {
|
||||
Authz int
|
||||
Order int
|
||||
}{
|
||||
3,
|
||||
5,
|
||||
},
|
||||
},
|
||||
dnsPort: takeNextPort(),
|
||||
}
|
||||
}
|
||||
|
||||
// pebbleConfig matches the JSON structure of the Pebble configuration file.
|
||||
type pebbleConfig struct {
|
||||
ListenAddress string
|
||||
ManagementListenAddress string
|
||||
HTTPPort uint32
|
||||
TLSPort uint32
|
||||
Certificate string
|
||||
PrivateKey string
|
||||
OCSPResponderURL string
|
||||
ExternalAccountBindingRequired bool
|
||||
ExternalAccountMACKeys map[string]string
|
||||
DomainBlocklist []string
|
||||
Profiles map[string]struct {
|
||||
Description string
|
||||
ValidityPeriod uint64
|
||||
}
|
||||
RetryAfter struct {
|
||||
Authz int
|
||||
Order int
|
||||
}
|
||||
}
|
||||
|
||||
func takeNextPort() uint32 {
|
||||
return nextPort.Add(1) - 1
|
||||
}
|
||||
|
||||
// startPebbleEnvironment is a test helper that spawns Pebble and Pebble
|
||||
// challenge test server processes based on the provided environmentConfig. The
|
||||
// processes will be torn down when the test ends.
|
||||
func startPebbleEnvironment(t *testing.T, config *environmentConfig) environment {
|
||||
t.Helper()
|
||||
|
||||
var pebbleDir string
|
||||
if *pebbleLocalDir != "" {
|
||||
pebbleDir = *pebbleLocalDir
|
||||
} else {
|
||||
pebbleDir = fetchModule(t, "github.com/letsencrypt/pebble/v2", pebbleModVersion)
|
||||
}
|
||||
|
||||
binDir := prepareBinaries(t, pebbleDir)
|
||||
|
||||
if config == nil {
|
||||
cfg := defaultConfig(pebbleDir)
|
||||
config = &cfg
|
||||
}
|
||||
|
||||
marshalConfig := struct {
|
||||
Pebble pebbleConfig
|
||||
}{
|
||||
Pebble: config.pebbleConfig,
|
||||
}
|
||||
|
||||
configData, err := json.Marshal(marshalConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal config: %v", err)
|
||||
}
|
||||
|
||||
configFile, err := os.CreateTemp("", "pebble-config-*.json")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp config file: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { os.Remove(configFile.Name()) })
|
||||
|
||||
if _, err := configFile.Write(configData); err != nil {
|
||||
t.Fatalf("failed to write config file: %v", err)
|
||||
}
|
||||
configFile.Close()
|
||||
|
||||
log.Printf("pebble dir: %s", pebbleDir)
|
||||
log.Printf("config file: %s", configFile.Name())
|
||||
|
||||
// Spawn the Pebble CA server. It answers ACME requests and performs
|
||||
// outbound validations. We configure it to use a mock DNS server that
|
||||
// always answers 127.0.0.1 for all A queries so that validation
|
||||
// requests for any domain name will resolve to our local challenge
|
||||
// server instances.
|
||||
spawnServerProcess(t, binDir, "pebble", "-config", configFile.Name(),
|
||||
"-dnsserver", fmt.Sprintf("127.0.0.1:%d", config.dnsPort),
|
||||
"-strict")
|
||||
|
||||
// Spawn the Pebble challenge test server. We'll use it to mock DNS
|
||||
// responses but disable all the other interfaces. We want to stand
|
||||
// up our own challenge response servers for TLS-ALPN-01,
|
||||
// etc.
|
||||
// Note: we specify -defaultIPv6 "" so that no AAAA records are served.
|
||||
// The LUCI CI runners have issues with IPv6 connectivity on localhost.
|
||||
spawnServerProcess(t, binDir, "pebble-challtestsrv",
|
||||
"-dns01", fmt.Sprintf(":%d", config.dnsPort),
|
||||
"-defaultIPv6", "",
|
||||
"-management", fmt.Sprintf(":%d", takeNextPort()),
|
||||
"-doh", "",
|
||||
"-http01", "",
|
||||
"-tlsalpn01", "",
|
||||
"-https01", "")
|
||||
|
||||
waitForServer(t, config.pebbleConfig.ListenAddress)
|
||||
waitForServer(t, fmt.Sprintf("127.0.0.1:%d", config.dnsPort))
|
||||
|
||||
log.Printf("pebble environment ready")
|
||||
|
||||
// Construct a cert pool that contains the CA certificate used by the ACME
|
||||
// interface's certificate chain. This is separate from the issuing
|
||||
// hierarchy and is used for the ACME client to interact with the ACME
|
||||
// interface without cert verification error.
|
||||
caCertPath := filepath.Join(pebbleDir, "test/certs/pebble.minica.pem")
|
||||
caCert, err := os.ReadFile(caCertPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read CA certificate %s: %v", caCertPath, err)
|
||||
}
|
||||
caCertPool := x509.NewCertPool()
|
||||
if !caCertPool.AppendCertsFromPEM(caCert) {
|
||||
t.Fatalf("failed to parse CA certificate %s", caCertPath)
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
RootCAs: caCertPool,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create an ACME account keypair/client and verify it can discover
|
||||
// the Pebble server's ACME directory without error.
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to generate account key: %v", err)
|
||||
}
|
||||
client := &acme.Client{
|
||||
Key: key,
|
||||
HTTPClient: httpClient,
|
||||
DirectoryURL: fmt.Sprintf("https://%s/dir", config.ListenAddress),
|
||||
}
|
||||
_, err = client.Discover(context.TODO())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to discover ACME directory: %v", err)
|
||||
}
|
||||
|
||||
return environment{
|
||||
config: config,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func waitForServer(t *testing.T, addr string) {
|
||||
t.Helper()
|
||||
|
||||
for i := 0; i < 20; i++ {
|
||||
if conn, err := net.Dial("tcp", addr); err == nil {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Duration(i*100) * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("failed to connect to %q after 20 tries", addr)
|
||||
}
|
||||
|
||||
// fetchModule fetches the module at the given version and returns the directory
|
||||
// containing its source tree. It skips the test if fetching modules is not
|
||||
// possible in this environment.
|
||||
//
|
||||
// Copied from the stdlib cryptotest.FetchModule and adapted to not rely on the
|
||||
// stdlib internal testenv package.
|
||||
func fetchModule(t *testing.T, module, version string) string {
|
||||
// If the default GOMODCACHE doesn't exist, use a temporary directory
|
||||
// instead. (For example, run.bash sets GOPATH=/nonexist-gopath.)
|
||||
out, err := exec.Command("go", "env", "GOMODCACHE").Output()
|
||||
if err != nil {
|
||||
t.Errorf("go env GOMODCACHE: %v\n%s", err, out)
|
||||
if ee, ok := err.(*exec.ExitError); ok {
|
||||
t.Logf("%s", ee.Stderr)
|
||||
}
|
||||
t.FailNow()
|
||||
}
|
||||
modcacheOk := false
|
||||
if gomodcache := string(bytes.TrimSpace(out)); gomodcache != "" {
|
||||
if _, err := os.Stat(gomodcache); err == nil {
|
||||
modcacheOk = true
|
||||
}
|
||||
}
|
||||
if !modcacheOk {
|
||||
t.Setenv("GOMODCACHE", t.TempDir())
|
||||
// Allow t.TempDir() to clean up subdirectories.
|
||||
t.Setenv("GOFLAGS", os.Getenv("GOFLAGS")+" -modcacherw")
|
||||
}
|
||||
|
||||
t.Logf("fetching %s@%s\n", module, version)
|
||||
|
||||
output, err := exec.Command("go", "mod", "download", "-json", module+"@"+version).CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to download %s@%s: %s\n%s\n", module, version, err, output)
|
||||
}
|
||||
var j struct {
|
||||
Dir string
|
||||
}
|
||||
if err := json.Unmarshal(output, &j); err != nil {
|
||||
t.Fatalf("failed to parse 'go mod download': %s\n%s\n", err, output)
|
||||
}
|
||||
|
||||
return j.Dir
|
||||
}
|
||||
|
||||
func prepareBinaries(t *testing.T, pebbleDir string) string {
|
||||
t.Helper()
|
||||
|
||||
// We don't want to build in the module cache dir, which might not be
|
||||
// writable or to pollute the user's clone with binaries if pebbleLocalDir
|
||||
// is used.
|
||||
binDir := t.TempDir()
|
||||
|
||||
build := func(cmd string) {
|
||||
log.Printf("building %s", cmd)
|
||||
buildCmd := exec.Command(
|
||||
"go",
|
||||
"build", "-o", filepath.Join(binDir, cmd), "-mod", "mod", "./cmd/"+cmd)
|
||||
buildCmd.Dir = pebbleDir
|
||||
output, err := buildCmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to build %s: %s\n%s\n", cmd, err, output)
|
||||
}
|
||||
}
|
||||
|
||||
build("pebble")
|
||||
build("pebble-challtestsrv")
|
||||
|
||||
return binDir
|
||||
}
|
||||
|
||||
func spawnServerProcess(t *testing.T, dir string, cmd string, args ...string) {
|
||||
t.Helper()
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
||||
cmdInstance := exec.Command("./"+cmd, args...)
|
||||
cmdInstance.Dir = dir
|
||||
cmdInstance.Stdout = &stdout
|
||||
cmdInstance.Stderr = &stderr
|
||||
|
||||
if err := cmdInstance.Start(); err != nil {
|
||||
t.Fatalf("failed to start %s: %v", cmd, err)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
cmdInstance.Process.Kill()
|
||||
cmdInstance.Wait()
|
||||
|
||||
if t.Failed() || testing.Verbose() {
|
||||
t.Logf("=== %s output ===", cmd)
|
||||
if stdout.Len() > 0 {
|
||||
t.Logf("stdout:\n%s", strings.TrimSpace(stdout.String()))
|
||||
}
|
||||
if stderr.Len() > 0 {
|
||||
t.Logf("stderr:\n%s", strings.TrimSpace(stderr.String()))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
+6
-3
@@ -53,6 +53,9 @@ func (c *Client) registerRFC(ctx context.Context, acct *Account, prompt func(tos
|
||||
Contact: acct.Contact,
|
||||
}
|
||||
if c.dir.Terms != "" {
|
||||
if prompt == nil {
|
||||
return nil, errors.New("acme: missing Manager.Prompt to accept server's terms of service")
|
||||
}
|
||||
req.TermsAgreed = prompt(c.dir.Terms)
|
||||
}
|
||||
|
||||
@@ -232,7 +235,7 @@ func (c *Client) AuthorizeOrder(ctx context.Context, id []AuthzID, opt ...OrderO
|
||||
return responseOrder(res)
|
||||
}
|
||||
|
||||
// GetOrder retrives an order identified by the given URL.
|
||||
// GetOrder retrieves an order identified by the given URL.
|
||||
// For orders created with AuthorizeOrder, the url value is Order.URI.
|
||||
//
|
||||
// If a caller needs to poll an order until its status is final,
|
||||
@@ -272,7 +275,7 @@ func (c *Client) WaitOrder(ctx context.Context, url string) (*Order, error) {
|
||||
case err != nil:
|
||||
// Skip and retry.
|
||||
case o.Status == StatusInvalid:
|
||||
return nil, &OrderError{OrderURL: o.URI, Status: o.Status}
|
||||
return nil, &OrderError{OrderURL: o.URI, Status: o.Status, Problem: o.Error}
|
||||
case o.Status == StatusReady || o.Status == StatusValid:
|
||||
return o, nil
|
||||
}
|
||||
@@ -369,7 +372,7 @@ func (c *Client) CreateOrderCert(ctx context.Context, url string, csr []byte, bu
|
||||
}
|
||||
// The only acceptable status post finalize and WaitOrder is "valid".
|
||||
if o.Status != StatusValid {
|
||||
return nil, "", &OrderError{OrderURL: o.URI, Status: o.Status}
|
||||
return nil, "", &OrderError{OrderURL: o.URI, Status: o.Status, Problem: o.Error}
|
||||
}
|
||||
crt, err := c.fetchCertRFC(ctx, o.CertURL, bundle)
|
||||
return crt, o.CertURL, err
|
||||
|
||||
+16
-3
@@ -885,11 +885,17 @@ func TestRFC_WaitOrderError(t *testing.T) {
|
||||
s.handle("/orders/1", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Location", s.url("/orders/1"))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
s := StatusPending
|
||||
if count > 0 {
|
||||
s = StatusInvalid
|
||||
// https://www.rfc-editor.org/rfc/rfc8555#section-7.3.3
|
||||
errorData := `{
|
||||
"type": "urn:ietf:params:acme:error:userActionRequired",
|
||||
"detail": "Terms of service have changed",
|
||||
"instance": "https://example.com/acme/agreement/?token=W8Ih3PswD-8"
|
||||
}`
|
||||
fmt.Fprintf(w, `{"status": %q, "error": %s}`, StatusInvalid, errorData)
|
||||
} else {
|
||||
fmt.Fprintf(w, `{"status": %q}`, StatusPending)
|
||||
}
|
||||
fmt.Fprintf(w, `{"status": %q}`, s)
|
||||
count++
|
||||
})
|
||||
s.start()
|
||||
@@ -910,6 +916,13 @@ func TestRFC_WaitOrderError(t *testing.T) {
|
||||
if e.Status != StatusInvalid {
|
||||
t.Errorf("e.Status = %q; want %q", e.Status, StatusInvalid)
|
||||
}
|
||||
if e.Problem == nil {
|
||||
t.Errorf("e.Problem = nil")
|
||||
}
|
||||
expectedProbType := "urn:ietf:params:acme:error:userActionRequired"
|
||||
if e.Problem.ProblemType != expectedProbType {
|
||||
t.Errorf("e.Problem.ProblemType = %q; want %q", e.Problem.ProblemType, expectedProbType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRFC_CreateOrderCert(t *testing.T) {
|
||||
|
||||
+26
-4
@@ -7,6 +7,7 @@ package acme
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -55,6 +56,10 @@ var (
|
||||
|
||||
// ErrNoAccount indicates that the Client's key has not been registered with the CA.
|
||||
ErrNoAccount = errors.New("acme: account does not exist")
|
||||
|
||||
// errPreAuthorizationNotSupported indicates that the server does not
|
||||
// support pre-authorization of identifiers.
|
||||
errPreAuthorizationNotSupported = errors.New("acme: pre-authorization is not supported")
|
||||
)
|
||||
|
||||
// A Subproblem describes an ACME subproblem as reported in an Error.
|
||||
@@ -149,17 +154,24 @@ func (a *AuthorizationError) Error() string {
|
||||
|
||||
// OrderError is returned from Client's order related methods.
|
||||
// It indicates the order is unusable and the clients should start over with
|
||||
// AuthorizeOrder.
|
||||
// AuthorizeOrder. A Problem description may be provided with details on
|
||||
// what caused the order to become unusable.
|
||||
//
|
||||
// The clients can still fetch the order object from CA using GetOrder
|
||||
// to inspect its state.
|
||||
type OrderError struct {
|
||||
OrderURL string
|
||||
Status string
|
||||
// Problem is the error that occurred while processing the order.
|
||||
Problem *Error
|
||||
}
|
||||
|
||||
func (oe *OrderError) Error() string {
|
||||
return fmt.Sprintf("acme: order %s status: %s", oe.OrderURL, oe.Status)
|
||||
str := fmt.Sprintf("acme: order %s status: %s", oe.OrderURL, oe.Status)
|
||||
if oe.Problem != nil {
|
||||
str += fmt.Sprintf("; problem: %s", oe.Problem)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// RateLimit reports whether err represents a rate limit error and
|
||||
@@ -288,7 +300,7 @@ type Directory struct {
|
||||
// KeyChangeURL allows to perform account key rollover flow.
|
||||
KeyChangeURL string
|
||||
|
||||
// Term is a URI identifying the current terms of service.
|
||||
// Terms is a URI identifying the current terms of service.
|
||||
Terms string
|
||||
|
||||
// Website is an HTTP or HTTPS URL locating a website
|
||||
@@ -527,6 +539,16 @@ type Challenge struct {
|
||||
// when this challenge was used.
|
||||
// The type of a non-nil value is *Error.
|
||||
Error error
|
||||
|
||||
// Payload is the JSON-formatted payload that the client sends
|
||||
// to the server to indicate it is ready to respond to the challenge.
|
||||
// When unset, it defaults to an empty JSON object: {}.
|
||||
// For most challenges, the client must not set Payload,
|
||||
// see https://tools.ietf.org/html/rfc8555#section-7.5.1.
|
||||
// Payload is used only for newer challenges (such as "device-attest-01")
|
||||
// where the client must send additional data for the server to validate
|
||||
// the challenge.
|
||||
Payload json.RawMessage
|
||||
}
|
||||
|
||||
// wireChallenge is ACME JSON challenge representation.
|
||||
@@ -604,7 +626,7 @@ func (*certOptKey) privateCertOpt() {}
|
||||
//
|
||||
// In TLS ChallengeCert methods, the template is also used as parent,
|
||||
// resulting in a self-signed certificate.
|
||||
// The DNSNames field of t is always overwritten for tls-sni challenge certs.
|
||||
// The DNSNames or IPAddresses fields of t are always overwritten for tls-alpn challenge certs.
|
||||
func WithTemplate(t *x509.Certificate) CertOption {
|
||||
return (*certOptTemplate)(t)
|
||||
}
|
||||
|
||||
@@ -217,3 +217,19 @@ func TestErrorStringerWithSubproblems(t *testing.T) {
|
||||
t.Errorf("Unexpected error string: wanted %q, got %q", expectedStr, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderErrorProblem(t *testing.T) {
|
||||
oe := &OrderError{
|
||||
OrderURL: "url",
|
||||
Status: "invalid",
|
||||
Problem: &Error{
|
||||
StatusCode: 400,
|
||||
ProblemType: "type",
|
||||
Detail: "detail",
|
||||
},
|
||||
}
|
||||
want := "acme: order url status: invalid; problem: 400 type: detail"
|
||||
if got := oe.Error(); got != want {
|
||||
t.Errorf("oe.Error() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,287 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
. "github.com/mmcloughlin/avo/build"
|
||||
. "github.com/mmcloughlin/avo/operand"
|
||||
. "github.com/mmcloughlin/avo/reg"
|
||||
_ "golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
//go:generate go run . -out ../blamka_amd64.s -pkg argon2
|
||||
|
||||
func main() {
|
||||
Package("golang.org/x/crypto/argon2")
|
||||
ConstraintExpr("amd64,gc,!purego")
|
||||
|
||||
blamkaSSE4()
|
||||
mixBlocksSSE2()
|
||||
xorBlocksSSE2()
|
||||
Generate()
|
||||
}
|
||||
|
||||
func blamkaSSE4() {
|
||||
Implement("blamkaSSE4")
|
||||
Attributes(NOSPLIT)
|
||||
AllocLocal(0)
|
||||
|
||||
Load(Param("b"), RAX)
|
||||
|
||||
c40 := c40_DATA()
|
||||
c48 := c48_DATA()
|
||||
|
||||
MOVOU(c40, X10)
|
||||
MOVOU(c48, X11)
|
||||
|
||||
BLAMKA_ROUND_0(AX, 0, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_0(AX, 16, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_0(AX, 32, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_0(AX, 48, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_0(AX, 64, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_0(AX, 80, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_0(AX, 96, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_0(AX, 112, X8, X9, X10, X11)
|
||||
|
||||
BLAMKA_ROUND_1(AX, 0, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_1(AX, 2, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_1(AX, 4, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_1(AX, 6, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_1(AX, 8, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_1(AX, 10, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_1(AX, 12, X8, X9, X10, X11)
|
||||
BLAMKA_ROUND_1(AX, 14, X8, X9, X10, X11)
|
||||
RET()
|
||||
}
|
||||
|
||||
func mixBlocksSSE2() {
|
||||
Implement("mixBlocksSSE2")
|
||||
Attributes(NOSPLIT)
|
||||
AllocLocal(0)
|
||||
|
||||
Load(Param("out"), RDX)
|
||||
Load(Param("a"), RAX)
|
||||
Load(Param("b"), RBX)
|
||||
Load(Param("c"), RCX)
|
||||
MOVQ(U32(128), RDI)
|
||||
|
||||
Label("loop")
|
||||
MOVOU(Mem{Base: AX}.Offset(0), X0)
|
||||
MOVOU(Mem{Base: BX}.Offset(0), X1)
|
||||
MOVOU(Mem{Base: CX}.Offset(0), X2)
|
||||
PXOR(X1, X0)
|
||||
PXOR(X2, X0)
|
||||
MOVOU(X0, Mem{Base: DX}.Offset(0))
|
||||
ADDQ(Imm(16), RAX)
|
||||
ADDQ(Imm(16), RBX)
|
||||
ADDQ(Imm(16), RCX)
|
||||
ADDQ(Imm(16), RDX)
|
||||
SUBQ(Imm(2), RDI)
|
||||
JA(LabelRef("loop"))
|
||||
RET()
|
||||
}
|
||||
|
||||
func xorBlocksSSE2() {
|
||||
Implement("xorBlocksSSE2")
|
||||
Attributes(NOSPLIT)
|
||||
AllocLocal(0)
|
||||
|
||||
Load(Param("out"), RDX)
|
||||
Load(Param("a"), RAX)
|
||||
Load(Param("b"), RBX)
|
||||
Load(Param("c"), RCX)
|
||||
MOVQ(U32(128), RDI)
|
||||
|
||||
Label("loop")
|
||||
MOVOU(Mem{Base: AX}.Offset(0), X0)
|
||||
MOVOU(Mem{Base: BX}.Offset(0), X1)
|
||||
MOVOU(Mem{Base: CX}.Offset(0), X2)
|
||||
MOVOU(Mem{Base: DX}.Offset(0), X3)
|
||||
PXOR(X1, X0)
|
||||
PXOR(X2, X0)
|
||||
PXOR(X3, X0)
|
||||
MOVOU(X0, Mem{Base: DX}.Offset(0))
|
||||
ADDQ(Imm(16), RAX)
|
||||
ADDQ(Imm(16), RBX)
|
||||
ADDQ(Imm(16), RCX)
|
||||
ADDQ(Imm(16), RDX)
|
||||
SUBQ(Imm(2), RDI)
|
||||
JA(LabelRef("loop"))
|
||||
RET()
|
||||
}
|
||||
|
||||
func SHUFFLE(v2, v3, v4, v5, v6, v7, t1, t2 VecPhysical) {
|
||||
MOVO(v4, t1)
|
||||
MOVO(v5, v4)
|
||||
MOVO(t1, v5)
|
||||
MOVO(v6, t1)
|
||||
PUNPCKLQDQ(v6, t2)
|
||||
PUNPCKHQDQ(v7, v6)
|
||||
PUNPCKHQDQ(t2, v6)
|
||||
PUNPCKLQDQ(v7, t2)
|
||||
MOVO(t1, v7)
|
||||
MOVO(v2, t1)
|
||||
PUNPCKHQDQ(t2, v7)
|
||||
PUNPCKLQDQ(v3, t2)
|
||||
PUNPCKHQDQ(t2, v2)
|
||||
PUNPCKLQDQ(t1, t2)
|
||||
PUNPCKHQDQ(t2, v3)
|
||||
}
|
||||
|
||||
func SHUFFLE_INV(v2, v3, v4, v5, v6, v7, t1, t2 VecPhysical) {
|
||||
MOVO(v4, t1)
|
||||
MOVO(v5, v4)
|
||||
MOVO(t1, v5)
|
||||
MOVO(v2, t1)
|
||||
PUNPCKLQDQ(v2, t2)
|
||||
PUNPCKHQDQ(v3, v2)
|
||||
PUNPCKHQDQ(t2, v2)
|
||||
PUNPCKLQDQ(v3, t2)
|
||||
MOVO(t1, v3)
|
||||
MOVO(v6, t1)
|
||||
PUNPCKHQDQ(t2, v3)
|
||||
PUNPCKLQDQ(v7, t2)
|
||||
PUNPCKHQDQ(t2, v6)
|
||||
PUNPCKLQDQ(t1, t2)
|
||||
PUNPCKHQDQ(t2, v7)
|
||||
}
|
||||
|
||||
func HALF_ROUND(v0, v1, v2, v3, v4, v5, v6, v7, t0, c40, c48 VecPhysical) {
|
||||
MOVO(v0, t0)
|
||||
PMULULQ(v2, t0)
|
||||
PADDQ(v2, v0)
|
||||
PADDQ(t0, v0)
|
||||
PADDQ(t0, v0)
|
||||
PXOR(v0, v6)
|
||||
PSHUFD(Imm(0xB1), v6, v6)
|
||||
MOVO(v4, t0)
|
||||
PMULULQ(v6, t0)
|
||||
PADDQ(v6, v4)
|
||||
PADDQ(t0, v4)
|
||||
PADDQ(t0, v4)
|
||||
PXOR(v4, v2)
|
||||
PSHUFB(c40, v2)
|
||||
MOVO(v0, t0)
|
||||
PMULULQ(v2, t0)
|
||||
PADDQ(v2, v0)
|
||||
PADDQ(t0, v0)
|
||||
PADDQ(t0, v0)
|
||||
PXOR(v0, v6)
|
||||
PSHUFB(c48, v6)
|
||||
MOVO(v4, t0)
|
||||
PMULULQ(v6, t0)
|
||||
PADDQ(v6, v4)
|
||||
PADDQ(t0, v4)
|
||||
PADDQ(t0, v4)
|
||||
PXOR(v4, v2)
|
||||
MOVO(v2, t0)
|
||||
PADDQ(v2, t0)
|
||||
PSRLQ(Imm(63), v2)
|
||||
PXOR(t0, v2)
|
||||
MOVO(v1, t0)
|
||||
PMULULQ(v3, t0)
|
||||
PADDQ(v3, v1)
|
||||
PADDQ(t0, v1)
|
||||
PADDQ(t0, v1)
|
||||
PXOR(v1, v7)
|
||||
PSHUFD(Imm(0xB1), v7, v7)
|
||||
MOVO(v5, t0)
|
||||
PMULULQ(v7, t0)
|
||||
PADDQ(v7, v5)
|
||||
PADDQ(t0, v5)
|
||||
PADDQ(t0, v5)
|
||||
PXOR(v5, v3)
|
||||
PSHUFB(c40, v3)
|
||||
MOVO(v1, t0)
|
||||
PMULULQ(v3, t0)
|
||||
PADDQ(v3, v1)
|
||||
PADDQ(t0, v1)
|
||||
PADDQ(t0, v1)
|
||||
PXOR(v1, v7)
|
||||
PSHUFB(c48, v7)
|
||||
MOVO(v5, t0)
|
||||
PMULULQ(v7, t0)
|
||||
PADDQ(v7, v5)
|
||||
PADDQ(t0, v5)
|
||||
PADDQ(t0, v5)
|
||||
PXOR(v5, v3)
|
||||
MOVO(v3, t0)
|
||||
PADDQ(v3, t0)
|
||||
PSRLQ(Imm(63), v3)
|
||||
PXOR(t0, v3)
|
||||
}
|
||||
|
||||
func LOAD_MSG_0(block GPPhysical, off int) {
|
||||
var registers = []VecPhysical{X0, X1, X2, X3, X4, X5, X6, X7}
|
||||
for i, r := range registers {
|
||||
MOVOU(Mem{Base: block}.Offset(8*(off+(i*2))), r)
|
||||
}
|
||||
}
|
||||
|
||||
func STORE_MSG_0(block GPPhysical, off int) {
|
||||
var registers = []VecPhysical{X0, X1, X2, X3, X4, X5, X6, X7}
|
||||
for i, r := range registers {
|
||||
MOVOU(r, Mem{Base: block}.Offset(8*(off+(i*2))))
|
||||
}
|
||||
}
|
||||
|
||||
func LOAD_MSG_1(block GPPhysical, off int) {
|
||||
var registers = []VecPhysical{X0, X1, X2, X3, X4, X5, X6, X7}
|
||||
for i, r := range registers {
|
||||
MOVOU(Mem{Base: block}.Offset(8*off+i*16*8), r)
|
||||
}
|
||||
}
|
||||
|
||||
func STORE_MSG_1(block GPPhysical, off int) {
|
||||
var registers = []VecPhysical{X0, X1, X2, X3, X4, X5, X6, X7}
|
||||
for i, r := range registers {
|
||||
MOVOU(r, Mem{Base: block}.Offset(8*off+i*16*8))
|
||||
}
|
||||
}
|
||||
|
||||
func BLAMKA_ROUND_0(block GPPhysical, off int, t0, t1, c40, c48 VecPhysical) {
|
||||
LOAD_MSG_0(block, off)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, t0, c40, c48)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, t0, t1)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, t0, c40, c48)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, t0, t1)
|
||||
STORE_MSG_0(block, off)
|
||||
}
|
||||
|
||||
func BLAMKA_ROUND_1(block GPPhysical, off int, t0, t1, c40, c48 VecPhysical) {
|
||||
LOAD_MSG_1(block, off)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, t0, c40, c48)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, t0, t1)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, t0, c40, c48)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, t0, t1)
|
||||
STORE_MSG_1(block, off)
|
||||
}
|
||||
|
||||
// ##------------------DATA SECTION-------------------##
|
||||
|
||||
var c40_DATA_ptr, c48_DATA_ptr *Mem
|
||||
|
||||
func c40_DATA() Mem {
|
||||
if c40_DATA_ptr != nil {
|
||||
return *c40_DATA_ptr
|
||||
}
|
||||
|
||||
c40_DATA := GLOBL("·c40", NOPTR|RODATA)
|
||||
c40_DATA_ptr = &c40_DATA
|
||||
DATA(0x00, U64(0x0201000706050403))
|
||||
DATA(0x08, U64(0x0a09080f0e0d0c0b))
|
||||
return c40_DATA
|
||||
}
|
||||
func c48_DATA() Mem {
|
||||
if c48_DATA_ptr != nil {
|
||||
return *c48_DATA_ptr
|
||||
}
|
||||
|
||||
c48_DATA := GLOBL("·c48", NOPTR|RODATA)
|
||||
c48_DATA_ptr = &c48_DATA
|
||||
DATA(0x00, U64(0x0100070605040302))
|
||||
DATA(0x08, U64(0x09080f0e0d0c0b0a))
|
||||
return c48_DATA
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
module argon2/_asm
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/mmcloughlin/avo v0.6.0
|
||||
golang.org/x/crypto v0.33.0
|
||||
)
|
||||
|
||||
require (
|
||||
golang.org/x/mod v0.20.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/tools v0.24.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
|
||||
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
|
||||
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
|
||||
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
|
||||
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
|
||||
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
|
||||
+22
-17
@@ -6,7 +6,7 @@
|
||||
// Argon2 was selected as the winner of the Password Hashing Competition and can
|
||||
// be used to derive cryptographic keys from passwords.
|
||||
//
|
||||
// For a detailed specification of Argon2 see [1].
|
||||
// For a detailed specification of Argon2 see [argon2-specs.pdf].
|
||||
//
|
||||
// If you aren't sure which function you need, use Argon2id (IDKey) and
|
||||
// the parameter recommendations for your scenario.
|
||||
@@ -17,8 +17,8 @@
|
||||
// It uses data-independent memory access, which is preferred for password
|
||||
// hashing and password-based key derivation. Argon2i requires more passes over
|
||||
// memory than Argon2id to protect from trade-off attacks. The recommended
|
||||
// parameters (taken from [2]) for non-interactive operations are time=3 and to
|
||||
// use the maximum available memory.
|
||||
// parameters (taken from [RFC 9106 Section 7.3]) for non-interactive
|
||||
// operations are time=3 and to use the maximum available memory.
|
||||
//
|
||||
// # Argon2id
|
||||
//
|
||||
@@ -26,12 +26,15 @@
|
||||
// Argon2i and Argon2d. It uses data-independent memory access for the first
|
||||
// half of the first iteration over the memory and data-dependent memory access
|
||||
// for the rest. Argon2id is side-channel resistant and provides better brute-
|
||||
// force cost savings due to time-memory tradeoffs than Argon2i. The recommended
|
||||
// parameters for non-interactive operations (taken from [2]) are time=1 and to
|
||||
// use the maximum available memory.
|
||||
// force cost savings due to time-memory tradeoffs than Argon2i. [RFC 9106
|
||||
// Section 4] recommends time=1, memory=2*1024*1024 KiB (2 GiB), and threads=4
|
||||
// as the first recommended option. If much less memory is available, it
|
||||
// recommends time=3, memory=64*1024 KiB (64 MiB), and threads=4 as the second
|
||||
// recommended option.
|
||||
//
|
||||
// [1] https://github.com/P-H-C/phc-winner-argon2/blob/master/argon2-specs.pdf
|
||||
// [2] https://tools.ietf.org/html/draft-irtf-cfrg-argon2-03#section-9.3
|
||||
// [argon2-specs.pdf]: https://github.com/P-H-C/phc-winner-argon2/blob/master/argon2-specs.pdf
|
||||
// [RFC 9106 Section 4]: https://www.rfc-editor.org/rfc/rfc9106.html#section-4
|
||||
// [RFC 9106 Section 7.3]: https://www.rfc-editor.org/rfc/rfc9106.html#section-7.3
|
||||
package argon2
|
||||
|
||||
import (
|
||||
@@ -59,9 +62,9 @@ const (
|
||||
//
|
||||
// key := argon2.Key([]byte("some password"), salt, 3, 32*1024, 4, 32)
|
||||
//
|
||||
// The draft RFC recommends[2] time=3, and memory=32*1024 is a sensible number.
|
||||
// If using that amount of memory (32 MB) is not possible in some contexts then
|
||||
// the time parameter can be increased to compensate.
|
||||
// The example above uses time=3 and memory=32*1024. Argon2i generally
|
||||
// requires more passes over memory than Argon2id. If in doubt, prefer IDKey
|
||||
// and its Argon2id parameter recommendations.
|
||||
//
|
||||
// The time parameter specifies the number of passes over the memory and the
|
||||
// memory parameter specifies the size of the memory in KiB. For example
|
||||
@@ -81,18 +84,20 @@ func Key(password, salt []byte, time, memory uint32, threads uint8, keyLen uint3
|
||||
// For example, you can get a derived key for e.g. AES-256 (which needs a
|
||||
// 32-byte key) by doing:
|
||||
//
|
||||
// key := argon2.IDKey([]byte("some password"), salt, 1, 64*1024, 4, 32)
|
||||
// key := argon2.IDKey([]byte("some password"), salt, 1, 2*1024*1024, 4, 32)
|
||||
//
|
||||
// The draft RFC recommends[2] time=1, and memory=64*1024 is a sensible number.
|
||||
// If using that amount of memory (64 MB) is not possible in some contexts then
|
||||
// the time parameter can be increased to compensate.
|
||||
// The example above uses the first [RFC 9106 Section 4] recommended option.
|
||||
// If much less memory is available, the second recommended option is time=3,
|
||||
// memory=64*1024 KiB (64 MiB), and threads=4.
|
||||
//
|
||||
// The time parameter specifies the number of passes over the memory and the
|
||||
// memory parameter specifies the size of the memory in KiB. For example
|
||||
// memory=64*1024 sets the memory cost to ~64 MB. The number of threads can be
|
||||
// adjusted to the numbers of available CPUs. The cost parameters should be
|
||||
// memory=2*1024*1024 sets the memory cost to ~2 GiB. The number of threads can
|
||||
// be adjusted to the numbers of available CPUs. The cost parameters should be
|
||||
// increased as memory latency and CPU parallelism increases. Remember to get a
|
||||
// good random salt.
|
||||
//
|
||||
// [RFC 9106 Section 4]: https://www.rfc-editor.org/rfc/rfc9106.html#section-4
|
||||
func IDKey(password, salt []byte, time, memory uint32, threads uint8, keyLen uint32) []byte {
|
||||
return deriveKey(argon2id, password, salt, nil, nil, time, memory, threads, keyLen)
|
||||
}
|
||||
|
||||
+2761
-213
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -50,7 +50,7 @@ func (ih InvalidHashPrefixError) Error() string {
|
||||
type InvalidCostError int
|
||||
|
||||
func (ic InvalidCostError) Error() string {
|
||||
return fmt.Sprintf("crypto/bcrypt: cost %d is outside allowed range (%d,%d)", int(ic), MinCost, MaxCost)
|
||||
return fmt.Sprintf("crypto/bcrypt: cost %d is outside allowed inclusive range %d..%d", int(ic), MinCost, MaxCost)
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,16 @@
|
||||
module blake2b/_asm/AVX2
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/mmcloughlin/avo v0.6.0
|
||||
golang.org/x/crypto v0.33.0
|
||||
|
||||
)
|
||||
|
||||
require (
|
||||
golang.org/x/mod v0.20.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/tools v0.24.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
|
||||
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
|
||||
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
|
||||
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
|
||||
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
|
||||
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
|
||||
@@ -0,0 +1,361 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
. "github.com/mmcloughlin/avo/build"
|
||||
. "github.com/mmcloughlin/avo/operand"
|
||||
. "github.com/mmcloughlin/avo/reg"
|
||||
_ "golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
//go:generate go run . -out ../../blake2b_amd64.s -pkg blake2b
|
||||
|
||||
const ThatPeskyUnicodeDot = "\u00b7"
|
||||
|
||||
var iv0_DATA_ptr, iv1_DATA_ptr, iv2_DATA_ptr, iv3_DATA_ptr, c40_DATA_ptr, c48_DATA_ptr *Mem
|
||||
|
||||
func main() {
|
||||
Package("golang.org/x/crypto/blake2b")
|
||||
ConstraintExpr("amd64,gc,!purego")
|
||||
hashBlocksSSE4()
|
||||
Generate()
|
||||
}
|
||||
|
||||
func SHUFFLE(v2, v3, v4, v5, v6, v7, t1, t2 VecPhysical) {
|
||||
MOVO(v4, t1)
|
||||
MOVO(v5, v4)
|
||||
MOVO(t1, v5)
|
||||
MOVO(v6, t1)
|
||||
PUNPCKLQDQ(v6, t2)
|
||||
PUNPCKHQDQ(v7, v6)
|
||||
PUNPCKHQDQ(t2, v6)
|
||||
PUNPCKLQDQ(v7, t2)
|
||||
MOVO(t1, v7)
|
||||
MOVO(v2, t1)
|
||||
PUNPCKHQDQ(t2, v7)
|
||||
PUNPCKLQDQ(v3, t2)
|
||||
PUNPCKHQDQ(t2, v2)
|
||||
PUNPCKLQDQ(t1, t2)
|
||||
PUNPCKHQDQ(t2, v3)
|
||||
}
|
||||
|
||||
func SHUFFLE_INV(v2, v3, v4, v5, v6, v7, t1, t2 VecPhysical) {
|
||||
MOVO(v4, t1)
|
||||
MOVO(v5, v4)
|
||||
MOVO(t1, v5)
|
||||
MOVO(v2, t1)
|
||||
PUNPCKLQDQ(v2, t2)
|
||||
PUNPCKHQDQ(v3, v2)
|
||||
PUNPCKHQDQ(t2, v2)
|
||||
PUNPCKLQDQ(v3, t2)
|
||||
MOVO(t1, v3)
|
||||
MOVO(v6, t1)
|
||||
PUNPCKHQDQ(t2, v3)
|
||||
PUNPCKLQDQ(v7, t2)
|
||||
PUNPCKHQDQ(t2, v6)
|
||||
PUNPCKLQDQ(t1, t2)
|
||||
PUNPCKHQDQ(t2, v7)
|
||||
}
|
||||
|
||||
func HALF_ROUND(v0, v1, v2, v3, v4, v5, v6, v7 VecPhysical, m0, m1, m2, m3 Op, t0, c40, c48 VecPhysical) {
|
||||
PADDQ(m0, v0)
|
||||
PADDQ(m1, v1)
|
||||
PADDQ(v2, v0)
|
||||
PADDQ(v3, v1)
|
||||
PXOR(v0, v6)
|
||||
PXOR(v1, v7)
|
||||
PSHUFD(Imm(0xB1), v6, v6)
|
||||
PSHUFD(Imm(0xB1), v7, v7)
|
||||
PADDQ(v6, v4)
|
||||
PADDQ(v7, v5)
|
||||
PXOR(v4, v2)
|
||||
PXOR(v5, v3)
|
||||
PSHUFB(c40, v2)
|
||||
PSHUFB(c40, v3)
|
||||
PADDQ(m2, v0)
|
||||
PADDQ(m3, v1)
|
||||
PADDQ(v2, v0)
|
||||
PADDQ(v3, v1)
|
||||
PXOR(v0, v6)
|
||||
PXOR(v1, v7)
|
||||
PSHUFB(c48, v6)
|
||||
PSHUFB(c48, v7)
|
||||
PADDQ(v6, v4)
|
||||
PADDQ(v7, v5)
|
||||
PXOR(v4, v2)
|
||||
PXOR(v5, v3)
|
||||
MOVOU(v2, t0)
|
||||
PADDQ(v2, t0)
|
||||
PSRLQ(Imm(63), v2)
|
||||
PXOR(t0, v2)
|
||||
MOVOU(v3, t0)
|
||||
PADDQ(v3, t0)
|
||||
PSRLQ(Imm(63), v3)
|
||||
PXOR(t0, v3)
|
||||
}
|
||||
|
||||
func LOAD_MSG(m0, m1, m2, m3 VecPhysical, src GPPhysical, i0, i1, i2, i3, i4, i5, i6, i7 int) {
|
||||
MOVQ(Mem{Base: src}.Offset(i0*8), m0)
|
||||
PINSRQ(Imm(1), Mem{Base: src}.Offset(i1*8), m0)
|
||||
MOVQ(Mem{Base: src}.Offset(i2*8), m1)
|
||||
PINSRQ(Imm(1), Mem{Base: src}.Offset(i3*8), m1)
|
||||
MOVQ(Mem{Base: src}.Offset(i4*8), m2)
|
||||
PINSRQ(Imm(1), Mem{Base: src}.Offset(i5*8), m2)
|
||||
MOVQ(Mem{Base: src}.Offset(i6*8), m3)
|
||||
PINSRQ(Imm(1), Mem{Base: src}.Offset(i7*8), m3)
|
||||
}
|
||||
|
||||
func hashBlocksSSE4() {
|
||||
Implement("hashBlocksSSE4")
|
||||
Attributes(4)
|
||||
AllocLocal(288) // frame size = 272 + 16 byte alignment
|
||||
|
||||
Load(Param("h"), RAX)
|
||||
Load(Param("c"), RBX)
|
||||
Load(Param("flag"), RCX)
|
||||
Load(Param("blocks").Base(), RSI)
|
||||
Load(Param("blocks").Len(), RDI)
|
||||
|
||||
MOVQ(RSP, R10)
|
||||
ADDQ(Imm(15), R10)
|
||||
ANDQ(I32(-16), R10)
|
||||
|
||||
iv3 := iv3_DATA()
|
||||
MOVOU(iv3, X0)
|
||||
MOVO(X0, Mem{Base: R10}.Offset(0))
|
||||
XORQ(RCX, Mem{Base: R10}.Offset(0)) // 0(R10) = ·iv3 ^ (CX || 0)
|
||||
|
||||
c40 := c40_DATA()
|
||||
c48 := c48_DATA()
|
||||
MOVOU(c40, X13)
|
||||
MOVOU(c48, X14)
|
||||
|
||||
MOVOU(Mem{Base: AX}.Offset(0), X12)
|
||||
MOVOU(Mem{Base: AX}.Offset(16), X15)
|
||||
|
||||
MOVQ(Mem{Base: BX}.Offset(0), R8)
|
||||
MOVQ(Mem{Base: BX}.Offset(8), R9)
|
||||
|
||||
Label("loop")
|
||||
ADDQ(Imm(128), R8)
|
||||
CMPQ(R8, Imm(128))
|
||||
JGE(LabelRef("noinc"))
|
||||
INCQ(R9)
|
||||
|
||||
Label("noinc")
|
||||
MOVQ(R8, X8)
|
||||
PINSRQ(Imm(1), R9, X8)
|
||||
|
||||
iv0 := iv0_DATA()
|
||||
iv1 := iv1_DATA()
|
||||
iv2 := iv2_DATA()
|
||||
|
||||
MOVO(X12, X0)
|
||||
MOVO(X15, X1)
|
||||
MOVOU(Mem{Base: AX}.Offset(32), X2)
|
||||
MOVOU(Mem{Base: AX}.Offset(48), X3)
|
||||
MOVOU(iv0, X4)
|
||||
MOVOU(iv1, X5)
|
||||
MOVOU(iv2, X6)
|
||||
|
||||
PXOR(X8, X6)
|
||||
MOVO(Mem{Base: R10}.Offset(0), X7)
|
||||
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 0, 2, 4, 6, 1, 3, 5, 7)
|
||||
MOVO(X8, Mem{Base: R10}.Offset(16))
|
||||
MOVO(X9, Mem{Base: R10}.Offset(32))
|
||||
MOVO(X10, Mem{Base: R10}.Offset(48))
|
||||
MOVO(X11, Mem{Base: R10}.Offset(64))
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 8, 10, 12, 14, 9, 11, 13, 15)
|
||||
MOVO(X8, Mem{Base: R10}.Offset(80))
|
||||
MOVO(X9, Mem{Base: R10}.Offset(96))
|
||||
MOVO(X10, Mem{Base: R10}.Offset(112))
|
||||
MOVO(X11, Mem{Base: R10}.Offset(128))
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 14, 4, 9, 13, 10, 8, 15, 6)
|
||||
MOVO(X8, Mem{Base: R10}.Offset(144))
|
||||
MOVO(X9, Mem{Base: R10}.Offset(160))
|
||||
MOVO(X10, Mem{Base: R10}.Offset(176))
|
||||
MOVO(X11, Mem{Base: R10}.Offset(192))
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 1, 0, 11, 5, 12, 2, 7, 3)
|
||||
MOVO(X8, Mem{Base: R10}.Offset(208))
|
||||
MOVO(X9, Mem{Base: R10}.Offset(224))
|
||||
MOVO(X10, Mem{Base: R10}.Offset(240))
|
||||
MOVO(X11, Mem{Base: R10}.Offset(256))
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 11, 12, 5, 15, 8, 0, 2, 13)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 10, 3, 7, 9, 14, 6, 1, 4)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 7, 3, 13, 11, 9, 1, 12, 14)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 2, 5, 4, 15, 6, 10, 0, 8)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 9, 5, 2, 10, 0, 7, 4, 15)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 14, 11, 6, 3, 1, 12, 8, 13)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 2, 6, 0, 8, 12, 10, 11, 3)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 4, 7, 15, 1, 13, 5, 14, 9)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 12, 1, 14, 4, 5, 15, 13, 10)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 0, 6, 9, 8, 7, 3, 2, 11)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 13, 7, 12, 3, 11, 14, 1, 9)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 5, 15, 8, 2, 0, 4, 6, 10)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 6, 14, 11, 0, 15, 9, 3, 8)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 12, 13, 1, 10, 2, 7, 4, 5)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 10, 8, 7, 1, 2, 4, 6, 5)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
LOAD_MSG(X8, X9, X10, X11, SI, 15, 9, 3, 13, 11, 14, 12, 0)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, Mem{Base: R10}.Offset(16), Mem{Base: R10}.Offset(32), Mem{Base: R10}.Offset(48), Mem{Base: R10}.Offset(64), X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, Mem{Base: R10}.Offset(80), Mem{Base: R10}.Offset(96), Mem{Base: R10}.Offset(112), Mem{Base: R10}.Offset(128), X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, Mem{Base: R10}.Offset(144), Mem{Base: R10}.Offset(160), Mem{Base: R10}.Offset(176), Mem{Base: R10}.Offset(192), X11, X13, X14)
|
||||
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, Mem{Base: R10}.Offset(208), Mem{Base: R10}.Offset(224), Mem{Base: R10}.Offset(240), Mem{Base: R10}.Offset(256), X11, X13, X14)
|
||||
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
|
||||
|
||||
MOVOU(Mem{Base: AX}.Offset(32), X10)
|
||||
MOVOU(Mem{Base: AX}.Offset(48), X11)
|
||||
PXOR(X0, X12)
|
||||
PXOR(X1, X15)
|
||||
PXOR(X2, X10)
|
||||
PXOR(X3, X11)
|
||||
PXOR(X4, X12)
|
||||
PXOR(X5, X15)
|
||||
PXOR(X6, X10)
|
||||
PXOR(X7, X11)
|
||||
MOVOU(X10, Mem{Base: AX}.Offset(32))
|
||||
MOVOU(X11, Mem{Base: AX}.Offset(48))
|
||||
|
||||
LEAQ(Mem{Base: SI}.Offset(128), RSI)
|
||||
SUBQ(Imm(128), RDI)
|
||||
JNE(LabelRef("loop"))
|
||||
|
||||
MOVOU(X12, Mem{Base: AX}.Offset(0))
|
||||
MOVOU(X15, Mem{Base: AX}.Offset(16))
|
||||
|
||||
MOVQ(R8, Mem{Base: BX}.Offset(0))
|
||||
MOVQ(R9, Mem{Base: BX}.Offset(8))
|
||||
|
||||
RET()
|
||||
}
|
||||
|
||||
// #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~DATA SECTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
|
||||
|
||||
func iv0_DATA() Mem {
|
||||
if iv0_DATA_ptr != nil {
|
||||
return *iv0_DATA_ptr
|
||||
}
|
||||
|
||||
iv0 := GLOBL(ThatPeskyUnicodeDot+"iv0", NOPTR|RODATA)
|
||||
iv0_DATA_ptr = &iv0
|
||||
DATA(0x00, U64(0x6a09e667f3bcc908))
|
||||
DATA(0x08, U64(0xbb67ae8584caa73b))
|
||||
return iv0
|
||||
}
|
||||
|
||||
func iv1_DATA() Mem {
|
||||
if iv1_DATA_ptr != nil {
|
||||
return *iv1_DATA_ptr
|
||||
}
|
||||
|
||||
iv1 := GLOBL(ThatPeskyUnicodeDot+"iv1", NOPTR|RODATA)
|
||||
iv1_DATA_ptr = &iv1
|
||||
DATA(0x00, U64(0x3c6ef372fe94f82b))
|
||||
DATA(0x08, U64(0xa54ff53a5f1d36f1))
|
||||
return iv1
|
||||
}
|
||||
|
||||
func iv2_DATA() Mem {
|
||||
if iv2_DATA_ptr != nil {
|
||||
return *iv2_DATA_ptr
|
||||
}
|
||||
|
||||
iv2 := GLOBL(ThatPeskyUnicodeDot+"iv2", NOPTR|RODATA)
|
||||
iv2_DATA_ptr = &iv2
|
||||
DATA(0x00, U64(0x510e527fade682d1))
|
||||
DATA(0x08, U64(0x9b05688c2b3e6c1f))
|
||||
return iv2
|
||||
}
|
||||
|
||||
func iv3_DATA() Mem {
|
||||
if iv3_DATA_ptr != nil {
|
||||
return *iv3_DATA_ptr
|
||||
}
|
||||
|
||||
iv3 := GLOBL(ThatPeskyUnicodeDot+"iv3", NOPTR|RODATA)
|
||||
iv3_DATA_ptr = &iv3
|
||||
DATA(0x00, U64(0x1f83d9abfb41bd6b))
|
||||
DATA(0x08, U64(0x5be0cd19137e2179))
|
||||
return iv3
|
||||
}
|
||||
|
||||
func c40_DATA() Mem {
|
||||
if c40_DATA_ptr != nil {
|
||||
return *c40_DATA_ptr
|
||||
}
|
||||
|
||||
c40 := GLOBL(ThatPeskyUnicodeDot+"c40", NOPTR|RODATA)
|
||||
c40_DATA_ptr = &c40
|
||||
DATA(0x00, U64(0x0201000706050403))
|
||||
DATA(0x08, U64(0x0a09080f0e0d0c0b))
|
||||
return c40
|
||||
}
|
||||
|
||||
func c48_DATA() Mem {
|
||||
if c48_DATA_ptr != nil {
|
||||
return *c48_DATA_ptr
|
||||
}
|
||||
|
||||
c48 := GLOBL(ThatPeskyUnicodeDot+"c48", NOPTR|RODATA)
|
||||
c48_DATA_ptr = &c48
|
||||
DATA(0x00, U64(0x0100070605040302))
|
||||
DATA(0x08, U64(0x09080f0e0d0c0b0a))
|
||||
return c48
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
module blake2b/_asm
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/mmcloughlin/avo v0.6.0
|
||||
golang.org/x/crypto v0.33.0
|
||||
)
|
||||
|
||||
require (
|
||||
golang.org/x/mod v0.20.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/tools v0.24.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
|
||||
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
|
||||
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
|
||||
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
|
||||
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
|
||||
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
|
||||
+4492
-677
File diff suppressed because it is too large
Load Diff
+1427
-264
File diff suppressed because it is too large
Load Diff
@@ -14,6 +14,8 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ hash.XOF = (*xof)(nil)
|
||||
|
||||
func fromHex(s string) []byte {
|
||||
b, err := hex.DecodeString(s)
|
||||
if err != nil {
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
|
||||
// XOF defines the interface to hash functions that
|
||||
// support arbitrary-length output.
|
||||
//
|
||||
// New callers should prefer the standard library [hash.XOF].
|
||||
type XOF interface {
|
||||
// Write absorbs more data into the hash's state. It panics if called
|
||||
// after Read.
|
||||
@@ -47,6 +49,8 @@ const maxOutputLength = (1 << 32) * 64
|
||||
//
|
||||
// A non-nil key turns the hash into a MAC. The key must between
|
||||
// zero and 32 bytes long.
|
||||
//
|
||||
// The result can be safely interface-upgraded to [hash.XOF].
|
||||
func NewXOF(size uint32, key []byte) (XOF, error) {
|
||||
if len(key) > Size {
|
||||
return nil, errKeySize
|
||||
@@ -93,6 +97,10 @@ func (x *xof) Clone() XOF {
|
||||
return &clone
|
||||
}
|
||||
|
||||
func (x *xof) BlockSize() int {
|
||||
return x.d.BlockSize()
|
||||
}
|
||||
|
||||
func (x *xof) Reset() {
|
||||
x.cfg[0] = byte(Size)
|
||||
binary.LittleEndian.PutUint32(x.cfg[4:], uint32(Size)) // leaf length
|
||||
|
||||
@@ -0,0 +1,525 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
. "github.com/mmcloughlin/avo/build"
|
||||
"github.com/mmcloughlin/avo/ir"
|
||||
. "github.com/mmcloughlin/avo/operand"
|
||||
. "github.com/mmcloughlin/avo/reg"
|
||||
_ "golang.org/x/crypto/blake2s"
|
||||
)
|
||||
|
||||
//go:generate go run . -out ../blake2s_amd64.s -pkg blake2s
|
||||
|
||||
func main() {
|
||||
Package("golang.org/x/crypto/blake2s")
|
||||
ConstraintExpr("amd64,gc,!purego")
|
||||
hashBlocksSSE2()
|
||||
hashBlocksSSSE3()
|
||||
hashBlocksSSE4()
|
||||
Generate()
|
||||
}
|
||||
|
||||
func ROTL_SSE2(n uint64, t, v VecPhysical) {
|
||||
MOVO(v, t)
|
||||
PSLLL(Imm(n), t)
|
||||
PSRLL(Imm(32-n), v)
|
||||
PXOR(t, v)
|
||||
}
|
||||
|
||||
func ROTL_SSSE3(c, v VecPhysical) {
|
||||
PSHUFB(c, v)
|
||||
}
|
||||
|
||||
func ROUND_SSE2(v0, v1, v2, v3 VecPhysical, m0, m1, m2, m3 Mem, t VecPhysical) {
|
||||
PADDL(m0, v0)
|
||||
PADDL(v1, v0)
|
||||
PXOR(v0, v3)
|
||||
ROTL_SSE2(16, t, v3)
|
||||
PADDL(v3, v2)
|
||||
PXOR(v2, v1)
|
||||
ROTL_SSE2(20, t, v1)
|
||||
PADDL(m1, v0)
|
||||
PADDL(v1, v0)
|
||||
PXOR(v0, v3)
|
||||
ROTL_SSE2(24, t, v3)
|
||||
PADDL(v3, v2)
|
||||
PXOR(v2, v1)
|
||||
ROTL_SSE2(25, t, v1)
|
||||
PSHUFL(Imm(0x39), v1, v1)
|
||||
PSHUFL(Imm(0x4E), v2, v2)
|
||||
PSHUFL(Imm(0x93), v3, v3)
|
||||
PADDL(m2, v0)
|
||||
PADDL(v1, v0)
|
||||
PXOR(v0, v3)
|
||||
ROTL_SSE2(16, t, v3)
|
||||
PADDL(v3, v2)
|
||||
PXOR(v2, v1)
|
||||
ROTL_SSE2(20, t, v1)
|
||||
PADDL(m3, v0)
|
||||
PADDL(v1, v0)
|
||||
PXOR(v0, v3)
|
||||
ROTL_SSE2(24, t, v3)
|
||||
PADDL(v3, v2)
|
||||
PXOR(v2, v1)
|
||||
ROTL_SSE2(25, t, v1)
|
||||
PSHUFL(Imm(0x39), v3, v3)
|
||||
PSHUFL(Imm(0x4E), v2, v2)
|
||||
PSHUFL(Imm(0x93), v1, v1)
|
||||
}
|
||||
|
||||
func ROUND_SSSE3(v0, v1, v2, v3 VecPhysical, m0, m1, m2, m3 Op, t, c16, c8 VecPhysical) {
|
||||
PADDL(m0, v0)
|
||||
PADDL(v1, v0)
|
||||
PXOR(v0, v3)
|
||||
ROTL_SSSE3(c16, v3)
|
||||
PADDL(v3, v2)
|
||||
PXOR(v2, v1)
|
||||
ROTL_SSE2(20, t, v1)
|
||||
PADDL(m1, v0)
|
||||
PADDL(v1, v0)
|
||||
PXOR(v0, v3)
|
||||
ROTL_SSSE3(c8, v3)
|
||||
PADDL(v3, v2)
|
||||
PXOR(v2, v1)
|
||||
ROTL_SSE2(25, t, v1)
|
||||
PSHUFL(Imm(0x39), v1, v1)
|
||||
PSHUFL(Imm(0x4E), v2, v2)
|
||||
PSHUFL(Imm(0x93), v3, v3)
|
||||
PADDL(m2, v0)
|
||||
PADDL(v1, v0)
|
||||
PXOR(v0, v3)
|
||||
ROTL_SSSE3(c16, v3)
|
||||
PADDL(v3, v2)
|
||||
PXOR(v2, v1)
|
||||
ROTL_SSE2(20, t, v1)
|
||||
PADDL(m3, v0)
|
||||
PADDL(v1, v0)
|
||||
PXOR(v0, v3)
|
||||
ROTL_SSSE3(c8, v3)
|
||||
PADDL(v3, v2)
|
||||
PXOR(v2, v1)
|
||||
ROTL_SSE2(25, t, v1)
|
||||
PSHUFL(Imm(0x39), v3, v3)
|
||||
PSHUFL(Imm(0x4E), v2, v2)
|
||||
PSHUFL(Imm(0x93), v1, v1)
|
||||
}
|
||||
|
||||
func LOAD_MSG_SSE4(m0, m1, m2, m3 VecPhysical, src GPPhysical, i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15 int) {
|
||||
// Hack to get Avo to emit a MOVL instruction with a VecPhysical as the destination
|
||||
Instruction(&ir.Instruction{Opcode: "MOVL", Operands: []Op{Mem{Base: src}.Offset(i0 * 4), m0}})
|
||||
PINSRD(Imm(1), Mem{Base: src}.Offset(i1*4), m0)
|
||||
PINSRD(Imm(2), Mem{Base: src}.Offset(i2*4), m0)
|
||||
PINSRD(Imm(3), Mem{Base: src}.Offset(i3*4), m0)
|
||||
Instruction(&ir.Instruction{Opcode: "MOVL", Operands: []Op{Mem{Base: src}.Offset(i4 * 4), m1}})
|
||||
PINSRD(Imm(1), Mem{Base: src}.Offset(i5*4), m1)
|
||||
PINSRD(Imm(2), Mem{Base: src}.Offset(i6*4), m1)
|
||||
PINSRD(Imm(3), Mem{Base: src}.Offset(i7*4), m1)
|
||||
Instruction(&ir.Instruction{Opcode: "MOVL", Operands: []Op{Mem{Base: src}.Offset(i8 * 4), m2}})
|
||||
PINSRD(Imm(1), Mem{Base: src}.Offset(i9*4), m2)
|
||||
PINSRD(Imm(2), Mem{Base: src}.Offset(i10*4), m2)
|
||||
PINSRD(Imm(3), Mem{Base: src}.Offset(i11*4), m2)
|
||||
Instruction(&ir.Instruction{Opcode: "MOVL", Operands: []Op{Mem{Base: src}.Offset(i12 * 4), m3}})
|
||||
PINSRD(Imm(1), Mem{Base: src}.Offset(i13*4), m3)
|
||||
PINSRD(Imm(2), Mem{Base: src}.Offset(i14*4), m3)
|
||||
PINSRD(Imm(3), Mem{Base: src}.Offset(i15*4), m3)
|
||||
}
|
||||
|
||||
func PRECOMPUTE_MSG(dst GPPhysical, off int, src, R8, R9, R10, R11, R12, R13, R14, R15 GPPhysical) {
|
||||
MOVQ(Mem{Base: src}.Offset(0*4), R8)
|
||||
MOVQ(Mem{Base: src}.Offset(2*4), R9)
|
||||
MOVQ(Mem{Base: src}.Offset(4*4), R10)
|
||||
MOVQ(Mem{Base: src}.Offset(6*4), R11)
|
||||
MOVQ(Mem{Base: src}.Offset(8*4), R12)
|
||||
MOVQ(Mem{Base: src}.Offset(10*4), R13)
|
||||
MOVQ(Mem{Base: src}.Offset(12*4), R14)
|
||||
MOVQ(Mem{Base: src}.Offset(14*4), R15)
|
||||
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(0*4+off+0))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(9*4+off+64))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(5*4+off+128))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(14*4+off+192))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(4*4+off+256))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(2*4+off+320))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(8*4+off+384))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(12*4+off+448))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(3*4+off+512))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(15*4+off+576))
|
||||
SHRQ(Imm(32), R8)
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(4*4+off+0))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(8*4+off+64))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(14*4+off+128))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(5*4+off+192))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(12*4+off+256))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(11*4+off+320))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(1*4+off+384))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(6*4+off+448))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(10*4+off+512))
|
||||
MOVL(R8L, Mem{Base: dst}.Offset(3*4+off+576))
|
||||
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(1*4+off+0))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(13*4+off+64))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(6*4+off+128))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(8*4+off+192))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(2*4+off+256))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(0*4+off+320))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(14*4+off+384))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(11*4+off+448))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(12*4+off+512))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(4*4+off+576))
|
||||
SHRQ(Imm(32), R9)
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(5*4+off+0))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(15*4+off+64))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(9*4+off+128))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(1*4+off+192))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(11*4+off+256))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(7*4+off+320))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(13*4+off+384))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(3*4+off+448))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(6*4+off+512))
|
||||
MOVL(R9L, Mem{Base: dst}.Offset(10*4+off+576))
|
||||
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(2*4+off+0))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(1*4+off+64))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(15*4+off+128))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(10*4+off+192))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(6*4+off+256))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(8*4+off+320))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(3*4+off+384))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(13*4+off+448))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(14*4+off+512))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(5*4+off+576))
|
||||
SHRQ(Imm(32), R10)
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(6*4+off+0))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(11*4+off+64))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(2*4+off+128))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(9*4+off+192))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(1*4+off+256))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(13*4+off+320))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(4*4+off+384))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(8*4+off+448))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(15*4+off+512))
|
||||
MOVL(R10L, Mem{Base: dst}.Offset(7*4+off+576))
|
||||
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(3*4+off+0))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(7*4+off+64))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(13*4+off+128))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(12*4+off+192))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(10*4+off+256))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(1*4+off+320))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(9*4+off+384))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(14*4+off+448))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(0*4+off+512))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(6*4+off+576))
|
||||
SHRQ(Imm(32), R11)
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(7*4+off+0))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(14*4+off+64))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(10*4+off+128))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(0*4+off+192))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(5*4+off+256))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(9*4+off+320))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(12*4+off+384))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(1*4+off+448))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(13*4+off+512))
|
||||
MOVL(R11L, Mem{Base: dst}.Offset(2*4+off+576))
|
||||
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(8*4+off+0))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(5*4+off+64))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(4*4+off+128))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(15*4+off+192))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(14*4+off+256))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(3*4+off+320))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(11*4+off+384))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(10*4+off+448))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(7*4+off+512))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(1*4+off+576))
|
||||
SHRQ(Imm(32), R12)
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(12*4+off+0))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(2*4+off+64))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(11*4+off+128))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(4*4+off+192))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(0*4+off+256))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(15*4+off+320))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(10*4+off+384))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(7*4+off+448))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(5*4+off+512))
|
||||
MOVL(R12L, Mem{Base: dst}.Offset(9*4+off+576))
|
||||
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(9*4+off+0))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(4*4+off+64))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(8*4+off+128))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(13*4+off+192))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(3*4+off+256))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(5*4+off+320))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(7*4+off+384))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(15*4+off+448))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(11*4+off+512))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(0*4+off+576))
|
||||
SHRQ(Imm(32), R13)
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(13*4+off+0))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(10*4+off+64))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(0*4+off+128))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(3*4+off+192))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(9*4+off+256))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(6*4+off+320))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(15*4+off+384))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(4*4+off+448))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(2*4+off+512))
|
||||
MOVL(R13L, Mem{Base: dst}.Offset(12*4+off+576))
|
||||
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(10*4+off+0))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(12*4+off+64))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(1*4+off+128))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(6*4+off+192))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(13*4+off+256))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(4*4+off+320))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(0*4+off+384))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(2*4+off+448))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(8*4+off+512))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(14*4+off+576))
|
||||
SHRQ(Imm(32), R14)
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(14*4+off+0))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(3*4+off+64))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(7*4+off+128))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(2*4+off+192))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(15*4+off+256))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(12*4+off+320))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(6*4+off+384))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(0*4+off+448))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(9*4+off+512))
|
||||
MOVL(R14L, Mem{Base: dst}.Offset(11*4+off+576))
|
||||
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(11*4+off+0))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(0*4+off+64))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(12*4+off+128))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(7*4+off+192))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(8*4+off+256))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(14*4+off+320))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(2*4+off+384))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(5*4+off+448))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(1*4+off+512))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(13*4+off+576))
|
||||
SHRQ(Imm(32), R15)
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(15*4+off+0))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(6*4+off+64))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(3*4+off+128))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(11*4+off+192))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(7*4+off+256))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(10*4+off+320))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(5*4+off+384))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(9*4+off+448))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(4*4+off+512))
|
||||
MOVL(R15L, Mem{Base: dst}.Offset(8*4+off+576))
|
||||
}
|
||||
|
||||
func BLAKE2s_SSE2() {
|
||||
PRECOMPUTE_MSG(BP, 16, SI, R8, R9, R10, R11, R12, R13, R14, R15)
|
||||
for i := 0; i < 10; i++ {
|
||||
ROUND_SSE2(X4, X5, X6, X7, Mem{Base: BP}.Offset(16+64*i), Mem{Base: BP}.Offset(32+64*i), Mem{Base: BP}.Offset(48+64*i), Mem{Base: BP}.Offset(64+64*i), X8)
|
||||
}
|
||||
}
|
||||
|
||||
func BLAKE2s_SSSE3() {
|
||||
PRECOMPUTE_MSG(BP, 16, SI, R8, R9, R10, R11, R12, R13, R14, R15)
|
||||
for i := 0; i < 10; i++ {
|
||||
ROUND_SSSE3(X4, X5, X6, X7, Mem{Base: BP}.Offset(16+64*i), Mem{Base: BP}.Offset(32+64*i), Mem{Base: BP}.Offset(48+64*i), Mem{Base: BP}.Offset(64+64*i), X8, X13, X14)
|
||||
}
|
||||
}
|
||||
|
||||
func BLAKE2s_SSE4() {
|
||||
LOAD_MSG_SSE4(X8, X9, X10, X11, SI, 0, 2, 4, 6, 1, 3, 5, 7, 8, 10, 12, 14, 9, 11, 13, 15)
|
||||
ROUND_SSSE3(X4, X5, X6, X7, X8, X9, X10, X11, X8, X13, X14)
|
||||
LOAD_MSG_SSE4(X8, X9, X10, X11, SI, 14, 4, 9, 13, 10, 8, 15, 6, 1, 0, 11, 5, 12, 2, 7, 3)
|
||||
ROUND_SSSE3(X4, X5, X6, X7, X8, X9, X10, X11, X8, X13, X14)
|
||||
LOAD_MSG_SSE4(X8, X9, X10, X11, SI, 11, 12, 5, 15, 8, 0, 2, 13, 10, 3, 7, 9, 14, 6, 1, 4)
|
||||
ROUND_SSSE3(X4, X5, X6, X7, X8, X9, X10, X11, X8, X13, X14)
|
||||
LOAD_MSG_SSE4(X8, X9, X10, X11, SI, 7, 3, 13, 11, 9, 1, 12, 14, 2, 5, 4, 15, 6, 10, 0, 8)
|
||||
ROUND_SSSE3(X4, X5, X6, X7, X8, X9, X10, X11, X8, X13, X14)
|
||||
LOAD_MSG_SSE4(X8, X9, X10, X11, SI, 9, 5, 2, 10, 0, 7, 4, 15, 14, 11, 6, 3, 1, 12, 8, 13)
|
||||
ROUND_SSSE3(X4, X5, X6, X7, X8, X9, X10, X11, X8, X13, X14)
|
||||
LOAD_MSG_SSE4(X8, X9, X10, X11, SI, 2, 6, 0, 8, 12, 10, 11, 3, 4, 7, 15, 1, 13, 5, 14, 9)
|
||||
ROUND_SSSE3(X4, X5, X6, X7, X8, X9, X10, X11, X8, X13, X14)
|
||||
LOAD_MSG_SSE4(X8, X9, X10, X11, SI, 12, 1, 14, 4, 5, 15, 13, 10, 0, 6, 9, 8, 7, 3, 2, 11)
|
||||
ROUND_SSSE3(X4, X5, X6, X7, X8, X9, X10, X11, X8, X13, X14)
|
||||
LOAD_MSG_SSE4(X8, X9, X10, X11, SI, 13, 7, 12, 3, 11, 14, 1, 9, 5, 15, 8, 2, 0, 4, 6, 10)
|
||||
ROUND_SSSE3(X4, X5, X6, X7, X8, X9, X10, X11, X8, X13, X14)
|
||||
LOAD_MSG_SSE4(X8, X9, X10, X11, SI, 6, 14, 11, 0, 15, 9, 3, 8, 12, 13, 1, 10, 2, 7, 4, 5)
|
||||
ROUND_SSSE3(X4, X5, X6, X7, X8, X9, X10, X11, X8, X13, X14)
|
||||
LOAD_MSG_SSE4(X8, X9, X10, X11, SI, 10, 8, 7, 1, 2, 4, 6, 5, 15, 9, 3, 13, 11, 14, 12, 0)
|
||||
ROUND_SSSE3(X4, X5, X6, X7, X8, X9, X10, X11, X8, X13, X14)
|
||||
}
|
||||
|
||||
func HASH_BLOCKS(h, c, flag, blocks_base, blocks_len Mem, BLAKE2s_FUNC func()) {
|
||||
MOVQ(h, RAX)
|
||||
MOVQ(c, RBX)
|
||||
MOVL(flag, ECX)
|
||||
MOVQ(blocks_base, RSI)
|
||||
MOVQ(blocks_len, RDX)
|
||||
|
||||
MOVQ(RSP, RBP)
|
||||
ADDQ(Imm(15), RBP)
|
||||
ANDQ(I32(^15), RBP)
|
||||
|
||||
MOVQ(Mem{Base: BX}.Offset(0), R9)
|
||||
MOVQ(R9, Mem{Base: BP}.Offset(0))
|
||||
MOVQ(RCX, Mem{Base: BP}.Offset(8))
|
||||
|
||||
MOVOU(Mem{Base: AX}.Offset(0), X0)
|
||||
MOVOU(Mem{Base: AX}.Offset(16), X1)
|
||||
|
||||
iv0 := iv0_DATA()
|
||||
iv1 := iv1_DATA()
|
||||
MOVOU(iv0, X2)
|
||||
MOVOU(iv1, X3)
|
||||
|
||||
counter := counter_DATA()
|
||||
rol16 := rol16_DATA()
|
||||
rol8 := rol8_DATA()
|
||||
MOVOU(counter, X12)
|
||||
MOVOU(rol16, X13)
|
||||
MOVOU(rol8, X14)
|
||||
MOVO(Mem{Base: BP}.Offset(0), X15)
|
||||
|
||||
Label("loop")
|
||||
MOVO(X0, X4)
|
||||
MOVO(X1, X5)
|
||||
MOVO(X2, X6)
|
||||
MOVO(X3, X7)
|
||||
|
||||
PADDQ(X12, X15)
|
||||
PXOR(X15, X7)
|
||||
|
||||
BLAKE2s_FUNC()
|
||||
|
||||
PXOR(X4, X0)
|
||||
PXOR(X5, X1)
|
||||
PXOR(X6, X0)
|
||||
PXOR(X7, X1)
|
||||
|
||||
LEAQ(Mem{Base: SI}.Offset(64), RSI)
|
||||
SUBQ(Imm(64), RDX)
|
||||
JNE(LabelRef("loop"))
|
||||
|
||||
MOVO(X15, Mem{Base: BP}.Offset(0))
|
||||
MOVQ(Mem{Base: BP}.Offset(0), R9)
|
||||
MOVQ(R9, Mem{Base: BX}.Offset(0))
|
||||
|
||||
MOVOU(X0, Mem{Base: AX}.Offset(0))
|
||||
MOVOU(X1, Mem{Base: AX}.Offset(16))
|
||||
}
|
||||
|
||||
func hashBlocksSSE2() {
|
||||
Implement("hashBlocksSSE2")
|
||||
Attributes(0)
|
||||
AllocLocal(672) // frame = 656 + 16 byte alignment
|
||||
|
||||
h := NewParamAddr("h", 0)
|
||||
c := NewParamAddr("c", 8)
|
||||
flag := NewParamAddr("flag", 16)
|
||||
blocks_base := NewParamAddr("blocks_base", 24)
|
||||
blocks_len := NewParamAddr("blocks_len", 32)
|
||||
|
||||
HASH_BLOCKS(h, c, flag, blocks_base, blocks_len, BLAKE2s_SSE2)
|
||||
RET()
|
||||
}
|
||||
|
||||
func hashBlocksSSSE3() {
|
||||
Implement("hashBlocksSSSE3")
|
||||
Attributes(0)
|
||||
AllocLocal(672) // frame = 656 + 16 byte alignment
|
||||
|
||||
h := NewParamAddr("h", 0)
|
||||
c := NewParamAddr("c", 8)
|
||||
flag := NewParamAddr("flag", 16)
|
||||
blocks_base := NewParamAddr("blocks_base", 24)
|
||||
blocks_len := NewParamAddr("blocks_len", 32)
|
||||
|
||||
HASH_BLOCKS(h, c, flag, blocks_base, blocks_len, BLAKE2s_SSSE3)
|
||||
RET()
|
||||
}
|
||||
|
||||
func hashBlocksSSE4() {
|
||||
Implement("hashBlocksSSE4")
|
||||
Attributes(0)
|
||||
AllocLocal(32) // frame = 16 + 16 byte alignment
|
||||
|
||||
h := NewParamAddr("h", 0)
|
||||
c := NewParamAddr("c", 8)
|
||||
flag := NewParamAddr("flag", 16)
|
||||
blocks_base := NewParamAddr("blocks_base", 24)
|
||||
blocks_len := NewParamAddr("blocks_len", 32)
|
||||
|
||||
HASH_BLOCKS(h, c, flag, blocks_base, blocks_len, BLAKE2s_SSE4)
|
||||
RET()
|
||||
}
|
||||
|
||||
// ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~DATA SECTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
|
||||
|
||||
var iv0_DATA_ptr, iv1_DATA_ptr, rol16_DATA_ptr, rol8_DATA_ptr, counter_DATA_ptr *Mem
|
||||
|
||||
func iv0_DATA() Mem {
|
||||
if iv0_DATA_ptr != nil {
|
||||
return *iv0_DATA_ptr
|
||||
}
|
||||
|
||||
iv0_DATA := GLOBL("iv0", NOPTR|RODATA)
|
||||
iv0_DATA_ptr = &iv0_DATA
|
||||
DATA(0x00, U32(0x6a09e667))
|
||||
DATA(0x04, U32(0xbb67ae85))
|
||||
DATA(0x08, U32(0x3c6ef372))
|
||||
DATA(0x0c, U32(0xa54ff53a))
|
||||
return iv0_DATA
|
||||
}
|
||||
|
||||
func iv1_DATA() Mem {
|
||||
if iv1_DATA_ptr != nil {
|
||||
return *iv1_DATA_ptr
|
||||
}
|
||||
|
||||
iv1_DATA := GLOBL("iv1", NOPTR|RODATA)
|
||||
iv1_DATA_ptr = &iv1_DATA
|
||||
DATA(0x00, U32(0x510e527f))
|
||||
DATA(0x04, U32(0x9b05688c))
|
||||
DATA(0x08, U32(0x1f83d9ab))
|
||||
DATA(0x0c, U32(0x5be0cd19))
|
||||
return iv1_DATA
|
||||
}
|
||||
|
||||
func rol16_DATA() Mem {
|
||||
if rol16_DATA_ptr != nil {
|
||||
return *rol16_DATA_ptr
|
||||
}
|
||||
|
||||
rol16_DATA := GLOBL("rol16", NOPTR|RODATA)
|
||||
rol16_DATA_ptr = &rol16_DATA
|
||||
DATA(0x00, U64(0x0504070601000302))
|
||||
DATA(0x08, U64(0x0D0C0F0E09080B0A))
|
||||
return rol16_DATA
|
||||
}
|
||||
|
||||
func rol8_DATA() Mem {
|
||||
if rol8_DATA_ptr != nil {
|
||||
return *rol8_DATA_ptr
|
||||
}
|
||||
|
||||
rol8_DATA := GLOBL("rol8", NOPTR|RODATA)
|
||||
rol8_DATA_ptr = &rol8_DATA
|
||||
DATA(0x00, U64(0x0407060500030201))
|
||||
DATA(0x08, U64(0x0C0F0E0D080B0A09))
|
||||
return rol8_DATA
|
||||
}
|
||||
|
||||
func counter_DATA() Mem {
|
||||
if counter_DATA_ptr != nil {
|
||||
return *counter_DATA_ptr
|
||||
}
|
||||
|
||||
counter_DATA := GLOBL("counter", NOPTR|RODATA)
|
||||
counter_DATA_ptr = &counter_DATA
|
||||
DATA(0x00, U64(0x0000000000000040))
|
||||
DATA(0x08, U64(0x0000000000000000))
|
||||
return counter_DATA
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
module blake2s/_asm
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/mmcloughlin/avo v0.6.0
|
||||
golang.org/x/crypto v0.33.0
|
||||
)
|
||||
|
||||
require (
|
||||
golang.org/x/mod v0.20.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/tools v0.24.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
|
||||
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
|
||||
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
|
||||
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
|
||||
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
|
||||
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
|
||||
+2160
-419
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -5,7 +5,7 @@
|
||||
package bn256
|
||||
|
||||
// For details of the algorithms used, see "Multiplication and Squaring on
|
||||
// Pairing-Friendly Fields, Devegili et al.
|
||||
// Pairing-Friendly Fields", Devegili et al.
|
||||
// http://eprint.iacr.org/2006/471.pdf.
|
||||
|
||||
import (
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
package bn256
|
||||
|
||||
// For details of the algorithms used, see "Multiplication and Squaring on
|
||||
// Pairing-Friendly Fields, Devegili et al.
|
||||
// Pairing-Friendly Fields", Devegili et al.
|
||||
// http://eprint.iacr.org/2006/471.pdf.
|
||||
|
||||
import (
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
package bn256
|
||||
|
||||
// For details of the algorithms used, see "Multiplication and Squaring on
|
||||
// Pairing-Friendly Fields, Devegili et al.
|
||||
// Pairing-Friendly Fields", Devegili et al.
|
||||
// http://eprint.iacr.org/2006/471.pdf.
|
||||
|
||||
import (
|
||||
|
||||
@@ -29,7 +29,7 @@ loop:
|
||||
MOVD $NUM_ROUNDS, R21
|
||||
VLD1 (R11), [V30.S4, V31.S4]
|
||||
|
||||
// load contants
|
||||
// load constants
|
||||
// VLD4R (R10), [V0.S4, V1.S4, V2.S4, V3.S4]
|
||||
WORD $0x4D60E940
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build (!arm64 && !s390x && !ppc64le) || !gc || purego
|
||||
//go:build (!arm64 && !s390x && !ppc64 && !ppc64le) || !gc || purego
|
||||
|
||||
package chacha20
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build gc && !purego
|
||||
//go:build gc && !purego && (ppc64 || ppc64le)
|
||||
|
||||
package chacha20
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
// The differences in this and the original implementation are
|
||||
// due to the calling conventions and initialization of constants.
|
||||
|
||||
//go:build gc && !purego
|
||||
//go:build gc && !purego && (ppc64 || ppc64le)
|
||||
|
||||
#include "textflag.h"
|
||||
|
||||
@@ -36,32 +36,68 @@
|
||||
// for VPERMXOR
|
||||
#define MASK R18
|
||||
|
||||
DATA consts<>+0x00(SB)/8, $0x3320646e61707865
|
||||
DATA consts<>+0x08(SB)/8, $0x6b20657479622d32
|
||||
DATA consts<>+0x10(SB)/8, $0x0000000000000001
|
||||
DATA consts<>+0x18(SB)/8, $0x0000000000000000
|
||||
DATA consts<>+0x20(SB)/8, $0x0000000000000004
|
||||
DATA consts<>+0x28(SB)/8, $0x0000000000000000
|
||||
DATA consts<>+0x30(SB)/8, $0x0a0b08090e0f0c0d
|
||||
DATA consts<>+0x38(SB)/8, $0x0203000106070405
|
||||
DATA consts<>+0x40(SB)/8, $0x090a0b080d0e0f0c
|
||||
DATA consts<>+0x48(SB)/8, $0x0102030005060704
|
||||
DATA consts<>+0x50(SB)/8, $0x6170786561707865
|
||||
DATA consts<>+0x58(SB)/8, $0x6170786561707865
|
||||
DATA consts<>+0x60(SB)/8, $0x3320646e3320646e
|
||||
DATA consts<>+0x68(SB)/8, $0x3320646e3320646e
|
||||
DATA consts<>+0x70(SB)/8, $0x79622d3279622d32
|
||||
DATA consts<>+0x78(SB)/8, $0x79622d3279622d32
|
||||
DATA consts<>+0x80(SB)/8, $0x6b2065746b206574
|
||||
DATA consts<>+0x88(SB)/8, $0x6b2065746b206574
|
||||
DATA consts<>+0x90(SB)/8, $0x0000000100000000
|
||||
DATA consts<>+0x98(SB)/8, $0x0000000300000002
|
||||
DATA consts<>+0xa0(SB)/8, $0x5566774411223300
|
||||
DATA consts<>+0xa8(SB)/8, $0xddeeffcc99aabb88
|
||||
DATA consts<>+0xb0(SB)/8, $0x6677445522330011
|
||||
DATA consts<>+0xb8(SB)/8, $0xeeffccddaabb8899
|
||||
DATA consts<>+0x00(SB)/4, $0x61707865
|
||||
DATA consts<>+0x04(SB)/4, $0x3320646e
|
||||
DATA consts<>+0x08(SB)/4, $0x79622d32
|
||||
DATA consts<>+0x0c(SB)/4, $0x6b206574
|
||||
DATA consts<>+0x10(SB)/4, $0x00000001
|
||||
DATA consts<>+0x14(SB)/4, $0x00000000
|
||||
DATA consts<>+0x18(SB)/4, $0x00000000
|
||||
DATA consts<>+0x1c(SB)/4, $0x00000000
|
||||
DATA consts<>+0x20(SB)/4, $0x00000004
|
||||
DATA consts<>+0x24(SB)/4, $0x00000000
|
||||
DATA consts<>+0x28(SB)/4, $0x00000000
|
||||
DATA consts<>+0x2c(SB)/4, $0x00000000
|
||||
DATA consts<>+0x30(SB)/4, $0x0e0f0c0d
|
||||
DATA consts<>+0x34(SB)/4, $0x0a0b0809
|
||||
DATA consts<>+0x38(SB)/4, $0x06070405
|
||||
DATA consts<>+0x3c(SB)/4, $0x02030001
|
||||
DATA consts<>+0x40(SB)/4, $0x0d0e0f0c
|
||||
DATA consts<>+0x44(SB)/4, $0x090a0b08
|
||||
DATA consts<>+0x48(SB)/4, $0x05060704
|
||||
DATA consts<>+0x4c(SB)/4, $0x01020300
|
||||
DATA consts<>+0x50(SB)/4, $0x61707865
|
||||
DATA consts<>+0x54(SB)/4, $0x61707865
|
||||
DATA consts<>+0x58(SB)/4, $0x61707865
|
||||
DATA consts<>+0x5c(SB)/4, $0x61707865
|
||||
DATA consts<>+0x60(SB)/4, $0x3320646e
|
||||
DATA consts<>+0x64(SB)/4, $0x3320646e
|
||||
DATA consts<>+0x68(SB)/4, $0x3320646e
|
||||
DATA consts<>+0x6c(SB)/4, $0x3320646e
|
||||
DATA consts<>+0x70(SB)/4, $0x79622d32
|
||||
DATA consts<>+0x74(SB)/4, $0x79622d32
|
||||
DATA consts<>+0x78(SB)/4, $0x79622d32
|
||||
DATA consts<>+0x7c(SB)/4, $0x79622d32
|
||||
DATA consts<>+0x80(SB)/4, $0x6b206574
|
||||
DATA consts<>+0x84(SB)/4, $0x6b206574
|
||||
DATA consts<>+0x88(SB)/4, $0x6b206574
|
||||
DATA consts<>+0x8c(SB)/4, $0x6b206574
|
||||
DATA consts<>+0x90(SB)/4, $0x00000000
|
||||
DATA consts<>+0x94(SB)/4, $0x00000001
|
||||
DATA consts<>+0x98(SB)/4, $0x00000002
|
||||
DATA consts<>+0x9c(SB)/4, $0x00000003
|
||||
DATA consts<>+0xa0(SB)/4, $0x11223300
|
||||
DATA consts<>+0xa4(SB)/4, $0x55667744
|
||||
DATA consts<>+0xa8(SB)/4, $0x99aabb88
|
||||
DATA consts<>+0xac(SB)/4, $0xddeeffcc
|
||||
DATA consts<>+0xb0(SB)/4, $0x22330011
|
||||
DATA consts<>+0xb4(SB)/4, $0x66774455
|
||||
DATA consts<>+0xb8(SB)/4, $0xaabb8899
|
||||
DATA consts<>+0xbc(SB)/4, $0xeeffccdd
|
||||
GLOBL consts<>(SB), RODATA, $0xc0
|
||||
|
||||
#ifdef GOARCH_ppc64
|
||||
#define BE_XXBRW_INIT() \
|
||||
LVSL (R0)(R0), V24 \
|
||||
VSPLTISB $3, V25 \
|
||||
VXOR V24, V25, V24 \
|
||||
|
||||
#define BE_XXBRW(vr) VPERM vr, vr, V24, vr
|
||||
#else
|
||||
#define BE_XXBRW_INIT()
|
||||
#define BE_XXBRW(vr)
|
||||
#endif
|
||||
|
||||
//func chaCha20_ctr32_vsx(out, inp *byte, len int, key *[8]uint32, counter *uint32)
|
||||
TEXT ·chaCha20_ctr32_vsx(SB),NOSPLIT,$64-40
|
||||
MOVD out+0(FP), OUT
|
||||
@@ -94,6 +130,8 @@ TEXT ·chaCha20_ctr32_vsx(SB),NOSPLIT,$64-40
|
||||
// Clear V27
|
||||
VXOR V27, V27, V27
|
||||
|
||||
BE_XXBRW_INIT()
|
||||
|
||||
// V28
|
||||
LXVW4X (CONSTBASE)(R11), VS60
|
||||
|
||||
@@ -299,6 +337,11 @@ loop_vsx:
|
||||
VADDUWM V8, V18, V8
|
||||
VADDUWM V12, V19, V12
|
||||
|
||||
BE_XXBRW(V0)
|
||||
BE_XXBRW(V4)
|
||||
BE_XXBRW(V8)
|
||||
BE_XXBRW(V12)
|
||||
|
||||
CMPU LEN, $64
|
||||
BLT tail_vsx
|
||||
|
||||
@@ -327,6 +370,11 @@ loop_vsx:
|
||||
VADDUWM V9, V18, V8
|
||||
VADDUWM V13, V19, V12
|
||||
|
||||
BE_XXBRW(V0)
|
||||
BE_XXBRW(V4)
|
||||
BE_XXBRW(V8)
|
||||
BE_XXBRW(V12)
|
||||
|
||||
CMPU LEN, $64
|
||||
BLT tail_vsx
|
||||
|
||||
@@ -334,8 +382,8 @@ loop_vsx:
|
||||
LXVW4X (INP)(R8), VS60
|
||||
LXVW4X (INP)(R9), VS61
|
||||
LXVW4X (INP)(R10), VS62
|
||||
VXOR V27, V0, V27
|
||||
|
||||
VXOR V27, V0, V27
|
||||
VXOR V28, V4, V28
|
||||
VXOR V29, V8, V29
|
||||
VXOR V30, V12, V30
|
||||
@@ -354,6 +402,11 @@ loop_vsx:
|
||||
VADDUWM V10, V18, V8
|
||||
VADDUWM V14, V19, V12
|
||||
|
||||
BE_XXBRW(V0)
|
||||
BE_XXBRW(V4)
|
||||
BE_XXBRW(V8)
|
||||
BE_XXBRW(V12)
|
||||
|
||||
CMPU LEN, $64
|
||||
BLT tail_vsx
|
||||
|
||||
@@ -381,6 +434,11 @@ loop_vsx:
|
||||
VADDUWM V11, V18, V8
|
||||
VADDUWM V15, V19, V12
|
||||
|
||||
BE_XXBRW(V0)
|
||||
BE_XXBRW(V4)
|
||||
BE_XXBRW(V8)
|
||||
BE_XXBRW(V12)
|
||||
|
||||
CMPU LEN, $64
|
||||
BLT tail_vsx
|
||||
|
||||
@@ -408,9 +466,9 @@ loop_vsx:
|
||||
|
||||
done_vsx:
|
||||
// Increment counter by number of 64 byte blocks
|
||||
MOVD (CNT), R14
|
||||
MOVWZ (CNT), R14
|
||||
ADD BLOCKS, R14
|
||||
MOVD R14, (CNT)
|
||||
MOVWZ R14, (CNT)
|
||||
RET
|
||||
|
||||
tail_vsx:
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,15 @@
|
||||
module chacha20poly1305/_asm
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/mmcloughlin/avo v0.6.0
|
||||
golang.org/x/crypto v0.33.0
|
||||
)
|
||||
|
||||
require (
|
||||
golang.org/x/mod v0.20.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/tools v0.24.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
|
||||
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
|
||||
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
|
||||
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
|
||||
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
|
||||
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
|
||||
@@ -38,6 +38,9 @@ type chacha20poly1305 struct {
|
||||
|
||||
// New returns a ChaCha20-Poly1305 AEAD that uses the given 256-bit key.
|
||||
func New(key []byte) (cipher.AEAD, error) {
|
||||
if fips140Enforced() {
|
||||
return nil, errors.New("chacha20poly1305: use of ChaCha20Poly1305 is not allowed in FIPS 140-only mode")
|
||||
}
|
||||
if len(key) != KeySize {
|
||||
return nil, errors.New("chacha20poly1305: bad key length")
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func chacha20Poly1305Open(dst []byte, key []uint32, src, ad []byte) bool
|
||||
func chacha20Poly1305Seal(dst []byte, key []uint32, src, ad []byte)
|
||||
|
||||
var (
|
||||
useAVX2 = cpu.X86.HasAVX2 && cpu.X86.HasBMI2
|
||||
useAVX2 = cpu.X86.HasSSSE3 && cpu.X86.HasAVX2 && cpu.X86.HasBMI2
|
||||
)
|
||||
|
||||
// setupState writes a ChaCha20 input matrix to state. See
|
||||
@@ -47,7 +47,7 @@ func setupState(state *[16]uint32, key *[32]byte, nonce []byte) {
|
||||
}
|
||||
|
||||
func (c *chacha20poly1305) seal(dst, nonce, plaintext, additionalData []byte) []byte {
|
||||
if !cpu.X86.HasSSSE3 {
|
||||
if !useAVX2 {
|
||||
return c.sealGeneric(dst, nonce, plaintext, additionalData)
|
||||
}
|
||||
|
||||
@@ -56,14 +56,17 @@ func (c *chacha20poly1305) seal(dst, nonce, plaintext, additionalData []byte) []
|
||||
|
||||
ret, out := sliceForAppend(dst, len(plaintext)+16)
|
||||
if alias.InexactOverlap(out, plaintext) {
|
||||
panic("chacha20poly1305: invalid buffer overlap")
|
||||
panic("chacha20poly1305: invalid buffer overlap of output and input")
|
||||
}
|
||||
if alias.AnyOverlap(out, additionalData) {
|
||||
panic("chacha20poly1305: invalid buffer overlap of output and additional data")
|
||||
}
|
||||
chacha20Poly1305Seal(out[:], state[:], plaintext, additionalData)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c *chacha20poly1305) open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
|
||||
if !cpu.X86.HasSSSE3 {
|
||||
if !useAVX2 {
|
||||
return c.openGeneric(dst, nonce, ciphertext, additionalData)
|
||||
}
|
||||
|
||||
@@ -73,7 +76,10 @@ func (c *chacha20poly1305) open(dst, nonce, ciphertext, additionalData []byte) (
|
||||
ciphertext = ciphertext[:len(ciphertext)-16]
|
||||
ret, out := sliceForAppend(dst, len(ciphertext))
|
||||
if alias.InexactOverlap(out, ciphertext) {
|
||||
panic("chacha20poly1305: invalid buffer overlap")
|
||||
panic("chacha20poly1305: invalid buffer overlap of output and input")
|
||||
}
|
||||
if alias.AnyOverlap(out, additionalData) {
|
||||
panic("chacha20poly1305: invalid buffer overlap of output and additional data")
|
||||
}
|
||||
if !chacha20Poly1305Open(out, state[:], ciphertext, additionalData) {
|
||||
for i := range out {
|
||||
|
||||
+4968
-2453
File diff suppressed because it is too large
Load Diff
@@ -31,7 +31,10 @@ func (c *chacha20poly1305) sealGeneric(dst, nonce, plaintext, additionalData []b
|
||||
ret, out := sliceForAppend(dst, len(plaintext)+poly1305.TagSize)
|
||||
ciphertext, tag := out[:len(plaintext)], out[len(plaintext):]
|
||||
if alias.InexactOverlap(out, plaintext) {
|
||||
panic("chacha20poly1305: invalid buffer overlap")
|
||||
panic("chacha20poly1305: invalid buffer overlap of output and input")
|
||||
}
|
||||
if alias.AnyOverlap(out, additionalData) {
|
||||
panic("chacha20poly1305: invalid buffer overlap of output and additional data")
|
||||
}
|
||||
|
||||
var polyKey [32]byte
|
||||
@@ -67,7 +70,10 @@ func (c *chacha20poly1305) openGeneric(dst, nonce, ciphertext, additionalData []
|
||||
|
||||
ret, out := sliceForAppend(dst, len(ciphertext))
|
||||
if alias.InexactOverlap(out, ciphertext) {
|
||||
panic("chacha20poly1305: invalid buffer overlap")
|
||||
panic("chacha20poly1305: invalid buffer overlap of output and input")
|
||||
}
|
||||
if alias.AnyOverlap(out, additionalData) {
|
||||
panic("chacha20poly1305: invalid buffer overlap of output and additional data")
|
||||
}
|
||||
if !p.Verify(tag) {
|
||||
for i := range out {
|
||||
|
||||
@@ -153,7 +153,7 @@ func TestRandom(t *testing.T) {
|
||||
t.Run("X", func(t *testing.T) { f(t, NonceSizeX) })
|
||||
}
|
||||
|
||||
func benchamarkChaCha20Poly1305Seal(b *testing.B, buf []byte, nonceSize int) {
|
||||
func benchmarkChaCha20Poly1305Seal(b *testing.B, buf []byte, nonceSize int) {
|
||||
b.ReportAllocs()
|
||||
b.SetBytes(int64(len(buf)))
|
||||
|
||||
@@ -176,7 +176,7 @@ func benchamarkChaCha20Poly1305Seal(b *testing.B, buf []byte, nonceSize int) {
|
||||
}
|
||||
}
|
||||
|
||||
func benchamarkChaCha20Poly1305Open(b *testing.B, buf []byte, nonceSize int) {
|
||||
func benchmarkChaCha20Poly1305Open(b *testing.B, buf []byte, nonceSize int) {
|
||||
b.ReportAllocs()
|
||||
b.SetBytes(int64(len(buf)))
|
||||
|
||||
@@ -204,17 +204,17 @@ func benchamarkChaCha20Poly1305Open(b *testing.B, buf []byte, nonceSize int) {
|
||||
func BenchmarkChacha20Poly1305(b *testing.B) {
|
||||
for _, length := range []int{64, 1350, 8 * 1024} {
|
||||
b.Run("Open-"+strconv.Itoa(length), func(b *testing.B) {
|
||||
benchamarkChaCha20Poly1305Open(b, make([]byte, length), NonceSize)
|
||||
benchmarkChaCha20Poly1305Open(b, make([]byte, length), NonceSize)
|
||||
})
|
||||
b.Run("Seal-"+strconv.Itoa(length), func(b *testing.B) {
|
||||
benchamarkChaCha20Poly1305Seal(b, make([]byte, length), NonceSize)
|
||||
benchmarkChaCha20Poly1305Seal(b, make([]byte, length), NonceSize)
|
||||
})
|
||||
|
||||
b.Run("Open-"+strconv.Itoa(length)+"-X", func(b *testing.B) {
|
||||
benchamarkChaCha20Poly1305Open(b, make([]byte, length), NonceSizeX)
|
||||
benchmarkChaCha20Poly1305Open(b, make([]byte, length), NonceSizeX)
|
||||
})
|
||||
b.Run("Seal-"+strconv.Itoa(length)+"-X", func(b *testing.B) {
|
||||
benchamarkChaCha20Poly1305Seal(b, make([]byte, length), NonceSizeX)
|
||||
benchmarkChaCha20Poly1305Seal(b, make([]byte, length), NonceSizeX)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// Copyright 2025 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.
|
||||
|
||||
//go:build !go1.26
|
||||
|
||||
package chacha20poly1305
|
||||
|
||||
func fips140Enforced() bool { return false }
|
||||
@@ -0,0 +1,11 @@
|
||||
// Copyright 2025 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.
|
||||
|
||||
//go:build go1.26
|
||||
|
||||
package chacha20poly1305
|
||||
|
||||
import "crypto/fips140"
|
||||
|
||||
func fips140Enforced() bool { return fips140.Enforced() }
|
||||
@@ -22,6 +22,9 @@ type xchacha20poly1305 struct {
|
||||
// preferred when nonce uniqueness cannot be trivially ensured, or whenever
|
||||
// nonces are randomly generated.
|
||||
func NewX(key []byte) (cipher.AEAD, error) {
|
||||
if fips140Enforced() {
|
||||
return nil, errors.New("chacha20poly1305: use of ChaCha20Poly1305 is not allowed in FIPS 140-only mode")
|
||||
}
|
||||
if len(key) != KeySize {
|
||||
return nil, errors.New("chacha20poly1305: bad key length")
|
||||
}
|
||||
|
||||
+1
-1
@@ -234,7 +234,7 @@ func (b *Builder) AddASN1(tag asn1.Tag, f BuilderContinuation) {
|
||||
// Identifiers with the low five bits set indicate high-tag-number format
|
||||
// (two or more octets), which we don't support.
|
||||
if tag&0x1f == 0x1f {
|
||||
b.err = fmt.Errorf("cryptobyte: high-tag number identifier octects not supported: 0x%x", tag)
|
||||
b.err = fmt.Errorf("cryptobyte: high-tag number identifier octets not supported: 0x%x", tag)
|
||||
return
|
||||
}
|
||||
b.AddUint8(uint8(tag))
|
||||
|
||||
@@ -3,11 +3,14 @@
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package curve25519 provides an implementation of the X25519 function, which
|
||||
// performs scalar multiplication on the elliptic curve known as Curve25519.
|
||||
// See RFC 7748.
|
||||
// performs scalar multiplication on the elliptic curve known as Curve25519
|
||||
// according to [RFC 7748].
|
||||
//
|
||||
// This package is a wrapper for the X25519 implementation
|
||||
// in the crypto/ecdh package.
|
||||
// The curve25519 package is a wrapper for the X25519 implementation in the
|
||||
// crypto/ecdh package. It is [frozen] and is not accepting new features.
|
||||
//
|
||||
// [RFC 7748]: https://datatracker.ietf.org/doc/html/rfc7748
|
||||
// [frozen]: https://go.dev/wiki/Frozen
|
||||
package curve25519
|
||||
|
||||
import "crypto/ecdh"
|
||||
@@ -36,7 +39,7 @@ func ScalarBaseMult(dst, scalar *[32]byte) {
|
||||
curve := ecdh.X25519()
|
||||
priv, err := curve.NewPrivateKey(scalar[:])
|
||||
if err != nil {
|
||||
panic("curve25519: internal error: scalarBaseMult was not 32 bytes")
|
||||
panic("curve25519: " + err.Error())
|
||||
}
|
||||
copy(dst[:], priv.PublicKey().Bytes())
|
||||
}
|
||||
|
||||
+7
-4
@@ -2,16 +2,19 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package ed25519 implements the Ed25519 signature algorithm. See
|
||||
// https://ed25519.cr.yp.to/.
|
||||
// Package ed25519 implements the Ed25519 signature algorithm.
|
||||
//
|
||||
// These functions are also compatible with the “Ed25519” function defined in
|
||||
// RFC 8032. However, unlike RFC 8032's formulation, this package's private key
|
||||
// [RFC 8032]. However, unlike RFC 8032's formulation, this package's private key
|
||||
// representation includes a public key suffix to make multiple signing
|
||||
// operations with the same key more efficient. This package refers to the RFC
|
||||
// 8032 private key as the “seed”.
|
||||
//
|
||||
// This package is a wrapper around the standard library crypto/ed25519 package.
|
||||
// The ed25519 package is a wrapper for the Ed25519 implementation in the
|
||||
// crypto/ed25519 package. It is [frozen] and is not accepting new features.
|
||||
//
|
||||
// [RFC 8032]: https://datatracker.ietf.org/doc/html/rfc8032
|
||||
// [frozen]: https://go.dev/wiki/Frozen
|
||||
package ed25519
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
module golang.org/x/crypto
|
||||
|
||||
go 1.20
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
golang.org/x/net v0.21.0 // tagx:ignore
|
||||
golang.org/x/sys v0.23.0
|
||||
golang.org/x/term v0.23.0
|
||||
golang.org/x/net v0.56.0 // tagx:ignore
|
||||
golang.org/x/sys v0.47.0
|
||||
golang.org/x/term v0.45.0
|
||||
)
|
||||
|
||||
require golang.org/x/text v0.17.0 // indirect
|
||||
require golang.org/x/text v0.40.0 // indirect
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
||||
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
|
||||
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/net v0.56.0 h1:Rw8j/hFzGvJUZwNBXnAtf5sVDVt+65SK2C7IxCxZt5o=
|
||||
golang.org/x/net v0.56.0/go.mod h1:D3Ku6r+V6JROoZK144D2XfMHFcMq/0zSfLelVTCFKec=
|
||||
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
|
||||
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/term v0.45.0 h1:NwWyBmoJCbfTHpxrWoZ9C6/VxOf7ic219I8xZZFdrf0=
|
||||
golang.org/x/term v0.45.0/go.mod h1:9aqxs0blBcrm/n0L9QW0aRVD+ktan8ssZromtqJC43w=
|
||||
golang.org/x/text v0.40.0 h1:Ub2Z6/xjgF1WrYQz2nuITOEegKFtiIy+rieRJ5lHZKs=
|
||||
golang.org/x/text v0.40.0/go.mod h1:hpnzDAfGV753zIKo+wk3u1bVKCGPbrnF7+7LBF/UHVY=
|
||||
|
||||
+13
-8
@@ -11,6 +11,7 @@
|
||||
package hkdf
|
||||
|
||||
import (
|
||||
"crypto/hkdf"
|
||||
"crypto/hmac"
|
||||
"errors"
|
||||
"hash"
|
||||
@@ -24,15 +25,19 @@ import (
|
||||
// Expand invocations and different context values. Most common scenarios,
|
||||
// including the generation of multiple keys, should use New instead.
|
||||
func Extract(hash func() hash.Hash, secret, salt []byte) []byte {
|
||||
if salt == nil {
|
||||
salt = make([]byte, hash().Size())
|
||||
// Use the stdlib Extract, which disables FIPS 140 enforcement of the HMAC
|
||||
// key (which in HKDF is the salt). The only possible error is FIPS 140
|
||||
// enforcement of the hash, which had to panic under this API anyway. We
|
||||
// don't use the stdlib Expand, because it switched to returning a []byte
|
||||
// instead of an io.Reader, and Expand uses the HMAC key as a key.
|
||||
out, err := hkdf.Extract(hash, secret, salt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
extractor := hmac.New(hash, salt)
|
||||
extractor.Write(secret)
|
||||
return extractor.Sum(nil)
|
||||
return out
|
||||
}
|
||||
|
||||
type hkdf struct {
|
||||
type hkdfReader struct {
|
||||
expander hash.Hash
|
||||
size int
|
||||
|
||||
@@ -43,7 +48,7 @@ type hkdf struct {
|
||||
buf []byte
|
||||
}
|
||||
|
||||
func (f *hkdf) Read(p []byte) (int, error) {
|
||||
func (f *hkdfReader) Read(p []byte) (int, error) {
|
||||
// Check whether enough data can be generated
|
||||
need := len(p)
|
||||
remains := len(f.buf) + int(255-f.counter+1)*f.size
|
||||
@@ -84,7 +89,7 @@ func (f *hkdf) Read(p []byte) (int, error) {
|
||||
// 3.3. Most common scenarios will want to use New instead.
|
||||
func Expand(hash func() hash.Hash, pseudorandomKey, info []byte) io.Reader {
|
||||
expander := hmac.New(hash, pseudorandomKey)
|
||||
return &hkdf{expander, expander.Size(), info, 1, nil, nil}
|
||||
return &hkdfReader{expander, expander.Size(), info, 1, nil, nil}
|
||||
}
|
||||
|
||||
// New returns a Reader, from which keys can be read, using the given hash,
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
module internal/poly1305/_asm
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/mmcloughlin/avo v0.6.0
|
||||
golang.org/x/crypto v0.33.0
|
||||
)
|
||||
|
||||
require (
|
||||
golang.org/x/mod v0.20.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/tools v0.24.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
github.com/mmcloughlin/avo v0.6.0 h1:QH6FU8SKoTLaVs80GA8TJuLNkUYl4VokHKlPhVDg4YY=
|
||||
github.com/mmcloughlin/avo v0.6.0/go.mod h1:8CoAGaCSYXtCPR+8y18Y9aB/kxb8JSS6FRI7mSkvD+8=
|
||||
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
|
||||
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
|
||||
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
|
||||
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
|
||||
@@ -0,0 +1,126 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
. "github.com/mmcloughlin/avo/build"
|
||||
. "github.com/mmcloughlin/avo/operand"
|
||||
. "github.com/mmcloughlin/avo/reg"
|
||||
_ "golang.org/x/crypto/sha3"
|
||||
)
|
||||
|
||||
//go:generate go run . -out ../sum_amd64.s -pkg poly1305
|
||||
|
||||
func main() {
|
||||
Package("golang.org/x/crypto/internal/poly1305")
|
||||
ConstraintExpr("gc,!purego")
|
||||
update()
|
||||
Generate()
|
||||
}
|
||||
|
||||
func update() {
|
||||
Implement("update")
|
||||
|
||||
Load(Param("state"), RDI)
|
||||
MOVQ(NewParamAddr("msg_base", 8), RSI)
|
||||
MOVQ(NewParamAddr("msg_len", 16), R15)
|
||||
|
||||
MOVQ(Mem{Base: DI}.Offset(0), R8) // h0
|
||||
MOVQ(Mem{Base: DI}.Offset(8), R9) // h1
|
||||
MOVQ(Mem{Base: DI}.Offset(16), R10) // h2
|
||||
MOVQ(Mem{Base: DI}.Offset(24), R11) // r0
|
||||
MOVQ(Mem{Base: DI}.Offset(32), R12) // r1
|
||||
|
||||
CMPQ(R15, Imm(16))
|
||||
JB(LabelRef("bytes_between_0_and_15"))
|
||||
|
||||
Label("loop")
|
||||
POLY1305_ADD(RSI, R8, R9, R10)
|
||||
|
||||
Label("multiply")
|
||||
POLY1305_MUL(R8, R9, R10, R11, R12, RBX, RCX, R13, R14)
|
||||
SUBQ(Imm(16), R15)
|
||||
CMPQ(R15, Imm(16))
|
||||
JAE(LabelRef("loop"))
|
||||
|
||||
Label("bytes_between_0_and_15")
|
||||
TESTQ(R15, R15)
|
||||
JZ(LabelRef("done"))
|
||||
MOVQ(U32(1), RBX)
|
||||
XORQ(RCX, RCX)
|
||||
XORQ(R13, R13)
|
||||
ADDQ(R15, RSI)
|
||||
|
||||
Label("flush_buffer")
|
||||
SHLQ(Imm(8), RBX, RCX)
|
||||
SHLQ(Imm(8), RBX)
|
||||
MOVB(Mem{Base: SI}.Offset(-1), R13B)
|
||||
XORQ(R13, RBX)
|
||||
DECQ(RSI)
|
||||
DECQ(R15)
|
||||
JNZ(LabelRef("flush_buffer"))
|
||||
|
||||
ADDQ(RBX, R8)
|
||||
ADCQ(RCX, R9)
|
||||
ADCQ(Imm(0), R10)
|
||||
MOVQ(U32(16), R15)
|
||||
JMP(LabelRef("multiply"))
|
||||
|
||||
Label("done")
|
||||
MOVQ(R8, Mem{Base: DI}.Offset(0))
|
||||
MOVQ(R9, Mem{Base: DI}.Offset(8))
|
||||
MOVQ(R10, Mem{Base: DI}.Offset(16))
|
||||
RET()
|
||||
}
|
||||
|
||||
func POLY1305_ADD(msg, h0, h1, h2 GPPhysical) {
|
||||
ADDQ(Mem{Base: msg}.Offset(0), h0)
|
||||
ADCQ(Mem{Base: msg}.Offset(8), h1)
|
||||
ADCQ(Imm(1), h2)
|
||||
LEAQ(Mem{Base: msg}.Offset(16), msg)
|
||||
}
|
||||
|
||||
func POLY1305_MUL(h0, h1, h2, r0, r1, t0, t1, t2, t3 GPPhysical) {
|
||||
MOVQ(r0, RAX)
|
||||
MULQ(h0)
|
||||
MOVQ(RAX, t0)
|
||||
MOVQ(RDX, t1)
|
||||
MOVQ(r0, RAX)
|
||||
MULQ(h1)
|
||||
ADDQ(RAX, t1)
|
||||
ADCQ(Imm(0), RDX)
|
||||
MOVQ(r0, t2)
|
||||
IMULQ(h2, t2)
|
||||
ADDQ(RDX, t2)
|
||||
|
||||
MOVQ(r1, RAX)
|
||||
MULQ(h0)
|
||||
ADDQ(RAX, t1)
|
||||
ADCQ(Imm(0), RDX)
|
||||
MOVQ(RDX, h0)
|
||||
MOVQ(r1, t3)
|
||||
IMULQ(h2, t3)
|
||||
MOVQ(r1, RAX)
|
||||
MULQ(h1)
|
||||
ADDQ(RAX, t2)
|
||||
ADCQ(RDX, t3)
|
||||
ADDQ(h0, t2)
|
||||
ADCQ(Imm(0), t3)
|
||||
|
||||
MOVQ(t0, h0)
|
||||
MOVQ(t1, h1)
|
||||
MOVQ(t2, h2)
|
||||
ANDQ(Imm(3), h2)
|
||||
MOVQ(t2, t0)
|
||||
ANDQ(I32(-4), t0)
|
||||
ADDQ(t0, h0)
|
||||
ADCQ(t3, h1)
|
||||
ADCQ(Imm(0), h2)
|
||||
SHRQ(Imm(2), t3, t2)
|
||||
SHRQ(Imm(2), t3)
|
||||
ADDQ(t2, h0)
|
||||
ADCQ(t3, h1)
|
||||
ADCQ(Imm(0), h2)
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build (!amd64 && !ppc64le && !s390x) || !gc || purego
|
||||
//go:build (!amd64 && !loong64 && !ppc64le && !ppc64 && !s390x) || !gc || purego
|
||||
|
||||
package poly1305
|
||||
|
||||
|
||||
@@ -1,108 +1,93 @@
|
||||
// Copyright 2012 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.
|
||||
// Code generated by command: go run sum_amd64_asm.go -out ../sum_amd64.s -pkg poly1305. DO NOT EDIT.
|
||||
|
||||
//go:build gc && !purego
|
||||
|
||||
#include "textflag.h"
|
||||
|
||||
#define POLY1305_ADD(msg, h0, h1, h2) \
|
||||
ADDQ 0(msg), h0; \
|
||||
ADCQ 8(msg), h1; \
|
||||
ADCQ $1, h2; \
|
||||
LEAQ 16(msg), msg
|
||||
|
||||
#define POLY1305_MUL(h0, h1, h2, r0, r1, t0, t1, t2, t3) \
|
||||
MOVQ r0, AX; \
|
||||
MULQ h0; \
|
||||
MOVQ AX, t0; \
|
||||
MOVQ DX, t1; \
|
||||
MOVQ r0, AX; \
|
||||
MULQ h1; \
|
||||
ADDQ AX, t1; \
|
||||
ADCQ $0, DX; \
|
||||
MOVQ r0, t2; \
|
||||
IMULQ h2, t2; \
|
||||
ADDQ DX, t2; \
|
||||
\
|
||||
MOVQ r1, AX; \
|
||||
MULQ h0; \
|
||||
ADDQ AX, t1; \
|
||||
ADCQ $0, DX; \
|
||||
MOVQ DX, h0; \
|
||||
MOVQ r1, t3; \
|
||||
IMULQ h2, t3; \
|
||||
MOVQ r1, AX; \
|
||||
MULQ h1; \
|
||||
ADDQ AX, t2; \
|
||||
ADCQ DX, t3; \
|
||||
ADDQ h0, t2; \
|
||||
ADCQ $0, t3; \
|
||||
\
|
||||
MOVQ t0, h0; \
|
||||
MOVQ t1, h1; \
|
||||
MOVQ t2, h2; \
|
||||
ANDQ $3, h2; \
|
||||
MOVQ t2, t0; \
|
||||
ANDQ $0xFFFFFFFFFFFFFFFC, t0; \
|
||||
ADDQ t0, h0; \
|
||||
ADCQ t3, h1; \
|
||||
ADCQ $0, h2; \
|
||||
SHRQ $2, t3, t2; \
|
||||
SHRQ $2, t3; \
|
||||
ADDQ t2, h0; \
|
||||
ADCQ t3, h1; \
|
||||
ADCQ $0, h2
|
||||
|
||||
// func update(state *[7]uint64, msg []byte)
|
||||
// func update(state *macState, msg []byte)
|
||||
TEXT ·update(SB), $0-32
|
||||
MOVQ state+0(FP), DI
|
||||
MOVQ msg_base+8(FP), SI
|
||||
MOVQ msg_len+16(FP), R15
|
||||
|
||||
MOVQ 0(DI), R8 // h0
|
||||
MOVQ 8(DI), R9 // h1
|
||||
MOVQ 16(DI), R10 // h2
|
||||
MOVQ 24(DI), R11 // r0
|
||||
MOVQ 32(DI), R12 // r1
|
||||
|
||||
CMPQ R15, $16
|
||||
MOVQ (DI), R8
|
||||
MOVQ 8(DI), R9
|
||||
MOVQ 16(DI), R10
|
||||
MOVQ 24(DI), R11
|
||||
MOVQ 32(DI), R12
|
||||
CMPQ R15, $0x10
|
||||
JB bytes_between_0_and_15
|
||||
|
||||
loop:
|
||||
POLY1305_ADD(SI, R8, R9, R10)
|
||||
ADDQ (SI), R8
|
||||
ADCQ 8(SI), R9
|
||||
ADCQ $0x01, R10
|
||||
LEAQ 16(SI), SI
|
||||
|
||||
multiply:
|
||||
POLY1305_MUL(R8, R9, R10, R11, R12, BX, CX, R13, R14)
|
||||
SUBQ $16, R15
|
||||
CMPQ R15, $16
|
||||
JAE loop
|
||||
MOVQ R11, AX
|
||||
MULQ R8
|
||||
MOVQ AX, BX
|
||||
MOVQ DX, CX
|
||||
MOVQ R11, AX
|
||||
MULQ R9
|
||||
ADDQ AX, CX
|
||||
ADCQ $0x00, DX
|
||||
MOVQ R11, R13
|
||||
IMULQ R10, R13
|
||||
ADDQ DX, R13
|
||||
MOVQ R12, AX
|
||||
MULQ R8
|
||||
ADDQ AX, CX
|
||||
ADCQ $0x00, DX
|
||||
MOVQ DX, R8
|
||||
MOVQ R12, R14
|
||||
IMULQ R10, R14
|
||||
MOVQ R12, AX
|
||||
MULQ R9
|
||||
ADDQ AX, R13
|
||||
ADCQ DX, R14
|
||||
ADDQ R8, R13
|
||||
ADCQ $0x00, R14
|
||||
MOVQ BX, R8
|
||||
MOVQ CX, R9
|
||||
MOVQ R13, R10
|
||||
ANDQ $0x03, R10
|
||||
MOVQ R13, BX
|
||||
ANDQ $-4, BX
|
||||
ADDQ BX, R8
|
||||
ADCQ R14, R9
|
||||
ADCQ $0x00, R10
|
||||
SHRQ $0x02, R14, R13
|
||||
SHRQ $0x02, R14
|
||||
ADDQ R13, R8
|
||||
ADCQ R14, R9
|
||||
ADCQ $0x00, R10
|
||||
SUBQ $0x10, R15
|
||||
CMPQ R15, $0x10
|
||||
JAE loop
|
||||
|
||||
bytes_between_0_and_15:
|
||||
TESTQ R15, R15
|
||||
JZ done
|
||||
MOVQ $1, BX
|
||||
MOVQ $0x00000001, BX
|
||||
XORQ CX, CX
|
||||
XORQ R13, R13
|
||||
ADDQ R15, SI
|
||||
|
||||
flush_buffer:
|
||||
SHLQ $8, BX, CX
|
||||
SHLQ $8, BX
|
||||
SHLQ $0x08, BX, CX
|
||||
SHLQ $0x08, BX
|
||||
MOVB -1(SI), R13
|
||||
XORQ R13, BX
|
||||
DECQ SI
|
||||
DECQ R15
|
||||
JNZ flush_buffer
|
||||
|
||||
ADDQ BX, R8
|
||||
ADCQ CX, R9
|
||||
ADCQ $0, R10
|
||||
MOVQ $16, R15
|
||||
ADCQ $0x00, R10
|
||||
MOVQ $0x00000010, R15
|
||||
JMP multiply
|
||||
|
||||
done:
|
||||
MOVQ R8, 0(DI)
|
||||
MOVQ R8, (DI)
|
||||
MOVQ R9, 8(DI)
|
||||
MOVQ R10, 16(DI)
|
||||
RET
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build gc && !purego
|
||||
//go:build gc && !purego && (amd64 || loong64 || ppc64 || ppc64le)
|
||||
|
||||
package poly1305
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
// Copyright 2025 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.
|
||||
|
||||
//go:build gc && !purego
|
||||
|
||||
// func update(state *macState, msg []byte)
|
||||
TEXT ·update(SB), $0-32
|
||||
MOVV state+0(FP), R4
|
||||
MOVV msg_base+8(FP), R5
|
||||
MOVV msg_len+16(FP), R6
|
||||
|
||||
MOVV $0x10, R7
|
||||
|
||||
MOVV (R4), R8 // h0
|
||||
MOVV 8(R4), R9 // h1
|
||||
MOVV 16(R4), R10 // h2
|
||||
MOVV 24(R4), R11 // r0
|
||||
MOVV 32(R4), R12 // r1
|
||||
|
||||
BLT R6, R7, bytes_between_0_and_15
|
||||
|
||||
loop:
|
||||
MOVV (R5), R14 // msg[0:8]
|
||||
MOVV 8(R5), R16 // msg[8:16]
|
||||
ADDV R14, R8, R8 // h0 (x1 + y1 = z1', if z1' < x1 then z1' overflow)
|
||||
ADDV R16, R9, R27
|
||||
SGTU R14, R8, R24 // h0.carry
|
||||
SGTU R9, R27, R28
|
||||
ADDV R27, R24, R9 // h1
|
||||
SGTU R27, R9, R24
|
||||
OR R24, R28, R24 // h1.carry
|
||||
ADDV $0x01, R24, R24
|
||||
ADDV R10, R24, R10 // h2
|
||||
|
||||
ADDV $16, R5, R5 // msg = msg[16:]
|
||||
|
||||
multiply:
|
||||
MULV R8, R11, R14 // h0r0.lo
|
||||
MULHVU R8, R11, R15 // h0r0.hi
|
||||
MULV R9, R11, R13 // h1r0.lo
|
||||
MULHVU R9, R11, R16 // h1r0.hi
|
||||
ADDV R13, R15, R15
|
||||
SGTU R13, R15, R24
|
||||
ADDV R24, R16, R16
|
||||
MULV R10, R11, R25
|
||||
ADDV R16, R25, R25
|
||||
MULV R8, R12, R13 // h0r1.lo
|
||||
MULHVU R8, R12, R16 // h0r1.hi
|
||||
ADDV R13, R15, R15
|
||||
SGTU R13, R15, R24
|
||||
ADDV R24, R16, R16
|
||||
MOVV R16, R8
|
||||
MULV R10, R12, R26 // h2r1
|
||||
MULV R9, R12, R13 // h1r1.lo
|
||||
MULHVU R9, R12, R16 // h1r1.hi
|
||||
ADDV R13, R25, R25
|
||||
ADDV R16, R26, R27
|
||||
SGTU R13, R25, R24
|
||||
ADDV R27, R24, R26
|
||||
ADDV R8, R25, R25
|
||||
SGTU R8, R25, R24
|
||||
ADDV R24, R26, R26
|
||||
AND $3, R25, R10
|
||||
AND $-4, R25, R17
|
||||
ADDV R17, R14, R8
|
||||
ADDV R26, R15, R27
|
||||
SGTU R17, R8, R24
|
||||
SGTU R26, R27, R28
|
||||
ADDV R27, R24, R9
|
||||
SGTU R27, R9, R24
|
||||
OR R24, R28, R24
|
||||
ADDV R24, R10, R10
|
||||
SLLV $62, R26, R27
|
||||
SRLV $2, R25, R28
|
||||
SRLV $2, R26, R26
|
||||
OR R27, R28, R25
|
||||
ADDV R25, R8, R8
|
||||
ADDV R26, R9, R27
|
||||
SGTU R25, R8, R24
|
||||
SGTU R26, R27, R28
|
||||
ADDV R27, R24, R9
|
||||
SGTU R27, R9, R24
|
||||
OR R24, R28, R24
|
||||
ADDV R24, R10, R10
|
||||
|
||||
SUBV $16, R6, R6
|
||||
BGE R6, R7, loop
|
||||
|
||||
bytes_between_0_and_15:
|
||||
BEQ R6, R0, done
|
||||
MOVV $1, R14
|
||||
XOR R15, R15
|
||||
ADDV R6, R5, R5
|
||||
|
||||
flush_buffer:
|
||||
MOVBU -1(R5), R25
|
||||
SRLV $56, R14, R24
|
||||
SLLV $8, R15, R28
|
||||
SLLV $8, R14, R14
|
||||
OR R24, R28, R15
|
||||
XOR R25, R14, R14
|
||||
SUBV $1, R6, R6
|
||||
SUBV $1, R5, R5
|
||||
BNE R6, R0, flush_buffer
|
||||
|
||||
ADDV R14, R8, R8
|
||||
SGTU R14, R8, R24
|
||||
ADDV R15, R9, R27
|
||||
SGTU R15, R27, R28
|
||||
ADDV R27, R24, R9
|
||||
SGTU R27, R9, R24
|
||||
OR R24, R28, R24
|
||||
ADDV R10, R24, R10
|
||||
|
||||
MOVV $16, R6
|
||||
JMP multiply
|
||||
|
||||
done:
|
||||
MOVV R8, (R4)
|
||||
MOVV R9, 8(R4)
|
||||
MOVV R10, 16(R4)
|
||||
RET
|
||||
@@ -1,47 +0,0 @@
|
||||
// Copyright 2019 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.
|
||||
|
||||
//go:build gc && !purego
|
||||
|
||||
package poly1305
|
||||
|
||||
//go:noescape
|
||||
func update(state *macState, msg []byte)
|
||||
|
||||
// mac is a wrapper for macGeneric that redirects calls that would have gone to
|
||||
// updateGeneric to update.
|
||||
//
|
||||
// Its Write and Sum methods are otherwise identical to the macGeneric ones, but
|
||||
// using function pointers would carry a major performance cost.
|
||||
type mac struct{ macGeneric }
|
||||
|
||||
func (h *mac) Write(p []byte) (int, error) {
|
||||
nn := len(p)
|
||||
if h.offset > 0 {
|
||||
n := copy(h.buffer[h.offset:], p)
|
||||
if h.offset+n < TagSize {
|
||||
h.offset += n
|
||||
return nn, nil
|
||||
}
|
||||
p = p[n:]
|
||||
h.offset = 0
|
||||
update(&h.macState, h.buffer[:])
|
||||
}
|
||||
if n := len(p) - (len(p) % TagSize); n > 0 {
|
||||
update(&h.macState, p[:n])
|
||||
p = p[n:]
|
||||
}
|
||||
if len(p) > 0 {
|
||||
h.offset += copy(h.buffer[h.offset:], p)
|
||||
}
|
||||
return nn, nil
|
||||
}
|
||||
|
||||
func (h *mac) Sum(out *[16]byte) {
|
||||
state := h.macState
|
||||
if h.offset > 0 {
|
||||
update(&state, h.buffer[:h.offset])
|
||||
}
|
||||
finalize(out, &state.h, &state.s)
|
||||
}
|
||||
@@ -2,15 +2,25 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build gc && !purego
|
||||
//go:build gc && !purego && (ppc64 || ppc64le)
|
||||
|
||||
#include "textflag.h"
|
||||
|
||||
// This was ported from the amd64 implementation.
|
||||
|
||||
#ifdef GOARCH_ppc64le
|
||||
#define LE_MOVD MOVD
|
||||
#define LE_MOVWZ MOVWZ
|
||||
#define LE_MOVHZ MOVHZ
|
||||
#else
|
||||
#define LE_MOVD MOVDBR
|
||||
#define LE_MOVWZ MOVWBR
|
||||
#define LE_MOVHZ MOVHBR
|
||||
#endif
|
||||
|
||||
#define POLY1305_ADD(msg, h0, h1, h2, t0, t1, t2) \
|
||||
MOVD (msg), t0; \
|
||||
MOVD 8(msg), t1; \
|
||||
LE_MOVD (msg)( R0), t0; \
|
||||
LE_MOVD (msg)(R24), t1; \
|
||||
MOVD $1, t2; \
|
||||
ADDC t0, h0, h0; \
|
||||
ADDE t1, h1, h1; \
|
||||
@@ -50,10 +60,6 @@
|
||||
ADDE t3, h1, h1; \
|
||||
ADDZE h2
|
||||
|
||||
DATA ·poly1305Mask<>+0x00(SB)/8, $0x0FFFFFFC0FFFFFFF
|
||||
DATA ·poly1305Mask<>+0x08(SB)/8, $0x0FFFFFFC0FFFFFFC
|
||||
GLOBL ·poly1305Mask<>(SB), RODATA, $16
|
||||
|
||||
// func update(state *[7]uint64, msg []byte)
|
||||
TEXT ·update(SB), $0-32
|
||||
MOVD state+0(FP), R3
|
||||
@@ -66,6 +72,8 @@ TEXT ·update(SB), $0-32
|
||||
MOVD 24(R3), R11 // r0
|
||||
MOVD 32(R3), R12 // r1
|
||||
|
||||
MOVD $8, R24
|
||||
|
||||
CMP R5, $16
|
||||
BLT bytes_between_0_and_15
|
||||
|
||||
@@ -94,7 +102,7 @@ flush_buffer:
|
||||
|
||||
// Greater than 8 -- load the rightmost remaining bytes in msg
|
||||
// and put into R17 (h1)
|
||||
MOVD (R4)(R21), R17
|
||||
LE_MOVD (R4)(R21), R17
|
||||
MOVD $16, R22
|
||||
|
||||
// Find the offset to those bytes
|
||||
@@ -118,7 +126,7 @@ just1:
|
||||
BLT less8
|
||||
|
||||
// Exactly 8
|
||||
MOVD (R4), R16
|
||||
LE_MOVD (R4), R16
|
||||
|
||||
CMP R17, $0
|
||||
|
||||
@@ -133,7 +141,7 @@ less8:
|
||||
MOVD $0, R22 // shift count
|
||||
CMP R5, $4
|
||||
BLT less4
|
||||
MOVWZ (R4), R16
|
||||
LE_MOVWZ (R4), R16
|
||||
ADD $4, R4
|
||||
ADD $-4, R5
|
||||
MOVD $32, R22
|
||||
@@ -141,7 +149,7 @@ less8:
|
||||
less4:
|
||||
CMP R5, $2
|
||||
BLT less2
|
||||
MOVHZ (R4), R21
|
||||
LE_MOVHZ (R4), R21
|
||||
SLD R22, R21, R21
|
||||
OR R16, R21, R16
|
||||
ADD $16, R22
|
||||
@@ -1,12 +0,0 @@
|
||||
This package runs a set of the Wycheproof tests provided by
|
||||
https://github.com/google/wycheproof.
|
||||
|
||||
The JSON test files live in
|
||||
https://github.com/google/wycheproof/tree/master/testvectors
|
||||
and are being fetched and cached at a pinned version every time
|
||||
these tests are run. To change the version of the wycheproof
|
||||
repository that is being used for testing, update wycheproofModVer.
|
||||
|
||||
The structs for these tests are generated from the
|
||||
schemas provided in https://github.com/google/wycheproof/tree/master/schemas
|
||||
using https://github.com/a-h/generate.
|
||||
@@ -1,176 +0,0 @@
|
||||
// Copyright 2020 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 wycheproof
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
)
|
||||
|
||||
func TestAEAD(t *testing.T) {
|
||||
// AeadTestVector
|
||||
type AeadTestVector struct {
|
||||
|
||||
// additional authenticated data
|
||||
Aad string `json:"aad,omitempty"`
|
||||
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
|
||||
// the ciphertext (without iv and tag)
|
||||
Ct string `json:"ct,omitempty"`
|
||||
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
|
||||
// the nonce
|
||||
Iv string `json:"iv,omitempty"`
|
||||
|
||||
// the key
|
||||
Key string `json:"key,omitempty"`
|
||||
|
||||
// the plaintext
|
||||
Msg string `json:"msg,omitempty"`
|
||||
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
|
||||
// the authentication tag
|
||||
Tag string `json:"tag,omitempty"`
|
||||
|
||||
// Identifier of the test case
|
||||
TcId int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
// Notes a description of the labels used in the test vectors
|
||||
type Notes struct {
|
||||
}
|
||||
|
||||
// AeadTestGroup
|
||||
type AeadTestGroup struct {
|
||||
|
||||
// the IV size in bits
|
||||
IvSize int `json:"ivSize,omitempty"`
|
||||
|
||||
// the keySize in bits
|
||||
KeySize int `json:"keySize,omitempty"`
|
||||
|
||||
// the expected size of the tag in bits
|
||||
TagSize int `json:"tagSize,omitempty"`
|
||||
Tests []*AeadTestVector `json:"tests,omitempty"`
|
||||
Type interface{} `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// Root
|
||||
type Root struct {
|
||||
|
||||
// the primitive tested in the test file
|
||||
Algorithm string `json:"algorithm,omitempty"`
|
||||
|
||||
// the version of the test vectors.
|
||||
GeneratorVersion string `json:"generatorVersion,omitempty"`
|
||||
|
||||
// additional documentation
|
||||
Header []string `json:"header,omitempty"`
|
||||
|
||||
// a description of the labels used in the test vectors
|
||||
Notes *Notes `json:"notes,omitempty"`
|
||||
|
||||
// the number of test vectors in this test
|
||||
NumberOfTests int `json:"numberOfTests,omitempty"`
|
||||
Schema interface{} `json:"schema,omitempty"`
|
||||
TestGroups []*AeadTestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
testSealOpen := func(t *testing.T, aead cipher.AEAD, tv *AeadTestVector, recoverBadNonce func()) {
|
||||
defer recoverBadNonce()
|
||||
|
||||
iv, tag, ct, msg, aad := decodeHex(tv.Iv), decodeHex(tv.Tag), decodeHex(tv.Ct), decodeHex(tv.Msg), decodeHex(tv.Aad)
|
||||
|
||||
genCT := aead.Seal(nil, iv, msg, aad)
|
||||
genMsg, err := aead.Open(nil, iv, genCT, aad)
|
||||
if err != nil {
|
||||
t.Errorf("failed to decrypt generated ciphertext: %s", err)
|
||||
}
|
||||
if !bytes.Equal(genMsg, msg) {
|
||||
t.Errorf("unexpected roundtripped plaintext: got %x, want %x", genMsg, msg)
|
||||
}
|
||||
|
||||
ctWithTag := append(ct, tag...)
|
||||
msg2, err := aead.Open(nil, iv, ctWithTag, aad)
|
||||
wantPass := shouldPass(tv.Result, tv.Flags, nil)
|
||||
if !wantPass && err == nil {
|
||||
t.Error("decryption succeeded when it should've failed")
|
||||
} else if wantPass {
|
||||
if err != nil {
|
||||
t.Fatalf("decryption failed: %s", err)
|
||||
}
|
||||
if !bytes.Equal(genCT, ctWithTag) {
|
||||
t.Errorf("generated ciphertext doesn't match expected: got %x, want %x", genCT, ctWithTag)
|
||||
}
|
||||
if !bytes.Equal(msg, msg2) {
|
||||
t.Errorf("decrypted ciphertext doesn't match expected: got %x, want %x", msg2, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vectors := map[string]func(*testing.T, []byte) cipher.AEAD{
|
||||
"aes_gcm_test.json": func(t *testing.T, key []byte) cipher.AEAD {
|
||||
aesCipher, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to construct cipher: %s", err)
|
||||
}
|
||||
aead, err := cipher.NewGCM(aesCipher)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to construct cipher: %s", err)
|
||||
}
|
||||
return aead
|
||||
},
|
||||
"chacha20_poly1305_test.json": func(t *testing.T, key []byte) cipher.AEAD {
|
||||
aead, err := chacha20poly1305.New(key)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to construct cipher: %s", err)
|
||||
}
|
||||
return aead
|
||||
},
|
||||
"xchacha20_poly1305_test.json": func(t *testing.T, key []byte) cipher.AEAD {
|
||||
aead, err := chacha20poly1305.NewX(key)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to construct cipher: %s", err)
|
||||
}
|
||||
return aead
|
||||
},
|
||||
}
|
||||
for file, cipherInit := range vectors {
|
||||
var root Root
|
||||
readTestVector(t, file, &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
for _, tv := range tg.Tests {
|
||||
testName := fmt.Sprintf("%s #%d", file, tv.TcId)
|
||||
if tv.Comment != "" {
|
||||
testName += fmt.Sprintf(" %s", tv.Comment)
|
||||
}
|
||||
t.Run(testName, func(t *testing.T) {
|
||||
aead := cipherInit(t, decodeHex(tv.Key))
|
||||
testSealOpen(t, aead, tv, func() {
|
||||
// A bad nonce causes a panic in AEAD.Seal and AEAD.Open,
|
||||
// so should be recovered. Fail the test if it broke for
|
||||
// some other reason.
|
||||
if r := recover(); r != nil {
|
||||
if tg.IvSize/8 == aead.NonceSize() {
|
||||
t.Error("unexpected panic")
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
// Copyright 2019 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 wycheproof
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAesCbc(t *testing.T) {
|
||||
// IndCpaTestVector
|
||||
type IndCpaTestVector struct {
|
||||
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
|
||||
// the raw ciphertext (without IV)
|
||||
Ct string `json:"ct,omitempty"`
|
||||
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
|
||||
// the initialization vector
|
||||
Iv string `json:"iv,omitempty"`
|
||||
|
||||
// the key
|
||||
Key string `json:"key,omitempty"`
|
||||
|
||||
// the plaintext
|
||||
Msg string `json:"msg,omitempty"`
|
||||
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
|
||||
// Identifier of the test case
|
||||
TcId int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
// Notes a description of the labels used in the test vectors
|
||||
type Notes struct {
|
||||
}
|
||||
|
||||
// IndCpaTestGroup
|
||||
type IndCpaTestGroup struct {
|
||||
|
||||
// the IV size in bits
|
||||
IvSize int `json:"ivSize,omitempty"`
|
||||
|
||||
// the keySize in bits
|
||||
KeySize int `json:"keySize,omitempty"`
|
||||
|
||||
// the expected size of the tag in bits
|
||||
TagSize int `json:"tagSize,omitempty"`
|
||||
Tests []*IndCpaTestVector `json:"tests,omitempty"`
|
||||
Type interface{} `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// Root
|
||||
type Root struct {
|
||||
|
||||
// the primitive tested in the test file
|
||||
Algorithm string `json:"algorithm,omitempty"`
|
||||
|
||||
// the version of the test vectors.
|
||||
GeneratorVersion string `json:"generatorVersion,omitempty"`
|
||||
|
||||
// additional documentation
|
||||
Header []string `json:"header,omitempty"`
|
||||
|
||||
// a description of the labels used in the test vectors
|
||||
Notes *Notes `json:"notes,omitempty"`
|
||||
|
||||
// the number of test vectors in this test
|
||||
NumberOfTests int `json:"numberOfTests,omitempty"`
|
||||
Schema interface{} `json:"schema,omitempty"`
|
||||
TestGroups []*IndCpaTestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
var root Root
|
||||
readTestVector(t, "aes_cbc_pkcs5_test.json", &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
tests:
|
||||
for _, tv := range tg.Tests {
|
||||
block, err := aes.NewCipher(decodeHex(tv.Key))
|
||||
if err != nil {
|
||||
t.Fatalf("#%d: %v", tv.TcId, err)
|
||||
}
|
||||
mode := cipher.NewCBCDecrypter(block, decodeHex(tv.Iv))
|
||||
ct := decodeHex(tv.Ct)
|
||||
if len(ct)%aes.BlockSize != 0 {
|
||||
panic(fmt.Sprintf("#%d: ciphertext is not a multiple of the block size", tv.TcId))
|
||||
}
|
||||
mode.CryptBlocks(ct, ct) // decrypt the block in place
|
||||
|
||||
// Skip the tests that are broken due to bad padding. Panic if there are any
|
||||
// tests left that are invalid for some other reason in the future, to
|
||||
// evaluate what to do with those tests.
|
||||
for _, flag := range tv.Flags {
|
||||
if flag == "BadPadding" {
|
||||
continue tests
|
||||
}
|
||||
}
|
||||
if !shouldPass(tv.Result, tv.Flags, nil) {
|
||||
panic(fmt.Sprintf("#%d: found an invalid test that is broken for some reason other than bad padding", tv.TcId))
|
||||
}
|
||||
|
||||
// Remove the PKCS#5 padding from the given ciphertext to validate it
|
||||
padding := ct[len(ct)-1]
|
||||
paddingNum := int(padding)
|
||||
for i := paddingNum; i > 0; i-- {
|
||||
if ct[len(ct)-i] != padding { // panic if the padding is unexpectedly bad
|
||||
panic(fmt.Sprintf("#%d: bad padding at index=%d of %v", tv.TcId, i, ct))
|
||||
}
|
||||
}
|
||||
ct = ct[:len(ct)-paddingNum]
|
||||
|
||||
if got, want := hex.EncodeToString(ct), tv.Msg; got != want {
|
||||
t.Errorf("#%d, type: %s, comment: %q, decoded ciphertext not equal: %s, want %s", tv.TcId, tv.Result, tv.Comment, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
// Copyright 2022 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.
|
||||
|
||||
//go:build boringcrypto
|
||||
|
||||
package wycheproof
|
||||
|
||||
const boringcryptoEnabled = true
|
||||
@@ -1,123 +0,0 @@
|
||||
// Copyright 2019 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 wycheproof
|
||||
|
||||
import (
|
||||
"crypto/dsa"
|
||||
"testing"
|
||||
|
||||
wdsa "golang.org/x/crypto/internal/wycheproof/internal/dsa"
|
||||
)
|
||||
|
||||
func TestDsa(t *testing.T) {
|
||||
// AsnSignatureTestVector
|
||||
type AsnSignatureTestVector struct {
|
||||
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
|
||||
// The message to sign
|
||||
Msg string `json:"msg,omitempty"`
|
||||
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
|
||||
// An ASN encoded signature for msg
|
||||
Sig string `json:"sig,omitempty"`
|
||||
|
||||
// Identifier of the test case
|
||||
TcId int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
// DsaPublicKey
|
||||
type DsaPublicKey struct {
|
||||
|
||||
// the generator of the multiplicative subgroup
|
||||
G string `json:"g,omitempty"`
|
||||
|
||||
// the key size in bits
|
||||
KeySize int `json:"keySize,omitempty"`
|
||||
|
||||
// the modulus p
|
||||
P string `json:"p,omitempty"`
|
||||
|
||||
// the order of the generator g
|
||||
Q string `json:"q,omitempty"`
|
||||
|
||||
// the key type
|
||||
Type string `json:"type,omitempty"`
|
||||
|
||||
// the public key value
|
||||
Y string `json:"y,omitempty"`
|
||||
}
|
||||
|
||||
// DsaTestGroup
|
||||
type DsaTestGroup struct {
|
||||
|
||||
// unenocded DSA public key
|
||||
Key *DsaPublicKey `json:"key,omitempty"`
|
||||
|
||||
// DER encoded public key
|
||||
KeyDer string `json:"keyDer,omitempty"`
|
||||
|
||||
// Pem encoded public key
|
||||
KeyPem string `json:"keyPem,omitempty"`
|
||||
|
||||
// the hash function used for DSA
|
||||
Sha string `json:"sha,omitempty"`
|
||||
Tests []*AsnSignatureTestVector `json:"tests,omitempty"`
|
||||
Type interface{} `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// Notes a description of the labels used in the test vectors
|
||||
type Notes struct {
|
||||
}
|
||||
|
||||
// Root
|
||||
type Root struct {
|
||||
|
||||
// the primitive tested in the test file
|
||||
Algorithm string `json:"algorithm,omitempty"`
|
||||
|
||||
// the version of the test vectors.
|
||||
GeneratorVersion string `json:"generatorVersion,omitempty"`
|
||||
|
||||
// additional documentation
|
||||
Header []string `json:"header,omitempty"`
|
||||
|
||||
// a description of the labels used in the test vectors
|
||||
Notes *Notes `json:"notes,omitempty"`
|
||||
|
||||
// the number of test vectors in this test
|
||||
NumberOfTests int `json:"numberOfTests,omitempty"`
|
||||
Schema interface{} `json:"schema,omitempty"`
|
||||
TestGroups []*DsaTestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
flagsShouldPass := map[string]bool{
|
||||
// An encoded ASN.1 integer missing a leading zero is invalid, but accepted by some implementations.
|
||||
"NoLeadingZero": false,
|
||||
}
|
||||
|
||||
var root Root
|
||||
readTestVector(t, "dsa_test.json", &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
pub := decodePublicKey(tg.KeyDer).(*dsa.PublicKey)
|
||||
h := parseHash(tg.Sha).New()
|
||||
for _, sig := range tg.Tests {
|
||||
h.Reset()
|
||||
h.Write(decodeHex(sig.Msg))
|
||||
hashed := h.Sum(nil)
|
||||
hashed = hashed[:pub.Q.BitLen()/8] // Truncate to the byte-length of the subgroup (Q)
|
||||
got := wdsa.VerifyASN1(pub, hashed, decodeHex(sig.Sig))
|
||||
if want := shouldPass(sig.Result, sig.Flags, flagsShouldPass); got != want {
|
||||
t.Errorf("tcid: %d, type: %s, comment: %q, wanted success: %t", sig.TcId, sig.Result, sig.Comment, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
// Copyright 2022 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 wycheproof
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ecdh"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestECDHStdLib(t *testing.T) {
|
||||
type ECDHTestVector struct {
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
// the private key
|
||||
Private string `json:"private,omitempty"`
|
||||
// Encoded public key
|
||||
Public string `json:"public,omitempty"`
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
// The shared secret key
|
||||
Shared string `json:"shared,omitempty"`
|
||||
// Identifier of the test case
|
||||
TcID int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
type ECDHTestGroup struct {
|
||||
Curve string `json:"curve,omitempty"`
|
||||
Tests []*ECDHTestVector `json:"tests,omitempty"`
|
||||
}
|
||||
|
||||
type Root struct {
|
||||
TestGroups []*ECDHTestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
flagsShouldPass := map[string]bool{
|
||||
// We don't support compressed points.
|
||||
"CompressedPoint": false,
|
||||
// We don't support decoding custom curves.
|
||||
"UnnamedCurve": false,
|
||||
// WrongOrder and UnusedParam are only found with UnnamedCurve.
|
||||
"WrongOrder": false,
|
||||
"UnusedParam": false,
|
||||
|
||||
// X25519 specific flags
|
||||
"Twist": true,
|
||||
"SmallPublicKey": false,
|
||||
"LowOrderPublic": false,
|
||||
"ZeroSharedSecret": false,
|
||||
"NonCanonicalPublic": true,
|
||||
}
|
||||
|
||||
// curveToCurve is a map of all elliptic curves supported
|
||||
// by crypto/elliptic, which can subsequently be parsed and tested.
|
||||
curveToCurve := map[string]ecdh.Curve{
|
||||
"secp256r1": ecdh.P256(),
|
||||
"secp384r1": ecdh.P384(),
|
||||
"secp521r1": ecdh.P521(),
|
||||
"curve25519": ecdh.X25519(),
|
||||
}
|
||||
|
||||
curveToKeySize := map[string]int{
|
||||
"secp256r1": 32,
|
||||
"secp384r1": 48,
|
||||
"secp521r1": 66,
|
||||
"curve25519": 32,
|
||||
}
|
||||
|
||||
for _, f := range []string{
|
||||
"ecdh_secp256r1_ecpoint_test.json",
|
||||
"ecdh_secp384r1_ecpoint_test.json",
|
||||
"ecdh_secp521r1_ecpoint_test.json",
|
||||
"x25519_test.json",
|
||||
} {
|
||||
var root Root
|
||||
readTestVector(t, f, &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
if _, ok := curveToCurve[tg.Curve]; !ok {
|
||||
continue
|
||||
}
|
||||
for _, tt := range tg.Tests {
|
||||
tg, tt := tg, tt
|
||||
t.Run(fmt.Sprintf("%s/%d", tg.Curve, tt.TcID), func(t *testing.T) {
|
||||
t.Logf("Type: %v", tt.Result)
|
||||
t.Logf("Flags: %q", tt.Flags)
|
||||
t.Log(tt.Comment)
|
||||
|
||||
shouldPass := shouldPass(tt.Result, tt.Flags, flagsShouldPass)
|
||||
|
||||
curve := curveToCurve[tg.Curve]
|
||||
p := decodeHex(tt.Public)
|
||||
pub, err := curve.NewPublicKey(p)
|
||||
if err != nil {
|
||||
if shouldPass {
|
||||
t.Errorf("NewPublicKey: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
privBytes := decodeHex(tt.Private)
|
||||
if len(privBytes) != curveToKeySize[tg.Curve] {
|
||||
t.Skipf("non-standard key size %d", len(privBytes))
|
||||
}
|
||||
|
||||
priv, err := curve.NewPrivateKey(privBytes)
|
||||
if err != nil {
|
||||
if shouldPass {
|
||||
t.Errorf("NewPrivateKey: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
shared := decodeHex(tt.Shared)
|
||||
x, err := priv.ECDH(pub)
|
||||
if err != nil {
|
||||
if tg.Curve == "curve25519" && !shouldPass {
|
||||
// ECDH is expected to only return an error when using X25519,
|
||||
// in all other cases an error is unexpected.
|
||||
return
|
||||
}
|
||||
t.Fatalf("ECDH: %v", err)
|
||||
}
|
||||
|
||||
if bytes.Equal(shared, x) != shouldPass {
|
||||
if shouldPass {
|
||||
t.Errorf("ECDH = %x, want %x", shared, x)
|
||||
} else {
|
||||
t.Errorf("ECDH = %x, want anything else", shared)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
// Copyright 2019 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 wycheproof
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/x509"
|
||||
"encoding/asn1"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/cryptobyte"
|
||||
casn1 "golang.org/x/crypto/cryptobyte/asn1"
|
||||
)
|
||||
|
||||
func TestECDH(t *testing.T) {
|
||||
type ECDHTestVector struct {
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
// the private key
|
||||
Private string `json:"private,omitempty"`
|
||||
// Encoded public key
|
||||
Public string `json:"public,omitempty"`
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
// The shared secret key
|
||||
Shared string `json:"shared,omitempty"`
|
||||
// Identifier of the test case
|
||||
TcID int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
type ECDHTestGroup struct {
|
||||
Curve string `json:"curve,omitempty"`
|
||||
Tests []*ECDHTestVector `json:"tests,omitempty"`
|
||||
}
|
||||
|
||||
type Root struct {
|
||||
TestGroups []*ECDHTestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
flagsShouldPass := map[string]bool{
|
||||
// ParsePKIXPublicKey doesn't support compressed points, but we test
|
||||
// them against UnmarshalCompressed anyway.
|
||||
"CompressedPoint": true,
|
||||
// We don't support decoding custom curves.
|
||||
"UnnamedCurve": false,
|
||||
// WrongOrder and UnusedParam are only found with UnnamedCurve.
|
||||
"WrongOrder": false,
|
||||
"UnusedParam": false,
|
||||
}
|
||||
|
||||
// supportedCurves is a map of all elliptic curves supported
|
||||
// by crypto/elliptic, which can subsequently be parsed and tested.
|
||||
supportedCurves := map[string]bool{
|
||||
"secp224r1": true,
|
||||
"secp256r1": true,
|
||||
"secp384r1": true,
|
||||
"secp521r1": true,
|
||||
}
|
||||
|
||||
var root Root
|
||||
readTestVector(t, "ecdh_test.json", &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
if !supportedCurves[tg.Curve] {
|
||||
continue
|
||||
}
|
||||
for _, tt := range tg.Tests {
|
||||
tg, tt := tg, tt
|
||||
t.Run(fmt.Sprintf("%s/%d", tg.Curve, tt.TcID), func(t *testing.T) {
|
||||
t.Logf("Type: %v", tt.Result)
|
||||
t.Logf("Flags: %q", tt.Flags)
|
||||
t.Log(tt.Comment)
|
||||
|
||||
shouldPass := shouldPass(tt.Result, tt.Flags, flagsShouldPass)
|
||||
|
||||
p := decodeHex(tt.Public)
|
||||
pp, err := x509.ParsePKIXPublicKey(p)
|
||||
if err != nil {
|
||||
pp, err = decodeCompressedPKIX(p)
|
||||
}
|
||||
if err != nil {
|
||||
if shouldPass {
|
||||
t.Errorf("unexpected parsing error: %s", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
pub := pp.(*ecdsa.PublicKey)
|
||||
|
||||
priv := decodeHex(tt.Private)
|
||||
shared := decodeHex(tt.Shared)
|
||||
|
||||
x, _ := pub.Curve.ScalarMult(pub.X, pub.Y, priv)
|
||||
xBytes := make([]byte, (pub.Curve.Params().BitSize+7)/8)
|
||||
got := bytes.Equal(shared, x.FillBytes(xBytes))
|
||||
|
||||
if want := shouldPass; got != want {
|
||||
t.Errorf("wanted success %v, got %v", want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func decodeCompressedPKIX(der []byte) (interface{}, error) {
|
||||
s := cryptobyte.String(der)
|
||||
var s1, s2 cryptobyte.String
|
||||
var algoOID, namedCurveOID asn1.ObjectIdentifier
|
||||
var pointDER []byte
|
||||
if !s.ReadASN1(&s1, casn1.SEQUENCE) || !s.Empty() ||
|
||||
!s1.ReadASN1(&s2, casn1.SEQUENCE) ||
|
||||
!s2.ReadASN1ObjectIdentifier(&algoOID) ||
|
||||
!s2.ReadASN1ObjectIdentifier(&namedCurveOID) || !s2.Empty() ||
|
||||
!s1.ReadASN1BitStringAsBytes(&pointDER) || !s1.Empty() {
|
||||
return nil, errors.New("failed to parse PKIX structure")
|
||||
}
|
||||
|
||||
if !algoOID.Equal(oidPublicKeyECDSA) {
|
||||
return nil, errors.New("wrong algorithm OID")
|
||||
}
|
||||
namedCurve := namedCurveFromOID(namedCurveOID)
|
||||
if namedCurve == nil {
|
||||
return nil, errors.New("unsupported elliptic curve")
|
||||
}
|
||||
x, y := elliptic.UnmarshalCompressed(namedCurve, pointDER)
|
||||
if x == nil {
|
||||
return nil, errors.New("failed to unmarshal elliptic curve point")
|
||||
}
|
||||
pub := &ecdsa.PublicKey{
|
||||
Curve: namedCurve,
|
||||
X: x,
|
||||
Y: y,
|
||||
}
|
||||
return pub, nil
|
||||
}
|
||||
|
||||
var (
|
||||
oidPublicKeyECDSA = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1}
|
||||
oidNamedCurveP224 = asn1.ObjectIdentifier{1, 3, 132, 0, 33}
|
||||
oidNamedCurveP256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7}
|
||||
oidNamedCurveP384 = asn1.ObjectIdentifier{1, 3, 132, 0, 34}
|
||||
oidNamedCurveP521 = asn1.ObjectIdentifier{1, 3, 132, 0, 35}
|
||||
)
|
||||
|
||||
func namedCurveFromOID(oid asn1.ObjectIdentifier) elliptic.Curve {
|
||||
switch {
|
||||
case oid.Equal(oidNamedCurveP224):
|
||||
return elliptic.P224()
|
||||
case oid.Equal(oidNamedCurveP256):
|
||||
return elliptic.P256()
|
||||
case oid.Equal(oidNamedCurveP384):
|
||||
return elliptic.P384()
|
||||
case oid.Equal(oidNamedCurveP521):
|
||||
return elliptic.P521()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
// Copyright 2019 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 wycheproof
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/cryptobyte"
|
||||
"golang.org/x/crypto/cryptobyte/asn1"
|
||||
)
|
||||
|
||||
func TestECDSA(t *testing.T) {
|
||||
type ASNSignatureTestVector struct {
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment"`
|
||||
// A list of flags
|
||||
Flags []string `json:"flags"`
|
||||
// The message to sign
|
||||
Msg string `json:"msg"`
|
||||
// Test result
|
||||
Result string `json:"result"`
|
||||
// An ASN.1 encoded signature for msg
|
||||
Sig string `json:"sig"`
|
||||
// Identifier of the test case
|
||||
TcID int `json:"tcId"`
|
||||
}
|
||||
|
||||
type ECPublicKey struct {
|
||||
// The EC group used by this public key
|
||||
Curve interface{} `json:"curve"`
|
||||
}
|
||||
|
||||
type ECDSATestGroup struct {
|
||||
// Unencoded EC public key
|
||||
Key *ECPublicKey `json:"key"`
|
||||
// DER encoded public key
|
||||
KeyDER string `json:"keyDer"`
|
||||
// the hash function used for ECDSA
|
||||
SHA string `json:"sha"`
|
||||
Tests []*ASNSignatureTestVector `json:"tests"`
|
||||
}
|
||||
|
||||
type Root struct {
|
||||
TestGroups []*ECDSATestGroup `json:"testGroups"`
|
||||
}
|
||||
|
||||
flagsShouldPass := map[string]bool{
|
||||
// An encoded ASN.1 integer missing a leading zero is invalid, but
|
||||
// accepted by some implementations.
|
||||
"MissingZero": false,
|
||||
// A signature using a weaker hash than the EC params is not a security
|
||||
// risk, as long as the hash is secure.
|
||||
// https://www.imperialviolet.org/2014/05/25/strengthmatching.html
|
||||
"WeakHash": true,
|
||||
}
|
||||
|
||||
// supportedCurves is a map of all elliptic curves supported
|
||||
// by crypto/elliptic, which can subsequently be parsed and tested.
|
||||
supportedCurves := map[string]bool{
|
||||
"secp224r1": true,
|
||||
"secp256r1": true,
|
||||
"secp384r1": true,
|
||||
"secp521r1": true,
|
||||
}
|
||||
|
||||
var root Root
|
||||
readTestVector(t, "ecdsa_test.json", &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
curve := tg.Key.Curve.(string)
|
||||
if !supportedCurves[curve] {
|
||||
continue
|
||||
}
|
||||
pub := decodePublicKey(tg.KeyDER).(*ecdsa.PublicKey)
|
||||
h := parseHash(tg.SHA).New()
|
||||
for _, sig := range tg.Tests {
|
||||
h.Reset()
|
||||
h.Write(decodeHex(sig.Msg))
|
||||
hashed := h.Sum(nil)
|
||||
sigBytes := decodeHex(sig.Sig)
|
||||
got := ecdsa.VerifyASN1(pub, hashed, sigBytes)
|
||||
if want := shouldPass(sig.Result, sig.Flags, flagsShouldPass); got != want {
|
||||
t.Errorf("tcid: %d, type: %s, comment: %q, VerifyASN1 wanted success: %t", sig.TcID, sig.Result, sig.Comment, want)
|
||||
}
|
||||
|
||||
var r, s big.Int
|
||||
var inner cryptobyte.String
|
||||
input := cryptobyte.String(sigBytes)
|
||||
if !input.ReadASN1(&inner, asn1.SEQUENCE) ||
|
||||
!input.Empty() ||
|
||||
!inner.ReadASN1Integer(&r) ||
|
||||
!inner.ReadASN1Integer(&s) ||
|
||||
!inner.Empty() {
|
||||
continue
|
||||
}
|
||||
got = ecdsa.Verify(pub, hashed, &r, &s)
|
||||
if want := shouldPass(sig.Result, sig.Flags, flagsShouldPass); got != want {
|
||||
t.Errorf("tcid: %d, type: %s, comment: %q, Verify wanted success: %t", sig.TcID, sig.Result, sig.Comment, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
// Copyright 2019 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 wycheproof
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEddsa(t *testing.T) {
|
||||
// Jwk the private key in webcrypto format
|
||||
type Jwk struct {
|
||||
}
|
||||
|
||||
// Key unencoded key pair
|
||||
type Key struct {
|
||||
}
|
||||
|
||||
// Notes a description of the labels used in the test vectors
|
||||
type Notes struct {
|
||||
}
|
||||
|
||||
// SignatureTestVector
|
||||
type SignatureTestVector struct {
|
||||
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
|
||||
// The message to sign
|
||||
Msg string `json:"msg,omitempty"`
|
||||
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
|
||||
// A signature for msg
|
||||
Sig string `json:"sig,omitempty"`
|
||||
|
||||
// Identifier of the test case
|
||||
TcId int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
// EddsaTestGroup
|
||||
type EddsaTestGroup struct {
|
||||
|
||||
// the private key in webcrypto format
|
||||
Jwk *Jwk `json:"jwk,omitempty"`
|
||||
|
||||
// unencoded key pair
|
||||
Key *Key `json:"key,omitempty"`
|
||||
|
||||
// Asn encoded public key
|
||||
KeyDer string `json:"keyDer,omitempty"`
|
||||
|
||||
// Pem encoded public key
|
||||
KeyPem string `json:"keyPem,omitempty"`
|
||||
Tests []*SignatureTestVector `json:"tests,omitempty"`
|
||||
Type interface{} `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// Root
|
||||
type Root struct {
|
||||
|
||||
// the primitive tested in the test file
|
||||
Algorithm string `json:"algorithm,omitempty"`
|
||||
|
||||
// the version of the test vectors.
|
||||
GeneratorVersion string `json:"generatorVersion,omitempty"`
|
||||
|
||||
// additional documentation
|
||||
Header []string `json:"header,omitempty"`
|
||||
|
||||
// a description of the labels used in the test vectors
|
||||
Notes *Notes `json:"notes,omitempty"`
|
||||
|
||||
// the number of test vectors in this test
|
||||
NumberOfTests int `json:"numberOfTests,omitempty"`
|
||||
Schema interface{} `json:"schema,omitempty"`
|
||||
TestGroups []*EddsaTestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
var root Root
|
||||
readTestVector(t, "eddsa_test.json", &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
pub := decodePublicKey(tg.KeyDer).(ed25519.PublicKey)
|
||||
for _, sig := range tg.Tests {
|
||||
got := ed25519.Verify(pub, decodeHex(sig.Msg), decodeHex(sig.Sig))
|
||||
if want := shouldPass(sig.Result, sig.Flags, nil); got != want {
|
||||
t.Errorf("tcid: %d, type: %s, comment: %q, wanted success: %t", sig.TcId, sig.Result, sig.Comment, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
// Copyright 2019 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 wycheproof
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/hkdf"
|
||||
)
|
||||
|
||||
func TestHkdf(t *testing.T) {
|
||||
|
||||
// HkdfTestVector
|
||||
type HkdfTestVector struct {
|
||||
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
|
||||
// the key (input key material)
|
||||
Ikm string `json:"ikm,omitempty"`
|
||||
|
||||
// additional information used in the key derivation
|
||||
Info string `json:"info,omitempty"`
|
||||
|
||||
// the generated bytes (output key material)
|
||||
Okm string `json:"okm,omitempty"`
|
||||
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
|
||||
// the salt for the key derivation
|
||||
Salt string `json:"salt,omitempty"`
|
||||
|
||||
// the size of the output in bytes
|
||||
Size int `json:"size,omitempty"`
|
||||
|
||||
// Identifier of the test case
|
||||
TcId int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
// Notes a description of the labels used in the test vectors
|
||||
type Notes struct {
|
||||
}
|
||||
|
||||
// HkdfTestGroup
|
||||
type HkdfTestGroup struct {
|
||||
|
||||
// the size of the ikm in bits
|
||||
KeySize int `json:"keySize,omitempty"`
|
||||
Tests []*HkdfTestVector `json:"tests,omitempty"`
|
||||
Type interface{} `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// Root
|
||||
type Root struct {
|
||||
|
||||
// the primitive tested in the test file
|
||||
Algorithm string `json:"algorithm,omitempty"`
|
||||
|
||||
// the version of the test vectors.
|
||||
GeneratorVersion string `json:"generatorVersion,omitempty"`
|
||||
|
||||
// additional documentation
|
||||
Header []string `json:"header,omitempty"`
|
||||
|
||||
// a description of the labels used in the test vectors
|
||||
Notes *Notes `json:"notes,omitempty"`
|
||||
|
||||
// the number of test vectors in this test
|
||||
NumberOfTests int `json:"numberOfTests,omitempty"`
|
||||
Schema interface{} `json:"schema,omitempty"`
|
||||
TestGroups []*HkdfTestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
fileHashAlgorithms := map[string]string{
|
||||
"hkdf_sha1_test.json": "SHA-1",
|
||||
"hkdf_sha256_test.json": "SHA-256",
|
||||
"hkdf_sha384_test.json": "SHA-384",
|
||||
"hkdf_sha512_test.json": "SHA-512",
|
||||
}
|
||||
|
||||
for f := range fileHashAlgorithms {
|
||||
var root Root
|
||||
readTestVector(t, f, &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
for _, tv := range tg.Tests {
|
||||
h := parseHash(fileHashAlgorithms[f]).New
|
||||
hkdf := hkdf.New(h, decodeHex(tv.Ikm), decodeHex(tv.Salt), decodeHex(tv.Info))
|
||||
key := make([]byte, tv.Size)
|
||||
wantPass := shouldPass(tv.Result, tv.Flags, nil)
|
||||
_, err := io.ReadFull(hkdf, key)
|
||||
if (err == nil) != wantPass {
|
||||
t.Errorf("tcid: %d, type: %s, comment: %q, wanted success: %t, got: %v", tv.TcId, tv.Result, tv.Comment, wantPass, err)
|
||||
}
|
||||
if err != nil {
|
||||
continue // don't validate output text if reading failed
|
||||
}
|
||||
if got, want := key, decodeHex(tv.Okm); !bytes.Equal(got, want) {
|
||||
t.Errorf("tcid: %d, type: %s, comment: %q, output bytes don't match", tv.TcId, tv.Result, tv.Comment)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
// Copyright 2020 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 wycheproof
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHMAC(t *testing.T) {
|
||||
// MacTestVector
|
||||
type MacTestVector struct {
|
||||
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
|
||||
// the key
|
||||
Key string `json:"key,omitempty"`
|
||||
|
||||
// the plaintext
|
||||
Msg string `json:"msg,omitempty"`
|
||||
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
|
||||
// the authentication tag
|
||||
Tag string `json:"tag,omitempty"`
|
||||
|
||||
// Identifier of the test case
|
||||
TcId int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
// MacTestGroup
|
||||
type MacTestGroup struct {
|
||||
|
||||
// the keySize in bits
|
||||
KeySize int `json:"keySize,omitempty"`
|
||||
|
||||
// the expected size of the tag in bits
|
||||
TagSize int `json:"tagSize,omitempty"`
|
||||
Tests []*MacTestVector `json:"tests,omitempty"`
|
||||
Type interface{} `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// Notes a description of the labels used in the test vectors
|
||||
type Notes struct {
|
||||
}
|
||||
|
||||
// Root
|
||||
type Root struct {
|
||||
|
||||
// the primitive tested in the test file
|
||||
Algorithm string `json:"algorithm,omitempty"`
|
||||
|
||||
// the version of the test vectors.
|
||||
GeneratorVersion string `json:"generatorVersion,omitempty"`
|
||||
|
||||
// additional documentation
|
||||
Header []string `json:"header,omitempty"`
|
||||
|
||||
// a description of the labels used in the test vectors
|
||||
Notes *Notes `json:"notes,omitempty"`
|
||||
|
||||
// the number of test vectors in this test
|
||||
NumberOfTests int `json:"numberOfTests,omitempty"`
|
||||
Schema interface{} `json:"schema,omitempty"`
|
||||
TestGroups []*MacTestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
fileHashAlgs := map[string]string{
|
||||
"hmac_sha1_test.json": "SHA-1",
|
||||
"hmac_sha224_test.json": "SHA-224",
|
||||
"hmac_sha256_test.json": "SHA-256",
|
||||
"hmac_sha384_test.json": "SHA-384",
|
||||
"hmac_sha512_test.json": "SHA-512",
|
||||
}
|
||||
|
||||
for f := range fileHashAlgs {
|
||||
var root Root
|
||||
readTestVector(t, f, &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
h := parseHash(fileHashAlgs[f])
|
||||
// Skip test vectors where the tag length does not equal the
|
||||
// hash length, since crypto/hmac does not support generating
|
||||
// these truncated tags.
|
||||
if tg.TagSize/8 != h.Size() {
|
||||
continue
|
||||
}
|
||||
for _, tv := range tg.Tests {
|
||||
hm := hmac.New(h.New, decodeHex(tv.Key))
|
||||
hm.Write(decodeHex(tv.Msg))
|
||||
tag := hm.Sum(nil)
|
||||
got := hmac.Equal(decodeHex(tv.Tag), tag)
|
||||
if want := shouldPass(tv.Result, tv.Flags, nil); want != got {
|
||||
t.Errorf("%s, tcid: %d, type: %s, comment: %q, unexpected result", f, tv.TcId, tv.Result, tv.Comment)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
// Copyright 2019 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 dsa provides an internal version of dsa.Verify
|
||||
// that is used for the Wycheproof tests.
|
||||
package dsa
|
||||
|
||||
import (
|
||||
"crypto/dsa"
|
||||
"math/big"
|
||||
|
||||
"golang.org/x/crypto/cryptobyte"
|
||||
"golang.org/x/crypto/cryptobyte/asn1"
|
||||
)
|
||||
|
||||
// VerifyASN1 verifies the ASN1 encoded signature, sig, of hash using the
|
||||
// public key, pub. Its return value records whether the signature is valid.
|
||||
func VerifyASN1(pub *dsa.PublicKey, hash, sig []byte) bool {
|
||||
var (
|
||||
r, s = &big.Int{}, &big.Int{}
|
||||
inner cryptobyte.String
|
||||
)
|
||||
input := cryptobyte.String(sig)
|
||||
if !input.ReadASN1(&inner, asn1.SEQUENCE) ||
|
||||
!input.Empty() ||
|
||||
!inner.ReadASN1Integer(r) ||
|
||||
!inner.ReadASN1Integer(s) ||
|
||||
!inner.Empty() {
|
||||
return false
|
||||
}
|
||||
return dsa.Verify(pub, hash, r, s)
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
// Copyright 2022 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.
|
||||
|
||||
//go:build !boringcrypto
|
||||
|
||||
package wycheproof
|
||||
|
||||
const boringcryptoEnabled = false
|
||||
@@ -1,149 +0,0 @@
|
||||
// Copyright 2020 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 wycheproof
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRSAOAEPDecrypt(t *testing.T) {
|
||||
// Notes a description of the labels used in the test vectors
|
||||
type Notes struct {
|
||||
}
|
||||
|
||||
// RsaesOaepTestVector
|
||||
type RsaesOaepTestVector struct {
|
||||
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
|
||||
// An encryption of msg
|
||||
Ct string `json:"ct,omitempty"`
|
||||
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
|
||||
// The label used for the encryption
|
||||
Label string `json:"label,omitempty"`
|
||||
|
||||
// The encrypted message
|
||||
Msg string `json:"msg,omitempty"`
|
||||
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
|
||||
// Identifier of the test case
|
||||
TcId int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
// RsaesOaepTestGroup
|
||||
type RsaesOaepTestGroup struct {
|
||||
|
||||
// The private exponent
|
||||
D string `json:"d,omitempty"`
|
||||
|
||||
// The public exponent
|
||||
E string `json:"e,omitempty"`
|
||||
|
||||
// the message generating function (e.g. MGF1)
|
||||
Mgf string `json:"mgf,omitempty"`
|
||||
|
||||
// The hash function used for the message generating function.
|
||||
MgfSha string `json:"mgfSha,omitempty"`
|
||||
|
||||
// The modulus of the key
|
||||
N string `json:"n,omitempty"`
|
||||
|
||||
// Pem encoded private key
|
||||
PrivateKeyPem string `json:"privateKeyPem,omitempty"`
|
||||
|
||||
// Pkcs 8 encoded private key.
|
||||
PrivateKeyPkcs8 string `json:"privateKeyPkcs8,omitempty"`
|
||||
|
||||
// The hash function for hashing the label.
|
||||
Sha string `json:"sha,omitempty"`
|
||||
Tests []*RsaesOaepTestVector `json:"tests,omitempty"`
|
||||
Type interface{} `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// Root
|
||||
type Root struct {
|
||||
|
||||
// the primitive tested in the test file
|
||||
Algorithm string `json:"algorithm,omitempty"`
|
||||
|
||||
// the version of the test vectors.
|
||||
GeneratorVersion string `json:"generatorVersion,omitempty"`
|
||||
|
||||
// additional documentation
|
||||
Header []string `json:"header,omitempty"`
|
||||
|
||||
// a description of the labels used in the test vectors
|
||||
Notes *Notes `json:"notes,omitempty"`
|
||||
|
||||
// the number of test vectors in this test
|
||||
NumberOfTests int `json:"numberOfTests,omitempty"`
|
||||
Schema interface{} `json:"schema,omitempty"`
|
||||
TestGroups []*RsaesOaepTestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
// rsa.DecryptOAEP doesn't support using a different hash for the
|
||||
// MGF and the label, so skip all of the test vectors that use
|
||||
// these unbalanced constructions. rsa_oaep_misc_test.json contains
|
||||
// both balanced and unbalanced constructions so in that case
|
||||
// we just filter out any test groups where MgfSha != Sha
|
||||
files := []string{
|
||||
"rsa_oaep_2048_sha1_mgf1sha1_test.json",
|
||||
"rsa_oaep_2048_sha224_mgf1sha224_test.json",
|
||||
"rsa_oaep_2048_sha256_mgf1sha256_test.json",
|
||||
"rsa_oaep_2048_sha384_mgf1sha384_test.json",
|
||||
"rsa_oaep_2048_sha512_mgf1sha512_test.json",
|
||||
"rsa_oaep_3072_sha256_mgf1sha256_test.json",
|
||||
"rsa_oaep_3072_sha512_mgf1sha512_test.json",
|
||||
"rsa_oaep_4096_sha256_mgf1sha256_test.json",
|
||||
"rsa_oaep_4096_sha512_mgf1sha512_test.json",
|
||||
"rsa_oaep_misc_test.json",
|
||||
}
|
||||
|
||||
flagsShouldPass := map[string]bool{
|
||||
// rsa.DecryptOAEP happily supports small key sizes
|
||||
"SmallModulus": true,
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
var root Root
|
||||
readTestVector(t, f, &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
if tg.MgfSha != tg.Sha {
|
||||
continue
|
||||
}
|
||||
priv, err := x509.ParsePKCS8PrivateKey(decodeHex(tg.PrivateKeyPkcs8))
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to parse PKCS #8 private key: %s", f, err)
|
||||
}
|
||||
hash := parseHash(tg.Sha)
|
||||
for _, tv := range tg.Tests {
|
||||
t.Run(fmt.Sprintf("%s #%d", f, tv.TcId), func(t *testing.T) {
|
||||
wantPass := shouldPass(tv.Result, tv.Flags, flagsShouldPass)
|
||||
plaintext, err := rsa.DecryptOAEP(hash.New(), nil, priv.(*rsa.PrivateKey), decodeHex(tv.Ct), decodeHex(tv.Label))
|
||||
if wantPass {
|
||||
if err != nil {
|
||||
t.Fatalf("comment: %s, expected success: %s", tv.Comment, err)
|
||||
}
|
||||
if !bytes.Equal(plaintext, decodeHex(tv.Msg)) {
|
||||
t.Errorf("comment: %s, unexpected plaintext: got %x, want %s", tv.Comment, plaintext, tv.Msg)
|
||||
}
|
||||
} else if err == nil {
|
||||
t.Errorf("comment: %s, expected failure", tv.Comment)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
// Copyright 2019 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 wycheproof
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRsaPss(t *testing.T) {
|
||||
// KeyJwk Public key in JWK format
|
||||
type KeyJwk struct {
|
||||
}
|
||||
|
||||
// Notes a description of the labels used in the test vectors
|
||||
type Notes struct {
|
||||
}
|
||||
|
||||
// SignatureTestVector
|
||||
type SignatureTestVector struct {
|
||||
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
|
||||
// The message to sign
|
||||
Msg string `json:"msg,omitempty"`
|
||||
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
|
||||
// A signature for msg
|
||||
Sig string `json:"sig,omitempty"`
|
||||
|
||||
// Identifier of the test case
|
||||
TcId int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
// RsassaPkcs1TestGroup
|
||||
type RsassaPkcs1TestGroup struct {
|
||||
|
||||
// The private exponent
|
||||
D string `json:"d,omitempty"`
|
||||
|
||||
// The public exponent
|
||||
E string `json:"e,omitempty"`
|
||||
|
||||
// ASN encoding of the sequence [n, e]
|
||||
KeyAsn string `json:"keyAsn,omitempty"`
|
||||
|
||||
// ASN encoding of the public key
|
||||
KeyDer string `json:"keyDer,omitempty"`
|
||||
|
||||
// Public key in JWK format
|
||||
KeyJwk *KeyJwk `json:"keyJwk,omitempty"`
|
||||
|
||||
// Pem encoded public key
|
||||
KeyPem string `json:"keyPem,omitempty"`
|
||||
|
||||
// the size of the modulus in bits
|
||||
KeySize int `json:"keySize,omitempty"`
|
||||
|
||||
// The modulus of the key
|
||||
N string `json:"n,omitempty"`
|
||||
|
||||
// The salt length
|
||||
SLen int `json:"sLen,omitempty"`
|
||||
|
||||
// the hash function used for the message
|
||||
Sha string `json:"sha,omitempty"`
|
||||
Tests []*SignatureTestVector `json:"tests,omitempty"`
|
||||
Type interface{} `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// Root
|
||||
type Root struct {
|
||||
|
||||
// the primitive tested in the test file
|
||||
Algorithm string `json:"algorithm,omitempty"`
|
||||
|
||||
// the version of the test vectors.
|
||||
GeneratorVersion string `json:"generatorVersion,omitempty"`
|
||||
|
||||
// additional documentation
|
||||
Header []string `json:"header,omitempty"`
|
||||
|
||||
// a description of the labels used in the test vectors
|
||||
Notes *Notes `json:"notes,omitempty"`
|
||||
|
||||
// the number of test vectors in this test
|
||||
NumberOfTests int `json:"numberOfTests,omitempty"`
|
||||
Schema interface{} `json:"schema,omitempty"`
|
||||
TestGroups []*RsassaPkcs1TestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
flagsShouldPass := map[string]bool{
|
||||
// A signature using a weaker hash than the EC params is not a security risk, as long as the hash is secure.
|
||||
// https://www.imperialviolet.org/2014/05/25/strengthmatching.html
|
||||
"WeakHash": true,
|
||||
}
|
||||
|
||||
// filesOverrideToPassZeroSLen is a map of all test files
|
||||
// and which TcIds that should be overridden to pass if the
|
||||
// rsa.PSSOptions.SaltLength is zero.
|
||||
// These tests expect a failure with a PSSOptions.SaltLength: 0
|
||||
// and a signature that uses a different salt length. However,
|
||||
// a salt length of 0 is defined as rsa.PSSSaltLengthAuto which
|
||||
// works deterministically to auto-detect the length when
|
||||
// verifying, so these tests actually pass as they should.
|
||||
filesOverrideToPassZeroSLen := map[string][]int{
|
||||
"rsa_pss_2048_sha1_mgf1_20_test.json": []int{46, 47},
|
||||
"rsa_pss_2048_sha256_mgf1_0_test.json": []int{67, 68},
|
||||
"rsa_pss_2048_sha256_mgf1_32_test.json": []int{67, 68},
|
||||
"rsa_pss_3072_sha256_mgf1_32_test.json": []int{67, 68},
|
||||
"rsa_pss_4096_sha256_mgf1_32_test.json": []int{67, 68},
|
||||
"rsa_pss_4096_sha512_mgf1_32_test.json": []int{136, 137},
|
||||
// "rsa_pss_misc_test.json": nil, // TODO: This ones seems to be broken right now, but can enable later on.
|
||||
}
|
||||
|
||||
if !boringcryptoEnabled {
|
||||
// boringcrypto doesn't support the truncated SHA-512 hashes, so only
|
||||
// test them if boringcrypto isn't enabled.
|
||||
filesOverrideToPassZeroSLen["rsa_pss_2048_sha512_256_mgf1_28_test.json"] = []int{13, 14, 15}
|
||||
filesOverrideToPassZeroSLen["rsa_pss_2048_sha512_256_mgf1_32_test.json"] = []int{13, 14}
|
||||
}
|
||||
|
||||
for f := range filesOverrideToPassZeroSLen {
|
||||
var root Root
|
||||
readTestVector(t, f, &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
pub := decodePublicKey(tg.KeyDer).(*rsa.PublicKey)
|
||||
ch := parseHash(tg.Sha)
|
||||
h := ch.New()
|
||||
opts := &rsa.PSSOptions{
|
||||
Hash: ch,
|
||||
SaltLength: rsa.PSSSaltLengthAuto,
|
||||
}
|
||||
// Run all the tests twice: the first time with the salt length
|
||||
// as PSSSaltLengthAuto, and the second time with the salt length
|
||||
// explictily set to tg.SLen.
|
||||
for i := 0; i < 2; i++ {
|
||||
for _, sig := range tg.Tests {
|
||||
h.Reset()
|
||||
h.Write(decodeHex(sig.Msg))
|
||||
hashed := h.Sum(nil)
|
||||
err := rsa.VerifyPSS(pub, ch, hashed, decodeHex(sig.Sig), opts)
|
||||
want := shouldPass(sig.Result, sig.Flags, flagsShouldPass)
|
||||
if opts.SaltLength == 0 {
|
||||
for _, id := range filesOverrideToPassZeroSLen[f] {
|
||||
if sig.TcId == id {
|
||||
want = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if (err == nil) != want {
|
||||
t.Errorf("file: %v, tcid: %d, type: %s, opts.SaltLength: %v, comment: %q, wanted success: %t", f, sig.TcId, sig.Result, opts.SaltLength, sig.Comment, want)
|
||||
}
|
||||
}
|
||||
// Update opts.SaltLength for the second run of the tests.
|
||||
opts.SaltLength = tg.SLen
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
// Copyright 2019 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 wycheproof
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRsa(t *testing.T) {
|
||||
// KeyJwk Public key in JWK format
|
||||
type KeyJwk struct {
|
||||
}
|
||||
|
||||
// Notes a description of the labels used in the test vectors
|
||||
type Notes struct {
|
||||
}
|
||||
|
||||
// SignatureTestVector
|
||||
type SignatureTestVector struct {
|
||||
|
||||
// A brief description of the test case
|
||||
Comment string `json:"comment,omitempty"`
|
||||
|
||||
// A list of flags
|
||||
Flags []string `json:"flags,omitempty"`
|
||||
|
||||
// The message to sign
|
||||
Msg string `json:"msg,omitempty"`
|
||||
|
||||
// Test result
|
||||
Result string `json:"result,omitempty"`
|
||||
|
||||
// A signature for msg
|
||||
Sig string `json:"sig,omitempty"`
|
||||
|
||||
// Identifier of the test case
|
||||
TcId int `json:"tcId,omitempty"`
|
||||
}
|
||||
|
||||
// RsassaPkcs1TestGroup
|
||||
type RsassaPkcs1TestGroup struct {
|
||||
|
||||
// The private exponent
|
||||
D string `json:"d,omitempty"`
|
||||
|
||||
// The public exponent
|
||||
E string `json:"e,omitempty"`
|
||||
|
||||
// ASN encoding of the sequence [n, e]
|
||||
KeyAsn string `json:"keyAsn,omitempty"`
|
||||
|
||||
// ASN encoding of the public key
|
||||
KeyDer string `json:"keyDer,omitempty"`
|
||||
|
||||
// Public key in JWK format
|
||||
KeyJwk *KeyJwk `json:"keyJwk,omitempty"`
|
||||
|
||||
// Pem encoded public key
|
||||
KeyPem string `json:"keyPem,omitempty"`
|
||||
|
||||
// the size of the modulus in bits
|
||||
KeySize int `json:"keySize,omitempty"`
|
||||
|
||||
// The modulus of the key
|
||||
N string `json:"n,omitempty"`
|
||||
|
||||
// the hash function used for the message
|
||||
Sha string `json:"sha,omitempty"`
|
||||
Tests []*SignatureTestVector `json:"tests,omitempty"`
|
||||
Type interface{} `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// Root
|
||||
type Root struct {
|
||||
|
||||
// the primitive tested in the test file
|
||||
Algorithm string `json:"algorithm,omitempty"`
|
||||
|
||||
// the version of the test vectors.
|
||||
GeneratorVersion string `json:"generatorVersion,omitempty"`
|
||||
|
||||
// additional documentation
|
||||
Header []string `json:"header,omitempty"`
|
||||
|
||||
// a description of the labels used in the test vectors
|
||||
Notes *Notes `json:"notes,omitempty"`
|
||||
|
||||
// the number of test vectors in this test
|
||||
NumberOfTests int `json:"numberOfTests,omitempty"`
|
||||
Schema interface{} `json:"schema,omitempty"`
|
||||
TestGroups []*RsassaPkcs1TestGroup `json:"testGroups,omitempty"`
|
||||
}
|
||||
|
||||
flagsShouldPass := map[string]bool{
|
||||
// Omitting the parameter field in an ASN encoded integer is a legacy behavior.
|
||||
"MissingNull": false,
|
||||
// Keys with a modulus less than 2048 bits are supported by crypto/rsa.
|
||||
"SmallModulus": true,
|
||||
// Small public keys are supported by crypto/rsa.
|
||||
"SmallPublicKey": true,
|
||||
}
|
||||
|
||||
var root Root
|
||||
readTestVector(t, "rsa_signature_test.json", &root)
|
||||
for _, tg := range root.TestGroups {
|
||||
pub := decodePublicKey(tg.KeyDer).(*rsa.PublicKey)
|
||||
ch := parseHash(tg.Sha)
|
||||
h := ch.New()
|
||||
for _, sig := range tg.Tests {
|
||||
h.Reset()
|
||||
h.Write(decodeHex(sig.Msg))
|
||||
hashed := h.Sum(nil)
|
||||
err := rsa.VerifyPKCS1v15(pub, ch, hashed, decodeHex(sig.Sig))
|
||||
want := shouldPass(sig.Result, sig.Flags, flagsShouldPass)
|
||||
if (err == nil) != want {
|
||||
t.Errorf("tcid: %d, type: %s, comment: %q, wanted success: %t", sig.TcId, sig.Result, sig.Comment, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
// Copyright 2019 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 wycheproof runs a set of the Wycheproof tests
|
||||
// provided by https://github.com/google/wycheproof.
|
||||
package wycheproof
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/x509"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
_ "crypto/sha1"
|
||||
_ "crypto/sha256"
|
||||
_ "crypto/sha512"
|
||||
)
|
||||
|
||||
const wycheproofModVer = "v0.0.0-20191219022705-2196000605e4"
|
||||
|
||||
var wycheproofTestVectorsDir string
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
flag.Parse()
|
||||
if flag.Lookup("test.short").Value.(flag.Getter).Get().(bool) {
|
||||
log.Println("skipping test that downloads testdata via 'go mod download' in short mode")
|
||||
os.Exit(0)
|
||||
}
|
||||
if _, err := exec.LookPath("go"); err != nil {
|
||||
log.Printf("skipping test because 'go' command is unavailable: %v", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
if os.Getenv("GO_BUILDER_FLAKY_NET") != "" {
|
||||
log.Printf("skipping test because GO_BUILDER_FLAKY_NET is set")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Download the JSON test files from github.com/google/wycheproof
|
||||
// using `go mod download -json` so the cached source of the testdata
|
||||
// can be used in the following tests.
|
||||
path := "github.com/google/wycheproof@" + wycheproofModVer
|
||||
cmd := exec.Command("go", "mod", "download", "-json", path)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to run `go mod download -json %s`, output: %s", path, output)
|
||||
}
|
||||
var dm struct {
|
||||
Dir string // absolute path to cached source root directory
|
||||
}
|
||||
if err := json.Unmarshal(output, &dm); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Now that the module has been downloaded, use the absolute path of the
|
||||
// cached source as the root directory for all tests going forward.
|
||||
wycheproofTestVectorsDir = filepath.Join(dm.Dir, "testvectors")
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func readTestVector(t *testing.T, f string, dest interface{}) {
|
||||
b, err := os.ReadFile(filepath.Join(wycheproofTestVectorsDir, f))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read json file: %v", err)
|
||||
}
|
||||
if err := json.Unmarshal(b, &dest); err != nil {
|
||||
t.Fatalf("failed to unmarshal json file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func decodeHex(s string) []byte {
|
||||
b, err := hex.DecodeString(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func decodePublicKey(der string) interface{} {
|
||||
d := decodeHex(der)
|
||||
pub, err := x509.ParsePKIXPublicKey(d)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to parse DER encoded public key: %v", err))
|
||||
}
|
||||
return pub
|
||||
}
|
||||
|
||||
func parseHash(h string) crypto.Hash {
|
||||
switch h {
|
||||
case "SHA-1":
|
||||
return crypto.SHA1
|
||||
case "SHA-256":
|
||||
return crypto.SHA256
|
||||
case "SHA-224":
|
||||
return crypto.SHA224
|
||||
case "SHA-384":
|
||||
return crypto.SHA384
|
||||
case "SHA-512":
|
||||
return crypto.SHA512
|
||||
case "SHA-512/224":
|
||||
return crypto.SHA512_224
|
||||
case "SHA-512/256":
|
||||
return crypto.SHA512_256
|
||||
default:
|
||||
panic(fmt.Sprintf("could not identify SHA hash algorithm: %q", h))
|
||||
}
|
||||
}
|
||||
|
||||
// shouldPass returns whether or not the test should pass.
|
||||
// flagsShouldPass is a map associated with whether or not
|
||||
// a flag for an "acceptable" result should pass.
|
||||
// Every possible flag value that's associated with an
|
||||
// "acceptable" result should be explicitly specified,
|
||||
// otherwise the test will panic.
|
||||
func shouldPass(result string, flags []string, flagsShouldPass map[string]bool) bool {
|
||||
switch result {
|
||||
case "valid":
|
||||
return true
|
||||
case "invalid":
|
||||
return false
|
||||
case "acceptable":
|
||||
for _, flag := range flags {
|
||||
pass, ok := flagsShouldPass[flag]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("unspecified flag: %q", flag))
|
||||
}
|
||||
if !pass {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true // There are no flags, or all are meant to pass.
|
||||
default:
|
||||
panic(fmt.Sprintf("unexpected result: %v", result))
|
||||
}
|
||||
}
|
||||
+10
-19
@@ -2,25 +2,16 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
/*
|
||||
Package auth authenticates a message using a secret key.
|
||||
|
||||
The Sum function, viewed as a function of the message for a uniform random
|
||||
key, is designed to meet the standard notion of unforgeability. This means
|
||||
that an attacker cannot find authenticators for any messages not authenticated
|
||||
by the sender, even if the attacker has adaptively influenced the messages
|
||||
authenticated by the sender. For a formal definition see, e.g., Section 2.4
|
||||
of Bellare, Kilian, and Rogaway, "The security of the cipher block chaining
|
||||
message authentication code," Journal of Computer and System Sciences 61 (2000),
|
||||
362–399; http://www-cse.ucsd.edu/~mihir/papers/cbc.html.
|
||||
|
||||
auth does not make any promises regarding "strong" unforgeability; perhaps
|
||||
one valid authenticator can be converted into another valid authenticator for
|
||||
the same message. NaCl also does not make any promises regarding "truncated
|
||||
unforgeability."
|
||||
|
||||
This package is interoperable with NaCl: https://nacl.cr.yp.to/auth.html.
|
||||
*/
|
||||
// Package auth authenticates a message using a secret key.
|
||||
//
|
||||
// This package is interoperable with [NaCl].
|
||||
//
|
||||
// The auth package is essentially a wrapper for HMAC-SHA-512 (implemented by
|
||||
// crypto/hmac and crypto/sha512), truncated to 32 bytes. It is [frozen] and is
|
||||
// not accepting new features.
|
||||
//
|
||||
// [NaCl]: https://nacl.cr.yp.to/auth.html
|
||||
// [frozen]: https://go.dev/wiki/Frozen
|
||||
package auth
|
||||
|
||||
import (
|
||||
|
||||
@@ -114,7 +114,7 @@ func TestSealOpenAnonymous(t *testing.T) {
|
||||
t.Fatal("expected out to be unchanged")
|
||||
}
|
||||
if !bytes.HasPrefix(box, orig) {
|
||||
t.Fatal("expected out to be coppied to returned slice")
|
||||
t.Fatal("expected out to be copied to returned slice")
|
||||
}
|
||||
_, ok = OpenAnonymous(nil, box[len(out):], publicKey, privateKey)
|
||||
if !ok {
|
||||
|
||||
+8
-13
@@ -4,20 +4,15 @@
|
||||
|
||||
// Package sign signs small messages using public-key cryptography.
|
||||
//
|
||||
// Sign uses Ed25519 to sign messages. The length of messages is not hidden.
|
||||
// Messages should be small because:
|
||||
// 1. The whole message needs to be held in memory to be processed.
|
||||
// 2. Using large messages pressures implementations on small machines to process
|
||||
// plaintext without verifying the signature. This is very dangerous, and this API
|
||||
// discourages it, but a protocol that uses excessive message sizes might present
|
||||
// some implementations with no other choice.
|
||||
// 3. Performance may be improved by working with messages that fit into data caches.
|
||||
// Thus large amounts of data should be chunked so that each message is small.
|
||||
// This package is interoperable with [libsodium], as well as [TweetNaCl].
|
||||
//
|
||||
// This package is not interoperable with the current release of NaCl
|
||||
// (https://nacl.cr.yp.to/sign.html), which does not support Ed25519 yet. However,
|
||||
// it is compatible with the NaCl fork libsodium (https://www.libsodium.org), as well
|
||||
// as TweetNaCl (https://tweetnacl.cr.yp.to/).
|
||||
// The sign package is essentially a wrapper for the Ed25519 signature
|
||||
// algorithm (implemented by crypto/ed25519). It is [frozen] and is not accepting
|
||||
// new features.
|
||||
//
|
||||
// [libsodium]: https://libsodium.gitbook.io/doc/public-key_cryptography/public-key_signatures
|
||||
// [TweetNaCl]: https://tweetnacl.cr.yp.to/
|
||||
// [frozen]: https://go.dev/wiki/Frozen
|
||||
package sign
|
||||
|
||||
import (
|
||||
|
||||
@@ -69,6 +69,6 @@ func TestGenerateSignOpen(t *testing.T) {
|
||||
}
|
||||
|
||||
if !bytes.Equal(message, testMessage) {
|
||||
t.Fatalf("verified message does not match signed messge, got\n%x\n, expected\n%x", message, testMessage)
|
||||
t.Fatalf("verified message does not match signed message, got\n%x\n, expected\n%x", message, testMessage)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,12 @@
|
||||
// Package armor implements OpenPGP ASCII Armor, see RFC 4880. OpenPGP Armor is
|
||||
// very similar to PEM except that it has an additional CRC checksum.
|
||||
//
|
||||
// Deprecated: this package is unmaintained except for security fixes. New
|
||||
// applications should consider a more focused, modern alternative to OpenPGP
|
||||
// for their specific task. If you are required to interoperate with OpenPGP
|
||||
// systems and need a maintained package, consider a community fork.
|
||||
// See https://golang.org/issue/44226.
|
||||
// Deprecated: this package is unsafe by design, and has numerous known security
|
||||
// issues. It is not maintained, and should not be used. New applications should
|
||||
// consider a more focused, modern alternative to OpenPGP for their specific
|
||||
// task. If you are required to interoperate with OpenPGP systems and need a
|
||||
// maintained package, consider github.com/ProtonMail/go-crypto/openpgp, which
|
||||
// is a maintained fork that aims to be a drop-in replacement for this package.
|
||||
package armor
|
||||
|
||||
import (
|
||||
|
||||
@@ -8,11 +8,12 @@
|
||||
// Clearsigned messages are cryptographically signed, but the contents of the
|
||||
// message are kept in plaintext so that it can be read without special tools.
|
||||
//
|
||||
// Deprecated: this package is unmaintained except for security fixes. New
|
||||
// applications should consider a more focused, modern alternative to OpenPGP
|
||||
// for their specific task. If you are required to interoperate with OpenPGP
|
||||
// systems and need a maintained package, consider a community fork.
|
||||
// See https://golang.org/issue/44226.
|
||||
// Deprecated: this package is unsafe by design, and has numerous known security
|
||||
// issues. It is not maintained, and should not be used. New applications should
|
||||
// consider a more focused, modern alternative to OpenPGP for their specific
|
||||
// task. If you are required to interoperate with OpenPGP systems and need a
|
||||
// maintained package, consider github.com/ProtonMail/go-crypto/openpgp, which
|
||||
// is a maintained fork that aims to be a drop-in replacement for this package.
|
||||
package clearsign
|
||||
|
||||
import (
|
||||
|
||||
@@ -123,24 +123,12 @@ func TestSigning(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// We use this to make test keys, so that they aren't all the same.
|
||||
type quickRand byte
|
||||
|
||||
func (qr *quickRand) Read(p []byte) (int, error) {
|
||||
for i := range p {
|
||||
p[i] = byte(*qr)
|
||||
}
|
||||
*qr++
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func TestMultiSign(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping long test in -short mode")
|
||||
}
|
||||
|
||||
zero := quickRand(0)
|
||||
config := packet.Config{Rand: &zero}
|
||||
var config packet.Config
|
||||
|
||||
for nKeys := 0; nKeys < 4; nKeys++ {
|
||||
nextTest:
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
|
||||
// Package errors contains common error types for the OpenPGP packages.
|
||||
//
|
||||
// Deprecated: this package is unmaintained except for security fixes. New
|
||||
// applications should consider a more focused, modern alternative to OpenPGP
|
||||
// for their specific task. If you are required to interoperate with OpenPGP
|
||||
// systems and need a maintained package, consider a community fork.
|
||||
// See https://golang.org/issue/44226.
|
||||
// Deprecated: this package is unsafe by design, and has numerous known security
|
||||
// issues. It is not maintained, and should not be used. New applications should
|
||||
// consider a more focused, modern alternative to OpenPGP for their specific
|
||||
// task. If you are required to interoperate with OpenPGP systems and need a
|
||||
// maintained package, consider github.com/ProtonMail/go-crypto/openpgp, which
|
||||
// is a maintained fork that aims to be a drop-in replacement for this package.
|
||||
package errors
|
||||
|
||||
import (
|
||||
|
||||
@@ -51,7 +51,7 @@ func TestOpaqueParseReason(t *testing.T) {
|
||||
const expectedBad = 3
|
||||
// Test post-conditions, make sure we actually parsed packets as expected.
|
||||
if badPackets != expectedBad {
|
||||
t.Errorf("unexpected # unparseable packets: %d (want %d)", badPackets, expectedBad)
|
||||
t.Errorf("unexpected # unparsable packets: %d (want %d)", badPackets, expectedBad)
|
||||
}
|
||||
if uid == nil {
|
||||
t.Errorf("failed to find expected UID in unsupported keyring")
|
||||
|
||||
@@ -5,11 +5,12 @@
|
||||
// Package packet implements parsing and serialization of OpenPGP packets, as
|
||||
// specified in RFC 4880.
|
||||
//
|
||||
// Deprecated: this package is unmaintained except for security fixes. New
|
||||
// applications should consider a more focused, modern alternative to OpenPGP
|
||||
// for their specific task. If you are required to interoperate with OpenPGP
|
||||
// systems and need a maintained package, consider a community fork.
|
||||
// See https://golang.org/issue/44226.
|
||||
// Deprecated: this package is unsafe by design, and has numerous known security
|
||||
// issues. It is not maintained, and should not be used. New applications should
|
||||
// consider a more focused, modern alternative to OpenPGP for their specific
|
||||
// task. If you are required to interoperate with OpenPGP systems and need a
|
||||
// maintained package, consider github.com/ProtonMail/go-crypto/openpgp, which
|
||||
// is a maintained fork that aims to be a drop-in replacement for this package.
|
||||
package packet
|
||||
|
||||
import (
|
||||
|
||||
+6
-5
@@ -4,11 +4,12 @@
|
||||
|
||||
// Package openpgp implements high level operations on OpenPGP messages.
|
||||
//
|
||||
// Deprecated: this package is unmaintained except for security fixes. New
|
||||
// applications should consider a more focused, modern alternative to OpenPGP
|
||||
// for their specific task. If you are required to interoperate with OpenPGP
|
||||
// systems and need a maintained package, consider a community fork.
|
||||
// See https://golang.org/issue/44226.
|
||||
// Deprecated: this package is unsafe by design, and has numerous known security
|
||||
// issues. It is not maintained, and should not be used. New applications should
|
||||
// consider a more focused, modern alternative to OpenPGP for their specific
|
||||
// task. If you are required to interoperate with OpenPGP systems and need a
|
||||
// maintained package, consider github.com/ProtonMail/go-crypto/openpgp, which
|
||||
// is a maintained fork that aims to be a drop-in replacement for this package.
|
||||
package openpgp
|
||||
|
||||
import (
|
||||
|
||||
+7
-6
@@ -5,11 +5,12 @@
|
||||
// Package s2k implements the various OpenPGP string-to-key transforms as
|
||||
// specified in RFC 4800 section 3.7.1.
|
||||
//
|
||||
// Deprecated: this package is unmaintained except for security fixes. New
|
||||
// applications should consider a more focused, modern alternative to OpenPGP
|
||||
// for their specific task. If you are required to interoperate with OpenPGP
|
||||
// systems and need a maintained package, consider a community fork.
|
||||
// See https://golang.org/issue/44226.
|
||||
// Deprecated: this package is unsafe by design, and has numerous known security
|
||||
// issues. It is not maintained, and should not be used. New applications should
|
||||
// consider a more focused, modern alternative to OpenPGP for their specific
|
||||
// task. If you are required to interoperate with OpenPGP systems and need a
|
||||
// maintained package, consider github.com/ProtonMail/go-crypto/openpgp, which
|
||||
// is a maintained fork that aims to be a drop-in replacement for this package.
|
||||
package s2k
|
||||
|
||||
import (
|
||||
@@ -53,7 +54,7 @@ func (c *Config) hash() crypto.Hash {
|
||||
|
||||
func (c *Config) encodedCount() uint8 {
|
||||
if c == nil || c.S2KCount == 0 {
|
||||
return 96 // The common case. Correspoding to 65536
|
||||
return 96 // The common case. Corresponding to 65536
|
||||
}
|
||||
|
||||
i := c.S2KCount
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
// The version of OTR implemented by this package has been deprecated
|
||||
// (https://bugs.otr.im/lib/libotr/issues/140). An implementation of OTRv3 is
|
||||
// available at https://github.com/coyim/otr3.
|
||||
//
|
||||
// The otr package is [frozen] and is not accepting new features.
|
||||
//
|
||||
// [frozen]: https://go.dev/wiki/Frozen
|
||||
package otr
|
||||
|
||||
import (
|
||||
|
||||
+3
-3
@@ -75,8 +75,8 @@ const libOTRPrivateKey = `(privkeys
|
||||
(account
|
||||
(name "foo@example.com")
|
||||
(protocol prpl-jabber)
|
||||
(private-key
|
||||
(dsa
|
||||
(private-key
|
||||
(dsa
|
||||
(p #00FC07ABCF0DC916AFF6E9AE47BEF60C7AB9B4D6B2469E436630E36F8A489BE812486A09F30B71224508654940A835301ACC525A4FF133FC152CC53DCC59D65C30A54F1993FE13FE63E5823D4C746DB21B90F9B9C00B49EC7404AB1D929BA7FBA12F2E45C6E0A651689750E8528AB8C031D3561FECEE72EBB4A090D450A9B7A857#)
|
||||
(q #00997BD266EF7B1F60A5C23F3A741F2AEFD07A2081#)
|
||||
(g #535E360E8A95EBA46A4F7DE50AD6E9B2A6DB785A66B64EB9F20338D2A3E8FB0E94725848F1AA6CC567CB83A1CC517EC806F2E92EAE71457E80B2210A189B91250779434B41FC8A8873F6DB94BEA7D177F5D59E7E114EE10A49CFD9CEF88AE43387023B672927BA74B04EB6BBB5E57597766A2F9CE3857D7ACE3E1E3BC1FC6F26#)
|
||||
@@ -402,7 +402,7 @@ func TestAgainstLibOTR(t *testing.T) {
|
||||
// This test requires otr.c.test to be built as /tmp/a.out.
|
||||
// If enabled, this tests runs forever performing OTR handshakes in a
|
||||
// loop.
|
||||
return
|
||||
t.Skip("This test requires otr.c.test to be built as /tmp/a.out")
|
||||
|
||||
alicePrivateKey, _ := hex.DecodeString(alicePrivateKeyHex)
|
||||
var alice Conversation
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user