config.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. }
  95. type AesCfb struct {
  96. KeyBytes int
  97. }
  98. func (v *AesCfb) KeySize() int {
  99. return v.KeyBytes
  100. }
  101. func (v *AesCfb) IVSize() int {
  102. return 16
  103. }
  104. func (v *AesCfb) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  105. stream := crypto.NewAesEncryptionStream(key, iv)
  106. return buf.NewWriter(crypto.NewCryptionWriter(stream, writer)), nil
  107. }
  108. func (v *AesCfb) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  109. stream := crypto.NewAesDecryptionStream(key, iv)
  110. return buf.NewReader(crypto.NewCryptionReader(stream, reader)), nil
  111. }
  112. type AEADCipher struct {
  113. KeyBytes int
  114. IVBytes int
  115. AEADAuthCreator func(key []byte) cipher.AEAD
  116. }
  117. func (c *AEADCipher) KeySize() int {
  118. return c.KeyBytes
  119. }
  120. func (c *AEADCipher) IVSize() int {
  121. return c.IVBytes
  122. }
  123. func (c *AEADCipher) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  124. nonce := crypto.NewIncreasingAEADNonceGenerator()
  125. subkey := make([]byte, c.KeyBytes)
  126. hkdfSHA1(key, iv, subkey)
  127. auth := &crypto.AEADAuthenticator{
  128. AEAD: c.AEADAuthCreator(subkey),
  129. NonceGenerator: nonce,
  130. }
  131. return crypto.NewAuthenticationWriter(auth, &crypto.AEADChunkSizeParser{
  132. Auth: auth,
  133. }, writer, protocol.TransferTypeStream), nil
  134. }
  135. func (c *AEADCipher) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  136. nonce := crypto.NewIncreasingAEADNonceGenerator()
  137. subkey := make([]byte, c.KeyBytes)
  138. hkdfSHA1(key, iv, subkey)
  139. auth := &crypto.AEADAuthenticator{
  140. AEAD: c.AEADAuthCreator(subkey),
  141. NonceGenerator: nonce,
  142. }
  143. return crypto.NewAuthenticationReader(auth, &crypto.AEADChunkSizeParser{
  144. Auth: auth,
  145. }, reader, protocol.TransferTypeStream), nil
  146. }
  147. type ChaCha20 struct {
  148. IVBytes int
  149. }
  150. func (v *ChaCha20) KeySize() int {
  151. return 32
  152. }
  153. func (v *ChaCha20) IVSize() int {
  154. return v.IVBytes
  155. }
  156. func (v *ChaCha20) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  157. stream := crypto.NewChaCha20Stream(key, iv)
  158. return buf.NewWriter(crypto.NewCryptionWriter(stream, writer)), nil
  159. }
  160. func (v *ChaCha20) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  161. stream := crypto.NewChaCha20Stream(key, iv)
  162. return buf.NewReader(crypto.NewCryptionReader(stream, reader)), nil
  163. }
  164. func PasswordToCipherKey(password string, keySize int) []byte {
  165. pwdBytes := []byte(password)
  166. key := make([]byte, 0, keySize)
  167. md5Sum := md5.Sum(pwdBytes)
  168. key = append(key, md5Sum[:]...)
  169. for len(key) < keySize {
  170. md5Hash := md5.New()
  171. md5Hash.Write(md5Sum[:])
  172. md5Hash.Write(pwdBytes)
  173. md5Hash.Sum(md5Sum[:0])
  174. key = append(key, md5Sum[:]...)
  175. }
  176. return key
  177. }
  178. func hkdfSHA1(secret, salt, outkey []byte) {
  179. r := hkdf.New(sha1.New, secret, salt, []byte("ss-subkey"))
  180. common.Must2(io.ReadFull(r, outkey))
  181. }