cryptobyte: handle AddASN1BigInt with -1
Fixes golang/go#38510 Change-Id: Ie52db22bf85a5f79046fad95e5bbe2788c3bec92 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/229457 Reviewed-by: Filippo Valsorda <filippo@golang.org>
This commit is contained in:
+1
-1
@@ -81,7 +81,7 @@ func (b *Builder) AddASN1BigInt(n *big.Int) {
|
||||
for i := range bytes {
|
||||
bytes[i] ^= 0xff
|
||||
}
|
||||
if bytes[0]&0x80 == 0 {
|
||||
if len(bytes) == 0 || bytes[0]&0x80 == 0 {
|
||||
c.add(0xff)
|
||||
}
|
||||
c.add(bytes...)
|
||||
|
||||
@@ -335,3 +335,19 @@ func TestReadASN1BitString(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddASN1BigInt(t *testing.T) {
|
||||
x := big.NewInt(-1)
|
||||
var b Builder
|
||||
b.AddASN1BigInt(x)
|
||||
got, err := b.Bytes()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error adding -1: %v", err)
|
||||
}
|
||||
s := String(got)
|
||||
var y big.Int
|
||||
ok := s.ReadASN1Integer(&y)
|
||||
if !ok || x.Cmp(&y) != 0 {
|
||||
t.Errorf("unexpected bytes %v, want %v", &y, x)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user