crypt.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. package kcp
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "crypto/des"
  6. "crypto/sha1"
  7. "github.com/templexxx/xor"
  8. "github.com/tjfoc/gmsm/sm4"
  9. "golang.org/x/crypto/blowfish"
  10. "golang.org/x/crypto/cast5"
  11. "golang.org/x/crypto/pbkdf2"
  12. "golang.org/x/crypto/salsa20"
  13. "golang.org/x/crypto/tea"
  14. "golang.org/x/crypto/twofish"
  15. "golang.org/x/crypto/xtea"
  16. )
  17. var (
  18. initialVector = []byte{167, 115, 79, 156, 18, 172, 27, 1, 164, 21, 242, 193, 252, 120, 230, 107}
  19. saltxor = `sH3CIVoF#rWLtJo6`
  20. )
  21. // BlockCrypt defines encryption/decryption methods for a given byte slice.
  22. // Notes on implementing: the data to be encrypted contains a builtin
  23. // nonce at the first 16 bytes
  24. type BlockCrypt interface {
  25. // Encrypt encrypts the whole block in src into dst.
  26. // Dst and src may point at the same memory.
  27. Encrypt(dst, src []byte)
  28. // Decrypt decrypts the whole block in src into dst.
  29. // Dst and src may point at the same memory.
  30. Decrypt(dst, src []byte)
  31. }
  32. type salsa20BlockCrypt struct {
  33. key [32]byte
  34. }
  35. // NewSalsa20BlockCrypt https://en.wikipedia.org/wiki/Salsa20
  36. func NewSalsa20BlockCrypt(key []byte) (BlockCrypt, error) {
  37. c := new(salsa20BlockCrypt)
  38. copy(c.key[:], key)
  39. return c, nil
  40. }
  41. func (c *salsa20BlockCrypt) Encrypt(dst, src []byte) {
  42. salsa20.XORKeyStream(dst[8:], src[8:], src[:8], &c.key)
  43. copy(dst[:8], src[:8])
  44. }
  45. func (c *salsa20BlockCrypt) Decrypt(dst, src []byte) {
  46. salsa20.XORKeyStream(dst[8:], src[8:], src[:8], &c.key)
  47. copy(dst[:8], src[:8])
  48. }
  49. type sm4BlockCrypt struct {
  50. encbuf [sm4.BlockSize]byte
  51. decbuf [2 * sm4.BlockSize]byte
  52. block cipher.Block
  53. }
  54. // NewSM4BlockCrypt https://github.com/tjfoc/gmsm/tree/master/sm4
  55. func NewSM4BlockCrypt(key []byte) (BlockCrypt, error) {
  56. c := new(sm4BlockCrypt)
  57. block, err := sm4.NewCipher(key)
  58. if err != nil {
  59. return nil, err
  60. }
  61. c.block = block
  62. return c, nil
  63. }
  64. func (c *sm4BlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  65. func (c *sm4BlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  66. type twofishBlockCrypt struct {
  67. encbuf [twofish.BlockSize]byte
  68. decbuf [2 * twofish.BlockSize]byte
  69. block cipher.Block
  70. }
  71. // NewTwofishBlockCrypt https://en.wikipedia.org/wiki/Twofish
  72. func NewTwofishBlockCrypt(key []byte) (BlockCrypt, error) {
  73. c := new(twofishBlockCrypt)
  74. block, err := twofish.NewCipher(key)
  75. if err != nil {
  76. return nil, err
  77. }
  78. c.block = block
  79. return c, nil
  80. }
  81. func (c *twofishBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  82. func (c *twofishBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  83. type tripleDESBlockCrypt struct {
  84. encbuf [des.BlockSize]byte
  85. decbuf [2 * des.BlockSize]byte
  86. block cipher.Block
  87. }
  88. // NewTripleDESBlockCrypt https://en.wikipedia.org/wiki/Triple_DES
  89. func NewTripleDESBlockCrypt(key []byte) (BlockCrypt, error) {
  90. c := new(tripleDESBlockCrypt)
  91. block, err := des.NewTripleDESCipher(key)
  92. if err != nil {
  93. return nil, err
  94. }
  95. c.block = block
  96. return c, nil
  97. }
  98. func (c *tripleDESBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  99. func (c *tripleDESBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  100. type cast5BlockCrypt struct {
  101. encbuf [cast5.BlockSize]byte
  102. decbuf [2 * cast5.BlockSize]byte
  103. block cipher.Block
  104. }
  105. // NewCast5BlockCrypt https://en.wikipedia.org/wiki/CAST-128
  106. func NewCast5BlockCrypt(key []byte) (BlockCrypt, error) {
  107. c := new(cast5BlockCrypt)
  108. block, err := cast5.NewCipher(key)
  109. if err != nil {
  110. return nil, err
  111. }
  112. c.block = block
  113. return c, nil
  114. }
  115. func (c *cast5BlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  116. func (c *cast5BlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  117. type blowfishBlockCrypt struct {
  118. encbuf [blowfish.BlockSize]byte
  119. decbuf [2 * blowfish.BlockSize]byte
  120. block cipher.Block
  121. }
  122. // NewBlowfishBlockCrypt https://en.wikipedia.org/wiki/Blowfish_(cipher)
  123. func NewBlowfishBlockCrypt(key []byte) (BlockCrypt, error) {
  124. c := new(blowfishBlockCrypt)
  125. block, err := blowfish.NewCipher(key)
  126. if err != nil {
  127. return nil, err
  128. }
  129. c.block = block
  130. return c, nil
  131. }
  132. func (c *blowfishBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  133. func (c *blowfishBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  134. type aesBlockCrypt struct {
  135. encbuf [aes.BlockSize]byte
  136. decbuf [2 * aes.BlockSize]byte
  137. block cipher.Block
  138. }
  139. // NewAESBlockCrypt https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
  140. func NewAESBlockCrypt(key []byte) (BlockCrypt, error) {
  141. c := new(aesBlockCrypt)
  142. block, err := aes.NewCipher(key)
  143. if err != nil {
  144. return nil, err
  145. }
  146. c.block = block
  147. return c, nil
  148. }
  149. func (c *aesBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  150. func (c *aesBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  151. type teaBlockCrypt struct {
  152. encbuf [tea.BlockSize]byte
  153. decbuf [2 * tea.BlockSize]byte
  154. block cipher.Block
  155. }
  156. // NewTEABlockCrypt https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
  157. func NewTEABlockCrypt(key []byte) (BlockCrypt, error) {
  158. c := new(teaBlockCrypt)
  159. block, err := tea.NewCipherWithRounds(key, 16)
  160. if err != nil {
  161. return nil, err
  162. }
  163. c.block = block
  164. return c, nil
  165. }
  166. func (c *teaBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  167. func (c *teaBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  168. type xteaBlockCrypt struct {
  169. encbuf [xtea.BlockSize]byte
  170. decbuf [2 * xtea.BlockSize]byte
  171. block cipher.Block
  172. }
  173. // NewXTEABlockCrypt https://en.wikipedia.org/wiki/XTEA
  174. func NewXTEABlockCrypt(key []byte) (BlockCrypt, error) {
  175. c := new(xteaBlockCrypt)
  176. block, err := xtea.NewCipher(key)
  177. if err != nil {
  178. return nil, err
  179. }
  180. c.block = block
  181. return c, nil
  182. }
  183. func (c *xteaBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  184. func (c *xteaBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  185. type simpleXORBlockCrypt struct {
  186. xortbl []byte
  187. }
  188. // NewSimpleXORBlockCrypt simple xor with key expanding
  189. func NewSimpleXORBlockCrypt(key []byte) (BlockCrypt, error) {
  190. c := new(simpleXORBlockCrypt)
  191. c.xortbl = pbkdf2.Key(key, []byte(saltxor), 32, mtuLimit, sha1.New)
  192. return c, nil
  193. }
  194. func (c *simpleXORBlockCrypt) Encrypt(dst, src []byte) { xor.Bytes(dst, src, c.xortbl) }
  195. func (c *simpleXORBlockCrypt) Decrypt(dst, src []byte) { xor.Bytes(dst, src, c.xortbl) }
  196. type noneBlockCrypt struct{}
  197. // NewNoneBlockCrypt does nothing but copying
  198. func NewNoneBlockCrypt(key []byte) (BlockCrypt, error) {
  199. return new(noneBlockCrypt), nil
  200. }
  201. func (c *noneBlockCrypt) Encrypt(dst, src []byte) { copy(dst, src) }
  202. func (c *noneBlockCrypt) Decrypt(dst, src []byte) { copy(dst, src) }
  203. // packet encryption with local CFB mode
  204. func encrypt(block cipher.Block, dst, src, buf []byte) {
  205. switch block.BlockSize() {
  206. case 8:
  207. encrypt8(block, dst, src, buf)
  208. case 16:
  209. encrypt16(block, dst, src, buf)
  210. default:
  211. encryptVariant(block, dst, src, buf)
  212. }
  213. }
  214. // optimized encryption for the ciphers which works in 8-bytes
  215. func encrypt8(block cipher.Block, dst, src, buf []byte) {
  216. tbl := buf[:8]
  217. block.Encrypt(tbl, initialVector)
  218. n := len(src) / 8
  219. base := 0
  220. repeat := n / 8
  221. left := n % 8
  222. for i := 0; i < repeat; i++ {
  223. s := src[base:][0:64]
  224. d := dst[base:][0:64]
  225. // 1
  226. xor.BytesSrc1(d[0:8], s[0:8], tbl)
  227. block.Encrypt(tbl, d[0:8])
  228. // 2
  229. xor.BytesSrc1(d[8:16], s[8:16], tbl)
  230. block.Encrypt(tbl, d[8:16])
  231. // 3
  232. xor.BytesSrc1(d[16:24], s[16:24], tbl)
  233. block.Encrypt(tbl, d[16:24])
  234. // 4
  235. xor.BytesSrc1(d[24:32], s[24:32], tbl)
  236. block.Encrypt(tbl, d[24:32])
  237. // 5
  238. xor.BytesSrc1(d[32:40], s[32:40], tbl)
  239. block.Encrypt(tbl, d[32:40])
  240. // 6
  241. xor.BytesSrc1(d[40:48], s[40:48], tbl)
  242. block.Encrypt(tbl, d[40:48])
  243. // 7
  244. xor.BytesSrc1(d[48:56], s[48:56], tbl)
  245. block.Encrypt(tbl, d[48:56])
  246. // 8
  247. xor.BytesSrc1(d[56:64], s[56:64], tbl)
  248. block.Encrypt(tbl, d[56:64])
  249. base += 64
  250. }
  251. switch left {
  252. case 7:
  253. xor.BytesSrc1(dst[base:], src[base:], tbl)
  254. block.Encrypt(tbl, dst[base:])
  255. base += 8
  256. fallthrough
  257. case 6:
  258. xor.BytesSrc1(dst[base:], src[base:], tbl)
  259. block.Encrypt(tbl, dst[base:])
  260. base += 8
  261. fallthrough
  262. case 5:
  263. xor.BytesSrc1(dst[base:], src[base:], tbl)
  264. block.Encrypt(tbl, dst[base:])
  265. base += 8
  266. fallthrough
  267. case 4:
  268. xor.BytesSrc1(dst[base:], src[base:], tbl)
  269. block.Encrypt(tbl, dst[base:])
  270. base += 8
  271. fallthrough
  272. case 3:
  273. xor.BytesSrc1(dst[base:], src[base:], tbl)
  274. block.Encrypt(tbl, dst[base:])
  275. base += 8
  276. fallthrough
  277. case 2:
  278. xor.BytesSrc1(dst[base:], src[base:], tbl)
  279. block.Encrypt(tbl, dst[base:])
  280. base += 8
  281. fallthrough
  282. case 1:
  283. xor.BytesSrc1(dst[base:], src[base:], tbl)
  284. block.Encrypt(tbl, dst[base:])
  285. base += 8
  286. fallthrough
  287. case 0:
  288. xor.BytesSrc0(dst[base:], src[base:], tbl)
  289. }
  290. }
  291. // optimized encryption for the ciphers which works in 16-bytes
  292. func encrypt16(block cipher.Block, dst, src, buf []byte) {
  293. tbl := buf[:16]
  294. block.Encrypt(tbl, initialVector)
  295. n := len(src) / 16
  296. base := 0
  297. repeat := n / 8
  298. left := n % 8
  299. for i := 0; i < repeat; i++ {
  300. s := src[base:][0:128]
  301. d := dst[base:][0:128]
  302. // 1
  303. xor.BytesSrc1(d[0:16], s[0:16], tbl)
  304. block.Encrypt(tbl, d[0:16])
  305. // 2
  306. xor.BytesSrc1(d[16:32], s[16:32], tbl)
  307. block.Encrypt(tbl, d[16:32])
  308. // 3
  309. xor.BytesSrc1(d[32:48], s[32:48], tbl)
  310. block.Encrypt(tbl, d[32:48])
  311. // 4
  312. xor.BytesSrc1(d[48:64], s[48:64], tbl)
  313. block.Encrypt(tbl, d[48:64])
  314. // 5
  315. xor.BytesSrc1(d[64:80], s[64:80], tbl)
  316. block.Encrypt(tbl, d[64:80])
  317. // 6
  318. xor.BytesSrc1(d[80:96], s[80:96], tbl)
  319. block.Encrypt(tbl, d[80:96])
  320. // 7
  321. xor.BytesSrc1(d[96:112], s[96:112], tbl)
  322. block.Encrypt(tbl, d[96:112])
  323. // 8
  324. xor.BytesSrc1(d[112:128], s[112:128], tbl)
  325. block.Encrypt(tbl, d[112:128])
  326. base += 128
  327. }
  328. switch left {
  329. case 7:
  330. xor.BytesSrc1(dst[base:], src[base:], tbl)
  331. block.Encrypt(tbl, dst[base:])
  332. base += 16
  333. fallthrough
  334. case 6:
  335. xor.BytesSrc1(dst[base:], src[base:], tbl)
  336. block.Encrypt(tbl, dst[base:])
  337. base += 16
  338. fallthrough
  339. case 5:
  340. xor.BytesSrc1(dst[base:], src[base:], tbl)
  341. block.Encrypt(tbl, dst[base:])
  342. base += 16
  343. fallthrough
  344. case 4:
  345. xor.BytesSrc1(dst[base:], src[base:], tbl)
  346. block.Encrypt(tbl, dst[base:])
  347. base += 16
  348. fallthrough
  349. case 3:
  350. xor.BytesSrc1(dst[base:], src[base:], tbl)
  351. block.Encrypt(tbl, dst[base:])
  352. base += 16
  353. fallthrough
  354. case 2:
  355. xor.BytesSrc1(dst[base:], src[base:], tbl)
  356. block.Encrypt(tbl, dst[base:])
  357. base += 16
  358. fallthrough
  359. case 1:
  360. xor.BytesSrc1(dst[base:], src[base:], tbl)
  361. block.Encrypt(tbl, dst[base:])
  362. base += 16
  363. fallthrough
  364. case 0:
  365. xor.BytesSrc0(dst[base:], src[base:], tbl)
  366. }
  367. }
  368. func encryptVariant(block cipher.Block, dst, src, buf []byte) {
  369. blocksize := block.BlockSize()
  370. tbl := buf[:blocksize]
  371. block.Encrypt(tbl, initialVector)
  372. n := len(src) / blocksize
  373. base := 0
  374. repeat := n / 8
  375. left := n % 8
  376. for i := 0; i < repeat; i++ {
  377. // 1
  378. xor.BytesSrc1(dst[base:], src[base:], tbl)
  379. block.Encrypt(tbl, dst[base:])
  380. base += blocksize
  381. // 2
  382. xor.BytesSrc1(dst[base:], src[base:], tbl)
  383. block.Encrypt(tbl, dst[base:])
  384. base += blocksize
  385. // 3
  386. xor.BytesSrc1(dst[base:], src[base:], tbl)
  387. block.Encrypt(tbl, dst[base:])
  388. base += blocksize
  389. // 4
  390. xor.BytesSrc1(dst[base:], src[base:], tbl)
  391. block.Encrypt(tbl, dst[base:])
  392. base += blocksize
  393. // 5
  394. xor.BytesSrc1(dst[base:], src[base:], tbl)
  395. block.Encrypt(tbl, dst[base:])
  396. base += blocksize
  397. // 6
  398. xor.BytesSrc1(dst[base:], src[base:], tbl)
  399. block.Encrypt(tbl, dst[base:])
  400. base += blocksize
  401. // 7
  402. xor.BytesSrc1(dst[base:], src[base:], tbl)
  403. block.Encrypt(tbl, dst[base:])
  404. base += blocksize
  405. // 8
  406. xor.BytesSrc1(dst[base:], src[base:], tbl)
  407. block.Encrypt(tbl, dst[base:])
  408. base += blocksize
  409. }
  410. switch left {
  411. case 7:
  412. xor.BytesSrc1(dst[base:], src[base:], tbl)
  413. block.Encrypt(tbl, dst[base:])
  414. base += blocksize
  415. fallthrough
  416. case 6:
  417. xor.BytesSrc1(dst[base:], src[base:], tbl)
  418. block.Encrypt(tbl, dst[base:])
  419. base += blocksize
  420. fallthrough
  421. case 5:
  422. xor.BytesSrc1(dst[base:], src[base:], tbl)
  423. block.Encrypt(tbl, dst[base:])
  424. base += blocksize
  425. fallthrough
  426. case 4:
  427. xor.BytesSrc1(dst[base:], src[base:], tbl)
  428. block.Encrypt(tbl, dst[base:])
  429. base += blocksize
  430. fallthrough
  431. case 3:
  432. xor.BytesSrc1(dst[base:], src[base:], tbl)
  433. block.Encrypt(tbl, dst[base:])
  434. base += blocksize
  435. fallthrough
  436. case 2:
  437. xor.BytesSrc1(dst[base:], src[base:], tbl)
  438. block.Encrypt(tbl, dst[base:])
  439. base += blocksize
  440. fallthrough
  441. case 1:
  442. xor.BytesSrc1(dst[base:], src[base:], tbl)
  443. block.Encrypt(tbl, dst[base:])
  444. base += blocksize
  445. fallthrough
  446. case 0:
  447. xor.BytesSrc0(dst[base:], src[base:], tbl)
  448. }
  449. }
  450. // decryption
  451. func decrypt(block cipher.Block, dst, src, buf []byte) {
  452. switch block.BlockSize() {
  453. case 8:
  454. decrypt8(block, dst, src, buf)
  455. case 16:
  456. decrypt16(block, dst, src, buf)
  457. default:
  458. decryptVariant(block, dst, src, buf)
  459. }
  460. }
  461. func decrypt8(block cipher.Block, dst, src, buf []byte) {
  462. tbl := buf[0:8]
  463. next := buf[8:16]
  464. block.Encrypt(tbl, initialVector)
  465. n := len(src) / 8
  466. base := 0
  467. repeat := n / 8
  468. left := n % 8
  469. for i := 0; i < repeat; i++ {
  470. s := src[base:][0:64]
  471. d := dst[base:][0:64]
  472. // 1
  473. block.Encrypt(next, s[0:8])
  474. xor.BytesSrc1(d[0:8], s[0:8], tbl)
  475. // 2
  476. block.Encrypt(tbl, s[8:16])
  477. xor.BytesSrc1(d[8:16], s[8:16], next)
  478. // 3
  479. block.Encrypt(next, s[16:24])
  480. xor.BytesSrc1(d[16:24], s[16:24], tbl)
  481. // 4
  482. block.Encrypt(tbl, s[24:32])
  483. xor.BytesSrc1(d[24:32], s[24:32], next)
  484. // 5
  485. block.Encrypt(next, s[32:40])
  486. xor.BytesSrc1(d[32:40], s[32:40], tbl)
  487. // 6
  488. block.Encrypt(tbl, s[40:48])
  489. xor.BytesSrc1(d[40:48], s[40:48], next)
  490. // 7
  491. block.Encrypt(next, s[48:56])
  492. xor.BytesSrc1(d[48:56], s[48:56], tbl)
  493. // 8
  494. block.Encrypt(tbl, s[56:64])
  495. xor.BytesSrc1(d[56:64], s[56:64], next)
  496. base += 64
  497. }
  498. switch left {
  499. case 7:
  500. block.Encrypt(next, src[base:])
  501. xor.BytesSrc1(dst[base:], src[base:], tbl)
  502. tbl, next = next, tbl
  503. base += 8
  504. fallthrough
  505. case 6:
  506. block.Encrypt(next, src[base:])
  507. xor.BytesSrc1(dst[base:], src[base:], tbl)
  508. tbl, next = next, tbl
  509. base += 8
  510. fallthrough
  511. case 5:
  512. block.Encrypt(next, src[base:])
  513. xor.BytesSrc1(dst[base:], src[base:], tbl)
  514. tbl, next = next, tbl
  515. base += 8
  516. fallthrough
  517. case 4:
  518. block.Encrypt(next, src[base:])
  519. xor.BytesSrc1(dst[base:], src[base:], tbl)
  520. tbl, next = next, tbl
  521. base += 8
  522. fallthrough
  523. case 3:
  524. block.Encrypt(next, src[base:])
  525. xor.BytesSrc1(dst[base:], src[base:], tbl)
  526. tbl, next = next, tbl
  527. base += 8
  528. fallthrough
  529. case 2:
  530. block.Encrypt(next, src[base:])
  531. xor.BytesSrc1(dst[base:], src[base:], tbl)
  532. tbl, next = next, tbl
  533. base += 8
  534. fallthrough
  535. case 1:
  536. block.Encrypt(next, src[base:])
  537. xor.BytesSrc1(dst[base:], src[base:], tbl)
  538. tbl, next = next, tbl
  539. base += 8
  540. fallthrough
  541. case 0:
  542. xor.BytesSrc0(dst[base:], src[base:], tbl)
  543. }
  544. }
  545. func decrypt16(block cipher.Block, dst, src, buf []byte) {
  546. tbl := buf[0:16]
  547. next := buf[16:32]
  548. block.Encrypt(tbl, initialVector)
  549. n := len(src) / 16
  550. base := 0
  551. repeat := n / 8
  552. left := n % 8
  553. for i := 0; i < repeat; i++ {
  554. s := src[base:][0:128]
  555. d := dst[base:][0:128]
  556. // 1
  557. block.Encrypt(next, s[0:16])
  558. xor.BytesSrc1(d[0:16], s[0:16], tbl)
  559. // 2
  560. block.Encrypt(tbl, s[16:32])
  561. xor.BytesSrc1(d[16:32], s[16:32], next)
  562. // 3
  563. block.Encrypt(next, s[32:48])
  564. xor.BytesSrc1(d[32:48], s[32:48], tbl)
  565. // 4
  566. block.Encrypt(tbl, s[48:64])
  567. xor.BytesSrc1(d[48:64], s[48:64], next)
  568. // 5
  569. block.Encrypt(next, s[64:80])
  570. xor.BytesSrc1(d[64:80], s[64:80], tbl)
  571. // 6
  572. block.Encrypt(tbl, s[80:96])
  573. xor.BytesSrc1(d[80:96], s[80:96], next)
  574. // 7
  575. block.Encrypt(next, s[96:112])
  576. xor.BytesSrc1(d[96:112], s[96:112], tbl)
  577. // 8
  578. block.Encrypt(tbl, s[112:128])
  579. xor.BytesSrc1(d[112:128], s[112:128], next)
  580. base += 128
  581. }
  582. switch left {
  583. case 7:
  584. block.Encrypt(next, src[base:])
  585. xor.BytesSrc1(dst[base:], src[base:], tbl)
  586. tbl, next = next, tbl
  587. base += 16
  588. fallthrough
  589. case 6:
  590. block.Encrypt(next, src[base:])
  591. xor.BytesSrc1(dst[base:], src[base:], tbl)
  592. tbl, next = next, tbl
  593. base += 16
  594. fallthrough
  595. case 5:
  596. block.Encrypt(next, src[base:])
  597. xor.BytesSrc1(dst[base:], src[base:], tbl)
  598. tbl, next = next, tbl
  599. base += 16
  600. fallthrough
  601. case 4:
  602. block.Encrypt(next, src[base:])
  603. xor.BytesSrc1(dst[base:], src[base:], tbl)
  604. tbl, next = next, tbl
  605. base += 16
  606. fallthrough
  607. case 3:
  608. block.Encrypt(next, src[base:])
  609. xor.BytesSrc1(dst[base:], src[base:], tbl)
  610. tbl, next = next, tbl
  611. base += 16
  612. fallthrough
  613. case 2:
  614. block.Encrypt(next, src[base:])
  615. xor.BytesSrc1(dst[base:], src[base:], tbl)
  616. tbl, next = next, tbl
  617. base += 16
  618. fallthrough
  619. case 1:
  620. block.Encrypt(next, src[base:])
  621. xor.BytesSrc1(dst[base:], src[base:], tbl)
  622. tbl, next = next, tbl
  623. base += 16
  624. fallthrough
  625. case 0:
  626. xor.BytesSrc0(dst[base:], src[base:], tbl)
  627. }
  628. }
  629. func decryptVariant(block cipher.Block, dst, src, buf []byte) {
  630. blocksize := block.BlockSize()
  631. tbl := buf[:blocksize]
  632. next := buf[blocksize:]
  633. block.Encrypt(tbl, initialVector)
  634. n := len(src) / blocksize
  635. base := 0
  636. repeat := n / 8
  637. left := n % 8
  638. for i := 0; i < repeat; i++ {
  639. // 1
  640. block.Encrypt(next, src[base:])
  641. xor.BytesSrc1(dst[base:], src[base:], tbl)
  642. base += blocksize
  643. // 2
  644. block.Encrypt(tbl, src[base:])
  645. xor.BytesSrc1(dst[base:], src[base:], next)
  646. base += blocksize
  647. // 3
  648. block.Encrypt(next, src[base:])
  649. xor.BytesSrc1(dst[base:], src[base:], tbl)
  650. base += blocksize
  651. // 4
  652. block.Encrypt(tbl, src[base:])
  653. xor.BytesSrc1(dst[base:], src[base:], next)
  654. base += blocksize
  655. // 5
  656. block.Encrypt(next, src[base:])
  657. xor.BytesSrc1(dst[base:], src[base:], tbl)
  658. base += blocksize
  659. // 6
  660. block.Encrypt(tbl, src[base:])
  661. xor.BytesSrc1(dst[base:], src[base:], next)
  662. base += blocksize
  663. // 7
  664. block.Encrypt(next, src[base:])
  665. xor.BytesSrc1(dst[base:], src[base:], tbl)
  666. base += blocksize
  667. // 8
  668. block.Encrypt(tbl, src[base:])
  669. xor.BytesSrc1(dst[base:], src[base:], next)
  670. base += blocksize
  671. }
  672. switch left {
  673. case 7:
  674. block.Encrypt(next, src[base:])
  675. xor.BytesSrc1(dst[base:], src[base:], tbl)
  676. tbl, next = next, tbl
  677. base += blocksize
  678. fallthrough
  679. case 6:
  680. block.Encrypt(next, src[base:])
  681. xor.BytesSrc1(dst[base:], src[base:], tbl)
  682. tbl, next = next, tbl
  683. base += blocksize
  684. fallthrough
  685. case 5:
  686. block.Encrypt(next, src[base:])
  687. xor.BytesSrc1(dst[base:], src[base:], tbl)
  688. tbl, next = next, tbl
  689. base += blocksize
  690. fallthrough
  691. case 4:
  692. block.Encrypt(next, src[base:])
  693. xor.BytesSrc1(dst[base:], src[base:], tbl)
  694. tbl, next = next, tbl
  695. base += blocksize
  696. fallthrough
  697. case 3:
  698. block.Encrypt(next, src[base:])
  699. xor.BytesSrc1(dst[base:], src[base:], tbl)
  700. tbl, next = next, tbl
  701. base += blocksize
  702. fallthrough
  703. case 2:
  704. block.Encrypt(next, src[base:])
  705. xor.BytesSrc1(dst[base:], src[base:], tbl)
  706. tbl, next = next, tbl
  707. base += blocksize
  708. fallthrough
  709. case 1:
  710. block.Encrypt(next, src[base:])
  711. xor.BytesSrc1(dst[base:], src[base:], tbl)
  712. tbl, next = next, tbl
  713. base += blocksize
  714. fallthrough
  715. case 0:
  716. xor.BytesSrc0(dst[base:], src[base:], tbl)
  717. }
  718. }