|
@@ -6,13 +6,14 @@
|
|
|
// Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
|
|
|
//
|
|
|
import XCTest
|
|
|
+import Foundation
|
|
|
@testable import CryptoSwift
|
|
|
|
|
|
final class AESTests: XCTestCase {
|
|
|
// 128 bit key
|
|
|
let aesKey:Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
|
|
|
|
|
|
- func testAES_encrypt2() {
|
|
|
+ func testAESEncrypt2() {
|
|
|
let key:Array<UInt8> = [0x36, 0x37, 0x39, 0x66, 0x62, 0x31, 0x64, 0x64, 0x66, 0x37, 0x64, 0x38, 0x31, 0x62, 0x65, 0x65];
|
|
|
let iv:Array<UInt8> = [0x6b, 0x64, 0x66, 0x36, 0x37, 0x33, 0x39, 0x38, 0x44, 0x46, 0x37, 0x33, 0x38, 0x33, 0x66, 0x64]
|
|
|
let input:Array<UInt8> = [0x62, 0x72, 0x61, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
|
@@ -26,7 +27,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(decrypted, input, "decryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt3() {
|
|
|
+ func testAESEncrypt3() {
|
|
|
let key = "679fb1ddf7d81bee"
|
|
|
let iv = "kdf67398DF7383fd"
|
|
|
let input:Array<UInt8> = [0x62, 0x72, 0x61, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
|
@@ -39,7 +40,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(decrypted, input, "decryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt() {
|
|
|
+ func testAESEncrypt() {
|
|
|
let input:Array<UInt8> = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff];
|
|
|
let expected:Array<UInt8> = [0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x4, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a];
|
|
|
|
|
@@ -50,7 +51,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(decrypted, input, "decryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt_cbc_no_padding() {
|
|
|
+ func testAESEncryptCBCNoPadding() {
|
|
|
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
|
|
|
let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
|
|
|
let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
|
|
@@ -63,7 +64,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(decrypted, plaintext, "decryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt_cbc_with_padding() {
|
|
|
+ func testAESEncryptCBCWithPadding() {
|
|
|
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
|
|
|
let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
|
|
|
let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
|
|
@@ -76,7 +77,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(decrypted, plaintext, "decryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt_cbc_with_padding_partial() {
|
|
|
+ func testAESEncryptCBCWithPaddingPartial() {
|
|
|
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
|
|
|
let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
|
|
|
let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
|
|
@@ -92,7 +93,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(try! aes.encrypt(plaintext), ciphertext, "encryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt_incremental() {
|
|
|
+ func testAESEncryptIncremental() {
|
|
|
do {
|
|
|
var ciphertext = Array<UInt8>()
|
|
|
let plaintext = "Today Apple launched the open source Swift community, as well as amazing new tools and resources."
|
|
@@ -107,7 +108,7 @@ final class AESTests: XCTestCase {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- func testAES_decrypt_cbc_with_padding_partial() {
|
|
|
+ func testAESDecryptCBCWithPaddingPartial() {
|
|
|
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
|
|
|
let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
|
|
|
let ciphertext:Array<UInt8> = [118, 73, 171, 172, 129, 25, 178, 70, 206, 233, 142, 155, 18, 233, 25, 125, 76, 187, 200, 88, 117, 107, 53, 129, 37, 82, 158, 150, 152, 163, 143, 68, 169, 105, 137, 234, 93, 98, 239, 215, 41, 45, 51, 254, 138, 92, 251, 17]
|
|
@@ -122,7 +123,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(try! aes.decrypt(ciphertext), plaintext, "encryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt_cfb() {
|
|
|
+ func testAESEncryptCFB() {
|
|
|
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
|
|
|
let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
|
|
|
let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
|
|
@@ -136,7 +137,7 @@ final class AESTests: XCTestCase {
|
|
|
}
|
|
|
|
|
|
// https://github.com/krzyzanowskim/CryptoSwift/issues/142
|
|
|
- func testAES_encrypt_cfb_long() {
|
|
|
+ func testAESEncryptCFBLong() {
|
|
|
let key: Array<UInt8> = [56, 118, 37, 51, 125, 78, 103, 107, 119, 40, 74, 88, 117, 112, 123, 75, 122, 89, 72, 36, 46, 91, 106, 60, 54, 110, 34, 126, 69, 126, 61, 87]
|
|
|
let iv: Array<UInt8> = [69, 122, 99, 87, 83, 112, 110, 65, 54, 109, 107, 89, 73, 122, 74, 49]
|
|
|
let plaintext: Array<UInt8> = [123, 10, 32, 32, 34, 67, 111, 110, 102, 105, 114, 109, 34, 32, 58, 32, 34, 116, 101, 115, 116, 105, 110, 103, 34, 44, 10, 32, 32, 34, 70, 105, 114, 115, 116, 78, 97, 109, 101, 34, 32, 58, 32, 34, 84, 101, 115, 116, 34, 44, 10, 32, 32, 34, 69, 109, 97, 105, 108, 34, 32, 58, 32, 34, 116, 101, 115, 116, 64, 116, 101, 115, 116, 46, 99, 111, 109, 34, 44, 10, 32, 32, 34, 76, 97, 115, 116, 78, 97, 109, 101, 34, 32, 58, 32, 34, 84, 101, 115, 116, 101, 114, 34, 44, 10, 32, 32, 34, 80, 97, 115, 115, 119, 111, 114, 100, 34, 32, 58, 32, 34, 116, 101, 115, 116, 105, 110, 103, 34, 44, 10, 32, 32, 34, 85, 115, 101, 114, 110, 97, 109, 101, 34, 32, 58, 32, 34, 84, 101, 115, 116, 34, 10, 125]
|
|
@@ -145,7 +146,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssert(decrypted == plaintext, "decryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt_ofb128() {
|
|
|
+ func testAESEncryptOFB128() {
|
|
|
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
|
|
|
let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
|
|
|
let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
|
|
@@ -158,7 +159,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(decrypted, plaintext, "decryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt_ofb256() {
|
|
|
+ func testAESEncryptOFB256() {
|
|
|
let key: Array<UInt8> = [0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4]
|
|
|
let iv: Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
|
|
|
let plaintext: Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
|
|
@@ -171,7 +172,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(decrypted, plaintext, "decryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt_pcbc256() {
|
|
|
+ func testAESEncryptPCBC256() {
|
|
|
let key: Array<UInt8> = [0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4]
|
|
|
let iv: Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
|
|
|
let plaintext: Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
|
|
@@ -185,7 +186,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(decrypted, plaintext, "decryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt_ctr() {
|
|
|
+ func testAESEncryptCTR() {
|
|
|
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
|
|
|
let iv:Array<UInt8> = [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff]
|
|
|
let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
|
|
@@ -198,7 +199,7 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertEqual(decrypted, plaintext, "decryption failed")
|
|
|
}
|
|
|
|
|
|
- func testAES_encrypt_ctr_irregular_length() {
|
|
|
+ func testAESEncryptCTRIrregularLength() {
|
|
|
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
|
|
|
let iv:Array<UInt8> = [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff]
|
|
|
let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x01]
|
|
@@ -212,7 +213,7 @@ final class AESTests: XCTestCase {
|
|
|
}
|
|
|
|
|
|
// https://github.com/krzyzanowskim/CryptoSwift/pull/290
|
|
|
- func testAES_decrypt_ctr_seek() {
|
|
|
+ func testAESDecryptCTRSeek() {
|
|
|
let key:Array<UInt8> = [0x52, 0x72, 0xb5, 0x9c, 0xab, 0x07, 0xc5, 0x01, 0x11, 0x7a, 0x39, 0xb6, 0x10, 0x35, 0x87, 0x02];
|
|
|
let iv:Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
|
|
|
var plaintext:Array<UInt8> = Array<UInt8>(repeating: 0, count: 6000)
|
|
@@ -253,7 +254,7 @@ final class AESTests: XCTestCase {
|
|
|
}
|
|
|
|
|
|
// https://github.com/krzyzanowskim/CryptoSwift/pull/289
|
|
|
- func testAES_encrypt_ctr_irregular_length_incremental_update() {
|
|
|
+ func testAESEncryptCTRIrregularLengthIncrementalUpdate() {
|
|
|
let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
|
|
|
let iv:Array<UInt8> = [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff]
|
|
|
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]
|
|
@@ -278,7 +279,7 @@ final class AESTests: XCTestCase {
|
|
|
}
|
|
|
|
|
|
// https://github.com/krzyzanowskim/CryptoSwift/issues/298
|
|
|
- func test_issue298() {
|
|
|
+ func testIssue298() {
|
|
|
let encryptedValue = "47595cfa90f7b0b0e0d9d7240a2e035f7f4acde27d7ca778a7d8b05add32a0a92d945c0a59f7f0e029d7f2fbb258b2f0"
|
|
|
let key = "0123456789abcdef"
|
|
|
let iv = "fedcba9876543210"
|
|
@@ -286,7 +287,7 @@ final class AESTests: XCTestCase {
|
|
|
do {
|
|
|
let aes = try AES(key: key, iv: iv, blockMode: .CBC, padding: NoPadding())
|
|
|
let ciphertext = try aes.decrypt(Array<UInt8>(hex: encryptedValue))
|
|
|
- let str = String(data: Data(ciphertext), encoding: String.Encoding.utf8)
|
|
|
+ let str = String(data: Data(bytes: ciphertext), encoding: String.Encoding.utf8)
|
|
|
XCTAssertEqual(str!, "74b653e36430d7a9cd91a24d9944b032OrFeckrWo\0\0\0\0\0\0\0")
|
|
|
} catch {
|
|
|
XCTFail("failed")
|
|
@@ -326,4 +327,25 @@ final class AESTests: XCTestCase {
|
|
|
XCTAssertTrue(decrypted! != plaintext, "failed")
|
|
|
}
|
|
|
|
|
|
+ static let allTests = [
|
|
|
+ ("testAESEncrypt2", testAESEncrypt2),
|
|
|
+ ("testAESEncrypt3", testAESEncrypt3),
|
|
|
+ ("testAESEncrypt", testAESEncrypt),
|
|
|
+ ("testAESEncryptCBCNoPadding", testAESEncryptCBCNoPadding),
|
|
|
+ ("testAESEncryptCBCWithPadding", testAESEncryptCBCWithPadding),
|
|
|
+ ("testAESEncryptCBCWithPaddingPartial", testAESEncryptCBCWithPaddingPartial),
|
|
|
+ ("testAESEncryptIncremental", testAESEncryptIncremental),
|
|
|
+ ("testAESDecryptCBCWithPaddingPartial", testAESDecryptCBCWithPaddingPartial),
|
|
|
+ ("testAESEncryptCFB", testAESEncryptCFB),
|
|
|
+ ("testAESEncryptCFBLong", testAESEncryptCFBLong),
|
|
|
+ ("testAESEncryptOFB128", testAESEncryptOFB128),
|
|
|
+ ("testAESEncryptOFB256", testAESEncryptOFB256),
|
|
|
+ ("testAESEncryptPCBC256", testAESEncryptPCBC256),
|
|
|
+ ("testAESEncryptCTR", testAESEncryptCTR),
|
|
|
+ ("testAESEncryptCTRIrregularLength", testAESEncryptCTRIrregularLength),
|
|
|
+ ("testAESDecryptCTRSeek", testAESDecryptCTRSeek),
|
|
|
+ ("testAESEncryptCTRIrregularLengthIncrementalUpdate", testAESEncryptCTRIrregularLengthIncrementalUpdate),
|
|
|
+ ("testIssue298", testIssue298),
|
|
|
+ ("testAESWithWrongKey", testAESWithWrongKey)
|
|
|
+ ]
|
|
|
}
|