AESTests.swift 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2021 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. import Foundation
  16. import XCTest
  17. @testable import CryptoSwift
  18. final class AESTests: XCTestCase {
  19. // 128 bit key
  20. let aesKey: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  21. func testAESEncrypt() {
  22. let input: Array<UInt8> = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]
  23. let expected: Array<UInt8> = [0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x4, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a]
  24. let aes = try! AES(key: self.aesKey, blockMode: ECB(), padding: .noPadding)
  25. let encrypted = try! aes.encrypt(input)
  26. XCTAssertEqual(encrypted, expected, "encryption failed")
  27. let decrypted = try! aes.decrypt(encrypted)
  28. XCTAssertEqual(decrypted, input, "decryption failed")
  29. }
  30. func testAESEncrypt2() {
  31. let key: Array<UInt8> = [0x36, 0x37, 0x39, 0x66, 0x62, 0x31, 0x64, 0x64, 0x66, 0x37, 0x64, 0x38, 0x31, 0x62, 0x65, 0x65]
  32. let iv: Array<UInt8> = [0x6b, 0x64, 0x66, 0x36, 0x37, 0x33, 0x39, 0x38, 0x44, 0x46, 0x37, 0x33, 0x38, 0x33, 0x66, 0x64]
  33. let input: Array<UInt8> = [0x62, 0x72, 0x61, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  34. let expected: Array<UInt8> = [0xae, 0x8c, 0x59, 0x95, 0xb2, 0x6f, 0x8e, 0x3d, 0xb0, 0x6f, 0x0a, 0xa5, 0xfe, 0xc4, 0xf0, 0xc2]
  35. let aes = try! AES(key: key, blockMode: CBC(iv: iv), padding: .noPadding)
  36. do {
  37. let encrypted = try aes.encrypt(input)
  38. XCTAssertEqual(encrypted, expected, "encryption failed")
  39. let decrypted = try aes.decrypt(encrypted)
  40. XCTAssertEqual(decrypted, input, "decryption failed")
  41. } catch {
  42. XCTFail("\(error)")
  43. }
  44. }
  45. func testAESEncrypt3() {
  46. let key = "679fb1ddf7d81bee"
  47. let iv = "kdf67398DF7383fd"
  48. let input: Array<UInt8> = [0x62, 0x72, 0x61, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  49. let expected: Array<UInt8> = [0xae, 0x8c, 0x59, 0x95, 0xb2, 0x6f, 0x8e, 0x3d, 0xb0, 0x6f, 0x0a, 0xa5, 0xfe, 0xc4, 0xf0, 0xc2]
  50. do {
  51. let aes = try AES(key: key, iv: iv, padding: .noPadding)
  52. let encrypted = try aes.encrypt(input)
  53. XCTAssertEqual(encrypted, expected, "encryption failed")
  54. let decrypted = try aes.decrypt(encrypted)
  55. XCTAssertEqual(decrypted, input, "decryption failed")
  56. } catch {
  57. XCTFail("\(error)")
  58. }
  59. }
  60. func testAESEncryptCBCNoPadding() {
  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]
  65. let aes = try! AES(key: key, blockMode: CBC(iv: iv), padding: .noPadding)
  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 testAESEncryptCBCWithPadding() {
  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]
  75. 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]
  76. let aes = try! AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs7)
  77. let encrypted = try! aes.encrypt(plaintext)
  78. XCTAssertEqual(encrypted, expected, "encryption failed")
  79. let decrypted = try! aes.decrypt(encrypted)
  80. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  81. }
  82. func testAESEncryptCBCWithPaddingPartial() {
  83. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  84. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  85. 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]
  86. let aes = try! AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs7)
  87. var ciphertext = Array<UInt8>()
  88. var encryptor = try! aes.makeEncryptor()
  89. ciphertext += try! encryptor.update(withBytes: plaintext[0..<8])
  90. ciphertext += try! encryptor.update(withBytes: plaintext[8..<16])
  91. ciphertext += try! encryptor.update(withBytes: plaintext[16..<32])
  92. ciphertext += try! encryptor.finish()
  93. XCTAssertEqual(try! aes.encrypt(plaintext), ciphertext, "encryption failed")
  94. }
  95. func testAESEncryptIncremental() {
  96. do {
  97. var ciphertext = Array<UInt8>()
  98. let plaintext = "Today Apple launched the open source Swift community, as well as amazing new tools and resources."
  99. let aes = try AES(key: "passwordpassword".bytes, blockMode: CBC(iv: "drowssapdrowssap".bytes))
  100. var encryptor = try! aes.makeEncryptor()
  101. ciphertext += try encryptor.update(withBytes: plaintext.bytes)
  102. ciphertext += try encryptor.finish()
  103. XCTAssertEqual(try aes.encrypt(plaintext.bytes), ciphertext, "encryption failed")
  104. } catch {
  105. XCTFail("\(error)")
  106. }
  107. }
  108. func testAESDecryptCBCWithPaddingPartial() {
  109. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  110. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  111. 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]
  112. let aes = try! AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs7)
  113. var plaintext = Array<UInt8>()
  114. var decryptor = try! aes.makeDecryptor()
  115. plaintext += try! decryptor.update(withBytes: ciphertext[0..<8])
  116. plaintext += try! decryptor.update(withBytes: ciphertext[8..<16])
  117. plaintext += try! decryptor.update(withBytes: ciphertext[16..<32])
  118. plaintext += try! decryptor.finish()
  119. XCTAssertEqual(try! aes.decrypt(ciphertext), plaintext, "encryption failed")
  120. }
  121. func testAESEncryptCFB() {
  122. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  123. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  124. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  125. let expected: Array<UInt8> = [0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a]
  126. let aes = try! AES(key: key, blockMode: CFB(iv: iv), padding: .noPadding)
  127. let encrypted = try! aes.encrypt(plaintext)
  128. XCTAssertEqual(encrypted, expected, "encryption failed")
  129. let decrypted = try! aes.decrypt(encrypted)
  130. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  131. }
  132. // https://github.com/krzyzanowskim/CryptoSwift/issues/142
  133. func testAESEncryptCFBLong() {
  134. 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]
  135. let iv: Array<UInt8> = [69, 122, 99, 87, 83, 112, 110, 65, 54, 109, 107, 89, 73, 122, 74, 49]
  136. 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]
  137. let encrypted: Array<UInt8> = try! AES(key: key, blockMode: CFB(iv: iv)).encrypt(plaintext)
  138. let decrypted: Array<UInt8> = try! AES(key: key, blockMode: CFB(iv: iv)).decrypt(encrypted)
  139. XCTAssert(decrypted == plaintext, "decryption failed")
  140. }
  141. // https://github.com/krzyzanowskim/CryptoSwift/issues/500
  142. func testAESEncryptCFB8() {
  143. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  144. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  145. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d]
  146. let expected: Array<UInt8> = [0x3b, 0x79, 0x42, 0x4c, 0x9c, 0x0d, 0xd4, 0x36, 0xba, 0xce, 0x9e, 0x0e, 0xd4, 0x58, 0x6a, 0x4f, 0x32, 0xb9]
  147. let aes = try! AES(key: key, blockMode: CFB(iv: iv, segmentSize: .cfb8), padding: .noPadding)
  148. let encrypted = try! aes.encrypt(plaintext)
  149. XCTAssertEqual(encrypted, expected, "encryption failed")
  150. let decrypted = try! aes.decrypt(encrypted)
  151. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  152. }
  153. func testAESEncryptOFB128() {
  154. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  155. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  156. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  157. let expected: Array<UInt8> = [0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a]
  158. let aes = try! AES(key: key, blockMode: OFB(iv: iv), padding: .noPadding)
  159. let encrypted = try! aes.encrypt(plaintext)
  160. XCTAssertEqual(encrypted, expected, "encryption failed")
  161. let decrypted = try! aes.decrypt(encrypted)
  162. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  163. }
  164. func testAESEncryptOFB256() {
  165. 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]
  166. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  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> = [0xdc, 0x7e, 0x84, 0xbf, 0xda, 0x79, 0x16, 0x4b, 0x7e, 0xcd, 0x84, 0x86, 0x98, 0x5d, 0x38, 0x60]
  169. let aes = try! AES(key: key, blockMode: OFB(iv: iv), padding: .noPadding)
  170. let encrypted = try! aes.encrypt(plaintext)
  171. XCTAssertEqual(encrypted, expected, "encryption failed")
  172. let decrypted = try! aes.decrypt(encrypted)
  173. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  174. }
  175. func testAESEncryptPCBC256() {
  176. 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]
  177. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  178. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  179. let expected: Array<UInt8> = [0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6]
  180. let aes = try! AES(key: key, blockMode: PCBC(iv: iv), padding: .noPadding)
  181. let encrypted = try! aes.encrypt(plaintext)
  182. print(encrypted.toHexString())
  183. XCTAssertEqual(encrypted, expected, "encryption failed")
  184. let decrypted = try! aes.decrypt(encrypted)
  185. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  186. }
  187. func testAESEncryptBigPCBC128() {
  188. let key = "0123456789abcdef".bytes
  189. let iv = "fedcba9876543210".bytes
  190. let plaintext = "64 byte plaintext that will split into 4 chunks of 16 bytes each".bytes
  191. let ciphertext: Array<UInt8> = [0xd6, 0x83, 0x7b, 0xb8, 0xfe, 0x1d, 0x62, 0xf7, 0x04, 0x69, 0xd1, 0xfd, 0x47, 0x06, 0x9c, 0x3d, 0xc0, 0x7c, 0xfe, 0xc9, 0x3d, 0xba, 0x35, 0x61, 0x40, 0xef, 0xe2, 0xac, 0xc6, 0x4c, 0x3d, 0x04, 0xbf, 0x4c, 0xa4, 0xf6, 0xfc, 0x09, 0xfc, 0x8c, 0x2e, 0x09, 0xd0, 0x74, 0x66, 0x2b, 0x8f, 0x02, 0x54, 0x01, 0x25, 0x76, 0x20, 0x88, 0x5e, 0x19, 0x3f, 0x74, 0xcd, 0x48, 0x29, 0xc7, 0xe1, 0xc6, 0xfb, 0xc9, 0xb9, 0xcf, 0xcd, 0xf8, 0xeb, 0x42, 0xbc, 0x0f, 0xc5, 0x73, 0x96, 0xe4, 0xf8, 0x0f]
  192. let aes = try! AES(key: key, blockMode: PCBC(iv: iv), padding: .pkcs7)
  193. let encrypted = try! aes.encrypt(plaintext)
  194. XCTAssertEqual(encrypted, ciphertext, "encryption failed")
  195. let decrypted = try! aes.decrypt(ciphertext)
  196. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  197. }
  198. func testAESEncryptCTR() {
  199. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  200. let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
  201. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
  202. let expected: Array<UInt8> = [0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce]
  203. let aes = try! AES(key: key, blockMode: CTR(iv: iv), padding: .noPadding)
  204. let encrypted = try! aes.encrypt(plaintext)
  205. XCTAssertEqual(encrypted.count, plaintext.count)
  206. XCTAssertEqual(encrypted, expected, "encryption failed")
  207. let decrypted = try! aes.decrypt(encrypted)
  208. XCTAssertEqual(decrypted.count, plaintext.count)
  209. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  210. }
  211. // https://github.com/krzyzanowskim/CryptoSwift/issues/424
  212. func testAESEncryptCTRZeroPadding() {
  213. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  214. let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
  215. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0x01]
  216. let expected: Array<UInt8> = [0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce, 0x37, 0x2b, 0x7c, 0x3c, 0x67, 0x73, 0x51, 0x63, 0x18, 0xa0, 0x77, 0xd7, 0xfc, 0x50, 0x73, 0xae]
  217. let aes = try! AES(key: key, blockMode: CTR(iv: iv), padding: .zeroPadding)
  218. let encrypted = try! aes.encrypt(plaintext)
  219. XCTAssertEqual(plaintext.count, 17)
  220. XCTAssertEqual(encrypted.count, 32, "padding failed")
  221. XCTAssertEqual(encrypted, expected, "encryption failed")
  222. }
  223. func testAESEncryptCTRIrregularLength() {
  224. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  225. let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
  226. let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0x01]
  227. let expected: Array<UInt8> = [0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce, 0x37]
  228. let aes = try! AES(key: key, blockMode: CTR(iv: iv), padding: .noPadding)
  229. let encrypted = try! aes.encrypt(plaintext)
  230. XCTAssertEqual(encrypted, expected, "encryption failed")
  231. let decrypted = try! aes.decrypt(encrypted)
  232. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  233. }
  234. // https://github.com/krzyzanowskim/CryptoSwift/pull/290
  235. func testAESDecryptCTRSeek() {
  236. let key: Array<UInt8> = [0x52, 0x72, 0xb5, 0x9c, 0xab, 0x07, 0xc5, 0x01, 0x11, 0x7a, 0x39, 0xb6, 0x10, 0x35, 0x87, 0x02]
  237. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
  238. var plaintext: Array<UInt8> = Array<UInt8>(repeating: 0, count: 6000)
  239. for i in 0..<plaintext.count / 6 {
  240. let s = String(format: "%05d", i).bytes
  241. plaintext[i * 6 + 0] = s[0]
  242. plaintext[i * 6 + 1] = s[1]
  243. plaintext[i * 6 + 2] = s[2]
  244. plaintext[i * 6 + 3] = s[3]
  245. plaintext[i * 6 + 4] = s[4]
  246. plaintext[i * 6 + 5] = "|".utf8.first!
  247. }
  248. var aes = try! AES(key: key, blockMode: CTR(iv: iv), padding: .noPadding)
  249. let encrypted = try! aes.encrypt(plaintext)
  250. aes = try! AES(key: key, blockMode: CTR(iv: iv), padding: .noPadding)
  251. var decryptor = try! aes.makeDecryptor()
  252. try! decryptor.seek(to: 2)
  253. var part1 = try! decryptor.update(withBytes: Array(encrypted[2..<5]))
  254. part1 += try! decryptor.finish()
  255. XCTAssertEqual(part1, Array(plaintext[2..<5]), "seek decryption failed")
  256. try! decryptor.seek(to: 1000)
  257. var part2 = try! decryptor.update(withBytes: Array(encrypted[1000..<1010]))
  258. part2 += try! decryptor.finish()
  259. XCTAssertEqual(part2, Array(plaintext[1000..<1010]), "seek decryption failed")
  260. try! decryptor.seek(to: 5500)
  261. var part3 = try! decryptor.update(withBytes: Array(encrypted[5500..<6000]))
  262. part3 += try! decryptor.finish()
  263. XCTAssertEqual(part3, Array(plaintext[5500..<6000]), "seek decryption failed")
  264. try! decryptor.seek(to: 0)
  265. var part4 = try! decryptor.update(withBytes: Array(encrypted[0..<80]))
  266. part4 += try! decryptor.finish()
  267. XCTAssertEqual(part4, Array(plaintext[0..<80]), "seek decryption failed")
  268. }
  269. // https://github.com/krzyzanowskim/CryptoSwift/pull/289
  270. func testAESEncryptCTRIrregularLengthIncrementalUpdate() {
  271. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  272. let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
  273. 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]
  274. 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]
  275. let aes = try! AES(key: key, blockMode: CTR(iv: iv), padding: .noPadding)
  276. var encryptor = try! aes.makeEncryptor()
  277. var encrypted = Array<UInt8>()
  278. encrypted += try! encryptor.update(withBytes: plaintext[0..<5])
  279. encrypted += try! encryptor.update(withBytes: plaintext[5..<15])
  280. encrypted += try! encryptor.update(withBytes: plaintext[15...])
  281. encrypted += try! encryptor.finish()
  282. XCTAssertEqual(encrypted, expected, "encryption failed")
  283. var decryptor = try! aes.makeDecryptor()
  284. var decrypted = Array<UInt8>()
  285. decrypted += try! decryptor.update(withBytes: expected[0..<5])
  286. decrypted += try! decryptor.update(withBytes: expected[5..<15])
  287. decrypted += try! decryptor.update(withBytes: expected[15...])
  288. decrypted += try! decryptor.finish()
  289. XCTAssertEqual(decrypted, plaintext, "decryption failed")
  290. }
  291. func testAESEncryptCTRStream() {
  292. let key = Array<UInt8>(hex: "0xbe3e9020816eb838782e2d9f4a2f40d4")
  293. let iv = Array<UInt8>(hex: "0x0000000000000000a9bbd681ded0c0c8")
  294. do {
  295. let aes = try AES(key: key, blockMode: CTR(iv: iv), padding: .noPadding)
  296. var encryptor = try aes.makeEncryptor()
  297. let encrypted1 = try encryptor.update(withBytes: [0x00, 0x01, 0x02, 0x03] as [UInt8])
  298. XCTAssertEqual(encrypted1, Array<UInt8>(hex: "d79d0344"))
  299. let encrypted2 = try encryptor.update(withBytes: [0x04, 0x05, 0x06, 0x07] as [UInt8])
  300. XCTAssertEqual(encrypted2, Array<UInt8>(hex: "b2a08879"))
  301. let encrypted3 = try encryptor.update(withBytes: [0x08] as [UInt8])
  302. XCTAssertEqual(encrypted3, Array<UInt8>(hex: "db"))
  303. } catch {
  304. XCTFail(error.localizedDescription)
  305. }
  306. }
  307. func testAESWithWrongKey() {
  308. let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
  309. let key2: Array<UInt8> = [0x22, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x33]
  310. let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  311. let plaintext: Array<UInt8> = [49, 46, 50, 50, 50, 51, 51, 51, 51]
  312. let aes = try! AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs7)
  313. let aes2 = try! AES(key: key2, blockMode: CBC(iv: iv), padding: .pkcs7)
  314. let encrypted = try! aes.encrypt(plaintext)
  315. let decrypted = try? aes2.decrypt(encrypted)
  316. XCTAssertTrue(decrypted! != plaintext, "failed")
  317. }
  318. // https://github.com/krzyzanowskim/CryptoSwift/issues/298
  319. func testIssue298() {
  320. let encryptedValue = "47595cfa90f7b0b0e0d9d7240a2e035f7f4acde27d7ca778a7d8b05add32a0a92d945c0a59f7f0e029d7f2fbb258b2f0"
  321. 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]
  322. let key = "0123456789abcdef"
  323. let iv = "fedcba9876543210"
  324. do {
  325. let aes = try AES(key: key, iv: iv, padding: .noPadding)
  326. let ciphertext = try aes.decrypt(Array<UInt8>(hex: encryptedValue))
  327. XCTAssertEqual(ciphertext, expected)
  328. } catch {
  329. XCTFail("failed")
  330. }
  331. }
  332. // https://github.com/krzyzanowskim/CryptoSwift/issues/394
  333. func testIssue394() {
  334. let plaintext = "Nullam quis risus eget urna mollis ornare vel eu leo.".bytes
  335. let key = "passwordpassword".bytes.md5() // -md md5
  336. let iv = "drowssapdrowssap".bytes // -iv 64726f777373617064726f7773736170
  337. let aes = try! AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs7) // -aes-128-cbc
  338. let ciphertext = try! aes.encrypt(plaintext) // enc
  339. // $ 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
  340. // cij+965z2Xqoj9tIHgtA72ZPfv5sxnt76vwkIt1CodYY313oa7mr0pSc5o++g0CX
  341. // YczxK2fGIa84xtwDtRMwBQ==
  342. XCTAssertEqual(ciphertext.toBase64(), "cij+965z2Xqoj9tIHgtA72ZPfv5sxnt76vwkIt1CodYY313oa7mr0pSc5o++g0CXYczxK2fGIa84xtwDtRMwBQ==")
  343. }
  344. // https://github.com/krzyzanowskim/CryptoSwift/issues/411
  345. func testIssue411() {
  346. let ciphertext: Array<UInt8> = [0x2a, 0x3a, 0x80, 0x05, 0xaf, 0x46, 0x58, 0x2d, 0x66, 0x52, 0x10, 0xae, 0x86, 0xd3, 0x8e, 0x8f] // test
  347. let key = "passwordpassword".bytes.md5() // -md md5
  348. let iv = "drowssapdrowssap".bytes // -iv 64726f777373617064726f7773736170
  349. let aes = try! AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs7) // -aes-128-cbc
  350. let plaintext = try! ciphertext.decrypt(cipher: aes)
  351. XCTAssertEqual("74657374", plaintext.toHexString())
  352. }
  353. }
  354. // MARK: - GCM
  355. extension AESTests {
  356. func testAESGCMTestCase1() {
  357. // Test Case 1
  358. let key = Array<UInt8>(hex: "0x00000000000000000000000000000000")
  359. let iv = Array<UInt8>(hex: "0x000000000000000000000000")
  360. let gcm = GCM(iv: iv, mode: .detached)
  361. let aes = try! AES(key: key, blockMode: gcm, padding: .noPadding)
  362. let encrypted = try! aes.encrypt([UInt8]())
  363. XCTAssertEqual(Array(encrypted), [UInt8](hex: "")) // C
  364. XCTAssertEqual(gcm.authenticationTag, [UInt8](hex: "58e2fccefa7e3061367f1d57a4e7455a")) // T (128-bit)
  365. }
  366. func testAESGCMTestCase2() {
  367. // Test Case 2
  368. let key = Array<UInt8>(hex: "0x00000000000000000000000000000000")
  369. let plaintext = Array<UInt8>(hex: "0x00000000000000000000000000000000")
  370. let iv = Array<UInt8>(hex: "0x000000000000000000000000")
  371. let gcm = GCM(iv: iv, mode: .detached)
  372. let aes = try! AES(key: key, blockMode: gcm, padding: .noPadding)
  373. let encrypted = try! aes.encrypt(plaintext)
  374. XCTAssertEqual(Array(encrypted), [UInt8](hex: "0388dace60b6a392f328c2b971b2fe78")) // C
  375. XCTAssertEqual(gcm.authenticationTag, [UInt8](hex: "ab6e47d42cec13bdf53a67b21257bddf")) // T (128-bit)
  376. }
  377. func testAESGCMTestCase3() {
  378. // Test Case 3
  379. let key = Array<UInt8>(hex: "0xfeffe9928665731c6d6a8f9467308308")
  380. let plaintext = Array<UInt8>(hex: "0xd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255")
  381. let iv = Array<UInt8>(hex: "0xcafebabefacedbaddecaf888")
  382. let encGCM = GCM(iv: iv, mode: .detached)
  383. let aes = try! AES(key: key, blockMode: encGCM, padding: .noPadding)
  384. let encrypted = try! aes.encrypt(plaintext)
  385. XCTAssertNotNil(encGCM.authenticationTag)
  386. XCTAssertEqual(Array(encrypted), [UInt8](hex: "0x42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985")) // C
  387. XCTAssertEqual(encGCM.authenticationTag, [UInt8](hex: "0x4d5c2af327cd64a62cf35abd2ba6fab4")) // T (128-bit)
  388. // decrypt
  389. func decrypt(_ encrypted: Array<UInt8>, tag: Array<UInt8>) -> Array<UInt8> {
  390. let decGCM = GCM(iv: iv, authenticationTag: tag, mode: .detached)
  391. let aes = try! AES(key: key, blockMode: decGCM, padding: .noPadding)
  392. return try! aes.decrypt(encrypted)
  393. }
  394. let decrypted = decrypt(encrypted, tag: encGCM.authenticationTag!)
  395. XCTAssertEqual(decrypted, plaintext)
  396. }
  397. func testAESGCMTestCase3Combined() {
  398. // Test Case 3
  399. let key = Array<UInt8>(hex: "0xfeffe9928665731c6d6a8f9467308308")
  400. let plaintext = Array<UInt8>(hex: "0xd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255")
  401. let iv = Array<UInt8>(hex: "0xcafebabefacedbaddecaf888")
  402. let encGCM = GCM(iv: iv, mode: .combined)
  403. let aes = try! AES(key: key, blockMode: encGCM, padding: .noPadding)
  404. let encrypted = try! aes.encrypt(plaintext)
  405. XCTAssertNotNil(encGCM.authenticationTag)
  406. XCTAssertEqual(Array(encrypted), [UInt8](hex: "0x42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f59854d5c2af327cd64a62cf35abd2ba6fab4")) // C
  407. XCTAssertEqual(encGCM.authenticationTag, [UInt8](hex: "0x4d5c2af327cd64a62cf35abd2ba6fab4")) // T (128-bit)
  408. // decrypt
  409. func decrypt(_ encrypted: Array<UInt8>) -> Array<UInt8> {
  410. let decGCM = GCM(iv: iv, mode: .combined)
  411. let aes = try! AES(key: key, blockMode: decGCM, padding: .noPadding)
  412. return try! aes.decrypt(encrypted)
  413. }
  414. let decrypted = decrypt(encrypted)
  415. XCTAssertEqual(decrypted, plaintext)
  416. }
  417. func testAESGCMTestCase4() {
  418. // Test Case 4
  419. let key = Array<UInt8>(hex: "0xfeffe9928665731c6d6a8f9467308308")
  420. let plaintext = Array<UInt8>(hex: "0xd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39")
  421. let iv = Array<UInt8>(hex: "0xcafebabefacedbaddecaf888")
  422. let auth = Array<UInt8>(hex: "0xfeedfacedeadbeeffeedfacedeadbeefabaddad2")
  423. let gcm = GCM(iv: iv, additionalAuthenticatedData: auth, mode: .detached)
  424. let aes = try! AES(key: key, blockMode: gcm, padding: .noPadding)
  425. let encrypted = try! aes.encrypt(plaintext)
  426. XCTAssertEqual(Array(encrypted), [UInt8](hex: "0x42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091")) // C
  427. XCTAssertEqual(gcm.authenticationTag, [UInt8](hex: "0x5bc94fbc3221a5db94fae95ae7121a47")) // T (128-bit)
  428. }
  429. func testAESGCMTestCase5() {
  430. // Test Case 5
  431. let key = Array<UInt8>(hex: "0xfeffe9928665731c6d6a8f9467308308")
  432. let plaintext = Array<UInt8>(hex: "0xd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39")
  433. let iv = Array<UInt8>(hex: "0xcafebabefacedbad")
  434. let auth = Array<UInt8>(hex: "0xfeedfacedeadbeeffeedfacedeadbeefabaddad2")
  435. let gcm = GCM(iv: iv, additionalAuthenticatedData: auth, mode: .detached)
  436. let aes = try! AES(key: key, blockMode: gcm, padding: .noPadding)
  437. let encrypted = try! aes.encrypt(plaintext)
  438. XCTAssertEqual(Array(encrypted), [UInt8](hex: "0x61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598")) // C
  439. XCTAssertEqual(gcm.authenticationTag, [UInt8](hex: "0x3612d2e79e3b0785561be14aaca2fccb")) // T (128-bit)
  440. }
  441. func testAESGCMTestCase6() {
  442. // Test Case 6
  443. let key = Array<UInt8>(hex: "0xfeffe9928665731c6d6a8f9467308308")
  444. let plaintext = Array<UInt8>(hex: "0xd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39")
  445. let iv = Array<UInt8>(hex: "0x9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b")
  446. let auth = Array<UInt8>(hex: "0xfeedfacedeadbeeffeedfacedeadbeefabaddad2")
  447. let gcm = GCM(iv: iv, additionalAuthenticatedData: auth, mode: .detached)
  448. let aes = try! AES(key: key, blockMode: gcm, padding: .noPadding)
  449. let encrypted = try! aes.encrypt(plaintext)
  450. XCTAssertEqual(Array(encrypted), [UInt8](hex: "0x8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5")) // C
  451. XCTAssertEqual(gcm.authenticationTag, [UInt8](hex: "0x619cc5aefffe0bfa462af43c1699d050")) // T (128-bit)
  452. }
  453. func testAESGCMTestCase7() {
  454. // Test Case 7
  455. let key = Array<UInt8>(hex: "0x000000000000000000000000000000000000000000000000")
  456. let plaintext = Array<UInt8>(hex: "")
  457. let iv = Array<UInt8>(hex: "0x000000000000000000000000")
  458. let gcm = GCM(iv: iv, mode: .detached)
  459. let aes = try! AES(key: key, blockMode: gcm, padding: .noPadding)
  460. let encrypted = try! aes.encrypt(plaintext)
  461. XCTAssertEqual(Array(encrypted), [UInt8](hex: "")) // C
  462. XCTAssertEqual(gcm.authenticationTag, [UInt8](hex: "0xcd33b28ac773f74ba00ed1f312572435")) // T (128-bit)
  463. }
  464. func testAESGCMTagLengthDetached() {
  465. // Test Case 2
  466. let key = Array<UInt8>(hex: "0x00000000000000000000000000000000")
  467. let plaintext = Array<UInt8>(hex: "0x00000000000000000000000000000000")
  468. let iv = Array<UInt8>(hex: "0x000000000000000000000000")
  469. let gcm = GCM(iv: iv, tagLength: 12, mode: .detached)
  470. let aes = try! AES(key: key, blockMode: gcm, padding: .noPadding)
  471. let encrypted = try! aes.encrypt(plaintext)
  472. XCTAssertEqual(Array(encrypted), [UInt8](hex: "0388dace60b6a392f328c2b971b2fe78")) // C
  473. XCTAssertEqual(gcm.authenticationTag, [UInt8](hex: "ab6e47d42cec13bdf53a67b2")) // T (96-bit)
  474. // decrypt
  475. func decrypt(_ encrypted: Array<UInt8>) -> Array<UInt8> {
  476. let decGCM = GCM(iv: iv, authenticationTag: gcm.authenticationTag!, mode: .detached)
  477. let aes = try! AES(key: key, blockMode: decGCM, padding: .noPadding)
  478. return try! aes.decrypt(encrypted)
  479. }
  480. let decrypted = decrypt(encrypted)
  481. XCTAssertEqual(decrypted, plaintext)
  482. }
  483. func testAESGCMTagLengthCombined() {
  484. // Test Case 2
  485. let key = Array<UInt8>(hex: "0x00000000000000000000000000000000")
  486. let plaintext = Array<UInt8>(hex: "0x00000000000000000000000000000000")
  487. let iv = Array<UInt8>(hex: "0x000000000000000000000000")
  488. let gcm = GCM(iv: iv, tagLength: 12, mode: .combined)
  489. let aes = try! AES(key: key, blockMode: gcm, padding: .noPadding)
  490. let encrypted = try! aes.encrypt(plaintext)
  491. XCTAssertEqual(Array(encrypted), [UInt8](hex: "0388dace60b6a392f328c2b971b2fe78ab6e47d42cec13bdf53a67b2")) // C
  492. XCTAssertEqual(gcm.authenticationTag, [UInt8](hex: "ab6e47d42cec13bdf53a67b2")) // T (96-bit)
  493. // decrypt
  494. func decrypt(_ encrypted: Array<UInt8>) -> Array<UInt8> {
  495. let decGCM = GCM(iv: iv, authenticationTag: gcm.authenticationTag!, mode: .combined)
  496. let aes = try! AES(key: key, blockMode: decGCM, padding: .noPadding)
  497. return try! aes.decrypt(encrypted)
  498. }
  499. let decrypted = decrypt(encrypted)
  500. XCTAssertEqual(decrypted, plaintext)
  501. }
  502. func testAESGCMTagLengthCombined2() {
  503. let key = Array<UInt8>(hex: "0x00000000000000000000000000000000")
  504. let plaintext = Array<UInt8>(hex: "0x0000000000000000000000000000000000000000")
  505. let iv = Array<UInt8>(hex: "0x000000000000")
  506. let gcm = GCM(iv: iv, tagLength: 12, mode: .combined)
  507. let aes = try! AES(key: key, blockMode: gcm, padding: .noPadding)
  508. let encrypted = try! aes.encrypt(plaintext)
  509. // decrypt
  510. func decrypt(_ encrypted: Array<UInt8>) -> Array<UInt8> {
  511. let decGCM = GCM(iv: iv, authenticationTag: gcm.authenticationTag!, mode: .combined)
  512. let aes = try! AES(key: key, blockMode: decGCM, padding: .noPadding)
  513. return try! aes.decrypt(encrypted)
  514. }
  515. let decrypted = decrypt(encrypted)
  516. XCTAssertEqual(decrypted, plaintext)
  517. }
  518. func testAESGCMTestCaseIrregularCombined1() {
  519. // echo -n "0123456789010123456789012345" | openssl enc -aes-128-gcm -K feffe9928665731c6d6a8f9467308308 -iv cafebabefacedbaddecaf888 -nopad -nosalt
  520. // openssl note: The enc program does not support authenticated encryption modes like CCM and GCM. The utility does not store or retrieve the authentication tag
  521. let key = Array<UInt8>(hex: "0xfeffe9928665731c6d6a8f9467308308")
  522. let plaintext = "0123456789010123456789012345".bytes
  523. let iv = Array<UInt8>(hex: "0xcafebabefacedbaddecaf888")
  524. let encGCM = GCM(iv: iv, mode: .combined)
  525. let aes = try! AES(key: key, blockMode: encGCM, padding: .noPadding)
  526. let encrypted = try! aes.encrypt(plaintext)
  527. XCTAssertNotNil(encGCM.authenticationTag)
  528. XCTAssertEqual(Array(encrypted), [UInt8](hex: "0xab831ed4edc644f6d61218431b14c0355138be4b010f630b29be7a2b9793b9fbecc7b44cc86dfd697a50c1c6")) // C
  529. XCTAssertEqual(encGCM.authenticationTag, [UInt8](hex: "0x9793b9fbecc7b44cc86dfd697a50c1c6")) // T (128-bit)
  530. // decrypt
  531. func decrypt(_ encrypted: Array<UInt8>) -> Array<UInt8> {
  532. let decGCM = GCM(iv: iv, mode: .combined)
  533. let aes = try! AES(key: key, blockMode: decGCM, padding: .noPadding)
  534. return try! aes.decrypt(encrypted)
  535. }
  536. let decrypted = decrypt(encrypted)
  537. XCTAssertEqual(decrypted, plaintext)
  538. }
  539. func testAESGCMTestCaseIrregularCombined2() {
  540. // echo -n "0123456789010123456789012345012345678901012345678901234567" | openssl enc -aes-128-gcm -K feffe9928665731c6d6a8f9467308308 -iv cafebabefacedbaddecaf888 -nopad -nosalt
  541. // openssl note: The enc program does not support authenticated encryption modes like CCM and GCM. The utility does not store or retrieve the authentication tag
  542. let key = Array<UInt8>(hex: "0xfeffe9928665731c6d6a8f9467308308")
  543. let plaintext = "0123456789010123456789012345012345678901012345678901234567".bytes
  544. let iv = Array<UInt8>(hex: "0xcafebabefacedbaddecaf888")
  545. let encGCM = GCM(iv: iv, mode: .combined)
  546. let aes = try! AES(key: key, blockMode: encGCM, padding: .noPadding)
  547. let encrypted = try! aes.encrypt(plaintext)
  548. XCTAssertNotNil(encGCM.authenticationTag)
  549. XCTAssertEqual(Array(encrypted), [UInt8](hex: "0xab831ed4edc644f6d61218431b14c0355138be4b010f630b29be7a2b93ac196f09dc2e10f937aa7e6271564dd117291792f0d6fdf2347ef5b10c86a7f414f0c91a8e59fd2405b850527e")) // C
  550. XCTAssertEqual(encGCM.authenticationTag, [UInt8](hex: "0x86a7f414f0c91a8e59fd2405b850527e")) // T (128-bit)
  551. // decrypt
  552. func decrypt(_ encrypted: Array<UInt8>) -> Array<UInt8> {
  553. let decGCM = GCM(iv: iv, mode: .combined)
  554. let aes = try! AES(key: key, blockMode: decGCM, padding: .noPadding)
  555. return try! aes.decrypt(encrypted)
  556. }
  557. let decrypted = decrypt(encrypted)
  558. XCTAssertEqual(decrypted, plaintext)
  559. }
  560. }
  561. extension AESTests {
  562. static func allTests() -> [(String, (AESTests) -> () -> Void)] {
  563. let tests = [
  564. ("testAESEncrypt", testAESEncrypt),
  565. ("testAESEncrypt2", testAESEncrypt2),
  566. ("testAESEncrypt3", testAESEncrypt3),
  567. ("testAESEncryptCBCNoPadding", testAESEncryptCBCNoPadding),
  568. ("testAESEncryptCBCWithPadding", testAESEncryptCBCWithPadding),
  569. ("testAESEncryptCBCWithPaddingPartial", testAESEncryptCBCWithPaddingPartial),
  570. ("testAESEncryptIncremental", testAESEncryptIncremental),
  571. ("testAESDecryptCBCWithPaddingPartial", testAESDecryptCBCWithPaddingPartial),
  572. ("testAESEncryptCFB", testAESEncryptCFB),
  573. ("testAESEncryptCFBLong", testAESEncryptCFBLong),
  574. ("testAESEncryptCFB8", testAESEncryptCFB8),
  575. ("testAESEncryptOFB128", testAESEncryptOFB128),
  576. ("testAESEncryptOFB256", testAESEncryptOFB256),
  577. ("testAESEncryptPCBC256", testAESEncryptPCBC256),
  578. ("testAESEncryptCTR", testAESEncryptCTR),
  579. ("testAESEncryptCTRZeroPadding", testAESEncryptCTRZeroPadding),
  580. ("testAESEncryptCTRIrregularLength", testAESEncryptCTRIrregularLength),
  581. ("testAESDecryptCTRSeek", testAESDecryptCTRSeek),
  582. ("testAESEncryptCTRIrregularLengthIncrementalUpdate", testAESEncryptCTRIrregularLengthIncrementalUpdate),
  583. ("testAESEncryptCTRStream", testAESEncryptCTRStream),
  584. ("testIssue298", testIssue298),
  585. ("testIssue394", testIssue394),
  586. ("testIssue411", testIssue411),
  587. ("testAESWithWrongKey", testAESWithWrongKey),
  588. ("testAESGCMTestCase1", testAESGCMTestCase1),
  589. ("testAESGCMTestCase2", testAESGCMTestCase2),
  590. ("testAESGCMTestCase3", testAESGCMTestCase3),
  591. ("testAESGCMTestCase3Combined", testAESGCMTestCase3Combined),
  592. ("testAESGCMTestCase4", testAESGCMTestCase4),
  593. ("testAESGCMTestCase5", testAESGCMTestCase5),
  594. ("testAESGCMTestCase6", testAESGCMTestCase6),
  595. ("testAESGCMTestCase7", testAESGCMTestCase7),
  596. ("testAESGCMTestTagLengthDetached", testAESGCMTagLengthDetached),
  597. ("testAESGCMTestTagLengthCombined", testAESGCMTagLengthCombined),
  598. ("testAESGCMTestCaseIrregularCombined1", testAESGCMTestCaseIrregularCombined1),
  599. ("testAESGCMTestCaseIrregularCombined2", testAESGCMTestCaseIrregularCombined2)
  600. ]
  601. return tests
  602. }
  603. }