You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

317 lines
10 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/ssl.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <openssl/aead.h>
  18. #include <openssl/err.h>
  19. #include <openssl/rand.h>
  20. #include <openssl/type_check.h>
  21. #include "internal.h"
  22. OPENSSL_COMPILE_ASSERT(EVP_AEAD_MAX_NONCE_LENGTH < 256,
  23. variable_nonce_len_doesnt_fit_in_uint8_t);
  24. SSL_AEAD_CTX *SSL_AEAD_CTX_new(enum evp_aead_direction_t direction,
  25. uint16_t version, const SSL_CIPHER *cipher,
  26. const uint8_t *enc_key, size_t enc_key_len,
  27. const uint8_t *mac_key, size_t mac_key_len,
  28. const uint8_t *fixed_iv, size_t fixed_iv_len) {
  29. const EVP_AEAD *aead;
  30. size_t discard;
  31. if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, cipher, version)) {
  32. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  33. return 0;
  34. }
  35. uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
  36. if (mac_key_len > 0) {
  37. /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
  38. * suites). */
  39. if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) {
  40. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  41. return 0;
  42. }
  43. memcpy(merged_key, mac_key, mac_key_len);
  44. memcpy(merged_key + mac_key_len, enc_key, enc_key_len);
  45. memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv, fixed_iv_len);
  46. enc_key = merged_key;
  47. enc_key_len += mac_key_len;
  48. enc_key_len += fixed_iv_len;
  49. }
  50. SSL_AEAD_CTX *aead_ctx = OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
  51. if (aead_ctx == NULL) {
  52. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  53. return NULL;
  54. }
  55. memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX));
  56. aead_ctx->cipher = cipher;
  57. if (!EVP_AEAD_CTX_init_with_direction(
  58. &aead_ctx->ctx, aead, enc_key, enc_key_len,
  59. EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
  60. OPENSSL_free(aead_ctx);
  61. return NULL;
  62. }
  63. assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
  64. aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead);
  65. if (mac_key_len == 0) {
  66. assert(fixed_iv_len <= sizeof(aead_ctx->fixed_nonce));
  67. memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len);
  68. aead_ctx->fixed_nonce_len = fixed_iv_len;
  69. if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
  70. /* The fixed nonce into the actual nonce (the sequence number). */
  71. aead_ctx->xor_fixed_nonce = 1;
  72. aead_ctx->variable_nonce_len = 8;
  73. } else {
  74. /* The fixed IV is prepended to the nonce. */
  75. assert(fixed_iv_len <= aead_ctx->variable_nonce_len);
  76. aead_ctx->variable_nonce_len -= fixed_iv_len;
  77. }
  78. /* AES-GCM uses an explicit nonce. */
  79. if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
  80. aead_ctx->variable_nonce_included_in_record = 1;
  81. }
  82. } else {
  83. aead_ctx->variable_nonce_included_in_record = 1;
  84. aead_ctx->random_variable_nonce = 1;
  85. aead_ctx->omit_length_in_ad = 1;
  86. aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
  87. }
  88. return aead_ctx;
  89. }
  90. void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
  91. if (aead == NULL) {
  92. return;
  93. }
  94. EVP_AEAD_CTX_cleanup(&aead->ctx);
  95. OPENSSL_free(aead);
  96. }
  97. size_t SSL_AEAD_CTX_explicit_nonce_len(SSL_AEAD_CTX *aead) {
  98. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  99. aead = NULL;
  100. #endif
  101. if (aead != NULL && aead->variable_nonce_included_in_record) {
  102. return aead->variable_nonce_len;
  103. }
  104. return 0;
  105. }
  106. size_t SSL_AEAD_CTX_max_overhead(SSL_AEAD_CTX *aead) {
  107. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  108. aead = NULL;
  109. #endif
  110. if (aead == NULL) {
  111. return 0;
  112. }
  113. return EVP_AEAD_max_overhead(aead->ctx.aead) +
  114. SSL_AEAD_CTX_explicit_nonce_len(aead);
  115. }
  116. /* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
  117. * returns the number of bytes written. */
  118. static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
  119. uint8_t type, uint16_t wire_version,
  120. const uint8_t seqnum[8],
  121. size_t plaintext_len) {
  122. memcpy(out, seqnum, 8);
  123. size_t len = 8;
  124. out[len++] = type;
  125. if (!aead->omit_version_in_ad) {
  126. out[len++] = (uint8_t)(wire_version >> 8);
  127. out[len++] = (uint8_t)wire_version;
  128. }
  129. if (!aead->omit_length_in_ad) {
  130. out[len++] = (uint8_t)(plaintext_len >> 8);
  131. out[len++] = (uint8_t)plaintext_len;
  132. }
  133. return len;
  134. }
  135. int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
  136. size_t max_out, uint8_t type, uint16_t wire_version,
  137. const uint8_t seqnum[8], const uint8_t *in,
  138. size_t in_len) {
  139. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  140. aead = NULL;
  141. #endif
  142. if (aead == NULL) {
  143. /* Handle the initial NULL cipher. */
  144. if (in_len > max_out) {
  145. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  146. return 0;
  147. }
  148. memmove(out, in, in_len);
  149. *out_len = in_len;
  150. return 1;
  151. }
  152. /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
  153. * overhead. Otherwise the parameter is unused. */
  154. size_t plaintext_len = 0;
  155. if (!aead->omit_length_in_ad) {
  156. size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
  157. if (in_len < overhead) {
  158. /* Publicly invalid. */
  159. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  160. return 0;
  161. }
  162. plaintext_len = in_len - overhead;
  163. }
  164. uint8_t ad[13];
  165. size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
  166. plaintext_len);
  167. /* Assemble the nonce. */
  168. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  169. size_t nonce_len = 0;
  170. /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
  171. if (aead->xor_fixed_nonce) {
  172. nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
  173. memset(nonce, 0, nonce_len);
  174. } else {
  175. memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
  176. nonce_len += aead->fixed_nonce_len;
  177. }
  178. /* Add the variable nonce. */
  179. if (aead->variable_nonce_included_in_record) {
  180. if (in_len < aead->variable_nonce_len) {
  181. /* Publicly invalid. */
  182. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  183. return 0;
  184. }
  185. memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
  186. in += aead->variable_nonce_len;
  187. in_len -= aead->variable_nonce_len;
  188. } else {
  189. assert(aead->variable_nonce_len == 8);
  190. memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
  191. }
  192. nonce_len += aead->variable_nonce_len;
  193. /* XOR the fixed nonce, if necessary. */
  194. if (aead->xor_fixed_nonce) {
  195. assert(nonce_len == aead->fixed_nonce_len);
  196. size_t i;
  197. for (i = 0; i < aead->fixed_nonce_len; i++) {
  198. nonce[i] ^= aead->fixed_nonce[i];
  199. }
  200. }
  201. return EVP_AEAD_CTX_open(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
  202. in, in_len, ad, ad_len);
  203. }
  204. int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
  205. size_t max_out, uint8_t type, uint16_t wire_version,
  206. const uint8_t seqnum[8], const uint8_t *in,
  207. size_t in_len) {
  208. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  209. aead = NULL;
  210. #endif
  211. if (aead == NULL) {
  212. /* Handle the initial NULL cipher. */
  213. if (in_len > max_out) {
  214. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  215. return 0;
  216. }
  217. memmove(out, in, in_len);
  218. *out_len = in_len;
  219. return 1;
  220. }
  221. uint8_t ad[13];
  222. size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
  223. in_len);
  224. /* Assemble the nonce. */
  225. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  226. size_t nonce_len = 0;
  227. /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
  228. if (aead->xor_fixed_nonce) {
  229. nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
  230. memset(nonce, 0, nonce_len);
  231. } else {
  232. memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
  233. nonce_len += aead->fixed_nonce_len;
  234. }
  235. /* Select the variable nonce. */
  236. if (aead->random_variable_nonce) {
  237. assert(aead->variable_nonce_included_in_record);
  238. if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
  239. return 0;
  240. }
  241. } else {
  242. /* When sending we use the sequence number as the variable part of the
  243. * nonce. */
  244. assert(aead->variable_nonce_len == 8);
  245. memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
  246. }
  247. nonce_len += aead->variable_nonce_len;
  248. /* Emit the variable nonce if included in the record. */
  249. size_t extra_len = 0;
  250. if (aead->variable_nonce_included_in_record) {
  251. assert(!aead->xor_fixed_nonce);
  252. if (max_out < aead->variable_nonce_len) {
  253. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  254. return 0;
  255. }
  256. if (out < in + in_len && in < out + aead->variable_nonce_len) {
  257. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  258. return 0;
  259. }
  260. memcpy(out, nonce + aead->fixed_nonce_len, aead->variable_nonce_len);
  261. extra_len = aead->variable_nonce_len;
  262. out += aead->variable_nonce_len;
  263. max_out -= aead->variable_nonce_len;
  264. }
  265. /* XOR the fixed nonce, if necessary. */
  266. if (aead->xor_fixed_nonce) {
  267. assert(nonce_len == aead->fixed_nonce_len);
  268. size_t i;
  269. for (i = 0; i < aead->fixed_nonce_len; i++) {
  270. nonce[i] ^= aead->fixed_nonce[i];
  271. }
  272. }
  273. if (!EVP_AEAD_CTX_seal(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
  274. in, in_len, ad, ad_len)) {
  275. return 0;
  276. }
  277. *out_len += extra_len;
  278. return 1;
  279. }