SwifterTestsAES128.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // SwifterTestsAES128.swift
  3. // Swifter
  4. //
  5. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  6. //
  7. //
  8. import XCTest
  9. import Swifter
  10. class SwifterTestsAES: XCTestCase {
  11. func testAES() {
  12. // Values from: http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf ( Page 34 )
  13. let key = AES128.Key(
  14. k0: UInt8(0x2b),
  15. k1: UInt8(0x7e),
  16. k2: UInt8(0x15),
  17. k3: UInt8(0x16),
  18. k4: UInt8(0x28),
  19. k5: UInt8(0xae),
  20. k6: UInt8(0xd2),
  21. k7: UInt8(0xa6),
  22. k8: UInt8(0xab),
  23. k9: UInt8(0xf7),
  24. k10: UInt8(0x15),
  25. k11: UInt8(0x88),
  26. k12: UInt8(0x09),
  27. k13: UInt8(0xcf),
  28. k14: UInt8(0x4f),
  29. k15: UInt8(0x3c))
  30. let text = AES128.Block(
  31. s00: 0x32, s01: 0x88, s02: 0x31, s03: 0xe0,
  32. s10: 0x43, s11: 0x5a, s12: 0x31, s13: 0x37,
  33. s20: 0xf6, s21: 0x30, s22: 0x98, s23: 0x07,
  34. s30: 0xa8, s31: 0x8d, s32: 0xa2, s33: 0x34
  35. )
  36. let encrypted = AES128.encryptBlock(text, key)
  37. XCTAssert(encrypted.s00 == 0x39)
  38. XCTAssert(encrypted.s10 == 0x25)
  39. XCTAssert(encrypted.s20 == 0x84)
  40. XCTAssert(encrypted.s30 == 0x1d)
  41. XCTAssert(encrypted.s01 == 0x02)
  42. XCTAssert(encrypted.s11 == 0xdc)
  43. XCTAssert(encrypted.s21 == 0x09)
  44. XCTAssert(encrypted.s31 == 0xfb)
  45. XCTAssert(encrypted.s02 == 0xdc)
  46. XCTAssert(encrypted.s12 == 0x11)
  47. XCTAssert(encrypted.s22 == 0x85)
  48. XCTAssert(encrypted.s32 == 0x97)
  49. XCTAssert(encrypted.s03 == 0x19)
  50. XCTAssert(encrypted.s13 == 0x6a)
  51. XCTAssert(encrypted.s23 == 0x0b)
  52. XCTAssert(encrypted.s33 == 0x32)
  53. }
  54. }