ssl3_record_tls13.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "../ssl_locl.h"
  10. #include "record_locl.h"
  11. #include "internal/cryptlib.h"
  12. /*-
  13. * tls13_enc encrypts/decrypts |n_recs| in |recs|.
  14. *
  15. * Returns:
  16. * 0: (in non-constant time) if the record is publically invalid (i.e. too
  17. * short etc).
  18. * 1: if the record encryption was successful.
  19. * -1: if the record's AEAD-authenticator is invalid or, if sending,
  20. * an internal error occurred.
  21. */
  22. int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
  23. {
  24. EVP_CIPHER_CTX *ctx;
  25. unsigned char iv[EVP_MAX_IV_LENGTH];
  26. size_t ivlen, taglen, offset, loop;
  27. unsigned char *staticiv;
  28. unsigned char *seq;
  29. int lenu, lenf;
  30. SSL3_RECORD *rec = &recs[0];
  31. uint32_t alg_enc;
  32. if (n_recs != 1) {
  33. /* Should not happen */
  34. /* TODO(TLS1.3): Support pipelining */
  35. return -1;
  36. }
  37. if (sending) {
  38. ctx = s->enc_write_ctx;
  39. staticiv = s->write_iv;
  40. seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
  41. } else {
  42. ctx = s->enc_read_ctx;
  43. staticiv = s->read_iv;
  44. seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
  45. }
  46. if (ctx == NULL) {
  47. memmove(rec->data, rec->input, rec->length);
  48. rec->input = rec->data;
  49. return 1;
  50. }
  51. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  52. if (s->early_data_state == SSL_EARLY_DATA_WRITING
  53. || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
  54. alg_enc = s->session->cipher->algorithm_enc;
  55. } else {
  56. /*
  57. * To get here we must have selected a ciphersuite - otherwise ctx would
  58. * be NULL
  59. */
  60. if (!ossl_assert(s->s3->tmp.new_cipher != NULL))
  61. return -1;
  62. alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
  63. }
  64. if (alg_enc & SSL_AESCCM) {
  65. if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
  66. taglen = EVP_CCM8_TLS_TAG_LEN;
  67. else
  68. taglen = EVP_CCM_TLS_TAG_LEN;
  69. if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
  70. NULL) <= 0)
  71. return -1;
  72. } else if (alg_enc & SSL_AESGCM) {
  73. taglen = EVP_GCM_TLS_TAG_LEN;
  74. } else if (alg_enc & SSL_CHACHA20) {
  75. taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
  76. } else {
  77. return -1;
  78. }
  79. if (!sending) {
  80. /*
  81. * Take off tag. There must be at least one byte of content type as
  82. * well as the tag
  83. */
  84. if (rec->length < taglen + 1)
  85. return 0;
  86. rec->length -= taglen;
  87. }
  88. /* Set up IV */
  89. if (ivlen < SEQ_NUM_SIZE) {
  90. /* Should not happen */
  91. return -1;
  92. }
  93. offset = ivlen - SEQ_NUM_SIZE;
  94. memcpy(iv, staticiv, offset);
  95. for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
  96. iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
  97. /* Increment the sequence counter */
  98. for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
  99. ++seq[loop - 1];
  100. if (seq[loop - 1] != 0)
  101. break;
  102. }
  103. if (loop == 0) {
  104. /* Sequence has wrapped */
  105. return -1;
  106. }
  107. /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
  108. if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
  109. || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  110. taglen,
  111. rec->data + rec->length) <= 0)
  112. || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
  113. (unsigned int)rec->length) <= 0
  114. || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
  115. || (size_t)(lenu + lenf) != rec->length) {
  116. return -1;
  117. }
  118. if (sending) {
  119. /* Add the tag */
  120. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
  121. rec->data + rec->length) <= 0)
  122. return -1;
  123. rec->length += taglen;
  124. }
  125. return 1;
  126. }