1
- // Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1
+ // Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
2
2
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package hmac
19
19
import (
20
20
"bytes"
21
21
"encoding/hex"
22
+ "encoding/json"
22
23
"reflect"
23
24
"testing"
24
25
@@ -130,6 +131,98 @@ func TestImportKey(t *testing.T) {
130
131
131
132
}
132
133
134
+ func Test_ImportKey_JsonWebKey (t * testing.T ) {
135
+ t .Run ("import no use" , func (t * testing.T ) {
136
+ k := `{"kty":"oct","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
137
+ var jwk webcrypto.JsonWebKey
138
+ if err := json .Unmarshal ([]byte (k ), & jwk ); err != nil {
139
+ t .Errorf ("failed to unmarshal json: %s" , err .Error ())
140
+ }
141
+
142
+ _ , err := subtle .ImportKey (webcrypto .Jwk , & jwk , & webcrypto.Algorithm {
143
+ Name : "HMAC" ,
144
+ Params : & ImportParams {
145
+ Hash : "SHA-256" ,
146
+ },
147
+ }, true , []webcrypto.KeyUsage {webcrypto .Sign , webcrypto .Verify })
148
+ if err != nil {
149
+ t .Errorf ("failed to import key: %s" , err .Error ())
150
+ }
151
+ })
152
+
153
+ t .Run ("import valid use" , func (t * testing.T ) {
154
+ k := `{"kty":"oct","use":"sign","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
155
+ var jwk webcrypto.JsonWebKey
156
+ if err := json .Unmarshal ([]byte (k ), & jwk ); err != nil {
157
+ t .Errorf ("failed to unmarshal json: %s" , err .Error ())
158
+ }
159
+
160
+ _ , err := subtle .ImportKey (webcrypto .Jwk , & jwk , & webcrypto.Algorithm {
161
+ Name : "HMAC" ,
162
+ Params : & ImportParams {
163
+ Hash : "SHA-256" ,
164
+ },
165
+ }, true , []webcrypto.KeyUsage {webcrypto .Sign , webcrypto .Verify })
166
+ if err != nil {
167
+ t .Errorf ("failed to import key: %s" , err .Error ())
168
+ }
169
+ })
170
+
171
+ t .Run ("import invalid use" , func (t * testing.T ) {
172
+ k := `{"kty":"oct","use":"enc","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
173
+ var jwk webcrypto.JsonWebKey
174
+ if err := json .Unmarshal ([]byte (k ), & jwk ); err != nil {
175
+ t .Errorf ("failed to unmarshal json: %s" , err .Error ())
176
+ }
177
+
178
+ _ , err := subtle .ImportKey (webcrypto .Jwk , & jwk , & webcrypto.Algorithm {
179
+ Name : "HMAC" ,
180
+ Params : & ImportParams {
181
+ Hash : "SHA-256" ,
182
+ },
183
+ }, true , []webcrypto.KeyUsage {webcrypto .Sign , webcrypto .Verify })
184
+ if err == nil {
185
+ t .Error ("importKey should have returned error" )
186
+ }
187
+ })
188
+
189
+ t .Run ("import invalid key_ops" , func (t * testing.T ) {
190
+ k := `{"kty":"oct","key_ops":["encrypt","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
191
+ var jwk webcrypto.JsonWebKey
192
+ if err := json .Unmarshal ([]byte (k ), & jwk ); err != nil {
193
+ t .Errorf ("failed to unmarshal json: %s" , err .Error ())
194
+ }
195
+
196
+ _ , err := subtle .ImportKey (webcrypto .Jwk , & jwk , & webcrypto.Algorithm {
197
+ Name : "HMAC" ,
198
+ Params : & ImportParams {
199
+ Hash : "SHA-256" ,
200
+ },
201
+ }, true , []webcrypto.KeyUsage {webcrypto .Sign , webcrypto .Verify })
202
+ if err == nil {
203
+ t .Error ("importKey should have returned error" )
204
+ }
205
+ })
206
+
207
+ t .Run ("import invalid key length" , func (t * testing.T ) {
208
+ k := `{"kty":"oct","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"VrmFU2huAL6phqi_vvGPvItpX2cJFy6rzjEQpjMqKA0"}`
209
+ var jwk webcrypto.JsonWebKey
210
+ if err := json .Unmarshal ([]byte (k ), & jwk ); err != nil {
211
+ t .Errorf ("failed to unmarshal json: %s" , err .Error ())
212
+ }
213
+
214
+ _ , err := subtle .ImportKey (webcrypto .Jwk , & jwk , & webcrypto.Algorithm {
215
+ Name : "HMAC" ,
216
+ Params : & ImportParams {
217
+ Hash : "SHA-256" ,
218
+ },
219
+ }, true , []webcrypto.KeyUsage {webcrypto .Sign , webcrypto .Verify })
220
+ if err == nil {
221
+ t .Error ("importKey should have returned error" )
222
+ }
223
+ })
224
+ }
225
+
133
226
func TestSign (t * testing.T ) {
134
227
raw , err := hex .DecodeString (rawHexKey )
135
228
if err != nil {
0 commit comments