md5_sha1.c 2.6 KB

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