m_md5_sha1.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2015-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. #if !defined(OPENSSL_NO_MD5)
  10. # include <openssl/evp.h>
  11. # include <openssl/objects.h>
  12. # include <openssl/x509.h>
  13. # include <openssl/md5.h>
  14. # include <openssl/sha.h>
  15. # include "internal/cryptlib.h"
  16. # include "crypto/evp.h"
  17. # include <openssl/rsa.h>
  18. struct md5_sha1_ctx {
  19. MD5_CTX md5;
  20. SHA_CTX sha1;
  21. };
  22. static int init(EVP_MD_CTX *ctx)
  23. {
  24. struct md5_sha1_ctx *mctx = EVP_MD_CTX_md_data(ctx);
  25. if (!MD5_Init(&mctx->md5))
  26. return 0;
  27. return SHA1_Init(&mctx->sha1);
  28. }
  29. static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
  30. {
  31. struct md5_sha1_ctx *mctx = EVP_MD_CTX_md_data(ctx);
  32. if (!MD5_Update(&mctx->md5, data, count))
  33. return 0;
  34. return SHA1_Update(&mctx->sha1, data, count);
  35. }
  36. static int final(EVP_MD_CTX *ctx, unsigned char *md)
  37. {
  38. struct md5_sha1_ctx *mctx = EVP_MD_CTX_md_data(ctx);
  39. if (!MD5_Final(md, &mctx->md5))
  40. return 0;
  41. return SHA1_Final(md + MD5_DIGEST_LENGTH, &mctx->sha1);
  42. }
  43. static int ctrl(EVP_MD_CTX *ctx, int cmd, int mslen, void *ms)
  44. {
  45. unsigned char padtmp[48];
  46. unsigned char md5tmp[MD5_DIGEST_LENGTH];
  47. unsigned char sha1tmp[SHA_DIGEST_LENGTH];
  48. struct md5_sha1_ctx *mctx;
  49. if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
  50. return -2;
  51. if (ctx == NULL)
  52. return 0;
  53. mctx = EVP_MD_CTX_md_data(ctx);
  54. /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
  55. if (mslen != 48)
  56. return 0;
  57. /* At this point hash contains all handshake messages, update
  58. * with master secret and pad_1.
  59. */
  60. if (update(ctx, ms, mslen) <= 0)
  61. return 0;
  62. /* Set padtmp to pad_1 value */
  63. memset(padtmp, 0x36, sizeof(padtmp));
  64. if (!MD5_Update(&mctx->md5, padtmp, sizeof(padtmp)))
  65. return 0;
  66. if (!MD5_Final(md5tmp, &mctx->md5))
  67. return 0;
  68. if (!SHA1_Update(&mctx->sha1, padtmp, 40))
  69. return 0;
  70. if (!SHA1_Final(sha1tmp, &mctx->sha1))
  71. return 0;
  72. /* Reinitialise context */
  73. if (!init(ctx))
  74. return 0;
  75. if (update(ctx, ms, mslen) <= 0)
  76. return 0;
  77. /* Set padtmp to pad_2 value */
  78. memset(padtmp, 0x5c, sizeof(padtmp));
  79. if (!MD5_Update(&mctx->md5, padtmp, sizeof(padtmp)))
  80. return 0;
  81. if (!MD5_Update(&mctx->md5, md5tmp, sizeof(md5tmp)))
  82. return 0;
  83. if (!SHA1_Update(&mctx->sha1, padtmp, 40))
  84. return 0;
  85. if (!SHA1_Update(&mctx->sha1, sha1tmp, sizeof(sha1tmp)))
  86. return 0;
  87. /* Now when ctx is finalised it will return the SSL v3 hash value */
  88. OPENSSL_cleanse(md5tmp, sizeof(md5tmp));
  89. OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
  90. return 1;
  91. }
  92. static const EVP_MD md5_sha1_md = {
  93. NID_md5_sha1,
  94. NID_md5_sha1,
  95. MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
  96. 0,
  97. init,
  98. update,
  99. final,
  100. NULL,
  101. NULL,
  102. MD5_CBLOCK,
  103. sizeof(EVP_MD *) + sizeof(struct md5_sha1_ctx),
  104. ctrl
  105. };
  106. const EVP_MD *EVP_md5_sha1(void)
  107. {
  108. return &md5_sha1_md;
  109. }
  110. #endif