config.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package shadowsocks
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/md5"
  7. "crypto/sha1"
  8. "io"
  9. "golang.org/x/crypto/chacha20poly1305"
  10. "golang.org/x/crypto/hkdf"
  11. "v2ray.com/core/common"
  12. "v2ray.com/core/common/buf"
  13. "v2ray.com/core/common/crypto"
  14. "v2ray.com/core/common/protocol"
  15. )
  16. // MemoryAccount is an account type converted from Account.
  17. type MemoryAccount struct {
  18. Cipher Cipher
  19. Key []byte
  20. OneTimeAuth Account_OneTimeAuth
  21. }
  22. // Equals implements protocol.Account.Equals().
  23. func (a *MemoryAccount) Equals(another protocol.Account) bool {
  24. if account, ok := another.(*MemoryAccount); ok {
  25. return bytes.Equal(a.Key, account.Key)
  26. }
  27. return false
  28. }
  29. func createAesGcm(key []byte) cipher.AEAD {
  30. block, err := aes.NewCipher(key)
  31. common.Must(err)
  32. gcm, err := cipher.NewGCM(block)
  33. common.Must(err)
  34. return gcm
  35. }
  36. func createChacha20Poly1305(key []byte) cipher.AEAD {
  37. chacha20, err := chacha20poly1305.New(key)
  38. common.Must(err)
  39. return chacha20
  40. }
  41. func (a *Account) getCipher() (Cipher, error) {
  42. switch a.CipherType {
  43. case CipherType_AES_128_CFB:
  44. return &AesCfb{KeyBytes: 16}, nil
  45. case CipherType_AES_256_CFB:
  46. return &AesCfb{KeyBytes: 32}, nil
  47. case CipherType_CHACHA20:
  48. return &ChaCha20{IVBytes: 8}, nil
  49. case CipherType_CHACHA20_IETF:
  50. return &ChaCha20{IVBytes: 12}, nil
  51. case CipherType_AES_128_GCM:
  52. return &AEADCipher{
  53. KeyBytes: 16,
  54. IVBytes: 16,
  55. AEADAuthCreator: createAesGcm,
  56. }, nil
  57. case CipherType_AES_256_GCM:
  58. return &AEADCipher{
  59. KeyBytes: 32,
  60. IVBytes: 32,
  61. AEADAuthCreator: createAesGcm,
  62. }, nil
  63. case CipherType_CHACHA20_POLY1305:
  64. return &AEADCipher{
  65. KeyBytes: 32,
  66. IVBytes: 32,
  67. AEADAuthCreator: createChacha20Poly1305,
  68. }, nil
  69. case CipherType_NONE:
  70. return NoneCipher{}, nil
  71. default:
  72. return nil, newError("Unsupported cipher.")
  73. }
  74. }
  75. // AsAccount implements protocol.AsAccount.
  76. func (a *Account) AsAccount() (protocol.Account, error) {
  77. cipher, err := a.getCipher()
  78. if err != nil {
  79. return nil, newError("failed to get cipher").Base(err)
  80. }
  81. return &MemoryAccount{
  82. Cipher: cipher,
  83. Key: passwordToCipherKey([]byte(a.Password), cipher.KeySize()),
  84. OneTimeAuth: a.Ota,
  85. }, nil
  86. }
  87. // Cipher is an interface for all Shadowsocks ciphers.
  88. type Cipher interface {
  89. KeySize() int
  90. IVSize() int
  91. NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error)
  92. NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error)
  93. IsAEAD() bool
  94. EncodePacket(key []byte, b *buf.Buffer) error
  95. DecodePacket(key []byte, b *buf.Buffer) error
  96. }
  97. // AesCfb represents all AES-CFB ciphers.
  98. type AesCfb struct {
  99. KeyBytes int
  100. }
  101. func (*AesCfb) IsAEAD() bool {
  102. return false
  103. }
  104. func (v *AesCfb) KeySize() int {
  105. return v.KeyBytes
  106. }
  107. func (v *AesCfb) IVSize() int {
  108. return 16
  109. }
  110. func (v *AesCfb) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  111. stream := crypto.NewAesEncryptionStream(key, iv)
  112. return buf.NewWriter(crypto.NewCryptionWriter(stream, writer)), nil
  113. }
  114. func (v *AesCfb) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  115. stream := crypto.NewAesDecryptionStream(key, iv)
  116. return buf.NewReader(crypto.NewCryptionReader(stream, reader)), nil
  117. }
  118. func (v *AesCfb) EncodePacket(key []byte, b *buf.Buffer) error {
  119. iv := b.BytesTo(v.IVSize())
  120. stream := crypto.NewAesEncryptionStream(key, iv)
  121. stream.XORKeyStream(b.BytesFrom(v.IVSize()), b.BytesFrom(v.IVSize()))
  122. return nil
  123. }
  124. func (v *AesCfb) DecodePacket(key []byte, b *buf.Buffer) error {
  125. iv := b.BytesTo(v.IVSize())
  126. stream := crypto.NewAesDecryptionStream(key, iv)
  127. stream.XORKeyStream(b.BytesFrom(v.IVSize()), b.BytesFrom(v.IVSize()))
  128. b.SliceFrom(v.IVSize())
  129. return nil
  130. }
  131. type AEADCipher struct {
  132. KeyBytes int
  133. IVBytes int
  134. AEADAuthCreator func(key []byte) cipher.AEAD
  135. }
  136. func (*AEADCipher) IsAEAD() bool {
  137. return true
  138. }
  139. func (c *AEADCipher) KeySize() int {
  140. return c.KeyBytes
  141. }
  142. func (c *AEADCipher) IVSize() int {
  143. return c.IVBytes
  144. }
  145. func (c *AEADCipher) createAuthenticator(key []byte, iv []byte) *crypto.AEADAuthenticator {
  146. nonce := crypto.NewIncreasingAEADNonceGenerator()
  147. subkey := make([]byte, c.KeyBytes)
  148. hkdfSHA1(key, iv, subkey)
  149. return &crypto.AEADAuthenticator{
  150. AEAD: c.AEADAuthCreator(subkey),
  151. NonceGenerator: nonce,
  152. }
  153. }
  154. func (c *AEADCipher) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  155. auth := c.createAuthenticator(key, iv)
  156. return crypto.NewAuthenticationWriter(auth, &crypto.AEADChunkSizeParser{
  157. Auth: auth,
  158. }, writer, protocol.TransferTypeStream), nil
  159. }
  160. func (c *AEADCipher) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  161. auth := c.createAuthenticator(key, iv)
  162. return crypto.NewAuthenticationReader(auth, &crypto.AEADChunkSizeParser{
  163. Auth: auth,
  164. }, reader, protocol.TransferTypeStream), nil
  165. }
  166. func (c *AEADCipher) EncodePacket(key []byte, b *buf.Buffer) error {
  167. ivLen := c.IVSize()
  168. payloadLen := b.Len()
  169. auth := c.createAuthenticator(key, b.BytesTo(ivLen))
  170. return b.Reset(func(bb []byte) (int, error) {
  171. bbb, err := auth.Seal(bb[:ivLen], bb[ivLen:payloadLen])
  172. if err != nil {
  173. return 0, err
  174. }
  175. return len(bbb), nil
  176. })
  177. }
  178. func (c *AEADCipher) DecodePacket(key []byte, b *buf.Buffer) error {
  179. ivLen := c.IVSize()
  180. payloadLen := b.Len()
  181. auth := c.createAuthenticator(key, b.BytesTo(ivLen))
  182. if err := b.Reset(func(bb []byte) (int, error) {
  183. bbb, err := auth.Open(bb[:ivLen], bb[ivLen:payloadLen])
  184. if err != nil {
  185. return 0, err
  186. }
  187. return len(bbb), nil
  188. }); err != nil {
  189. return err
  190. }
  191. b.SliceFrom(ivLen)
  192. return nil
  193. }
  194. type ChaCha20 struct {
  195. IVBytes int
  196. }
  197. func (*ChaCha20) IsAEAD() bool {
  198. return false
  199. }
  200. func (v *ChaCha20) KeySize() int {
  201. return 32
  202. }
  203. func (v *ChaCha20) IVSize() int {
  204. return v.IVBytes
  205. }
  206. func (v *ChaCha20) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  207. stream := crypto.NewChaCha20Stream(key, iv)
  208. return buf.NewWriter(crypto.NewCryptionWriter(stream, writer)), nil
  209. }
  210. func (v *ChaCha20) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  211. stream := crypto.NewChaCha20Stream(key, iv)
  212. return buf.NewReader(crypto.NewCryptionReader(stream, reader)), nil
  213. }
  214. func (v *ChaCha20) EncodePacket(key []byte, b *buf.Buffer) error {
  215. iv := b.BytesTo(v.IVSize())
  216. stream := crypto.NewChaCha20Stream(key, iv)
  217. stream.XORKeyStream(b.BytesFrom(v.IVSize()), b.BytesFrom(v.IVSize()))
  218. return nil
  219. }
  220. func (v *ChaCha20) DecodePacket(key []byte, b *buf.Buffer) error {
  221. iv := b.BytesTo(v.IVSize())
  222. stream := crypto.NewChaCha20Stream(key, iv)
  223. stream.XORKeyStream(b.BytesFrom(v.IVSize()), b.BytesFrom(v.IVSize()))
  224. b.SliceFrom(v.IVSize())
  225. return nil
  226. }
  227. type NoneCipher struct{}
  228. func (NoneCipher) KeySize() int { return 0 }
  229. func (NoneCipher) IVSize() int { return 0 }
  230. func (NoneCipher) IsAEAD() bool {
  231. return true // to avoid OTA
  232. }
  233. func (NoneCipher) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  234. return buf.NewReader(reader), nil
  235. }
  236. func (NoneCipher) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  237. return buf.NewWriter(writer), nil
  238. }
  239. func (NoneCipher) EncodePacket(key []byte, b *buf.Buffer) error {
  240. return nil
  241. }
  242. func (NoneCipher) DecodePacket(key []byte, b *buf.Buffer) error {
  243. return nil
  244. }
  245. func passwordToCipherKey(password []byte, keySize int) []byte {
  246. key := make([]byte, 0, keySize)
  247. md5Sum := md5.Sum(password)
  248. key = append(key, md5Sum[:]...)
  249. for len(key) < keySize {
  250. md5Hash := md5.New()
  251. common.Must2(md5Hash.Write(md5Sum[:]))
  252. common.Must2(md5Hash.Write(password))
  253. md5Hash.Sum(md5Sum[:0])
  254. key = append(key, md5Sum[:]...)
  255. }
  256. return key
  257. }
  258. func hkdfSHA1(secret, salt, outkey []byte) {
  259. r := hkdf.New(sha1.New, secret, salt, []byte("ss-subkey"))
  260. common.Must2(io.ReadFull(r, outkey))
  261. }