Improve Swift bridging of CryptoFlags

This commit is contained in:
Davide De Rosa 2018-10-19 16:51:54 +02:00
parent 22ab63b4a9
commit a430beb35f
4 changed files with 8 additions and 9 deletions

View File

@ -44,10 +44,10 @@ NS_ASSUME_NONNULL_BEGIN
@protocol DataPathDecrypter;
typedef struct {
const uint8_t *iv;
int ivLength;
const uint8_t *ad;
int adLength;
const uint8_t *_Nullable iv;
NSInteger ivLength;
const uint8_t *_Nullable ad;
NSInteger adLength;
} CryptoFlags;
// WARNING: dest must be able to hold ciphertext

View File

@ -127,7 +127,7 @@ const NSInteger CryptoAEADTagLength = 16;
memcpy(self.cipherIVEnc, flags->iv, flags->ivLength);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CipherInit(self.cipherCtxEnc, NULL, NULL, self.cipherIVEnc, -1);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CipherUpdate(self.cipherCtxEnc, NULL, &x, flags->ad, flags->adLength);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CipherUpdate(self.cipherCtxEnc, NULL, &x, flags->ad, (int)flags->adLength);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CipherUpdate(self.cipherCtxEnc, dest + CryptoAEADTagLength, &l1, bytes, (int)length);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CipherFinal(self.cipherCtxEnc, dest + CryptoAEADTagLength + l1, &l2);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CIPHER_CTX_ctrl(self.cipherCtxEnc, EVP_CTRL_GCM_GET_TAG, CryptoAEADTagLength, dest);
@ -174,7 +174,7 @@ const NSInteger CryptoAEADTagLength = 16;
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CipherInit(self.cipherCtxDec, NULL, NULL, self.cipherIVDec, -1);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CIPHER_CTX_ctrl(self.cipherCtxDec, EVP_CTRL_GCM_SET_TAG, CryptoAEADTagLength, (uint8_t *)bytes);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CipherUpdate(self.cipherCtxDec, NULL, &x, flags->ad, flags->adLength);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CipherUpdate(self.cipherCtxDec, NULL, &x, flags->ad, (int)flags->adLength);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CipherUpdate(self.cipherCtxDec, dest, &l1, bytes + CryptoAEADTagLength, (int)length - CryptoAEADTagLength);
TUNNEL_CRYPTO_TRACK_STATUS(code) EVP_CipherFinal(self.cipherCtxDec, dest + l1, &l2);

View File

@ -80,9 +80,8 @@ class EncryptionPerformanceTests: XCTestCase {
// 0.684s
func testGCMEncryption() {
let suite = TestUtils.generateDataSuite(1000, 100000)
let iv: [UInt8] = [0x11, 0x22, 0x33, 0x44]
let ad: [UInt8] = [0x11, 0x22, 0x33, 0x44]
var flags = CryptoFlags(iv: iv, ivLength: 4, ad: ad, adLength: 4)
var flags = CryptoFlags(iv: nil, ivLength: 0, ad: ad, adLength: ad.count)
measure {
for data in suite {
let _ = try! self.gcmEncrypter.encryptData(data, flags: &flags)

View File

@ -81,7 +81,7 @@ class EncryptionTests: XCTestCase {
let packetId: [UInt8] = [0x56, 0x34, 0x12, 0x00]
let ad: [UInt8] = [0x00, 0x12, 0x34, 0x56]
var flags = CryptoFlags(iv: packetId, ivLength: 4, ad: ad, adLength: 4)
var flags = CryptoFlags(iv: packetId, ivLength: packetId.count, ad: ad, adLength: ad.count)
let plain = Data(hex: "00112233445566778899")
let encrypted = try! client.encrypter().encryptData(plain, flags: &flags)
let decrypted = try! server.decrypter().decryptData(encrypted, flags: &flags)