Skip to content

Commit

Permalink
rename AlgorithmInvalid to AlgorithmReserved
Browse files Browse the repository at this point in the history
Signed-off-by: qmuntal <[email protected]>
  • Loading branch information
qmuntal committed Jul 24, 2023
1 parent 0f0bbda commit adad1c2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
6 changes: 4 additions & 2 deletions algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ const (
// PureEdDSA by RFC 8152.
AlgorithmEdDSA Algorithm = -8

// An invalid/unrecognised algorithm.
AlgorithmInvalid Algorithm = 0
// Reserved value.
AlgorithmReserved Algorithm = 0
)

// Algorithm represents an IANA algorithm entry in the COSE Algorithms registry.
Expand Down Expand Up @@ -75,6 +75,8 @@ func (a Algorithm) String() string {
// As stated in RFC 8152 8.2, only the pure EdDSA version is used for
// COSE.
return "EdDSA"
case AlgorithmReserved:
return "Reserved"
default:
return "unknown algorithm value " + strconv.Itoa(int(a))
}
Expand Down
17 changes: 10 additions & 7 deletions algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ import (
func TestAlgorithm_String(t *testing.T) {
// run tests
tests := []struct {
name string
alg Algorithm
want string
}{
{
name: "unknown algorithm",
alg: 0,
want: "unknown algorithm value 0",
},
{AlgorithmPS256, "PS256"},
{AlgorithmPS384, "PS384"},
{AlgorithmPS512, "PS512"},
{AlgorithmES256, "ES256"},
{AlgorithmES384, "ES384"},
{AlgorithmES512, "ES512"},
{AlgorithmEdDSA, "EdDSA"},
{AlgorithmReserved, "Reserved"},
{7, "unknown algorithm value 7"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run(tt.want, func(t *testing.T) {
if got := tt.alg.String(); got != tt.want {
t.Errorf("Algorithm.String() = %v, want %v", got, tt.want)
}
Expand Down
4 changes: 2 additions & 2 deletions headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ func (h ProtectedHeader) Algorithm() (Algorithm, error) {
case int64:
return Algorithm(alg), nil
case string:
return AlgorithmInvalid, fmt.Errorf("unknown algorithm value %q", alg)
return AlgorithmReserved, fmt.Errorf("unknown algorithm value %q", alg)
default:
return AlgorithmInvalid, ErrInvalidAlgorithm
return AlgorithmReserved, ErrInvalidAlgorithm
}
}

Expand Down
18 changes: 9 additions & 9 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func NewKeyFromPublic(pub crypto.PublicKey) (*Key, error) {
case *ecdsa.PublicKey:
alg := algorithmFromEllipticCurve(vk.Curve)

if alg == AlgorithmInvalid {
if alg == AlgorithmReserved {
return nil, fmt.Errorf("unsupported curve: %v", vk.Curve)
}

Expand All @@ -406,7 +406,7 @@ func NewKeyFromPrivate(priv crypto.PrivateKey) (*Key, error) {
case *ecdsa.PrivateKey:
alg := algorithmFromEllipticCurve(sk.Curve)

if alg == AlgorithmInvalid {
if alg == AlgorithmReserved {
return nil, fmt.Errorf("unsupported curve: %v", sk.Curve)
}

Expand Down Expand Up @@ -500,7 +500,7 @@ func (k Key) validate(op KeyOp) error {
}

// If Algorithm is set, it must match the specified key parameters.
if k.Algorithm != AlgorithmInvalid {
if k.Algorithm != AlgorithmReserved {
expectedAlg, err := k.deriveAlgorithm()
if err != nil {
return err
Expand Down Expand Up @@ -538,7 +538,7 @@ func (k *Key) MarshalCBOR() ([]byte, error) {
if k.ID != nil {
tmp[keyLabelKeyID] = k.ID
}
if k.Algorithm != AlgorithmInvalid {
if k.Algorithm != AlgorithmReserved {
tmp[keyLabelAlgorithm] = k.Algorithm
}
if k.Ops != nil {
Expand Down Expand Up @@ -750,7 +750,7 @@ func (k *Key) PrivateKey() (crypto.PrivateKey, error) {
// Key.Curve. This method does NOT validate that Key.Algorithm, if set, aligns
// with Key.Curve.
func (k *Key) AlgorithmOrDefault() (Algorithm, error) {
if k.Algorithm != AlgorithmInvalid {
if k.Algorithm != AlgorithmReserved {
return k.Algorithm, nil
}

Expand Down Expand Up @@ -814,7 +814,7 @@ func (k *Key) deriveAlgorithm() (Algorithm, error) {
case CurveP521:
return AlgorithmES512, nil
default:
return AlgorithmInvalid, fmt.Errorf(
return AlgorithmReserved, fmt.Errorf(
"unsupported curve %q for key type EC2", crv.String())
}
case KeyTypeOKP:
Expand All @@ -823,12 +823,12 @@ func (k *Key) deriveAlgorithm() (Algorithm, error) {
case CurveEd25519:
return AlgorithmEdDSA, nil
default:
return AlgorithmInvalid, fmt.Errorf(
return AlgorithmReserved, fmt.Errorf(
"unsupported curve %q for key type OKP", crv.String())
}
default:
// Symmetric algorithms are not supported in the current inmplementation.
return AlgorithmInvalid, fmt.Errorf("unexpected key type %q", k.Type.String())
return AlgorithmReserved, fmt.Errorf("unexpected key type %q", k.Type.String())
}
}

Expand All @@ -841,7 +841,7 @@ func algorithmFromEllipticCurve(c elliptic.Curve) Algorithm {
case elliptic.P521():
return AlgorithmES512
default:
return AlgorithmInvalid
return AlgorithmReserved
}
}

Expand Down
16 changes: 8 additions & 8 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ func TestKey_AlgorithmOrDefault(t *testing.T) {
KeyLabelOKPCurve: CurveP256,
},
},
AlgorithmInvalid,
AlgorithmReserved,
`unsupported curve "P-256" for key type OKP`,
},
{
Expand Down Expand Up @@ -1117,7 +1117,7 @@ func TestKey_AlgorithmOrDefault(t *testing.T) {
KeyLabelEC2Curve: CurveEd25519,
},
},
AlgorithmInvalid,
AlgorithmReserved,
`unsupported curve "Ed25519" for key type EC2`,
},
}
Expand Down Expand Up @@ -1301,7 +1301,7 @@ func TestKey_Signer(t *testing.T) {
KeyLabelOKPD: d,
},
},
AlgorithmInvalid,
AlgorithmReserved,
"invalid key: curve not supported for the given key type",
},
{
Expand All @@ -1314,7 +1314,7 @@ func TestKey_Signer(t *testing.T) {
KeyLabelOKPD: d,
},
},
AlgorithmInvalid,
AlgorithmReserved,
ErrOpNotSupported.Error(),
},
{
Expand All @@ -1325,7 +1325,7 @@ func TestKey_Signer(t *testing.T) {
KeyLabelSymmetricK: d,
},
},
AlgorithmInvalid,
AlgorithmReserved,
`unexpected key type "Symmetric"`,
},
}
Expand Down Expand Up @@ -1385,7 +1385,7 @@ func TestKey_Verifier(t *testing.T) {
KeyLabelOKPX: x,
},
},
AlgorithmInvalid,
AlgorithmReserved,
"invalid key: curve not supported for the given key type",
},
{
Expand All @@ -1397,7 +1397,7 @@ func TestKey_Verifier(t *testing.T) {
KeyLabelOKPX: x,
},
},
AlgorithmInvalid,
AlgorithmReserved,
ErrOpNotSupported.Error(),
},
{
Expand All @@ -1408,7 +1408,7 @@ func TestKey_Verifier(t *testing.T) {
KeyLabelSymmetricK: x,
},
},
AlgorithmInvalid,
AlgorithmReserved,
`unexpected key type "Symmetric"`,
},
}
Expand Down

0 comments on commit adad1c2

Please sign in to comment.