BcTlsCrypto.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement.Srp;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  15. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  16. {
  17. /**
  18. * Class for providing cryptographic services for TLS based on implementations in the BC light-weight API.
  19. * <p>
  20. * This class provides default implementations for everything. If you need to customise it, extend the class
  21. * and override the appropriate methods.
  22. * </p>
  23. */
  24. public class BcTlsCrypto
  25. : AbstractTlsCrypto
  26. {
  27. private readonly SecureRandom m_entropySource;
  28. public BcTlsCrypto(SecureRandom entropySource)
  29. {
  30. this.m_entropySource = entropySource;
  31. }
  32. internal virtual BcTlsSecret AdoptLocalSecret(byte[] data)
  33. {
  34. return new BcTlsSecret(this, data);
  35. }
  36. public override SecureRandom SecureRandom
  37. {
  38. get { return m_entropySource; }
  39. }
  40. public override TlsCertificate CreateCertificate(byte[] encoding)
  41. {
  42. return new BcTlsCertificate(this, encoding);
  43. }
  44. public override TlsCipher CreateCipher(TlsCryptoParameters cryptoParams, int encryptionAlgorithm,
  45. int macAlgorithm)
  46. {
  47. switch (encryptionAlgorithm)
  48. {
  49. case EncryptionAlgorithm.AES_128_CBC:
  50. case EncryptionAlgorithm.ARIA_128_CBC:
  51. case EncryptionAlgorithm.CAMELLIA_128_CBC:
  52. case EncryptionAlgorithm.SEED_CBC:
  53. case EncryptionAlgorithm.SM4_CBC:
  54. return CreateCipher_Cbc(cryptoParams, encryptionAlgorithm, 16, macAlgorithm);
  55. case EncryptionAlgorithm.cls_3DES_EDE_CBC:
  56. return CreateCipher_Cbc(cryptoParams, encryptionAlgorithm, 24, macAlgorithm);
  57. case EncryptionAlgorithm.AES_256_CBC:
  58. case EncryptionAlgorithm.ARIA_256_CBC:
  59. case EncryptionAlgorithm.CAMELLIA_256_CBC:
  60. return CreateCipher_Cbc(cryptoParams, encryptionAlgorithm, 32, macAlgorithm);
  61. case EncryptionAlgorithm.AES_128_CCM:
  62. // NOTE: Ignores macAlgorithm
  63. return CreateCipher_Aes_Ccm(cryptoParams, 16, 16);
  64. case EncryptionAlgorithm.AES_128_CCM_8:
  65. // NOTE: Ignores macAlgorithm
  66. return CreateCipher_Aes_Ccm(cryptoParams, 16, 8);
  67. case EncryptionAlgorithm.AES_128_GCM:
  68. // NOTE: Ignores macAlgorithm
  69. return CreateCipher_Aes_Gcm(cryptoParams, 16, 16);
  70. case EncryptionAlgorithm.AES_256_CCM:
  71. // NOTE: Ignores macAlgorithm
  72. return CreateCipher_Aes_Ccm(cryptoParams, 32, 16);
  73. case EncryptionAlgorithm.AES_256_CCM_8:
  74. // NOTE: Ignores macAlgorithm
  75. return CreateCipher_Aes_Ccm(cryptoParams, 32, 8);
  76. case EncryptionAlgorithm.AES_256_GCM:
  77. // NOTE: Ignores macAlgorithm
  78. return CreateCipher_Aes_Gcm(cryptoParams, 32, 16);
  79. case EncryptionAlgorithm.ARIA_128_GCM:
  80. // NOTE: Ignores macAlgorithm
  81. return CreateCipher_Aria_Gcm(cryptoParams, 16, 16);
  82. case EncryptionAlgorithm.ARIA_256_GCM:
  83. // NOTE: Ignores macAlgorithm
  84. return CreateCipher_Aria_Gcm(cryptoParams, 32, 16);
  85. case EncryptionAlgorithm.CAMELLIA_128_GCM:
  86. // NOTE: Ignores macAlgorithm
  87. return CreateCipher_Camellia_Gcm(cryptoParams, 16, 16);
  88. case EncryptionAlgorithm.CAMELLIA_256_GCM:
  89. // NOTE: Ignores macAlgorithm
  90. return CreateCipher_Camellia_Gcm(cryptoParams, 32, 16);
  91. case EncryptionAlgorithm.CHACHA20_POLY1305:
  92. // NOTE: Ignores macAlgorithm
  93. return CreateChaCha20Poly1305(cryptoParams);
  94. case EncryptionAlgorithm.NULL:
  95. return CreateNullCipher(cryptoParams, macAlgorithm);
  96. case EncryptionAlgorithm.SM4_CCM:
  97. // NOTE: Ignores macAlgorithm
  98. return CreateCipher_SM4_Ccm(cryptoParams);
  99. case EncryptionAlgorithm.SM4_GCM:
  100. // NOTE: Ignores macAlgorithm
  101. return CreateCipher_SM4_Gcm(cryptoParams);
  102. case EncryptionAlgorithm.DES40_CBC:
  103. case EncryptionAlgorithm.DES_CBC:
  104. case EncryptionAlgorithm.IDEA_CBC:
  105. case EncryptionAlgorithm.RC2_CBC_40:
  106. case EncryptionAlgorithm.RC4_128:
  107. case EncryptionAlgorithm.RC4_40:
  108. default:
  109. throw new TlsFatalAlert(AlertDescription.internal_error);
  110. }
  111. }
  112. public override TlsDHDomain CreateDHDomain(TlsDHConfig dhConfig)
  113. {
  114. return new BcTlsDHDomain(this, dhConfig);
  115. }
  116. public override TlsECDomain CreateECDomain(TlsECConfig ecConfig)
  117. {
  118. switch (ecConfig.NamedGroup)
  119. {
  120. case NamedGroup.x25519:
  121. return new BcX25519Domain(this);
  122. case NamedGroup.x448:
  123. return new BcX448Domain(this);
  124. default:
  125. return new BcTlsECDomain(this, ecConfig);
  126. }
  127. }
  128. public override TlsNonceGenerator CreateNonceGenerator(byte[] additionalSeedMaterial)
  129. {
  130. IDigest digest = CreateDigest(CryptoHashAlgorithm.sha256);
  131. byte[] seed = new byte[digest.GetDigestSize()];
  132. SecureRandom.NextBytes(seed);
  133. DigestRandomGenerator randomGenerator = new DigestRandomGenerator(digest);
  134. randomGenerator.AddSeedMaterial(additionalSeedMaterial);
  135. randomGenerator.AddSeedMaterial(seed);
  136. return new BcTlsNonceGenerator(randomGenerator);
  137. }
  138. public override bool HasAllRawSignatureAlgorithms()
  139. {
  140. // TODO[RFC 8422] Revisit the need to buffer the handshake for "Intrinsic" hash signatures
  141. return !HasSignatureAlgorithm(SignatureAlgorithm.ed25519)
  142. && !HasSignatureAlgorithm(SignatureAlgorithm.ed448);
  143. }
  144. public override bool HasDHAgreement()
  145. {
  146. return true;
  147. }
  148. public override bool HasECDHAgreement()
  149. {
  150. return true;
  151. }
  152. public override bool HasEncryptionAlgorithm(int encryptionAlgorithm)
  153. {
  154. switch (encryptionAlgorithm)
  155. {
  156. case EncryptionAlgorithm.DES40_CBC:
  157. case EncryptionAlgorithm.DES_CBC:
  158. case EncryptionAlgorithm.IDEA_CBC:
  159. case EncryptionAlgorithm.RC2_CBC_40:
  160. case EncryptionAlgorithm.RC4_128:
  161. case EncryptionAlgorithm.RC4_40:
  162. return false;
  163. default:
  164. return true;
  165. }
  166. }
  167. public override bool HasCryptoHashAlgorithm(int cryptoHashAlgorithm)
  168. {
  169. return true;
  170. }
  171. public override bool HasCryptoSignatureAlgorithm(int cryptoSignatureAlgorithm)
  172. {
  173. switch (cryptoSignatureAlgorithm)
  174. {
  175. case CryptoSignatureAlgorithm.rsa:
  176. case CryptoSignatureAlgorithm.dsa:
  177. case CryptoSignatureAlgorithm.ecdsa:
  178. case CryptoSignatureAlgorithm.rsa_pss_rsae_sha256:
  179. case CryptoSignatureAlgorithm.rsa_pss_rsae_sha384:
  180. case CryptoSignatureAlgorithm.rsa_pss_rsae_sha512:
  181. case CryptoSignatureAlgorithm.ed25519:
  182. case CryptoSignatureAlgorithm.ed448:
  183. case CryptoSignatureAlgorithm.rsa_pss_pss_sha256:
  184. case CryptoSignatureAlgorithm.rsa_pss_pss_sha384:
  185. case CryptoSignatureAlgorithm.rsa_pss_pss_sha512:
  186. return true;
  187. // TODO[draft-smyshlyaev-tls12-gost-suites-10]
  188. case CryptoSignatureAlgorithm.gostr34102012_256:
  189. case CryptoSignatureAlgorithm.gostr34102012_512:
  190. // TODO[RFC 8998]
  191. case CryptoSignatureAlgorithm.sm2:
  192. default:
  193. return false;
  194. }
  195. }
  196. public override bool HasMacAlgorithm(int macAlgorithm)
  197. {
  198. return true;
  199. }
  200. public override bool HasNamedGroup(int namedGroup)
  201. {
  202. return NamedGroup.RefersToASpecificGroup(namedGroup);
  203. }
  204. public override bool HasRsaEncryption()
  205. {
  206. return true;
  207. }
  208. public override bool HasSignatureAlgorithm(short signatureAlgorithm)
  209. {
  210. switch (signatureAlgorithm)
  211. {
  212. case SignatureAlgorithm.rsa:
  213. case SignatureAlgorithm.dsa:
  214. case SignatureAlgorithm.ecdsa:
  215. case SignatureAlgorithm.ed25519:
  216. case SignatureAlgorithm.ed448:
  217. case SignatureAlgorithm.rsa_pss_rsae_sha256:
  218. case SignatureAlgorithm.rsa_pss_rsae_sha384:
  219. case SignatureAlgorithm.rsa_pss_rsae_sha512:
  220. case SignatureAlgorithm.rsa_pss_pss_sha256:
  221. case SignatureAlgorithm.rsa_pss_pss_sha384:
  222. case SignatureAlgorithm.rsa_pss_pss_sha512:
  223. case SignatureAlgorithm.ecdsa_brainpoolP256r1tls13_sha256:
  224. case SignatureAlgorithm.ecdsa_brainpoolP384r1tls13_sha384:
  225. case SignatureAlgorithm.ecdsa_brainpoolP512r1tls13_sha512:
  226. return true;
  227. // TODO[draft-smyshlyaev-tls12-gost-suites-10]
  228. case SignatureAlgorithm.gostr34102012_256:
  229. case SignatureAlgorithm.gostr34102012_512:
  230. // TODO[RFC 8998]
  231. //case SignatureAlgorithm.sm2:
  232. default:
  233. return false;
  234. }
  235. }
  236. public override bool HasSignatureAndHashAlgorithm(SignatureAndHashAlgorithm sigAndHashAlgorithm)
  237. {
  238. short signature = sigAndHashAlgorithm.Signature;
  239. switch (sigAndHashAlgorithm.Hash)
  240. {
  241. case HashAlgorithm.md5:
  242. return SignatureAlgorithm.rsa == signature && HasSignatureAlgorithm(signature);
  243. default:
  244. return HasSignatureAlgorithm(signature);
  245. }
  246. }
  247. public override bool HasSignatureScheme(int signatureScheme)
  248. {
  249. switch (signatureScheme)
  250. {
  251. case SignatureScheme.sm2sig_sm3:
  252. return false;
  253. default:
  254. {
  255. short signature = SignatureScheme.GetSignatureAlgorithm(signatureScheme);
  256. switch(SignatureScheme.GetCryptoHashAlgorithm(signatureScheme))
  257. {
  258. case CryptoHashAlgorithm.md5:
  259. return SignatureAlgorithm.rsa == signature && HasSignatureAlgorithm(signature);
  260. default:
  261. return HasSignatureAlgorithm(signature);
  262. }
  263. }
  264. }
  265. }
  266. public override bool HasSrpAuthentication()
  267. {
  268. return true;
  269. }
  270. public override TlsSecret CreateSecret(byte[] data)
  271. {
  272. try
  273. {
  274. return AdoptLocalSecret(Arrays.Clone(data));
  275. }
  276. finally
  277. {
  278. // TODO[tls-ops] Add this after checking all callers
  279. //if (data != null)
  280. //{
  281. // Array.Clear(data, 0, data.Length);
  282. //}
  283. }
  284. }
  285. public override TlsSecret GenerateRsaPreMasterSecret(ProtocolVersion version)
  286. {
  287. byte[] data = new byte[48];
  288. SecureRandom.NextBytes(data);
  289. TlsUtilities.WriteVersion(version, data, 0);
  290. return AdoptLocalSecret(data);
  291. }
  292. public virtual IDigest CloneDigest(int cryptoHashAlgorithm, IDigest digest)
  293. {
  294. switch (cryptoHashAlgorithm)
  295. {
  296. case CryptoHashAlgorithm.md5:
  297. return new MD5Digest((MD5Digest)digest);
  298. case CryptoHashAlgorithm.sha1:
  299. return new Sha1Digest((Sha1Digest)digest);
  300. case CryptoHashAlgorithm.sha224:
  301. return new Sha224Digest((Sha224Digest)digest);
  302. case CryptoHashAlgorithm.sha256:
  303. return new Sha256Digest((Sha256Digest)digest);
  304. case CryptoHashAlgorithm.sha384:
  305. return new Sha384Digest((Sha384Digest)digest);
  306. case CryptoHashAlgorithm.sha512:
  307. return new Sha512Digest((Sha512Digest)digest);
  308. case CryptoHashAlgorithm.sm3:
  309. return new SM3Digest((SM3Digest)digest);
  310. default:
  311. throw new ArgumentException("invalid CryptoHashAlgorithm: " + cryptoHashAlgorithm);
  312. }
  313. }
  314. public virtual IDigest CreateDigest(int cryptoHashAlgorithm)
  315. {
  316. switch (cryptoHashAlgorithm)
  317. {
  318. case CryptoHashAlgorithm.md5:
  319. return new MD5Digest();
  320. case CryptoHashAlgorithm.sha1:
  321. return new Sha1Digest();
  322. case CryptoHashAlgorithm.sha224:
  323. return new Sha224Digest();
  324. case CryptoHashAlgorithm.sha256:
  325. return new Sha256Digest();
  326. case CryptoHashAlgorithm.sha384:
  327. return new Sha384Digest();
  328. case CryptoHashAlgorithm.sha512:
  329. return new Sha512Digest();
  330. case CryptoHashAlgorithm.sm3:
  331. return new SM3Digest();
  332. default:
  333. throw new ArgumentException("invalid CryptoHashAlgorithm: " + cryptoHashAlgorithm);
  334. }
  335. }
  336. public override TlsHash CreateHash(int cryptoHashAlgorithm)
  337. {
  338. return new BcTlsHash(this, cryptoHashAlgorithm);
  339. }
  340. protected virtual IBlockCipher CreateBlockCipher(int encryptionAlgorithm)
  341. {
  342. switch (encryptionAlgorithm)
  343. {
  344. case EncryptionAlgorithm.cls_3DES_EDE_CBC:
  345. return CreateDesEdeEngine();
  346. case EncryptionAlgorithm.AES_128_CBC:
  347. case EncryptionAlgorithm.AES_256_CBC:
  348. return CreateAesEngine();
  349. case EncryptionAlgorithm.ARIA_128_CBC:
  350. case EncryptionAlgorithm.ARIA_256_CBC:
  351. return CreateAriaEngine();
  352. case EncryptionAlgorithm.CAMELLIA_128_CBC:
  353. case EncryptionAlgorithm.CAMELLIA_256_CBC:
  354. return CreateCamelliaEngine();
  355. case EncryptionAlgorithm.SEED_CBC:
  356. return CreateSeedEngine();
  357. case EncryptionAlgorithm.SM4_CBC:
  358. return CreateSM4Engine();
  359. default:
  360. throw new TlsFatalAlert(AlertDescription.internal_error);
  361. }
  362. }
  363. protected virtual IBlockCipher CreateCbcBlockCipher(IBlockCipher blockCipher)
  364. {
  365. return new CbcBlockCipher(blockCipher);
  366. }
  367. protected virtual IBlockCipher CreateCbcBlockCipher(int encryptionAlgorithm)
  368. {
  369. return CreateCbcBlockCipher(CreateBlockCipher(encryptionAlgorithm));
  370. }
  371. protected virtual TlsCipher CreateChaCha20Poly1305(TlsCryptoParameters cryptoParams)
  372. {
  373. BcChaCha20Poly1305 encrypt = new BcChaCha20Poly1305(true);
  374. BcChaCha20Poly1305 decrypt = new BcChaCha20Poly1305(false);
  375. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, 32, 16, TlsAeadCipher.AEAD_CHACHA20_POLY1305);
  376. }
  377. protected virtual TlsAeadCipher CreateCipher_Aes_Ccm(TlsCryptoParameters cryptoParams, int cipherKeySize,
  378. int macSize)
  379. {
  380. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_Aes_Ccm(), true);
  381. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_Aes_Ccm(), false);
  382. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAeadCipher.AEAD_CCM);
  383. }
  384. protected virtual TlsAeadCipher CreateCipher_Aes_Gcm(TlsCryptoParameters cryptoParams, int cipherKeySize,
  385. int macSize)
  386. {
  387. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_Aes_Gcm(), true);
  388. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_Aes_Gcm(), false);
  389. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAeadCipher.AEAD_GCM);
  390. }
  391. protected virtual TlsAeadCipher CreateCipher_Aria_Gcm(TlsCryptoParameters cryptoParams, int cipherKeySize,
  392. int macSize)
  393. {
  394. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_Aria_Gcm(), true);
  395. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_Aria_Gcm(), false);
  396. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAeadCipher.AEAD_GCM);
  397. }
  398. protected virtual TlsAeadCipher CreateCipher_Camellia_Gcm(TlsCryptoParameters cryptoParams, int cipherKeySize,
  399. int macSize)
  400. {
  401. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_Camellia_Gcm(), true);
  402. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_Camellia_Gcm(), false);
  403. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAeadCipher.AEAD_GCM);
  404. }
  405. protected virtual TlsCipher CreateCipher_Cbc(TlsCryptoParameters cryptoParams, int encryptionAlgorithm,
  406. int cipherKeySize, int macAlgorithm)
  407. {
  408. BcTlsBlockCipherImpl encrypt = new BcTlsBlockCipherImpl(CreateCbcBlockCipher(encryptionAlgorithm), true);
  409. BcTlsBlockCipherImpl decrypt = new BcTlsBlockCipherImpl(CreateCbcBlockCipher(encryptionAlgorithm), false);
  410. TlsHmac clientMac = CreateMac(cryptoParams, macAlgorithm);
  411. TlsHmac serverMac = CreateMac(cryptoParams, macAlgorithm);
  412. return new TlsBlockCipher(cryptoParams, encrypt, decrypt, clientMac, serverMac, cipherKeySize);
  413. }
  414. protected virtual TlsAeadCipher CreateCipher_SM4_Ccm(TlsCryptoParameters cryptoParams)
  415. {
  416. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_SM4_Ccm(), true);
  417. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_SM4_Ccm(), false);
  418. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, 16, 16, TlsAeadCipher.AEAD_CCM);
  419. }
  420. protected virtual TlsAeadCipher CreateCipher_SM4_Gcm(TlsCryptoParameters cryptoParams)
  421. {
  422. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_SM4_Gcm(), true);
  423. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadBlockCipher_SM4_Gcm(), false);
  424. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, 16, 16, TlsAeadCipher.AEAD_GCM);
  425. }
  426. protected virtual TlsNullCipher CreateNullCipher(TlsCryptoParameters cryptoParams, int macAlgorithm)
  427. {
  428. return new TlsNullCipher(cryptoParams, CreateMac(cryptoParams, macAlgorithm),
  429. CreateMac(cryptoParams, macAlgorithm));
  430. }
  431. protected virtual IBlockCipher CreateAesEngine()
  432. {
  433. //return new AesEngine();
  434. return new AesFastEngine();
  435. }
  436. protected virtual IBlockCipher CreateAriaEngine()
  437. {
  438. return new AriaEngine();
  439. }
  440. protected virtual IBlockCipher CreateCamelliaEngine()
  441. {
  442. return new CamelliaEngine();
  443. }
  444. protected virtual IBlockCipher CreateDesEdeEngine()
  445. {
  446. return new DesEdeEngine();
  447. }
  448. protected virtual IBlockCipher CreateSeedEngine()
  449. {
  450. return new SeedEngine();
  451. }
  452. protected virtual IBlockCipher CreateSM4Engine()
  453. {
  454. return new SM4Engine();
  455. }
  456. protected virtual IAeadBlockCipher CreateCcmMode(IBlockCipher engine)
  457. {
  458. return new CcmBlockCipher(engine);
  459. }
  460. protected virtual IAeadBlockCipher CreateGcmMode(IBlockCipher engine)
  461. {
  462. // TODO Consider allowing custom configuration of multiplier
  463. return new GcmBlockCipher(engine);
  464. }
  465. protected virtual IAeadBlockCipher CreateAeadBlockCipher_Aes_Ccm()
  466. {
  467. return CreateCcmMode(CreateAesEngine());
  468. }
  469. protected virtual IAeadBlockCipher CreateAeadBlockCipher_Aes_Gcm()
  470. {
  471. return CreateGcmMode(CreateAesEngine());
  472. }
  473. protected virtual IAeadBlockCipher CreateAeadBlockCipher_Aria_Gcm()
  474. {
  475. return CreateGcmMode(CreateAriaEngine());
  476. }
  477. protected virtual IAeadBlockCipher CreateAeadBlockCipher_Camellia_Gcm()
  478. {
  479. return CreateGcmMode(CreateCamelliaEngine());
  480. }
  481. protected virtual IAeadBlockCipher CreateAeadBlockCipher_SM4_Ccm()
  482. {
  483. return CreateCcmMode(CreateSM4Engine());
  484. }
  485. protected virtual IAeadBlockCipher CreateAeadBlockCipher_SM4_Gcm()
  486. {
  487. return CreateGcmMode(CreateSM4Engine());
  488. }
  489. public override TlsHmac CreateHmac(int macAlgorithm)
  490. {
  491. return CreateHmacForHash(TlsCryptoUtilities.GetHashForHmac(macAlgorithm));
  492. }
  493. public override TlsHmac CreateHmacForHash(int cryptoHashAlgorithm)
  494. {
  495. return new BcTlsHmac(new HMac(CreateDigest(cryptoHashAlgorithm)));
  496. }
  497. protected virtual TlsHmac CreateHmac_Ssl(int macAlgorithm)
  498. {
  499. switch (macAlgorithm)
  500. {
  501. case MacAlgorithm.hmac_md5:
  502. return new BcSsl3Hmac(CreateDigest(CryptoHashAlgorithm.md5));
  503. case MacAlgorithm.hmac_sha1:
  504. return new BcSsl3Hmac(CreateDigest(CryptoHashAlgorithm.sha1));
  505. case MacAlgorithm.hmac_sha256:
  506. return new BcSsl3Hmac(CreateDigest(CryptoHashAlgorithm.sha256));
  507. case MacAlgorithm.hmac_sha384:
  508. return new BcSsl3Hmac(CreateDigest(CryptoHashAlgorithm.sha384));
  509. case MacAlgorithm.hmac_sha512:
  510. return new BcSsl3Hmac(CreateDigest(CryptoHashAlgorithm.sha512));
  511. default:
  512. throw new TlsFatalAlert(AlertDescription.internal_error);
  513. }
  514. }
  515. protected virtual TlsHmac CreateMac(TlsCryptoParameters cryptoParams, int macAlgorithm)
  516. {
  517. if (TlsImplUtilities.IsSsl(cryptoParams))
  518. {
  519. return CreateHmac_Ssl(macAlgorithm);
  520. }
  521. else
  522. {
  523. return CreateHmac(macAlgorithm);
  524. }
  525. }
  526. public override TlsSrp6Client CreateSrp6Client(TlsSrpConfig srpConfig)
  527. {
  528. BigInteger[] ng = srpConfig.GetExplicitNG();
  529. Srp6GroupParameters srpGroup = new Srp6GroupParameters(ng[0], ng[1]);
  530. Srp6Client srp6Client = new Srp6Client();
  531. srp6Client.Init(srpGroup, CreateDigest(CryptoHashAlgorithm.sha1), SecureRandom);
  532. return new BcTlsSrp6Client(srp6Client);
  533. }
  534. public override TlsSrp6Server CreateSrp6Server(TlsSrpConfig srpConfig, BigInteger srpVerifier)
  535. {
  536. BigInteger[] ng = srpConfig.GetExplicitNG();
  537. Srp6GroupParameters srpGroup = new Srp6GroupParameters(ng[0], ng[1]);
  538. Srp6Server srp6Server = new Srp6Server();
  539. srp6Server.Init(srpGroup, srpVerifier, CreateDigest(CryptoHashAlgorithm.sha1), SecureRandom);
  540. return new BcTlsSrp6Server(srp6Server);
  541. }
  542. public override TlsSrp6VerifierGenerator CreateSrp6VerifierGenerator(TlsSrpConfig srpConfig)
  543. {
  544. BigInteger[] ng = srpConfig.GetExplicitNG();
  545. Srp6VerifierGenerator srp6VerifierGenerator = new Srp6VerifierGenerator();
  546. srp6VerifierGenerator.Init(ng[0], ng[1], CreateDigest(CryptoHashAlgorithm.sha1));
  547. return new BcTlsSrp6VerifierGenerator(srp6VerifierGenerator);
  548. }
  549. public override TlsSecret HkdfInit(int cryptoHashAlgorithm)
  550. {
  551. return AdoptLocalSecret(new byte[TlsCryptoUtilities.GetHashOutputSize(cryptoHashAlgorithm)]);
  552. }
  553. }
  554. }
  555. #pragma warning restore
  556. #endif