Skip to content

Commit b4f932c

Browse files
committed
Fixes incremental updates with AES-CTR. Backport 057ec9a. Fixes #287.
1 parent 2a31fbb commit b4f932c

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

CryptoSwiftTests/AESTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,31 @@ final class AESTests: XCTestCase {
268268
self.stopMeasuring()
269269
})
270270
}
271+
272+
// https://github.com/krzyzanowskim/CryptoSwift/pull/289
273+
func testAES_encrypt_ctr_irregular_length_incremental_update() {
274+
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
275+
let iv:Array<UInt8> = [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff]
276+
let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x01,0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x01]
277+
let expected:Array<UInt8> = [0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,0x1b,0xef,0x68,0x64,0x99,0xd,0xb6,0xce,0x37,0x40,0xbd,0x82,0x85,0x5d,0x11,0xfc,0x8e,0x49,0x4a,0xa9,0xed,0x23,0xe0,0xb9,0x40,0x2d]
278+
279+
let aes = try! AES(key: key, iv:iv, blockMode: .CTR, padding: NoPadding())
280+
var encryptor = aes.makeEncryptor()
281+
var encrypted = Array<UInt8>()
282+
encrypted += try! encryptor.update(withBytes: Array(plaintext[0..<5]))
283+
encrypted += try! encryptor.update(withBytes: Array(plaintext[5..<15]))
284+
encrypted += try! encryptor.update(withBytes: Array(plaintext[15..<plaintext.count]))
285+
encrypted += try! encryptor.finish()
286+
XCTAssertEqual(encrypted, expected, "encryption failed")
287+
288+
var decryptor = aes.makeDecryptor()
289+
var decrypted = Array<UInt8>()
290+
decrypted += try! decryptor.update(withBytes: Array(expected[0..<5]))
291+
decrypted += try! decryptor.update(withBytes: Array(expected[5..<15]))
292+
decrypted += try! decryptor.update(withBytes: Array(expected[15..<plaintext.count]))
293+
decrypted += try! decryptor.finish()
294+
XCTAssertEqual(decrypted, plaintext, "decryption failed")
295+
}
271296

272297
func testAESWithWrongKey() {
273298
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];

Sources/CryptoSwift/AES.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,14 @@ extension AES {
406406
mutating public func update(withBytes bytes:Array<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
407407
self.accumulated += bytes
408408

409-
if (isLast) {
409+
if isLast {
410410
self.accumulated = padding.add(self.accumulated, blockSize: AES.blockSize)
411411
}
412412

413-
//CTR does not require full block therefore work with anything
414413
var encrypted = Array<UInt8>()
415414
encrypted.reserveCapacity(self.accumulated.count)
416415
for chunk in self.accumulated.chunks(AES.blockSize) {
417-
if (!self.paddingRequired || self.accumulated.count >= AES.blockSize) {
416+
if (isLast || self.accumulated.count >= AES.blockSize) {
418417
encrypted += worker.encrypt(chunk)
419418
self.accumulated.removeFirst(chunk.count)
420419
}
@@ -452,13 +451,13 @@ extension AES {
452451
var plaintext = Array<UInt8>()
453452
plaintext.reserveCapacity(self.accumulated.count)
454453
for chunk in self.accumulated.chunks(AES.blockSize) {
455-
if (!self.paddingRequired || self.accumulated.count >= AES.blockSize) {
454+
if (isLast || self.accumulated.count >= AES.blockSize) {
456455
plaintext += worker.decrypt(chunk)
457456
self.accumulated.removeFirst(chunk.count)
458457
}
459458
}
460459

461-
if (isLast) {
460+
if isLast {
462461
plaintext = padding.remove(plaintext, blockSize: AES.blockSize)
463462
}
464463

Sources/CryptoSwift/BlockMode/CTR.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ struct CTRModeWorker: BlockModeWorker {
3232
}
3333

3434
mutating func decrypt(ciphertext: Array<UInt8>) -> Array<UInt8> {
35-
let nonce = buildNonce(iv, counter: UInt64(counter))
36-
counter = counter + 1
37-
38-
guard let plaintext = cipherOperation(block: nonce) else {
39-
return ciphertext
40-
}
41-
return xor(plaintext, ciphertext)
35+
return encrypt(ciphertext)
4236
}
4337
}
4438

0 commit comments

Comments
 (0)