eih_aes.go 2.6 KB

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