Python Hazmat密码学从ECDH的输出自定义输入到HKDF

发布于 2025-01-27 17:55:48 字数 1013 浏览 4 评论 0原文

与我的ECDH密钥交易所的对应物为HKDF函数的输入密钥材料使用非标准形式。不仅是通常的情况,不仅是计算出的EC点或EC X坐标。相反,他们使用曲线点的合并和计算的点坐标。使用Python,我正在尝试嘲笑他们将要做什么,以便我可以测试我的解密代码。

我遇到的问题是“自定义” Python中HKDF函数的输入。我不知道Exchange()函数的输出是什么(变量shared_key)。使用type()告诉我“类似字节般的对象”,而Hazmat Crytography文档并未详细说明此输出值所代表的内容。我认为这是EC上的计算点,但不是从指示EC点的0x04或0x03字节开始。

这是我正在执行ECDH操作的Python函数:

from cryptography.hazmat.primitives.asymmetric import ec
    def eciesEncrypt(publicKey, plaintext, aad):
        epk = ec.generate_private_key(ec.SECP256R1())
        shared_key = epk.exchange(ec.ECDH(), publicKey)
        iv = os.urandom(12)
        
        derived_key = HKDF(
            algorithm=hashes.SHA256(),
            length=32,
            salt=None,
            info=None,
        ).derive(shared_key)
    
        cipherText = aesgcm256_encrypt_buffer(derived_key, iv, aad, plaintext)
        
        return iv, cipherText

基本上,我想了解代表shared_key的值是什么,以便我可以为我的用例形成适当的输入。谢谢。

The counterpart to my ECDH key exchange uses a non-standard form for the input key material to an HKDF function. Not simply the calculated EC point, or EC x-coordinate as is often the case. Rather they use an amalgamation of the Curve point and calculated point coordinates. Using Python I am trying to mock up what they will be doing so I can test my decryption code.

The problem I am having is with 'customizing' the input to the HKDF function in Python. I do not know what is the output of the exchange() function (variable shared_key). Using type() tells me "bytes-like object" and the Hazmat Crytography documentation does not elaborate on what this output value represents. I thought it is the calculated point on the EC but it does not start with the 0x04 or 0x03 byte indicating an EC point.

Here's my python function which is doing the ECDH operation:

from cryptography.hazmat.primitives.asymmetric import ec
    def eciesEncrypt(publicKey, plaintext, aad):
        epk = ec.generate_private_key(ec.SECP256R1())
        shared_key = epk.exchange(ec.ECDH(), publicKey)
        iv = os.urandom(12)
        
        derived_key = HKDF(
            algorithm=hashes.SHA256(),
            length=32,
            salt=None,
            info=None,
        ).derive(shared_key)
    
        cipherText = aesgcm256_encrypt_buffer(derived_key, iv, aad, plaintext)
        
        return iv, cipherText

Basically, I want to understand what is the value of shared_key representing so that I can form the proper input to HKDF for my use case. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文