TlsAeadCipher.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.IO;
  4. using Org.BouncyCastle.Crypto.Modes;
  5. using Org.BouncyCastle.Crypto.Parameters;
  6. using Org.BouncyCastle.Utilities;
  7. namespace Org.BouncyCastle.Crypto.Tls
  8. {
  9. public class TlsAeadCipher
  10. : TlsCipher
  11. {
  12. // TODO[draft-zauner-tls-aes-ocb-04] Apply data volume limit described in section 8.4
  13. public const int NONCE_RFC5288 = 1;
  14. /*
  15. * draft-zauner-tls-aes-ocb-04 specifies the nonce construction from draft-ietf-tls-chacha20-poly1305-04
  16. */
  17. internal const int NONCE_DRAFT_CHACHA20_POLY1305 = 2;
  18. protected readonly TlsContext context;
  19. protected readonly int macSize;
  20. // TODO SecurityParameters.record_iv_length
  21. protected readonly int record_iv_length;
  22. protected readonly IAeadBlockCipher encryptCipher;
  23. protected readonly IAeadBlockCipher decryptCipher;
  24. protected readonly byte[] encryptImplicitNonce, decryptImplicitNonce;
  25. protected readonly int nonceMode;
  26. /// <exception cref="IOException"></exception>
  27. public TlsAeadCipher(TlsContext context, IAeadBlockCipher clientWriteCipher, IAeadBlockCipher serverWriteCipher,
  28. int cipherKeySize, int macSize)
  29. : this(context, clientWriteCipher, serverWriteCipher, cipherKeySize, macSize, NONCE_RFC5288)
  30. {
  31. }
  32. /// <exception cref="IOException"></exception>
  33. internal TlsAeadCipher(TlsContext context, IAeadBlockCipher clientWriteCipher, IAeadBlockCipher serverWriteCipher,
  34. int cipherKeySize, int macSize, int nonceMode)
  35. {
  36. if (!TlsUtilities.IsTlsV12(context))
  37. throw new TlsFatalAlert(AlertDescription.internal_error);
  38. this.nonceMode = nonceMode;
  39. // TODO SecurityParameters.fixed_iv_length
  40. int fixed_iv_length;
  41. switch (nonceMode)
  42. {
  43. case NONCE_RFC5288:
  44. fixed_iv_length = 4;
  45. this.record_iv_length = 8;
  46. break;
  47. case NONCE_DRAFT_CHACHA20_POLY1305:
  48. fixed_iv_length = 12;
  49. this.record_iv_length = 0;
  50. break;
  51. default:
  52. throw new TlsFatalAlert(AlertDescription.internal_error);
  53. }
  54. this.context = context;
  55. this.macSize = macSize;
  56. int key_block_size = (2 * cipherKeySize) + (2 * fixed_iv_length);
  57. byte[] key_block = TlsUtilities.CalculateKeyBlock(context, key_block_size);
  58. int offset = 0;
  59. KeyParameter client_write_key = new KeyParameter(key_block, offset, cipherKeySize);
  60. offset += cipherKeySize;
  61. KeyParameter server_write_key = new KeyParameter(key_block, offset, cipherKeySize);
  62. offset += cipherKeySize;
  63. byte[] client_write_IV = Arrays.CopyOfRange(key_block, offset, offset + fixed_iv_length);
  64. offset += fixed_iv_length;
  65. byte[] server_write_IV = Arrays.CopyOfRange(key_block, offset, offset + fixed_iv_length);
  66. offset += fixed_iv_length;
  67. if (offset != key_block_size)
  68. throw new TlsFatalAlert(AlertDescription.internal_error);
  69. KeyParameter encryptKey, decryptKey;
  70. if (context.IsServer)
  71. {
  72. this.encryptCipher = serverWriteCipher;
  73. this.decryptCipher = clientWriteCipher;
  74. this.encryptImplicitNonce = server_write_IV;
  75. this.decryptImplicitNonce = client_write_IV;
  76. encryptKey = server_write_key;
  77. decryptKey = client_write_key;
  78. }
  79. else
  80. {
  81. this.encryptCipher = clientWriteCipher;
  82. this.decryptCipher = serverWriteCipher;
  83. this.encryptImplicitNonce = client_write_IV;
  84. this.decryptImplicitNonce = server_write_IV;
  85. encryptKey = client_write_key;
  86. decryptKey = server_write_key;
  87. }
  88. byte[] dummyNonce = new byte[fixed_iv_length + record_iv_length];
  89. this.encryptCipher.Init(true, new AeadParameters(encryptKey, 8 * macSize, dummyNonce));
  90. this.decryptCipher.Init(false, new AeadParameters(decryptKey, 8 * macSize, dummyNonce));
  91. }
  92. public virtual int GetPlaintextLimit(int ciphertextLimit)
  93. {
  94. // TODO We ought to be able to ask the decryptCipher (independently of it's current state!)
  95. return ciphertextLimit - macSize - record_iv_length;
  96. }
  97. /// <exception cref="IOException"></exception>
  98. public virtual byte[] EncodePlaintext(long seqNo, byte type, byte[] plaintext, int offset, int len)
  99. {
  100. byte[] nonce = new byte[encryptImplicitNonce.Length + record_iv_length];
  101. switch (nonceMode)
  102. {
  103. case NONCE_RFC5288:
  104. Array.Copy(encryptImplicitNonce, 0, nonce, 0, encryptImplicitNonce.Length);
  105. // RFC 5288/6655: The nonce_explicit MAY be the 64-bit sequence number.
  106. TlsUtilities.WriteUint64(seqNo, nonce, encryptImplicitNonce.Length);
  107. break;
  108. case NONCE_DRAFT_CHACHA20_POLY1305:
  109. TlsUtilities.WriteUint64(seqNo, nonce, nonce.Length - 8);
  110. for (int i = 0; i < encryptImplicitNonce.Length; ++i)
  111. {
  112. nonce[i] ^= encryptImplicitNonce[i];
  113. }
  114. break;
  115. default:
  116. throw new TlsFatalAlert(AlertDescription.internal_error);
  117. }
  118. int plaintextOffset = offset;
  119. int plaintextLength = len;
  120. int ciphertextLength = encryptCipher.GetOutputSize(plaintextLength);
  121. byte[] output = new byte[record_iv_length + ciphertextLength];
  122. if (record_iv_length != 0)
  123. {
  124. Array.Copy(nonce, nonce.Length - record_iv_length, output, 0, record_iv_length);
  125. }
  126. int outputPos = record_iv_length;
  127. byte[] additionalData = GetAdditionalData(seqNo, type, plaintextLength);
  128. AeadParameters parameters = new AeadParameters(null, 8 * macSize, nonce, additionalData);
  129. try
  130. {
  131. encryptCipher.Init(true, parameters);
  132. outputPos += encryptCipher.ProcessBytes(plaintext, plaintextOffset, plaintextLength, output, outputPos);
  133. outputPos += encryptCipher.DoFinal(output, outputPos);
  134. }
  135. catch (Exception e)
  136. {
  137. throw new TlsFatalAlert(AlertDescription.internal_error, e);
  138. }
  139. if (outputPos != output.Length)
  140. {
  141. // NOTE: Existing AEAD cipher implementations all give exact output lengths
  142. throw new TlsFatalAlert(AlertDescription.internal_error);
  143. }
  144. return output;
  145. }
  146. /// <exception cref="IOException"></exception>
  147. public virtual byte[] DecodeCiphertext(long seqNo, byte type, byte[] ciphertext, int offset, int len)
  148. {
  149. if (GetPlaintextLimit(len) < 0)
  150. throw new TlsFatalAlert(AlertDescription.decode_error);
  151. byte[] nonce = new byte[decryptImplicitNonce.Length + record_iv_length];
  152. switch (nonceMode)
  153. {
  154. case NONCE_RFC5288:
  155. Array.Copy(decryptImplicitNonce, 0, nonce, 0, decryptImplicitNonce.Length);
  156. Array.Copy(ciphertext, offset, nonce, nonce.Length - record_iv_length, record_iv_length);
  157. break;
  158. case NONCE_DRAFT_CHACHA20_POLY1305:
  159. TlsUtilities.WriteUint64(seqNo, nonce, nonce.Length - 8);
  160. for (int i = 0; i < decryptImplicitNonce.Length; ++i)
  161. {
  162. nonce[i] ^= decryptImplicitNonce[i];
  163. }
  164. break;
  165. default:
  166. throw new TlsFatalAlert(AlertDescription.internal_error);
  167. }
  168. int ciphertextOffset = offset + record_iv_length;
  169. int ciphertextLength = len - record_iv_length;
  170. int plaintextLength = decryptCipher.GetOutputSize(ciphertextLength);
  171. byte[] output = new byte[plaintextLength];
  172. int outputPos = 0;
  173. byte[] additionalData = GetAdditionalData(seqNo, type, plaintextLength);
  174. AeadParameters parameters = new AeadParameters(null, 8 * macSize, nonce, additionalData);
  175. try
  176. {
  177. decryptCipher.Init(false, parameters);
  178. outputPos += decryptCipher.ProcessBytes(ciphertext, ciphertextOffset, ciphertextLength, output, outputPos);
  179. outputPos += decryptCipher.DoFinal(output, outputPos);
  180. }
  181. catch (Exception e)
  182. {
  183. throw new TlsFatalAlert(AlertDescription.bad_record_mac, e);
  184. }
  185. if (outputPos != output.Length)
  186. {
  187. // NOTE: Existing AEAD cipher implementations all give exact output lengths
  188. throw new TlsFatalAlert(AlertDescription.internal_error);
  189. }
  190. return output;
  191. }
  192. /// <exception cref="IOException"></exception>
  193. protected virtual byte[] GetAdditionalData(long seqNo, byte type, int len)
  194. {
  195. /*
  196. * additional_data = seq_num + TLSCompressed.type + TLSCompressed.version +
  197. * TLSCompressed.length
  198. */
  199. byte[] additional_data = new byte[13];
  200. TlsUtilities.WriteUint64(seqNo, additional_data, 0);
  201. TlsUtilities.WriteUint8(type, additional_data, 8);
  202. TlsUtilities.WriteVersion(context.ServerVersion, additional_data, 9);
  203. TlsUtilities.WriteUint16(len, additional_data, 11);
  204. return additional_data;
  205. }
  206. }
  207. }
  208. #endif