Deriving DH shared secret

46 Views Asked by At

I'm quite new to cypto and I'm trying to derive DH shared secret between two parties. One part is in OP-TEE (a TA) and other part is a remote server. This is my public and private key values in the TA.

static const uint8_t derive_key_dh_public_value[] = {
    0x46, 0x3b, 0x61, 0x4, 0x74, 0x26, 0x81, 0x71, 0xee, 0xba, 0x8b, 0xe, 0xe, 0x34, 0xd, 0x39,
    0x8f, 0xb3, 0xa1, 0x44, 0x63, 0x19, 0x8f, 0x66, 0x6b, 0x80, 0x48, 0x55, 0x8b, 0x96, 0x65, 0x3c,
    0x4a, 0x69, 0x48, 0xbd, 0x65, 0xa5, 0xa5, 0x5c, 0x9, 0x12, 0x30, 0x86, 0xcd, 0x68, 0x11, 0x24,
    0x6, 0x68, 0xb1, 0x36, 0x47, 0x1a, 0x19, 0xb2, 0x5c, 0xb, 0x92, 0x29, 0x2d, 0xa6, 0x1f, 0x22,
    0xc0, 0x36, 0x4e, 0xcd, 0x1, 0x68, 0xcb, 0xd9, 0xd4, 0x1b, 0xae, 0xf9, 0x4b, 0x70, 0x61, 0x7c,
    0x7c, 0xb8, 0x42, 0xf0, 0xaa, 0xd9, 0x5b, 0xd2, 0x37, 0x84, 0x70, 0x1d, 0x18, 0x4a, 0xd4, 0xf3,
    0xac, 0x95, 0x6e, 0xd7, 0xea, 0x70, 0x66, 0xd6, 0x20, 0x27, 0x27, 0xbe, 0x40, 0x62, 0x96, 0xdb,
    0x6e, 0xa7, 0xc4, 0x7, 0xaa, 0x75, 0x86, 0xb5, 0xc9, 0x44, 0xa1, 0xa8, 0x9f, 0xe0, 0xc6, 0x4d,
    0x71, 0x15, 0x80, 0x83, 0x5, 0x5, 0x15, 0xfc, 0xba, 0x56, 0x52, 0xd4, 0x25, 0xcd, 0x75, 0x88,
    0xcc, 0xce, 0x9d, 0x5e, 0xdf, 0xef, 0xc1, 0x58, 0xfc, 0xc6, 0x91, 0xc3, 0xcd, 0x72, 0xfe, 0xa5,
    0x36, 0xdd, 0x68, 0x90, 0x66, 0xea, 0x38, 0x2a, 0x1a, 0x6, 0xb, 0x38, 0xb5, 0xaf, 0x58, 0x7e,
    0x39, 0x9c, 0x6c, 0xab, 0x1f, 0xd5, 0x0, 0xb4, 0x69, 0x63, 0xa4, 0x8b, 0xa3, 0xf1, 0x1e, 0xef,
    0x97, 0x28, 0x32, 0x7a, 0xd0, 0x1c, 0xa3, 0x5d, 0xf, 0x93, 0xa, 0x6b, 0x90, 0x78, 0x6b, 0xf0,
    0x1d, 0xed, 0xeb, 0x85, 0x46, 0x54, 0x76, 0x32, 0x15, 0xbe, 0x96, 0x8e, 0xe2, 0x70, 0x60, 0x16,
    0xae, 0xf7, 0xe5, 0x29, 0xc3, 0x70, 0xf0, 0x6b, 0x4c, 0xb7, 0xf0, 0x7c, 0x7a, 0xc8, 0xee, 0x38,
    0x7f, 0x5d, 0xf4, 0x28, 0x64, 0x94, 0x59, 0x43, 0xcd, 0x57, 0x87, 0x33, 0x7b, 0xae, 0xee, 0xfe
};


