sha2_prov.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2019-2023 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 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/core_dispatch.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/sha.h>
  18. #include <openssl/params.h>
  19. #include <openssl/core_names.h>
  20. #include "prov/digestcommon.h"
  21. #include "prov/implementations.h"
  22. #include "crypto/sha.h"
  23. #define SHA2_FLAGS PROV_DIGEST_FLAG_ALGID_ABSENT
  24. static OSSL_FUNC_digest_set_ctx_params_fn sha1_set_ctx_params;
  25. static OSSL_FUNC_digest_settable_ctx_params_fn sha1_settable_ctx_params;
  26. static const OSSL_PARAM known_sha1_settable_ctx_params[] = {
  27. {OSSL_DIGEST_PARAM_SSL3_MS, OSSL_PARAM_OCTET_STRING, NULL, 0, 0},
  28. OSSL_PARAM_END
  29. };
  30. static const OSSL_PARAM *sha1_settable_ctx_params(ossl_unused void *ctx,
  31. ossl_unused void *provctx)
  32. {
  33. return known_sha1_settable_ctx_params;
  34. }
  35. /* Special set_params method for SSL3 */
  36. static int sha1_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  37. {
  38. const OSSL_PARAM *p;
  39. SHA_CTX *ctx = (SHA_CTX *)vctx;
  40. if (ctx == NULL)
  41. return 0;
  42. if (params == NULL)
  43. return 1;
  44. p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SSL3_MS);
  45. if (p != NULL && p->data_type == OSSL_PARAM_OCTET_STRING)
  46. return ossl_sha1_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET,
  47. p->data_size, p->data);
  48. return 1;
  49. }
  50. /* ossl_sha1_functions */
  51. IMPLEMENT_digest_functions_with_settable_ctx(
  52. sha1, SHA_CTX, SHA_CBLOCK, SHA_DIGEST_LENGTH, SHA2_FLAGS,
  53. SHA1_Init, SHA1_Update, SHA1_Final,
  54. sha1_settable_ctx_params, sha1_set_ctx_params)
  55. /* ossl_sha224_functions */
  56. IMPLEMENT_digest_functions(sha224, SHA256_CTX,
  57. SHA256_CBLOCK, SHA224_DIGEST_LENGTH, SHA2_FLAGS,
  58. SHA224_Init, SHA224_Update, SHA224_Final)
  59. /* ossl_sha256_functions */
  60. IMPLEMENT_digest_functions(sha256, SHA256_CTX,
  61. SHA256_CBLOCK, SHA256_DIGEST_LENGTH, SHA2_FLAGS,
  62. SHA256_Init, SHA256_Update, SHA256_Final)
  63. #ifndef FIPS_MODULE
  64. /* ossl_sha256_192_functions */
  65. IMPLEMENT_digest_functions(sha256_192, SHA256_CTX,
  66. SHA256_CBLOCK, SHA256_192_DIGEST_LENGTH, SHA2_FLAGS,
  67. ossl_sha256_192_init, SHA256_Update, SHA256_Final)
  68. #endif
  69. /* ossl_sha384_functions */
  70. IMPLEMENT_digest_functions(sha384, SHA512_CTX,
  71. SHA512_CBLOCK, SHA384_DIGEST_LENGTH, SHA2_FLAGS,
  72. SHA384_Init, SHA384_Update, SHA384_Final)
  73. /* ossl_sha512_functions */
  74. IMPLEMENT_digest_functions(sha512, SHA512_CTX,
  75. SHA512_CBLOCK, SHA512_DIGEST_LENGTH, SHA2_FLAGS,
  76. SHA512_Init, SHA512_Update, SHA512_Final)
  77. /* ossl_sha512_224_functions */
  78. IMPLEMENT_digest_functions(sha512_224, SHA512_CTX,
  79. SHA512_CBLOCK, SHA224_DIGEST_LENGTH, SHA2_FLAGS,
  80. sha512_224_init, SHA512_Update, SHA512_Final)
  81. /* ossl_sha512_256_functions */
  82. IMPLEMENT_digest_functions(sha512_256, SHA512_CTX,
  83. SHA512_CBLOCK, SHA256_DIGEST_LENGTH, SHA2_FLAGS,
  84. sha512_256_init, SHA512_Update, SHA512_Final)