sha.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 1995-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. #ifndef OPENSSL_SHA_H
  10. #define OPENSSL_SHA_H
  11. #pragma once
  12. #include <openssl/macros.h>
  13. #ifndef OPENSSL_NO_DEPRECATED_3_0
  14. #define HEADER_SHA_H
  15. #endif
  16. #include <openssl/e_os2.h>
  17. #include <stddef.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define SHA_DIGEST_LENGTH 20
  22. #ifndef OPENSSL_NO_DEPRECATED_3_0
  23. /*-
  24. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  25. * ! SHA_LONG has to be at least 32 bits wide. !
  26. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  27. */
  28. #define SHA_LONG unsigned int
  29. #define SHA_LBLOCK 16
  30. #define SHA_CBLOCK (SHA_LBLOCK * 4) /* SHA treats input data as a \
  31. * contiguous array of 32 bit wide \
  32. * big-endian values. */
  33. #define SHA_LAST_BLOCK (SHA_CBLOCK - 8)
  34. typedef struct SHAstate_st {
  35. SHA_LONG h0, h1, h2, h3, h4;
  36. SHA_LONG Nl, Nh;
  37. SHA_LONG data[SHA_LBLOCK];
  38. unsigned int num;
  39. } SHA_CTX;
  40. OSSL_DEPRECATEDIN_3_0 int SHA1_Init(SHA_CTX *c);
  41. OSSL_DEPRECATEDIN_3_0 int SHA1_Update(SHA_CTX *c, const void *data, size_t len);
  42. OSSL_DEPRECATEDIN_3_0 int SHA1_Final(unsigned char *md, SHA_CTX *c);
  43. OSSL_DEPRECATEDIN_3_0 void SHA1_Transform(SHA_CTX *c, const unsigned char *data);
  44. #endif
  45. unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md);
  46. #ifndef OPENSSL_NO_DEPRECATED_3_0
  47. #define SHA256_CBLOCK (SHA_LBLOCK * 4) /* SHA-256 treats input data as a \
  48. * contiguous array of 32 bit wide \
  49. * big-endian values. */
  50. typedef struct SHA256state_st {
  51. SHA_LONG h[8];
  52. SHA_LONG Nl, Nh;
  53. SHA_LONG data[SHA_LBLOCK];
  54. unsigned int num, md_len;
  55. } SHA256_CTX;
  56. OSSL_DEPRECATEDIN_3_0 int SHA224_Init(SHA256_CTX *c);
  57. OSSL_DEPRECATEDIN_3_0 int SHA224_Update(SHA256_CTX *c,
  58. const void *data, size_t len);
  59. OSSL_DEPRECATEDIN_3_0 int SHA224_Final(unsigned char *md, SHA256_CTX *c);
  60. OSSL_DEPRECATEDIN_3_0 int SHA256_Init(SHA256_CTX *c);
  61. OSSL_DEPRECATEDIN_3_0 int SHA256_Update(SHA256_CTX *c,
  62. const void *data, size_t len);
  63. OSSL_DEPRECATEDIN_3_0 int SHA256_Final(unsigned char *md, SHA256_CTX *c);
  64. OSSL_DEPRECATEDIN_3_0 void SHA256_Transform(SHA256_CTX *c,
  65. const unsigned char *data);
  66. #endif
  67. unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md);
  68. unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md);
  69. #define SHA256_192_DIGEST_LENGTH 24
  70. #define SHA224_DIGEST_LENGTH 28
  71. #define SHA256_DIGEST_LENGTH 32
  72. #define SHA384_DIGEST_LENGTH 48
  73. #define SHA512_DIGEST_LENGTH 64
  74. #ifndef OPENSSL_NO_DEPRECATED_3_0
  75. /*
  76. * Unlike 32-bit digest algorithms, SHA-512 *relies* on SHA_LONG64
  77. * being exactly 64-bit wide. See Implementation Notes in sha512.c
  78. * for further details.
  79. */
  80. /*
  81. * SHA-512 treats input data as a
  82. * contiguous array of 64 bit
  83. * wide big-endian values.
  84. */
  85. #define SHA512_CBLOCK (SHA_LBLOCK * 8)
  86. #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
  87. #define SHA_LONG64 unsigned __int64
  88. #elif defined(__arch64__)
  89. #define SHA_LONG64 unsigned long
  90. #else
  91. #define SHA_LONG64 unsigned long long
  92. #endif
  93. typedef struct SHA512state_st {
  94. SHA_LONG64 h[8];
  95. SHA_LONG64 Nl, Nh;
  96. union {
  97. SHA_LONG64 d[SHA_LBLOCK];
  98. unsigned char p[SHA512_CBLOCK];
  99. } u;
  100. unsigned int num, md_len;
  101. } SHA512_CTX;
  102. OSSL_DEPRECATEDIN_3_0 int SHA384_Init(SHA512_CTX *c);
  103. OSSL_DEPRECATEDIN_3_0 int SHA384_Update(SHA512_CTX *c,
  104. const void *data, size_t len);
  105. OSSL_DEPRECATEDIN_3_0 int SHA384_Final(unsigned char *md, SHA512_CTX *c);
  106. OSSL_DEPRECATEDIN_3_0 int SHA512_Init(SHA512_CTX *c);
  107. OSSL_DEPRECATEDIN_3_0 int SHA512_Update(SHA512_CTX *c,
  108. const void *data, size_t len);
  109. OSSL_DEPRECATEDIN_3_0 int SHA512_Final(unsigned char *md, SHA512_CTX *c);
  110. OSSL_DEPRECATEDIN_3_0 void SHA512_Transform(SHA512_CTX *c,
  111. const unsigned char *data);
  112. #endif
  113. unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md);
  114. unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md);
  115. #ifdef __cplusplus
  116. }
  117. #endif
  118. #endif