dh_kdf.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2013-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 "e_os.h"
  10. #ifndef OPENSSL_NO_CMS
  11. #include <string.h>
  12. #include <openssl/dh.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/asn1.h>
  15. #include <openssl/cms.h>
  16. /* Key derivation from X9.42/RFC2631 */
  17. /* Uses CMS functions, hence the #ifdef wrapper. */
  18. #define DH_KDF_MAX (1L << 30)
  19. /* Skip past an ASN1 structure: for OBJECT skip content octets too */
  20. static int skip_asn1(unsigned char **pp, long *plen, int exptag)
  21. {
  22. const unsigned char *q = *pp;
  23. int i, tag, xclass;
  24. long tmplen;
  25. i = ASN1_get_object(&q, &tmplen, &tag, &xclass, *plen);
  26. if (i & 0x80)
  27. return 0;
  28. if (tag != exptag || xclass != V_ASN1_UNIVERSAL)
  29. return 0;
  30. if (tag == V_ASN1_OBJECT)
  31. q += tmplen;
  32. *plen -= q - *pp;
  33. *pp = (unsigned char *)q;
  34. return 1;
  35. }
  36. /*
  37. * Encode the DH shared info structure, return an offset to the counter value
  38. * so we can update the structure without reencoding it.
  39. */
  40. static int dh_sharedinfo_encode(unsigned char **pder, unsigned char **pctr,
  41. ASN1_OBJECT *key_oid, size_t outlen,
  42. const unsigned char *ukm, size_t ukmlen)
  43. {
  44. unsigned char *p;
  45. int derlen;
  46. long tlen;
  47. /* "magic" value to check offset is sane */
  48. static unsigned char ctr[4] = { 0xF3, 0x17, 0x22, 0x53 };
  49. X509_ALGOR atmp;
  50. ASN1_OCTET_STRING ctr_oct, ukm_oct, *pukm_oct;
  51. ASN1_TYPE ctr_atype;
  52. if (ukmlen > DH_KDF_MAX || outlen > DH_KDF_MAX)
  53. return 0;
  54. ctr_oct.data = ctr;
  55. ctr_oct.length = 4;
  56. ctr_oct.flags = 0;
  57. ctr_oct.type = V_ASN1_OCTET_STRING;
  58. ctr_atype.type = V_ASN1_OCTET_STRING;
  59. ctr_atype.value.octet_string = &ctr_oct;
  60. atmp.algorithm = key_oid;
  61. atmp.parameter = &ctr_atype;
  62. if (ukm) {
  63. ukm_oct.type = V_ASN1_OCTET_STRING;
  64. ukm_oct.flags = 0;
  65. ukm_oct.data = (unsigned char *)ukm;
  66. ukm_oct.length = ukmlen;
  67. pukm_oct = &ukm_oct;
  68. } else
  69. pukm_oct = NULL;
  70. derlen = CMS_SharedInfo_encode(pder, &atmp, pukm_oct, outlen);
  71. if (derlen <= 0)
  72. return 0;
  73. p = *pder;
  74. tlen = derlen;
  75. if (!skip_asn1(&p, &tlen, V_ASN1_SEQUENCE))
  76. return 0;
  77. if (!skip_asn1(&p, &tlen, V_ASN1_SEQUENCE))
  78. return 0;
  79. if (!skip_asn1(&p, &tlen, V_ASN1_OBJECT))
  80. return 0;
  81. if (!skip_asn1(&p, &tlen, V_ASN1_OCTET_STRING))
  82. return 0;
  83. if (CRYPTO_memcmp(p, ctr, 4))
  84. return 0;
  85. *pctr = p;
  86. return derlen;
  87. }
  88. int DH_KDF_X9_42(unsigned char *out, size_t outlen,
  89. const unsigned char *Z, size_t Zlen,
  90. ASN1_OBJECT *key_oid,
  91. const unsigned char *ukm, size_t ukmlen, const EVP_MD *md)
  92. {
  93. EVP_MD_CTX *mctx = NULL;
  94. int rv = 0;
  95. unsigned int i;
  96. size_t mdlen;
  97. unsigned char *der = NULL, *ctr;
  98. int derlen;
  99. if (Zlen > DH_KDF_MAX)
  100. return 0;
  101. mctx = EVP_MD_CTX_new();
  102. if (mctx == NULL)
  103. return 0;
  104. mdlen = EVP_MD_size(md);
  105. derlen = dh_sharedinfo_encode(&der, &ctr, key_oid, outlen, ukm, ukmlen);
  106. if (derlen == 0)
  107. goto err;
  108. for (i = 1;; i++) {
  109. unsigned char mtmp[EVP_MAX_MD_SIZE];
  110. if (!EVP_DigestInit_ex(mctx, md, NULL)
  111. || !EVP_DigestUpdate(mctx, Z, Zlen))
  112. goto err;
  113. ctr[3] = i & 0xFF;
  114. ctr[2] = (i >> 8) & 0xFF;
  115. ctr[1] = (i >> 16) & 0xFF;
  116. ctr[0] = (i >> 24) & 0xFF;
  117. if (!EVP_DigestUpdate(mctx, der, derlen))
  118. goto err;
  119. if (outlen >= mdlen) {
  120. if (!EVP_DigestFinal(mctx, out, NULL))
  121. goto err;
  122. outlen -= mdlen;
  123. if (outlen == 0)
  124. break;
  125. out += mdlen;
  126. } else {
  127. if (!EVP_DigestFinal(mctx, mtmp, NULL))
  128. goto err;
  129. memcpy(out, mtmp, outlen);
  130. OPENSSL_cleanse(mtmp, mdlen);
  131. break;
  132. }
  133. }
  134. rv = 1;
  135. err:
  136. OPENSSL_free(der);
  137. EVP_MD_CTX_free(mctx);
  138. return rv;
  139. }
  140. #endif