Skip to content

Commit

Permalink
fix(#36): fixed importKey, added unit tests, copyright notice updates
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencedro committed Jan 22, 2025
1 parent c7b923a commit a71502d
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
# Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
# Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion algorithm.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion algorithms/ecdsa/ecdsa.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion algorithms/ecdsa/ecdsa_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
17 changes: 10 additions & 7 deletions algorithms/hmac/hmac.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -297,8 +297,10 @@ func importKeyFromJsonWebKey(keyData *webcrypto.JsonWebKey, params *ImportParams

// If usages is non-empty and the use field of jwk is present and is not "sign", then throw a DataError.
if len(usages) != 0 {
if keyData.Use != "sign" {
return nil, webcrypto.NewError(webcrypto.ErrDataError, "use must be 'sign'")
if keyData.Use != "" {
if keyData.Use != "sign" {
return nil, webcrypto.NewError(webcrypto.ErrDataError, "use must be 'sign'")
}
}
}

Expand All @@ -316,12 +318,13 @@ func importKeyFromJsonWebKey(keyData *webcrypto.JsonWebKey, params *ImportParams
return nil, webcrypto.NewError(webcrypto.ErrDataError, "k length cannot be less than hash length")
}

if params.Length != uint64(length) {
return nil, webcrypto.NewError(webcrypto.ErrDataError, "length provided does not match key length")
// If the params length is specified, we'll check and ensure the key provided matches the length
if params.Length != 0 {
if params.Length != uint64(length) {
return nil, webcrypto.NewError(webcrypto.ErrDataError, "length provided does not match key length")
}
}

params.Length = uint64(length)

if keyData.Ext != extractable {
return nil, webcrypto.NewError(webcrypto.ErrDataError, "ext in key does not match value provided")
}
Expand Down
95 changes: 94 additions & 1 deletion algorithms/hmac/hmac_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ package hmac
import (
"bytes"
"encoding/hex"
"encoding/json"
"reflect"
"testing"

Expand Down Expand Up @@ -130,6 +131,98 @@ func TestImportKey(t *testing.T) {

}

func Test_ImportKey_JsonWebKey(t *testing.T) {
t.Run("import no use", func(t *testing.T) {
k := `{"kty":"oct","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
var jwk webcrypto.JsonWebKey
if err := json.Unmarshal([]byte(k), &jwk); err != nil {
t.Errorf("failed to unmarshal json: %s", err.Error())
}

_, err := subtle.ImportKey(webcrypto.Jwk, &jwk, &webcrypto.Algorithm{
Name: "HMAC",
Params: &ImportParams{
Hash: "SHA-256",
},
}, true, []webcrypto.KeyUsage{webcrypto.Sign, webcrypto.Verify})
if err != nil {
t.Errorf("failed to import key: %s", err.Error())
}
})

t.Run("import valid use", func(t *testing.T) {
k := `{"kty":"oct","use":"sign","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
var jwk webcrypto.JsonWebKey
if err := json.Unmarshal([]byte(k), &jwk); err != nil {
t.Errorf("failed to unmarshal json: %s", err.Error())
}

_, err := subtle.ImportKey(webcrypto.Jwk, &jwk, &webcrypto.Algorithm{
Name: "HMAC",
Params: &ImportParams{
Hash: "SHA-256",
},
}, true, []webcrypto.KeyUsage{webcrypto.Sign, webcrypto.Verify})
if err != nil {
t.Errorf("failed to import key: %s", err.Error())
}
})

t.Run("import invalid use", func(t *testing.T) {
k := `{"kty":"oct","use":"enc","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
var jwk webcrypto.JsonWebKey
if err := json.Unmarshal([]byte(k), &jwk); err != nil {
t.Errorf("failed to unmarshal json: %s", err.Error())
}

_, err := subtle.ImportKey(webcrypto.Jwk, &jwk, &webcrypto.Algorithm{
Name: "HMAC",
Params: &ImportParams{
Hash: "SHA-256",
},
}, true, []webcrypto.KeyUsage{webcrypto.Sign, webcrypto.Verify})
if err == nil {
t.Error("importKey should have returned error")
}
})

t.Run("import invalid key_ops", func(t *testing.T) {
k := `{"kty":"oct","key_ops":["encrypt","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
var jwk webcrypto.JsonWebKey
if err := json.Unmarshal([]byte(k), &jwk); err != nil {
t.Errorf("failed to unmarshal json: %s", err.Error())
}

_, err := subtle.ImportKey(webcrypto.Jwk, &jwk, &webcrypto.Algorithm{
Name: "HMAC",
Params: &ImportParams{
Hash: "SHA-256",
},
}, true, []webcrypto.KeyUsage{webcrypto.Sign, webcrypto.Verify})
if err == nil {
t.Error("importKey should have returned error")
}
})

t.Run("import invalid key length", func(t *testing.T) {
k := `{"kty":"oct","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"VrmFU2huAL6phqi_vvGPvItpX2cJFy6rzjEQpjMqKA0"}`
var jwk webcrypto.JsonWebKey
if err := json.Unmarshal([]byte(k), &jwk); err != nil {
t.Errorf("failed to unmarshal json: %s", err.Error())
}

_, err := subtle.ImportKey(webcrypto.Jwk, &jwk, &webcrypto.Algorithm{
Name: "HMAC",
Params: &ImportParams{
Hash: "SHA-256",
},
}, true, []webcrypto.KeyUsage{webcrypto.Sign, webcrypto.Verify})
if err == nil {
t.Error("importKey should have returned error")
}
})
}

func TestSign(t *testing.T) {
raw, err := hex.DecodeString(rawHexKey)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion algorithms/rsa/rsa.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion algorithms/rsa/rsa_oaep.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion algorithms/rsa/rsa_oaep_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion algorithms/sha/sha.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion algorithms/sha/sha_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion crypto.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion crypto_key.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion format.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion subtle.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion util/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion util/util_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down

0 comments on commit a71502d

Please sign in to comment.