Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion edhoc/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import cbor2
from cose.algorithms import AESCCM1664128, Sha256, EdDSA, AESCCM16128128, Es256, A128GCM, A256GCM, Sha384, Es384
from cose.curves import X25519, Ed25519, P256, P384
from cose.keys.curves import X25519, Ed25519, P256, P384

from edhoc.exceptions import EdhocException

Expand Down
16 changes: 10 additions & 6 deletions edhoc/messages/message1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@


class MessageOne(EdhocMessage):
METHOD_CORR = 0
CIPHERS = 1
G_X = 2
CONN_ID = 3
AAD1 = 4
C_1 = 0
METHOD_CORR = 1
CIPHERS = 2
G_X = 3
CONN_ID = 4
AAD1 = 5

@classmethod
def decode(cls, received: bytes) -> 'MessageOne':
Expand All @@ -33,6 +34,9 @@ def decode(cls, received: bytes) -> 'MessageOne':

method_corr = decoded[cls.METHOD_CORR]

if decoded[cls.C_1] is not None:
raise EdhocInvalidMessage("No leading nil")

if isinstance(decoded[cls.CIPHERS], int):
selected_cipher = decoded[cls.CIPHERS]
supported_ciphers = [decoded[cls.CIPHERS]]
Expand Down Expand Up @@ -109,7 +113,7 @@ def encode(self, corr: Correlation) -> bytes:
else:
raise ValueError('Cipher suite list must contain at least 1 item.')

msg = [self.method_corr, suites, self.g_x, self.encode_bstr_id(self.conn_idi)]
msg = [None, self.method_corr, suites, self.g_x, self.encode_bstr_id(self.conn_idi)]

if self.aad1 != b'':
msg.append(cbor2.dumps(self.aad1))
Expand Down
14 changes: 7 additions & 7 deletions edhoc/roles/edhoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import cbor2
from cose import headers
from cose.curves import X25519, X448, P256
from cose.exceptions import CoseIllegalCurve
from cose.keys.curves import X25519, X448, P256, P384
from cose.exceptions import CoseUnsupportedCurve
from cose.headers import CoseHeaderAttribute
from cose.keys import OKPKey, EC2Key, SymmetricKey
from cose.keys.keyops import EncryptOp
Expand All @@ -15,7 +15,6 @@
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, hmac, serialization
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.asymmetric.ec import SECP256R1
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey, X25519PublicKey
from cryptography.hazmat.primitives.asymmetric.x448 import X448PublicKey, X448PrivateKey
from cryptography.hazmat.primitives.kdf.hkdf import HKDFExpand
Expand Down Expand Up @@ -133,16 +132,17 @@ def shared_secret(private_key: 'CK', public_key: 'CK') -> bytes:

x = X448PublicKey.from_public_bytes(public_key.x)
secret = d.exchange(x)
elif public_key.crv == P256:
d = ec.derive_private_key(int(hexlify(private_key.d), 16), SECP256R1(), default_backend())
elif public_key.crv in (P256, P384):
curve_obj = public_key.crv.curve_obj()
d = ec.derive_private_key(int(hexlify(private_key.d), 16), curve_obj, default_backend())

x = ec.EllipticCurvePublicNumbers(int(hexlify(public_key.x), 16),
int(hexlify(public_key.y), 16),
SECP256R1())
curve_obj)
x = x.public_key()
secret = d.exchange(ec.ECDH(), x)
else:
raise CoseIllegalCurve(f"{public_key.crv} is unsupported")
raise CoseUnsupportedCurve(f"{public_key.crv} is unsupported")

return secret

Expand Down
2 changes: 1 addition & 1 deletion edhoc/roles/initiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cbor2
from asn1crypto.x509 import Certificate
from cose import headers
from cose.curves import X448, X25519
from cose.keys.curves import X448, X25519
from cose.headers import KID
from cose.keys import OKPKey, EC2Key
from cose.keys.keyops import EncryptOp
Expand Down
4 changes: 2 additions & 2 deletions edhoc/roles/responder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cbor2
from asn1crypto.x509 import Certificate
from cose import headers
from cose.curves import X25519, X448
from cose.keys.curves import X25519, X448
from cose.headers import KID
from cose.keys import OKPKey, EC2Key
from cose.keys.keyops import DecryptOp
Expand Down Expand Up @@ -220,7 +220,7 @@ def finalize(self, message_three: bytes) -> Union[Tuple[bytes, bytes, int, int],
self.cred_idi = decoded[0]

if not self._verify_signature_or_mac3(signature_or_mac3=decoded[1]):
return MessageError(err_msg='').encode()
return MessageError(err_msg='Signature verification failed').encode()

try:
ad_3 = decoded[2]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cbor2>=5.2.0
cryptography>=3.2.1
cose>=0.9dev5
cose>=0.9dev7
aiocoap
asn1crypto

Expand Down