-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move initial in-memory ECDH-1PU support; enable tests; rename methods…
… and variables for consistency Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
- Loading branch information
1 parent
5d2bbe2
commit c9f0aa5
Showing
12 changed files
with
149 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""In-memory wallet support.""" | ||
|
||
from .profile import InMemoryProfile, InMemoryProfileManager, InMemoryProfileSession | ||
|
||
__all__ = ["InMemoryProfile", "InMemoryProfileManager", "InMemoryProfileSession"] |
File renamed without changes.
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,32 @@ | ||
"""Functions for performing Key Agreement using ECDH-1PU.""" | ||
|
||
from .derive_ecdh import derive_shared_secret, concat_kdf | ||
|
||
|
||
def derive_1pu(ze, zs, alg, apu, apv, keydatalen): | ||
"""Generate shared encryption key from two ECDH shared secrets.""" | ||
|
||
z = ze + zs | ||
key = concat_kdf(z, alg, apu, apv, keydatalen) | ||
return key | ||
|
||
|
||
def derive_sender_1pu(epk, sender_sk, recip_pk, alg, apu, apv, keydatalen): | ||
"""Generate two shared secrets (ze, zs).""" | ||
|
||
ze = derive_shared_secret(epk, recip_pk) | ||
zs = derive_shared_secret(sender_sk, recip_pk) | ||
|
||
key = derive_1pu(ze, zs, alg, apu, apv, keydatalen) | ||
return key | ||
|
||
|
||
def derive_receiver_1pu(epk, sender_pk, recip_sk, alg, apu, apv, keydatalen): | ||
"""Generate two shared secrets (ze, zs).""" | ||
|
||
ze = derive_shared_secret(recip_sk, epk) | ||
zs = derive_shared_secret(recip_sk, sender_pk) | ||
|
||
key = derive_1pu(ze, zs, alg, apu, apv, keydatalen) | ||
|
||
return key |
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,68 @@ | ||
"""Functions for performing Key Agreement.""" | ||
|
||
import hashlib | ||
|
||
from binascii import unhexlify | ||
from typing import Union | ||
|
||
from ecdsa import ECDH, NIST256p | ||
|
||
|
||
def derive_shared_secret(private_key: bytes, public_key: bytes): | ||
"""Generate a shared secret from keys in byte format.""" | ||
|
||
derive = ECDH(curve=NIST256p) | ||
derive.load_private_key_bytes(unhexlify(private_key)) | ||
derive.load_received_public_key_bytes(unhexlify(public_key)) | ||
|
||
secret = derive.generate_sharedsecret_bytes() | ||
return secret | ||
|
||
|
||
def derive_shared_secret_from_key(private_key, public_key): | ||
"""Generate a shared secret from keys in ecdsa.Keys format.""" | ||
|
||
derive = ECDH(curve=NIST256p) | ||
derive.load_private_key(private_key) | ||
derive.load_received_public_key(public_key) | ||
|
||
secret = derive.generate_sharedsecret_bytes() | ||
return secret | ||
|
||
|
||
def _to_bytes(s: Union[str, bytes]) -> bytes: | ||
if isinstance(s, str): | ||
return s.encode("utf-8") | ||
return s | ||
|
||
|
||
def concat_kdf( | ||
shared_secret: bytes, | ||
alg: Union[str, bytes], | ||
apu: Union[str, bytes], | ||
apv: Union[str, bytes], | ||
keydatalen: int, | ||
): | ||
"""Generate a shared encryption key from a shared secret.""" | ||
|
||
alg = _to_bytes(alg) | ||
apu = _to_bytes(apu) | ||
apv = _to_bytes(apv) | ||
|
||
# ECDH-1PU requires a "round number 1" to be prefixed onto the shared secret z | ||
hasher = hashlib.sha256((1).to_bytes(4, "big")) | ||
# Ze + Zs | ||
hasher.update(shared_secret) | ||
# AlgId | ||
hasher.update(len(alg).to_bytes(4, "big")) | ||
hasher.update(alg) | ||
# PartyUInfo | ||
hasher.update(len(apu).to_bytes(4, "big")) | ||
hasher.update(apu) | ||
# PartyVInfo | ||
hasher.update(len(apv).to_bytes(4, "big")) | ||
hasher.update(apv) | ||
# SuppPubInfo | ||
hasher.update((keydatalen * 8).to_bytes(4, "big")) | ||
|
||
return hasher.digest() |
File renamed without changes.
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
Empty file.
3 changes: 1 addition & 2 deletions
3
...s_cloudagent/core/tests/test_in_memory.py → ...gent/core/in_memory/tests/test_profile.py
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.