md5_sha1.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <string.h>
  10. #include "prov/md5_sha1.h"
  11. #include <openssl/evp.h>
  12. int md5_sha1_init(MD5_SHA1_CTX *mctx)
  13. {
  14. if (!MD5_Init(&mctx->md5))
  15. return 0;
  16. return SHA1_Init(&mctx->sha1);
  17. }
  18. int md5_sha1_update(MD5_SHA1_CTX *mctx, const void *data, size_t count)
  19. {
  20. if (!MD5_Update(&mctx->md5, data, count))
  21. return 0;
  22. return SHA1_Update(&mctx->sha1, data, count);
  23. }
  24. int md5_sha1_final(unsigned char *md, MD5_SHA1_CTX *mctx)
  25. {
  26. if (!MD5_Final(md, &mctx->md5))
  27. return 0;
  28. return SHA1_Final(md + MD5_DIGEST_LENGTH, &mctx->sha1);
  29. }
  30. int md5_sha1_ctrl(MD5_SHA1_CTX *mctx, int cmd, int mslen, void *ms)
  31. {
  32. unsigned char padtmp[48];
  33. unsigned char md5tmp[MD5_DIGEST_LENGTH];
  34. unsigned char sha1tmp[SHA_DIGEST_LENGTH];
  35. if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
  36. return -2;
  37. if (mctx == NULL)
  38. return 0;
  39. /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
  40. if (mslen != 48)
  41. return 0;
  42. /* At this point hash contains all handshake messages, update
  43. * with master secret and pad_1.
  44. */
  45. if (md5_sha1_update(mctx, ms, mslen) <= 0)
  46. return 0;
  47. /* Set padtmp to pad_1 value */
  48. memset(padtmp, 0x36, sizeof(padtmp));
  49. if (!MD5_Update(&mctx->md5, padtmp, sizeof(padtmp)))
  50. return 0;
  51. if (!MD5_Final(md5tmp, &mctx->md5))
  52. return 0;
  53. if (!SHA1_Update(&mctx->sha1, padtmp, 40))
  54. return 0;
  55. if (!SHA1_Final(sha1tmp, &mctx->sha1))
  56. return 0;
  57. /* Reinitialise context */
  58. if (!md5_sha1_init(mctx))
  59. return 0;
  60. if (md5_sha1_update(mctx, ms, mslen) <= 0)
  61. return 0;
  62. /* Set padtmp to pad_2 value */
  63. memset(padtmp, 0x5c, sizeof(padtmp));
  64. if (!MD5_Update(&mctx->md5, padtmp, sizeof(padtmp)))
  65. return 0;
  66. if (!MD5_Update(&mctx->md5, md5tmp, sizeof(md5tmp)))
  67. return 0;
  68. if (!SHA1_Update(&mctx->sha1, padtmp, 40))
  69. return 0;
  70. if (!SHA1_Update(&mctx->sha1, sha1tmp, sizeof(sha1tmp)))
  71. return 0;
  72. /* Now when ctx is finalised it will return the SSL v3 hash value */
  73. OPENSSL_cleanse(md5tmp, sizeof(md5tmp));
  74. OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
  75. return 1;
  76. }