swift api SecKeyCreateEncryptedData 使用的额外认证数据是什么?
What is the additional authenticated data used by swift api SecKeyCreateEncryptedData?
我正在使用 rsaEncryptionOAEPSHA256AESGCM 在 iOS 上使用 SecKeyCreateEncryptedData 加密一些数据,然后在 golang 后端解密相同的数据。我正在使用 3072 位 rsa public 密钥来加密对称密钥。当我从 iOS 获取数据到后端时,我能够成功解密对称密钥,但 gcm 标签验证失败。我使用的是 iOS 使用的相同 16 字节 IV,但不知道 iOS 在加密时是否使用任何 aad(附加身份验证数据)。有谁知道 iOS 的 rsaEncryptionOAEPSHA256AESGCM 是否使用了一些 aad?这适用于 iOS 10+。
我已经尝试过使用 nil、空的 16 字节数组、aes 密钥本身、nonce 作为 aad,但是 none 这些都有效。
在Swift中:
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 标签的代码中抛出错误。
找到了。 AAD 是 ASN.1 DER 格式的 RSA public 密钥。
我正在使用 rsaEncryptionOAEPSHA256AESGCM 在 iOS 上使用 SecKeyCreateEncryptedData 加密一些数据,然后在 golang 后端解密相同的数据。我正在使用 3072 位 rsa public 密钥来加密对称密钥。当我从 iOS 获取数据到后端时,我能够成功解密对称密钥,但 gcm 标签验证失败。我使用的是 iOS 使用的相同 16 字节 IV,但不知道 iOS 在加密时是否使用任何 aad(附加身份验证数据)。有谁知道 iOS 的 rsaEncryptionOAEPSHA256AESGCM 是否使用了一些 aad?这适用于 iOS 10+。
我已经尝试过使用 nil、空的 16 字节数组、aes 密钥本身、nonce 作为 aad,但是 none 这些都有效。
在Swift中:
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 标签的代码中抛出错误。
找到了。 AAD 是 ASN.1 DER 格式的 RSA public 密钥。