1
0

crypt.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package kcp
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "crypto/des"
  6. "crypto/sha1"
  7. "golang.org/x/crypto/blowfish"
  8. "golang.org/x/crypto/cast5"
  9. "golang.org/x/crypto/pbkdf2"
  10. "golang.org/x/crypto/salsa20"
  11. "golang.org/x/crypto/tea"
  12. "golang.org/x/crypto/twofish"
  13. "golang.org/x/crypto/xtea"
  14. )
  15. var (
  16. initialVector = []byte{167, 115, 79, 156, 18, 172, 27, 1, 164, 21, 242, 193, 252, 120, 230, 107}
  17. saltxor = `sH3CIVoF#rWLtJo6`
  18. )
  19. // BlockCrypt defines encryption/decryption methods for a given byte slice.
  20. // Notes on implementing: the data to be encrypted contains a builtin
  21. // nonce at the first 16 bytes
  22. type BlockCrypt interface {
  23. // Encrypt encrypts the whole block in src into dst.
  24. // Dst and src may point at the same memory.
  25. Encrypt(dst, src []byte)
  26. // Decrypt decrypts the whole block in src into dst.
  27. // Dst and src may point at the same memory.
  28. Decrypt(dst, src []byte)
  29. }
  30. type salsa20BlockCrypt struct {
  31. key [32]byte
  32. }
  33. // NewSalsa20BlockCrypt https://en.wikipedia.org/wiki/Salsa20
  34. func NewSalsa20BlockCrypt(key []byte) (BlockCrypt, error) {
  35. c := new(salsa20BlockCrypt)
  36. copy(c.key[:], key)
  37. return c, nil
  38. }
  39. func (c *salsa20BlockCrypt) Encrypt(dst, src []byte) {
  40. salsa20.XORKeyStream(dst[8:], src[8:], src[:8], &c.key)
  41. copy(dst[:8], src[:8])
  42. }
  43. func (c *salsa20BlockCrypt) Decrypt(dst, src []byte) {
  44. salsa20.XORKeyStream(dst[8:], src[8:], src[:8], &c.key)
  45. copy(dst[:8], src[:8])
  46. }
  47. type twofishBlockCrypt struct {
  48. encbuf []byte
  49. decbuf []byte
  50. block cipher.Block
  51. }
  52. // NewTwofishBlockCrypt https://en.wikipedia.org/wiki/Twofish
  53. func NewTwofishBlockCrypt(key []byte) (BlockCrypt, error) {
  54. c := new(twofishBlockCrypt)
  55. block, err := twofish.NewCipher(key)
  56. if err != nil {
  57. return nil, err
  58. }
  59. c.block = block
  60. c.encbuf = make([]byte, twofish.BlockSize)
  61. c.decbuf = make([]byte, 2*twofish.BlockSize)
  62. return c, nil
  63. }
  64. func (c *twofishBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  65. func (c *twofishBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  66. type tripleDESBlockCrypt struct {
  67. encbuf []byte
  68. decbuf []byte
  69. block cipher.Block
  70. }
  71. // NewTripleDESBlockCrypt https://en.wikipedia.org/wiki/Triple_DES
  72. func NewTripleDESBlockCrypt(key []byte) (BlockCrypt, error) {
  73. c := new(tripleDESBlockCrypt)
  74. block, err := des.NewTripleDESCipher(key)
  75. if err != nil {
  76. return nil, err
  77. }
  78. c.block = block
  79. c.encbuf = make([]byte, des.BlockSize)
  80. c.decbuf = make([]byte, 2*des.BlockSize)
  81. return c, nil
  82. }
  83. func (c *tripleDESBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  84. func (c *tripleDESBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  85. type cast5BlockCrypt struct {
  86. encbuf []byte
  87. decbuf []byte
  88. block cipher.Block
  89. }
  90. // NewCast5BlockCrypt https://en.wikipedia.org/wiki/CAST-128
  91. func NewCast5BlockCrypt(key []byte) (BlockCrypt, error) {
  92. c := new(cast5BlockCrypt)
  93. block, err := cast5.NewCipher(key)
  94. if err != nil {
  95. return nil, err
  96. }
  97. c.block = block
  98. c.encbuf = make([]byte, cast5.BlockSize)
  99. c.decbuf = make([]byte, 2*cast5.BlockSize)
  100. return c, nil
  101. }
  102. func (c *cast5BlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  103. func (c *cast5BlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  104. type blowfishBlockCrypt struct {
  105. encbuf []byte
  106. decbuf []byte
  107. block cipher.Block
  108. }
  109. // NewBlowfishBlockCrypt https://en.wikipedia.org/wiki/Blowfish_(cipher)
  110. func NewBlowfishBlockCrypt(key []byte) (BlockCrypt, error) {
  111. c := new(blowfishBlockCrypt)
  112. block, err := blowfish.NewCipher(key)
  113. if err != nil {
  114. return nil, err
  115. }
  116. c.block = block
  117. c.encbuf = make([]byte, blowfish.BlockSize)
  118. c.decbuf = make([]byte, 2*blowfish.BlockSize)
  119. return c, nil
  120. }
  121. func (c *blowfishBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  122. func (c *blowfishBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  123. type aesBlockCrypt struct {
  124. encbuf []byte
  125. decbuf []byte
  126. block cipher.Block
  127. }
  128. // NewAESBlockCrypt https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
  129. func NewAESBlockCrypt(key []byte) (BlockCrypt, error) {
  130. c := new(aesBlockCrypt)
  131. block, err := aes.NewCipher(key)
  132. if err != nil {
  133. return nil, err
  134. }
  135. c.block = block
  136. c.encbuf = make([]byte, aes.BlockSize)
  137. c.decbuf = make([]byte, 2*aes.BlockSize)
  138. return c, nil
  139. }
  140. func (c *aesBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  141. func (c *aesBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  142. type teaBlockCrypt struct {
  143. encbuf []byte
  144. decbuf []byte
  145. block cipher.Block
  146. }
  147. // NewTEABlockCrypt https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
  148. func NewTEABlockCrypt(key []byte) (BlockCrypt, error) {
  149. c := new(teaBlockCrypt)
  150. block, err := tea.NewCipherWithRounds(key, 16)
  151. if err != nil {
  152. return nil, err
  153. }
  154. c.block = block
  155. c.encbuf = make([]byte, tea.BlockSize)
  156. c.decbuf = make([]byte, 2*tea.BlockSize)
  157. return c, nil
  158. }
  159. func (c *teaBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  160. func (c *teaBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  161. type xteaBlockCrypt struct {
  162. encbuf []byte
  163. decbuf []byte
  164. block cipher.Block
  165. }
  166. // NewXTEABlockCrypt https://en.wikipedia.org/wiki/XTEA
  167. func NewXTEABlockCrypt(key []byte) (BlockCrypt, error) {
  168. c := new(xteaBlockCrypt)
  169. block, err := xtea.NewCipher(key)
  170. if err != nil {
  171. return nil, err
  172. }
  173. c.block = block
  174. c.encbuf = make([]byte, xtea.BlockSize)
  175. c.decbuf = make([]byte, 2*xtea.BlockSize)
  176. return c, nil
  177. }
  178. func (c *xteaBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  179. func (c *xteaBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  180. type simpleXORBlockCrypt struct {
  181. xortbl []byte
  182. }
  183. // NewSimpleXORBlockCrypt simple xor with key expanding
  184. func NewSimpleXORBlockCrypt(key []byte) (BlockCrypt, error) {
  185. c := new(simpleXORBlockCrypt)
  186. c.xortbl = pbkdf2.Key(key, []byte(saltxor), 32, mtuLimit, sha1.New)
  187. return c, nil
  188. }
  189. func (c *simpleXORBlockCrypt) Encrypt(dst, src []byte) { xorBytes(dst, src, c.xortbl) }
  190. func (c *simpleXORBlockCrypt) Decrypt(dst, src []byte) { xorBytes(dst, src, c.xortbl) }
  191. type noneBlockCrypt struct{}
  192. // NewNoneBlockCrypt does nothing but copying
  193. func NewNoneBlockCrypt(key []byte) (BlockCrypt, error) {
  194. return new(noneBlockCrypt), nil
  195. }
  196. func (c *noneBlockCrypt) Encrypt(dst, src []byte) { copy(dst, src) }
  197. func (c *noneBlockCrypt) Decrypt(dst, src []byte) { copy(dst, src) }
  198. // packet encryption with local CFB mode
  199. func encrypt(block cipher.Block, dst, src, buf []byte) {
  200. blocksize := block.BlockSize()
  201. tbl := buf[:blocksize]
  202. block.Encrypt(tbl, initialVector)
  203. n := len(src) / blocksize
  204. base := 0
  205. for i := 0; i < n; i++ {
  206. xorWords(dst[base:], src[base:], tbl)
  207. block.Encrypt(tbl, dst[base:])
  208. base += blocksize
  209. }
  210. xorBytes(dst[base:], src[base:], tbl)
  211. }
  212. func decrypt(block cipher.Block, dst, src, buf []byte) {
  213. blocksize := block.BlockSize()
  214. tbl := buf[:blocksize]
  215. next := buf[blocksize:]
  216. block.Encrypt(tbl, initialVector)
  217. n := len(src) / blocksize
  218. base := 0
  219. for i := 0; i < n; i++ {
  220. block.Encrypt(next, src[base:])
  221. xorWords(dst[base:], src[base:], tbl)
  222. tbl, next = next, tbl
  223. base += blocksize
  224. }
  225. xorBytes(dst[base:], src[base:], tbl)
  226. }