lkcp9_crypt.go 18 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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 // import "go.gridfinity.dev/lkcp9"
  11. import (
  12. "crypto/aes"
  13. "crypto/cipher"
  14. "hash/fnv"
  15. "github.com/templexxx/xor"
  16. "github.com/tjfoc/gmsm/sm4"
  17. "golang.org/x/crypto/pbkdf2"
  18. "golang.org/x/crypto/salsa20"
  19. )
  20. const (
  21. chunk = 64
  22. )
  23. var (
  24. initialVector = []byte{
  25. 167, 115, 79, 156,
  26. 18, 172, 27, 1,
  27. 164, 21, 242, 193,
  28. 252, 120, 230, 107,
  29. }
  30. saltxor = `sH3CIVoF#rWLtJo6`
  31. )
  32. type digest struct {
  33. h [5]uint32
  34. x [chunk]byte
  35. nx int
  36. len uint64
  37. }
  38. // BlockCrypt defines crypto methods for a given byte slice
  39. type BlockCrypt interface {
  40. Encrypt(
  41. dst,
  42. src []byte,
  43. )
  44. Decrypt(
  45. dst,
  46. src []byte,
  47. )
  48. }
  49. type salsa20BlockCrypt struct {
  50. key [32]byte
  51. }
  52. // NewSalsa20BlockCrypt function
  53. func NewSalsa20BlockCrypt(
  54. key []byte,
  55. ) (
  56. BlockCrypt,
  57. error,
  58. ) {
  59. c := new(
  60. salsa20BlockCrypt,
  61. )
  62. copy(
  63. c.key[:],
  64. key,
  65. )
  66. return c, nil
  67. }
  68. func (
  69. c *salsa20BlockCrypt,
  70. ) Encrypt(
  71. dst,
  72. src []byte,
  73. ) {
  74. salsa20.XORKeyStream(
  75. dst[8:],
  76. src[8:],
  77. src[:8],
  78. &c.key)
  79. copy(
  80. dst[:8],
  81. src[:8],
  82. )
  83. }
  84. func (
  85. c *salsa20BlockCrypt,
  86. ) Decrypt(
  87. dst,
  88. src []byte,
  89. ) {
  90. salsa20.XORKeyStream(
  91. dst[8:],
  92. src[8:],
  93. src[:8],
  94. &c.key)
  95. copy(
  96. dst[:8],
  97. src[:8],
  98. )
  99. }
  100. type sm4BlockCrypt struct {
  101. encbuf [sm4.BlockSize]byte
  102. decbuf [2 * sm4.BlockSize]byte
  103. block cipher.Block
  104. }
  105. // NewSM4BlockCrypt function
  106. func NewSM4BlockCrypt(
  107. key []byte,
  108. ) (
  109. BlockCrypt,
  110. error,
  111. ) {
  112. c := new(
  113. sm4BlockCrypt,
  114. )
  115. block, err := sm4.NewCipher(
  116. key,
  117. )
  118. if err != nil {
  119. return nil, err
  120. }
  121. c.block = block
  122. return c, nil
  123. }
  124. func (
  125. c *sm4BlockCrypt,
  126. ) Encrypt(
  127. dst,
  128. src []byte,
  129. ) {
  130. encrypt(
  131. c.block,
  132. dst,
  133. src,
  134. c.encbuf[:],
  135. )
  136. }
  137. func (
  138. c *sm4BlockCrypt,
  139. ) Decrypt(
  140. dst,
  141. src []byte,
  142. ) {
  143. decrypt(
  144. c.block,
  145. dst,
  146. src,
  147. c.decbuf[:],
  148. )
  149. }
  150. type aesBlockCrypt struct {
  151. encbuf [aes.BlockSize]byte
  152. decbuf [2 * aes.BlockSize]byte
  153. block cipher.Block
  154. }
  155. // NewAESBlockCrypt function
  156. func NewAESBlockCrypt(
  157. key []byte,
  158. ) (
  159. BlockCrypt,
  160. error,
  161. ) {
  162. c := new(
  163. aesBlockCrypt,
  164. )
  165. block, err := aes.NewCipher(
  166. key,
  167. )
  168. if err != nil {
  169. return nil, err
  170. }
  171. c.block = block
  172. return c, nil
  173. }
  174. func (
  175. c *aesBlockCrypt,
  176. ) Encrypt(
  177. dst,
  178. src []byte,
  179. ) {
  180. encrypt(
  181. c.block,
  182. dst,
  183. src,
  184. c.encbuf[:],
  185. )
  186. }
  187. func (
  188. c *aesBlockCrypt,
  189. ) Decrypt(
  190. dst,
  191. src []byte,
  192. ) {
  193. decrypt(
  194. c.block,
  195. dst,
  196. src,
  197. c.decbuf[:],
  198. )
  199. }
  200. type simpleXORBlockCrypt struct {
  201. xortbl []byte
  202. }
  203. // NewSimpleXORBlockCrypt function
  204. func NewSimpleXORBlockCrypt(
  205. key []byte,
  206. ) (
  207. BlockCrypt,
  208. error,
  209. ) {
  210. c := new(
  211. simpleXORBlockCrypt,
  212. )
  213. c.xortbl = pbkdf2.Key(
  214. key,
  215. []byte(saltxor),
  216. 32,
  217. KcpMtuLimit,
  218. fnv.New128a,
  219. )
  220. return c, nil
  221. }
  222. func (
  223. c *simpleXORBlockCrypt,
  224. ) Encrypt(
  225. dst,
  226. src []byte,
  227. ) {
  228. xor.Bytes(
  229. dst,
  230. src,
  231. c.xortbl,
  232. )
  233. }
  234. func (
  235. c *simpleXORBlockCrypt,
  236. ) Decrypt(
  237. dst,
  238. src []byte,
  239. ) {
  240. xor.Bytes(
  241. dst,
  242. src,
  243. c.xortbl,
  244. )
  245. }
  246. type noneBlockCrypt struct{}
  247. // NewNoneBlockCrypt function (null encryption)
  248. func NewNoneBlockCrypt(
  249. key []byte,
  250. ) (
  251. BlockCrypt,
  252. error,
  253. ) {
  254. return new(
  255. noneBlockCrypt,
  256. ), nil
  257. }
  258. func (
  259. c *noneBlockCrypt,
  260. ) Encrypt(
  261. dst,
  262. src []byte,
  263. ) {
  264. copy(
  265. dst,
  266. src,
  267. )
  268. }
  269. func (
  270. c *noneBlockCrypt,
  271. ) Decrypt(
  272. dst,
  273. src []byte,
  274. ) {
  275. copy(
  276. dst,
  277. src,
  278. )
  279. }
  280. func encrypt(
  281. block cipher.Block,
  282. dst,
  283. src,
  284. buf []byte,
  285. ) {
  286. switch block.BlockSize() {
  287. case 8:
  288. encrypt8(block, dst, src, buf)
  289. case 16:
  290. encrypt16(block, dst, src, buf)
  291. default:
  292. encryptVariant(block, dst, src, buf)
  293. }
  294. }
  295. func encrypt8(block cipher.Block, dst, src, buf []byte) {
  296. tbl := buf[:8]
  297. block.Encrypt(tbl, initialVector)
  298. n := len(
  299. src,
  300. ) / 8
  301. base := 0
  302. repeat := n / 8
  303. left := n % 8
  304. for i := 0; i < repeat; i++ {
  305. s := src[base:][0:64]
  306. d := dst[base:][0:64]
  307. xor.BytesSrc1(
  308. d[0:8],
  309. s[0:8],
  310. tbl,
  311. )
  312. block.Encrypt(
  313. tbl,
  314. d[0:8],
  315. )
  316. xor.BytesSrc1(
  317. d[8:16],
  318. s[8:16],
  319. tbl,
  320. )
  321. block.Encrypt(
  322. tbl,
  323. d[8:16],
  324. )
  325. xor.BytesSrc1(
  326. d[16:24],
  327. s[16:24],
  328. tbl,
  329. )
  330. block.Encrypt(
  331. tbl,
  332. d[16:24],
  333. )
  334. xor.BytesSrc1(
  335. d[24:32],
  336. s[24:32],
  337. tbl,
  338. )
  339. block.Encrypt(
  340. tbl,
  341. d[24:32],
  342. )
  343. xor.BytesSrc1(
  344. d[32:40],
  345. s[32:40],
  346. tbl,
  347. )
  348. block.Encrypt(
  349. tbl,
  350. d[32:40],
  351. )
  352. xor.BytesSrc1(
  353. d[40:48],
  354. s[40:48],
  355. tbl,
  356. )
  357. block.Encrypt(
  358. tbl,
  359. d[40:48],
  360. )
  361. xor.BytesSrc1(
  362. d[48:56],
  363. s[48:56],
  364. tbl,
  365. )
  366. block.Encrypt(
  367. tbl,
  368. d[48:56],
  369. )
  370. xor.BytesSrc1(
  371. d[56:64],
  372. s[56:64],
  373. tbl,
  374. )
  375. block.Encrypt(
  376. tbl,
  377. d[56:64],
  378. )
  379. base += 64
  380. }
  381. switch left {
  382. case 7:
  383. xor.BytesSrc1(
  384. dst[base:],
  385. src[base:],
  386. tbl,
  387. )
  388. block.Encrypt(
  389. tbl,
  390. dst[base:],
  391. )
  392. base += 8
  393. fallthrough
  394. case 6:
  395. xor.BytesSrc1(
  396. dst[base:],
  397. src[base:],
  398. tbl,
  399. )
  400. block.Encrypt(
  401. tbl,
  402. dst[base:],
  403. )
  404. base += 8
  405. fallthrough
  406. case 5:
  407. xor.BytesSrc1(
  408. dst[base:],
  409. src[base:],
  410. tbl,
  411. )
  412. block.Encrypt(
  413. tbl,
  414. dst[base:],
  415. )
  416. base += 8
  417. fallthrough
  418. case 4:
  419. xor.BytesSrc1(
  420. dst[base:],
  421. src[base:],
  422. tbl,
  423. )
  424. block.Encrypt(
  425. tbl,
  426. dst[base:],
  427. )
  428. base += 8
  429. fallthrough
  430. case 3:
  431. xor.BytesSrc1(
  432. dst[base:],
  433. src[base:],
  434. tbl,
  435. )
  436. block.Encrypt(
  437. tbl,
  438. dst[base:],
  439. )
  440. base += 8
  441. fallthrough
  442. case 2:
  443. xor.BytesSrc1(dst[base:], src[base:], tbl)
  444. block.Encrypt(tbl, dst[base:])
  445. base += 8
  446. fallthrough
  447. case 1:
  448. xor.BytesSrc1(dst[base:], src[base:], tbl)
  449. block.Encrypt(tbl, dst[base:])
  450. base += 8
  451. fallthrough
  452. case 0:
  453. xor.BytesSrc0(dst[base:], src[base:], tbl)
  454. }
  455. }
  456. func encrypt16(
  457. block cipher.Block,
  458. dst,
  459. src,
  460. buf []byte,
  461. ) {
  462. tbl := buf[:16]
  463. block.Encrypt(
  464. tbl,
  465. initialVector,
  466. )
  467. n := len(
  468. src,
  469. ) / 16
  470. base := 0
  471. repeat := n / 8
  472. left := n % 8
  473. for i := 0; i < repeat; i++ {
  474. s := src[base:][0:128]
  475. d := dst[base:][0:128]
  476. xor.BytesSrc1(
  477. d[0:16],
  478. s[0:16],
  479. tbl,
  480. )
  481. block.Encrypt(
  482. tbl,
  483. d[0:16],
  484. )
  485. xor.BytesSrc1(
  486. d[16:32],
  487. s[16:32],
  488. tbl,
  489. )
  490. block.Encrypt(
  491. tbl,
  492. d[16:32],
  493. )
  494. xor.BytesSrc1(
  495. d[32:48],
  496. s[32:48],
  497. tbl,
  498. )
  499. block.Encrypt(
  500. tbl,
  501. d[32:48],
  502. )
  503. xor.BytesSrc1(
  504. d[48:64],
  505. s[48:64],
  506. tbl,
  507. )
  508. block.Encrypt(
  509. tbl,
  510. d[48:64],
  511. )
  512. xor.BytesSrc1(
  513. d[64:80],
  514. s[64:80],
  515. tbl,
  516. )
  517. block.Encrypt(
  518. tbl,
  519. d[64:80],
  520. )
  521. xor.BytesSrc1(
  522. d[80:96],
  523. s[80:96],
  524. tbl,
  525. )
  526. block.Encrypt(
  527. tbl,
  528. d[80:96],
  529. )
  530. xor.BytesSrc1(
  531. d[96:112],
  532. s[96:112],
  533. tbl,
  534. )
  535. block.Encrypt(
  536. tbl,
  537. d[96:112],
  538. )
  539. xor.BytesSrc1(
  540. d[112:128],
  541. s[112:128],
  542. tbl,
  543. )
  544. block.Encrypt(tbl, d[112:128])
  545. base += 128
  546. }
  547. switch left {
  548. case 7:
  549. xor.BytesSrc1(
  550. dst[base:],
  551. src[base:],
  552. tbl,
  553. )
  554. block.Encrypt(
  555. tbl,
  556. dst[base:],
  557. )
  558. base += 16
  559. fallthrough
  560. case 6:
  561. xor.BytesSrc1(
  562. dst[base:],
  563. src[base:],
  564. tbl,
  565. )
  566. block.Encrypt(
  567. tbl,
  568. dst[base:],
  569. )
  570. base += 16
  571. fallthrough
  572. case 5:
  573. xor.BytesSrc1(
  574. dst[base:],
  575. src[base:],
  576. tbl,
  577. )
  578. block.Encrypt(
  579. tbl,
  580. dst[base:],
  581. )
  582. base += 16
  583. fallthrough
  584. case 4:
  585. xor.BytesSrc1(
  586. dst[base:],
  587. src[base:],
  588. tbl,
  589. )
  590. block.Encrypt(
  591. tbl,
  592. dst[base:],
  593. )
  594. base += 16
  595. fallthrough
  596. case 3:
  597. xor.BytesSrc1(
  598. dst[base:],
  599. src[base:],
  600. tbl,
  601. )
  602. block.Encrypt(
  603. tbl,
  604. dst[base:],
  605. )
  606. base += 16
  607. fallthrough
  608. case 2:
  609. xor.BytesSrc1(
  610. dst[base:],
  611. src[base:],
  612. tbl,
  613. )
  614. block.Encrypt(
  615. tbl,
  616. dst[base:],
  617. )
  618. base += 16
  619. fallthrough
  620. case 1:
  621. xor.BytesSrc1(
  622. dst[base:],
  623. src[base:],
  624. tbl,
  625. )
  626. block.Encrypt(
  627. tbl,
  628. dst[base:],
  629. )
  630. base += 16
  631. fallthrough
  632. case 0:
  633. xor.BytesSrc0(dst[base:], src[base:], tbl)
  634. }
  635. }
  636. func encryptVariant(
  637. block cipher.Block,
  638. dst,
  639. src,
  640. buf []byte,
  641. ) {
  642. blocksize := block.BlockSize()
  643. tbl := buf[:blocksize]
  644. block.Encrypt(
  645. tbl,
  646. initialVector,
  647. )
  648. n := len(
  649. src,
  650. ) / blocksize
  651. base := 0
  652. repeat := n / 8
  653. left := n % 8
  654. for i := 0; i < repeat; i++ {
  655. xor.BytesSrc1(
  656. dst[base:],
  657. src[base:],
  658. tbl,
  659. )
  660. block.Encrypt(
  661. tbl,
  662. dst[base:],
  663. )
  664. base += blocksize
  665. xor.BytesSrc1(
  666. dst[base:],
  667. src[base:],
  668. tbl,
  669. )
  670. block.Encrypt(
  671. tbl,
  672. dst[base:],
  673. )
  674. base += blocksize
  675. xor.BytesSrc1(
  676. dst[base:],
  677. src[base:],
  678. tbl,
  679. )
  680. block.Encrypt(
  681. tbl,
  682. dst[base:],
  683. )
  684. base += blocksize
  685. xor.BytesSrc1(
  686. dst[base:],
  687. src[base:],
  688. tbl,
  689. )
  690. block.Encrypt(
  691. tbl,
  692. dst[base:],
  693. )
  694. base += blocksize
  695. xor.BytesSrc1(
  696. dst[base:],
  697. src[base:],
  698. tbl,
  699. )
  700. block.Encrypt(
  701. tbl,
  702. dst[base:],
  703. )
  704. base += blocksize
  705. xor.BytesSrc1(
  706. dst[base:],
  707. src[base:],
  708. tbl,
  709. )
  710. block.Encrypt(
  711. tbl,
  712. dst[base:],
  713. )
  714. base += blocksize
  715. xor.BytesSrc1(
  716. dst[base:],
  717. src[base:],
  718. tbl,
  719. )
  720. block.Encrypt(
  721. tbl,
  722. dst[base:],
  723. )
  724. base += blocksize
  725. xor.BytesSrc1(
  726. dst[base:],
  727. src[base:],
  728. tbl,
  729. )
  730. block.Encrypt(
  731. tbl,
  732. dst[base:],
  733. )
  734. base += blocksize
  735. }
  736. switch left {
  737. case 7:
  738. xor.BytesSrc1(
  739. dst[base:],
  740. src[base:],
  741. tbl,
  742. )
  743. block.Encrypt(
  744. tbl,
  745. dst[base:],
  746. )
  747. base += blocksize
  748. fallthrough
  749. case 6:
  750. xor.BytesSrc1(
  751. dst[base:],
  752. src[base:],
  753. tbl,
  754. )
  755. block.Encrypt(
  756. tbl,
  757. dst[base:],
  758. )
  759. base += blocksize
  760. fallthrough
  761. case 5:
  762. xor.BytesSrc1(
  763. dst[base:],
  764. src[base:],
  765. tbl,
  766. )
  767. block.Encrypt(
  768. tbl,
  769. dst[base:],
  770. )
  771. base += blocksize
  772. fallthrough
  773. case 4:
  774. xor.BytesSrc1(
  775. dst[base:],
  776. src[base:],
  777. tbl,
  778. )
  779. block.Encrypt(
  780. tbl,
  781. dst[base:],
  782. )
  783. base += blocksize
  784. fallthrough
  785. case 3:
  786. xor.BytesSrc1(
  787. dst[base:],
  788. src[base:],
  789. tbl,
  790. )
  791. block.Encrypt(
  792. tbl,
  793. dst[base:],
  794. )
  795. base += blocksize
  796. fallthrough
  797. case 2:
  798. xor.BytesSrc1(
  799. dst[base:],
  800. src[base:],
  801. tbl,
  802. )
  803. block.Encrypt(
  804. tbl,
  805. dst[base:],
  806. )
  807. base += blocksize
  808. fallthrough
  809. case 1:
  810. xor.BytesSrc1(
  811. dst[base:],
  812. src[base:],
  813. tbl,
  814. )
  815. block.Encrypt(
  816. tbl,
  817. dst[base:],
  818. )
  819. base += blocksize
  820. fallthrough
  821. case 0:
  822. xor.BytesSrc0(
  823. dst[base:],
  824. src[base:],
  825. tbl,
  826. )
  827. }
  828. }
  829. func decrypt(
  830. block cipher.Block,
  831. dst,
  832. src,
  833. buf []byte,
  834. ) {
  835. switch block.BlockSize() {
  836. case 8:
  837. decrypt8(
  838. block,
  839. dst,
  840. src,
  841. buf,
  842. )
  843. case 16:
  844. decrypt16(
  845. block,
  846. dst,
  847. src,
  848. buf,
  849. )
  850. default:
  851. decryptVariant(
  852. block,
  853. dst,
  854. src,
  855. buf,
  856. )
  857. }
  858. }
  859. func decrypt8(
  860. block cipher.Block,
  861. dst,
  862. src,
  863. buf []byte,
  864. ) {
  865. tbl := buf[0:8]
  866. next := buf[8:16]
  867. block.Encrypt(
  868. tbl,
  869. initialVector,
  870. )
  871. n := len(
  872. src,
  873. ) / 8
  874. base := 0
  875. repeat := n / 8
  876. left := n % 8
  877. for i := 0; i < repeat; i++ {
  878. s := src[base:][0:64]
  879. d := dst[base:][0:64]
  880. block.Encrypt(
  881. next,
  882. s[0:8],
  883. )
  884. xor.BytesSrc1(
  885. d[0:8],
  886. s[0:8],
  887. tbl,
  888. )
  889. block.Encrypt(
  890. tbl,
  891. s[8:16],
  892. )
  893. xor.BytesSrc1(
  894. d[8:16],
  895. s[8:16],
  896. next,
  897. )
  898. block.Encrypt(
  899. next,
  900. s[16:24],
  901. )
  902. xor.BytesSrc1(
  903. d[16:24],
  904. s[16:24],
  905. tbl,
  906. )
  907. block.Encrypt(
  908. tbl,
  909. s[24:32],
  910. )
  911. xor.BytesSrc1(
  912. d[24:32],
  913. s[24:32],
  914. next,
  915. )
  916. block.Encrypt(
  917. next,
  918. s[32:40],
  919. )
  920. xor.BytesSrc1(
  921. d[32:40],
  922. s[32:40],
  923. tbl,
  924. )
  925. block.Encrypt(
  926. tbl,
  927. s[40:48],
  928. )
  929. xor.BytesSrc1(
  930. d[40:48],
  931. s[40:48],
  932. next,
  933. )
  934. block.Encrypt(
  935. next,
  936. s[48:56],
  937. )
  938. xor.BytesSrc1(
  939. d[48:56],
  940. s[48:56],
  941. tbl,
  942. )
  943. block.Encrypt(
  944. tbl,
  945. s[56:64],
  946. )
  947. xor.BytesSrc1(
  948. d[56:64],
  949. s[56:64],
  950. next,
  951. )
  952. base += 64
  953. }
  954. switch left {
  955. case 7:
  956. block.Encrypt(
  957. next,
  958. src[base:],
  959. )
  960. xor.BytesSrc1(
  961. dst[base:],
  962. src[base:],
  963. tbl,
  964. )
  965. tbl,
  966. next = next,
  967. tbl
  968. base += 8
  969. fallthrough
  970. case 6:
  971. block.Encrypt(
  972. next,
  973. src[base:],
  974. )
  975. xor.BytesSrc1(
  976. dst[base:],
  977. src[base:],
  978. tbl,
  979. )
  980. tbl,
  981. next = next,
  982. tbl
  983. base += 8
  984. fallthrough
  985. case 5:
  986. block.Encrypt(
  987. next,
  988. src[base:],
  989. )
  990. xor.BytesSrc1(
  991. dst[base:],
  992. src[base:],
  993. tbl,
  994. )
  995. tbl, next = next, tbl
  996. base += 8
  997. fallthrough
  998. case 4:
  999. block.Encrypt(
  1000. next,
  1001. src[base:],
  1002. )
  1003. xor.BytesSrc1(
  1004. dst[base:],
  1005. src[base:],
  1006. tbl,
  1007. )
  1008. tbl,
  1009. next = next,
  1010. tbl
  1011. base += 8
  1012. fallthrough
  1013. case 3:
  1014. block.Encrypt(
  1015. next,
  1016. src[base:],
  1017. )
  1018. xor.BytesSrc1(dst[base:], src[base:], tbl)
  1019. tbl, next = next, tbl
  1020. base += 8
  1021. fallthrough
  1022. case 2:
  1023. block.Encrypt(
  1024. next,
  1025. src[base:],
  1026. )
  1027. xor.BytesSrc1(
  1028. dst[base:],
  1029. src[base:],
  1030. tbl,
  1031. )
  1032. tbl,
  1033. next = next,
  1034. tbl
  1035. base += 8
  1036. fallthrough
  1037. case 1:
  1038. block.Encrypt(next,
  1039. src[base:],
  1040. )
  1041. xor.BytesSrc1(
  1042. dst[base:],
  1043. src[base:],
  1044. tbl,
  1045. )
  1046. tbl,
  1047. next = next,
  1048. tbl
  1049. base += 8
  1050. fallthrough
  1051. case 0:
  1052. xor.BytesSrc0(
  1053. dst[base:],
  1054. src[base:],
  1055. tbl,
  1056. )
  1057. }
  1058. }
  1059. func decrypt16(
  1060. block cipher.Block,
  1061. dst,
  1062. src,
  1063. buf []byte,
  1064. ) {
  1065. tbl := buf[0:16]
  1066. next := buf[16:32]
  1067. block.Encrypt(
  1068. tbl,
  1069. initialVector,
  1070. )
  1071. n := len(
  1072. src,
  1073. ) / 16
  1074. base := 0
  1075. repeat := n / 8
  1076. left := n % 8
  1077. for i := 0; i < repeat; i++ {
  1078. s := src[base:][0:128]
  1079. d := dst[base:][0:128]
  1080. block.Encrypt(
  1081. next,
  1082. s[0:16],
  1083. )
  1084. xor.BytesSrc1(
  1085. d[0:16],
  1086. s[0:16],
  1087. tbl,
  1088. )
  1089. block.Encrypt(
  1090. tbl,
  1091. s[16:32],
  1092. )
  1093. xor.BytesSrc1(
  1094. d[16:32],
  1095. s[16:32],
  1096. next,
  1097. )
  1098. block.Encrypt(
  1099. next,
  1100. s[32:48],
  1101. )
  1102. xor.BytesSrc1(
  1103. d[32:48],
  1104. s[32:48],
  1105. tbl,
  1106. )
  1107. block.Encrypt(
  1108. tbl,
  1109. s[48:64],
  1110. )
  1111. xor.BytesSrc1(
  1112. d[48:64],
  1113. s[48:64],
  1114. next,
  1115. )
  1116. block.Encrypt(
  1117. next,
  1118. s[64:80],
  1119. )
  1120. xor.BytesSrc1(
  1121. d[64:80],
  1122. s[64:80],
  1123. tbl,
  1124. )
  1125. block.Encrypt(
  1126. tbl,
  1127. s[80:96],
  1128. )
  1129. xor.BytesSrc1(
  1130. d[80:96],
  1131. s[80:96],
  1132. next,
  1133. )
  1134. block.Encrypt(
  1135. next,
  1136. s[96:112],
  1137. )
  1138. xor.BytesSrc1(
  1139. d[96:112],
  1140. s[96:112],
  1141. tbl,
  1142. )
  1143. block.Encrypt(
  1144. tbl,
  1145. s[112:128],
  1146. )
  1147. xor.BytesSrc1(
  1148. d[112:128],
  1149. s[112:128],
  1150. next,
  1151. )
  1152. base += 128
  1153. }
  1154. switch left {
  1155. case 7:
  1156. block.Encrypt(
  1157. next,
  1158. src[base:],
  1159. )
  1160. xor.BytesSrc1(
  1161. dst[base:],
  1162. src[base:],
  1163. tbl,
  1164. )
  1165. tbl,
  1166. next = next,
  1167. tbl
  1168. base += 16
  1169. fallthrough
  1170. case 6:
  1171. block.Encrypt(
  1172. next,
  1173. src[base:],
  1174. )
  1175. xor.BytesSrc1(
  1176. dst[base:],
  1177. src[base:],
  1178. tbl,
  1179. )
  1180. tbl, next = next, tbl
  1181. base += 16
  1182. fallthrough
  1183. case 5:
  1184. block.Encrypt(
  1185. next,
  1186. src[base:],
  1187. )
  1188. xor.BytesSrc1(
  1189. dst[base:],
  1190. src[base:],
  1191. tbl,
  1192. )
  1193. tbl,
  1194. next = next,
  1195. tbl
  1196. base += 16
  1197. fallthrough
  1198. case 4:
  1199. block.Encrypt(
  1200. next,
  1201. src[base:],
  1202. )
  1203. xor.BytesSrc1(
  1204. dst[base:],
  1205. src[base:],
  1206. tbl,
  1207. )
  1208. tbl,
  1209. next = next,
  1210. tbl
  1211. base += 16
  1212. fallthrough
  1213. case 3:
  1214. block.Encrypt(
  1215. next,
  1216. src[base:],
  1217. )
  1218. xor.BytesSrc1(
  1219. dst[base:],
  1220. src[base:],
  1221. tbl,
  1222. )
  1223. tbl,
  1224. next = next,
  1225. tbl
  1226. base += 16
  1227. fallthrough
  1228. case 2:
  1229. block.Encrypt(
  1230. next,
  1231. src[base:],
  1232. )
  1233. xor.BytesSrc1(
  1234. dst[base:],
  1235. src[base:],
  1236. tbl,
  1237. )
  1238. tbl,
  1239. next = next,
  1240. tbl
  1241. base += 16
  1242. fallthrough
  1243. case 1:
  1244. block.Encrypt(
  1245. next,
  1246. src[base:],
  1247. )
  1248. xor.BytesSrc1(
  1249. dst[base:],
  1250. src[base:],
  1251. tbl,
  1252. )
  1253. tbl,
  1254. next = next,
  1255. tbl
  1256. base += 16
  1257. fallthrough
  1258. case 0:
  1259. xor.BytesSrc0(
  1260. dst[base:],
  1261. src[base:],
  1262. tbl,
  1263. )
  1264. }
  1265. }
  1266. func decryptVariant(
  1267. block cipher.Block,
  1268. dst,
  1269. src,
  1270. buf []byte,
  1271. ) {
  1272. blocksize := block.BlockSize()
  1273. tbl := buf[:blocksize]
  1274. next := buf[blocksize:]
  1275. block.Encrypt(
  1276. tbl,
  1277. initialVector,
  1278. )
  1279. n := len(
  1280. src,
  1281. ) / blocksize
  1282. base := 0
  1283. repeat := n / 8
  1284. left := n % 8
  1285. for i := 0; i < repeat; i++ {
  1286. block.Encrypt(
  1287. next,
  1288. src[base:],
  1289. )
  1290. xor.BytesSrc1(
  1291. dst[base:],
  1292. src[base:],
  1293. tbl,
  1294. )
  1295. base += blocksize
  1296. block.Encrypt(
  1297. tbl,
  1298. src[base:],
  1299. )
  1300. xor.BytesSrc1(
  1301. dst[base:],
  1302. src[base:],
  1303. next,
  1304. )
  1305. base += blocksize
  1306. block.Encrypt(
  1307. next,
  1308. src[base:],
  1309. )
  1310. xor.BytesSrc1(
  1311. dst[base:],
  1312. src[base:],
  1313. tbl,
  1314. )
  1315. base += blocksize
  1316. block.Encrypt(
  1317. tbl,
  1318. src[base:],
  1319. )
  1320. xor.BytesSrc1(
  1321. dst[base:],
  1322. src[base:],
  1323. next,
  1324. )
  1325. base += blocksize
  1326. block.Encrypt(
  1327. next,
  1328. src[base:],
  1329. )
  1330. xor.BytesSrc1(
  1331. dst[base:],
  1332. src[base:],
  1333. tbl,
  1334. )
  1335. base += blocksize
  1336. block.Encrypt(
  1337. tbl,
  1338. src[base:],
  1339. )
  1340. xor.BytesSrc1(
  1341. dst[base:],
  1342. src[base:],
  1343. next,
  1344. )
  1345. base += blocksize
  1346. block.Encrypt(
  1347. next,
  1348. src[base:],
  1349. )
  1350. xor.BytesSrc1(
  1351. dst[base:],
  1352. src[base:],
  1353. tbl,
  1354. )
  1355. base += blocksize
  1356. block.Encrypt(
  1357. tbl,
  1358. src[base:],
  1359. )
  1360. xor.BytesSrc1(
  1361. dst[base:],
  1362. src[base:],
  1363. next,
  1364. )
  1365. base += blocksize
  1366. }
  1367. switch left {
  1368. case 7:
  1369. block.Encrypt(
  1370. next,
  1371. src[base:],
  1372. )
  1373. xor.BytesSrc1(
  1374. dst[base:],
  1375. src[base:],
  1376. tbl,
  1377. )
  1378. tbl,
  1379. next = next,
  1380. tbl
  1381. base += blocksize
  1382. fallthrough
  1383. case 6:
  1384. block.Encrypt(
  1385. next,
  1386. src[base:],
  1387. )
  1388. xor.BytesSrc1(
  1389. dst[base:],
  1390. src[base:],
  1391. tbl,
  1392. )
  1393. tbl,
  1394. next = next,
  1395. tbl
  1396. base += blocksize
  1397. fallthrough
  1398. case 5:
  1399. block.Encrypt(
  1400. next,
  1401. src[base:],
  1402. )
  1403. xor.BytesSrc1(
  1404. dst[base:],
  1405. src[base:],
  1406. tbl,
  1407. )
  1408. tbl,
  1409. next = next,
  1410. tbl
  1411. base += blocksize
  1412. fallthrough
  1413. case 4:
  1414. block.Encrypt(
  1415. next,
  1416. src[base:],
  1417. )
  1418. xor.BytesSrc1(
  1419. dst[base:],
  1420. src[base:],
  1421. tbl,
  1422. )
  1423. tbl, next = next, tbl
  1424. base += blocksize
  1425. fallthrough
  1426. case 3:
  1427. block.Encrypt(
  1428. next,
  1429. src[base:],
  1430. )
  1431. xor.BytesSrc1(
  1432. dst[base:],
  1433. src[base:],
  1434. tbl,
  1435. )
  1436. tbl, next = next, tbl
  1437. base += blocksize
  1438. fallthrough
  1439. case 2:
  1440. block.Encrypt(
  1441. next,
  1442. src[base:],
  1443. )
  1444. xor.BytesSrc1(
  1445. dst[base:],
  1446. src[base:],
  1447. tbl,
  1448. )
  1449. tbl, next = next, tbl
  1450. base += blocksize
  1451. fallthrough
  1452. case 1:
  1453. block.Encrypt(
  1454. next,
  1455. src[base:],
  1456. )
  1457. xor.BytesSrc1(
  1458. dst[base:],
  1459. src[base:],
  1460. tbl,
  1461. )
  1462. tbl, next = next, tbl
  1463. base += blocksize
  1464. fallthrough
  1465. case 0:
  1466. xor.BytesSrc0(
  1467. dst[base:],
  1468. src[base:],
  1469. tbl,
  1470. )
  1471. }
  1472. }