Skip to content

Commit 272bdef

Browse files
committed
Add enum COSEKeyOps.
1 parent 6d142ef commit 272bdef

3 files changed

Lines changed: 93 additions & 19 deletions

File tree

cwt/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
set_private_claim_names,
1313
)
1414
from .encrypted_cose_key import EncryptedCOSEKey
15-
from .enums import COSEAlgs, COSEHeaders, COSEKeyParams, COSETypes, CWTClaims
15+
from .enums import (
16+
COSEAlgs,
17+
COSEHeaders,
18+
COSEKeyOps,
19+
COSEKeyParams,
20+
COSETypes,
21+
CWTClaims,
22+
)
1623
from .exceptions import CWTError, DecodeError, EncodeError, VerifyError
1724
from .helpers.hcert import load_pem_hcert_dsc
1825
from .recipient import Recipient
@@ -38,6 +45,7 @@
3845
"COSE",
3946
"COSEAlgs",
4047
"COSEHeaders",
48+
"COSEKeyOps",
4149
"COSEKeyParams",
4250
"COSETypes",
4351
"COSEKey",

cwt/enums.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,16 @@ class CWTClaims(enum.IntEnum):
137137
LOCATION = 17
138138
EAT_PROFILE = 18
139139
SUBMODS = 20
140+
141+
142+
class COSEKeyOps(enum.IntEnum):
143+
SIGN = 1
144+
VERIFY = 2
145+
ENCRYPT = 3
146+
DECRYPT = 4
147+
WRAP_KEY = 5
148+
UNWRAP_KEY = 6
149+
DERIVE_KEY = 7
150+
DERIVE_BITS = 8
151+
MAC_CREATE = 9
152+
MAC_VERIFY = 10

tests/test_algs_okp.py

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from cwt.algs.okp import OKPKey
99
from cwt.cose_key import COSEKey
10-
from cwt.enums import COSEKeyParams
10+
from cwt.enums import COSEKeyOps, COSEKeyParams
1111
from cwt.exceptions import VerifyError
1212

