sha1_one.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 1995-2021 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 <stdio.h>
  15. #include <string.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/sha.h>
  18. #include <openssl/evp.h>
  19. #include "crypto/sha.h"
  20. unsigned char *ossl_sha1(const unsigned char *d, size_t n, unsigned char *md)
  21. {
  22. SHA_CTX c;
  23. static unsigned char m[SHA_DIGEST_LENGTH];
  24. if (md == NULL)
  25. md = m;
  26. if (!SHA1_Init(&c))
  27. return NULL;
  28. SHA1_Update(&c, d, n);
  29. SHA1_Final(md, &c);
  30. OPENSSL_cleanse(&c, sizeof(c));
  31. return md;
  32. }
  33. unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md)
  34. {
  35. static unsigned char m[SHA_DIGEST_LENGTH];
  36. if (md == NULL)
  37. md = m;
  38. return EVP_Q_digest(NULL, "SHA1", NULL, d, n, md, NULL) ? md : NULL;
  39. }
  40. unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md)
  41. {
  42. static unsigned char m[SHA224_DIGEST_LENGTH];
  43. if (md == NULL)
  44. md = m;
  45. return EVP_Q_digest(NULL, "SHA224", NULL, d, n, md, NULL) ? md : NULL;
  46. }
  47. unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md)
  48. {
  49. static unsigned char m[SHA256_DIGEST_LENGTH];
  50. if (md == NULL)
  51. md = m;
  52. return EVP_Q_digest(NULL, "SHA256", NULL, d, n, md, NULL) ? md : NULL;
  53. }
  54. unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md)
  55. {
  56. static unsigned char m[SHA384_DIGEST_LENGTH];
  57. if (md == NULL)
  58. md = m;
  59. return EVP_Q_digest(NULL, "SHA384", NULL, d, n, md, NULL) ? md : NULL;
  60. }
  61. unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md)
  62. {
  63. static unsigned char m[SHA512_DIGEST_LENGTH];
  64. if (md == NULL)
  65. md = m;
  66. return EVP_Q_digest(NULL, "SHA512", NULL, d, n, md, NULL) ? md : NULL;
  67. }