-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from dajiaji/add-raw-key
Add raw key.
- Loading branch information
Showing
8 changed files
with
108 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import Any, Dict | ||
|
||
from ..cose_key_interface import COSEKeyInterface | ||
|
||
|
||
class RawKey(COSEKeyInterface): | ||
def __init__(self, params: Dict[int, Any]): | ||
super().__init__(params) | ||
|
||
self._key: bytes = b"" | ||
self._alg = None | ||
|
||
# Validate kty. | ||
if params[1] != 4: | ||
raise ValueError("kty(1) should be Symmetric(4).") | ||
|
||
# Validate k. | ||
if -1 not in params: | ||
raise ValueError("k(-1) should be set.") | ||
if not isinstance(params[-1], bytes): | ||
raise ValueError("k(-1) should be bytes(bstr).") | ||
self._key = params[-1] | ||
|
||
@property | ||
def key(self) -> bytes: | ||
return self._key | ||
|
||
def to_dict(self) -> Dict[int, Any]: | ||
res = super().to_dict() | ||
res[-1] = self._key | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
Tests for RawKey. | ||
""" | ||
import pytest | ||
|
||
from cwt.algs.raw import RawKey | ||
|
||
|
||
class TestRawKey: | ||
""" | ||
Tests for RawKey. | ||
""" | ||
|
||
def test_raw_key_constructor(self): | ||
key = RawKey( | ||
{ | ||
1: 4, | ||
-1: b"mysecret", | ||
} | ||
) | ||
assert key.key == b"mysecret" | ||
assert key.alg is None | ||
assert key.key_ops == [] | ||
assert key.base_iv is None | ||
|
||
@pytest.mark.parametrize( | ||
"invalid, msg", | ||
[ | ||
( | ||
{1: 2}, | ||
"kty(1) should be Symmetric(4).", | ||
), | ||
( | ||
{1: 4}, | ||
"k(-1) should be set.", | ||
), | ||
( | ||
{1: 4, -1: 123}, | ||
"k(-1) should be bytes(bstr).", | ||
), | ||
], | ||
) | ||
def test_symmetric_key_constructor_with_invalid_args(self, invalid, msg): | ||
with pytest.raises(ValueError) as err: | ||
RawKey(invalid) | ||
pytest.fail("SymmetricKey should fail.") | ||
assert msg in str(err.value) | ||
|
||
def test_raw_key_to_dict(self): | ||
key = RawKey( | ||
{ | ||
1: 4, | ||
-1: b"mysecret", | ||
} | ||
) | ||
k = key.to_dict() | ||
assert k[1] == 4 | ||
assert k[-1] == b"mysecret" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters