sha1-tlsprf.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * TLS PRF (SHA1 + MD5)
  3. * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "sha1.h"
  17. #include "md5.h"
  18. #include "crypto.h"
  19. /**
  20. * tls_prf - Pseudo-Random Function for TLS (TLS-PRF, RFC 2246)
  21. * @secret: Key for PRF
  22. * @secret_len: Length of the key in bytes
  23. * @label: A unique label for each purpose of the PRF
  24. * @seed: Seed value to bind into the key
  25. * @seed_len: Length of the seed
  26. * @out: Buffer for the generated pseudo-random key
  27. * @outlen: Number of bytes of key to generate
  28. * Returns: 0 on success, -1 on failure.
  29. *
  30. * This function is used to derive new, cryptographically separate keys from a
  31. * given key in TLS. This PRF is defined in RFC 2246, Chapter 5.
  32. */
  33. int tls_prf(const u8 *secret, size_t secret_len, const char *label,
  34. const u8 *seed, size_t seed_len, u8 *out, size_t outlen)
  35. {
  36. size_t L_S1, L_S2, i;
  37. const u8 *S1, *S2;
  38. u8 A_MD5[MD5_MAC_LEN], A_SHA1[SHA1_MAC_LEN];
  39. u8 P_MD5[MD5_MAC_LEN], P_SHA1[SHA1_MAC_LEN];
  40. int MD5_pos, SHA1_pos;
  41. const u8 *MD5_addr[3];
  42. size_t MD5_len[3];
  43. const unsigned char *SHA1_addr[3];
  44. size_t SHA1_len[3];
  45. if (secret_len & 1)
  46. return -1;
  47. MD5_addr[0] = A_MD5;
  48. MD5_len[0] = MD5_MAC_LEN;
  49. MD5_addr[1] = (unsigned char *) label;
  50. MD5_len[1] = os_strlen(label);
  51. MD5_addr[2] = seed;
  52. MD5_len[2] = seed_len;
  53. SHA1_addr[0] = A_SHA1;
  54. SHA1_len[0] = SHA1_MAC_LEN;
  55. SHA1_addr[1] = (unsigned char *) label;
  56. SHA1_len[1] = os_strlen(label);
  57. SHA1_addr[2] = seed;
  58. SHA1_len[2] = seed_len;
  59. /* RFC 2246, Chapter 5
  60. * A(0) = seed, A(i) = HMAC(secret, A(i-1))
  61. * P_hash = HMAC(secret, A(1) + seed) + HMAC(secret, A(2) + seed) + ..
  62. * PRF = P_MD5(S1, label + seed) XOR P_SHA-1(S2, label + seed)
  63. */
  64. L_S1 = L_S2 = (secret_len + 1) / 2;
  65. S1 = secret;
  66. S2 = secret + L_S1;
  67. if (secret_len & 1) {
  68. /* The last byte of S1 will be shared with S2 */
  69. S2--;
  70. }
  71. hmac_md5_vector_non_fips_allow(S1, L_S1, 2, &MD5_addr[1], &MD5_len[1],
  72. A_MD5);
  73. hmac_sha1_vector(S2, L_S2, 2, &SHA1_addr[1], &SHA1_len[1], A_SHA1);
  74. MD5_pos = MD5_MAC_LEN;
  75. SHA1_pos = SHA1_MAC_LEN;
  76. for (i = 0; i < outlen; i++) {
  77. if (MD5_pos == MD5_MAC_LEN) {
  78. hmac_md5_vector_non_fips_allow(S1, L_S1, 3, MD5_addr,
  79. MD5_len, P_MD5);
  80. MD5_pos = 0;
  81. hmac_md5_non_fips_allow(S1, L_S1, A_MD5, MD5_MAC_LEN,
  82. A_MD5);
  83. }
  84. if (SHA1_pos == SHA1_MAC_LEN) {
  85. hmac_sha1_vector(S2, L_S2, 3, SHA1_addr, SHA1_len,
  86. P_SHA1);
  87. SHA1_pos = 0;
  88. hmac_sha1(S2, L_S2, A_SHA1, SHA1_MAC_LEN, A_SHA1);
  89. }
  90. out[i] = P_MD5[MD5_pos] ^ P_SHA1[SHA1_pos];
  91. MD5_pos++;
  92. SHA1_pos++;
  93. }
  94. return 0;
  95. }