FastTlsCrypto.cs 7.8 KB

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