From d864dd5cadc2e0f3599f4ad3b724b854af9f33e9 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Tue, 1 Aug 2023 11:28:14 +0200 Subject: [PATCH] rewrite interface{} to any --- bench_test.go | 180 ++++++++++++++++++++++++------------------------ fuzz_test.go | 2 +- headers.go | 38 +++++----- headers_test.go | 18 ++--- key.go | 42 +++++------ key_test.go | 150 ++++++++++++++++++++-------------------- sign.go | 2 +- sign1.go | 2 +- 8 files changed, 217 insertions(+), 217 deletions(-) diff --git a/bench_test.go b/bench_test.go index 9a95f96..a60d1aa 100644 --- a/bench_test.go +++ b/bench_test.go @@ -1,90 +1,90 @@ -package cose_test - -import ( - "io" - "testing" - - "github.com/veraison/go-cose" -) - -func newSign1Message() *cose.Sign1Message { - return &cose.Sign1Message{ - Headers: cose.Headers{ - Protected: cose.ProtectedHeader{ - cose.HeaderLabelAlgorithm: cose.AlgorithmES256, - }, - Unprotected: cose.UnprotectedHeader{ - cose.HeaderLabelKeyID: []byte{0x01}, - }, - }, - Payload: make([]byte, 100), - Signature: make([]byte, 32), - } -} - -type noSigner struct{} - -func (noSigner) Algorithm() cose.Algorithm { - return cose.AlgorithmES256 -} - -func (noSigner) Sign(_ io.Reader, digest []byte) ([]byte, error) { - return digest, nil -} - -func (noSigner) Verify(_, _ []byte) error { - return nil -} - -func BenchmarkSign1Message_MarshalCBOR(b *testing.B) { - msg := newSign1Message() - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - _, err := msg.MarshalCBOR() - if err != nil { - b.Fatal(err) - } - } -} - -func BenchmarkSign1Message_UnmarshalCBOR(b *testing.B) { - data, err := newSign1Message().MarshalCBOR() - if err != nil { - b.Fatal(err) - } - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - var m cose.Sign1Message - err = m.UnmarshalCBOR(data) - if err != nil { - b.Fatal(err) - } - } -} - -func BenchmarkSign1Message_Sign(b *testing.B) { - msg := newSign1Message() - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - msg.Signature = nil - err := msg.Sign(zeroSource{}, nil, noSigner{}) - if err != nil { - b.Fatal(err) - } - } -} - -func BenchmarkSign1Message_Verify(b *testing.B) { - msg := newSign1Message() - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - err := msg.Verify(nil, noSigner{}) - if err != nil { - b.Fatal(err) - } - } -} +package cose_test + +import ( + "io" + "testing" + + "github.com/veraison/go-cose" +) + +func newSign1Message() *cose.Sign1Message { + return &cose.Sign1Message{ + Headers: cose.Headers{ + Protected: cose.ProtectedHeader{ + cose.HeaderLabelAlgorithm: cose.AlgorithmES256, + }, + Unprotected: cose.UnprotectedHeader{ + cose.HeaderLabelKeyID: []byte{0x01}, + }, + }, + Payload: make([]byte, 100), + Signature: make([]byte, 32), + } +} + +type noSigner struct{} + +func (noSigner) Algorithm() cose.Algorithm { + return cose.AlgorithmES256 +} + +func (noSigner) Sign(_ io.Reader, digest []byte) ([]byte, error) { + return digest, nil +} + +func (noSigner) Verify(_, _ []byte) error { + return nil +} + +func BenchmarkSign1Message_MarshalCBOR(b *testing.B) { + msg := newSign1Message() + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := msg.MarshalCBOR() + if err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkSign1Message_UnmarshalCBOR(b *testing.B) { + data, err := newSign1Message().MarshalCBOR() + if err != nil { + b.Fatal(err) + } + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + var m cose.Sign1Message + err = m.UnmarshalCBOR(data) + if err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkSign1Message_Sign(b *testing.B) { + msg := newSign1Message() + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + msg.Signature = nil + err := msg.Sign(zeroSource{}, nil, noSigner{}) + if err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkSign1Message_Verify(b *testing.B) { + msg := newSign1Message() + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + err := msg.Verify(nil, noSigner{}) + if err != nil { + b.Fatal(err) + } + } +} diff --git a/fuzz_test.go b/fuzz_test.go index 4e697a1..aa5db19 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -58,7 +58,7 @@ func FuzzSign1Message_UnmarshalCBOR(f *testing.F) { enc, _ := cbor.CanonicalEncOptions().EncMode() dec, _ := cbor.DecOptions{IntDec: cbor.IntDecConvertSigned}.DecMode() isCanonical := func(b []byte) bool { - var tmp interface{} + var tmp any err := dec.Unmarshal(b, &tmp) if err != nil { return false diff --git a/headers.go b/headers.go index 88ab2d6..ceacefa 100644 --- a/headers.go +++ b/headers.go @@ -28,7 +28,7 @@ const ( // ProtectedHeader contains parameters that are to be cryptographically // protected. -type ProtectedHeader map[interface{}]interface{} +type ProtectedHeader map[any]any // MarshalCBOR encodes the protected header into a CBOR bstr object. // A zero-length header is encoded as a zero-length string rather than as a @@ -42,7 +42,7 @@ func (h ProtectedHeader) MarshalCBOR() ([]byte, error) { if err != nil { return nil, fmt.Errorf("protected header: %w", err) } - encoded, err = encMode.Marshal(map[interface{}]interface{}(h)) + encoded, err = encMode.Marshal(map[any]any(h)) if err != nil { return nil, err } @@ -75,7 +75,7 @@ func (h *ProtectedHeader) UnmarshalCBOR(data []byte) error { if err := validateHeaderLabelCBOR(encoded); err != nil { return err } - var header map[interface{}]interface{} + var header map[any]any if err := decMode.Unmarshal(encoded, &header); err != nil { return err } @@ -129,7 +129,7 @@ func (h ProtectedHeader) Algorithm() (Algorithm, error) { // processing a message is required to understand. // // Reference: https://datatracker.ietf.org/doc/html/rfc8152#section-3.1 -func (h ProtectedHeader) Critical() ([]interface{}, error) { +func (h ProtectedHeader) Critical() ([]any, error) { value, ok := h[HeaderLabelCritical] if !ok { return nil, nil @@ -138,12 +138,12 @@ func (h ProtectedHeader) Critical() ([]interface{}, error) { if err != nil { return nil, err } - return value.([]interface{}), nil + return value.([]any), nil } // ensureCritical ensures all critical headers are present in the protected bucket. -func ensureCritical(value interface{}, headers map[interface{}]interface{}) error { - labels, ok := value.([]interface{}) +func ensureCritical(value any, headers map[any]any) error { + labels, ok := value.([]any) if !ok { return errors.New("invalid crit header") } @@ -164,7 +164,7 @@ func ensureCritical(value interface{}, headers map[interface{}]interface{}) erro // UnprotectedHeader contains parameters that are not cryptographically // protected. -type UnprotectedHeader map[interface{}]interface{} +type UnprotectedHeader map[any]any // MarshalCBOR encodes the unprotected header into a CBOR map object. // A zero-length header is encoded as a zero-length map (encoded as h'a0'). @@ -175,7 +175,7 @@ func (h UnprotectedHeader) MarshalCBOR() ([]byte, error) { if err := validateHeaderParameters(h, false); err != nil { return nil, fmt.Errorf("unprotected header: %w", err) } - return encMode.Marshal(map[interface{}]interface{}(h)) + return encMode.Marshal(map[any]any(h)) } // UnmarshalCBOR decodes a CBOR map object into UnprotectedHeader. @@ -197,7 +197,7 @@ func (h *UnprotectedHeader) UnmarshalCBOR(data []byte) error { if err := validateHeaderLabelCBOR(data); err != nil { return err } - var header map[interface{}]interface{} + var header map[any]any if err := decMode.Unmarshal(data, &header); err != nil { return err } @@ -376,14 +376,14 @@ func (h *Headers) ensureIV() error { } // hasLabel returns true if h contains label. -func hasLabel(h map[interface{}]interface{}, label interface{}) bool { +func hasLabel(h map[any]any, label any) bool { _, ok := h[label] return ok } // validateHeaderParameters validates all headers conform to the spec. -func validateHeaderParameters(h map[interface{}]interface{}, protected bool) error { - existing := make(map[interface{}]struct{}, len(h)) +func validateHeaderParameters(h map[any]any, protected bool) error { + existing := make(map[any]struct{}, len(h)) for label, value := range h { // Validate that all header labels are integers or strings. // Reference: https://datatracker.ietf.org/doc/html/rfc8152#section-1.4 @@ -443,7 +443,7 @@ func validateHeaderParameters(h map[interface{}]interface{}, protected bool) err } // canUint reports whether v can be used as a CBOR uint type. -func canUint(v interface{}) bool { +func canUint(v any) bool { switch v := v.(type) { case uint, uint8, uint16, uint32, uint64: return true @@ -462,7 +462,7 @@ func canUint(v interface{}) bool { } // canInt reports whether v can be used as a CBOR int type. -func canInt(v interface{}) bool { +func canInt(v any) bool { switch v.(type) { case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: @@ -472,20 +472,20 @@ func canInt(v interface{}) bool { } // canTstr reports whether v can be used as a CBOR tstr type. -func canTstr(v interface{}) bool { +func canTstr(v any) bool { _, ok := v.(string) return ok } // canBstr reports whether v can be used as a CBOR bstr type. -func canBstr(v interface{}) bool { +func canBstr(v any) bool { _, ok := v.([]byte) return ok } // normalizeLabel tries to cast label into a int64 or a string. // Returns (nil, false) if the label type is not valid. -func normalizeLabel(label interface{}) (interface{}, bool) { +func normalizeLabel(label any) (any, bool) { switch v := label.(type) { case int: label = int64(v) @@ -517,7 +517,7 @@ func normalizeLabel(label interface{}) (interface{}, bool) { // headerLabelValidator is used to validate the header label of a COSE header. type headerLabelValidator struct { - value interface{} + value any } // String prints the value without brackets `{}`. Useful in error printing. diff --git a/headers_test.go b/headers_test.go index 11d4e72..004390b 100644 --- a/headers_test.go +++ b/headers_test.go @@ -17,7 +17,7 @@ func TestProtectedHeader_MarshalCBOR(t *testing.T) { name: "valid header", h: ProtectedHeader{ HeaderLabelAlgorithm: AlgorithmES256, - HeaderLabelCritical: []interface{}{ + HeaderLabelCritical: []any{ HeaderLabelContentType, "foo", }, @@ -84,7 +84,7 @@ func TestProtectedHeader_MarshalCBOR(t *testing.T) { { name: "empty critical", h: ProtectedHeader{ - HeaderLabelCritical: []interface{}{}, + HeaderLabelCritical: []any{}, }, wantErr: "protected header: header parameter: crit: empty crit header", }, @@ -98,7 +98,7 @@ func TestProtectedHeader_MarshalCBOR(t *testing.T) { { name: "missing header marked as critical", h: ProtectedHeader{ - HeaderLabelCritical: []interface{}{ + HeaderLabelCritical: []any{ HeaderLabelContentType, }, }, @@ -107,7 +107,7 @@ func TestProtectedHeader_MarshalCBOR(t *testing.T) { { name: "critical header contains non-label element", h: ProtectedHeader{ - HeaderLabelCritical: []interface{}{[]uint8{}}, + HeaderLabelCritical: []any{[]uint8{}}, }, wantErr: "protected header: header parameter: crit: require int / tstr type, got '[]uint8': []", }, @@ -199,7 +199,7 @@ func TestProtectedHeader_UnmarshalCBOR(t *testing.T) { }, want: ProtectedHeader{ HeaderLabelAlgorithm: AlgorithmES256, - HeaderLabelCritical: []interface{}{ + HeaderLabelCritical: []any{ HeaderLabelContentType, "foo", }, @@ -451,21 +451,21 @@ func TestProtectedHeader_Critical(t *testing.T) { tests := []struct { name string h ProtectedHeader - want []interface{} + want []any wantErr string }{ { name: "valid header", h: ProtectedHeader{ HeaderLabelAlgorithm: AlgorithmES256, - HeaderLabelCritical: []interface{}{ + HeaderLabelCritical: []any{ HeaderLabelContentType, "foo", }, HeaderLabelContentType: "text/plain", "foo": "bar", }, - want: []interface{}{ + want: []any{ HeaderLabelContentType, "foo", }, @@ -490,7 +490,7 @@ func TestProtectedHeader_Critical(t *testing.T) { { name: "empty critical", h: ProtectedHeader{ - HeaderLabelCritical: []interface{}{}, + HeaderLabelCritical: []any{}, }, wantErr: "empty crit header", }, diff --git a/key.go b/key.go index 7c042b4..71ad331 100644 --- a/key.go +++ b/key.go @@ -239,7 +239,7 @@ type Key struct { BaseIV []byte // Any additional parameter (label,value) pairs. - Params map[interface{}]interface{} + Params map[any]any } // NewKeyOKP returns a Key created using the provided Octet Key Pair data. @@ -251,7 +251,7 @@ func NewKeyOKP(alg Algorithm, x, d []byte) (*Key, error) { key := &Key{ Type: KeyTypeOKP, Algorithm: alg, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, }, } @@ -269,35 +269,35 @@ func NewKeyOKP(alg Algorithm, x, d []byte) (*Key, error) { // ParamBytes returns the value of the parameter with the given label, if it // exists and is of type []byte or can be converted to []byte. -func (k *Key) ParamBytes(label interface{}) ([]byte, bool) { +func (k *Key) ParamBytes(label any) ([]byte, bool) { v, ok, err := decodeBytes(k.Params, label) return v, ok && err == nil } // ParamInt returns the value of the parameter with the given label, if it // exists and is of type int64 or can be converted to int64. -func (k *Key) ParamInt(label interface{}) (int64, bool) { +func (k *Key) ParamInt(label any) (int64, bool) { v, ok, err := decodeInt(k.Params, label) return v, ok && err == nil } // ParamUint returns the value of the parameter with the given label, if it // exists and is of type uint64 or can be converted to uint64. -func (k *Key) ParamUint(label interface{}) (uint64, bool) { +func (k *Key) ParamUint(label any) (uint64, bool) { v, ok, err := decodeUint(k.Params, label) return v, ok && err == nil } // ParamString returns the value of the parameter with the given label, if it // exists and is of type string or can be converted to string. -func (k *Key) ParamString(label interface{}) (string, bool) { +func (k *Key) ParamString(label any) (string, bool) { v, ok, err := decodeString(k.Params, label) return v, ok && err == nil } // ParamBool returns the value of the parameter with the given label, if it // exists and is of type bool or can be converted to bool. -func (k *Key) ParamBool(label interface{}) (bool, bool) { +func (k *Key) ParamBool(label any) (bool, bool) { v, ok, err := decodeBool(k.Params, label) return v, ok && err == nil } @@ -332,7 +332,7 @@ func NewKeyEC2(alg Algorithm, x, y, d []byte) (*Key, error) { key := &Key{ Type: KeyTypeEC2, Algorithm: alg, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: curve, }, } @@ -368,7 +368,7 @@ func (k *Key) EC2() (crv Curve, x []byte, y, d []byte) { func NewKeySymmetric(k []byte) *Key { return &Key{ Type: KeyTypeSymmetric, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelSymmetricK: k, }, } @@ -532,7 +532,7 @@ func (k Key) canOp(op KeyOp) bool { // MarshalCBOR encodes Key into a COSE_Key object. func (k *Key) MarshalCBOR() ([]byte, error) { - tmp := map[interface{}]interface{}{ + tmp := map[any]any{ keyLabelKeyType: k.Type, } if k.ID != nil { @@ -547,7 +547,7 @@ func (k *Key) MarshalCBOR() ([]byte, error) { if k.BaseIV != nil { tmp[keyLabelBaseIV] = k.BaseIV } - existing := make(map[interface{}]struct{}, len(k.Params)) + existing := make(map[any]struct{}, len(k.Params)) for label, v := range k.Params { lbl, ok := normalizeLabel(label) if !ok { @@ -576,7 +576,7 @@ func (k *Key) MarshalCBOR() ([]byte, error) { // UnmarshalCBOR decodes a COSE_Key object into Key. func (k *Key) UnmarshalCBOR(data []byte) error { - var tmp map[interface{}]interface{} + var tmp map[any]any if err := decMode.Unmarshal(data, &tmp); err != nil { return err } @@ -634,7 +634,7 @@ func (k *Key) UnmarshalCBOR(data []byte) error { delete(tmp, keyLabelBaseIV) if len(tmp) > 0 { - k.Params = make(map[interface{}]interface{}, len(tmp)) + k.Params = make(map[any]any, len(tmp)) for lbl, v := range tmp { switch lbl := lbl.(type) { case int64: @@ -858,7 +858,7 @@ func curveSize(crv Curve) int { return (bitSize + 7) / 8 } -func decodeBytes(dic map[interface{}]interface{}, lbl interface{}) (b []byte, ok bool, err error) { +func decodeBytes(dic map[any]any, lbl any) (b []byte, ok bool, err error) { val, ok := dic[lbl] if !ok { return nil, false, nil @@ -874,7 +874,7 @@ func decodeBytes(dic map[interface{}]interface{}, lbl interface{}) (b []byte, ok return reflect.ValueOf(val).Bytes(), true, nil } -func decodeInt(dic map[interface{}]interface{}, lbl interface{}) (int64, bool, error) { +func decodeInt(dic map[any]any, lbl any) (int64, bool, error) { val, ok := dic[lbl] if !ok { return 0, false, nil @@ -888,7 +888,7 @@ func decodeInt(dic map[interface{}]interface{}, lbl interface{}) (int64, bool, e return 0, true, fmt.Errorf("invalid type: expected int64, got %T", val) } -func decodeUint(dic map[interface{}]interface{}, lbl interface{}) (uint64, bool, error) { +func decodeUint(dic map[any]any, lbl any) (uint64, bool, error) { val, ok := dic[lbl] if !ok { return 0, false, nil @@ -908,7 +908,7 @@ func decodeUint(dic map[interface{}]interface{}, lbl interface{}) (uint64, bool, return 0, true, fmt.Errorf("invalid type: expected uint64, got %T", val) } -func decodeString(dic map[interface{}]interface{}, lbl interface{}) (string, bool, error) { +func decodeString(dic map[any]any, lbl any) (string, bool, error) { val, ok := dic[lbl] if !ok { return "", false, nil @@ -922,7 +922,7 @@ func decodeString(dic map[interface{}]interface{}, lbl interface{}) (string, boo return "", true, fmt.Errorf("invalid type: expected uint64, got %T", val) } -func decodeBool(dic map[interface{}]interface{}, lbl interface{}) (bool, bool, error) { +func decodeBool(dic map[any]any, lbl any) (bool, bool, error) { val, ok := dic[lbl] if !ok { return false, false, nil @@ -936,14 +936,14 @@ func decodeBool(dic map[interface{}]interface{}, lbl interface{}) (bool, bool, e return false, true, fmt.Errorf("invalid type: expected uint64, got %T", val) } -func decodeSlice(dic map[interface{}]interface{}, lbl interface{}) ([]interface{}, error) { +func decodeSlice(dic map[any]any, lbl any) ([]any, error) { v, ok := dic[lbl] if !ok { return nil, nil } - arr, ok := v.([]interface{}) + arr, ok := v.([]any) if !ok { - return nil, fmt.Errorf("invalid type: expected []interface{}, got %T", v) + return nil, fmt.Errorf("invalid type: expected []any, got %T", v) } return arr, nil } diff --git a/key_test.go b/key_test.go index 0c1ab73..92691e5 100644 --- a/key_test.go +++ b/key_test.go @@ -15,7 +15,7 @@ import ( func TestKey_ParamBytes(t *testing.T) { key := &Key{ - Params: map[interface{}]interface{}{ + Params: map[any]any{ int64(-1): []byte{1}, 2: []byte{2}, uint16(3): []byte{3}, @@ -24,7 +24,7 @@ func TestKey_ParamBytes(t *testing.T) { }, } tests := []struct { - label interface{} + label any want []byte want1 bool }{ @@ -53,7 +53,7 @@ func TestKey_ParamInt(t *testing.T) { type i16 int16 type u16 uint16 key := &Key{ - Params: map[interface{}]interface{}{ + Params: map[any]any{ int64(-1): 1, 2: int8(2), uint16(3): i16(3), @@ -63,7 +63,7 @@ func TestKey_ParamInt(t *testing.T) { }, } tests := []struct { - label interface{} + label any want int64 want1 bool }{ @@ -93,7 +93,7 @@ func TestKey_ParamUint(t *testing.T) { type i16 int16 type u16 uint16 key := &Key{ - Params: map[interface{}]interface{}{ + Params: map[any]any{ int64(-1): 1, 2: int8(2), uint16(3): i16(3), @@ -104,7 +104,7 @@ func TestKey_ParamUint(t *testing.T) { }, } tests := []struct { - label interface{} + label any want uint64 want1 bool }{ @@ -134,7 +134,7 @@ func TestKey_ParamUint(t *testing.T) { func TestKey_ParamString(t *testing.T) { type str string key := &Key{ - Params: map[interface{}]interface{}{ + Params: map[any]any{ 1: "foo", "2": str("bar"), 3: []byte("baz"), @@ -142,7 +142,7 @@ func TestKey_ParamString(t *testing.T) { }, } tests := []struct { - label interface{} + label any want string want1 bool }{ @@ -167,7 +167,7 @@ func TestKey_ParamString(t *testing.T) { func TestKey_ParamBool(t *testing.T) { type boo bool key := &Key{ - Params: map[interface{}]interface{}{ + Params: map[any]any{ 1: true, "2": boo(false), 3: []byte("baz"), @@ -175,7 +175,7 @@ func TestKey_ParamBool(t *testing.T) { }, } tests := []struct { - label interface{} + label any want bool want1 bool }{ @@ -289,7 +289,7 @@ func TestKey_UnmarshalCBOR(t *testing.T) { 0x04, 0x41, 0x01, // key_ops: bstr(1) }, want: nil, - wantErr: "key_ops: invalid type: expected []interface{}, got []uint8", + wantErr: "key_ops: invalid type: expected []any, got []uint8", }, { name: "unknown key_ops entry value", data: []byte{ @@ -433,7 +433,7 @@ func TestKey_UnmarshalCBOR(t *testing.T) { }, want: &Key{ Type: -70000, - Params: map[interface{}]interface{}{ + Params: map[any]any{ int64(-1): int64(6), "f": "foo", }, @@ -461,7 +461,7 @@ func TestKey_UnmarshalCBOR(t *testing.T) { Algorithm: AlgorithmEdDSA, Ops: []KeyOp{KeyOpVerify, KeyOpSign}, BaseIV: []byte{0x03, 0x02, 0x01}, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: []byte{ 0x15, 0x52, 0x2e, 0xf1, 0x57, 0x29, 0xcc, 0xf3, @@ -485,7 +485,7 @@ func TestKey_UnmarshalCBOR(t *testing.T) { }, want: &Key{ Type: KeyTypeSymmetric, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelSymmetricK: []byte{ 0x15, 0x52, 0x2e, 0xf1, 0x57, 0x29, 0xcc, 0xf3, 0x95, 0x09, 0xea, 0x5c, 0x15, 0xa2, 0x6b, 0xe9, @@ -508,7 +508,7 @@ func TestKey_UnmarshalCBOR(t *testing.T) { want: &Key{ Type: KeyTypeEC2, ID: []byte("meriadoc.brandybuck@buckland.example"), - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: mustHexToBytes("65eda5a12577c2bae829437fe338701a10aaa375e1bb5b5de108de439c08551d"), KeyLabelEC2Y: mustHexToBytes("1e52ed75701163f7f9e40ddf9f341b3dc9ba860af7e0ca7ca7e9eecd0084d19c"), @@ -526,7 +526,7 @@ func TestKey_UnmarshalCBOR(t *testing.T) { want: &Key{ Type: KeyTypeEC2, ID: []byte("bilbo.baggins@hobbiton.example"), - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP521, KeyLabelEC2X: mustHexToBytes("0072992cb3ac08ecf3e5c63dedec0d51a8c1f79ef2f82f94f3c737bf5de7986671eac625fe8257bbd0394644caaa3aaf8f27a4585fbbcad0f2457620085e5c8f42ad"), KeyLabelEC2Y: mustHexToBytes("01dca6947bce88bc5790485ac97427342bc35f887d86d65a089377e247e60baa55e4e8501e2ada5724ac51d6909008033ebc10ac999b9d7f5cc2519f3fe1ea1d9475"), @@ -546,7 +546,7 @@ func TestKey_UnmarshalCBOR(t *testing.T) { want: &Key{ Type: KeyTypeEC2, ID: []byte("meriadoc.brandybuck@buckland.example"), - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: mustHexToBytes("65eda5a12577c2bae829437fe338701a10aaa375e1bb5b5de108de439c08551d"), KeyLabelEC2Y: mustHexToBytes("1e52ed75701163f7f9e40ddf9f341b3dc9ba860af7e0ca7ca7e9eecd0084d19c"), @@ -565,7 +565,7 @@ func TestKey_UnmarshalCBOR(t *testing.T) { want: &Key{ Type: KeyTypeEC2, ID: []byte("bilbo.baggins@hobbiton.example"), - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP521, KeyLabelEC2X: mustHexToBytes("0072992cb3ac08ecf3e5c63dedec0d51a8c1f79ef2f82f94f3c737bf5de7986671eac625fe8257bbd0394644caaa3aaf8f27a4585fbbcad0f2457620085e5c8f42ad"), KeyLabelEC2Y: mustHexToBytes("01dca6947bce88bc5790485ac97427342bc35f887d86d65a089377e247e60baa55e4e8501e2ada5724ac51d6909008033ebc10ac999b9d7f5cc2519f3fe1ea1d9475"), @@ -581,7 +581,7 @@ func TestKey_UnmarshalCBOR(t *testing.T) { want: &Key{ Type: KeyTypeSymmetric, ID: []byte("our-secret"), - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelSymmetricK: mustHexToBytes("849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188"), }, }, @@ -681,7 +681,7 @@ func TestKey_MarshalCBOR(t *testing.T) { name: "OKP with kty and private int params", key: &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ 0x46: 0x47, 0x66: 0x67, }, @@ -696,7 +696,7 @@ func TestKey_MarshalCBOR(t *testing.T) { name: "OKP with kty and private mixed params", key: &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ 0x1234: 0x47, "a": 0x67, }, @@ -711,7 +711,7 @@ func TestKey_MarshalCBOR(t *testing.T) { name: "OKP duplicated params", key: &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ int8(10): 0, int32(10): 1, }, @@ -721,7 +721,7 @@ func TestKey_MarshalCBOR(t *testing.T) { name: "OKP with invalid param label", key: &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ int8(10): 0, -3.5: 1, }, @@ -733,7 +733,7 @@ func TestKey_MarshalCBOR(t *testing.T) { Type: KeyTypeOKP, Algorithm: AlgorithmEdDSA, Ops: []KeyOp{KeyOpVerify, KeyOpEncrypt}, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: []byte{ 0x15, 0x52, 0x2e, 0xf1, 0x57, 0x29, 0xcc, 0xf3, @@ -763,7 +763,7 @@ func TestKey_MarshalCBOR(t *testing.T) { key: &Key{ Type: KeyTypeEC2, Algorithm: AlgorithmES256, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: []byte{0x01}, KeyLabelEC2Y: []byte{0x02, 0x03}, @@ -790,7 +790,7 @@ func TestKey_MarshalCBOR(t *testing.T) { name: "Symmetric", key: &Key{ Type: KeyTypeSymmetric, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelSymmetricK: []byte{ 0x15, 0x52, 0x2e, 0xf1, 0x57, 0x29, 0xcc, 0xf3, 0x95, 0x09, 0xea, 0x5c, 0x15, 0xa2, 0x6b, 0xe9, @@ -851,7 +851,7 @@ func TestNewKeyOKP(t *testing.T) { want: &Key{ Type: KeyTypeOKP, Algorithm: AlgorithmEdDSA, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: x, KeyLabelOKPD: d, @@ -903,7 +903,7 @@ func TestNewNewKeyEC2(t *testing.T) { want: &Key{ Type: KeyTypeEC2, Algorithm: AlgorithmES256, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: ec256x, KeyLabelEC2Y: ec256y, @@ -916,7 +916,7 @@ func TestNewNewKeyEC2(t *testing.T) { want: &Key{ Type: KeyTypeEC2, Algorithm: AlgorithmES384, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP384, KeyLabelEC2X: ec384x, KeyLabelEC2Y: ec384y, @@ -929,7 +929,7 @@ func TestNewNewKeyEC2(t *testing.T) { want: &Key{ Type: KeyTypeEC2, Algorithm: AlgorithmES512, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP521, KeyLabelEC2X: ec521x, KeyLabelEC2Y: ec521y, @@ -972,7 +972,7 @@ func TestNewKeySymmetric(t *testing.T) { }{ {"valid", args{[]byte{1, 2, 3}}, &Key{ Type: KeyTypeSymmetric, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelSymmetricK: []byte{1, 2, 3}, }, }}, @@ -1058,7 +1058,7 @@ func TestKey_AlgorithmOrDefault(t *testing.T) { "OKP-Ed25519", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, }, }, @@ -1069,7 +1069,7 @@ func TestKey_AlgorithmOrDefault(t *testing.T) { "OKP-P256", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveP256, }, }, @@ -1080,7 +1080,7 @@ func TestKey_AlgorithmOrDefault(t *testing.T) { "EC2-P256", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, }, }, @@ -1091,7 +1091,7 @@ func TestKey_AlgorithmOrDefault(t *testing.T) { "EC2-P384", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP384, }, }, @@ -1102,7 +1102,7 @@ func TestKey_AlgorithmOrDefault(t *testing.T) { "EC2-P521", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP521, }, }, @@ -1113,7 +1113,7 @@ func TestKey_AlgorithmOrDefault(t *testing.T) { "EC2-Ed25519", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveEd25519, }, }, @@ -1151,7 +1151,7 @@ func TestNewKeyFromPrivate(t *testing.T) { }, &Key{ Algorithm: AlgorithmES256, Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: x, KeyLabelEC2Y: y, @@ -1172,7 +1172,7 @@ func TestNewKeyFromPrivate(t *testing.T) { "ed25519", ed25519.PrivateKey(append(okpd, okpx...)), &Key{ Algorithm: AlgorithmEdDSA, Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: okpx, KeyLabelOKPD: okpd, @@ -1213,7 +1213,7 @@ func TestNewKeyFromPublic(t *testing.T) { &Key{ Algorithm: AlgorithmES256, Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: ecx, KeyLabelEC2Y: ecy, @@ -1231,7 +1231,7 @@ func TestNewKeyFromPublic(t *testing.T) { &Key{ Algorithm: AlgorithmEdDSA, Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: okpx, }, @@ -1270,7 +1270,7 @@ func TestKey_Signer(t *testing.T) { "without algorithm", &Key{ Type: KeyTypeOKP, Ops: []KeyOp{KeyOpSign}, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: x, KeyLabelOKPD: d, @@ -1283,7 +1283,7 @@ func TestKey_Signer(t *testing.T) { "without key_ops", &Key{ Type: KeyTypeOKP, Algorithm: AlgorithmEdDSA, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: x, KeyLabelOKPD: d, @@ -1295,7 +1295,7 @@ func TestKey_Signer(t *testing.T) { { "invalid algorithm", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveP256, KeyLabelOKPX: x, KeyLabelOKPD: d, @@ -1308,7 +1308,7 @@ func TestKey_Signer(t *testing.T) { "can't sign", &Key{ Type: KeyTypeOKP, Ops: []KeyOp{KeyOpVerify}, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: x, KeyLabelOKPD: d, @@ -1321,7 +1321,7 @@ func TestKey_Signer(t *testing.T) { "unsupported key", &Key{ Type: KeyTypeSymmetric, Ops: []KeyOp{KeyOpSign}, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelSymmetricK: d, }, }, @@ -1357,7 +1357,7 @@ func TestKey_Verifier(t *testing.T) { "without algorithm", &Key{ Type: KeyTypeOKP, Ops: []KeyOp{KeyOpVerify}, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: x, }, @@ -1369,7 +1369,7 @@ func TestKey_Verifier(t *testing.T) { "without key_ops", &Key{ Type: KeyTypeOKP, Algorithm: AlgorithmEdDSA, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: x, }, @@ -1380,7 +1380,7 @@ func TestKey_Verifier(t *testing.T) { { "invalid algorithm", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveP256, KeyLabelOKPX: x, }, @@ -1392,7 +1392,7 @@ func TestKey_Verifier(t *testing.T) { "can't verify", &Key{ Type: KeyTypeOKP, Ops: []KeyOp{KeyOpSign}, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: x, }, @@ -1404,7 +1404,7 @@ func TestKey_Verifier(t *testing.T) { "unsupported key", &Key{ Type: KeyTypeSymmetric, Ops: []KeyOp{KeyOpVerify}, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelSymmetricK: x, }, }, @@ -1442,7 +1442,7 @@ func TestKey_PrivateKey(t *testing.T) { { "CurveEd25519", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: okpx, KeyLabelOKPD: okpd, @@ -1453,7 +1453,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "CurveEd25519 missing x", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPD: okpd, }, @@ -1463,7 +1463,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "CurveP256", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: ec256x, KeyLabelEC2Y: ec256y, @@ -1482,7 +1482,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "CurveP256 missing x and y", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2D: ec256d, }, @@ -1499,7 +1499,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "CurveP384", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP384, KeyLabelEC2X: ec384x, KeyLabelEC2Y: ec384y, @@ -1518,7 +1518,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "CurveP521", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP521, KeyLabelEC2X: ec521x, KeyLabelEC2Y: ec521y, @@ -1543,7 +1543,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "OKP unknown curve", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: 70, KeyLabelOKPX: okpx, KeyLabelOKPD: okpd, @@ -1554,7 +1554,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "OKP missing d", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: okpx, }, @@ -1564,7 +1564,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "OKP incorrect x size", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: make([]byte, 10), KeyLabelOKPD: okpd, @@ -1575,7 +1575,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "OKP incorrect d size", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: okpx, KeyLabelOKPD: make([]byte, 5), @@ -1586,7 +1586,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "EC2 missing D", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: ec256x, KeyLabelEC2Y: ec256y, @@ -1597,7 +1597,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "EC2 unknown curve", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: 70, KeyLabelEC2X: ec256x, KeyLabelEC2Y: ec256y, @@ -1609,7 +1609,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "EC2 incorrect x size", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: ec384x, KeyLabelEC2Y: ec256y, @@ -1621,7 +1621,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "EC2 incorrect y size", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: ec256x, KeyLabelEC2Y: ec384y, @@ -1633,7 +1633,7 @@ func TestKey_PrivateKey(t *testing.T) { }, { "EC2 incorrect d size", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: ec256x, KeyLabelEC2Y: ec256y, @@ -1672,7 +1672,7 @@ func TestKey_PublicKey(t *testing.T) { { "CurveEd25519", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, KeyLabelOKPX: okpx, }, @@ -1682,7 +1682,7 @@ func TestKey_PublicKey(t *testing.T) { }, { "CurveP256", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: ec256x, KeyLabelEC2Y: ec256y, @@ -1697,7 +1697,7 @@ func TestKey_PublicKey(t *testing.T) { }, { "CurveP384", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP384, KeyLabelEC2X: ec384x, KeyLabelEC2Y: ec384y, @@ -1712,7 +1712,7 @@ func TestKey_PublicKey(t *testing.T) { }, { "CurveP521", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP521, KeyLabelEC2X: ec521x, KeyLabelEC2Y: ec521y, @@ -1739,7 +1739,7 @@ func TestKey_PublicKey(t *testing.T) { }, { "OKP missing X", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: CurveEd25519, }, }, @@ -1748,7 +1748,7 @@ func TestKey_PublicKey(t *testing.T) { }, { "OKP unknown curve", &Key{ Type: KeyTypeOKP, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelOKPCurve: 70, KeyLabelOKPX: okpx, }, @@ -1758,7 +1758,7 @@ func TestKey_PublicKey(t *testing.T) { }, { "EC2 missing X", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2Y: ec256y, }, @@ -1768,7 +1768,7 @@ func TestKey_PublicKey(t *testing.T) { }, { "EC2 missing Y", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: CurveP256, KeyLabelEC2X: ec256x, }, @@ -1778,7 +1778,7 @@ func TestKey_PublicKey(t *testing.T) { }, { "EC2 unknown curve", &Key{ Type: KeyTypeEC2, - Params: map[interface{}]interface{}{ + Params: map[any]any{ KeyLabelEC2Curve: 70, KeyLabelEC2X: ec256x, KeyLabelEC2Y: ec256y, diff --git a/sign.go b/sign.go index cd86d11..a2bb6c0 100644 --- a/sign.go +++ b/sign.go @@ -236,7 +236,7 @@ func (s *Signature) toBeSigned(bodyProtected cbor.RawMessage, payload, external if external == nil { external = []byte{} } - sigStructure := []interface{}{ + sigStructure := []any{ "Signature", // context bodyProtected, // body_protected signProtected, // sign_protected diff --git a/sign1.go b/sign1.go index 48640ab..e1bd4d0 100644 --- a/sign1.go +++ b/sign1.go @@ -174,7 +174,7 @@ func (m *Sign1Message) toBeSigned(external []byte) ([]byte, error) { if external == nil { external = []byte{} } - sigStructure := []interface{}{ + sigStructure := []any{ "Signature1", // context protected, // body_protected external, // external_aad