You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AeadAesGcmCrypto.cs 1.1 kB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Security.Cryptography;
  3. namespace Shadowsocks.Protocol.Shadowsocks.Crypto
  4. {
  5. class AeadAesGcmCrypto : ICrypto
  6. {
  7. AesGcm aes;
  8. CryptoParameter parameter;
  9. public AeadAesGcmCrypto(CryptoParameter parameter)
  10. {
  11. this.parameter = parameter;
  12. }
  13. public void Init(byte[] key, byte[] iv) => aes = new AesGcm(key);
  14. public int Decrypt(ReadOnlySpan<byte> nonce, Span<byte> plain, ReadOnlySpan<byte> cipher)
  15. {
  16. aes.Decrypt(
  17. nonce,
  18. cipher[0..^parameter.TagSize],
  19. cipher[^parameter.TagSize..],
  20. plain[0..(cipher.Length - parameter.TagSize)]);
  21. return cipher.Length - parameter.TagSize;
  22. }
  23. public int Encrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> plain, Span<byte> cipher)
  24. {
  25. aes.Encrypt(
  26. nonce,
  27. plain,
  28. cipher[0..plain.Length],
  29. cipher.Slice(plain.Length, parameter.TagSize));
  30. return plain.Length + parameter.TagSize;
  31. }
  32. }
  33. }