Skip to content

Commit 217872b

Browse files
committed
RsaOaepImpl and RsaOaepCtx now working. need to plumb in to Manifest and the apps.
1 parent c14ad26 commit 217872b

20 files changed

+503
-233
lines changed

ccnpy/core/DisplayFormatter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
class DisplayFormatter:
2222
@classmethod
2323
def hexlify(cls, value):
24+
if value is None:
25+
return "None"
2426
return str(binascii.hexlify(value), 'utf-8')
2527

2628
@classmethod

ccnpy/core/TlvType.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class constructor and the class is the corresponding TlvType. `auto_parse` will
7777
return cls.auto_value_parse(tlv.value(), name_class_pairs)
7878

7979
@staticmethod
80-
def auto_value_parse(tlv_value, name_class_pairs):
80+
def auto_value_parse(tlv_value, name_class_pairs, skip_unknown: bool = True):
8181
"""
8282
Like `auto_parse`, but only parses the value after we've verified the outer class_type.
8383
"""
@@ -101,7 +101,9 @@ def auto_value_parse(tlv_value, name_class_pairs):
101101
assert values[arg_name] is None
102102
values[arg_name] = clazz.parse(inner_tlv)
103103
except KeyError:
104-
raise ParseError("Unsupported TLV type %r" % inner_tlv)
104+
if not skip_unknown:
105+
raise ParseError("Unsupported TLV type %r" % inner_tlv)
106+
105107
return values
106108

107109

@@ -145,6 +147,9 @@ class OctetTlvType(TlvType, ABC):
145147
def __init__(self, value):
146148
TlvType.__init__(self)
147149

150+
if value is None:
151+
raise ValueError(f"Nonce value must not be None, use an empty list")
152+
148153
if isinstance(value, list):
149154
value = array.array("B", value)
150155

