config.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. type ShadowsocksAccount struct {
  17. Cipher Cipher
  18. Key []byte
  19. OneTimeAuth Account_OneTimeAuth
  20. }
  21. func (v *ShadowsocksAccount) Equals(another protocol.Account) bool {
  22. if account, ok := another.(*ShadowsocksAccount); ok {
  23. return bytes.Equal(v.Key, account.Key)
  24. }
  25. return false
  26. }
  27. func createAesGcm(key []byte) cipher.AEAD {
  28. block, err := aes.NewCipher(key)
  29. common.Must(err)
  30. gcm, err := cipher.NewGCM(block)
  31. common.Must(err)
  32. return gcm
  33. }
  34. func createChacha20Poly1305(key []byte) cipher.AEAD {
  35. chacha20, err := chacha20poly1305.New(key)
  36. common.Must(err)
  37. return chacha20
  38. }
  39. func (v *Account) GetCipher() (Cipher, error) {
  40. switch v.CipherType {
  41. case CipherType_AES_128_CFB:
  42. return &AesCfb{KeyBytes: 16}, nil
  43. case CipherType_AES_256_CFB:
  44. return &AesCfb{KeyBytes: 32}, nil
  45. case CipherType_CHACHA20:
  46. return &ChaCha20{IVBytes: 8}, nil
  47. case CipherType_CHACHA20_IETF:
  48. return &ChaCha20{IVBytes: 12}, nil
  49. case CipherType_AES_128_GCM:
  50. return &AEADCipher{
  51. KeyBytes: 16,
  52. IVBytes: 16,
  53. AEADAuthCreator: createAesGcm,
  54. }, nil
  55. case CipherType_AES_256_GCM:
  56. return &AEADCipher{
  57. KeyBytes: 32,
  58. IVBytes: 32,
  59. AEADAuthCreator: createAesGcm,
  60. }, nil
  61. case CipherType_CHACHA20_POLY1305:
  62. return &AEADCipher{
  63. KeyBytes: 32,
  64. IVBytes: 32,
  65. AEADAuthCreator: createChacha20Poly1305,
  66. }, nil
  67. default:
  68. return nil, newError("Unsupported cipher.")
  69. }
  70. }
  71. func (v *Account) AsAccount() (protocol.Account, error) {
  72. cipher, err := v.GetCipher()
  73. if err != nil {
  74. return nil, newError("failed to get cipher").Base(err)
  75. }
  76. return &ShadowsocksAccount{
  77. Cipher: cipher,
  78. Key: v.GetCipherKey(),
  79. OneTimeAuth: v.Ota,
  80. }, nil
  81. }
  82. func (v *Account) GetCipherKey() []byte {
  83. ct, err := v.GetCipher()
  84. if err != nil {
  85. return nil
  86. }
  87. return PasswordToCipherKey(v.Password, ct.KeySize())
  88. }
  89. type Cipher interface {
  90. KeySize() int
  91. IVSize() int
  92. NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error)
  93. NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error)
  94. IsAEAD() bool
  95. }
  96. type AesCfb struct {
  97. KeyBytes int
  98. }
  99. func (*AesCfb) IsAEAD() bool {
  100. return false
  101. }
  102. func (v *AesCfb) KeySize() int {
  103. return v.KeyBytes
  104. }
  105. func (v *AesCfb) IVSize() int {
  106. return 16
  107. }
  108. func (v *AesCfb) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  109. stream := crypto.NewAesEncryptionStream(key, iv)
  110. return buf.NewWriter(crypto.NewCryptionWriter(stream, writer)), nil
  111. }
  112. func (v *AesCfb) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  113. stream := crypto.NewAesDecryptionStream(key, iv)
  114. return buf.NewReader(crypto.NewCryptionReader(stream, reader)), nil
  115. }
  116. type AEADCipher struct {
  117. KeyBytes int
  118. IVBytes int
  119. AEADAuthCreator func(key []byte) cipher.AEAD
  120. }
  121. func (*AEADCipher) IsAEAD() bool {
  122. return true
  123. }
  124. func (c *AEADCipher) KeySize() int {
  125. return c.KeyBytes
  126. }
  127. func (c *AEADCipher) IVSize() int {
  128. return c.IVBytes
  129. }
  130. func (c *AEADCipher) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  131. nonce := crypto.NewIncreasingAEADNonceGenerator()
  132. subkey := make([]byte, c.KeyBytes)
  133. hkdfSHA1(key, iv, subkey)
  134. auth := &crypto.AEADAuthenticator{
  135. AEAD: c.AEADAuthCreator(subkey),
  136. NonceGenerator: nonce,
  137. }
  138. return crypto.NewAuthenticationWriter(auth, &crypto.AEADChunkSizeParser{
  139. Auth: auth,
  140. }, writer, protocol.TransferTypeStream), nil
  141. }
  142. func (c *AEADCipher) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  143. nonce := crypto.NewIncreasingAEADNonceGenerator()
  144. subkey := make([]byte, c.KeyBytes)
  145. hkdfSHA1(key, iv, subkey)
  146. auth := &crypto.AEADAuthenticator{
  147. AEAD: c.AEADAuthCreator(subkey),
  148. NonceGenerator: nonce,
  149. }
  150. return crypto.NewAuthenticationReader(auth, &crypto.AEADChunkSizeParser{
  151. Auth: auth,
  152. }, reader, protocol.TransferTypeStream), nil
  153. }
  154. type ChaCha20 struct {
  155. IVBytes int
  156. }
  157. func (*ChaCha20) IsAEAD() bool {
  158. return false
  159. }
  160. func (v *ChaCha20) KeySize() int {
  161. return 32
  162. }
  163. func (v *ChaCha20) IVSize() int {
  164. return v.IVBytes
  165. }
  166. func (v *ChaCha20) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  167. stream := crypto.NewChaCha20Stream(key, iv)
  168. return buf.NewWriter(crypto.NewCryptionWriter(stream, writer)), nil
  169. }
  170. func (v *ChaCha20) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  171. stream := crypto.NewChaCha20Stream(key, iv)
  172. return buf.NewReader(crypto.NewCryptionReader(stream, reader)), nil
  173. }
  174. func PasswordToCipherKey(password string, keySize int) []byte {
  175. pwdBytes := []byte(password)
  176. key := make([]byte, 0, keySize)
  177. md5Sum := md5.Sum(pwdBytes)
  178. key = append(key, md5Sum[:]...)
  179. for len(key) < keySize {
  180. md5Hash := md5.New()
  181. md5Hash.Write(md5Sum[:])
  182. md5Hash.Write(pwdBytes)
  183. md5Hash.Sum(md5Sum[:0])
  184. key = append(key, md5Sum[:]...)
  185. }
  186. return key
  187. }
  188. func hkdfSHA1(secret, salt, outkey []byte) {
  189. r := hkdf.New(sha1.New, secret, salt, []byte("ss-subkey"))
  190. common.Must2(io.ReadFull(r, outkey))
  191. }