sha1dgst.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 1995-2022 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. * 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 <openssl/crypto.h>
  15. #include <openssl/opensslconf.h>
  16. #include <openssl/opensslv.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/sha.h>
  19. /* The implementation is in crypto/md32_common.h */
  20. #include "sha_local.h"
  21. #include "crypto/sha.h"
  22. int ossl_sha1_ctrl(SHA_CTX *sha1, int cmd, int mslen, void *ms)
  23. {
  24. unsigned char padtmp[40];
  25. unsigned char sha1tmp[SHA_DIGEST_LENGTH];
  26. if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
  27. return -2;
  28. if (sha1 == NULL)
  29. return 0;
  30. /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
  31. if (mslen != 48)
  32. return 0;
  33. /* At this point hash contains all handshake messages, update
  34. * with master secret and pad_1.
  35. */
  36. if (SHA1_Update(sha1, ms, mslen) <= 0)
  37. return 0;
  38. /* Set padtmp to pad_1 value */
  39. memset(padtmp, 0x36, sizeof(padtmp));
  40. if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
  41. return 0;
  42. if (!SHA1_Final(sha1tmp, sha1))
  43. return 0;
  44. /* Reinitialise context */
  45. if (!SHA1_Init(sha1))
  46. return 0;
  47. if (SHA1_Update(sha1, ms, mslen) <= 0)
  48. return 0;
  49. /* Set padtmp to pad_2 value */
  50. memset(padtmp, 0x5c, sizeof(padtmp));
  51. if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
  52. return 0;
  53. if (!SHA1_Update(sha1, sha1tmp, sizeof(sha1tmp)))
  54. return 0;
  55. /* Now when ctx is finalised it will return the SSL v3 hash value */
  56. OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
  57. return 1;
  58. }