Chacha20Tests.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Chacha20Tests.swift
  2. // This file is part of KeePassKit.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // KeePassKit is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // KeePassKit is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with KeePassKit. If not, see <https://www.gnu.org/licenses/>.
  18. import Binary
  19. import XCTest
  20. @testable import Crypto
  21. class Chacha20Tests: XCTestCase {
  22. override func setUp() {
  23. // Put setup code here. This method is called before the invocation of each test method in the class.
  24. }
  25. override func tearDown() {
  26. // Put teardown code here. This method is called after the invocation of each test method in the class.
  27. }
  28. func testChaCha20() {
  29. let keys: [[UInt8]] = [
  30. [
  31. 0x00,
  32. 0x00,
  33. 0x00,
  34. 0x00,
  35. 0x00,
  36. 0x00,
  37. 0x00,
  38. 0x00,
  39. 0x00,
  40. 0x00,
  41. 0x00,
  42. 0x00,
  43. 0x00,
  44. 0x00,
  45. 0x00,
  46. 0x00,
  47. 0x00,
  48. 0x00,
  49. 0x00,
  50. 0x00,
  51. 0x00,
  52. 0x00,
  53. 0x00,
  54. 0x00,
  55. 0x00,
  56. 0x00,
  57. 0x00,
  58. 0x00,
  59. 0x00,
  60. 0x00,
  61. 0x00,
  62. 0x00,
  63. ],
  64. [
  65. 0x00,
  66. 0x00,
  67. 0x00,
  68. 0x00,
  69. 0x00,
  70. 0x00,
  71. 0x00,
  72. 0x00,
  73. 0x00,
  74. 0x00,
  75. 0x00,
  76. 0x00,
  77. 0x00,
  78. 0x00,
  79. 0x00,
  80. 0x00,
  81. 0x00,
  82. 0x00,
  83. 0x00,
  84. 0x00,
  85. 0x00,
  86. 0x00,
  87. 0x00,
  88. 0x00,
  89. 0x00,
  90. 0x00,
  91. 0x00,
  92. 0x00,
  93. 0x00,
  94. 0x00,
  95. 0x00,
  96. 0x01,
  97. ],
  98. [
  99. 0x00,
  100. 0x00,
  101. 0x00,
  102. 0x00,
  103. 0x00,
  104. 0x00,
  105. 0x00,
  106. 0x00,
  107. 0x00,
  108. 0x00,
  109. 0x00,
  110. 0x00,
  111. 0x00,
  112. 0x00,
  113. 0x00,
  114. 0x00,
  115. 0x00,
  116. 0x00,
  117. 0x00,
  118. 0x00,
  119. 0x00,
  120. 0x00,
  121. 0x00,
  122. 0x00,
  123. 0x00,
  124. 0x00,
  125. 0x00,
  126. 0x00,
  127. 0x00,
  128. 0x00,
  129. 0x00,
  130. 0x00,
  131. ],
  132. [
  133. 0x00,
  134. 0x00,
  135. 0x00,
  136. 0x00,
  137. 0x00,
  138. 0x00,
  139. 0x00,
  140. 0x00,
  141. 0x00,
  142. 0x00,
  143. 0x00,
  144. 0x00,
  145. 0x00,
  146. 0x00,
  147. 0x00,
  148. 0x00,
  149. 0x00,
  150. 0x00,
  151. 0x00,
  152. 0x00,
  153. 0x00,
  154. 0x00,
  155. 0x00,
  156. 0x00,
  157. 0x00,
  158. 0x00,
  159. 0x00,
  160. 0x00,
  161. 0x00,
  162. 0x00,
  163. 0x00,
  164. 0x00,
  165. ],
  166. [
  167. 0x00,
  168. 0x01,
  169. 0x02,
  170. 0x03,
  171. 0x04,
  172. 0x05,
  173. 0x06,
  174. 0x07,
  175. 0x08,
  176. 0x09,
  177. 0x0A,
  178. 0x0B,
  179. 0x0C,
  180. 0x0D,
  181. 0x0E,
  182. 0x0F,
  183. 0x10,
  184. 0x11,
  185. 0x12,
  186. 0x13,
  187. 0x14,
  188. 0x15,
  189. 0x16,
  190. 0x17,
  191. 0x18,
  192. 0x19,
  193. 0x1A,
  194. 0x1B,
  195. 0x1C,
  196. 0x1D,
  197. 0x1E,
  198. 0x1F,
  199. ],
  200. ]
  201. let ivs: [[UInt8]] = [
  202. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  203. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  204. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00],
  205. [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  206. [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00],
  207. ]
  208. let messages = [
  209. "76B8E0ADA0F13D90405D6AE55386BD28BDD219B8A08DED1AA836EFCC8B770DC7DA41597C5157488D7724E03FB8D84A376A43B8F41518A11CC387B669B2EE6586",
  210. "4540F05A9F1FB296D7736E7B208E3C96EB4FE1834688D2604F450952ED432D41BBE2A0B6EA7566D2A5D1E7E20D42AF2C53D792B1C43FEA817E9AD275AE546963",
  211. "DE9CBA7BF3D69EF5E786DC63973F653A0B49E015ADBFF7134FCB7DF137821031E85A050278A7084527214F73EFC7FA5B5277062EB7A0433E445F41E3",
  212. "EF3FDFD6C61578FBF5CF35BD3DD33B8009631634D21E42AC33960BD138E50D32111E4CAF237EE53CA8AD6426194A88545DDC497A0B466E7D6BBDB0041B2F586B",
  213. "F798A189F195E66982105FFB640BB7757F579DA31602FC93EC01AC56F85AC3C134A4547B733B46413042C9440049176905D3BE59EA1C53F15916155C2BE8241A38008B9A26BC35941E2444177C8ADE6689DE95264986D95889FB60E84629C9BD9A5ACB1CC118BE563EB9B3A4A472F82E09A7E778492B562EF7130E88DFE031C79DB9D4F7C7A899151B9A475032B63FC385245FE054E3DD5A97A5F576FE064025D3CE042C566AB2C507B138DB853E3D6959660996546CC9C4A6EAFDC777C040D70EAF46F76DAD3979E5C5360C3317166A1C894C94A371876A94DF7628FE4EAAF2CCB27D5AAAE0AD7AD0F9D4B6AD3B54098746D4524D38407A6DEB3AB78FAB78C9",
  214. ]
  215. for index in 0 ..< keys.count {
  216. let key = Bytes(rawValue: keys[index])
  217. let iv = Bytes(rawValue: ivs[index])
  218. let data = Bytes(string: messages[index], using: .utf8)!
  219. do {
  220. let encrypted = try ChaCha20(key: key, iv: iv).encrypt(data: data)
  221. let decrypted = try ChaCha20(key: key, iv: iv).decrypt(data: encrypted)
  222. let message = String(bytes: decrypted, encoding: .utf8)
  223. XCTAssertEqual(message, messages[index], "ChaCha20 decryption failed")
  224. } catch {
  225. XCTAssert(false, "Failed")
  226. }
  227. }
  228. }
  229. }