eih_aes.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package shadowsocks2022
  2. import (
  3. "crypto/subtle"
  4. "io"
  5. "github.com/lunixbochs/struc"
  6. "github.com/v2fly/v2ray-core/v5/common/buf"
  7. "lukechampine.com/blake3"
  8. )
  9. func newAESEIH(size int) *aesEIH {
  10. return &aesEIH{length: size}
  11. }
  12. func newAESEIHWithData(size int, eih [][aesEIHSize]byte) *aesEIH {
  13. return &aesEIH{length: size, eih: eih}
  14. }
  15. const aesEIHSize = 16
  16. type aesEIH struct {
  17. eih [][aesEIHSize]byte
  18. length int
  19. }
  20. func (a *aesEIH) Pack(p []byte, opt *struc.Options) (int, error) {
  21. var totalCopy int
  22. for i := 0; i < a.length; i++ {
  23. n := copy(p[aesEIHSize*i:aesEIHSize*(i+1)], a.eih[i][:])
  24. if n != 16 {
  25. return 0, newError("failed to pack aesEIH")
  26. }
  27. totalCopy += n
  28. }
  29. return totalCopy, nil
  30. }
  31. func (a *aesEIH) Unpack(r io.Reader, length int, opt *struc.Options) error {
  32. a.eih = make([][aesEIHSize]byte, a.length)
  33. for i := 0; i < a.length; i++ {
  34. n, err := r.Read(a.eih[i][:])
  35. if err != nil {
  36. return newError("failed to unpack aesEIH").Base(err)
  37. }
  38. if n != aesEIHSize {
  39. return newError("failed to unpack aesEIH")
  40. }
  41. }
  42. return nil
  43. }
  44. func (a *aesEIH) Size(opt *struc.Options) int {
  45. return a.length * aesEIHSize
  46. }
  47. func (a *aesEIH) String() string {
  48. return ""
  49. }
  50. const aesEIHPskHashSize = 16
  51. type aesEIHGenerator struct {
  52. ipsk [][]byte
  53. ipskHash [][aesEIHPskHashSize]byte
  54. psk []byte
  55. pskHash [aesEIHPskHashSize]byte
  56. length int
  57. }
  58. func newAESEIHGeneratorContainer(size int, effectivePsk []byte, ipsk [][]byte) *aesEIHGenerator {
  59. var ipskHash [][aesEIHPskHashSize]byte
  60. for _, v := range ipsk {
  61. hash := blake3.Sum512(v)
  62. ipskHash = append(ipskHash, [aesEIHPskHashSize]byte(hash[:16]))
  63. }
  64. pskHashFull := blake3.Sum512(effectivePsk)
  65. pskHash := [aesEIHPskHashSize]byte(pskHashFull[:16])
  66. return &aesEIHGenerator{length: size, ipsk: ipsk, ipskHash: ipskHash, psk: effectivePsk, pskHash: pskHash}
  67. }
  68. func (a *aesEIHGenerator) GenerateEIH(derivation KeyDerivation, method Method, salt []byte) (ExtensibleIdentityHeaders, error) {
  69. return a.generateEIHWithMask(derivation, method, salt, nil)
  70. }
  71. func (a *aesEIHGenerator) GenerateEIHUDP(derivation KeyDerivation, method Method, mask []byte) (ExtensibleIdentityHeaders, error) {
  72. return a.generateEIHWithMask(derivation, method, nil, mask)
  73. }
  74. func (a *aesEIHGenerator) generateEIHWithMask(derivation KeyDerivation, method Method, salt, mask []byte) (ExtensibleIdentityHeaders, error) {
  75. eih := make([][16]byte, a.length)
  76. current := a.length - 1
  77. currentPskHash := a.pskHash
  78. for {
  79. identityKeyBuf := buf.New()
  80. identityKey := identityKeyBuf.Extend(int32(method.GetSessionSubKeyAndSaltLength()))
  81. if mask == nil {
  82. err := derivation.GetIdentitySubKey(a.ipsk[current], salt, identityKey)
  83. if err != nil {
  84. return nil, newError("failed to get identity sub key").Base(err)
  85. }
  86. } else {
  87. copy(identityKey, a.ipsk[current])
  88. }
  89. eih[current] = [16]byte{}
  90. if mask != nil {
  91. subtle.XORBytes(currentPskHash[:], mask, currentPskHash[:])
  92. }
  93. err := method.GenerateEIH(identityKey, currentPskHash[:], eih[current][:])
  94. if err != nil {
  95. return nil, newError("failed to generate EIH").Base(err)
  96. }
  97. current--
  98. if current < 0 {
  99. break
  100. }
  101. currentPskHash = a.ipskHash[current]
  102. identityKeyBuf.Release()
  103. }
  104. return newAESEIHWithData(a.length, eih), nil
  105. }