1
0

crypt.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 []byte
  51. decbuf []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. c.encbuf = make([]byte, sm4.BlockSize)
  63. c.decbuf = make([]byte, 2*sm4.BlockSize)
  64. return c, nil
  65. }
  66. func (c *sm4BlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  67. func (c *sm4BlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  68. type twofishBlockCrypt struct {
  69. encbuf []byte
  70. decbuf []byte
  71. block cipher.Block
  72. }
  73. // NewTwofishBlockCrypt https://en.wikipedia.org/wiki/Twofish
  74. func NewTwofishBlockCrypt(key []byte) (BlockCrypt, error) {
  75. c := new(twofishBlockCrypt)
  76. block, err := twofish.NewCipher(key)
  77. if err != nil {
  78. return nil, err
  79. }
  80. c.block = block
  81. c.encbuf = make([]byte, twofish.BlockSize)
  82. c.decbuf = make([]byte, 2*twofish.BlockSize)
  83. return c, nil
  84. }
  85. func (c *twofishBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  86. func (c *twofishBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  87. type tripleDESBlockCrypt struct {
  88. encbuf []byte
  89. decbuf []byte
  90. block cipher.Block
  91. }
  92. // NewTripleDESBlockCrypt https://en.wikipedia.org/wiki/Triple_DES
  93. func NewTripleDESBlockCrypt(key []byte) (BlockCrypt, error) {
  94. c := new(tripleDESBlockCrypt)
  95. block, err := des.NewTripleDESCipher(key)
  96. if err != nil {
  97. return nil, err
  98. }
  99. c.block = block
  100. c.encbuf = make([]byte, des.BlockSize)
  101. c.decbuf = make([]byte, 2*des.BlockSize)
  102. return c, nil
  103. }
  104. func (c *tripleDESBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  105. func (c *tripleDESBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  106. type cast5BlockCrypt struct {
  107. encbuf []byte
  108. decbuf []byte
  109. block cipher.Block
  110. }
  111. // NewCast5BlockCrypt https://en.wikipedia.org/wiki/CAST-128
  112. func NewCast5BlockCrypt(key []byte) (BlockCrypt, error) {
  113. c := new(cast5BlockCrypt)
  114. block, err := cast5.NewCipher(key)
  115. if err != nil {
  116. return nil, err
  117. }
  118. c.block = block
  119. c.encbuf = make([]byte, cast5.BlockSize)
  120. c.decbuf = make([]byte, 2*cast5.BlockSize)
  121. return c, nil
  122. }
  123. func (c *cast5BlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  124. func (c *cast5BlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  125. type blowfishBlockCrypt struct {
  126. encbuf []byte
  127. decbuf []byte
  128. block cipher.Block
  129. }
  130. // NewBlowfishBlockCrypt https://en.wikipedia.org/wiki/Blowfish_(cipher)
  131. func NewBlowfishBlockCrypt(key []byte) (BlockCrypt, error) {
  132. c := new(blowfishBlockCrypt)
  133. block, err := blowfish.NewCipher(key)
  134. if err != nil {
  135. return nil, err
  136. }
  137. c.block = block
  138. c.encbuf = make([]byte, blowfish.BlockSize)
  139. c.decbuf = make([]byte, 2*blowfish.BlockSize)
  140. return c, nil
  141. }
  142. func (c *blowfishBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  143. func (c *blowfishBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  144. type aesBlockCrypt struct {
  145. encbuf []byte
  146. decbuf []byte
  147. block cipher.Block
  148. }
  149. // NewAESBlockCrypt https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
  150. func NewAESBlockCrypt(key []byte) (BlockCrypt, error) {
  151. c := new(aesBlockCrypt)
  152. block, err := aes.NewCipher(key)
  153. if err != nil {
  154. return nil, err
  155. }
  156. c.block = block
  157. c.encbuf = make([]byte, aes.BlockSize)
  158. c.decbuf = make([]byte, 2*aes.BlockSize)
  159. return c, nil
  160. }
  161. func (c *aesBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  162. func (c *aesBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  163. type teaBlockCrypt struct {
  164. encbuf []byte
  165. decbuf []byte
  166. block cipher.Block
  167. }
  168. // NewTEABlockCrypt https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
  169. func NewTEABlockCrypt(key []byte) (BlockCrypt, error) {
  170. c := new(teaBlockCrypt)
  171. block, err := tea.NewCipherWithRounds(key, 16)
  172. if err != nil {
  173. return nil, err
  174. }
  175. c.block = block
  176. c.encbuf = make([]byte, tea.BlockSize)
  177. c.decbuf = make([]byte, 2*tea.BlockSize)
  178. return c, nil
  179. }
  180. func (c *teaBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  181. func (c *teaBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  182. type xteaBlockCrypt struct {
  183. encbuf []byte
  184. decbuf []byte
  185. block cipher.Block
  186. }
  187. // NewXTEABlockCrypt https://en.wikipedia.org/wiki/XTEA
  188. func NewXTEABlockCrypt(key []byte) (BlockCrypt, error) {
  189. c := new(xteaBlockCrypt)
  190. block, err := xtea.NewCipher(key)
  191. if err != nil {
  192. return nil, err
  193. }
  194. c.block = block
  195. c.encbuf = make([]byte, xtea.BlockSize)
  196. c.decbuf = make([]byte, 2*xtea.BlockSize)
  197. return c, nil
  198. }
  199. func (c *xteaBlockCrypt) Encrypt(dst, src []byte) { encrypt(c.block, dst, src, c.encbuf) }
  200. func (c *xteaBlockCrypt) Decrypt(dst, src []byte) { decrypt(c.block, dst, src, c.decbuf) }
  201. type simpleXORBlockCrypt struct {
  202. xortbl []byte
  203. }
  204. // NewSimpleXORBlockCrypt simple xor with key expanding
  205. func NewSimpleXORBlockCrypt(key []byte) (BlockCrypt, error) {
  206. c := new(simpleXORBlockCrypt)
  207. c.xortbl = pbkdf2.Key(key, []byte(saltxor), 32, mtuLimit, sha1.New)
  208. return c, nil
  209. }
  210. func (c *simpleXORBlockCrypt) Encrypt(dst, src []byte) { xor.Bytes(dst, src, c.xortbl) }
  211. func (c *simpleXORBlockCrypt) Decrypt(dst, src []byte) { xor.Bytes(dst, src, c.xortbl) }
  212. type noneBlockCrypt struct{}
  213. // NewNoneBlockCrypt does nothing but copying
  214. func NewNoneBlockCrypt(key []byte) (BlockCrypt, error) {
  215. return new(noneBlockCrypt), nil
  216. }
  217. func (c *noneBlockCrypt) Encrypt(dst, src []byte) { copy(dst, src) }
  218. func (c *noneBlockCrypt) Decrypt(dst, src []byte) { copy(dst, src) }
  219. // packet encryption with local CFB mode
  220. func encrypt(block cipher.Block, dst, src, buf []byte) {
  221. blocksize := block.BlockSize()
  222. tbl := buf[:blocksize]
  223. block.Encrypt(tbl, initialVector)
  224. n := len(src) / blocksize
  225. base := 0
  226. for i := 0; i < n; i++ {
  227. xor.BytesSrc1(dst[base:], src[base:], tbl)
  228. block.Encrypt(tbl, dst[base:])
  229. base += blocksize
  230. }
  231. xor.BytesSrc0(dst[base:], src[base:], tbl)
  232. }
  233. func decrypt(block cipher.Block, dst, src, buf []byte) {
  234. blocksize := block.BlockSize()
  235. tbl := buf[:blocksize]
  236. next := buf[blocksize:]
  237. block.Encrypt(tbl, initialVector)
  238. n := len(src) / blocksize
  239. base := 0
  240. for i := 0; i < n; i++ {
  241. block.Encrypt(next, src[base:])
  242. xor.BytesSrc1(dst[base:], src[base:], tbl)
  243. tbl, next = next, tbl
  244. base += blocksize
  245. }
  246. xor.BytesSrc0(dst[base:], src[base:], tbl)
  247. }