ccnpy/crypto/AeadKey.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class AeadKey(ABC):
3030
AeadKey does not have a concept of salt, only an IV. It is up to the user of the class to
3131
handle salt, if used. `flic.aeadctx.AeadImpl` is where we find salt.
3232
"""
33-
33+
DEBUG = False
3434
__salt_length = 128
3535

3636
def __init__(self, key, algo):
@@ -43,6 +43,7 @@ def __init__(self, key, algo):
4343
self._key_bits = len(key) * 8
4444
self._algo = algo
4545
self._impl = algo(key)
46+
self._key = key
4647

4748
def __len__(self):
4849
return self._key_bits
@@ -94,6 +95,8 @@ def encrypt(self, iv, plaintext, associated_data):
9495
output = self._impl.encrypt(iv, plaintext, associated_data)
9596
ciphertext = array.array("B", output[:-self._tag_len])
9697
authtag = array.array("B", output[len(ciphertext):])
98+
if self.DEBUG:
99+
print(f"Encrypt: iv: {iv}, data: {associated_data}, authtag: {authtag}")
97100
return ciphertext, authtag
98101

99102
def decrypt(self, iv, ciphertext, associated_data, auth_tag):
@@ -118,11 +121,14 @@ def decrypt(self, iv, ciphertext, associated_data, auth_tag):
118121

119122
combined = ciphertext + auth_tag
120123

124+
if self.DEBUG:
125+
print(f"Decrypt: iv: {iv}, data: {associated_data}, authtag: {auth_tag}")
126+
121127
try:
122128
plaintext = self._impl.decrypt(iv, combined, associated_data)
123129
return array.array("B", plaintext)
124130
except InvalidTag as e:
125-
print("Decryption failed. Either the key or salt is incorrect for the packet.")
131+
print(f"Decryption failed due to tag mismatch. Either the key or salt is incorrect for the packet.")
126132
# translate a Cryptography package exception into our own exception
127133
raise DecryptionError(e)
128134

ccnpy/crypto/InsecureKeystore.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
from ccnpy.flic.tlvs.KeyNumber import KeyNumber
1919

2020

21+
class KeyIdNotFoundError(RuntimeError):
22+
pass
23+
24+
class KeyNumberNotFoundError(RuntimeError):
25+
pass
26+
27+
2128
class InsecureKeystore:
2229
"""
2330
This is a prototype keystore for symmetric and asymmetric keys. It is not secure. It should not be used
@@ -43,19 +50,31 @@ def add_aes_key(self, key_num: KeyNumber | int, key: AeadKey, salt: Optional[int
4350
return self
4451

4552
def get_aes_key(self, key_num: KeyNumber) -> AeadKey:
46-
return self._symmetric_by_keynum[key_num.value()]
53+
try:
54+
return self._symmetric_by_keynum[key_num.value()]
55+
except KeyError as e:
56+
raise KeyNumberNotFoundError(e)
4757

4858
def get_aes_salt(self, key_num: KeyNumber):
49-
return self._salt_by_keynum[key_num.value()]
59+
try:
60+
return self._salt_by_keynum[key_num.value()]
61+
except KeyError as e:
62+
raise KeyNumberNotFoundError(e)
5063

5164
def get_rsa(self, name_or_keyid) -> RsaKey:
5265
if name_or_keyid in self._asymmetric_by_keyid:
5366
return self._asymmetric_by_keyid[name_or_keyid]
54-
return self._asymmetric_by_name[name_or_keyid]
67+
try:
68+
return self._asymmetric_by_name[name_or_keyid]
69+
except KeyError as e:
70+
raise KeyIdNotFoundError(e)
5571

5672
def get_rsa_pub_key(self, keyid) -> RsaKey:
57-
k = self._asymmetric_by_keyid[keyid]
58-
if k.has_public_key():
59-
return k
60-
else:
61-
raise ValueError(f'Key matching keyid {keyid} has no public key')
73+
try:
74+
k = self._asymmetric_by_keyid[keyid]
75+
if k.has_public_key():
76+
return k
77+
else:
78+
raise KeyIdNotFoundError(f'Key matching keyid {keyid} has no public key')
79+
except KeyError as e:
80+
raise KeyIdNotFoundError(e)

ccnpy/flic/LeafOnlyManifestTree.py

Lines changed: 0 additions & 115 deletions
This file was deleted.

ccnpy/flic/ManifestEncryptor.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ class ManifestEncryptor(ABC):
2020
Abstract class used to sign a Packet.
2121
"""
2222
@abstractmethod
23-
def encrypt(self, node):
23+
def encrypt(self, node, **kwargs):
2424
"""
2525
Returns an encrypted Manifest
2626
:param node: The plaintext Node
27+
:param kwargs: Some encryptors may take optional arguments
2728
:return: The tuple (security_ctx, encrypted_node, auth_tag)
2829
"""
2930
pass
30-
31-
def salt_size(self):
32-
"""0 if no salt, otherwise the bytes of salt"""
33-
return 0

ccnpy/flic/RsaOaepCtx/RsaOaepEncryptor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class RsaOaepEncryptor(ManifestEncryptor):
2626

2727
@classmethod
28-
def create_with_new_key(cls, wrapping_key: RsaKey):
28+
def create_with_new_content_key(cls, wrapping_key: RsaKey):
2929
"""
3030
Creates with a random content encryption key and salt.
3131
@@ -41,5 +41,5 @@ def __init__(self, wrapping_key: RsaKey, key: AeadKey, key_number: KeyNumber, sa
4141
self._wrapper = RsaOaepWrapper.create_sha256(key_id=wrapping_key.keyid(), wrapped_key=self._wrapped_key)
4242
self._impl = RsaOaepImpl(wrapper=self._wrapper, key=key, key_number=key_number, salt=salt)
4343

44-
def encrypt(self, node):
45-
return self._impl.encrypt(node)
44+
def encrypt(self, node, **kwargs):
45+
return self._impl.encrypt(node, **kwargs)

0 commit comments

Comments
 (0)