AESTests.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // CipherAESTests.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 27/12/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import Foundation
  9. import XCTest
  10. import CryptoSwift
  11. class AESTests: XCTestCase {
  12. // 128 bit key
  13. let aesKey:[Byte] = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
  14. func testAES_encode() {
  15. let input:[Byte] = [0x00, 0x11, 0x22, 0x33,
  16. 0x44, 0x55, 0x66, 0x77,
  17. 0x88, 0x99, 0xaa, 0xbb,
  18. 0xcc, 0xdd, 0xee, 0xff];
  19. let expected:[Byte] = [0x69, 0xc4, 0xe0, 0xd8,
  20. 0x6a, 0x7b, 0x4, 0x30,
  21. 0xd8, 0xcd, 0xb7, 0x80,
  22. 0x70, 0xb4, 0xc5, 0x5a];
  23. if let aes = AES(key: NSData.withBytes(aesKey), iv: nil, blockMode: .Plain) {
  24. let encrypted = aes.encrypt(NSData.withBytes(input))
  25. XCTAssertEqual(encrypted!, NSData.withBytes(expected), "encryption failed")
  26. } else {
  27. XCTAssert(false, "failed")
  28. }
  29. }
  30. func testAES_encode_cbc() {
  31. let key:[Byte] = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
  32. let iv:[Byte] = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
  33. let plaintext:[Byte] = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
  34. let expected:[Byte] = [0x76,0x49,0xab,0xac,0x81,0x19,0xb2,0x46,0xce,0xe9,0x8e,0x9b,0x12,0xe9,0x19,0x7d];
  35. if let aes = AES(key: NSData.withBytes(key), iv:NSData.withBytes(iv), blockMode: .CBC) {
  36. XCTAssertTrue(aes.blockMode == .CBC, "Invalid block mode")
  37. let encrypted = aes.encrypt(NSData.withBytes(plaintext))
  38. XCTAssertEqual(encrypted!, NSData.withBytes(expected), "encryption failed")
  39. } else {
  40. XCTAssert(false, "failed")
  41. }
  42. }
  43. func testAES_encode_cfb() {
  44. let key:[Byte] = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
  45. let iv:[Byte] = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
  46. let plaintext:[Byte] = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
  47. let expected:[Byte] = [0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a];
  48. if let aes = AES(key: NSData.withBytes(key), iv:NSData.withBytes(iv), blockMode: .CFB) {
  49. XCTAssertTrue(aes.blockMode == .CFB, "Invalid block mode")
  50. let encrypted = aes.encrypt(NSData.withBytes(plaintext))
  51. XCTAssertEqual(encrypted!, NSData.withBytes(expected), "encryption failed")
  52. } else {
  53. XCTAssert(false, "failed")
  54. }
  55. }
  56. func testAES_SubBytes() {
  57. let input:[[Byte]] = [[0x00, 0x10, 0x20, 0x30],
  58. [0x40, 0x50, 0x60, 0x70],
  59. [0x80, 0x90, 0xa0, 0xb0],
  60. [0xc0, 0xd0, 0xe0, 0xf0]]
  61. let expected:[[Byte]] = [[0x63, 0xca, 0xb7, 0x04],
  62. [0x09, 0x53, 0xd0, 0x51],
  63. [0xcd, 0x60, 0xe0, 0xe7],
  64. [0xba, 0x70, 0xe1, 0x8c]]
  65. let substituted = AES(key: NSData.withBytes(aesKey))!.subBytes(input)
  66. XCTAssertTrue(compareMatrix(expected, substituted), "subBytes failed")
  67. let inverted = AES(key: NSData.withBytes(aesKey))!.invSubBytes(substituted)
  68. XCTAssertTrue(compareMatrix(input, inverted), "invSubBytes failed")
  69. }
  70. func testAES_shiftRows() {
  71. let input:[[Byte]] = [[0x63, 0x09, 0xcd, 0xba],
  72. [0xca, 0x53, 0x60, 0x70],
  73. [0xb7, 0xd0, 0xe0, 0xe1],
  74. [0x04, 0x51, 0xe7, 0x8c]]
  75. let expected:[[Byte]] = [[0x63, 0x9, 0xcd, 0xba],
  76. [0x53, 0x60, 0x70, 0xca],
  77. [0xe0, 0xe1, 0xb7, 0xd0],
  78. [0x8c, 0x4, 0x51, 0xe7]]
  79. let shifted = AES(key: NSData.withBytes(aesKey))!.shiftRows(input)
  80. XCTAssertTrue(compareMatrix(expected, shifted), "shiftRows failed")
  81. let inverted = AES(key: NSData.withBytes(aesKey))!.invShiftRows(shifted)
  82. XCTAssertTrue(compareMatrix(input, inverted), "invShiftRows failed")
  83. }
  84. func testAES_multiply() {
  85. XCTAssertTrue(AES(key: NSData.withBytes(aesKey))?.multiplyPolys(0x0e, 0x5f) == 0x17, "Multiplication failed")
  86. }
  87. func testAES_expandKey() {
  88. let expected:[Byte] = [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xd6, 0xaa, 0x74, 0xfd, 0xd2, 0xaf, 0x72, 0xfa, 0xda, 0xa6, 0x78, 0xf1, 0xd6, 0xab, 0x76, 0xfe, 0xb6, 0x92, 0xcf, 0xb, 0x64, 0x3d, 0xbd, 0xf1, 0xbe, 0x9b, 0xc5, 0x0, 0x68, 0x30, 0xb3, 0xfe, 0xb6, 0xff, 0x74, 0x4e, 0xd2, 0xc2, 0xc9, 0xbf, 0x6c, 0x59, 0xc, 0xbf, 0x4, 0x69, 0xbf, 0x41, 0x47, 0xf7, 0xf7, 0xbc, 0x95, 0x35, 0x3e, 0x3, 0xf9, 0x6c, 0x32, 0xbc, 0xfd, 0x5, 0x8d, 0xfd, 0x3c, 0xaa, 0xa3, 0xe8, 0xa9, 0x9f, 0x9d, 0xeb, 0x50, 0xf3, 0xaf, 0x57, 0xad, 0xf6, 0x22, 0xaa, 0x5e, 0x39, 0xf, 0x7d, 0xf7, 0xa6, 0x92, 0x96, 0xa7, 0x55, 0x3d, 0xc1, 0xa, 0xa3, 0x1f, 0x6b, 0x14, 0xf9, 0x70, 0x1a, 0xe3, 0x5f, 0xe2, 0x8c, 0x44, 0xa, 0xdf, 0x4d, 0x4e, 0xa9, 0xc0, 0x26, 0x47, 0x43, 0x87, 0x35, 0xa4, 0x1c, 0x65, 0xb9, 0xe0, 0x16, 0xba, 0xf4, 0xae, 0xbf, 0x7a, 0xd2, 0x54, 0x99, 0x32, 0xd1, 0xf0, 0x85, 0x57, 0x68, 0x10, 0x93, 0xed, 0x9c, 0xbe, 0x2c, 0x97, 0x4e, 0x13, 0x11, 0x1d, 0x7f, 0xe3, 0x94, 0x4a, 0x17, 0xf3, 0x7, 0xa7, 0x8b, 0x4d, 0x2b, 0x30, 0xc5]
  89. if let aes = AES(key: NSData.withBytes(aesKey)) {
  90. XCTAssertEqual(expected, aes.expandKey(), "expandKey failed")
  91. }
  92. }
  93. func testAES_addRoundKey() {
  94. let input:[[Byte]] = [[0x00, 0x44, 0x88, 0xcc],
  95. [0x11, 0x55, 0x99, 0xdd],
  96. [0x22, 0x66, 0xaa, 0xee],
  97. [0x33, 0x77, 0xbb, 0xff]]
  98. let expected:[[Byte]] = [[0, 64, 128, 192],
  99. [16, 80, 144, 208],
  100. [32, 96, 160, 224],
  101. [48, 112, 176, 240]]
  102. if let aes = AES(key: NSData.withBytes(aesKey)) {
  103. let result = aes.addRoundKey(input, aes.expandKey(), 0)
  104. XCTAssertTrue(compareMatrix(expected, result), "addRoundKey failed")
  105. }
  106. }
  107. func testAES_mixColumns() {
  108. let input:[[Byte]] = [[0x63, 0x9, 0xcd, 0xba],
  109. [0x53, 0x60, 0x70, 0xca],
  110. [0xe0, 0xe1, 0xb7, 0xd0],
  111. [0x8c, 0x4, 0x51, 0xe7]]
  112. let expected:[[Byte]] = [[0x5f, 0x57, 0xf7, 0x1d],
  113. [0x72, 0xf5, 0xbe, 0xb9],
  114. [0x64, 0xbc, 0x3b, 0xf9],
  115. [0x15, 0x92, 0x29, 0x1a]]
  116. if let aes = AES(key: NSData.withBytes(aesKey)) {
  117. let mixed = aes.mixColumns(input)
  118. XCTAssertTrue(compareMatrix(expected, mixed), "mixColumns failed")
  119. let inverted = aes.invMixColumns(mixed)
  120. XCTAssertTrue(compareMatrix(input, inverted), "invMixColumns failed")
  121. }
  122. }
  123. }