首页 > 解决方案 > swift api SecKeyCreateEncryptedData 使用的额外认证数据是什么?

问题描述

我正在使用 rsaEncryptionOAEPSHA256AESGCM 在 iOS 上使用 SecKeyCreateEncryptedData 加密一些数据,然后在 golang 的后端解密相同的数据。我正在使用 3072 位 rsa 公钥来加密对称密钥。当我从 iOS 获取数据到后端时,我能够成功解密对称密钥,但 gcm 标签验证失败。我正在使用与 iOS 相同的 16 字节 IV,但不知道 iOS 在加密时是否使用了任何 aad(附加身份验证数据)。有谁知道 iOS 的 rsaEncryptionOAEPSHA256AESGCM 是否使用了一些 aad?这适用于 iOS 10+。

我已经尝试过使用 nil、空的 16 字节数组、aes 键本身、nonce 作为 aad,但这些都不起作用。

在斯威夫特:

private let algorithm = SecKeyAlgorithm.rsaEncryptionOAEPSHA256AESGCM
// Key is 3072 bit RSA public key
// API doesnt take any aad
SecKeyCreateEncryptedData(key, algorithm, cfData, &error)

在 Golang 中:

c, err := aes.NewCipher(aesKey)
gcm, err := cipher.NewGCMWithNonceSize(c, 16)
nonce := make([]byte, 16)
// Data has both the ciphertext and the gcm tag as the last 16 bytes
// Last field in the api is the aad
dec, err := gcm.Open(nil, nonce, data, nil)

这是我在 golang "cipher: message authentication failed" 中看到的错误,该错误是从验证 gcm 标签的代码中抛出的。

标签: iosswiftgoencryptionaes-gcm

解决方案


找到了。AAD 是 ASN.1 DER 格式的 RSA 公钥。


推荐阅读