如何将有效值传递到cleartext_keyset_json以创建Tink密钥

use*_*598 2 python cryptography tink

在 Tink 中,可以将明文密钥集加载和写入为 json。下面是一个非工作示例:

{
  "primaryKeyId": 2800579,
  "key": [
    {
      "keyData": {
        "typeUrl": "type.googleapis.com/google.crypto.tink.AesGcmKey",
        "value": "ODA9eJX9wcAGwZocL0Jym==",
        "keyMaterialType": "SYMMETRIC"
      },
      "status": "ENABLED",
      "keyId": 2800579,
      "outputPrefixType": "TINK"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的问题是 - 是否可以将您自己的值插入到各种键/值对中以获得另一个有效的键集?我已经对此进行了实验,但没有取得太大成功 - 主要是因为“value”键抱怨INVALID_ARGUMENT: Could not parse key_data.value as key type 'type.googleapis.com/google.crypto.tink.AesGcmKey'知道有效的“value”是什么吗?

Top*_*aco 7

首先,发布的代码片段中值字段的 Base64 字符串无效,可能是复制/粘贴错误。

以下 Python 代码使用Tink 版本 1.5.0并创建 AES-256/GCM 密钥集并将其显示为 JSON:

import io
from tink import aead
from tink import tink_config
from tink import JsonKeysetWriter
from tink import new_keyset_handle
from tink import cleartext_keyset_handle

tink_config.register()

key_template = aead.aead_key_templates.AES256_GCM
keyset_handle = new_keyset_handle(key_template)

string_out = io.StringIO()
writer = JsonKeysetWriter(string_out)
cleartext_keyset_handle.write(writer, keyset_handle)

serialized_keyset = string_out.getvalue();
print(serialized_keyset);
Run Code Online (Sandbox Code Playgroud)

结果类似于您发布的 KeySet,例如:

{
  "primaryKeyId": 1794775293,
  "key": [
    {
      "keyData": {
        "typeUrl": "type.googleapis.com/google.crypto.tink.AesGcmKey",
        "value": "GiD5ojApaIM2MRpPhGf5sVMhxeA6NE5KjdzUxsJ0ChH/JA==",
        "keyMaterialType": "SYMMETRIC"
      },
      "status": "ENABLED",
      "keyId": 1794775293,
      "outputPrefixType": "TINK"
    }
  ]
}   
Run Code Online (Sandbox Code Playgroud)

我还没有找到描述一般结构或值字段结构的文档,但比较不同算法生成的 KeySet 可以得出结论。如果是十六进制编码,则结果为:

1a20f9a23029688336311a4f8467f9b15321c5e03a344e4a8ddcd4c6c2740a11ff24
Run Code Online (Sandbox Code Playgroud)

对于 AES-256/GCM,它有 34 个字节,其中最后 32 个字节是实际密钥。开头是算法的特征,第二个字节表示密钥的大小,例如AES-128/GCM 为0x1a10 , AES-256/GCM 为0x1a20 ,ChaCha20Poly1305 为0x1220(但根据算法可能会更复杂)。

为 AES-256/GCM 使用自定义密钥,例如

000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 
Run Code Online (Sandbox Code Playgroud)

前面加上0x1a20,Base64 编码结果:

GiAAAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHw==
Run Code Online (Sandbox Code Playgroud)

并应用此值代替上述 KeySet 中的旧值。

修改后的KeySet可以加载并用于加密,如下所示:

from tink import JsonKeysetReader
from tink import cleartext_keyset_handle

serialized_keyset = '''
{
  "primaryKeyId": 1794775293,
  "key": [
    {
      "keyData": {
        "typeUrl": "type.googleapis.com/google.crypto.tink.AesGcmKey",
        "value": "GiAAAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHw==",
        "keyMaterialType": "SYMMETRIC"
      },
      "status": "ENABLED",
      "keyId": 1794775293,
      "outputPrefixType": "TINK"
    }
  ]
}   
'''
reader = JsonKeysetReader(serialized_keyset)
keyset_handle = cleartext_keyset_handle.read(reader)

plaintext = b'The quick brown fox jumps over the lazy dog'
aead_primitive = keyset_handle.primitive(aead.Aead)
tink_ciphertext = aead_primitive.encrypt(plaintext, b'')
Run Code Online (Sandbox Code Playgroud)

KeySet 和示例密钥0001...1e1f之间的关系可以通过使用示例密钥(无需Tink,例如使用 PyCryptodome)解密生成的密文来验证。

Tink 密文的格式在Tink Wire Format, Crypto Formats中描述。第一个字节指定版本,接下来的 4 个字节指定密钥 ID,后面是实际数据。
对于 GCM,实际数据的格式为随机数(12 字节)|| 密文|| 标签(16 字节)。然后可以使用(使用 PyCryptodome)进行解密:

from Crypto.Cipher import AES

key = bytes.fromhex('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f')

prefix = tink_ciphertext[:5]
nonce = tink_ciphertext[5:5 + 12]
ciphertext = tink_ciphertext[5 + 12:-16]
tag = tink_ciphertext[-16:]

cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
cipher.update(b'')
decryptedText = cipher.decrypt_and_verify(ciphertext, tag)

print(decryptedText.decode('utf-8')) # The quick brown fox jumps over the lazy dog
Run Code Online (Sandbox Code Playgroud)

这证明示例密钥0001...1e1f已正确集成到 KeySet 中。