method_aes128gcm.go 908 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package shadowsocks2022
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. )
  6. func newAES128GCMMethod() *AES128GCMMethod {
  7. return &AES128GCMMethod{}
  8. }
  9. type AES128GCMMethod struct {
  10. }
  11. func (A AES128GCMMethod) GetSessionSubKeyAndSaltLength() int {
  12. return 16
  13. }
  14. func (A AES128GCMMethod) GetStreamAEAD(SessionSubKey []byte) (cipher.AEAD, error) {
  15. aesCipher, err := aes.NewCipher(SessionSubKey)
  16. if err != nil {
  17. return nil, newError("failed to create AES cipher").Base(err)
  18. }
  19. aead, err := cipher.NewGCM(aesCipher)
  20. if err != nil {
  21. return nil, newError("failed to create AES-GCM AEAD").Base(err)
  22. }
  23. return aead, nil
  24. }
  25. func (A AES128GCMMethod) GenerateEIH(CurrentIdentitySubKey []byte, nextPskHash []byte, out []byte) error {
  26. aesCipher, err := aes.NewCipher(CurrentIdentitySubKey)
  27. if err != nil {
  28. return newError("failed to create AES cipher").Base(err)
  29. }
  30. aesCipher.Encrypt(out, nextPskHash)
  31. return nil
  32. }