1313
from .utils import key_path
@@ -440,7 +440,10 @@ def test_okp_key_derive_bytes_with_raw_context(self):
440440
COSEKeyParams.CRV: 6,
441441
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
442442
COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
443-
COSEKeyParams.KEY_OPS: [7, 8],
443+
COSEKeyParams.KEY_OPS: [
444+
COSEKeyOps.DERIVE_KEY,
445+
COSEKeyOps.DERIVE_BITS,
446+
],
444447
},
445448
"Invalid key_ops for signing key.",
446449
),
@@ -451,7 +454,12 @@ def test_okp_key_derive_bytes_with_raw_context(self):
451454
COSEKeyParams.CRV: 6,
452455
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
453456
COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
454-
COSEKeyParams.KEY_OPS: [1, 2, 7, 8],
457+
COSEKeyParams.KEY_OPS: [
458+
COSEKeyOps.SIGN,
459+
COSEKeyOps.VERIFY,
460+
COSEKeyOps.DERIVE_KEY,
461+
COSEKeyOps.DERIVE_BITS,
462+
],
455463
},
456464
"Signing key should not be used for key derivation.",
457465
),
@@ -462,7 +470,10 @@ def test_okp_key_derive_bytes_with_raw_context(self):
462470
COSEKeyParams.CRV: 6,
463471
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
464472
# COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
465-
COSEKeyParams.KEY_OPS: [1, 2],
473+
COSEKeyParams.KEY_OPS: [
474+
COSEKeyOps.SIGN,
475+
COSEKeyOps.VERIFY,
476+
],
466477
},
467478
"Invalid key_ops for public key.",
468479
),
@@ -473,7 +484,10 @@ def test_okp_key_derive_bytes_with_raw_context(self):
473484
COSEKeyParams.CRV: 6,
474485
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
475486
COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
476-
COSEKeyParams.KEY_OPS: [1, 2],
487+
COSEKeyParams.KEY_OPS: [
488+
COSEKeyOps.SIGN,
489+
COSEKeyOps.VERIFY,
490+
],
477491
},
478492
"Invalid key_ops for key derivation.",
479493
),
@@ -484,7 +498,10 @@ def test_okp_key_derive_bytes_with_raw_context(self):
484498
COSEKeyParams.CRV: 6,
485499
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
486500
COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
487-
COSEKeyParams.KEY_OPS: [1, 2],
501+
COSEKeyParams.KEY_OPS: [
502+
COSEKeyOps.SIGN,
503+
COSEKeyOps.VERIFY,
504+
],
488505
},
489506
"Invalid key_ops for key derivation.",
490507
),
@@ -495,7 +512,12 @@ def test_okp_key_derive_bytes_with_raw_context(self):
495512
COSEKeyParams.CRV: 6,
496513
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
497514
COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
498-
COSEKeyParams.KEY_OPS: [1, 2, 7, 8],
515+
COSEKeyParams.KEY_OPS: [
516+
COSEKeyOps.SIGN,
517+
COSEKeyOps.VERIFY,
518+
COSEKeyOps.DERIVE_KEY,
519+
COSEKeyOps.DERIVE_BITS,
520+
],
499521
},
500522
"Private key for ECDHE should not be used for signing.",
501523
),
@@ -505,8 +527,10 @@ def test_okp_key_derive_bytes_with_raw_context(self):
505527
COSEKeyParams.ALG: -25,
506528
COSEKeyParams.CRV: 4,
507529
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
508-
# COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
509-
COSEKeyParams.KEY_OPS: [7, 8],
530+
COSEKeyParams.KEY_OPS: [
531+
COSEKeyOps.DERIVE_KEY,
532+
COSEKeyOps.DERIVE_BITS,
533+
],
510534
},
511535
"Public key for ECDHE should not have key_ops.",
512536
),
@@ -528,7 +552,12 @@ def test_okp_key_derive_bytes_with_raw_context(self):
528552
COSEKeyParams.CRV: 6,
529553
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
530554
COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
531-
COSEKeyParams.KEY_OPS: [1, 2, 7, 8],
555+
COSEKeyParams.KEY_OPS: [
556+
COSEKeyOps.SIGN,
557+
COSEKeyOps.VERIFY,
558+
COSEKeyOps.DERIVE_KEY,
559+
COSEKeyOps.DERIVE_BITS,
560+
],
532561
},
533562
"OKP private key should not be used for both signing and key derivation.",
534563
),
@@ -539,7 +568,12 @@ def test_okp_key_derive_bytes_with_raw_context(self):
539568
COSEKeyParams.CRV: 6,
540569
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
541570
# COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
542-
COSEKeyParams.KEY_OPS: [1, 2, 7, 8],
571+
COSEKeyParams.KEY_OPS: [
572+
COSEKeyOps.SIGN,
573+
COSEKeyOps.VERIFY,
574+
COSEKeyOps.DERIVE_KEY,
575+
COSEKeyOps.DERIVE_BITS,
576+
],
543577
},
544578
"Invalid key_ops for public key.",
545579
),
@@ -550,7 +584,10 @@ def test_okp_key_derive_bytes_with_raw_context(self):
550584
COSEKeyParams.CRV: 6,
551585
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
552586
# COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
553-
COSEKeyParams.KEY_OPS: [7, 8],
587+
COSEKeyParams.KEY_OPS: [
588+
COSEKeyOps.DERIVE_KEY,
589+
COSEKeyOps.DERIVE_BITS,
590+
],
554591
},
555592
"Invalid key_ops for public key.",
556593
),
@@ -562,7 +599,10 @@ def test_okp_key_derive_bytes_with_raw_context(self):
562599
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
563600
COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
564601
33: 123,
565-
COSEKeyParams.KEY_OPS: [7, 8],
602+
COSEKeyParams.KEY_OPS: [
603+
COSEKeyOps.DERIVE_KEY,
604+
COSEKeyOps.DERIVE_BITS,
605+
],
566606
},
567607
"x5c(33) should be bytes(bstr) or list.",
568608
),
@@ -571,7 +611,10 @@ def test_okp_key_derive_bytes_with_raw_context(self):
571611
# COSEKeyParams.KTY: 1,
572612
# COSEKeyParams.CRV: 4,
573613
# COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
574-
# COSEKeyParams.KEY_OPS: [7, 8],
614+
# COSEKeyParams.KEY_OPS: [
615+
# COSEKeyOps.DERIVE_KEY,
616+
# COSEKeyOps.DERIVE_BITS,
617+
# ],
575618
# },
576619
# "X25519/X448 needs alg explicitly.",
577620
# ),
@@ -582,7 +625,9 @@ def test_okp_key_derive_bytes_with_raw_context(self):
582625
COSEKeyParams.CRV: 6,
583626
# COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
584627
COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
585-
COSEKeyParams.KEY_OPS: [2],
628+
COSEKeyParams.KEY_OPS: [
629+
COSEKeyOps.VERIFY,
630+
],
586631
},
587632
"x(-2) not found.",
588633
),
@@ -593,15 +638,20 @@ def test_okp_key_derive_bytes_with_raw_context(self):
593638
COSEKeyParams.CRV: 4,
594639
COSEKeyParams.X: b"\x18Es\xe0\x9a\x83\xfd\x0e\xe9K\xa8n\xf39i\x17\xfe\n2+|\xd1q\xcc\x87\xd2\xe9\xa9\xe8 \x9b\xd9",
595640
# COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
596-
COSEKeyParams.KEY_OPS: [2],
641+
COSEKeyParams.KEY_OPS: [
642+
COSEKeyOps.VERIFY,
643+
],
597644
},
598645
"Unsupported or unknown alg used with X25519/X448: -8.",
599646
),
600647
(
601648
{
602649
COSEKeyParams.KTY: 1,
603650
COSEKeyParams.CRV: 6,
604-
COSEKeyParams.KEY_OPS: [7, 8],
651+
COSEKeyParams.KEY_OPS: [
652+
COSEKeyOps.DERIVE_KEY,
653+
COSEKeyOps.DERIVE_BITS,
654+
],
605655
},
606656
"The body of the key not found.",
607657
),
@@ -611,7 +661,10 @@ def test_okp_key_derive_bytes_with_raw_context(self):
611661
# COSEKeyParams.ALG: -8,
612662
COSEKeyParams.CRV: 6,
613663
COSEKeyParams.D: b"B\xc6u\xd0|-\x07\xe7)\x8d\x1c\x13\x14\xa2\x8dFC1\xdf3sQ\x049|\x14\xc1\xed\x01\xe5\xdb\xa9",
614-
COSEKeyParams.KEY_OPS: [7, 8],
664+
COSEKeyParams.KEY_OPS: [
665+
COSEKeyOps.DERIVE_KEY,
666+
COSEKeyOps.DERIVE_BITS,
667+
],
615668
},
616669
"x(-2) not found.",
617670
),

0 commit comments

Comments
 (0)