sha1dgst.c 1.8 KB

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