static const uint8_t derive_key_dh_private_value[] = {
    0x14, 0x4d, 0x4a, 0x39, 0xbc, 0xa5, 0x3, 0xcf, 0x50, 0xd0, 0x6e, 0x37, 0xf7, 0xc8, 0x40, 0x63,
    0x56, 0x5b, 0xb2, 0xe3, 0xb3, 0x81, 0x1e, 0x39, 0xb1, 0x9b, 0xaf, 0x19, 0xee, 0xd3, 0x84, 0xbf,
    0x2, 0xa9, 0xd3, 0x24, 0xb8, 0x5f, 0xfa, 0xe1, 0x26, 0xe5, 0x8e, 0x37, 0xb9, 0x11, 0xa9, 0x79,
    0xbb, 0xd5, 0x91, 0x12, 0x71, 0xd8, 0xe6, 0x9d, 0x76, 0x7, 0x21, 0x15, 0x7b, 0xfd, 0xe5, 0x7a,
    0x52, 0xee, 0x3c, 0x86, 0x31, 0x47, 0x6b, 0x96, 0x10, 0xea, 0x22, 0xbc, 0x7c, 0xa4, 0x79, 0xb2,
    0x1d, 0x8e, 0x55, 0xc1, 0xdc, 0x18, 0x5d, 0x35, 0x68, 0x46, 0x9d, 0x69, 0x63, 0x68, 0xfa, 0x67,
    0xf6, 0x64, 0x70, 0x4a, 0x62, 0xbf, 0xb2, 0x2d, 0xd5, 0x44, 0x31, 0x28, 0x54, 0x86, 0xf6, 0x30,
    0x52, 0x5c, 0x6e, 0x9b, 0x4b, 0x39, 0x15, 0xe7, 0x5, 0x5a, 0x7f, 0x64, 0x9, 0x24, 0x27, 0xc1
};

In the remote server side I'm using this peer public key like this:

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import dh

derive_key_dh_public_value = '463b614074268171eeba8b0e0e340d398fb3a14463198f666b8048558b96653c4a6948bd65a5a55c909123086cd681124668b136471a19b25cb9292da61f22c0364ecd0168cbd9d41baef94b70617c7cb842f0aad95bd23784701d184ad4f3ac956ed7ea7066d6202727be406296db6ea7c407aa7586b5c944a1a89fe0c64d711580835055fcba5652d425cd7588ccce9d5edfefc158fcc691c3cd72fea536dd689066ea382a1a60b38b5af587e399c6cab1fd500b46963a48ba3f11eef9728327ad01ca35d0f93a6b90786bf01dedeb854654763215be968ee2706016aef7e529c370f06b4cb7f07c7ac8ee387f5df42864945943cd5787337baeee'

public_key_int = int.from_bytes(bytes.fromhex(derive_key_dh_public_value), byteorder='big')

# Standard DH parameters for 2048-bit MODP Group from RFC 3526 (Group 14)
parameters = parameters_numbers.parameters(default_backend())

public_numbers = dh.DHPublicNumbers(public_key_int, parameters.parameter_numbers())
public_key = public_numbers.public_key(backend=default_backend())

I'm using the correct base = 5!

An I'm using hazmat library in Python to generate and derive the shared secret. I'm using the same p and g used in my OP-TEE TA. The generated public key in the server is:

from cryptography.hazmat.primitives import serialization

keygen_dh2048_p_bytes = bytes.fromhex("8F3EC136CA60CED1C5FD2205D69438204FE1AFBCA682BD71FDD6C261E8C1BDA95EFD0251B61F3830447694B3267935C4DF5180AF0D81CCA233D11E77B806D7E5833404F2962437FEBC209C664CEB4FFD1F995640D9E86B2A8D6B56B2B46D83474C18538BB1A351C207A336434B9410FD24A3777477FA984B430BB6EC7F5C7EBAB7C7AA72119F731445037A4EE5E75C64B86666CEEEF8FF610F5D4EF6EDB1E52F52AC2B8F340D13F64A3A6C56B33C52A8B9BC27CA3BFB6EE752FBB02B4FC4BD2436E4710774695FE0B8595F742FCC03B06D90D8D37C5A31465C7D1CC80D18808E5AA85E4D112B76AC1E005180E3ED7AC04F80FA5FD5D74FA714E1603C9577CA3B")
keygen_dh2048_g_bytes = bytes.fromhex("05")

