0

背景:我正在从google tink 库制作一个 python 版本的paymentmethodtoken来处理 gpay 消息。为此,我只使用 python 和 PyCryptodome。

话虽如此,我目前正在尝试实现与kem 功能等效的功能

private byte[] kem(JsonObject json, final byte[] contextInfo) throws GeneralSecurityException {
    int demKeySize = protocolVersionConfig.aesCtrKeySize + protocolVersionConfig.hmacSha256KeySize;
    byte[] ephemeralPublicKey =
        Base64.decode(
            json.get(PaymentMethodTokenConstants.JSON_EPHEMERAL_PUBLIC_KEY).getAsString());
    byte[] sharedSecret = recipientKem.computeSharedSecret(ephemeralPublicKey);
    return Hkdf.computeEciesHkdfSymmetricKey(
        ephemeralPublicKey,
        sharedSecret,
        PaymentMethodTokenConstants.HMAC_SHA256_ALGO,
        PaymentMethodTokenConstants.HKDF_EMPTY_SALT,
        contextInfo,
        demKeySize);
  }

python中的等价物:

    def __kem(self, signed_message: SignedMessage, context_info: bytearray) -> bytearray:
        dem_key_size: int = 64
        ephemeral_public_key: bytes = base64.b64decode(signed_message.ephemeral_public_key)
        shared_secret: bytearray = self.__compute_shared_secret(bytearray(ephemeral_public_key))
        empty_salt: bytearray = bytearray()
        # to do
        return self.__compute_ecies_hkdf_symmetric_key(ephemeral_public_key, shared_secret, empty_salt, context_info, dem_key_size)
    def __compute_ecies_hkdf_symmetric_key(self, ephemeral_public_key: bytes, shared_secret: bytearray,
                                           salt: bytearray, context_info: bytearray, dem_key_size: int) -> bytearray:
        # TODO: add function body
        hkdf_input: bytes = ephemeral_public_key + shared_secret
        key1, key2 = HKDF(master=bytes("something goes here", "utf-8"), hashmod=SHA256, salt=salt, key_len=dem_key_size)

        pass

据我所知,google tink computeEciesHkdfSymmetricKey (代码) 根本不像PyCryptodome HKDF 那样工作。我的问题是,pycryptomde 或其他库中是否存在与 computeEciesHkdfSymmetricKey 等效的内容,如果没有,是否可以重现相同的行为?

4

1 回答 1

1

我做了什么:

from Crypto.Protocol.KDF import HKDF

def __compute_ecies_hkdf_symmetric_key(self, ephemeral_public_key: bytes, shared_secret: bytearray, salt: bytearray, context_info: bytearray, dem_key_size: int) -> bytearray:
    hkdf_input: bytes = ephemeral_public_key + shared_secret
    keys = HKDF(master=hkdf_input, hashmod=SHA256, salt=salt, key_len=dem_key_size, context=context_info)
    if isinstance(keys, bytes):
        return bytearray(keys)
    elif isinstance(keys, tuple(bytes)):
        return bytearray(keys[0])
    else:
        raise GooglePaymentDecryptMessageError("type of hkdf is not compatible")
于 2021-11-22T15:05:06.927 回答