remove-crypt-fec.patch 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. From 0b9d0759f979a5d828b747ea51771f307c53d221 Mon Sep 17 00:00:00 2001
  2. From: David Fifield <david@bamsoftware.com>
  3. Date: Thu, 9 Apr 2020 11:27:44 -0600
  4. Subject: [PATCH] Remove crypt and FEC dependencies.
  5. ---
  6. crypt.go | 618 -----------------------------------------------------
  7. fec.go | 337 -----------------------------
  8. removed.go | 29 +++
  9. 3 files changed, 29 insertions(+), 955 deletions(-)
  10. delete mode 100644 crypt.go
  11. delete mode 100644 fec.go
  12. create mode 100644 removed.go
  13. diff --git a/crypt.go b/crypt.go
  14. deleted file mode 100644
  15. index d882852..0000000
  16. --- a/crypt.go
  17. +++ /dev/null
  18. @@ -1,618 +0,0 @@
  19. -package kcp
  20. -
  21. -import (
  22. - "crypto/aes"
  23. - "crypto/cipher"
  24. - "crypto/des"
  25. - "crypto/sha1"
  26. - "unsafe"
  27. -
  28. - xor "github.com/templexxx/xorsimd"
  29. - "github.com/tjfoc/gmsm/sm4"
  30. -
  31. - "golang.org/x/crypto/blowfish"
  32. - "golang.org/x/crypto/cast5"
  33. - "golang.org/x/crypto/pbkdf2"
  34. - "golang.org/x/crypto/salsa20"
  35. - "golang.org/x/crypto/tea"
  36. - "golang.org/x/crypto/twofish"
  37. - "golang.org/x/crypto/xtea"
  38. -)
  39. -
  40. -var (
  41. - initialVector = []byte{167, 115, 79, 156, 18, 172, 27, 1, 164, 21, 242, 193, 252, 120, 230, 107}
  42. - saltxor = `sH3CIVoF#rWLtJo6`
  43. -)
  44. -
  45. -// BlockCrypt defines encryption/decryption methods for a given byte slice.
  46. -// Notes on implementing: the data to be encrypted contains a builtin
  47. -// nonce at the first 16 bytes
  48. -type BlockCrypt interface {
  49. - // Encrypt encrypts the whole block in src into dst.
  50. - // Dst and src may point at the same memory.
  51. - Encrypt(dst, src []byte)
  52. -
  53. - // Decrypt decrypts the whole block in src into dst.
  54. - // Dst and src may point at the same memory.
  55. - Decrypt(dst, src []byte)
  56. -}
  57. -
  58. -type salsa20BlockCrypt struct {
  59. - key [32]byte
  60. -}
  61. -
  62. -// NewSalsa20BlockCrypt https://en.wikipedia.org/wiki/Salsa20
  63. -func NewSalsa20BlockCrypt(key []byte) (BlockCrypt, error) {
  64. - c := new(salsa20BlockCrypt)
  65. - copy(c.key[:], key)
  66. - return c, nil
  67. -}
  68. -
  69. -func (c *salsa20BlockCrypt) Encrypt(dst, src []byte) {
  70. - salsa20.XORKeyStream(dst[8:], src[8:], src[:8], &c.key)
  71. - copy(dst[:8], src[:8])
  72. -}
  73. -func (c *salsa20BlockCrypt) Decrypt(dst, src []byte) {
  74. - salsa20.XORKeyStream(dst[8:], src[8:], src[:8], &c.key)
  75. - copy(dst[:8], src[:8])
  76. -}
  77. -
  78. -type sm4BlockCrypt struct {
  79. - encbuf [sm4.BlockSize]byte // 64bit alignment enc/dec buffer
  80. - decbuf [2 * sm4.BlockSize]byte
  81. - block cipher.Block
  82. -}
  83. -
  84. -// NewSM4BlockCrypt https://github.com/tjfoc/gmsm/tree/master/sm4
  85. -func NewSM4BlockCrypt(key []byte) (BlockCrypt, error) {
  86. - c := new(sm4BlockCrypt)
  87. - block, err := sm4.NewCipher(key)
  88. - if err != nil {
  89. - return nil, err
  90. - }
  91. - c.block = block
  92. - return c, nil
  93. -}
  94. -
  95. -func (c *sm4BlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  96. -func (c *sm4BlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  97. -
  98. -type twofishBlockCrypt struct {
  99. - encbuf [twofish.BlockSize]byte
  100. - decbuf [2 * twofish.BlockSize]byte
  101. - block cipher.Block
  102. -}
  103. -
  104. -// NewTwofishBlockCrypt https://en.wikipedia.org/wiki/Twofish
  105. -func NewTwofishBlockCrypt(key []byte) (BlockCrypt, error) {
  106. - c := new(twofishBlockCrypt)
  107. - block, err := twofish.NewCipher(key)
  108. - if err != nil {
  109. - return nil, err
  110. - }
  111. - c.block = block
  112. - return c, nil
  113. -}
  114. -
  115. -func (c *twofishBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  116. -func (c *twofishBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  117. -
  118. -type tripleDESBlockCrypt struct {
  119. - encbuf [des.BlockSize]byte
  120. - decbuf [2 * des.BlockSize]byte
  121. - block cipher.Block
  122. -}
  123. -
  124. -// NewTripleDESBlockCrypt https://en.wikipedia.org/wiki/Triple_DES
  125. -func NewTripleDESBlockCrypt(key []byte) (BlockCrypt, error) {
  126. - c := new(tripleDESBlockCrypt)
  127. - block, err := des.NewTripleDESCipher(key)
  128. - if err != nil {
  129. - return nil, err
  130. - }
  131. - c.block = block
  132. - return c, nil
  133. -}
  134. -
  135. -func (c *tripleDESBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  136. -func (c *tripleDESBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  137. -
  138. -type cast5BlockCrypt struct {
  139. - encbuf [cast5.BlockSize]byte
  140. - decbuf [2 * cast5.BlockSize]byte
  141. - block cipher.Block
  142. -}
  143. -
  144. -// NewCast5BlockCrypt https://en.wikipedia.org/wiki/CAST-128
  145. -func NewCast5BlockCrypt(key []byte) (BlockCrypt, error) {
  146. - c := new(cast5BlockCrypt)
  147. - block, err := cast5.NewCipher(key)
  148. - if err != nil {
  149. - return nil, err
  150. - }
  151. - c.block = block
  152. - return c, nil
  153. -}
  154. -
  155. -func (c *cast5BlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  156. -func (c *cast5BlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  157. -
  158. -type blowfishBlockCrypt struct {
  159. - encbuf [blowfish.BlockSize]byte
  160. - decbuf [2 * blowfish.BlockSize]byte
  161. - block cipher.Block
  162. -}
  163. -
  164. -// NewBlowfishBlockCrypt https://en.wikipedia.org/wiki/Blowfish_(cipher)
  165. -func NewBlowfishBlockCrypt(key []byte) (BlockCrypt, error) {
  166. - c := new(blowfishBlockCrypt)
  167. - block, err := blowfish.NewCipher(key)
  168. - if err != nil {
  169. - return nil, err
  170. - }
  171. - c.block = block
  172. - return c, nil
  173. -}
  174. -
  175. -func (c *blowfishBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  176. -func (c *blowfishBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  177. -
  178. -type aesBlockCrypt struct {
  179. - encbuf [aes.BlockSize]byte
  180. - decbuf [2 * aes.BlockSize]byte
  181. - block cipher.Block
  182. -}
  183. -
  184. -// NewAESBlockCrypt https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
  185. -func NewAESBlockCrypt(key []byte) (BlockCrypt, error) {
  186. - c := new(aesBlockCrypt)
  187. - block, err := aes.NewCipher(key)
  188. - if err != nil {
  189. - return nil, err
  190. - }
  191. - c.block = block
  192. - return c, nil
  193. -}
  194. -
  195. -func (c *aesBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  196. -func (c *aesBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  197. -
  198. -type teaBlockCrypt struct {
  199. - encbuf [tea.BlockSize]byte
  200. - decbuf [2 * tea.BlockSize]byte
  201. - block cipher.Block
  202. -}
  203. -
  204. -// NewTEABlockCrypt https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
  205. -func NewTEABlockCrypt(key []byte) (BlockCrypt, error) {
  206. - c := new(teaBlockCrypt)
  207. - block, err := tea.NewCipherWithRounds(key, 16)
  208. - if err != nil {
  209. - return nil, err
  210. - }
  211. - c.block = block
  212. - return c, nil
  213. -}
  214. -
  215. -func (c *teaBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  216. -func (c *teaBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  217. -
  218. -type xteaBlockCrypt struct {
  219. - encbuf [xtea.BlockSize]byte
  220. - decbuf [2 * xtea.BlockSize]byte
  221. - block cipher.Block
  222. -}
  223. -
  224. -// NewXTEABlockCrypt https://en.wikipedia.org/wiki/XTEA
  225. -func NewXTEABlockCrypt(key []byte) (BlockCrypt, error) {
  226. - c := new(xteaBlockCrypt)
  227. - block, err := xtea.NewCipher(key)
  228. - if err != nil {
  229. - return nil, err
  230. - }
  231. - c.block = block
  232. - return c, nil
  233. -}
  234. -
  235. -func (c *xteaBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf[:]) }
  236. -func (c *xteaBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf[:]) }
  237. -
  238. -type simpleXORBlockCrypt struct {
  239. - xortbl []byte
  240. -}
  241. -
  242. -// NewSimpleXORBlockCrypt simple xor with key expanding
  243. -func NewSimpleXORBlockCrypt(key []byte) (BlockCrypt, error) {
  244. - c := new(simpleXORBlockCrypt)
  245. - c.xortbl = pbkdf2.Key(key, []byte(saltxor), 32, mtuLimit, sha1.New)
  246. - return c, nil
  247. -}
  248. -
  249. -func (c *simpleXORBlockCrypt) Encrypt(dst, src []byte) { xor.Bytes(dst, src, c.xortbl) }
  250. -func (c *simpleXORBlockCrypt) Decrypt(dst, src []byte) { xor.Bytes(dst, src, c.xortbl) }
  251. -
  252. -type noneBlockCrypt struct{}
  253. -
  254. -// NewNoneBlockCrypt does nothing but copying
  255. -func NewNoneBlockCrypt(key []byte) (BlockCrypt, error) {
  256. - return new(noneBlockCrypt), nil
  257. -}
  258. -
  259. -func (c *noneBlockCrypt) Encrypt(dst, src []byte) { copy(dst, src) }
  260. -func (c *noneBlockCrypt) Decrypt(dst, src []byte) { copy(dst, src) }
  261. -
  262. -// packet encryption with local CFB mode
  263. -func encrypt(block cipher.Block, dst, src, buf []byte) {
  264. - switch block.BlockSize() {
  265. - case 8:
  266. - encrypt8(block, dst, src, buf)
  267. - case 16:
  268. - encrypt16(block, dst, src, buf)
  269. - default:
  270. - panic("unsupported cipher block size")
  271. - }
  272. -}
  273. -
  274. -// optimized encryption for the ciphers which works in 8-bytes
  275. -func encrypt8(block cipher.Block, dst, src, buf []byte) {
  276. - tbl := buf[:8]
  277. - block.Encrypt(tbl, initialVector)
  278. - n := len(src) / 8
  279. - base := 0
  280. - repeat := n / 8
  281. - left := n % 8
  282. - ptr_tbl := (*uint64)(unsafe.Pointer(&tbl[0]))
  283. -
  284. - for i := 0; i < repeat; i++ {
  285. - s := src[base:][0:64]
  286. - d := dst[base:][0:64]
  287. - // 1
  288. - *(*uint64)(unsafe.Pointer(&d[0])) = *(*uint64)(unsafe.Pointer(&s[0])) ^ *ptr_tbl
  289. - block.Encrypt(tbl, d[0:8])
  290. - // 2
  291. - *(*uint64)(unsafe.Pointer(&d[8])) = *(*uint64)(unsafe.Pointer(&s[8])) ^ *ptr_tbl
  292. - block.Encrypt(tbl, d[8:16])
  293. - // 3
  294. - *(*uint64)(unsafe.Pointer(&d[16])) = *(*uint64)(unsafe.Pointer(&s[16])) ^ *ptr_tbl
  295. - block.Encrypt(tbl, d[16:24])
  296. - // 4
  297. - *(*uint64)(unsafe.Pointer(&d[24])) = *(*uint64)(unsafe.Pointer(&s[24])) ^ *ptr_tbl
  298. - block.Encrypt(tbl, d[24:32])
  299. - // 5
  300. - *(*uint64)(unsafe.Pointer(&d[32])) = *(*uint64)(unsafe.Pointer(&s[32])) ^ *ptr_tbl
  301. - block.Encrypt(tbl, d[32:40])
  302. - // 6
  303. - *(*uint64)(unsafe.Pointer(&d[40])) = *(*uint64)(unsafe.Pointer(&s[40])) ^ *ptr_tbl
  304. - block.Encrypt(tbl, d[40:48])
  305. - // 7
  306. - *(*uint64)(unsafe.Pointer(&d[48])) = *(*uint64)(unsafe.Pointer(&s[48])) ^ *ptr_tbl
  307. - block.Encrypt(tbl, d[48:56])
  308. - // 8
  309. - *(*uint64)(unsafe.Pointer(&d[56])) = *(*uint64)(unsafe.Pointer(&s[56])) ^ *ptr_tbl
  310. - block.Encrypt(tbl, d[56:64])
  311. - base += 64
  312. - }
  313. -
  314. - switch left {
  315. - case 7:
  316. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *ptr_tbl
  317. - block.Encrypt(tbl, dst[base:])
  318. - base += 8
  319. - fallthrough
  320. - case 6:
  321. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *ptr_tbl
  322. - block.Encrypt(tbl, dst[base:])
  323. - base += 8
  324. - fallthrough
  325. - case 5:
  326. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *ptr_tbl
  327. - block.Encrypt(tbl, dst[base:])
  328. - base += 8
  329. - fallthrough
  330. - case 4:
  331. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *ptr_tbl
  332. - block.Encrypt(tbl, dst[base:])
  333. - base += 8
  334. - fallthrough
  335. - case 3:
  336. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *ptr_tbl
  337. - block.Encrypt(tbl, dst[base:])
  338. - base += 8
  339. - fallthrough
  340. - case 2:
  341. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *ptr_tbl
  342. - block.Encrypt(tbl, dst[base:])
  343. - base += 8
  344. - fallthrough
  345. - case 1:
  346. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *ptr_tbl
  347. - block.Encrypt(tbl, dst[base:])
  348. - base += 8
  349. - fallthrough
  350. - case 0:
  351. - xorBytes(dst[base:], src[base:], tbl)
  352. - }
  353. -}
  354. -
  355. -// optimized encryption for the ciphers which works in 16-bytes
  356. -func encrypt16(block cipher.Block, dst, src, buf []byte) {
  357. - tbl := buf[:16]
  358. - block.Encrypt(tbl, initialVector)
  359. - n := len(src) / 16
  360. - base := 0
  361. - repeat := n / 8
  362. - left := n % 8
  363. - for i := 0; i < repeat; i++ {
  364. - s := src[base:][0:128]
  365. - d := dst[base:][0:128]
  366. - // 1
  367. - xor.Bytes16Align(d[0:16], s[0:16], tbl)
  368. - block.Encrypt(tbl, d[0:16])
  369. - // 2
  370. - xor.Bytes16Align(d[16:32], s[16:32], tbl)
  371. - block.Encrypt(tbl, d[16:32])
  372. - // 3
  373. - xor.Bytes16Align(d[32:48], s[32:48], tbl)
  374. - block.Encrypt(tbl, d[32:48])
  375. - // 4
  376. - xor.Bytes16Align(d[48:64], s[48:64], tbl)
  377. - block.Encrypt(tbl, d[48:64])
  378. - // 5
  379. - xor.Bytes16Align(d[64:80], s[64:80], tbl)
  380. - block.Encrypt(tbl, d[64:80])
  381. - // 6
  382. - xor.Bytes16Align(d[80:96], s[80:96], tbl)
  383. - block.Encrypt(tbl, d[80:96])
  384. - // 7
  385. - xor.Bytes16Align(d[96:112], s[96:112], tbl)
  386. - block.Encrypt(tbl, d[96:112])
  387. - // 8
  388. - xor.Bytes16Align(d[112:128], s[112:128], tbl)
  389. - block.Encrypt(tbl, d[112:128])
  390. - base += 128
  391. - }
  392. -
  393. - switch left {
  394. - case 7:
  395. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  396. - block.Encrypt(tbl, dst[base:])
  397. - base += 16
  398. - fallthrough
  399. - case 6:
  400. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  401. - block.Encrypt(tbl, dst[base:])
  402. - base += 16
  403. - fallthrough
  404. - case 5:
  405. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  406. - block.Encrypt(tbl, dst[base:])
  407. - base += 16
  408. - fallthrough
  409. - case 4:
  410. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  411. - block.Encrypt(tbl, dst[base:])
  412. - base += 16
  413. - fallthrough
  414. - case 3:
  415. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  416. - block.Encrypt(tbl, dst[base:])
  417. - base += 16
  418. - fallthrough
  419. - case 2:
  420. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  421. - block.Encrypt(tbl, dst[base:])
  422. - base += 16
  423. - fallthrough
  424. - case 1:
  425. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  426. - block.Encrypt(tbl, dst[base:])
  427. - base += 16
  428. - fallthrough
  429. - case 0:
  430. - xorBytes(dst[base:], src[base:], tbl)
  431. - }
  432. -}
  433. -
  434. -// decryption
  435. -func decrypt(block cipher.Block, dst, src, buf []byte) {
  436. - switch block.BlockSize() {
  437. - case 8:
  438. - decrypt8(block, dst, src, buf)
  439. - case 16:
  440. - decrypt16(block, dst, src, buf)
  441. - default:
  442. - panic("unsupported cipher block size")
  443. - }
  444. -}
  445. -
  446. -// decrypt 8 bytes block, all byte slices are supposed to be 64bit aligned
  447. -func decrypt8(block cipher.Block, dst, src, buf []byte) {
  448. - tbl := buf[0:8]
  449. - next := buf[8:16]
  450. - block.Encrypt(tbl, initialVector)
  451. - n := len(src) / 8
  452. - base := 0
  453. - repeat := n / 8
  454. - left := n % 8
  455. - ptr_tbl := (*uint64)(unsafe.Pointer(&tbl[0]))
  456. - ptr_next := (*uint64)(unsafe.Pointer(&next[0]))
  457. -
  458. - for i := 0; i < repeat; i++ {
  459. - s := src[base:][0:64]
  460. - d := dst[base:][0:64]
  461. - // 1
  462. - block.Encrypt(next, s[0:8])
  463. - *(*uint64)(unsafe.Pointer(&d[0])) = *(*uint64)(unsafe.Pointer(&s[0])) ^ *ptr_tbl
  464. - // 2
  465. - block.Encrypt(tbl, s[8:16])
  466. - *(*uint64)(unsafe.Pointer(&d[8])) = *(*uint64)(unsafe.Pointer(&s[8])) ^ *ptr_next
  467. - // 3
  468. - block.Encrypt(next, s[16:24])
  469. - *(*uint64)(unsafe.Pointer(&d[16])) = *(*uint64)(unsafe.Pointer(&s[16])) ^ *ptr_tbl
  470. - // 4
  471. - block.Encrypt(tbl, s[24:32])
  472. - *(*uint64)(unsafe.Pointer(&d[24])) = *(*uint64)(unsafe.Pointer(&s[24])) ^ *ptr_next
  473. - // 5
  474. - block.Encrypt(next, s[32:40])
  475. - *(*uint64)(unsafe.Pointer(&d[32])) = *(*uint64)(unsafe.Pointer(&s[32])) ^ *ptr_tbl
  476. - // 6
  477. - block.Encrypt(tbl, s[40:48])
  478. - *(*uint64)(unsafe.Pointer(&d[40])) = *(*uint64)(unsafe.Pointer(&s[40])) ^ *ptr_next
  479. - // 7
  480. - block.Encrypt(next, s[48:56])
  481. - *(*uint64)(unsafe.Pointer(&d[48])) = *(*uint64)(unsafe.Pointer(&s[48])) ^ *ptr_tbl
  482. - // 8
  483. - block.Encrypt(tbl, s[56:64])
  484. - *(*uint64)(unsafe.Pointer(&d[56])) = *(*uint64)(unsafe.Pointer(&s[56])) ^ *ptr_next
  485. - base += 64
  486. - }
  487. -
  488. - switch left {
  489. - case 7:
  490. - block.Encrypt(next, src[base:])
  491. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *(*uint64)(unsafe.Pointer(&tbl[0]))
  492. - tbl, next = next, tbl
  493. - base += 8
  494. - fallthrough
  495. - case 6:
  496. - block.Encrypt(next, src[base:])
  497. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *(*uint64)(unsafe.Pointer(&tbl[0]))
  498. - tbl, next = next, tbl
  499. - base += 8
  500. - fallthrough
  501. - case 5:
  502. - block.Encrypt(next, src[base:])
  503. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *(*uint64)(unsafe.Pointer(&tbl[0]))
  504. - tbl, next = next, tbl
  505. - base += 8
  506. - fallthrough
  507. - case 4:
  508. - block.Encrypt(next, src[base:])
  509. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *(*uint64)(unsafe.Pointer(&tbl[0]))
  510. - tbl, next = next, tbl
  511. - base += 8
  512. - fallthrough
  513. - case 3:
  514. - block.Encrypt(next, src[base:])
  515. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *(*uint64)(unsafe.Pointer(&tbl[0]))
  516. - tbl, next = next, tbl
  517. - base += 8
  518. - fallthrough
  519. - case 2:
  520. - block.Encrypt(next, src[base:])
  521. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *(*uint64)(unsafe.Pointer(&tbl[0]))
  522. - tbl, next = next, tbl
  523. - base += 8
  524. - fallthrough
  525. - case 1:
  526. - block.Encrypt(next, src[base:])
  527. - *(*uint64)(unsafe.Pointer(&dst[base])) = *(*uint64)(unsafe.Pointer(&src[base])) ^ *(*uint64)(unsafe.Pointer(&tbl[0]))
  528. - tbl, next = next, tbl
  529. - base += 8
  530. - fallthrough
  531. - case 0:
  532. - xorBytes(dst[base:], src[base:], tbl)
  533. - }
  534. -}
  535. -
  536. -func decrypt16(block cipher.Block, dst, src, buf []byte) {
  537. - tbl := buf[0:16]
  538. - next := buf[16:32]
  539. - block.Encrypt(tbl, initialVector)
  540. - n := len(src) / 16
  541. - base := 0
  542. - repeat := n / 8
  543. - left := n % 8
  544. - for i := 0; i < repeat; i++ {
  545. - s := src[base:][0:128]
  546. - d := dst[base:][0:128]
  547. - // 1
  548. - block.Encrypt(next, s[0:16])
  549. - xor.Bytes16Align(d[0:16], s[0:16], tbl)
  550. - // 2
  551. - block.Encrypt(tbl, s[16:32])
  552. - xor.Bytes16Align(d[16:32], s[16:32], next)
  553. - // 3
  554. - block.Encrypt(next, s[32:48])
  555. - xor.Bytes16Align(d[32:48], s[32:48], tbl)
  556. - // 4
  557. - block.Encrypt(tbl, s[48:64])
  558. - xor.Bytes16Align(d[48:64], s[48:64], next)
  559. - // 5
  560. - block.Encrypt(next, s[64:80])
  561. - xor.Bytes16Align(d[64:80], s[64:80], tbl)
  562. - // 6
  563. - block.Encrypt(tbl, s[80:96])
  564. - xor.Bytes16Align(d[80:96], s[80:96], next)
  565. - // 7
  566. - block.Encrypt(next, s[96:112])
  567. - xor.Bytes16Align(d[96:112], s[96:112], tbl)
  568. - // 8
  569. - block.Encrypt(tbl, s[112:128])
  570. - xor.Bytes16Align(d[112:128], s[112:128], next)
  571. - base += 128
  572. - }
  573. -
  574. - switch left {
  575. - case 7:
  576. - block.Encrypt(next, src[base:])
  577. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  578. - tbl, next = next, tbl
  579. - base += 16
  580. - fallthrough
  581. - case 6:
  582. - block.Encrypt(next, src[base:])
  583. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  584. - tbl, next = next, tbl
  585. - base += 16
  586. - fallthrough
  587. - case 5:
  588. - block.Encrypt(next, src[base:])
  589. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  590. - tbl, next = next, tbl
  591. - base += 16
  592. - fallthrough
  593. - case 4:
  594. - block.Encrypt(next, src[base:])
  595. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  596. - tbl, next = next, tbl
  597. - base += 16
  598. - fallthrough
  599. - case 3:
  600. - block.Encrypt(next, src[base:])
  601. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  602. - tbl, next = next, tbl
  603. - base += 16
  604. - fallthrough
  605. - case 2:
  606. - block.Encrypt(next, src[base:])
  607. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  608. - tbl, next = next, tbl
  609. - base += 16
  610. - fallthrough
  611. - case 1:
  612. - block.Encrypt(next, src[base:])
  613. - xor.Bytes16Align(dst[base:], src[base:], tbl)
  614. - tbl, next = next, tbl
  615. - base += 16
  616. - fallthrough
  617. - case 0:
  618. - xorBytes(dst[base:], src[base:], tbl)
  619. - }
  620. -}
  621. -
  622. -// per bytes xors
  623. -func xorBytes(dst, a, b []byte) int {
  624. - n := len(a)
  625. - if len(b) < n {
  626. - n = len(b)
  627. - }
  628. - if n == 0 {
  629. - return 0
  630. - }
  631. -
  632. - for i := 0; i < n; i++ {
  633. - dst[i] = a[i] ^ b[i]
  634. - }
  635. - return n
  636. -}
  637. diff --git a/fec.go b/fec.go
  638. deleted file mode 100644
  639. index 97cd40b..0000000
  640. --- a/fec.go
  641. +++ /dev/null
  642. @@ -1,337 +0,0 @@
  643. -package kcp
  644. -
  645. -import (
  646. - "encoding/binary"
  647. - "sync/atomic"
  648. -
  649. - "github.com/klauspost/reedsolomon"
  650. -)
  651. -
  652. -const (
  653. - fecHeaderSize = 6
  654. - fecHeaderSizePlus2 = fecHeaderSize + 2 // plus 2B data size
  655. - typeData = 0xf1
  656. - typeParity = 0xf2
  657. - fecExpire = 60000
  658. -)
  659. -
  660. -// fecPacket is a decoded FEC packet
  661. -type fecPacket []byte
  662. -
  663. -func (bts fecPacket) seqid() uint32 { return binary.LittleEndian.Uint32(bts) }
  664. -func (bts fecPacket) flag() uint16 { return binary.LittleEndian.Uint16(bts[4:]) }
  665. -func (bts fecPacket) data() []byte { return bts[6:] }
  666. -
  667. -// fecElement has auxcilliary time field
  668. -type fecElement struct {
  669. - fecPacket
  670. - ts uint32
  671. -}
  672. -
  673. -// fecDecoder for decoding incoming packets
  674. -type fecDecoder struct {
  675. - rxlimit int // queue size limit
  676. - dataShards int
  677. - parityShards int
  678. - shardSize int
  679. - rx []fecElement // ordered receive queue
  680. -
  681. - // caches
  682. - decodeCache [][]byte
  683. - flagCache []bool
  684. -
  685. - // zeros
  686. - zeros []byte
  687. -
  688. - // RS decoder
  689. - codec reedsolomon.Encoder
  690. -}
  691. -
  692. -func newFECDecoder(rxlimit, dataShards, parityShards int) *fecDecoder {
  693. - if dataShards <= 0 || parityShards <= 0 {
  694. - return nil
  695. - }
  696. - if rxlimit < dataShards+parityShards {
  697. - return nil
  698. - }
  699. -
  700. - dec := new(fecDecoder)
  701. - dec.rxlimit = rxlimit
  702. - dec.dataShards = dataShards
  703. - dec.parityShards = parityShards
  704. - dec.shardSize = dataShards + parityShards
  705. - codec, err := reedsolomon.New(dataShards, parityShards)
  706. - if err != nil {
  707. - return nil
  708. - }
  709. - dec.codec = codec
  710. - dec.decodeCache = make([][]byte, dec.shardSize)
  711. - dec.flagCache = make([]bool, dec.shardSize)
  712. - dec.zeros = make([]byte, mtuLimit)
  713. - return dec
  714. -}
  715. -
  716. -// decode a fec packet
  717. -func (dec *fecDecoder) decode(in fecPacket) (recovered [][]byte) {
  718. - // insertion
  719. - n := len(dec.rx) - 1
  720. - insertIdx := 0
  721. - for i := n; i >= 0; i-- {
  722. - if in.seqid() == dec.rx[i].seqid() { // de-duplicate
  723. - return nil
  724. - } else if _itimediff(in.seqid(), dec.rx[i].seqid()) > 0 { // insertion
  725. - insertIdx = i + 1
  726. - break
  727. - }
  728. - }
  729. -
  730. - // make a copy
  731. - pkt := fecPacket(xmitBuf.Get().([]byte)[:len(in)])
  732. - copy(pkt, in)
  733. - elem := fecElement{pkt, currentMs()}
  734. -
  735. - // insert into ordered rx queue
  736. - if insertIdx == n+1 {
  737. - dec.rx = append(dec.rx, elem)
  738. - } else {
  739. - dec.rx = append(dec.rx, fecElement{})
  740. - copy(dec.rx[insertIdx+1:], dec.rx[insertIdx:]) // shift right
  741. - dec.rx[insertIdx] = elem
  742. - }
  743. -
  744. - // shard range for current packet
  745. - shardBegin := pkt.seqid() - pkt.seqid()%uint32(dec.shardSize)
  746. - shardEnd := shardBegin + uint32(dec.shardSize) - 1
  747. -
  748. - // max search range in ordered queue for current shard
  749. - searchBegin := insertIdx - int(pkt.seqid()%uint32(dec.shardSize))
  750. - if searchBegin < 0 {
  751. - searchBegin = 0
  752. - }
  753. - searchEnd := searchBegin + dec.shardSize - 1
  754. - if searchEnd >= len(dec.rx) {
  755. - searchEnd = len(dec.rx) - 1
  756. - }
  757. -
  758. - // re-construct datashards
  759. - if searchEnd-searchBegin+1 >= dec.dataShards {
  760. - var numshard, numDataShard, first, maxlen int
  761. -
  762. - // zero caches
  763. - shards := dec.decodeCache
  764. - shardsflag := dec.flagCache
  765. - for k := range dec.decodeCache {
  766. - shards[k] = nil
  767. - shardsflag[k] = false
  768. - }
  769. -
  770. - // shard assembly
  771. - for i := searchBegin; i <= searchEnd; i++ {
  772. - seqid := dec.rx[i].seqid()
  773. - if _itimediff(seqid, shardEnd) > 0 {
  774. - break
  775. - } else if _itimediff(seqid, shardBegin) >= 0 {
  776. - shards[seqid%uint32(dec.shardSize)] = dec.rx[i].data()
  777. - shardsflag[seqid%uint32(dec.shardSize)] = true
  778. - numshard++
  779. - if dec.rx[i].flag() == typeData {
  780. - numDataShard++
  781. - }
  782. - if numshard == 1 {
  783. - first = i
  784. - }
  785. - if len(dec.rx[i].data()) > maxlen {
  786. - maxlen = len(dec.rx[i].data())
  787. - }
  788. - }
  789. - }
  790. -
  791. - if numDataShard == dec.dataShards {
  792. - // case 1: no loss on data shards
  793. - dec.rx = dec.freeRange(first, numshard, dec.rx)
  794. - } else if numshard >= dec.dataShards {
  795. - // case 2: loss on data shards, but it's recoverable from parity shards
  796. - for k := range shards {
  797. - if shards[k] != nil {
  798. - dlen := len(shards[k])
  799. - shards[k] = shards[k][:maxlen]
  800. - copy(shards[k][dlen:], dec.zeros)
  801. - } else if k < dec.dataShards {
  802. - shards[k] = xmitBuf.Get().([]byte)[:0]
  803. - }
  804. - }
  805. - if err := dec.codec.ReconstructData(shards); err == nil {
  806. - for k := range shards[:dec.dataShards] {
  807. - if !shardsflag[k] {
  808. - // recovered data should be recycled
  809. - recovered = append(recovered, shards[k])
  810. - }
  811. - }
  812. - }
  813. - dec.rx = dec.freeRange(first, numshard, dec.rx)
  814. - }
  815. - }
  816. -
  817. - // keep rxlimit
  818. - if len(dec.rx) > dec.rxlimit {
  819. - if dec.rx[0].flag() == typeData { // track the unrecoverable data
  820. - atomic.AddUint64(&DefaultSnmp.FECShortShards, 1)
  821. - }
  822. - dec.rx = dec.freeRange(0, 1, dec.rx)
  823. - }
  824. -
  825. - // timeout policy
  826. - current := currentMs()
  827. - numExpired := 0
  828. - for k := range dec.rx {
  829. - if _itimediff(current, dec.rx[k].ts) > fecExpire {
  830. - numExpired++
  831. - continue
  832. - }
  833. - break
  834. - }
  835. - if numExpired > 0 {
  836. - dec.rx = dec.freeRange(0, numExpired, dec.rx)
  837. - }
  838. - return
  839. -}
  840. -
  841. -// free a range of fecPacket
  842. -func (dec *fecDecoder) freeRange(first, n int, q []fecElement) []fecElement {
  843. - for i := first; i < first+n; i++ { // recycle buffer
  844. - xmitBuf.Put([]byte(q[i].fecPacket))
  845. - }
  846. -
  847. - if first == 0 && n < cap(q)/2 {
  848. - return q[n:]
  849. - }
  850. - copy(q[first:], q[first+n:])
  851. - return q[:len(q)-n]
  852. -}
  853. -
  854. -// release all segments back to xmitBuf
  855. -func (dec *fecDecoder) release() {
  856. - if n := len(dec.rx); n > 0 {
  857. - dec.rx = dec.freeRange(0, n, dec.rx)
  858. - }
  859. -}
  860. -
  861. -type (
  862. - // fecEncoder for encoding outgoing packets
  863. - fecEncoder struct {
  864. - dataShards int
  865. - parityShards int
  866. - shardSize int
  867. - paws uint32 // Protect Against Wrapped Sequence numbers
  868. - next uint32 // next seqid
  869. -
  870. - shardCount int // count the number of datashards collected
  871. - maxSize int // track maximum data length in datashard
  872. -
  873. - headerOffset int // FEC header offset
  874. - payloadOffset int // FEC payload offset
  875. -
  876. - // caches
  877. - shardCache [][]byte
  878. - encodeCache [][]byte
  879. -
  880. - // zeros
  881. - zeros []byte
  882. -
  883. - // RS encoder
  884. - codec reedsolomon.Encoder
  885. - }
  886. -)
  887. -
  888. -func newFECEncoder(dataShards, parityShards, offset int) *fecEncoder {
  889. - if dataShards <= 0 || parityShards <= 0 {
  890. - return nil
  891. - }
  892. - enc := new(fecEncoder)
  893. - enc.dataShards = dataShards
  894. - enc.parityShards = parityShards
  895. - enc.shardSize = dataShards + parityShards
  896. - enc.paws = 0xffffffff / uint32(enc.shardSize) * uint32(enc.shardSize)
  897. - enc.headerOffset = offset
  898. - enc.payloadOffset = enc.headerOffset + fecHeaderSize
  899. -
  900. - codec, err := reedsolomon.New(dataShards, parityShards)
  901. - if err != nil {
  902. - return nil
  903. - }
  904. - enc.codec = codec
  905. -
  906. - // caches
  907. - enc.encodeCache = make([][]byte, enc.shardSize)
  908. - enc.shardCache = make([][]byte, enc.shardSize)
  909. - for k := range enc.shardCache {
  910. - enc.shardCache[k] = make([]byte, mtuLimit)
  911. - }
  912. - enc.zeros = make([]byte, mtuLimit)
  913. - return enc
  914. -}
  915. -
  916. -// encodes the packet, outputs parity shards if we have collected quorum datashards
  917. -// notice: the contents of 'ps' will be re-written in successive calling
  918. -func (enc *fecEncoder) encode(b []byte) (ps [][]byte) {
  919. - // The header format:
  920. - // | FEC SEQID(4B) | FEC TYPE(2B) | SIZE (2B) | PAYLOAD(SIZE-2) |
  921. - // |<-headerOffset |<-payloadOffset
  922. - enc.markData(b[enc.headerOffset:])
  923. - binary.LittleEndian.PutUint16(b[enc.payloadOffset:], uint16(len(b[enc.payloadOffset:])))
  924. -
  925. - // copy data from payloadOffset to fec shard cache
  926. - sz := len(b)
  927. - enc.shardCache[enc.shardCount] = enc.shardCache[enc.shardCount][:sz]
  928. - copy(enc.shardCache[enc.shardCount][enc.payloadOffset:], b[enc.payloadOffset:])
  929. - enc.shardCount++
  930. -
  931. - // track max datashard length
  932. - if sz > enc.maxSize {
  933. - enc.maxSize = sz
  934. - }
  935. -
  936. - // Generation of Reed-Solomon Erasure Code
  937. - if enc.shardCount == enc.dataShards {
  938. - // fill '0' into the tail of each datashard
  939. - for i := 0; i < enc.dataShards; i++ {
  940. - shard := enc.shardCache[i]
  941. - slen := len(shard)
  942. - copy(shard[slen:enc.maxSize], enc.zeros)
  943. - }
  944. -
  945. - // construct equal-sized slice with stripped header
  946. - cache := enc.encodeCache
  947. - for k := range cache {
  948. - cache[k] = enc.shardCache[k][enc.payloadOffset:enc.maxSize]
  949. - }
  950. -
  951. - // encoding
  952. - if err := enc.codec.Encode(cache); err == nil {
  953. - ps = enc.shardCache[enc.dataShards:]
  954. - for k := range ps {
  955. - enc.markParity(ps[k][enc.headerOffset:])
  956. - ps[k] = ps[k][:enc.maxSize]
  957. - }
  958. - }
  959. -
  960. - // counters resetting
  961. - enc.shardCount = 0
  962. - enc.maxSize = 0
  963. - }
  964. -
  965. - return
  966. -}
  967. -
  968. -func (enc *fecEncoder) markData(data []byte) {
  969. - binary.LittleEndian.PutUint32(data, enc.next)
  970. - binary.LittleEndian.PutUint16(data[4:], typeData)
  971. - enc.next++
  972. -}
  973. -
  974. -func (enc *fecEncoder) markParity(data []byte) {
  975. - binary.LittleEndian.PutUint32(data, enc.next)
  976. - binary.LittleEndian.PutUint16(data[4:], typeParity)
  977. - // sequence wrap will only happen at parity shard
  978. - enc.next = (enc.next + 1) % enc.paws
  979. -}
  980. diff --git a/removed.go b/removed.go
  981. new file mode 100644
  982. index 0000000..5ecf446
  983. --- /dev/null
  984. +++ b/removed.go
  985. @@ -0,0 +1,29 @@
  986. +package kcp
  987. +
  988. +// Dummy implementations for types from crypt.go and fec.go, removed to reduce
  989. +// dependencies.
  990. +
  991. +const (
  992. + fecHeaderSize = 6
  993. + fecHeaderSizePlus2 = fecHeaderSize + 2
  994. + typeData = 0xf1
  995. + typeParity = 0xf2
  996. +)
  997. +
  998. +type (
  999. + BlockCrypt interface {
  1000. + Encrypt(_, _ []byte)
  1001. + Decrypt(_, _ []byte)
  1002. + }
  1003. + fecDecoder struct{}
  1004. + fecEncoder struct{}
  1005. + fecPacket []byte
  1006. +)
  1007. +
  1008. +func newFECDecoder(rxlimit, dataShards, parityShards int) *fecDecoder { return nil }
  1009. +func newFECEncoder(dataShards, parityShards, offset int) *fecEncoder { return nil }
  1010. +
  1011. +func (_ *fecDecoder) decode(in fecPacket) [][]byte { panic("disabled") }
  1012. +func (_ *fecDecoder) release() { panic("disabled") }
  1013. +func (_ *fecEncoder) encode(b []byte) [][]byte { panic("disabled") }
  1014. +func (_ fecPacket) flag() uint16 { panic("disabled") }
  1015. --
  1016. 2.20.1