Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept 12-byte IV for OMEMO media #1180

Merged
merged 1 commit into from
Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChatSecure.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
63F0CAFB1E60C1B40045359C /* OTRYapViewTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F0CAFA1E60C1B40045359C /* OTRYapViewTest.swift */; };
63F614DC1BB214660083A06A /* ChatSecureModelTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F614DB1BB214660083A06A /* ChatSecureModelTest.swift */; };
7CD871CB705CA365E0755104 /* libPods-ChatSecureCorePods-ChatSecureTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5179DA87B83F57EEA9589733 /* libPods-ChatSecureCorePods-ChatSecureTests.a */; };
D9108AA023F9ABDF00B1280D /* AESGCMTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9108A9F23F9ABDF00B1280D /* AESGCMTests.swift */; };
D91F9EFE1ED645F100AEA62C /* FileTransferIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D91F9EFD1ED645F100AEA62C /* FileTransferIntegrationTests.swift */; };
D9365E7A1A1EB0050006434A /* torrc in Resources */ = {isa = PBXBuildFile; fileRef = D9365E791A1EB0050006434A /* torrc */; };
D936D6CB1E8B1B34003B1343 /* FileTransferTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D936D6CA1E8B1B34003B1343 /* FileTransferTests.swift */; };
Expand Down Expand Up @@ -655,6 +656,7 @@
D90DA4F2236F3C6800C585B7 /* Appirater.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = Appirater.bundle; sourceTree = BUILT_PRODUCTS_DIR; };
D90DA4F3236F3C6800C585B7 /* CPAProxy.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = CPAProxy.bundle; sourceTree = BUILT_PRODUCTS_DIR; };
D90DA4F4236F3C6800C585B7 /* TUSafariActivity.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = TUSafariActivity.bundle; sourceTree = BUILT_PRODUCTS_DIR; };
D9108A9F23F9ABDF00B1280D /* AESGCMTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AESGCMTests.swift; sourceTree = "<group>"; };
D913A56C1B747B62006C5ACD /* Onboarding.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = Onboarding.storyboard; path = Interface/Onboarding.storyboard; sourceTree = "<group>"; };
D91F9EFD1ED645F100AEA62C /* FileTransferIntegrationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileTransferIntegrationTests.swift; sourceTree = "<group>"; };
D9227C231BA78E6B00B5E1D0 /* FontAwesome.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = FontAwesome.ttf; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1263,6 +1265,7 @@
children = (
63DDD8B91A9E9BD900C0A918 /* samples */,
636C63201B571B56008FEE69 /* OTRURLTests.m */,
D9108A9F23F9ABDF00B1280D /* AESGCMTests.swift */,
635FCC831D1B5116008F903C /* OTRStringTests.swift */,
63DDD8B41A9E94B700C0A918 /* OTRMediaTests.m */,
63F614DB1BB214660083A06A /* ChatSecureModelTest.swift */,
Expand Down Expand Up @@ -2605,6 +2608,7 @@
63634CE91DA704AA00B0BAE8 /* OTROMEMOIntegrationTest.swift in Sources */,
D91F9EFE1ED645F100AEA62C /* FileTransferIntegrationTests.swift in Sources */,
635FCC841D1B5116008F903C /* OTRStringTests.swift in Sources */,
D9108AA023F9ABDF00B1280D /* AESGCMTests.swift in Sources */,
63E353B21BB9D0CF005C54C3 /* PushSerializerTest.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
17 changes: 14 additions & 3 deletions ChatSecureCore/Classes/Controllers/FileTransferManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,20 @@ extension URL {
}

var aesGcmKey: (key: Data, iv: Data)? {
guard let data = self.anchorData, data.count == 48 else { return nil }
let iv = data.subdata(in: 0..<16)
let key = data.subdata(in: 16..<48)
guard let data = self.anchorData else { return nil }
let ivLength: Int
switch data.count {
case 48:
// legacy clients send 16-byte IVs
ivLength = 16
case 44:
// newer clients send 12-byte IVs
ivLength = 12
default:
return nil
}
let iv = data.subdata(in: 0..<ivLength)
let key = data.subdata(in: ivLength..<data.count)
return (key, iv)
}
}
Expand Down
33 changes: 0 additions & 33 deletions ChatSecureCoreTests/ChatSecureCoreTests.swift

This file was deleted.

22 changes: 0 additions & 22 deletions ChatSecureCoreTests/Info.plist

This file was deleted.

36 changes: 36 additions & 0 deletions ChatSecureTests/AESGCMTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// AESGCMTests.swift
// ChatSecureTests
//
// Created by Chris Ballinger on 2/16/20.
// Copyright © 2020 Chris Ballinger. All rights reserved.
//

import XCTest
@testable import ChatSecureCore

class AESGCMTests: XCTestCase {
func random(length: Int) -> Data {
let bytes = (0 ..< length).map { _ in UInt8.random(in: .min ... .max) }
XCTAssertEqual(bytes.count, length)
return Data(bytes)
}

func testLegacy16ByteIV() throws {
let messageData = "Test".data(using: .utf8)!
let key = random(length: 16)
let iv = random(length: 16)
let encryptedData = try XCTUnwrap(try OTRSignalEncryptionHelper.encryptData(messageData, key: key, iv: iv))
let decryptedData = try XCTUnwrap(try OTRSignalEncryptionHelper.decryptData(encryptedData.data, key: key, iv: iv, authTag: encryptedData.authTag))
XCTAssertEqual(messageData, decryptedData)
}

func test12ByteIV() throws {
let messageData = "Test".data(using: .utf8)!
let key = random(length: 16)
let iv = random(length: 12)
let encryptedData = try XCTUnwrap(try OTRSignalEncryptionHelper.encryptData(messageData, key: key, iv: iv))
let decryptedData = try XCTUnwrap(try OTRSignalEncryptionHelper.decryptData(encryptedData.data, key: key, iv: iv, authTag: encryptedData.authTag))
XCTAssertEqual(messageData, decryptedData)
}
}
2 changes: 1 addition & 1 deletion Submodules/OTRKit