s2n_prf.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include "tls/s2n_prf.h"
  16. #include <openssl/hmac.h>
  17. #include <openssl/md5.h>
  18. #include <openssl/sha.h>
  19. #include <string.h>
  20. #include <sys/param.h>
  21. #include "crypto/s2n_fips.h"
  22. #include "crypto/s2n_hash.h"
  23. #include "crypto/s2n_hmac.h"
  24. #include "crypto/s2n_openssl.h"
  25. #include "error/s2n_errno.h"
  26. #include "stuffer/s2n_stuffer.h"
  27. #include "tls/s2n_cipher_suites.h"
  28. #include "tls/s2n_connection.h"
  29. #include "tls/s2n_crypto_constants.h"
  30. #include "tls/s2n_tls.h"
  31. #include "utils/s2n_blob.h"
  32. #include "utils/s2n_mem.h"
  33. #include "utils/s2n_safety.h"
  34. S2N_RESULT s2n_key_material_init(struct s2n_key_material *key_material, struct s2n_connection *conn)
  35. {
  36. RESULT_ENSURE_REF(key_material);
  37. RESULT_ENSURE_REF(conn);
  38. RESULT_ENSURE_REF(conn->secure);
  39. RESULT_ENSURE_REF(conn->secure->cipher_suite);
  40. RESULT_ENSURE_REF(conn->secure->cipher_suite->record_alg);
  41. const struct s2n_cipher *cipher = conn->secure->cipher_suite->record_alg->cipher;
  42. RESULT_ENSURE_REF(cipher);
  43. uint8_t mac_size = 0;
  44. uint32_t key_size = 0;
  45. uint32_t iv_size = 0;
  46. /* MAC size */
  47. if (cipher->type == S2N_COMPOSITE) {
  48. mac_size = cipher->io.comp.mac_key_size;
  49. } else {
  50. RESULT_GUARD_POSIX(s2n_hmac_digest_size(conn->secure->cipher_suite->record_alg->hmac_alg, &mac_size));
  51. }
  52. /* KEY size */
  53. key_size = cipher->key_material_size;
  54. /* Only AEAD ciphers have implicit IVs for TLS >= 1.1 */
  55. if (conn->actual_protocol_version <= S2N_TLS10 || cipher->type == S2N_AEAD) {
  56. /* IV size */
  57. switch (cipher->type) {
  58. case S2N_AEAD:
  59. iv_size = cipher->io.aead.fixed_iv_size;
  60. break;
  61. case S2N_CBC:
  62. iv_size = cipher->io.cbc.block_size;
  63. break;
  64. case S2N_COMPOSITE:
  65. iv_size = cipher->io.comp.block_size;
  66. break;
  67. /* No-op for stream ciphers */
  68. default:
  69. break;
  70. }
  71. }
  72. struct s2n_stuffer key_material_stuffer = { 0 };
  73. struct s2n_blob key_material_blob = { 0 };
  74. RESULT_GUARD_POSIX(s2n_blob_init(&key_material_blob, key_material->key_block, sizeof(key_material->key_block)));
  75. RESULT_GUARD_POSIX(s2n_stuffer_init_written(&key_material_stuffer, &key_material_blob));
  76. /* initialize key_material blobs; incrementing ptr to point to the next slice of memory */
  77. uint8_t *ptr = NULL;
  78. /* MAC */
  79. ptr = s2n_stuffer_raw_read(&key_material_stuffer, mac_size);
  80. RESULT_ENSURE_REF(ptr);
  81. RESULT_GUARD_POSIX(s2n_blob_init(&key_material->client_mac, ptr, mac_size));
  82. ptr = s2n_stuffer_raw_read(&key_material_stuffer, mac_size);
  83. RESULT_ENSURE_REF(ptr);
  84. RESULT_GUARD_POSIX(s2n_blob_init(&key_material->server_mac, ptr, mac_size));
  85. /* KEY */
  86. ptr = s2n_stuffer_raw_read(&key_material_stuffer, key_size);
  87. RESULT_ENSURE_REF(ptr);
  88. RESULT_GUARD_POSIX(s2n_blob_init(&key_material->client_key, ptr, key_size));
  89. ptr = s2n_stuffer_raw_read(&key_material_stuffer, key_size);
  90. RESULT_ENSURE_REF(ptr);
  91. RESULT_GUARD_POSIX(s2n_blob_init(&key_material->server_key, ptr, key_size));
  92. /* IV */
  93. ptr = s2n_stuffer_raw_read(&key_material_stuffer, iv_size);
  94. RESULT_ENSURE_REF(ptr);
  95. RESULT_GUARD_POSIX(s2n_blob_init(&key_material->client_iv, ptr, iv_size));
  96. ptr = s2n_stuffer_raw_read(&key_material_stuffer, iv_size);
  97. RESULT_ENSURE_REF(ptr);
  98. RESULT_GUARD_POSIX(s2n_blob_init(&key_material->server_iv, ptr, iv_size));
  99. return S2N_RESULT_OK;
  100. }
  101. static int s2n_sslv3_prf(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *seed_a,
  102. struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out)
  103. {
  104. POSIX_ENSURE_REF(conn);
  105. POSIX_ENSURE_REF(conn->handshake.hashes);
  106. struct s2n_hash_state *workspace = &conn->handshake.hashes->hash_workspace;
  107. /* FIPS specifically allows MD5 for the legacy PRF */
  108. if (s2n_is_in_fips_mode() && conn->actual_protocol_version < S2N_TLS12) {
  109. POSIX_GUARD(s2n_hash_allow_md5_for_fips(workspace));
  110. }
  111. uint32_t outputlen = out->size;
  112. uint8_t *output = out->data;
  113. uint8_t iteration = 1;
  114. uint8_t md5_digest[MD5_DIGEST_LENGTH] = { 0 }, sha_digest[SHA_DIGEST_LENGTH] = { 0 };
  115. uint8_t A = 'A';
  116. while (outputlen) {
  117. struct s2n_hash_state *sha1 = workspace;
  118. POSIX_GUARD(s2n_hash_reset(sha1));
  119. POSIX_GUARD(s2n_hash_init(sha1, S2N_HASH_SHA1));
  120. for (int i = 0; i < iteration; i++) {
  121. POSIX_GUARD(s2n_hash_update(sha1, &A, 1));
  122. }
  123. POSIX_GUARD(s2n_hash_update(sha1, secret->data, secret->size));
  124. POSIX_GUARD(s2n_hash_update(sha1, seed_a->data, seed_a->size));
  125. if (seed_b) {
  126. POSIX_GUARD(s2n_hash_update(sha1, seed_b->data, seed_b->size));
  127. if (seed_c) {
  128. POSIX_GUARD(s2n_hash_update(sha1, seed_c->data, seed_c->size));
  129. }
  130. }
  131. POSIX_GUARD(s2n_hash_digest(sha1, sha_digest, sizeof(sha_digest)));
  132. struct s2n_hash_state *md5 = workspace;
  133. POSIX_GUARD(s2n_hash_reset(md5));
  134. POSIX_GUARD(s2n_hash_init(md5, S2N_HASH_MD5));
  135. POSIX_GUARD(s2n_hash_update(md5, secret->data, secret->size));
  136. POSIX_GUARD(s2n_hash_update(md5, sha_digest, sizeof(sha_digest)));
  137. POSIX_GUARD(s2n_hash_digest(md5, md5_digest, sizeof(md5_digest)));
  138. uint32_t bytes_to_copy = MIN(outputlen, sizeof(md5_digest));
  139. POSIX_CHECKED_MEMCPY(output, md5_digest, bytes_to_copy);
  140. outputlen -= bytes_to_copy;
  141. output += bytes_to_copy;
  142. /* Increment the letter */
  143. A++;
  144. iteration++;
  145. }
  146. return 0;
  147. }
  148. #if !defined(OPENSSL_IS_BORINGSSL) && !defined(OPENSSL_IS_AWSLC)
  149. static int s2n_evp_pkey_p_hash_alloc(struct s2n_prf_working_space *ws)
  150. {
  151. POSIX_ENSURE_REF(ws->p_hash.evp_hmac.evp_digest.ctx = S2N_EVP_MD_CTX_NEW());
  152. return 0;
  153. }
  154. static int s2n_evp_pkey_p_hash_digest_init(struct s2n_prf_working_space *ws)
  155. {
  156. POSIX_ENSURE_REF(ws->p_hash.evp_hmac.evp_digest.md);
  157. POSIX_ENSURE_REF(ws->p_hash.evp_hmac.evp_digest.ctx);
  158. POSIX_ENSURE_REF(ws->p_hash.evp_hmac.ctx.evp_pkey);
  159. /* Ignore the MD5 check when in FIPS mode to comply with the TLS 1.0 RFC */
  160. if (s2n_is_in_fips_mode()) {
  161. POSIX_GUARD(s2n_digest_allow_md5_for_fips(&ws->p_hash.evp_hmac.evp_digest));
  162. }
  163. POSIX_GUARD_OSSL(EVP_DigestSignInit(ws->p_hash.evp_hmac.evp_digest.ctx, NULL, ws->p_hash.evp_hmac.evp_digest.md, NULL, ws->p_hash.evp_hmac.ctx.evp_pkey),
  164. S2N_ERR_P_HASH_INIT_FAILED);
  165. return 0;
  166. }
  167. static int s2n_evp_pkey_p_hash_init(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret)
  168. {
  169. /* Initialize the message digest */
  170. POSIX_GUARD_RESULT(s2n_hmac_md_from_alg(alg, &ws->p_hash.evp_hmac.evp_digest.md));
  171. /* Initialize the mac key using the provided secret */
  172. POSIX_ENSURE_REF(ws->p_hash.evp_hmac.ctx.evp_pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret->data, secret->size));
  173. /* Initialize the message digest context with the above message digest and mac key */
  174. return s2n_evp_pkey_p_hash_digest_init(ws);
  175. }
  176. static int s2n_evp_pkey_p_hash_update(struct s2n_prf_working_space *ws, const void *data, uint32_t size)
  177. {
  178. POSIX_GUARD_OSSL(EVP_DigestSignUpdate(ws->p_hash.evp_hmac.evp_digest.ctx, data, (size_t) size), S2N_ERR_P_HASH_UPDATE_FAILED);
  179. return 0;
  180. }
  181. static int s2n_evp_pkey_p_hash_final(struct s2n_prf_working_space *ws, void *digest, uint32_t size)
  182. {
  183. /* EVP_DigestSign API's require size_t data structures */
  184. size_t digest_size = size;
  185. POSIX_GUARD_OSSL(EVP_DigestSignFinal(ws->p_hash.evp_hmac.evp_digest.ctx, (unsigned char *) digest, &digest_size), S2N_ERR_P_HASH_FINAL_FAILED);
  186. return 0;
  187. }
  188. static int s2n_evp_pkey_p_hash_wipe(struct s2n_prf_working_space *ws)
  189. {
  190. POSIX_GUARD_OSSL(S2N_EVP_MD_CTX_RESET(ws->p_hash.evp_hmac.evp_digest.ctx), S2N_ERR_P_HASH_WIPE_FAILED);
  191. return 0;
  192. }
  193. static int s2n_evp_pkey_p_hash_reset(struct s2n_prf_working_space *ws)
  194. {
  195. POSIX_GUARD(s2n_evp_pkey_p_hash_wipe(ws));
  196. /*
  197. * On some cleanup paths s2n_evp_pkey_p_hash_reset can be called before s2n_evp_pkey_p_hash_init so there is nothing
  198. * to reset.
  199. */
  200. if (ws->p_hash.evp_hmac.ctx.evp_pkey == NULL) {
  201. return S2N_SUCCESS;
  202. }
  203. return s2n_evp_pkey_p_hash_digest_init(ws);
  204. }
  205. static int s2n_evp_pkey_p_hash_cleanup(struct s2n_prf_working_space *ws)
  206. {
  207. /* Prepare the workspace md_ctx for the next p_hash */
  208. POSIX_GUARD(s2n_evp_pkey_p_hash_wipe(ws));
  209. /* Free mac key - PKEYs cannot be reused */
  210. POSIX_ENSURE_REF(ws->p_hash.evp_hmac.ctx.evp_pkey);
  211. EVP_PKEY_free(ws->p_hash.evp_hmac.ctx.evp_pkey);
  212. ws->p_hash.evp_hmac.ctx.evp_pkey = NULL;
  213. return 0;
  214. }
  215. static int s2n_evp_pkey_p_hash_free(struct s2n_prf_working_space *ws)
  216. {
  217. POSIX_ENSURE_REF(ws->p_hash.evp_hmac.evp_digest.ctx);
  218. S2N_EVP_MD_CTX_FREE(ws->p_hash.evp_hmac.evp_digest.ctx);
  219. ws->p_hash.evp_hmac.evp_digest.ctx = NULL;
  220. return 0;
  221. }
  222. static const struct s2n_p_hash_hmac s2n_evp_pkey_p_hash_hmac = {
  223. .alloc = &s2n_evp_pkey_p_hash_alloc,
  224. .init = &s2n_evp_pkey_p_hash_init,
  225. .update = &s2n_evp_pkey_p_hash_update,
  226. .final = &s2n_evp_pkey_p_hash_final,
  227. .reset = &s2n_evp_pkey_p_hash_reset,
  228. .cleanup = &s2n_evp_pkey_p_hash_cleanup,
  229. .free = &s2n_evp_pkey_p_hash_free,
  230. };
  231. #else
  232. static int s2n_evp_hmac_p_hash_alloc(struct s2n_prf_working_space *ws)
  233. {
  234. POSIX_ENSURE_REF(ws->p_hash.evp_hmac.ctx.hmac_ctx = HMAC_CTX_new());
  235. return S2N_SUCCESS;
  236. }
  237. static int s2n_evp_hmac_p_hash_init(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret)
  238. {
  239. /* Figure out the correct EVP_MD from s2n_hmac_algorithm */
  240. POSIX_GUARD_RESULT(s2n_hmac_md_from_alg(alg, &ws->p_hash.evp_hmac.evp_digest.md));
  241. /* Initialize the mac and digest */
  242. POSIX_GUARD_OSSL(HMAC_Init_ex(ws->p_hash.evp_hmac.ctx.hmac_ctx, secret->data, secret->size, ws->p_hash.evp_hmac.evp_digest.md, NULL), S2N_ERR_P_HASH_INIT_FAILED);
  243. return S2N_SUCCESS;
  244. }
  245. static int s2n_evp_hmac_p_hash_update(struct s2n_prf_working_space *ws, const void *data, uint32_t size)
  246. {
  247. POSIX_GUARD_OSSL(HMAC_Update(ws->p_hash.evp_hmac.ctx.hmac_ctx, data, (size_t) size), S2N_ERR_P_HASH_UPDATE_FAILED);
  248. return S2N_SUCCESS;
  249. }
  250. static int s2n_evp_hmac_p_hash_final(struct s2n_prf_working_space *ws, void *digest, uint32_t size)
  251. {
  252. /* HMAC_Final API's require size_t data structures */
  253. unsigned int digest_size = size;
  254. POSIX_GUARD_OSSL(HMAC_Final(ws->p_hash.evp_hmac.ctx.hmac_ctx, (unsigned char *) digest, &digest_size), S2N_ERR_P_HASH_FINAL_FAILED);
  255. return S2N_SUCCESS;
  256. }
  257. static int s2n_evp_hmac_p_hash_reset(struct s2n_prf_working_space *ws)
  258. {
  259. POSIX_ENSURE_REF(ws);
  260. if (ws->p_hash.evp_hmac.evp_digest.md == NULL) {
  261. return S2N_SUCCESS;
  262. }
  263. POSIX_GUARD_OSSL(HMAC_Init_ex(ws->p_hash.evp_hmac.ctx.hmac_ctx, NULL, 0, ws->p_hash.evp_hmac.evp_digest.md, NULL), S2N_ERR_P_HASH_INIT_FAILED);
  264. return S2N_SUCCESS;
  265. }
  266. static int s2n_evp_hmac_p_hash_cleanup(struct s2n_prf_working_space *ws)
  267. {
  268. /* Prepare the workspace md_ctx for the next p_hash */
  269. HMAC_CTX_reset(ws->p_hash.evp_hmac.ctx.hmac_ctx);
  270. return S2N_SUCCESS;
  271. }
  272. static int s2n_evp_hmac_p_hash_free(struct s2n_prf_working_space *ws)
  273. {
  274. HMAC_CTX_free(ws->p_hash.evp_hmac.ctx.hmac_ctx);
  275. return S2N_SUCCESS;
  276. }
  277. static const struct s2n_p_hash_hmac s2n_evp_hmac_p_hash_hmac = {
  278. .alloc = &s2n_evp_hmac_p_hash_alloc,
  279. .init = &s2n_evp_hmac_p_hash_init,
  280. .update = &s2n_evp_hmac_p_hash_update,
  281. .final = &s2n_evp_hmac_p_hash_final,
  282. .reset = &s2n_evp_hmac_p_hash_reset,
  283. .cleanup = &s2n_evp_hmac_p_hash_cleanup,
  284. .free = &s2n_evp_hmac_p_hash_free,
  285. };
  286. #endif /* !defined(OPENSSL_IS_BORINGSSL) && !defined(OPENSSL_IS_AWSLC) */
  287. static int s2n_hmac_p_hash_new(struct s2n_prf_working_space *ws)
  288. {
  289. POSIX_GUARD(s2n_hmac_new(&ws->p_hash.s2n_hmac));
  290. return s2n_hmac_init(&ws->p_hash.s2n_hmac, S2N_HMAC_NONE, NULL, 0);
  291. }
  292. static int s2n_hmac_p_hash_init(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret)
  293. {
  294. return s2n_hmac_init(&ws->p_hash.s2n_hmac, alg, secret->data, secret->size);
  295. }
  296. static int s2n_hmac_p_hash_update(struct s2n_prf_working_space *ws, const void *data, uint32_t size)
  297. {
  298. return s2n_hmac_update(&ws->p_hash.s2n_hmac, data, size);
  299. }
  300. static int s2n_hmac_p_hash_digest(struct s2n_prf_working_space *ws, void *digest, uint32_t size)
  301. {
  302. return s2n_hmac_digest(&ws->p_hash.s2n_hmac, digest, size);
  303. }
  304. static int s2n_hmac_p_hash_reset(struct s2n_prf_working_space *ws)
  305. {
  306. /* If we actually initialized s2n_hmac, wipe it.
  307. * A valid, initialized s2n_hmac_state will have a valid block size.
  308. */
  309. if (ws->p_hash.s2n_hmac.hash_block_size != 0) {
  310. return s2n_hmac_reset(&ws->p_hash.s2n_hmac);
  311. }
  312. return S2N_SUCCESS;
  313. }
  314. static int s2n_hmac_p_hash_cleanup(struct s2n_prf_working_space *ws)
  315. {
  316. return s2n_hmac_p_hash_reset(ws);
  317. }
  318. static int s2n_hmac_p_hash_free(struct s2n_prf_working_space *ws)
  319. {
  320. return s2n_hmac_free(&ws->p_hash.s2n_hmac);
  321. }
  322. static const struct s2n_p_hash_hmac s2n_internal_p_hash_hmac = {
  323. .alloc = &s2n_hmac_p_hash_new,
  324. .init = &s2n_hmac_p_hash_init,
  325. .update = &s2n_hmac_p_hash_update,
  326. .final = &s2n_hmac_p_hash_digest,
  327. .reset = &s2n_hmac_p_hash_reset,
  328. .cleanup = &s2n_hmac_p_hash_cleanup,
  329. .free = &s2n_hmac_p_hash_free,
  330. };
  331. const struct s2n_p_hash_hmac *s2n_get_hmac_implementation()
  332. {
  333. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  334. return s2n_is_in_fips_mode() ? &s2n_evp_hmac_p_hash_hmac : &s2n_internal_p_hash_hmac;
  335. #else
  336. return s2n_is_in_fips_mode() ? &s2n_evp_pkey_p_hash_hmac : &s2n_internal_p_hash_hmac;
  337. #endif
  338. }
  339. static int s2n_p_hash(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret, struct s2n_blob *label,
  340. struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out)
  341. {
  342. uint8_t digest_size;
  343. POSIX_GUARD(s2n_hmac_digest_size(alg, &digest_size));
  344. const struct s2n_p_hash_hmac *hmac = s2n_get_hmac_implementation();
  345. /* First compute hmac(secret + A(0)) */
  346. POSIX_GUARD(hmac->init(ws, alg, secret));
  347. POSIX_GUARD(hmac->update(ws, label->data, label->size));
  348. POSIX_GUARD(hmac->update(ws, seed_a->data, seed_a->size));
  349. if (seed_b) {
  350. POSIX_GUARD(hmac->update(ws, seed_b->data, seed_b->size));
  351. if (seed_c) {
  352. POSIX_GUARD(hmac->update(ws, seed_c->data, seed_c->size));
  353. }
  354. }
  355. POSIX_GUARD(hmac->final(ws, ws->digest0, digest_size));
  356. uint32_t outputlen = out->size;
  357. uint8_t *output = out->data;
  358. while (outputlen) {
  359. /* Now compute hmac(secret + A(N - 1) + seed) */
  360. POSIX_GUARD(hmac->reset(ws));
  361. POSIX_GUARD(hmac->update(ws, ws->digest0, digest_size));
  362. /* Add the label + seed and compute this round's A */
  363. POSIX_GUARD(hmac->update(ws, label->data, label->size));
  364. POSIX_GUARD(hmac->update(ws, seed_a->data, seed_a->size));
  365. if (seed_b) {
  366. POSIX_GUARD(hmac->update(ws, seed_b->data, seed_b->size));
  367. if (seed_c) {
  368. POSIX_GUARD(hmac->update(ws, seed_c->data, seed_c->size));
  369. }
  370. }
  371. POSIX_GUARD(hmac->final(ws, ws->digest1, digest_size));
  372. uint32_t bytes_to_xor = MIN(outputlen, digest_size);
  373. for (size_t i = 0; i < bytes_to_xor; i++) {
  374. *output ^= ws->digest1[i];
  375. output++;
  376. outputlen--;
  377. }
  378. /* Stash a digest of A(N), in A(N), for the next round */
  379. POSIX_GUARD(hmac->reset(ws));
  380. POSIX_GUARD(hmac->update(ws, ws->digest0, digest_size));
  381. POSIX_GUARD(hmac->final(ws, ws->digest0, digest_size));
  382. }
  383. POSIX_GUARD(hmac->cleanup(ws));
  384. return 0;
  385. }
  386. S2N_RESULT s2n_prf_new(struct s2n_connection *conn)
  387. {
  388. RESULT_ENSURE_REF(conn);
  389. RESULT_ENSURE_EQ(conn->prf_space, NULL);
  390. DEFER_CLEANUP(struct s2n_blob mem = { 0 }, s2n_free);
  391. RESULT_GUARD_POSIX(s2n_realloc(&mem, sizeof(struct s2n_prf_working_space)));
  392. RESULT_GUARD_POSIX(s2n_blob_zero(&mem));
  393. conn->prf_space = (struct s2n_prf_working_space *) (void *) mem.data;
  394. ZERO_TO_DISABLE_DEFER_CLEANUP(mem);
  395. /* Allocate the hmac state */
  396. const struct s2n_p_hash_hmac *hmac_impl = s2n_get_hmac_implementation();
  397. RESULT_GUARD_POSIX(hmac_impl->alloc(conn->prf_space));
  398. return S2N_RESULT_OK;
  399. }
  400. S2N_RESULT s2n_prf_wipe(struct s2n_connection *conn)
  401. {
  402. RESULT_ENSURE_REF(conn);
  403. RESULT_ENSURE_REF(conn->prf_space);
  404. const struct s2n_p_hash_hmac *hmac_impl = s2n_get_hmac_implementation();
  405. RESULT_GUARD_POSIX(hmac_impl->reset(conn->prf_space));
  406. return S2N_RESULT_OK;
  407. }
  408. S2N_RESULT s2n_prf_free(struct s2n_connection *conn)
  409. {
  410. RESULT_ENSURE_REF(conn);
  411. if (conn->prf_space == NULL) {
  412. return S2N_RESULT_OK;
  413. }
  414. const struct s2n_p_hash_hmac *hmac_impl = s2n_get_hmac_implementation();
  415. RESULT_GUARD_POSIX(hmac_impl->free(conn->prf_space));
  416. RESULT_GUARD_POSIX(s2n_free_object((uint8_t **) &conn->prf_space, sizeof(struct s2n_prf_working_space)));
  417. return S2N_RESULT_OK;
  418. }
  419. bool s2n_libcrypto_supports_tls_prf()
  420. {
  421. #if S2N_LIBCRYPTO_SUPPORTS_TLS_PRF
  422. return true;
  423. #else
  424. return false;
  425. #endif
  426. }
  427. S2N_RESULT s2n_custom_prf(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *label,
  428. struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out)
  429. {
  430. /* We zero the out blob because p_hash works by XOR'ing with the existing
  431. * buffer. This is a little convoluted but means we can avoid dynamic memory
  432. * allocation. When we call p_hash once (in the TLS1.2 case) it will produce
  433. * the right values. When we call it twice in the regular case, the two
  434. * outputs will be XORd just ass the TLS 1.0 and 1.1 RFCs require.
  435. */
  436. RESULT_GUARD_POSIX(s2n_blob_zero(out));
  437. if (conn->actual_protocol_version == S2N_TLS12) {
  438. RESULT_GUARD_POSIX(s2n_p_hash(conn->prf_space, conn->secure->cipher_suite->prf_alg, secret, label, seed_a,
  439. seed_b, seed_c, out));
  440. return S2N_RESULT_OK;
  441. }
  442. struct s2n_blob half_secret = { 0 };
  443. RESULT_GUARD_POSIX(s2n_blob_init(&half_secret, secret->data, (secret->size + 1) / 2));
  444. RESULT_GUARD_POSIX(s2n_p_hash(conn->prf_space, S2N_HMAC_MD5, &half_secret, label, seed_a, seed_b, seed_c, out));
  445. half_secret.data += secret->size - half_secret.size;
  446. RESULT_GUARD_POSIX(s2n_p_hash(conn->prf_space, S2N_HMAC_SHA1, &half_secret, label, seed_a, seed_b, seed_c, out));
  447. return S2N_RESULT_OK;
  448. }
  449. #if S2N_LIBCRYPTO_SUPPORTS_TLS_PRF
  450. /* The AWSLC TLS PRF API is exported in all AWSLC versions. However, in the AWSLC FIPS branch, this
  451. * API is defined in a private header:
  452. * https://github.com/aws/aws-lc/blob/d251b365b73a6e6acff6ee634aa8f077f23cdea4/crypto/fipsmodule/tls/internal.h#L27
  453. *
  454. * AWSLC has committed to this API definition, and the API has been added to a public header in the
  455. * main branch: https://github.com/aws/aws-lc/pull/1033. As such, this API is forward-declared in
  456. * order to make it accessible to s2n-tls when linked to AWSLC-FIPS.
  457. */
  458. int CRYPTO_tls1_prf(const EVP_MD *digest,
  459. uint8_t *out, size_t out_len,
  460. const uint8_t *secret, size_t secret_len,
  461. const char *label, size_t label_len,
  462. const uint8_t *seed1, size_t seed1_len,
  463. const uint8_t *seed2, size_t seed2_len);
  464. S2N_RESULT s2n_libcrypto_prf(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *label,
  465. struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out)
  466. {
  467. const EVP_MD *digest = NULL;
  468. if (conn->actual_protocol_version < S2N_TLS12) {
  469. /* md5_sha1 is a digest that indicates both MD5 and SHA1 should be used in the PRF calculation.
  470. * This is needed for pre-TLS12 PRFs.
  471. */
  472. digest = EVP_md5_sha1();
  473. } else {
  474. RESULT_GUARD(s2n_hmac_md_from_alg(conn->secure->cipher_suite->prf_alg, &digest));
  475. }
  476. RESULT_ENSURE_REF(digest);
  477. DEFER_CLEANUP(struct s2n_stuffer seed_b_stuffer = { 0 }, s2n_stuffer_free);
  478. size_t seed_b_len = 0;
  479. uint8_t *seed_b_data = NULL;
  480. if (seed_b != NULL) {
  481. struct s2n_blob seed_b_blob = { 0 };
  482. RESULT_GUARD_POSIX(s2n_blob_init(&seed_b_blob, seed_b->data, seed_b->size));
  483. RESULT_GUARD_POSIX(s2n_stuffer_init_written(&seed_b_stuffer, &seed_b_blob));
  484. if (seed_c != NULL) {
  485. /* The AWSLC TLS PRF implementation only provides two seed arguments. If three seeds
  486. * were provided, pass in the third seed by concatenating it with the second seed.
  487. */
  488. RESULT_GUARD_POSIX(s2n_stuffer_alloc(&seed_b_stuffer, seed_b->size + seed_c->size));
  489. RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&seed_b_stuffer, seed_b->data, seed_b->size));
  490. RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&seed_b_stuffer, seed_c->data, seed_c->size));
  491. }
  492. seed_b_len = s2n_stuffer_data_available(&seed_b_stuffer);
  493. seed_b_data = s2n_stuffer_raw_read(&seed_b_stuffer, seed_b_len);
  494. RESULT_ENSURE_REF(seed_b_data);
  495. }
  496. RESULT_GUARD_OSSL(CRYPTO_tls1_prf(digest,
  497. out->data, out->size,
  498. secret->data, secret->size,
  499. (const char *) label->data, label->size,
  500. seed_a->data, seed_a->size,
  501. seed_b_data, seed_b_len),
  502. S2N_ERR_PRF_DERIVE);
  503. return S2N_RESULT_OK;
  504. }
  505. #else
  506. S2N_RESULT s2n_libcrypto_prf(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *label,
  507. struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out)
  508. {
  509. RESULT_BAIL(S2N_ERR_UNIMPLEMENTED);
  510. }
  511. #endif /* S2N_LIBCRYPTO_SUPPORTS_TLS_PRF */
  512. int s2n_prf(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *label, struct s2n_blob *seed_a,
  513. struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out)
  514. {
  515. POSIX_ENSURE_REF(conn);
  516. POSIX_ENSURE_REF(conn->secure);
  517. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  518. POSIX_ENSURE_REF(conn->prf_space);
  519. POSIX_ENSURE_REF(secret);
  520. POSIX_ENSURE_REF(label);
  521. POSIX_ENSURE_REF(out);
  522. /* seed_a is always required, seed_b is optional, if seed_c is provided seed_b must also be provided */
  523. POSIX_ENSURE(seed_a != NULL, S2N_ERR_PRF_INVALID_SEED);
  524. POSIX_ENSURE(S2N_IMPLIES(seed_c != NULL, seed_b != NULL), S2N_ERR_PRF_INVALID_SEED);
  525. if (conn->actual_protocol_version == S2N_SSLv3) {
  526. POSIX_GUARD(s2n_sslv3_prf(conn, secret, seed_a, seed_b, seed_c, out));
  527. return S2N_SUCCESS;
  528. }
  529. /* By default, s2n-tls uses a custom PRF implementation. When operating in FIPS mode, the
  530. * FIPS-validated libcrypto implementation is used instead, if an implementation is provided.
  531. */
  532. if (s2n_is_in_fips_mode() && s2n_libcrypto_supports_tls_prf()) {
  533. POSIX_GUARD_RESULT(s2n_libcrypto_prf(conn, secret, label, seed_a, seed_b, seed_c, out));
  534. return S2N_SUCCESS;
  535. }
  536. POSIX_GUARD_RESULT(s2n_custom_prf(conn, secret, label, seed_a, seed_b, seed_c, out));
  537. return S2N_SUCCESS;
  538. }
  539. int s2n_tls_prf_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret)
  540. {
  541. POSIX_ENSURE_REF(conn);
  542. struct s2n_blob client_random = { 0 };
  543. POSIX_GUARD(s2n_blob_init(&client_random, conn->handshake_params.client_random, sizeof(conn->handshake_params.client_random)));
  544. struct s2n_blob server_random = { 0 };
  545. POSIX_GUARD(s2n_blob_init(&server_random, conn->handshake_params.server_random, sizeof(conn->handshake_params.server_random)));
  546. struct s2n_blob master_secret = { 0 };
  547. POSIX_GUARD(s2n_blob_init(&master_secret, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
  548. uint8_t master_secret_label[] = "master secret";
  549. struct s2n_blob label = { 0 };
  550. POSIX_GUARD(s2n_blob_init(&label, master_secret_label, sizeof(master_secret_label) - 1));
  551. return s2n_prf(conn, premaster_secret, &label, &client_random, &server_random, NULL, &master_secret);
  552. }
  553. int s2n_hybrid_prf_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret)
  554. {
  555. POSIX_ENSURE_REF(conn);
  556. struct s2n_blob client_random = { 0 };
  557. POSIX_GUARD(s2n_blob_init(&client_random, conn->handshake_params.client_random, sizeof(conn->handshake_params.client_random)));
  558. struct s2n_blob server_random = { 0 };
  559. POSIX_GUARD(s2n_blob_init(&server_random, conn->handshake_params.server_random, sizeof(conn->handshake_params.server_random)));
  560. struct s2n_blob master_secret = { 0 };
  561. POSIX_GUARD(s2n_blob_init(&master_secret, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
  562. uint8_t master_secret_label[] = "hybrid master secret";
  563. struct s2n_blob label = { 0 };
  564. POSIX_GUARD(s2n_blob_init(&label, master_secret_label, sizeof(master_secret_label) - 1));
  565. return s2n_prf(conn, premaster_secret, &label, &client_random, &server_random, &conn->kex_params.client_key_exchange_message, &master_secret);
  566. }
  567. int s2n_prf_calculate_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret)
  568. {
  569. POSIX_ENSURE_REF(conn);
  570. POSIX_ENSURE_REF(conn->secure);
  571. POSIX_ENSURE_EQ(s2n_conn_get_current_message_type(conn), CLIENT_KEY);
  572. if (!conn->ems_negotiated) {
  573. POSIX_GUARD(s2n_tls_prf_master_secret(conn, premaster_secret));
  574. return S2N_SUCCESS;
  575. }
  576. /* Only the client writes the Client Key Exchange message */
  577. if (conn->mode == S2N_CLIENT) {
  578. POSIX_GUARD(s2n_handshake_finish_header(&conn->handshake.io));
  579. }
  580. struct s2n_stuffer client_key_message = conn->handshake.io;
  581. POSIX_GUARD(s2n_stuffer_reread(&client_key_message));
  582. uint32_t client_key_message_size = s2n_stuffer_data_available(&client_key_message);
  583. struct s2n_blob client_key_blob = { 0 };
  584. POSIX_GUARD(s2n_blob_init(&client_key_blob, client_key_message.blob.data, client_key_message_size));
  585. uint8_t data[S2N_MAX_DIGEST_LEN] = { 0 };
  586. struct s2n_blob digest = { 0 };
  587. POSIX_GUARD(s2n_blob_init(&digest, data, sizeof(data)));
  588. if (conn->actual_protocol_version < S2N_TLS12) {
  589. uint8_t sha1_data[S2N_MAX_DIGEST_LEN] = { 0 };
  590. struct s2n_blob sha1_digest = { 0 };
  591. POSIX_GUARD(s2n_blob_init(&sha1_digest, sha1_data, sizeof(sha1_data)));
  592. POSIX_GUARD_RESULT(s2n_prf_get_digest_for_ems(conn, &client_key_blob, S2N_HASH_MD5, &digest));
  593. POSIX_GUARD_RESULT(s2n_prf_get_digest_for_ems(conn, &client_key_blob, S2N_HASH_SHA1, &sha1_digest));
  594. POSIX_GUARD_RESULT(s2n_tls_prf_extended_master_secret(conn, premaster_secret, &digest, &sha1_digest));
  595. } else {
  596. s2n_hmac_algorithm prf_alg = conn->secure->cipher_suite->prf_alg;
  597. s2n_hash_algorithm hash_alg = 0;
  598. POSIX_GUARD(s2n_hmac_hash_alg(prf_alg, &hash_alg));
  599. POSIX_GUARD_RESULT(s2n_prf_get_digest_for_ems(conn, &client_key_blob, hash_alg, &digest));
  600. POSIX_GUARD_RESULT(s2n_tls_prf_extended_master_secret(conn, premaster_secret, &digest, NULL));
  601. }
  602. return S2N_SUCCESS;
  603. }
  604. /**
  605. *= https://tools.ietf.org/rfc/rfc7627#section-4
  606. *# When the extended master secret extension is negotiated in a full
  607. *# handshake, the "master_secret" is computed as
  608. *#
  609. *# master_secret = PRF(pre_master_secret, "extended master secret",
  610. *# session_hash)
  611. *# [0..47];
  612. */
  613. S2N_RESULT s2n_tls_prf_extended_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret, struct s2n_blob *session_hash, struct s2n_blob *sha1_hash)
  614. {
  615. RESULT_ENSURE_REF(conn);
  616. struct s2n_blob extended_master_secret = { 0 };
  617. RESULT_GUARD_POSIX(s2n_blob_init(&extended_master_secret, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
  618. uint8_t extended_master_secret_label[] = "extended master secret";
  619. /* Subtract one from the label size to remove the "\0" */
  620. struct s2n_blob label = { 0 };
  621. RESULT_GUARD_POSIX(s2n_blob_init(&label, extended_master_secret_label, sizeof(extended_master_secret_label) - 1));
  622. RESULT_GUARD_POSIX(s2n_prf(conn, premaster_secret, &label, session_hash, sha1_hash, NULL, &extended_master_secret));
  623. return S2N_RESULT_OK;
  624. }
  625. S2N_RESULT s2n_prf_get_digest_for_ems(struct s2n_connection *conn, struct s2n_blob *message, s2n_hash_algorithm hash_alg, struct s2n_blob *output)
  626. {
  627. RESULT_ENSURE_REF(conn);
  628. RESULT_ENSURE_REF(conn->handshake.hashes);
  629. RESULT_ENSURE_REF(message);
  630. RESULT_ENSURE_REF(output);
  631. struct s2n_hash_state *hash_state = &conn->handshake.hashes->hash_workspace;
  632. RESULT_GUARD(s2n_handshake_copy_hash_state(conn, hash_alg, hash_state));
  633. RESULT_GUARD_POSIX(s2n_hash_update(hash_state, message->data, message->size));
  634. uint8_t digest_size = 0;
  635. RESULT_GUARD_POSIX(s2n_hash_digest_size(hash_alg, &digest_size));
  636. RESULT_ENSURE_GTE(output->size, digest_size);
  637. RESULT_GUARD_POSIX(s2n_hash_digest(hash_state, output->data, digest_size));
  638. output->size = digest_size;
  639. return S2N_RESULT_OK;
  640. }
  641. static int s2n_sslv3_finished(struct s2n_connection *conn, uint8_t prefix[4], struct s2n_hash_state *hash_workspace, uint8_t *out)
  642. {
  643. POSIX_ENSURE_REF(conn);
  644. POSIX_ENSURE_REF(conn->handshake.hashes);
  645. uint8_t xorpad1[48] = { 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  646. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36 };
  647. uint8_t xorpad2[48] = { 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  648. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c };
  649. uint8_t *md5_digest = out;
  650. uint8_t *sha_digest = out + MD5_DIGEST_LENGTH;
  651. POSIX_GUARD_RESULT(s2n_handshake_set_finished_len(conn, MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH));
  652. struct s2n_hash_state *md5 = hash_workspace;
  653. POSIX_GUARD(s2n_hash_copy(md5, &conn->handshake.hashes->md5));
  654. POSIX_GUARD(s2n_hash_update(md5, prefix, 4));
  655. POSIX_GUARD(s2n_hash_update(md5, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
  656. POSIX_GUARD(s2n_hash_update(md5, xorpad1, 48));
  657. POSIX_GUARD(s2n_hash_digest(md5, md5_digest, MD5_DIGEST_LENGTH));
  658. POSIX_GUARD(s2n_hash_reset(md5));
  659. POSIX_GUARD(s2n_hash_update(md5, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
  660. POSIX_GUARD(s2n_hash_update(md5, xorpad2, 48));
  661. POSIX_GUARD(s2n_hash_update(md5, md5_digest, MD5_DIGEST_LENGTH));
  662. POSIX_GUARD(s2n_hash_digest(md5, md5_digest, MD5_DIGEST_LENGTH));
  663. POSIX_GUARD(s2n_hash_reset(md5));
  664. struct s2n_hash_state *sha1 = hash_workspace;
  665. POSIX_GUARD(s2n_hash_copy(sha1, &conn->handshake.hashes->sha1));
  666. POSIX_GUARD(s2n_hash_update(sha1, prefix, 4));
  667. POSIX_GUARD(s2n_hash_update(sha1, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
  668. POSIX_GUARD(s2n_hash_update(sha1, xorpad1, 40));
  669. POSIX_GUARD(s2n_hash_digest(sha1, sha_digest, SHA_DIGEST_LENGTH));
  670. POSIX_GUARD(s2n_hash_reset(sha1));
  671. POSIX_GUARD(s2n_hash_update(sha1, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
  672. POSIX_GUARD(s2n_hash_update(sha1, xorpad2, 40));
  673. POSIX_GUARD(s2n_hash_update(sha1, sha_digest, SHA_DIGEST_LENGTH));
  674. POSIX_GUARD(s2n_hash_digest(sha1, sha_digest, SHA_DIGEST_LENGTH));
  675. POSIX_GUARD(s2n_hash_reset(sha1));
  676. return 0;
  677. }
  678. static int s2n_sslv3_client_finished(struct s2n_connection *conn)
  679. {
  680. POSIX_ENSURE_REF(conn);
  681. POSIX_ENSURE_REF(conn->handshake.hashes);
  682. uint8_t prefix[4] = { 0x43, 0x4c, 0x4e, 0x54 };
  683. return s2n_sslv3_finished(conn, prefix, &conn->handshake.hashes->hash_workspace, conn->handshake.client_finished);
  684. }
  685. static int s2n_sslv3_server_finished(struct s2n_connection *conn)
  686. {
  687. POSIX_ENSURE_REF(conn);
  688. POSIX_ENSURE_REF(conn->handshake.hashes);
  689. uint8_t prefix[4] = { 0x53, 0x52, 0x56, 0x52 };
  690. return s2n_sslv3_finished(conn, prefix, &conn->handshake.hashes->hash_workspace, conn->handshake.server_finished);
  691. }
  692. int s2n_prf_client_finished(struct s2n_connection *conn)
  693. {
  694. POSIX_ENSURE_REF(conn);
  695. POSIX_ENSURE_REF(conn->secure);
  696. POSIX_ENSURE_REF(conn->handshake.hashes);
  697. struct s2n_blob master_secret, md5, sha;
  698. uint8_t md5_digest[MD5_DIGEST_LENGTH];
  699. uint8_t sha_digest[SHA384_DIGEST_LENGTH];
  700. uint8_t client_finished_label[] = "client finished";
  701. struct s2n_blob client_finished = { 0 };
  702. struct s2n_blob label = { 0 };
  703. if (conn->actual_protocol_version == S2N_SSLv3) {
  704. return s2n_sslv3_client_finished(conn);
  705. }
  706. client_finished.data = conn->handshake.client_finished;
  707. client_finished.size = S2N_TLS_FINISHED_LEN;
  708. POSIX_GUARD_RESULT(s2n_handshake_set_finished_len(conn, client_finished.size));
  709. label.data = client_finished_label;
  710. label.size = sizeof(client_finished_label) - 1;
  711. master_secret.data = conn->secrets.version.tls12.master_secret;
  712. master_secret.size = sizeof(conn->secrets.version.tls12.master_secret);
  713. if (conn->actual_protocol_version == S2N_TLS12) {
  714. switch (conn->secure->cipher_suite->prf_alg) {
  715. case S2N_HMAC_SHA256:
  716. POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha256));
  717. POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA256_DIGEST_LENGTH));
  718. sha.size = SHA256_DIGEST_LENGTH;
  719. break;
  720. case S2N_HMAC_SHA384:
  721. POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha384));
  722. POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA384_DIGEST_LENGTH));
  723. sha.size = SHA384_DIGEST_LENGTH;
  724. break;
  725. default:
  726. POSIX_BAIL(S2N_ERR_PRF_INVALID_ALGORITHM);
  727. }
  728. sha.data = sha_digest;
  729. return s2n_prf(conn, &master_secret, &label, &sha, NULL, NULL, &client_finished);
  730. }
  731. POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->md5));
  732. POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, md5_digest, MD5_DIGEST_LENGTH));
  733. md5.data = md5_digest;
  734. md5.size = MD5_DIGEST_LENGTH;
  735. POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha1));
  736. POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA_DIGEST_LENGTH));
  737. sha.data = sha_digest;
  738. sha.size = SHA_DIGEST_LENGTH;
  739. return s2n_prf(conn, &master_secret, &label, &md5, &sha, NULL, &client_finished);
  740. }
  741. int s2n_prf_server_finished(struct s2n_connection *conn)
  742. {
  743. POSIX_ENSURE_REF(conn);
  744. POSIX_ENSURE_REF(conn->secure);
  745. POSIX_ENSURE_REF(conn->handshake.hashes);
  746. struct s2n_blob master_secret, md5, sha;
  747. uint8_t md5_digest[MD5_DIGEST_LENGTH];
  748. uint8_t sha_digest[SHA384_DIGEST_LENGTH];
  749. uint8_t server_finished_label[] = "server finished";
  750. struct s2n_blob server_finished = { 0 };
  751. struct s2n_blob label = { 0 };
  752. if (conn->actual_protocol_version == S2N_SSLv3) {
  753. return s2n_sslv3_server_finished(conn);
  754. }
  755. server_finished.data = conn->handshake.server_finished;
  756. server_finished.size = S2N_TLS_FINISHED_LEN;
  757. POSIX_GUARD_RESULT(s2n_handshake_set_finished_len(conn, server_finished.size));
  758. label.data = server_finished_label;
  759. label.size = sizeof(server_finished_label) - 1;
  760. master_secret.data = conn->secrets.version.tls12.master_secret;
  761. master_secret.size = sizeof(conn->secrets.version.tls12.master_secret);
  762. if (conn->actual_protocol_version == S2N_TLS12) {
  763. switch (conn->secure->cipher_suite->prf_alg) {
  764. case S2N_HMAC_SHA256:
  765. POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha256));
  766. POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA256_DIGEST_LENGTH));
  767. sha.size = SHA256_DIGEST_LENGTH;
  768. break;
  769. case S2N_HMAC_SHA384:
  770. POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha384));
  771. POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA384_DIGEST_LENGTH));
  772. sha.size = SHA384_DIGEST_LENGTH;
  773. break;
  774. default:
  775. POSIX_BAIL(S2N_ERR_PRF_INVALID_ALGORITHM);
  776. }
  777. sha.data = sha_digest;
  778. return s2n_prf(conn, &master_secret, &label, &sha, NULL, NULL, &server_finished);
  779. }
  780. POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->md5));
  781. POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, md5_digest, MD5_DIGEST_LENGTH));
  782. md5.data = md5_digest;
  783. md5.size = MD5_DIGEST_LENGTH;
  784. POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha1));
  785. POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA_DIGEST_LENGTH));
  786. sha.data = sha_digest;
  787. sha.size = SHA_DIGEST_LENGTH;
  788. return s2n_prf(conn, &master_secret, &label, &md5, &sha, NULL, &server_finished);
  789. }
  790. static int s2n_prf_make_client_key(struct s2n_connection *conn, struct s2n_key_material *key_material)
  791. {
  792. POSIX_ENSURE_REF(conn);
  793. POSIX_ENSURE_REF(conn->secure);
  794. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  795. POSIX_ENSURE_REF(conn->secure->cipher_suite->record_alg);
  796. const struct s2n_cipher *cipher = conn->secure->cipher_suite->record_alg->cipher;
  797. POSIX_ENSURE_REF(cipher);
  798. POSIX_ENSURE_REF(cipher->set_encryption_key);
  799. POSIX_ENSURE_REF(cipher->set_decryption_key);
  800. if (conn->mode == S2N_CLIENT) {
  801. POSIX_GUARD(cipher->set_encryption_key(&conn->secure->client_key, &key_material->client_key));
  802. } else {
  803. POSIX_GUARD(cipher->set_decryption_key(&conn->secure->client_key, &key_material->client_key));
  804. }
  805. return 0;
  806. }
  807. static int s2n_prf_make_server_key(struct s2n_connection *conn, struct s2n_key_material *key_material)
  808. {
  809. POSIX_ENSURE_REF(conn);
  810. POSIX_ENSURE_REF(conn->secure);
  811. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  812. POSIX_ENSURE_REF(conn->secure->cipher_suite->record_alg);
  813. const struct s2n_cipher *cipher = conn->secure->cipher_suite->record_alg->cipher;
  814. POSIX_ENSURE_REF(cipher);
  815. POSIX_ENSURE_REF(cipher->set_encryption_key);
  816. POSIX_ENSURE_REF(cipher->set_decryption_key);
  817. if (conn->mode == S2N_SERVER) {
  818. POSIX_GUARD(cipher->set_encryption_key(&conn->secure->server_key, &key_material->server_key));
  819. } else {
  820. POSIX_GUARD(cipher->set_decryption_key(&conn->secure->server_key, &key_material->server_key));
  821. }
  822. return 0;
  823. }
  824. S2N_RESULT s2n_prf_generate_key_material(struct s2n_connection *conn, struct s2n_key_material *key_material)
  825. {
  826. RESULT_ENSURE_REF(conn);
  827. RESULT_ENSURE_REF(key_material);
  828. struct s2n_blob client_random = { 0 };
  829. RESULT_GUARD_POSIX(s2n_blob_init(&client_random, conn->handshake_params.client_random, sizeof(conn->handshake_params.client_random)));
  830. struct s2n_blob server_random = { 0 };
  831. RESULT_GUARD_POSIX(s2n_blob_init(&server_random, conn->handshake_params.server_random, sizeof(conn->handshake_params.server_random)));
  832. struct s2n_blob master_secret = { 0 };
  833. RESULT_GUARD_POSIX(s2n_blob_init(&master_secret, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
  834. struct s2n_blob label = { 0 };
  835. uint8_t key_expansion_label[] = "key expansion";
  836. RESULT_GUARD_POSIX(s2n_blob_init(&label, key_expansion_label, sizeof(key_expansion_label) - 1));
  837. RESULT_GUARD(s2n_key_material_init(key_material, conn));
  838. struct s2n_blob prf_out = { 0 };
  839. RESULT_GUARD_POSIX(s2n_blob_init(&prf_out, key_material->key_block, sizeof(key_material->key_block)));
  840. RESULT_GUARD_POSIX(s2n_prf(conn, &master_secret, &label, &server_random, &client_random, NULL, &prf_out));
  841. return S2N_RESULT_OK;
  842. }
  843. int s2n_prf_key_expansion(struct s2n_connection *conn)
  844. {
  845. POSIX_ENSURE_REF(conn);
  846. POSIX_ENSURE_REF(conn->secure);
  847. struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite;
  848. POSIX_ENSURE_REF(cipher_suite);
  849. POSIX_ENSURE_REF(cipher_suite->record_alg);
  850. const struct s2n_cipher *cipher = cipher_suite->record_alg->cipher;
  851. POSIX_ENSURE_REF(cipher);
  852. struct s2n_key_material key_material = { 0 };
  853. POSIX_GUARD_RESULT(s2n_prf_generate_key_material(conn, &key_material));
  854. POSIX_ENSURE(cipher_suite->available, S2N_ERR_PRF_INVALID_ALGORITHM);
  855. POSIX_GUARD(cipher->init(&conn->secure->client_key));
  856. POSIX_GUARD(cipher->init(&conn->secure->server_key));
  857. /* Seed the client MAC */
  858. POSIX_GUARD(s2n_hmac_reset(&conn->secure->client_record_mac));
  859. POSIX_GUARD(s2n_hmac_init(
  860. &conn->secure->client_record_mac,
  861. cipher_suite->record_alg->hmac_alg,
  862. key_material.client_mac.data,
  863. key_material.client_mac.size));
  864. /* Seed the server MAC */
  865. POSIX_GUARD(s2n_hmac_reset(&conn->secure->server_record_mac));
  866. POSIX_GUARD(s2n_hmac_init(
  867. &conn->secure->server_record_mac,
  868. conn->secure->cipher_suite->record_alg->hmac_alg,
  869. key_material.server_mac.data,
  870. key_material.server_mac.size));
  871. /* Make the client key */
  872. POSIX_GUARD(s2n_prf_make_client_key(conn, &key_material));
  873. /* Make the server key */
  874. POSIX_GUARD(s2n_prf_make_server_key(conn, &key_material));
  875. /* Composite CBC does MAC inside the cipher, pass it the MAC key.
  876. * Must happen after setting encryption/decryption keys.
  877. */
  878. if (cipher->type == S2N_COMPOSITE) {
  879. POSIX_GUARD(cipher->io.comp.set_mac_write_key(&conn->secure->client_key, key_material.client_mac.data, key_material.client_mac.size));
  880. POSIX_GUARD(cipher->io.comp.set_mac_write_key(&conn->secure->server_key, key_material.server_mac.data, key_material.server_mac.size));
  881. }
  882. /* set IV */
  883. POSIX_ENSURE_EQ(key_material.client_iv.size, key_material.server_iv.size);
  884. POSIX_ENSURE_LTE(key_material.client_iv.size, S2N_TLS_MAX_IV_LEN);
  885. POSIX_CHECKED_MEMCPY(conn->secure->client_implicit_iv, key_material.client_iv.data, key_material.client_iv.size);
  886. POSIX_CHECKED_MEMCPY(conn->secure->server_implicit_iv, key_material.server_iv.data, key_material.server_iv.size);
  887. return 0;
  888. }