Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00fd4ff485 | ||
|
|
018c28f8a1 | ||
|
|
9cd0187c09 | ||
|
|
c6a20f9984 | ||
|
|
776e461a4e | ||
|
|
ebe92624d1 | ||
|
|
a9f661cb6e | ||
|
|
310bfa40f1 | ||
|
|
59ff47295c | ||
|
|
3d872d0428 | ||
|
|
bc7d1d1eb5 | ||
|
|
7e3ac2043e | ||
|
|
23edec0b38 | ||
|
|
f495dc37d5 | ||
|
|
eb2c406296 | ||
|
|
2c476679df |
+9
-22
@@ -354,11 +354,12 @@ func TestWaitAuthorization(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
t.Run("context cancel", func(t *testing.T) {
|
t.Run("context cancel", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := runWaitAuthorization(ctx, t, func(w http.ResponseWriter, r *http.Request) {
|
_, err := runWaitAuthorization(ctx, t, func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Retry-After", "60")
|
w.Header().Set("Retry-After", "60")
|
||||||
fmt.Fprintf(w, `{"status":"pending"}`)
|
fmt.Fprintf(w, `{"status":"pending"}`)
|
||||||
|
time.AfterFunc(1*time.Millisecond, cancel)
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("err is nil")
|
t.Error("err is nil")
|
||||||
@@ -373,28 +374,14 @@ func runWaitAuthorization(ctx context.Context, t *testing.T, h http.HandlerFunc)
|
|||||||
h(w, r)
|
h(w, r)
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
type res struct {
|
|
||||||
authz *Authorization
|
client := &Client{
|
||||||
err error
|
Key: testKey,
|
||||||
|
DirectoryURL: ts.URL,
|
||||||
|
dir: &Directory{},
|
||||||
|
KID: "some-key-id", // set to avoid lookup attempt
|
||||||
}
|
}
|
||||||
ch := make(chan res, 1)
|
return client.WaitAuthorization(ctx, ts.URL)
|
||||||
go func() {
|
|
||||||
client := &Client{
|
|
||||||
Key: testKey,
|
|
||||||
DirectoryURL: ts.URL,
|
|
||||||
dir: &Directory{},
|
|
||||||
KID: "some-key-id", // set to avoid lookup attempt
|
|
||||||
}
|
|
||||||
a, err := client.WaitAuthorization(ctx, ts.URL)
|
|
||||||
ch <- res{a, err}
|
|
||||||
}()
|
|
||||||
select {
|
|
||||||
case <-time.After(3 * time.Second):
|
|
||||||
t.Fatal("WaitAuthorization took too long to return")
|
|
||||||
case v := <-ch:
|
|
||||||
return v.authz, v.err
|
|
||||||
}
|
|
||||||
panic("runWaitAuthorization: out of select")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRevokeAuthorization(t *testing.T) {
|
func TestRevokeAuthorization(t *testing.T) {
|
||||||
|
|||||||
@@ -427,18 +427,7 @@ func TestGetCertificate(t *testing.T) {
|
|||||||
|
|
||||||
man.Client = &acme.Client{DirectoryURL: s.URL()}
|
man.Client = &acme.Client{DirectoryURL: s.URL()}
|
||||||
|
|
||||||
var tlscert *tls.Certificate
|
tlscert, err := man.GetCertificate(tt.hello)
|
||||||
var err error
|
|
||||||
done := make(chan struct{})
|
|
||||||
go func() {
|
|
||||||
tlscert, err = man.GetCertificate(tt.hello)
|
|
||||||
close(done)
|
|
||||||
}()
|
|
||||||
select {
|
|
||||||
case <-time.After(time.Minute):
|
|
||||||
t.Fatal("man.GetCertificate took too long to return")
|
|
||||||
case <-done:
|
|
||||||
}
|
|
||||||
if tt.expectError != "" {
|
if tt.expectError != "" {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error, got certificate")
|
t.Fatal("expected error, got certificate")
|
||||||
@@ -515,15 +504,12 @@ func TestGetCertificate_failedAttempt(t *testing.T) {
|
|||||||
if _, err := man.GetCertificate(hello); err == nil {
|
if _, err := man.GetCertificate(hello); err == nil {
|
||||||
t.Error("GetCertificate: err is nil")
|
t.Error("GetCertificate: err is nil")
|
||||||
}
|
}
|
||||||
select {
|
|
||||||
case <-time.After(5 * time.Second):
|
<-done
|
||||||
t.Errorf("took too long to remove the %q state", exampleCertKey)
|
man.stateMu.Lock()
|
||||||
case <-done:
|
defer man.stateMu.Unlock()
|
||||||
man.stateMu.Lock()
|
if v, exist := man.state[exampleCertKey]; exist {
|
||||||
defer man.stateMu.Unlock()
|
t.Errorf("state exists for %v: %+v", exampleCertKey, v)
|
||||||
if v, exist := man.state[exampleCertKey]; exist {
|
|
||||||
t.Errorf("state exists for %v: %+v", exampleCertKey, v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -542,8 +528,9 @@ func TestRevokeFailedAuthz(t *testing.T) {
|
|||||||
t.Fatal("expected GetCertificate to fail")
|
t.Fatal("expected GetCertificate to fail")
|
||||||
}
|
}
|
||||||
|
|
||||||
start := time.Now()
|
logTicker := time.NewTicker(3 * time.Second)
|
||||||
for time.Since(start) < 3*time.Second {
|
defer logTicker.Stop()
|
||||||
|
for {
|
||||||
authz, err := m.Client.GetAuthorization(context.Background(), ca.URL()+"/authz/0")
|
authz, err := m.Client.GetAuthorization(context.Background(), ca.URL()+"/authz/0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -551,10 +538,14 @@ func TestRevokeFailedAuthz(t *testing.T) {
|
|||||||
if authz.Status == acme.StatusDeactivated {
|
if authz.Status == acme.StatusDeactivated {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-logTicker.C:
|
||||||
|
t.Logf("still waiting on revocations")
|
||||||
|
default:
|
||||||
|
}
|
||||||
time.Sleep(50 * time.Millisecond)
|
time.Sleep(50 * time.Millisecond)
|
||||||
}
|
}
|
||||||
t.Error("revocations took too long")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHTTPHandlerDefaultFallback(t *testing.T) {
|
func TestHTTPHandlerDefaultFallback(t *testing.T) {
|
||||||
|
|||||||
+1
-1
@@ -117,7 +117,7 @@ func (c *Client) updateRegRFC(ctx context.Context, a *Account) (*Account, error)
|
|||||||
return responseAccount(res)
|
return responseAccount(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getGegRFC is equivalent to c.GetReg but for CAs implementing RFC 8555.
|
// getRegRFC is equivalent to c.GetReg but for CAs implementing RFC 8555.
|
||||||
// It expects c.Discover to have already been called.
|
// It expects c.Discover to have already been called.
|
||||||
func (c *Client) getRegRFC(ctx context.Context) (*Account, error) {
|
func (c *Client) getRegRFC(ctx context.Context) (*Account, error) {
|
||||||
req := json.RawMessage(`{"onlyReturnExisting": true}`)
|
req := json.RawMessage(`{"onlyReturnExisting": true}`)
|
||||||
|
|||||||
+25
-50
@@ -177,8 +177,7 @@ func TestRFC_postKID(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx := context.Background()
|
||||||
defer cancel()
|
|
||||||
cl := &Client{
|
cl := &Client{
|
||||||
Key: testKey,
|
Key: testKey,
|
||||||
DirectoryURL: ts.URL,
|
DirectoryURL: ts.URL,
|
||||||
@@ -316,8 +315,7 @@ func TestRFC_Register(t *testing.T) {
|
|||||||
s.start()
|
s.start()
|
||||||
defer s.close()
|
defer s.close()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx := context.Background()
|
||||||
defer cancel()
|
|
||||||
cl := &Client{
|
cl := &Client{
|
||||||
Key: testKeyEC,
|
Key: testKeyEC,
|
||||||
DirectoryURL: s.url("/"),
|
DirectoryURL: s.url("/"),
|
||||||
@@ -454,8 +452,7 @@ func TestRFC_RegisterExternalAccountBinding(t *testing.T) {
|
|||||||
s.start()
|
s.start()
|
||||||
defer s.close()
|
defer s.close()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx := context.Background()
|
||||||
defer cancel()
|
|
||||||
cl := &Client{
|
cl := &Client{
|
||||||
Key: testKeyEC,
|
Key: testKeyEC,
|
||||||
DirectoryURL: s.url("/"),
|
DirectoryURL: s.url("/"),
|
||||||
@@ -867,24 +864,13 @@ func testWaitOrderStatus(t *testing.T, okStatus string) {
|
|||||||
s.start()
|
s.start()
|
||||||
defer s.close()
|
defer s.close()
|
||||||
|
|
||||||
var order *Order
|
cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")}
|
||||||
var err error
|
order, err := cl.WaitOrder(context.Background(), s.url("/orders/1"))
|
||||||
done := make(chan struct{})
|
if err != nil {
|
||||||
go func() {
|
t.Fatalf("WaitOrder: %v", err)
|
||||||
cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")}
|
}
|
||||||
order, err = cl.WaitOrder(context.Background(), s.url("/orders/1"))
|
if order.Status != okStatus {
|
||||||
close(done)
|
t.Errorf("order.Status = %q; want %q", order.Status, okStatus)
|
||||||
}()
|
|
||||||
select {
|
|
||||||
case <-time.After(3 * time.Second):
|
|
||||||
t.Fatal("WaitOrder took too long to return")
|
|
||||||
case <-done:
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("WaitOrder: %v", err)
|
|
||||||
}
|
|
||||||
if order.Status != okStatus {
|
|
||||||
t.Errorf("order.Status = %q; want %q", order.Status, okStatus)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -909,30 +895,20 @@ func TestRFC_WaitOrderError(t *testing.T) {
|
|||||||
s.start()
|
s.start()
|
||||||
defer s.close()
|
defer s.close()
|
||||||
|
|
||||||
var err error
|
cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")}
|
||||||
done := make(chan struct{})
|
_, err := cl.WaitOrder(context.Background(), s.url("/orders/1"))
|
||||||
go func() {
|
if err == nil {
|
||||||
cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")}
|
t.Fatal("WaitOrder returned nil error")
|
||||||
_, err = cl.WaitOrder(context.Background(), s.url("/orders/1"))
|
}
|
||||||
close(done)
|
e, ok := err.(*OrderError)
|
||||||
}()
|
if !ok {
|
||||||
select {
|
t.Fatalf("err = %v (%T); want OrderError", err, err)
|
||||||
case <-time.After(3 * time.Second):
|
}
|
||||||
t.Fatal("WaitOrder took too long to return")
|
if e.OrderURL != s.url("/orders/1") {
|
||||||
case <-done:
|
t.Errorf("e.OrderURL = %q; want %q", e.OrderURL, s.url("/orders/1"))
|
||||||
if err == nil {
|
}
|
||||||
t.Fatal("WaitOrder returned nil error")
|
if e.Status != StatusInvalid {
|
||||||
}
|
t.Errorf("e.Status = %q; want %q", e.Status, StatusInvalid)
|
||||||
e, ok := err.(*OrderError)
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("err = %v (%T); want OrderError", err, err)
|
|
||||||
}
|
|
||||||
if e.OrderURL != s.url("/orders/1") {
|
|
||||||
t.Errorf("e.OrderURL = %q; want %q", e.OrderURL, s.url("/orders/1"))
|
|
||||||
}
|
|
||||||
if e.Status != StatusInvalid {
|
|
||||||
t.Errorf("e.Status = %q; want %q", e.Status, StatusInvalid)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -972,8 +948,7 @@ func TestRFC_CreateOrderCert(t *testing.T) {
|
|||||||
})
|
})
|
||||||
s.start()
|
s.start()
|
||||||
defer s.close()
|
defer s.close()
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
ctx := context.Background()
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")}
|
cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")}
|
||||||
cert, curl, err := cl.CreateOrderCert(ctx, s.url("/pleaseissue"), csr, true)
|
cert, curl, err := cl.CreateOrderCert(ctx, s.url("/pleaseissue"), csr, true)
|
||||||
|
|||||||
@@ -82,11 +82,20 @@ type hashed struct {
|
|||||||
minor byte
|
minor byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrPasswordTooLong is returned when the password passed to
|
||||||
|
// GenerateFromPassword is too long (i.e. > 72 bytes).
|
||||||
|
var ErrPasswordTooLong = errors.New("bcrypt: password length exceeds 72 bytes")
|
||||||
|
|
||||||
// GenerateFromPassword returns the bcrypt hash of the password at the given
|
// GenerateFromPassword returns the bcrypt hash of the password at the given
|
||||||
// cost. If the cost given is less than MinCost, the cost will be set to
|
// cost. If the cost given is less than MinCost, the cost will be set to
|
||||||
// DefaultCost, instead. Use CompareHashAndPassword, as defined in this package,
|
// DefaultCost, instead. Use CompareHashAndPassword, as defined in this package,
|
||||||
// to compare the returned hashed password with its cleartext version.
|
// to compare the returned hashed password with its cleartext version.
|
||||||
|
// GenerateFromPassword does not accept passwords longer than 72 bytes, which
|
||||||
|
// is the longest password bcrypt will operate on.
|
||||||
func GenerateFromPassword(password []byte, cost int) ([]byte, error) {
|
func GenerateFromPassword(password []byte, cost int) ([]byte, error) {
|
||||||
|
if len(password) > 72 {
|
||||||
|
return nil, ErrPasswordTooLong
|
||||||
|
}
|
||||||
p, err := newFromPassword(password, cost)
|
p, err := newFromPassword(password, cost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -241,3 +241,10 @@ func TestNoSideEffectsFromCompare(t *testing.T) {
|
|||||||
t.Errorf("got=%q want=%q", got, want)
|
t.Errorf("got=%q want=%q", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPasswordTooLong(t *testing.T) {
|
||||||
|
_, err := GenerateFromPassword(make([]byte, 73), 1)
|
||||||
|
if err != ErrPasswordTooLong {
|
||||||
|
t.Errorf("unexpected error: got %q, want %q", err, ErrPasswordTooLong)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+41
-34
@@ -264,36 +264,35 @@ func (s *String) ReadASN1Boolean(out *bool) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
var bigIntType = reflect.TypeOf((*big.Int)(nil)).Elem()
|
|
||||||
|
|
||||||
// ReadASN1Integer decodes an ASN.1 INTEGER into out and advances. If out does
|
// ReadASN1Integer decodes an ASN.1 INTEGER into out and advances. If out does
|
||||||
// not point to an integer or to a big.Int, it panics. It reports whether the
|
// not point to an integer, to a big.Int, or to a []byte it panics. Only
|
||||||
// read was successful.
|
// positive and zero values can be decoded into []byte, and they are returned as
|
||||||
|
// big-endian binary values that share memory with s. Positive values will have
|
||||||
|
// no leading zeroes, and zero will be returned as a single zero byte.
|
||||||
|
// ReadASN1Integer reports whether the read was successful.
|
||||||
func (s *String) ReadASN1Integer(out interface{}) bool {
|
func (s *String) ReadASN1Integer(out interface{}) bool {
|
||||||
if reflect.TypeOf(out).Kind() != reflect.Ptr {
|
switch out := out.(type) {
|
||||||
panic("out is not a pointer")
|
case *int, *int8, *int16, *int32, *int64:
|
||||||
}
|
|
||||||
switch reflect.ValueOf(out).Elem().Kind() {
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
||||||
var i int64
|
var i int64
|
||||||
if !s.readASN1Int64(&i) || reflect.ValueOf(out).Elem().OverflowInt(i) {
|
if !s.readASN1Int64(&i) || reflect.ValueOf(out).Elem().OverflowInt(i) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
reflect.ValueOf(out).Elem().SetInt(i)
|
reflect.ValueOf(out).Elem().SetInt(i)
|
||||||
return true
|
return true
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
case *uint, *uint8, *uint16, *uint32, *uint64:
|
||||||
var u uint64
|
var u uint64
|
||||||
if !s.readASN1Uint64(&u) || reflect.ValueOf(out).Elem().OverflowUint(u) {
|
if !s.readASN1Uint64(&u) || reflect.ValueOf(out).Elem().OverflowUint(u) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
reflect.ValueOf(out).Elem().SetUint(u)
|
reflect.ValueOf(out).Elem().SetUint(u)
|
||||||
return true
|
return true
|
||||||
case reflect.Struct:
|
case *big.Int:
|
||||||
if reflect.TypeOf(out).Elem() == bigIntType {
|
return s.readASN1BigInt(out)
|
||||||
return s.readASN1BigInt(out.(*big.Int))
|
case *[]byte:
|
||||||
}
|
return s.readASN1Bytes(out)
|
||||||
|
default:
|
||||||
|
panic("out does not point to an integer type")
|
||||||
}
|
}
|
||||||
panic("out does not point to an integer type")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkASN1Integer(bytes []byte) bool {
|
func checkASN1Integer(bytes []byte) bool {
|
||||||
@@ -333,6 +332,21 @@ func (s *String) readASN1BigInt(out *big.Int) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *String) readASN1Bytes(out *[]byte) bool {
|
||||||
|
var bytes String
|
||||||
|
if !s.ReadASN1(&bytes, asn1.INTEGER) || !checkASN1Integer(bytes) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if bytes[0]&0x80 == 0x80 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for len(bytes) > 1 && bytes[0] == 0 {
|
||||||
|
bytes = bytes[1:]
|
||||||
|
}
|
||||||
|
*out = bytes
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (s *String) readASN1Int64(out *int64) bool {
|
func (s *String) readASN1Int64(out *int64) bool {
|
||||||
var bytes String
|
var bytes String
|
||||||
if !s.ReadASN1(&bytes, asn1.INTEGER) || !checkASN1Integer(bytes) || !asn1Signed(out, bytes) {
|
if !s.ReadASN1(&bytes, asn1.INTEGER) || !checkASN1Integer(bytes) || !asn1Signed(out, bytes) {
|
||||||
@@ -545,7 +559,7 @@ func (s *String) ReadASN1BitString(out *encoding_asn1.BitString) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadASN1BitString decodes an ASN.1 BIT STRING into out and advances. It is
|
// ReadASN1BitStringAsBytes decodes an ASN.1 BIT STRING into out and advances. It is
|
||||||
// an error if the BIT STRING is not a whole number of bytes. It reports
|
// an error if the BIT STRING is not a whole number of bytes. It reports
|
||||||
// whether the read was successful.
|
// whether the read was successful.
|
||||||
func (s *String) ReadASN1BitStringAsBytes(out *[]byte) bool {
|
func (s *String) ReadASN1BitStringAsBytes(out *[]byte) bool {
|
||||||
@@ -654,34 +668,27 @@ func (s *String) SkipOptionalASN1(tag asn1.Tag) bool {
|
|||||||
return s.ReadASN1(&unused, tag)
|
return s.ReadASN1(&unused, tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadOptionalASN1Integer attempts to read an optional ASN.1 INTEGER
|
// ReadOptionalASN1Integer attempts to read an optional ASN.1 INTEGER explicitly
|
||||||
// explicitly tagged with tag into out and advances. If no element with a
|
// tagged with tag into out and advances. If no element with a matching tag is
|
||||||
// matching tag is present, it writes defaultValue into out instead. If out
|
// present, it writes defaultValue into out instead. Otherwise, it behaves like
|
||||||
// does not point to an integer or to a big.Int, it panics. It reports
|
// ReadASN1Integer.
|
||||||
// whether the read was successful.
|
|
||||||
func (s *String) ReadOptionalASN1Integer(out interface{}, tag asn1.Tag, defaultValue interface{}) bool {
|
func (s *String) ReadOptionalASN1Integer(out interface{}, tag asn1.Tag, defaultValue interface{}) bool {
|
||||||
if reflect.TypeOf(out).Kind() != reflect.Ptr {
|
|
||||||
panic("out is not a pointer")
|
|
||||||
}
|
|
||||||
var present bool
|
var present bool
|
||||||
var i String
|
var i String
|
||||||
if !s.ReadOptionalASN1(&i, &present, tag) {
|
if !s.ReadOptionalASN1(&i, &present, tag) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !present {
|
if !present {
|
||||||
switch reflect.ValueOf(out).Elem().Kind() {
|
switch out.(type) {
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
case *int, *int8, *int16, *int32, *int64,
|
||||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
*uint, *uint8, *uint16, *uint32, *uint64, *[]byte:
|
||||||
reflect.ValueOf(out).Elem().Set(reflect.ValueOf(defaultValue))
|
reflect.ValueOf(out).Elem().Set(reflect.ValueOf(defaultValue))
|
||||||
case reflect.Struct:
|
case *big.Int:
|
||||||
if reflect.TypeOf(out).Elem() != bigIntType {
|
if defaultValue, ok := defaultValue.(*big.Int); ok {
|
||||||
panic("invalid integer type")
|
out.(*big.Int).Set(defaultValue)
|
||||||
}
|
} else {
|
||||||
if reflect.TypeOf(defaultValue).Kind() != reflect.Ptr ||
|
|
||||||
reflect.TypeOf(defaultValue).Elem() != bigIntType {
|
|
||||||
panic("out points to big.Int, but defaultValue does not")
|
panic("out points to big.Int, but defaultValue does not")
|
||||||
}
|
}
|
||||||
out.(*big.Int).Set(defaultValue.(*big.Int))
|
|
||||||
default:
|
default:
|
||||||
panic("invalid integer type")
|
panic("invalid integer type")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,6 +154,32 @@ func TestReadASN1IntegerSigned(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Repeat the same cases, reading into a []byte.
|
||||||
|
t.Run("bytes", func(t *testing.T) {
|
||||||
|
for i, test := range testData64 {
|
||||||
|
in := String(test.in)
|
||||||
|
var out []byte
|
||||||
|
ok := in.ReadASN1Integer(&out)
|
||||||
|
if test.out < 0 {
|
||||||
|
if ok {
|
||||||
|
t.Errorf("#%d: in.ReadASN1Integer(%d) = %v, want false", i, test.out, ok)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("#%d: in.ReadASN1Integer() = %v, want true", i, ok)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
n := new(big.Int).SetBytes(out).Int64()
|
||||||
|
if n != test.out {
|
||||||
|
t.Errorf("#%d: in.ReadASN1Integer() = %v, want true; out = %x, want %d", i, ok, out, test.out)
|
||||||
|
}
|
||||||
|
if out[0] == 0 && len(out) > 1 {
|
||||||
|
t.Errorf("#%d: in.ReadASN1Integer() = %v; out = %x, has leading zeroes", i, ok, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Repeat with the implicit-tagging functions
|
// Repeat with the implicit-tagging functions
|
||||||
t.Run("WithTag", func(t *testing.T) {
|
t.Run("WithTag", func(t *testing.T) {
|
||||||
for i, test := range testData64 {
|
for i, test := range testData64 {
|
||||||
|
|||||||
@@ -303,9 +303,9 @@ func (b *Builder) add(bytes ...byte) {
|
|||||||
b.result = append(b.result, bytes...)
|
b.result = append(b.result, bytes...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unwrite rolls back n bytes written directly to the Builder. An attempt by a
|
// Unwrite rolls back non-negative n bytes written directly to the Builder.
|
||||||
// child builder passed to a continuation to unwrite bytes from its parent will
|
// An attempt by a child builder passed to a continuation to unwrite bytes
|
||||||
// panic.
|
// from its parent will panic.
|
||||||
func (b *Builder) Unwrite(n int) {
|
func (b *Builder) Unwrite(n int) {
|
||||||
if b.err != nil {
|
if b.err != nil {
|
||||||
return
|
return
|
||||||
@@ -317,6 +317,9 @@ func (b *Builder) Unwrite(n int) {
|
|||||||
if length < 0 {
|
if length < 0 {
|
||||||
panic("cryptobyte: internal error")
|
panic("cryptobyte: internal error")
|
||||||
}
|
}
|
||||||
|
if n < 0 {
|
||||||
|
panic("cryptobyte: attempted to unwrite negative number of bytes")
|
||||||
|
}
|
||||||
if n > length {
|
if n > length {
|
||||||
panic("cryptobyte: attempted to unwrite more than was written")
|
panic("cryptobyte: attempted to unwrite more than was written")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,71 +5,18 @@
|
|||||||
// Package curve25519 provides an implementation of the X25519 function, which
|
// Package curve25519 provides an implementation of the X25519 function, which
|
||||||
// performs scalar multiplication on the elliptic curve known as Curve25519.
|
// performs scalar multiplication on the elliptic curve known as Curve25519.
|
||||||
// See RFC 7748.
|
// See RFC 7748.
|
||||||
|
//
|
||||||
|
// Starting in Go 1.20, this package is a wrapper for the X25519 implementation
|
||||||
|
// in the crypto/ecdh package.
|
||||||
package curve25519 // import "golang.org/x/crypto/curve25519"
|
package curve25519 // import "golang.org/x/crypto/curve25519"
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/subtle"
|
|
||||||
"errors"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/curve25519/internal/field"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ScalarMult sets dst to the product scalar * point.
|
// ScalarMult sets dst to the product scalar * point.
|
||||||
//
|
//
|
||||||
// Deprecated: when provided a low-order point, ScalarMult will set dst to all
|
// Deprecated: when provided a low-order point, ScalarMult will set dst to all
|
||||||
// zeroes, irrespective of the scalar. Instead, use the X25519 function, which
|
// zeroes, irrespective of the scalar. Instead, use the X25519 function, which
|
||||||
// will return an error.
|
// will return an error.
|
||||||
func ScalarMult(dst, scalar, point *[32]byte) {
|
func ScalarMult(dst, scalar, point *[32]byte) {
|
||||||
var e [32]byte
|
scalarMult(dst, scalar, point)
|
||||||
|
|
||||||
copy(e[:], scalar[:])
|
|
||||||
e[0] &= 248
|
|
||||||
e[31] &= 127
|
|
||||||
e[31] |= 64
|
|
||||||
|
|
||||||
var x1, x2, z2, x3, z3, tmp0, tmp1 field.Element
|
|
||||||
x1.SetBytes(point[:])
|
|
||||||
x2.One()
|
|
||||||
x3.Set(&x1)
|
|
||||||
z3.One()
|
|
||||||
|
|
||||||
swap := 0
|
|
||||||
for pos := 254; pos >= 0; pos-- {
|
|
||||||
b := e[pos/8] >> uint(pos&7)
|
|
||||||
b &= 1
|
|
||||||
swap ^= int(b)
|
|
||||||
x2.Swap(&x3, swap)
|
|
||||||
z2.Swap(&z3, swap)
|
|
||||||
swap = int(b)
|
|
||||||
|
|
||||||
tmp0.Subtract(&x3, &z3)
|
|
||||||
tmp1.Subtract(&x2, &z2)
|
|
||||||
x2.Add(&x2, &z2)
|
|
||||||
z2.Add(&x3, &z3)
|
|
||||||
z3.Multiply(&tmp0, &x2)
|
|
||||||
z2.Multiply(&z2, &tmp1)
|
|
||||||
tmp0.Square(&tmp1)
|
|
||||||
tmp1.Square(&x2)
|
|
||||||
x3.Add(&z3, &z2)
|
|
||||||
z2.Subtract(&z3, &z2)
|
|
||||||
x2.Multiply(&tmp1, &tmp0)
|
|
||||||
tmp1.Subtract(&tmp1, &tmp0)
|
|
||||||
z2.Square(&z2)
|
|
||||||
|
|
||||||
z3.Mult32(&tmp1, 121666)
|
|
||||||
x3.Square(&x3)
|
|
||||||
tmp0.Add(&tmp0, &z3)
|
|
||||||
z3.Multiply(&x1, &z2)
|
|
||||||
z2.Multiply(&tmp1, &tmp0)
|
|
||||||
}
|
|
||||||
|
|
||||||
x2.Swap(&x3, swap)
|
|
||||||
z2.Swap(&z3, swap)
|
|
||||||
|
|
||||||
z2.Invert(&z2)
|
|
||||||
x2.Multiply(&x2, &z2)
|
|
||||||
copy(dst[:], x2.Bytes())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarBaseMult sets dst to the product scalar * base where base is the
|
// ScalarBaseMult sets dst to the product scalar * base where base is the
|
||||||
@@ -78,7 +25,7 @@ func ScalarMult(dst, scalar, point *[32]byte) {
|
|||||||
// It is recommended to use the X25519 function with Basepoint instead, as
|
// It is recommended to use the X25519 function with Basepoint instead, as
|
||||||
// copying into fixed size arrays can lead to unexpected bugs.
|
// copying into fixed size arrays can lead to unexpected bugs.
|
||||||
func ScalarBaseMult(dst, scalar *[32]byte) {
|
func ScalarBaseMult(dst, scalar *[32]byte) {
|
||||||
ScalarMult(dst, scalar, &basePoint)
|
scalarBaseMult(dst, scalar)
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -91,21 +38,10 @@ const (
|
|||||||
// Basepoint is the canonical Curve25519 generator.
|
// Basepoint is the canonical Curve25519 generator.
|
||||||
var Basepoint []byte
|
var Basepoint []byte
|
||||||
|
|
||||||
var basePoint = [32]byte{9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
var basePoint = [32]byte{9}
|
||||||
|
|
||||||
func init() { Basepoint = basePoint[:] }
|
func init() { Basepoint = basePoint[:] }
|
||||||
|
|
||||||
func checkBasepoint() {
|
|
||||||
if subtle.ConstantTimeCompare(Basepoint, []byte{
|
|
||||||
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
}) != 1 {
|
|
||||||
panic("curve25519: global Basepoint value was modified")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// X25519 returns the result of the scalar multiplication (scalar * point),
|
// X25519 returns the result of the scalar multiplication (scalar * point),
|
||||||
// according to RFC 7748, Section 5. scalar, point and the return value are
|
// according to RFC 7748, Section 5. scalar, point and the return value are
|
||||||
// slices of 32 bytes.
|
// slices of 32 bytes.
|
||||||
@@ -121,26 +57,3 @@ func X25519(scalar, point []byte) ([]byte, error) {
|
|||||||
var dst [32]byte
|
var dst [32]byte
|
||||||
return x25519(&dst, scalar, point)
|
return x25519(&dst, scalar, point)
|
||||||
}
|
}
|
||||||
|
|
||||||
func x25519(dst *[32]byte, scalar, point []byte) ([]byte, error) {
|
|
||||||
var in [32]byte
|
|
||||||
if l := len(scalar); l != 32 {
|
|
||||||
return nil, errors.New("bad scalar length: " + strconv.Itoa(l) + ", expected 32")
|
|
||||||
}
|
|
||||||
if l := len(point); l != 32 {
|
|
||||||
return nil, errors.New("bad point length: " + strconv.Itoa(l) + ", expected 32")
|
|
||||||
}
|
|
||||||
copy(in[:], scalar)
|
|
||||||
if &point[0] == &Basepoint[0] {
|
|
||||||
checkBasepoint()
|
|
||||||
ScalarBaseMult(dst, &in)
|
|
||||||
} else {
|
|
||||||
var base, zero [32]byte
|
|
||||||
copy(base[:], point)
|
|
||||||
ScalarMult(dst, &in, &base)
|
|
||||||
if subtle.ConstantTimeCompare(dst[:], zero[:]) == 1 {
|
|
||||||
return nil, errors.New("bad input point: low order point")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dst[:], nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
// 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 !go1.20
|
||||||
|
|
||||||
|
package curve25519
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/subtle"
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/curve25519/internal/field"
|
||||||
|
)
|
||||||
|
|
||||||
|
func scalarMult(dst, scalar, point *[32]byte) {
|
||||||
|
var e [32]byte
|
||||||
|
|
||||||
|
copy(e[:], scalar[:])
|
||||||
|
e[0] &= 248
|
||||||
|
e[31] &= 127
|
||||||
|
e[31] |= 64
|
||||||
|
|
||||||
|
var x1, x2, z2, x3, z3, tmp0, tmp1 field.Element
|
||||||
|
x1.SetBytes(point[:])
|
||||||
|
x2.One()
|
||||||
|
x3.Set(&x1)
|
||||||
|
z3.One()
|
||||||
|
|
||||||
|
swap := 0
|
||||||
|
for pos := 254; pos >= 0; pos-- {
|
||||||
|
b := e[pos/8] >> uint(pos&7)
|
||||||
|
b &= 1
|
||||||
|
swap ^= int(b)
|
||||||
|
x2.Swap(&x3, swap)
|
||||||
|
z2.Swap(&z3, swap)
|
||||||
|
swap = int(b)
|
||||||
|
|
||||||
|
tmp0.Subtract(&x3, &z3)
|
||||||
|
tmp1.Subtract(&x2, &z2)
|
||||||
|
x2.Add(&x2, &z2)
|
||||||
|
z2.Add(&x3, &z3)
|
||||||
|
z3.Multiply(&tmp0, &x2)
|
||||||
|
z2.Multiply(&z2, &tmp1)
|
||||||
|
tmp0.Square(&tmp1)
|
||||||
|
tmp1.Square(&x2)
|
||||||
|
x3.Add(&z3, &z2)
|
||||||
|
z2.Subtract(&z3, &z2)
|
||||||
|
x2.Multiply(&tmp1, &tmp0)
|
||||||
|
tmp1.Subtract(&tmp1, &tmp0)
|
||||||
|
z2.Square(&z2)
|
||||||
|
|
||||||
|
z3.Mult32(&tmp1, 121666)
|
||||||
|
x3.Square(&x3)
|
||||||
|
tmp0.Add(&tmp0, &z3)
|
||||||
|
z3.Multiply(&x1, &z2)
|
||||||
|
z2.Multiply(&tmp1, &tmp0)
|
||||||
|
}
|
||||||
|
|
||||||
|
x2.Swap(&x3, swap)
|
||||||
|
z2.Swap(&z3, swap)
|
||||||
|
|
||||||
|
z2.Invert(&z2)
|
||||||
|
x2.Multiply(&x2, &z2)
|
||||||
|
copy(dst[:], x2.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func scalarBaseMult(dst, scalar *[32]byte) {
|
||||||
|
checkBasepoint()
|
||||||
|
scalarMult(dst, scalar, &basePoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
func x25519(dst *[32]byte, scalar, point []byte) ([]byte, error) {
|
||||||
|
var in [32]byte
|
||||||
|
if l := len(scalar); l != 32 {
|
||||||
|
return nil, errors.New("bad scalar length: " + strconv.Itoa(l) + ", expected 32")
|
||||||
|
}
|
||||||
|
if l := len(point); l != 32 {
|
||||||
|
return nil, errors.New("bad point length: " + strconv.Itoa(l) + ", expected 32")
|
||||||
|
}
|
||||||
|
copy(in[:], scalar)
|
||||||
|
if &point[0] == &Basepoint[0] {
|
||||||
|
scalarBaseMult(dst, &in)
|
||||||
|
} else {
|
||||||
|
var base, zero [32]byte
|
||||||
|
copy(base[:], point)
|
||||||
|
scalarMult(dst, &in, &base)
|
||||||
|
if subtle.ConstantTimeCompare(dst[:], zero[:]) == 1 {
|
||||||
|
return nil, errors.New("bad input point: low order point")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dst[:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkBasepoint() {
|
||||||
|
if subtle.ConstantTimeCompare(Basepoint, []byte{
|
||||||
|
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
}) != 1 {
|
||||||
|
panic("curve25519: global Basepoint value was modified")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
// 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 go1.20
|
||||||
|
|
||||||
|
package curve25519
|
||||||
|
|
||||||
|
import "crypto/ecdh"
|
||||||
|
|
||||||
|
func x25519(dst *[32]byte, scalar, point []byte) ([]byte, error) {
|
||||||
|
curve := ecdh.X25519()
|
||||||
|
pub, err := curve.NewPublicKey(point)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
priv, err := curve.NewPrivateKey(scalar)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out, err := priv.ECDH(pub)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
copy(dst[:], out)
|
||||||
|
return dst[:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func scalarMult(dst, scalar, point *[32]byte) {
|
||||||
|
if _, err := x25519(dst, scalar[:], point[:]); err != nil {
|
||||||
|
// The only error condition for x25519 when the inputs are 32 bytes long
|
||||||
|
// is if the output would have been the all-zero value.
|
||||||
|
for i := range dst {
|
||||||
|
dst[i] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
copy(dst[:], priv.PublicKey().Bytes())
|
||||||
|
}
|
||||||
@@ -2,13 +2,15 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package curve25519
|
package curve25519_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/curve25519"
|
||||||
)
|
)
|
||||||
|
|
||||||
const expectedHex = "89161fde887b2b53de549af483940106ecc114d6982daa98256de23bdf77661a"
|
const expectedHex = "89161fde887b2b53de549af483940106ecc114d6982daa98256de23bdf77661a"
|
||||||
@@ -19,7 +21,7 @@ func TestX25519Basepoint(t *testing.T) {
|
|||||||
|
|
||||||
for i := 0; i < 200; i++ {
|
for i := 0; i < 200; i++ {
|
||||||
var err error
|
var err error
|
||||||
x, err = X25519(x, Basepoint)
|
x, err = curve25519.X25519(x, curve25519.Basepoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -32,12 +34,12 @@ func TestX25519Basepoint(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLowOrderPoints(t *testing.T) {
|
func TestLowOrderPoints(t *testing.T) {
|
||||||
scalar := make([]byte, ScalarSize)
|
scalar := make([]byte, curve25519.ScalarSize)
|
||||||
if _, err := rand.Read(scalar); err != nil {
|
if _, err := rand.Read(scalar); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
for i, p := range lowOrderPoints {
|
for i, p := range lowOrderPoints {
|
||||||
out, err := X25519(scalar, p)
|
out, err := curve25519.X25519(scalar, p)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("%d: expected error, got nil", i)
|
t.Errorf("%d: expected error, got nil", i)
|
||||||
}
|
}
|
||||||
@@ -48,10 +50,10 @@ func TestLowOrderPoints(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTestVectors(t *testing.T) {
|
func TestTestVectors(t *testing.T) {
|
||||||
t.Run("Legacy", func(t *testing.T) { testTestVectors(t, ScalarMult) })
|
t.Run("Legacy", func(t *testing.T) { testTestVectors(t, curve25519.ScalarMult) })
|
||||||
t.Run("X25519", func(t *testing.T) {
|
t.Run("X25519", func(t *testing.T) {
|
||||||
testTestVectors(t, func(dst, scalar, point *[32]byte) {
|
testTestVectors(t, func(dst, scalar, point *[32]byte) {
|
||||||
out, err := X25519(scalar[:], point[:])
|
out, err := curve25519.X25519(scalar[:], point[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -88,10 +90,10 @@ func TestHighBitIgnored(t *testing.T) {
|
|||||||
var hi0, hi1 [32]byte
|
var hi0, hi1 [32]byte
|
||||||
|
|
||||||
u[31] &= 0x7f
|
u[31] &= 0x7f
|
||||||
ScalarMult(&hi0, &s, &u)
|
curve25519.ScalarMult(&hi0, &s, &u)
|
||||||
|
|
||||||
u[31] |= 0x80
|
u[31] |= 0x80
|
||||||
ScalarMult(&hi1, &s, &u)
|
curve25519.ScalarMult(&hi1, &s, &u)
|
||||||
|
|
||||||
if !bytes.Equal(hi0[:], hi1[:]) {
|
if !bytes.Equal(hi0[:], hi1[:]) {
|
||||||
t.Errorf("high bit of group point should not affect result")
|
t.Errorf("high bit of group point should not affect result")
|
||||||
@@ -101,14 +103,14 @@ func TestHighBitIgnored(t *testing.T) {
|
|||||||
var benchmarkSink byte
|
var benchmarkSink byte
|
||||||
|
|
||||||
func BenchmarkX25519Basepoint(b *testing.B) {
|
func BenchmarkX25519Basepoint(b *testing.B) {
|
||||||
scalar := make([]byte, ScalarSize)
|
scalar := make([]byte, curve25519.ScalarSize)
|
||||||
if _, err := rand.Read(scalar); err != nil {
|
if _, err := rand.Read(scalar); err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
out, err := X25519(scalar, Basepoint)
|
out, err := curve25519.X25519(scalar, curve25519.Basepoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -117,11 +119,11 @@ func BenchmarkX25519Basepoint(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkX25519(b *testing.B) {
|
func BenchmarkX25519(b *testing.B) {
|
||||||
scalar := make([]byte, ScalarSize)
|
scalar := make([]byte, curve25519.ScalarSize)
|
||||||
if _, err := rand.Read(scalar); err != nil {
|
if _, err := rand.Read(scalar); err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
point, err := X25519(scalar, Basepoint)
|
point, err := curve25519.X25519(scalar, curve25519.Basepoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -131,7 +133,7 @@ func BenchmarkX25519(b *testing.B) {
|
|||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
out, err := X25519(scalar, point)
|
out, err := curve25519.X25519(scalar, point)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -245,7 +245,7 @@ func feSquareGeneric(v, a *Element) {
|
|||||||
v.carryPropagate()
|
v.carryPropagate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// carryPropagate brings the limbs below 52 bits by applying the reduction
|
// carryPropagateGeneric brings the limbs below 52 bits by applying the reduction
|
||||||
// identity (a * 2²⁵⁵ + b = a * 19 + b) to the l4 carry. TODO inline
|
// identity (a * 2²⁵⁵ + b = a * 19 + b) to the l4 carry. TODO inline
|
||||||
func (v *Element) carryPropagateGeneric() *Element {
|
func (v *Element) carryPropagateGeneric() *Element {
|
||||||
c0 := v.l0 >> 51
|
c0 := v.l0 >> 51
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package curve25519
|
package curve25519_test
|
||||||
|
|
||||||
// lowOrderPoints from libsodium.
|
// lowOrderPoints from libsodium.
|
||||||
// https://github.com/jedisct1/libsodium/blob/65621a1059a37d/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.c#L11-L70
|
// https://github.com/jedisct1/libsodium/blob/65621a1059a37d/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.c#L11-L70
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ module golang.org/x/crypto
|
|||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require (
|
require (
|
||||||
golang.org/x/net v0.2.0
|
golang.org/x/net v0.9.0
|
||||||
golang.org/x/sys v0.2.0
|
golang.org/x/sys v0.7.0
|
||||||
golang.org/x/term v0.2.0
|
golang.org/x/term v0.7.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require golang.org/x/text v0.4.0 // indirect
|
require golang.org/x/text v0.9.0 // indirect
|
||||||
|
|||||||
@@ -2,30 +2,37 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
|
|||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
|
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
|
||||||
|
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
|
||||||
|
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM=
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
|
||||||
|
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
||||||
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
|||||||
@@ -6,7 +6,11 @@ package wycheproof
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/cryptobyte"
|
||||||
|
"golang.org/x/crypto/cryptobyte/asn1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestECDSA(t *testing.T) {
|
func TestECDSA(t *testing.T) {
|
||||||
@@ -76,9 +80,25 @@ func TestECDSA(t *testing.T) {
|
|||||||
h.Reset()
|
h.Reset()
|
||||||
h.Write(decodeHex(sig.Msg))
|
h.Write(decodeHex(sig.Msg))
|
||||||
hashed := h.Sum(nil)
|
hashed := h.Sum(nil)
|
||||||
got := ecdsa.VerifyASN1(pub, hashed, decodeHex(sig.Sig))
|
sigBytes := decodeHex(sig.Sig)
|
||||||
|
got := ecdsa.VerifyASN1(pub, hashed, sigBytes)
|
||||||
if want := shouldPass(sig.Result, sig.Flags, flagsShouldPass); got != want {
|
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)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -61,7 +61,7 @@ type Key struct {
|
|||||||
type KeyRing interface {
|
type KeyRing interface {
|
||||||
// KeysById returns the set of keys that have the given key id.
|
// KeysById returns the set of keys that have the given key id.
|
||||||
KeysById(id uint64) []Key
|
KeysById(id uint64) []Key
|
||||||
// KeysByIdAndUsage returns the set of keys with the given id
|
// KeysByIdUsage returns the set of keys with the given id
|
||||||
// that also meet the key usage given by requiredUsage.
|
// that also meet the key usage given by requiredUsage.
|
||||||
// The requiredUsage is expressed as the bitwise-OR of
|
// The requiredUsage is expressed as the bitwise-OR of
|
||||||
// packet.KeyFlag* values.
|
// packet.KeyFlag* values.
|
||||||
@@ -183,7 +183,7 @@ func (el EntityList) KeysById(id uint64) (keys []Key) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeysByIdAndUsage returns the set of keys with the given id that also meet
|
// KeysByIdUsage returns the set of keys with the given id that also meet
|
||||||
// the key usage given by requiredUsage. The requiredUsage is expressed as
|
// the key usage given by requiredUsage. The requiredUsage is expressed as
|
||||||
// the bitwise-OR of packet.KeyFlag* values.
|
// the bitwise-OR of packet.KeyFlag* values.
|
||||||
func (el EntityList) KeysByIdUsage(id uint64, requiredUsage byte) (keys []Key) {
|
func (el EntityList) KeysByIdUsage(id uint64, requiredUsage byte) (keys []Key) {
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func (c *Compressed) parse(r io.Reader) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// compressedWriterCloser represents the serialized compression stream
|
// compressedWriteCloser represents the serialized compression stream
|
||||||
// header and the compressor. Its Close() method ensures that both the
|
// header and the compressor. Its Close() method ensures that both the
|
||||||
// compressor and serialized stream header are closed. Its Write()
|
// compressor and serialized stream header are closed. Its Write()
|
||||||
// method writes to the compressor.
|
// method writes to the compressor.
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@ import (
|
|||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server wraps an Agent and uses it to implement the agent side of
|
// server wraps an Agent and uses it to implement the agent side of
|
||||||
// the SSH-agent, wire protocol.
|
// the SSH-agent, wire protocol.
|
||||||
type server struct {
|
type server struct {
|
||||||
agent Agent
|
agent Agent
|
||||||
|
|||||||
+2
-1
@@ -114,7 +114,8 @@ var cipherModes = map[string]*cipherMode{
|
|||||||
"arcfour": {16, 0, streamCipherMode(0, newRC4)},
|
"arcfour": {16, 0, streamCipherMode(0, newRC4)},
|
||||||
|
|
||||||
// AEAD ciphers
|
// AEAD ciphers
|
||||||
gcmCipherID: {16, 12, newGCMCipher},
|
gcm128CipherID: {16, 12, newGCMCipher},
|
||||||
|
gcm256CipherID: {32, 12, newGCMCipher},
|
||||||
chacha20Poly1305ID: {64, 0, newChaCha20Cipher},
|
chacha20Poly1305ID: {64, 0, newChaCha20Cipher},
|
||||||
|
|
||||||
// CBC mode is insecure and so is not included in the default config.
|
// CBC mode is insecure and so is not included in the default config.
|
||||||
|
|||||||
+1
-1
@@ -141,7 +141,7 @@ func TestCVE202143565(t *testing.T) {
|
|||||||
constructPacket func(packetCipher) io.Reader
|
constructPacket func(packetCipher) io.Reader
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
cipher: gcmCipherID,
|
cipher: gcm128CipherID,
|
||||||
constructPacket: func(client packetCipher) io.Reader {
|
constructPacket: func(client packetCipher) io.Reader {
|
||||||
internalCipher := client.(*gcmCipher)
|
internalCipher := client.(*gcmCipher)
|
||||||
b := &bytes.Buffer{}
|
b := &bytes.Buffer{}
|
||||||
|
|||||||
+5
-4
@@ -28,7 +28,7 @@ const (
|
|||||||
// supportedCiphers lists ciphers we support but might not recommend.
|
// supportedCiphers lists ciphers we support but might not recommend.
|
||||||
var supportedCiphers = []string{
|
var supportedCiphers = []string{
|
||||||
"aes128-ctr", "aes192-ctr", "aes256-ctr",
|
"aes128-ctr", "aes192-ctr", "aes256-ctr",
|
||||||
"aes128-gcm@openssh.com",
|
"aes128-gcm@openssh.com", gcm256CipherID,
|
||||||
chacha20Poly1305ID,
|
chacha20Poly1305ID,
|
||||||
"arcfour256", "arcfour128", "arcfour",
|
"arcfour256", "arcfour128", "arcfour",
|
||||||
aes128cbcID,
|
aes128cbcID,
|
||||||
@@ -37,7 +37,7 @@ var supportedCiphers = []string{
|
|||||||
|
|
||||||
// preferredCiphers specifies the default preference for ciphers.
|
// preferredCiphers specifies the default preference for ciphers.
|
||||||
var preferredCiphers = []string{
|
var preferredCiphers = []string{
|
||||||
"aes128-gcm@openssh.com",
|
"aes128-gcm@openssh.com", gcm256CipherID,
|
||||||
chacha20Poly1305ID,
|
chacha20Poly1305ID,
|
||||||
"aes128-ctr", "aes192-ctr", "aes256-ctr",
|
"aes128-ctr", "aes192-ctr", "aes256-ctr",
|
||||||
}
|
}
|
||||||
@@ -168,7 +168,7 @@ func (a *directionAlgorithms) rekeyBytes() int64 {
|
|||||||
// 2^(BLOCKSIZE/4) blocks. For all AES flavors BLOCKSIZE is
|
// 2^(BLOCKSIZE/4) blocks. For all AES flavors BLOCKSIZE is
|
||||||
// 128.
|
// 128.
|
||||||
switch a.Cipher {
|
switch a.Cipher {
|
||||||
case "aes128-ctr", "aes192-ctr", "aes256-ctr", gcmCipherID, aes128cbcID:
|
case "aes128-ctr", "aes192-ctr", "aes256-ctr", gcm128CipherID, gcm256CipherID, aes128cbcID:
|
||||||
return 16 * (1 << 32)
|
return 16 * (1 << 32)
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -178,7 +178,8 @@ func (a *directionAlgorithms) rekeyBytes() int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var aeadCiphers = map[string]bool{
|
var aeadCiphers = map[string]bool{
|
||||||
gcmCipherID: true,
|
gcm128CipherID: true,
|
||||||
|
gcm256CipherID: true,
|
||||||
chacha20Poly1305ID: true,
|
chacha20Poly1305ID: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -97,7 +97,7 @@ func (c *connection) Close() error {
|
|||||||
return c.sshConn.conn.Close()
|
return c.sshConn.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// sshconn provides net.Conn metadata, but disallows direct reads and
|
// sshConn provides net.Conn metadata, but disallows direct reads and
|
||||||
// writes.
|
// writes.
|
||||||
type sshConn struct {
|
type sshConn struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
|
|||||||
+31
-19
@@ -58,11 +58,13 @@ type handshakeTransport struct {
|
|||||||
incoming chan []byte
|
incoming chan []byte
|
||||||
readError error
|
readError error
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
writeError error
|
writeError error
|
||||||
sentInitPacket []byte
|
sentInitPacket []byte
|
||||||
sentInitMsg *kexInitMsg
|
sentInitMsg *kexInitMsg
|
||||||
pendingPackets [][]byte // Used when a key exchange is in progress.
|
pendingPackets [][]byte // Used when a key exchange is in progress.
|
||||||
|
writePacketsLeft uint32
|
||||||
|
writeBytesLeft int64
|
||||||
|
|
||||||
// If the read loop wants to schedule a kex, it pings this
|
// If the read loop wants to schedule a kex, it pings this
|
||||||
// channel, and the write loop will send out a kex
|
// channel, and the write loop will send out a kex
|
||||||
@@ -71,7 +73,8 @@ type handshakeTransport struct {
|
|||||||
|
|
||||||
// If the other side requests or confirms a kex, its kexInit
|
// If the other side requests or confirms a kex, its kexInit
|
||||||
// packet is sent here for the write loop to find it.
|
// packet is sent here for the write loop to find it.
|
||||||
startKex chan *pendingKex
|
startKex chan *pendingKex
|
||||||
|
kexLoopDone chan struct{} // closed (with writeError non-nil) when kexLoop exits
|
||||||
|
|
||||||
// data for host key checking
|
// data for host key checking
|
||||||
hostKeyCallback HostKeyCallback
|
hostKeyCallback HostKeyCallback
|
||||||
@@ -86,12 +89,10 @@ type handshakeTransport struct {
|
|||||||
// Algorithms agreed in the last key exchange.
|
// Algorithms agreed in the last key exchange.
|
||||||
algorithms *algorithms
|
algorithms *algorithms
|
||||||
|
|
||||||
|
// Counters exclusively owned by readLoop.
|
||||||
readPacketsLeft uint32
|
readPacketsLeft uint32
|
||||||
readBytesLeft int64
|
readBytesLeft int64
|
||||||
|
|
||||||
writePacketsLeft uint32
|
|
||||||
writeBytesLeft int64
|
|
||||||
|
|
||||||
// The session ID or nil if first kex did not complete yet.
|
// The session ID or nil if first kex did not complete yet.
|
||||||
sessionID []byte
|
sessionID []byte
|
||||||
}
|
}
|
||||||
@@ -108,7 +109,8 @@ func newHandshakeTransport(conn keyingTransport, config *Config, clientVersion,
|
|||||||
clientVersion: clientVersion,
|
clientVersion: clientVersion,
|
||||||
incoming: make(chan []byte, chanSize),
|
incoming: make(chan []byte, chanSize),
|
||||||
requestKex: make(chan struct{}, 1),
|
requestKex: make(chan struct{}, 1),
|
||||||
startKex: make(chan *pendingKex, 1),
|
startKex: make(chan *pendingKex),
|
||||||
|
kexLoopDone: make(chan struct{}),
|
||||||
|
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
@@ -340,16 +342,17 @@ write:
|
|||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// drain startKex channel. We don't service t.requestKex
|
|
||||||
// because nobody does blocking sends there.
|
|
||||||
go func() {
|
|
||||||
for init := range t.startKex {
|
|
||||||
init.done <- t.writeError
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Unblock reader.
|
// Unblock reader.
|
||||||
t.conn.Close()
|
t.conn.Close()
|
||||||
|
|
||||||
|
// drain startKex channel. We don't service t.requestKex
|
||||||
|
// because nobody does blocking sends there.
|
||||||
|
for request := range t.startKex {
|
||||||
|
request.done <- t.getWriteError()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark that the loop is done so that Close can return.
|
||||||
|
close(t.kexLoopDone)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The protocol uses uint32 for packet counters, so we can't let them
|
// The protocol uses uint32 for packet counters, so we can't let them
|
||||||
@@ -545,7 +548,16 @@ func (t *handshakeTransport) writePacket(p []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *handshakeTransport) Close() error {
|
func (t *handshakeTransport) Close() error {
|
||||||
return t.conn.Close()
|
// Close the connection. This should cause the readLoop goroutine to wake up
|
||||||
|
// and close t.startKex, which will shut down kexLoop if running.
|
||||||
|
err := t.conn.Close()
|
||||||
|
|
||||||
|
// Wait for the kexLoop goroutine to complete.
|
||||||
|
// At that point we know that the readLoop goroutine is complete too,
|
||||||
|
// because kexLoop itself waits for readLoop to close the startKex channel.
|
||||||
|
<-t.kexLoopDone
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *handshakeTransport) enterKeyExchange(otherInitPacket []byte) error {
|
func (t *handshakeTransport) enterKeyExchange(otherInitPacket []byte) error {
|
||||||
|
|||||||
@@ -562,7 +562,7 @@ func TestHandshakeRekeyDefault(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshakeAEADCipherNoMAC(t *testing.T) {
|
func TestHandshakeAEADCipherNoMAC(t *testing.T) {
|
||||||
for _, cipher := range []string{chacha20Poly1305ID, gcmCipherID} {
|
for _, cipher := range []string{chacha20Poly1305ID, gcm128CipherID} {
|
||||||
checker := &syncChecker{
|
checker := &syncChecker{
|
||||||
called: make(chan int, 1),
|
called: make(chan int, 1),
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -1087,9 +1087,9 @@ func (*PassphraseMissingError) Error() string {
|
|||||||
return "ssh: this private key is passphrase protected"
|
return "ssh: this private key is passphrase protected"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseRawPrivateKey returns a private key from a PEM encoded private key. It
|
// ParseRawPrivateKey returns a private key from a PEM encoded private key. It supports
|
||||||
// supports RSA (PKCS#1), PKCS#8, DSA (OpenSSL), and ECDSA private keys. If the
|
// RSA, DSA, ECDSA, and Ed25519 private keys in PKCS#1, PKCS#8, OpenSSL, and OpenSSH
|
||||||
// private key is encrypted, it will return a PassphraseMissingError.
|
// formats. If the private key is encrypted, it will return a PassphraseMissingError.
|
||||||
func ParseRawPrivateKey(pemBytes []byte) (interface{}, error) {
|
func ParseRawPrivateKey(pemBytes []byte) (interface{}, error) {
|
||||||
block, _ := pem.Decode(pemBytes)
|
block, _ := pem.Decode(pemBytes)
|
||||||
if block == nil {
|
if block == nil {
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ func keyEq(a, b ssh.PublicKey) bool {
|
|||||||
return bytes.Equal(a.Marshal(), b.Marshal())
|
return bytes.Equal(a.Marshal(), b.Marshal())
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAuthorityForHost can be used as a callback in ssh.CertChecker
|
// IsHostAuthority can be used as a callback in ssh.CertChecker
|
||||||
func (db *hostKeyDB) IsHostAuthority(remote ssh.PublicKey, address string) bool {
|
func (db *hostKeyDB) IsHostAuthority(remote ssh.PublicKey, address string) bool {
|
||||||
h, p, err := net.SplitHostPort(address)
|
h, p, err := net.SplitHostPort(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+2
-1
@@ -17,7 +17,8 @@ import (
|
|||||||
const debugTransport = false
|
const debugTransport = false
|
||||||
|
|
||||||
const (
|
const (
|
||||||
gcmCipherID = "aes128-gcm@openssh.com"
|
gcm128CipherID = "aes128-gcm@openssh.com"
|
||||||
|
gcm256CipherID = "aes256-gcm@openssh.com"
|
||||||
aes128cbcID = "aes128-cbc"
|
aes128cbcID = "aes128-cbc"
|
||||||
tripledescbcID = "3des-cbc"
|
tripledescbcID = "3des-cbc"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user