FastTlsCrypto.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  3. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC;
  9. using Best.HTTP.Shared.TLS.Crypto.Impl;
  10. namespace Best.HTTP.Shared.TLS.Crypto
  11. {
  12. public sealed class FastTlsCrypto : BcTlsCrypto
  13. {
  14. public FastTlsCrypto(SecureRandom entropySource)
  15. : base(entropySource)
  16. {
  17. }
  18. public override TlsCipher CreateCipher(TlsCryptoParameters cryptoParams, int encryptionAlgorithm, int macAlgorithm)
  19. {
  20. HTTPManager.Logger.Verbose(nameof(FastTlsCrypto), $"CreateCipher({encryptionAlgorithm}, {macAlgorithm})");
  21. switch (encryptionAlgorithm)
  22. {
  23. case EncryptionAlgorithm.CHACHA20_POLY1305:
  24. {
  25. // NOTE: Ignores macAlgorithm
  26. //return CreateChaCha20Poly1305(cryptoParams);
  27. BcChaCha20Poly1305 encrypt = new BcChaCha20Poly1305(true);
  28. BcChaCha20Poly1305 decrypt = new BcChaCha20Poly1305(false);
  29. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, 32, 16, TlsAeadCipher.AEAD_CHACHA20_POLY1305);
  30. }
  31. case EncryptionAlgorithm.AES_128_CBC:
  32. case EncryptionAlgorithm.ARIA_128_CBC:
  33. case EncryptionAlgorithm.CAMELLIA_128_CBC:
  34. case EncryptionAlgorithm.SEED_CBC:
  35. case EncryptionAlgorithm.SM4_CBC:
  36. {
  37. //return CreateCipher_Cbc(cryptoParams, encryptionAlgorithm, 16, macAlgorithm);
  38. FastTlsBlockCipherImpl encrypt = new FastTlsBlockCipherImpl(CreateCbcBlockCipher(encryptionAlgorithm), true);
  39. FastTlsBlockCipherImpl decrypt = new FastTlsBlockCipherImpl(CreateCbcBlockCipher(encryptionAlgorithm), false);
  40. TlsHmac clientMac = CreateMac(cryptoParams, macAlgorithm);
  41. TlsHmac serverMac = CreateMac(cryptoParams, macAlgorithm);
  42. return new FastTlsBlockCipher(cryptoParams, encrypt, decrypt, clientMac, serverMac, 16);
  43. }
  44. case EncryptionAlgorithm.AES_256_CBC:
  45. case EncryptionAlgorithm.ARIA_256_CBC:
  46. case EncryptionAlgorithm.CAMELLIA_256_CBC:
  47. {
  48. //return CreateCipher_Cbc(cryptoParams, encryptionAlgorithm, 32, macAlgorithm);
  49. FastTlsBlockCipherImpl encrypt = new FastTlsBlockCipherImpl(CreateCbcBlockCipher(encryptionAlgorithm), true);
  50. FastTlsBlockCipherImpl decrypt = new FastTlsBlockCipherImpl(CreateCbcBlockCipher(encryptionAlgorithm), false);
  51. TlsHmac clientMac = CreateMac(cryptoParams, macAlgorithm);
  52. TlsHmac serverMac = CreateMac(cryptoParams, macAlgorithm);
  53. return new FastTlsBlockCipher(cryptoParams, encrypt, decrypt, clientMac, serverMac, 32);
  54. }
  55. case EncryptionAlgorithm.AES_128_CCM:
  56. {
  57. // NOTE: Ignores macAlgorithm
  58. //return CreateCipher_Aes_Ccm(cryptoParams, 16, 16);
  59. FastTlsAeadCipherImpl encrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Ccm(), true);
  60. FastTlsAeadCipherImpl decrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Ccm(), false);
  61. return new FastTlsAeadCipher(cryptoParams, encrypt, decrypt, 16, 16, TlsAeadCipher.AEAD_CCM);
  62. }
  63. case EncryptionAlgorithm.AES_128_CCM_8:
  64. {
  65. // NOTE: Ignores macAlgorithm
  66. //return CreateCipher_Aes_Ccm(cryptoParams, 16, 8);
  67. FastTlsAeadCipherImpl encrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Ccm(), true);
  68. FastTlsAeadCipherImpl decrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Ccm(), false);
  69. return new FastTlsAeadCipher(cryptoParams, encrypt, decrypt, 16, 8, TlsAeadCipher.AEAD_CCM);
  70. }
  71. case EncryptionAlgorithm.AES_256_CCM:
  72. {
  73. // NOTE: Ignores macAlgorithm
  74. //return CreateCipher_Aes_Ccm(cryptoParams, 32, 16);
  75. FastTlsAeadCipherImpl encrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Ccm(), true);
  76. FastTlsAeadCipherImpl decrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Ccm(), false);
  77. return new FastTlsAeadCipher(cryptoParams, encrypt, decrypt, 32, 16, TlsAeadCipher.AEAD_CCM);
  78. }
  79. case EncryptionAlgorithm.AES_256_CCM_8:
  80. {
  81. // NOTE: Ignores macAlgorithm
  82. //return CreateCipher_Aes_Ccm(cryptoParams, 32, 8);
  83. FastTlsAeadCipherImpl encrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Ccm(), true);
  84. FastTlsAeadCipherImpl decrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Ccm(), false);
  85. return new FastTlsAeadCipher(cryptoParams, encrypt, decrypt, 32, 8, TlsAeadCipher.AEAD_CCM);
  86. }
  87. case EncryptionAlgorithm.AES_128_GCM:
  88. {
  89. // NOTE: Ignores macAlgorithm
  90. //return CreateCipher_Aes_Gcm(cryptoParams, 16, 16);
  91. FastTlsAeadCipherImpl encrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Gcm(), true);
  92. FastTlsAeadCipherImpl decrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Gcm(), false);
  93. return new FastTlsAeadCipher(cryptoParams, encrypt, decrypt, 16, 16, TlsAeadCipher.AEAD_GCM);
  94. }
  95. case EncryptionAlgorithm.AES_256_GCM:
  96. {
  97. // NOTE: Ignores macAlgorithm
  98. //return CreateCipher_Aes_Gcm(cryptoParams, 32, 16);
  99. FastTlsAeadCipherImpl encrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Gcm(), true);
  100. FastTlsAeadCipherImpl decrypt = new FastTlsAeadCipherImpl(CreateAeadCipher_Aes_Gcm(), false);
  101. return new FastTlsAeadCipher(cryptoParams, encrypt, decrypt, 32, 16, TlsAeadCipher.AEAD_GCM);
  102. }
  103. default:
  104. return base.CreateCipher(cryptoParams, encryptionAlgorithm, macAlgorithm);
  105. }
  106. }
  107. protected override IBlockCipher CreateAesEngine()
  108. {
  109. //return new AesEngine();
  110. return new FastAesEngine();
  111. }
  112. protected override IAeadCipher CreateCcmMode(IBlockCipher engine)
  113. {
  114. return new FastCcmBlockCipher(engine);
  115. }
  116. protected override IAeadCipher CreateGcmMode(IBlockCipher engine)
  117. {
  118. // TODO Consider allowing custom configuration of multiplier
  119. return new FastGcmBlockCipher(engine);
  120. }
  121. protected override IBlockCipher CreateCbcBlockCipher(IBlockCipher blockCipher)
  122. {
  123. return new FastCbcBlockCipher(blockCipher);
  124. }
  125. }
  126. }
  127. #endif