AESTests.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //
  2. // CipherAESTests.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 27/12/14.
  6. // Copyright (C) 2014-2017 Krzyzanowski. All rights reserved.
  7. //
  8. import XCTest
  9. import Foundation
  10. @testable import CryptoSwift
  11. final class AESTests: XCTestCase {
  12. // 128 bit key
  13. let aesKey: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  14. func testAESEncrypt() {
  15. let input: Array<UInt8> = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]
  16. let expected: Array<UInt8> = [0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x4, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a]
  17. let aes = try! AES(key: aesKey, blockMode: .ECB, padding: .noPadding)
  18. let encrypted = try! aes.encrypt(input)
  19. XCTAssertEqual(encrypted, expected, "encryption failed")
  20. let decrypted = try! aes.decrypt(encrypted)
  21. XCTAssertEqual(decrypted, input, "decryption failed")
  22. }
  23. func testAESEncrypt2() {
  24. let key: Array<UInt8> = [0x36, 0x37, 0x39, 0x66, 0x62, 0x31, 0x64, 0x64, 0x66, 0x37, 0x64, 0x38, 0x31, 0x62, 0x65, 0x65]
  25. let iv: Array<UInt8> = [0x6b, 0x64, 0x66, 0x36, 0x37, 0x33, 0x39, 0x38, 0x44, 0x46, 0x37, 0x33, 0x38, 0x33, 0x66, 0x64]
  26. let input: Array<UInt8> = [0x62, 0x72, 0x61, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  27. let expected: Array<UInt8> = [0xae, 0x8c, 0x59, 0x95, 0xb2, 0x6f, 0x8e, 0x3d, 0xb0, 0x6f, 0x0a, 0xa5, 0xfe, 0xc4, 0xf0, 0xc2]
  28. let aes = try! AES(key: key, blockMode: .CBC(iv: iv), padding: .noPadding)
  29. do {
  30. let encrypted = try aes.encrypt(input)
  31. XCTAssertEqual(encrypted, expected, "encryption failed")
  32. let decrypted = try aes.decrypt(encrypted)
  33. XCTAssertEqual(decrypted, input, "decryption failed")
  34. } catch {
  35. XCTFail("\(error)")
  36. }
  37. }
  38. func testAESEncrypt3() {
  39. let key = "679fb1ddf7d81bee"
  40. let iv = "kdf67398DF7383fd"
  41. let input: Array<UInt8> = [0x62, 0x72, 0x61, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  42. let expected: Array<UInt8> = [0xae, 0x8c, 0x59, 0x95, 0xb2, 0x6f, 0x8e, 0x3d, 0xb0, 0x6f, 0x0a, 0xa5, 0xfe, 0xc4, 0xf0, 0xc2]
  43. let aes = try! AES(key: key, iv: iv, padding: .noPadding)
  44. let encrypted = try! aes.encrypt(input)
  45. XCTAssertEqual(encrypted, expected, "encryption failed")
  46. let decrypted = try! aes.decrypt(encrypted)
  47. XCTAssertEqual(decrypted, input, "decryption failed")
  48. }
  49. func testAESEncryptCBCNoPadding() {
  50. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  51. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  52. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  53. let expected: Array<UInt8> = [0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d]
  54. let aes = try! AES(key: key, blockMode: .CBC(iv: iv), padding: .noPadding)
  55. let encrypted = try! aes.encrypt(plaintext)
  56. XCTAssertEqual(encrypted, expected, "encryption failed")
  57. let decrypted = try! aes.decrypt(encrypted)
  58. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  59. }
  60. func testAESEncryptCBCWithPadding() {
  61. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  62. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  63. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  64. let expected: Array<UInt8> = [0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d, 0x89, 0x64, 0xe0, 0xb1, 0x49, 0xc1, 0x0b, 0x7b, 0x68, 0x2e, 0x6e, 0x39, 0xaa, 0xeb, 0x73, 0x1c]
  65. let aes = try! AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7)
  66. let encrypted = try! aes.encrypt(plaintext)
  67. XCTAssertEqual(encrypted, expected, "encryption failed")
  68. let decrypted = try! aes.decrypt(encrypted)
  69. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  70. }
  71. func testAESEncryptCBCWithPaddingPartial() {
  72. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  73. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  74. 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]
  75. let aes = try! AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7)
  76. var ciphertext = Array<UInt8>()
  77. var encryptor = try! aes.makeEncryptor()
  78. ciphertext += try! encryptor.update(withBytes: plaintext[0..<8])
  79. ciphertext += try! encryptor.update(withBytes: plaintext[8..<16])
  80. ciphertext += try! encryptor.update(withBytes: plaintext[16..<32])
  81. ciphertext += try! encryptor.finish()
  82. XCTAssertEqual(try! aes.encrypt(plaintext), ciphertext, "encryption failed")
  83. }
  84. func testAESEncryptIncremental() {
  85. do {
  86. var ciphertext = Array<UInt8>()
  87. let plaintext = "Today Apple launched the open source Swift community, as well as amazing new tools and resources."
  88. let aes = try AES(key: Array("passwordpassword".utf8), blockMode: .CBC(iv: Array("drowssapdrowssap".utf8)))
  89. var encryptor = try! aes.makeEncryptor()
  90. ciphertext += try encryptor.update(withBytes: Array(plaintext.utf8))
  91. ciphertext += try encryptor.finish()
  92. XCTAssertEqual(try aes.encrypt(Array(plaintext.utf8)), ciphertext, "encryption failed")
  93. } catch {
  94. XCTAssert(false, "\(error)")
  95. }
  96. }
  97. func testAESDecryptCBCWithPaddingPartial() {
  98. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  99. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  100. 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]
  101. let aes = try! AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7)
  102. var plaintext = Array<UInt8>()
  103. var decryptor = try! aes.makeDecryptor()
  104. plaintext += try! decryptor.update(withBytes: ciphertext[0..<8])
  105. plaintext += try! decryptor.update(withBytes: ciphertext[8..<16])
  106. plaintext += try! decryptor.update(withBytes: ciphertext[16..<32])
  107. plaintext += try! decryptor.finish()
  108. XCTAssertEqual(try! aes.decrypt(ciphertext), plaintext, "encryption failed")
  109. }
  110. func testAESEncryptCFB() {
  111. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  112. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  113. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  114. let expected: Array<UInt8> = [0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a]
  115. let aes = try! AES(key: key, blockMode: .CFB(iv: iv), padding: .noPadding)
  116. let encrypted = try! aes.encrypt(plaintext)
  117. XCTAssertEqual(encrypted, expected, "encryption failed")
  118. let decrypted = try! aes.decrypt(encrypted)
  119. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  120. }
  121. // https://github.com/krzyzanowskim/CryptoSwift/issues/142
  122. func testAESEncryptCFBLong() {
  123. 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]
  124. let iv: Array<UInt8> = [69, 122, 99, 87, 83, 112, 110, 65, 54, 109, 107, 89, 73, 122, 74, 49]
  125. 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]
  126. let encrypted: Array<UInt8> = try! AES(key: key, blockMode: .CFB(iv: iv)).encrypt(plaintext)
  127. let decrypted: Array<UInt8> = try! AES(key: key, blockMode: .CFB(iv: iv)).decrypt(encrypted)
  128. XCTAssert(decrypted == plaintext, "decryption failed")
  129. }
  130. func testAESEncryptOFB128() {
  131. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  132. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  133. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  134. let expected: Array<UInt8> = [0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a]
  135. let aes = try! AES(key: key, blockMode: .OFB(iv: iv), padding: .noPadding)
  136. let encrypted = try! aes.encrypt(plaintext)
  137. XCTAssertEqual(encrypted, expected, "encryption failed")
  138. let decrypted = try! aes.decrypt(encrypted)
  139. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  140. }
  141. func testAESEncryptOFB256() {
  142. 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]
  143. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  144. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  145. let expected: Array<UInt8> = [0xdc, 0x7e, 0x84, 0xbf, 0xda, 0x79, 0x16, 0x4b, 0x7e, 0xcd, 0x84, 0x86, 0x98, 0x5d, 0x38, 0x60]
  146. let aes = try! AES(key: key, blockMode: .OFB(iv: iv), padding: .noPadding)
  147. let encrypted = try! aes.encrypt(plaintext)
  148. XCTAssertEqual(encrypted, expected, "encryption failed")
  149. let decrypted = try! aes.decrypt(encrypted)
  150. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  151. }
  152. func testAESEncryptPCBC256() {
  153. 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]
  154. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  155. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  156. let expected: Array<UInt8> = [0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6]
  157. let aes = try! AES(key: key, blockMode: .PCBC(iv: iv), padding: .noPadding)
  158. let encrypted = try! aes.encrypt(plaintext)
  159. print(encrypted.toHexString())
  160. XCTAssertEqual(encrypted, expected, "encryption failed")
  161. let decrypted = try! aes.decrypt(encrypted)
  162. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  163. }
  164. func testAESEncryptCTR() {
  165. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  166. let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
  167. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  168. let expected: Array<UInt8> = [0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce]
  169. let aes = try! AES(key: key, blockMode: .CTR(iv: iv), padding: .noPadding)
  170. let encrypted = try! aes.encrypt(plaintext)
  171. XCTAssertEqual(encrypted.count, plaintext.count)
  172. XCTAssertEqual(encrypted, expected, "encryption failed")
  173. let decrypted = try! aes.decrypt(encrypted)
  174. XCTAssertEqual(decrypted.count, plaintext.count)
  175. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  176. }
  177. // https://github.com/krzyzanowskim/CryptoSwift/issues/424
  178. func testAESEncryptCTRZeroPadding() {
  179. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  180. let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
  181. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xfd]
  182. let aes = try! AES(key: key, blockMode: .CTR(iv: iv), padding: .zeroPadding)
  183. let encrypted = try! aes.encrypt(plaintext)
  184. XCTAssertEqual(plaintext.count, 17)
  185. XCTAssertEqual(encrypted.count, 32, "padding failed")
  186. }
  187. func testAESEncryptCTRIrregularLength() {
  188. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  189. let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
  190. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0x01]
  191. let expected: Array<UInt8> = [0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce, 0x37]
  192. let aes = try! AES(key: key, blockMode: .CTR(iv: iv), padding: .noPadding)
  193. let encrypted = try! aes.encrypt(plaintext)
  194. XCTAssertEqual(encrypted, expected, "encryption failed")
  195. let decrypted = try! aes.decrypt(encrypted)
  196. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  197. }
  198. // https://github.com/krzyzanowskim/CryptoSwift/pull/290
  199. func testAESDecryptCTRSeek() {
  200. let key: Array<UInt8> = [0x52, 0x72, 0xb5, 0x9c, 0xab, 0x07, 0xc5, 0x01, 0x11, 0x7a, 0x39, 0xb6, 0x10, 0x35, 0x87, 0x02]
  201. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
  202. var plaintext: Array<UInt8> = Array<UInt8>(repeating: 0, count: 6000)
  203. for i in 0..<plaintext.count / 6 {
  204. let s = Array(String(format: "%05d", i).utf8)
  205. plaintext[i * 6 + 0] = s[0]
  206. plaintext[i * 6 + 1] = s[1]
  207. plaintext[i * 6 + 2] = s[2]
  208. plaintext[i * 6 + 3] = s[3]
  209. plaintext[i * 6 + 4] = s[4]
  210. plaintext[i * 6 + 5] = "|".utf8.first!
  211. }
  212. let aes = try! AES(key: key, blockMode: .CTR(iv: iv), padding: .noPadding)
  213. let encrypted = try! aes.encrypt(plaintext)
  214. var decryptor = try! aes.makeDecryptor()
  215. decryptor.seek(to: 2)
  216. var part1 = try! decryptor.update(withBytes: Array(encrypted[2..<5]))
  217. part1 += try! decryptor.finish()
  218. XCTAssertEqual(part1, Array(plaintext[2..<5]), "seek decryption failed")
  219. decryptor.seek(to: 1000)
  220. var part2 = try! decryptor.update(withBytes: Array(encrypted[1000..<1200]))
  221. part2 += try! decryptor.finish()
  222. XCTAssertEqual(part2, Array(plaintext[1000..<1200]), "seek decryption failed")
  223. decryptor.seek(to: 5500)
  224. var part3 = try! decryptor.update(withBytes: Array(encrypted[5500..<6000]))
  225. part3 += try! decryptor.finish()
  226. XCTAssertEqual(part3, Array(plaintext[5500..<6000]), "seek decryption failed")
  227. decryptor.seek(to: 0)
  228. var part4 = try! decryptor.update(withBytes: Array(encrypted[0..<80]))
  229. part4 += try! decryptor.finish()
  230. XCTAssertEqual(part4, Array(plaintext[0..<80]), "seek decryption failed")
  231. }
  232. // https://github.com/krzyzanowskim/CryptoSwift/pull/289
  233. func testAESEncryptCTRIrregularLengthIncrementalUpdate() {
  234. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  235. let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
  236. 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]
  237. 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]
  238. let aes = try! AES(key: key, blockMode: .CTR(iv: iv), padding: .noPadding)
  239. var encryptor = try! aes.makeEncryptor()
  240. var encrypted = Array<UInt8>()
  241. encrypted += try! encryptor.update(withBytes: plaintext[0..<5])
  242. encrypted += try! encryptor.update(withBytes: plaintext[5..<15])
  243. encrypted += try! encryptor.update(withBytes: plaintext[15..<plaintext.count])
  244. encrypted += try! encryptor.finish()
  245. XCTAssertEqual(encrypted, expected, "encryption failed")
  246. var decryptor = try! aes.makeDecryptor()
  247. var decrypted = Array<UInt8>()
  248. decrypted += try! decryptor.update(withBytes: expected[0..<5])
  249. decrypted += try! decryptor.update(withBytes: expected[5..<15])
  250. decrypted += try! decryptor.update(withBytes: expected[15..<plaintext.count])
  251. decrypted += try! decryptor.finish()
  252. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  253. }
  254. func testAESWithWrongKey() {
  255. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  256. let key2: Array<UInt8> = [0x22, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x33]
  257. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  258. let plaintext: Array<UInt8> = [49, 46, 50, 50, 50, 51, 51, 51, 51]
  259. let aes = try! AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7)
  260. let aes2 = try! AES(key: key2, blockMode: .CBC(iv: iv), padding: .pkcs7)
  261. let encrypted = try! aes.encrypt(plaintext)
  262. let decrypted = try? aes2.decrypt(encrypted)
  263. XCTAssertTrue(decrypted! != plaintext, "failed")
  264. }
  265. // https://github.com/krzyzanowskim/CryptoSwift/issues/298
  266. func testIssue298() {
  267. let encryptedValue = "47595cfa90f7b0b0e0d9d7240a2e035f7f4acde27d7ca778a7d8b05add32a0a92d945c0a59f7f0e029d7f2fbb258b2f0"
  268. let expected: Array<UInt8> = [55, 52, 98, 54, 53, 51, 101, 51, 54, 52, 51, 48, 100, 55, 97, 57, 99, 100, 57, 49, 97, 50, 52, 100, 57, 57, 52, 52, 98, 48, 51, 50, 79, 114, 70, 101, 99, 107, 114, 87, 111, 0, 0, 0, 0, 0, 0, 0]
  269. let key = "0123456789abcdef"
  270. let iv = "fedcba9876543210"
  271. do {
  272. let aes = try AES(key: key, iv: iv, padding: .noPadding)
  273. let ciphertext = try aes.decrypt(Array<UInt8>(hex: encryptedValue))
  274. XCTAssertEqual(ciphertext, expected)
  275. } catch {
  276. XCTFail("failed")
  277. }
  278. }
  279. // https://github.com/krzyzanowskim/CryptoSwift/issues/394
  280. func testIssue394() {
  281. let plaintext = Array("Nullam quis risus eget urna mollis ornare vel eu leo.".utf8)
  282. let key = Array("passwordpassword".utf8).md5() // -md md5
  283. let iv = Array("drowssapdrowssap".utf8) // -iv 64726f777373617064726f7773736170
  284. let aes = try! AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7) // -aes-128-cbc
  285. let ciphertext = try! aes.encrypt(plaintext) // enc
  286. // $ echo -n "Nullam quis risus eget urna mollis ornare vel eu leo." | openssl enc -aes-128-cbc -md md5 -nosalt -iv 64726f777373617064726f7773736170 -pass pass:passwordpassword -base64
  287. // cij+965z2Xqoj9tIHgtA72ZPfv5sxnt76vwkIt1CodYY313oa7mr0pSc5o++g0CX
  288. // YczxK2fGIa84xtwDtRMwBQ==
  289. XCTAssertEqual(ciphertext.toBase64(), "cij+965z2Xqoj9tIHgtA72ZPfv5sxnt76vwkIt1CodYY313oa7mr0pSc5o++g0CXYczxK2fGIa84xtwDtRMwBQ==")
  290. }
  291. // https://github.com/krzyzanowskim/CryptoSwift/issues/411
  292. func testIssue411() {
  293. let ciphertext: Array<UInt8> = [0x2a, 0x3a, 0x80, 0x05, 0xaf, 0x46, 0x58, 0x2d, 0x66, 0x52, 0x10, 0xae, 0x86, 0xd3, 0x8e, 0x8f] // test
  294. let key = Array("passwordpassword".utf8).md5() // -md md5
  295. let iv = Array("drowssapdrowssap".utf8) // -iv 64726f777373617064726f7773736170
  296. let aes = try! AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7) // -aes-128-cbc
  297. let plaintext = try! ciphertext.decrypt(cipher: aes)
  298. XCTAssertEqual("74657374", plaintext.toHexString())
  299. }
  300. }
  301. #if !CI
  302. extension AESTests {
  303. func testAESEncryptPerformance() {
  304. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  305. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  306. let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
  307. let aes = try! AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7)
  308. measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
  309. _ = try! aes.encrypt(message)
  310. })
  311. }
  312. func testAESDecryptPerformance() {
  313. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  314. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  315. let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
  316. let aes = try! AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7)
  317. measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
  318. _ = try! aes.decrypt(message)
  319. })
  320. }
  321. }
  322. #endif
  323. extension AESTests {
  324. static func allTests() -> [(String, (AESTests) -> () -> Void)] {
  325. var tests = [
  326. ("testAESEncrypt", testAESEncrypt),
  327. ("testAESEncrypt2", testAESEncrypt2),
  328. ("testAESEncrypt3", testAESEncrypt3),
  329. ("testAESEncryptCBCNoPadding", testAESEncryptCBCNoPadding),
  330. ("testAESEncryptCBCWithPadding", testAESEncryptCBCWithPadding),
  331. ("testAESEncryptCBCWithPaddingPartial", testAESEncryptCBCWithPaddingPartial),
  332. ("testAESEncryptIncremental", testAESEncryptIncremental),
  333. ("testAESDecryptCBCWithPaddingPartial", testAESDecryptCBCWithPaddingPartial),
  334. ("testAESEncryptCFB", testAESEncryptCFB),
  335. ("testAESEncryptCFBLong", testAESEncryptCFBLong),
  336. ("testAESEncryptOFB128", testAESEncryptOFB128),
  337. ("testAESEncryptOFB256", testAESEncryptOFB256),
  338. ("testAESEncryptPCBC256", testAESEncryptPCBC256),
  339. ("testAESEncryptCTR", testAESEncryptCTR),
  340. ("testAESEncryptCTRZeroPadding", testAESEncryptCTRZeroPadding),
  341. ("testAESEncryptCTRIrregularLength", testAESEncryptCTRIrregularLength),
  342. ("testAESDecryptCTRSeek", testAESDecryptCTRSeek),
  343. ("testAESEncryptCTRIrregularLengthIncrementalUpdate", testAESEncryptCTRIrregularLengthIncrementalUpdate),
  344. ("testIssue298", testIssue298),
  345. ("testIssue394", testIssue394),
  346. ("testIssue411", testIssue411),
  347. ("testAESWithWrongKey", testAESWithWrongKey),
  348. ]
  349. #if !CI
  350. tests += [
  351. ("testAESEncryptPerformance", testAESEncryptPerformance),
  352. ("testAESDecryptPerformance", testAESDecryptPerformance),
  353. ]
  354. #endif
  355. return tests
  356. }
  357. }