lkcp9_crypt_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
  2. // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
  3. // Copyright © 2020 Gridfinity, LLC. <admin@gridfinity.com>.
  4. // Copyright © 2020 Jeffrey H. Johnson <jeff@gridfinity.com>.
  5. //
  6. // All rights reserved.
  7. //
  8. // All use of this code is governed by the MIT license.
  9. // The complete license is available in the LICENSE file.
  10. package lkcp9_test
  11. import (
  12. "bytes"
  13. "crypto/aes"
  14. "crypto/md5"
  15. "crypto/rand"
  16. "hash/crc32"
  17. "io"
  18. "testing"
  19. "go.gridfinity.dev/lkcp9"
  20. u "go.gridfinity.dev/leaktestfe"
  21. )
  22. func TestSM4(
  23. t *testing.T,
  24. ) {
  25. defer u.Leakplug(
  26. t,
  27. )
  28. bc, err := lkcp9.NewSM4BlockCrypt(
  29. pass[:16],
  30. )
  31. if err != nil {
  32. t.Fatal(
  33. err,
  34. )
  35. }
  36. cryptTest(
  37. t,
  38. bc,
  39. )
  40. }
  41. func TestAES(
  42. t *testing.T,
  43. ) {
  44. defer u.Leakplug(
  45. t,
  46. )
  47. bc, err := lkcp9.NewAESBlockCrypt(
  48. pass[:32],
  49. )
  50. if err != nil {
  51. t.Fatal(
  52. err,
  53. )
  54. }
  55. cryptTest(
  56. t,
  57. bc,
  58. )
  59. }
  60. func TestXOR(
  61. t *testing.T,
  62. ) {
  63. defer u.Leakplug(
  64. t,
  65. )
  66. bc, err := lkcp9.NewSimpleXORBlockCrypt(
  67. pass[:32],
  68. )
  69. if err != nil {
  70. t.Fatal(
  71. err,
  72. )
  73. }
  74. cryptTest(
  75. t,
  76. bc,
  77. )
  78. }
  79. func TestNone(
  80. t *testing.T,
  81. ) {
  82. defer u.Leakplug(
  83. t,
  84. )
  85. bc, err := lkcp9.NewNoneBlockCrypt(pass[:32])
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. cryptTest(
  90. t,
  91. bc,
  92. )
  93. }
  94. func TestSalsa20(
  95. t *testing.T,
  96. ) {
  97. defer u.Leakplug(
  98. t,
  99. )
  100. bc, err := lkcp9.NewSalsa20BlockCrypt(
  101. pass[:32],
  102. )
  103. if err != nil {
  104. t.Fatal(
  105. err,
  106. )
  107. }
  108. cryptTest(
  109. t,
  110. bc,
  111. )
  112. }
  113. func cryptTest(
  114. t *testing.T,
  115. bc lkcp9.BlockCrypt,
  116. ) {
  117. defer u.Leakplug(
  118. t,
  119. )
  120. data := make(
  121. []byte,
  122. lkcp9.KcpMtuLimit,
  123. )
  124. io.ReadFull(
  125. rand.Reader,
  126. data,
  127. )
  128. dec := make(
  129. []byte,
  130. lkcp9.KcpMtuLimit,
  131. )
  132. enc := make(
  133. []byte,
  134. lkcp9.KcpMtuLimit,
  135. )
  136. bc.Encrypt(
  137. enc,
  138. data,
  139. )
  140. bc.Decrypt(
  141. dec,
  142. enc,
  143. )
  144. if !bytes.Equal(
  145. data,
  146. dec,
  147. ) {
  148. t.Fail()
  149. }
  150. }
  151. func BenchmarkSM4(b *testing.B) {
  152. bc, err := lkcp9.NewSM4BlockCrypt(pass[:16])
  153. if err != nil {
  154. b.Fatal(err)
  155. }
  156. benchCrypt(b, bc)
  157. }
  158. func BenchmarkAES128(b *testing.B) {
  159. bc, err := lkcp9.NewAESBlockCrypt(
  160. pass[:16],
  161. )
  162. if err != nil {
  163. b.Fatal(err)
  164. }
  165. benchCrypt(
  166. b,
  167. bc,
  168. )
  169. }
  170. func BenchmarkAES192(b *testing.B) {
  171. bc, err := lkcp9.NewAESBlockCrypt(
  172. pass[:24],
  173. )
  174. if err != nil {
  175. b.Fatal(
  176. err,
  177. )
  178. }
  179. benchCrypt(
  180. b,
  181. bc,
  182. )
  183. }
  184. func BenchmarkAES256(
  185. b *testing.B,
  186. ) {
  187. bc, err := lkcp9.NewAESBlockCrypt(
  188. pass[:32],
  189. )
  190. if err != nil {
  191. b.Fatal(
  192. err,
  193. )
  194. }
  195. benchCrypt(
  196. b,
  197. bc,
  198. )
  199. }
  200. func BenchmarkXOR(
  201. b *testing.B,
  202. ) {
  203. bc, err := lkcp9.NewSimpleXORBlockCrypt(
  204. pass[:32],
  205. )
  206. if err != nil {
  207. b.Fatal(
  208. err,
  209. )
  210. }
  211. benchCrypt(
  212. b,
  213. bc,
  214. )
  215. }
  216. func BenchmarkNone(
  217. b *testing.B,
  218. ) {
  219. bc, err := lkcp9.NewNoneBlockCrypt(
  220. pass[:32],
  221. )
  222. if err != nil {
  223. b.Fatal(err)
  224. }
  225. benchCrypt(
  226. b,
  227. bc,
  228. )
  229. }
  230. func BenchmarkSalsa20(b *testing.B) {
  231. bc, err := lkcp9.NewSalsa20BlockCrypt(pass[:32])
  232. if err != nil {
  233. b.Fatal(
  234. err,
  235. )
  236. }
  237. benchCrypt(
  238. b,
  239. bc,
  240. )
  241. }
  242. func benchCrypt(
  243. b *testing.B,
  244. bc lkcp9.BlockCrypt,
  245. ) {
  246. data := make(
  247. []byte,
  248. lkcp9.KcpMtuLimit,
  249. )
  250. io.ReadFull(
  251. rand.Reader,
  252. data,
  253. )
  254. dec := make(
  255. []byte,
  256. lkcp9.KcpMtuLimit,
  257. )
  258. enc := make(
  259. []byte,
  260. lkcp9.KcpMtuLimit,
  261. )
  262. b.ReportAllocs()
  263. b.SetBytes(
  264. int64(len(
  265. enc,
  266. ) * 2,
  267. ),
  268. )
  269. b.ResetTimer()
  270. for i := 0; i < b.N; i++ {
  271. bc.Encrypt(
  272. enc,
  273. data,
  274. )
  275. bc.Decrypt(
  276. dec,
  277. enc,
  278. )
  279. }
  280. }
  281. func BenchmarkCRC32(
  282. b *testing.B,
  283. ) {
  284. content := make(
  285. []byte,
  286. 1024,
  287. )
  288. b.SetBytes(
  289. int64(
  290. len(
  291. content,
  292. ),
  293. ),
  294. )
  295. for i := 0; i < b.N; i++ {
  296. crc32.ChecksumIEEE(
  297. content,
  298. )
  299. }
  300. }
  301. func BenchmarkCsprngSystem(
  302. b *testing.B,
  303. ) {
  304. data := make([]byte, md5.Size)
  305. b.SetBytes(int64(len(data)))
  306. for i := 0; i < b.N; i++ {
  307. io.ReadFull(rand.Reader, data)
  308. }
  309. }
  310. func BenchmarkCsprngMD5(b *testing.B) {
  311. var data [md5.Size]byte
  312. b.SetBytes(
  313. md5.Size,
  314. )
  315. for i := 0; i < b.N; i++ {
  316. data = md5.Sum(
  317. data[:],
  318. )
  319. }
  320. }
  321. func BenchmarkCsprngKcpNonceMD5(
  322. b *testing.B,
  323. ) {
  324. var ng lkcp9.KcpNonceMD5
  325. ng.Init()
  326. b.SetBytes(
  327. md5.Size,
  328. )
  329. data := make(
  330. []byte,
  331. md5.Size,
  332. )
  333. for i := 0; i < b.N; i++ {
  334. ng.Fill(
  335. data,
  336. )
  337. }
  338. }
  339. func BenchmarkCsprngNonceAES128(
  340. b *testing.B,
  341. ) {
  342. var ng lkcp9.KcpNonceAES128
  343. ng.Init()
  344. b.SetBytes(
  345. aes.BlockSize,
  346. )
  347. data := make(
  348. []byte,
  349. aes.BlockSize,
  350. )
  351. for i := 0; i < b.N; i++ {
  352. ng.Fill(
  353. data,
  354. )
  355. }
  356. }