# Load the parameters
parameters_numbers = dh.DHParameterNumbers(
    p=int.from_bytes(keygen_dh2048_p_bytes, "big"),
    g=int.from_bytes(keygen_dh2048_g_bytes, "big")
)


def generate_dh_key_pair():
    parameters = parameters = parameters_numbers.parameters(default_backend())
    private_key = parameters.generate_private_key()
    public_key = private_key.public_key()
    return private_key, public_key

def serialize_public_key(public_key):
    return public_key.public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.SubjectPublicKeyInfo
    )
def format_bytes_as_c_array(byte_array):
    return ', '.join(['0x{:02x}'.format(b) for b in byte_array])

private_key, public_key = generate_dh_key_pair()
    alice_public_key_bytes = serialize_public_key(public_key)
    formatted_public_key = format_bytes_as_c_array(alice_public_key_bytes)

And I'm using this peer public key in TA in the following format:

static const uint8_t dh_key[] = {
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 
0x20, 0x50, 0x55, 0x42, 0x4c, 0x49, 0x43, 0x20, 0x4b, 0x45, 
0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 
0x43, 0x4a, 0x44, 0x43, 0x43, 0x41, 0x52, 0x63, 0x47, 0x43, 
0x53, 0x71, 0x47, 0x53, 0x49, 0x62, 0x33, 0x44, 0x51, 0x45, 
0x44, 0x41, 0x54, 0x43, 0x43, 0x41, 0x51, 0x67, 0x43, 0x67, 
0x67, 0x45, 0x42, 0x41, 0x4b, 0x58, 0x39, 0x66, 0x62, 0x50, 
0x52, 0x6b, 0x4a, 0x75, 0x4e, 0x44, 0x34, 0x72, 0x70, 0x7a, 
0x78, 0x30, 0x70, 0x59, 0x46, 0x38, 0x36, 0x51, 0x46, 0x64, 
0x38, 0x0a, 0x54, 0x61, 0x76, 0x42, 0x65, 0x35, 0x5a, 0x69, 
0x65, 0x67, 0x35, 0x36, 0x33, 0x58, 0x76, 0x64, 0x67, 0x76, 
0x50, 0x67, 0x54, 0x62, 0x54, 0x71, 0x49, 0x74, 0x56, 0x76, 
0x30, 0x46, 0x43, 0x4b, 0x50, 0x49, 0x4a, 0x4e, 0x69, 0x68, 
0x39, 0x7a, 0x6b, 0x36, 0x76, 0x67, 0x39, 0x48, 0x48, 0x75, 
0x48, 0x6c, 0x48, 0x53, 0x72, 0x44, 0x59, 0x63, 0x4a, 0x47, 
0x73, 0x67, 0x32, 0x66, 0x49, 0x73, 0x0a, 0x39, 0x70, 0x65, 
0x6d, 0x56, 0x6c, 0x75, 0x6c, 0x68, 0x6d, 0x33, 0x47, 0x57, 
0x77, 0x37, 0x50, 0x41, 0x54, 0x36, 0x75, 0x6d, 0x61, 0x4d, 
0x59, 0x55, 0x4c, 0x55, 0x35, 0x77, 0x35, 0x79, 0x30, 0x63, 
0x32, 0x56, 0x4e, 0x70, 0x5a, 0x52, 0x34, 0x6a, 0x56, 0x5a, 
0x68, 0x45, 0x6d, 0x4a, 0x71, 0x73, 0x4a, 0x66, 0x4e, 0x47, 
0x48, 0x53, 0x49, 0x77, 0x65, 0x70, 0x31, 0x73, 0x54, 0x7a, 
0x52, 0x0a, 0x6e, 0x37, 0x31, 0x43, 0x39, 0x54, 0x39, 0x73, 
0x68, 0x4f, 0x75, 0x66, 0x36, 0x64, 0x76, 0x6d, 0x53, 0x6e, 
0x46, 0x65, 0x61, 0x51, 0x42, 0x54, 0x56, 0x31, 0x6e, 0x70, 
0x6e, 0x68, 0x50, 0x45, 0x65, 0x51, 0x41, 0x6f, 0x31, 0x2f, 
0x38, 0x53, 0x6d, 0x61, 0x4a, 0x4d, 0x43, 0x49, 0x36, 0x5a, 
0x54, 0x4f, 0x77, 0x4b, 0x69, 0x36, 0x34, 0x53, 0x4e, 0x72, 
0x38, 0x41, 0x71, 0x7a, 0x45, 0x72, 0x0a, 0x38, 0x50, 0x6c, 
0x70, 0x75, 0x57, 0x50, 0x6b, 0x36, 0x49, 0x49, 0x66, 0x66, 
0x77, 0x6d, 0x53, 0x46, 0x55, 0x2b, 0x53, 0x50, 0x4b, 0x54, 
0x7a, 0x68, 0x34, 0x52, 0x31, 0x38, 0x78, 0x4b, 0x55, 0x35, 
0x68, 0x67, 0x4d, 0x44, 0x7a, 0x76, 0x53, 0x71, 0x30, 0x41, 
0x72, 0x39, 0x47, 0x4b, 0x4c, 0x61, 0x48, 0x6b, 0x63, 0x77, 
0x6b, 0x43, 0x48, 0x73, 0x67, 0x77, 0x34, 0x63, 0x6f, 0x58, 
0x74, 0x0a, 0x30, 0x41, 0x33, 0x5a, 0x73, 0x36, 0x55, 0x56, 
0x6c, 0x51, 0x67, 0x6e, 0x43, 0x64, 0x79, 0x2f, 0x64, 0x50, 
0x5a, 0x75, 0x74, 0x71, 0x4a, 0x5a, 0x4b, 0x7a, 0x69, 0x70, 
0x37, 0x55, 0x6b, 0x4c, 0x41, 0x4a, 0x65, 0x73, 0x71, 0x53, 
0x30, 0x34, 0x4c, 0x75, 0x34, 0x72, 0x38, 0x2b, 0x4b, 0x41, 
0x56, 0x54, 0x67, 0x52, 0x34, 0x65, 0x64, 0x32, 0x36, 0x4c, 
0x4d, 0x43, 0x41, 0x51, 0x49, 0x44, 0x0a, 0x67, 0x67, 0x45, 
0x46, 0x41, 0x41, 0x4b, 0x43, 0x41, 0x51, 0x42, 0x4b, 0x6b, 
0x42, 0x31, 0x4c, 0x43, 0x35, 0x36, 0x30, 0x4a, 0x68, 0x77, 
0x71, 0x6a, 0x61, 0x38, 0x51, 0x4d, 0x70, 0x6a, 0x73, 0x36, 
0x6d, 0x6a, 0x6c, 0x4b, 0x37, 0x79, 0x79, 0x4b, 0x66, 0x72, 
0x4a, 0x42, 0x58, 0x4b, 0x67, 0x34, 0x47, 0x38, 0x6b, 0x67, 
0x78, 0x76, 0x56, 0x79, 0x59, 0x69, 0x48, 0x79, 0x44, 0x45, 
0x33, 0x0a, 0x36, 0x43, 0x57, 0x62, 0x50, 0x33, 0x62, 0x4e, 
0x71, 0x49, 0x4f, 0x72, 0x71, 0x36, 0x6c, 0x30, 0x77, 0x56, 
0x70, 0x6c, 0x58, 0x38, 0x4c, 0x42, 0x55, 0x33, 0x72, 0x70, 
0x2f, 0x57, 0x6b, 0x74, 0x70, 0x79, 0x4b, 0x75, 0x6a, 0x41, 
0x70, 0x52, 0x6b, 0x64, 0x47, 0x75, 0x41, 0x64, 0x38, 0x72, 
0x70, 0x37, 0x41, 0x5a, 0x52, 0x58, 0x4d, 0x50, 0x65, 0x6a, 
0x51, 0x6a, 0x39, 0x34, 0x6a, 0x69, 0x0a, 0x52, 0x52, 0x4e, 
0x4e, 0x74, 0x32, 0x4b, 0x42, 0x71, 0x37, 0x73, 0x4b, 0x41, 
0x71, 0x45, 0x6e, 0x68, 0x65, 0x47, 0x45, 0x46, 0x7a, 0x36, 
0x62, 0x56, 0x79, 0x32, 0x6e, 0x78, 0x42, 0x6e, 0x30, 0x39, 
0x41, 0x35, 0x33, 0x58, 0x75, 0x75, 0x64, 0x4b, 0x7a, 0x49, 
0x6b, 0x61, 0x4b, 0x71, 0x4d, 0x34, 0x34, 0x50, 0x69, 0x41, 
0x48, 0x62, 0x46, 0x61, 0x57, 0x42, 0x6e, 0x68, 0x42, 0x67, 
0x6e, 0x0a, 0x35, 0x77, 0x53, 0x64, 0x76, 0x4b, 0x2f, 0x70, 
0x36, 0x77, 0x30, 0x4d, 0x4f, 0x55, 0x6d, 0x54, 0x6d, 0x35, 
0x6d, 0x56, 0x2b, 0x4d, 0x43, 0x71, 0x65, 0x44, 0x73, 0x6b, 
0x49, 0x33, 0x39, 0x63, 0x66, 0x51, 0x4e, 0x57, 0x59, 0x70, 
0x72, 0x44, 0x72, 0x45, 0x74, 0x44, 0x4b, 0x5a, 0x33, 0x72, 
0x6e, 0x56, 0x36, 0x37, 0x33, 0x4b, 0x4d, 0x55, 0x67, 0x64, 
0x54, 0x4e, 0x69, 0x4d, 0x61, 0x6d, 0x0a, 0x49, 0x31, 0x7a, 
0x75, 0x72, 0x2f, 0x6c, 0x6e, 0x73, 0x63, 0x2f, 0x56, 0x61, 
0x66, 0x30, 0x4e, 0x38, 0x41, 0x4a, 0x6c, 0x52, 0x43, 0x71, 
0x30, 0x6a, 0x4a, 0x39, 0x55, 0x4a, 0x49, 0x51, 0x6d, 0x43, 
0x42, 0x6e, 0x43, 0x70, 0x63, 0x61, 0x54, 0x6b, 0x64, 0x56, 
0x41, 0x75, 0x5a, 0x47, 0x34, 0x47, 0x61, 0x58, 0x38, 0x72, 
0x73, 0x75, 0x62, 0x56, 0x35, 0x44, 0x6a, 0x32, 0x72, 0x30, 
0x38, 0x0a, 0x70, 0x7a, 0x2f, 0x56, 0x38, 0x61, 0x7a, 0x71, 
0x36, 0x59, 0x4a, 0x6f, 0x58, 0x38, 0x31, 0x45, 0x5a, 0x76, 
0x52, 0x4a, 0x42, 0x4f, 0x43, 0x4a, 0x66, 0x63, 0x38, 0x61, 
0x6d, 0x6c, 0x36, 0x78, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 
0x45, 0x4e, 0x44, 0x20, 0x50, 0x55, 0x42, 0x4c, 0x49, 0x43, 
0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a
};

In remote python server:

shared_secret = derive_shared_secret(private_key, public_key)

The derived shared secret is not the same. I'm not sure where I'm doing wrong. Thanks for your help.

0

There are 0 best solutions below