Skip to content

Commit cf72f4d

Browse files
Merge pull request #1180 from ChatSecure/12-byte-iv
Accept 12-byte IV for OMEMO media
2 parents dd9da3b + 74c0600 commit cf72f4d

File tree

6 files changed

+55
-59
lines changed

6 files changed

+55
-59
lines changed

ChatSecure.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
63F0CAFB1E60C1B40045359C /* OTRYapViewTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F0CAFA1E60C1B40045359C /* OTRYapViewTest.swift */; };
3434
63F614DC1BB214660083A06A /* ChatSecureModelTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F614DB1BB214660083A06A /* ChatSecureModelTest.swift */; };
3535
7CD871CB705CA365E0755104 /* libPods-ChatSecureCorePods-ChatSecureTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5179DA87B83F57EEA9589733 /* libPods-ChatSecureCorePods-ChatSecureTests.a */; };
36+
D9108AA023F9ABDF00B1280D /* AESGCMTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9108A9F23F9ABDF00B1280D /* AESGCMTests.swift */; };
3637
D91F9EFE1ED645F100AEA62C /* FileTransferIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D91F9EFD1ED645F100AEA62C /* FileTransferIntegrationTests.swift */; };
3738
D9365E7A1A1EB0050006434A /* torrc in Resources */ = {isa = PBXBuildFile; fileRef = D9365E791A1EB0050006434A /* torrc */; };
3839
D936D6CB1E8B1B34003B1343 /* FileTransferTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D936D6CA1E8B1B34003B1343 /* FileTransferTests.swift */; };
@@ -655,6 +656,7 @@
655656
D90DA4F2236F3C6800C585B7 /* Appirater.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = Appirater.bundle; sourceTree = BUILT_PRODUCTS_DIR; };
656657
D90DA4F3236F3C6800C585B7 /* CPAProxy.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = CPAProxy.bundle; sourceTree = BUILT_PRODUCTS_DIR; };
657658
D90DA4F4236F3C6800C585B7 /* TUSafariActivity.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = TUSafariActivity.bundle; sourceTree = BUILT_PRODUCTS_DIR; };
659+
D9108A9F23F9ABDF00B1280D /* AESGCMTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AESGCMTests.swift; sourceTree = "<group>"; };
658660
D913A56C1B747B62006C5ACD /* Onboarding.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = Onboarding.storyboard; path = Interface/Onboarding.storyboard; sourceTree = "<group>"; };
659661
D91F9EFD1ED645F100AEA62C /* FileTransferIntegrationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileTransferIntegrationTests.swift; sourceTree = "<group>"; };
660662
D9227C231BA78E6B00B5E1D0 /* FontAwesome.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = FontAwesome.ttf; sourceTree = "<group>"; };
@@ -1263,6 +1265,7 @@
12631265
children = (
12641266
63DDD8B91A9E9BD900C0A918 /* samples */,
12651267
636C63201B571B56008FEE69 /* OTRURLTests.m */,
1268+
D9108A9F23F9ABDF00B1280D /* AESGCMTests.swift */,
12661269
635FCC831D1B5116008F903C /* OTRStringTests.swift */,
12671270
63DDD8B41A9E94B700C0A918 /* OTRMediaTests.m */,
12681271
63F614DB1BB214660083A06A /* ChatSecureModelTest.swift */,
@@ -2605,6 +2608,7 @@
26052608
63634CE91DA704AA00B0BAE8 /* OTROMEMOIntegrationTest.swift in Sources */,
26062609
D91F9EFE1ED645F100AEA62C /* FileTransferIntegrationTests.swift in Sources */,
26072610
635FCC841D1B5116008F903C /* OTRStringTests.swift in Sources */,
2611+
D9108AA023F9ABDF00B1280D /* AESGCMTests.swift in Sources */,
26082612
63E353B21BB9D0CF005C54C3 /* PushSerializerTest.swift in Sources */,
26092613
);
26102614
runOnlyForDeploymentPostprocessing = 0;

ChatSecureCore/Classes/Controllers/FileTransferManager.swift

+14-3
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,20 @@ extension URL {
904904
}
905905

906906
var aesGcmKey: (key: Data, iv: Data)? {
907-
guard let data = self.anchorData, data.count == 48 else { return nil }
908-
let iv = data.subdata(in: 0..<16)
909-
let key = data.subdata(in: 16..<48)
907+
guard let data = self.anchorData else { return nil }
908+
let ivLength: Int
909+
switch data.count {
910+
case 48:
911+
// legacy clients send 16-byte IVs
912+
ivLength = 16
913+
case 44:
914+
// newer clients send 12-byte IVs
915+
ivLength = 12
916+
default:
917+
return nil
918+
}
919+
let iv = data.subdata(in: 0..<ivLength)
920+
let key = data.subdata(in: ivLength..<data.count)
910921
return (key, iv)
911922
}
912923
}

ChatSecureCoreTests/ChatSecureCoreTests.swift

-33
This file was deleted.

ChatSecureCoreTests/Info.plist

-22
This file was deleted.

ChatSecureTests/AESGCMTests.swift

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// AESGCMTests.swift
3+
// ChatSecureTests
4+
//
5+
// Created by Chris Ballinger on 2/16/20.
6+
// Copyright © 2020 Chris Ballinger. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import ChatSecureCore
11+
12+
class AESGCMTests: XCTestCase {
13+
func random(length: Int) -> Data {
14+
let bytes = (0 ..< length).map { _ in UInt8.random(in: .min ... .max) }
15+
XCTAssertEqual(bytes.count, length)
16+
return Data(bytes)
17+
}
18+
19+
func testLegacy16ByteIV() throws {
20+
let messageData = "Test".data(using: .utf8)!
21+
let key = random(length: 16)
22+
let iv = random(length: 16)
23+
let encryptedData = try XCTUnwrap(try OTRSignalEncryptionHelper.encryptData(messageData, key: key, iv: iv))
24+
let decryptedData = try XCTUnwrap(try OTRSignalEncryptionHelper.decryptData(encryptedData.data, key: key, iv: iv, authTag: encryptedData.authTag))
25+
XCTAssertEqual(messageData, decryptedData)
26+
}
27+
28+
func test12ByteIV() throws {
29+
let messageData = "Test".data(using: .utf8)!
30+
let key = random(length: 16)
31+
let iv = random(length: 12)
32+
let encryptedData = try XCTUnwrap(try OTRSignalEncryptionHelper.encryptData(messageData, key: key, iv: iv))
33+
let decryptedData = try XCTUnwrap(try OTRSignalEncryptionHelper.decryptData(encryptedData.data, key: key, iv: iv, authTag: encryptedData.authTag))
34+
XCTAssertEqual(messageData, decryptedData)
35+
}
36+
}

Submodules/OTRKit

0 commit comments

Comments
 (0)