예제 #1
0
def derive_hkdf_key(
    master_key: bytes,
    dklen: int,
    hashalgo: typing.Union[str, BaseHash],
    salt: bytes,
    cipher_ctx: bytes = b"enc-key",
    auth_ctx: bytes = b"auth-key",
) -> typing.Tuple[bytes, bytes]:
    """Derive key materials for HMAC from given master key.

    Args:
        master_key (bytes): The key used to derive the keys from.
        dklen (int): Desired lenth of the derived key.
        hashalgo (str, BaseHash): The name of the hash algorithm.
        salt (bytes): The salt to use.
        cipher_ctx (bytes): Context for cipher.
        auth_ctx (bytes): Context for HMAC.

    Returns:
        tuple[bytes, bytes]: A pair of *cipher key* and *MAC key*.
    """
    if isinstance(hashalgo, str):
        hash_ = Hash.new(hashalgo)
    elif isinstance(hashalgo, BaseHash):
        # use our hashalgo
        hash_ = hashalgo.new()
    else:
        raise TypeError(
            "hashalgo must be a str or an object implementing BaseHash."
        )

    key = KDF.HKDF(
        master=master_key,
        key_len=dklen,
        salt=salt,
        hashmod=hash_,
        num_keys=1,
        context=cipher_ctx,
    )

    hkey = KDF.HKDF(
        master=master_key,
        key_len=hash_.digest_size,
        salt=salt,
        hashmod=hash_,
        num_keys=1,
        context=auth_ctx,
    )
    return key, hkey
예제 #2
0
 def __init__(self, key):
     self.share_key = key
     #set counter for sending and receiving
     self.sender_counter = 0
     self.receiver_counter = 0
     #derive the encryption key and authentication key from share_key
     self.encry_key = (KDF.HKDF(self.share_key,
                                salt=None,
                                key_len=32,
                                hashmod=SHA256,
                                num_keys=2,
                                context=None))[0]
     self.auth_key = (KDF.HKDF(self.share_key,
                               salt=None,
                               key_len=32,
                               hashmod=SHA256,
                               num_keys=2,
                               context=None))[1]
예제 #3
0
 def __init__(self):
     self.name = 'Bob'
     self.key = RSA.generate(1024)
     self.publicKey = self.key.publickey()
     self.CAdic = {}
     self.Sb = 10
     self.share_key = ''
     self.certificate = None
     self.sender_counter = 0
     self.receiver_counter = 0
     self.encry_key = (KDF.HKDF(self.share_key,
                                salt=None,
                                key_len=32,
                                hashmod=SHA256,
                                num_keys=2,
                                context=None))[0]
     self.auth_key = (KDF.HKDF(self.share_key,
                               salt=None,
                               key_len=32,
                               hashmod=SHA256,
                               num_keys=2,
                               context=None))[1]
예제 #4
0
#print decryptor.decrypt_and_verify(binascii.unhexlify('16042f8a8df1c09dee68e56a1a1d9157ce8aaa8490d12a99e538f499eabab47b0f3578c16aecd9e4bbb0d8f52f0e4f0e'), tag)
#print decryptor.decrypt(binascii.unhexlify('16042f8a8df1c09dee68e56a1a1d9157ce8aaa8490d12a99e538f499eabab47b0f3578c16aecd9e4bbb0d8f52f0e4f0e'))
#16042f8a8df1c09dee68e56a1a1d9157
#ce8aaa8490d12a99e538f499eabab47b
#0f3578c16aecd9e4bbb0d8f52f0e4f0f

#===============================================
#print len(hashlib.sha256(format(0, 'x')).hexdigest()[:16])

from Cryptodome.Protocol import KDF
from Crypto.Hash import SHA256
master = "abcd"
array = (KDF.HKDF(master,
                  salt=None,
                  key_len=32,
                  hashmod=SHA256,
                  num_keys=2,
                  context=None))
#print len(array[0].encode('hex')), (binascii.hexlify(array[1]))
#
#print (("Msg from alice to bob").encode('hex'))
#print (("Msg from alice to bob").encode('hex') + (16 - len("Msg from alice to bob") % 16)*'80')


class Peer(object):
    def __init__(self, key):
        self.share_key = key
        #set counter for sending and receiving
        self.sender_counter = 0
        self.receiver_counter = 0
        #derive the encryption key and authentication key from share_key