2
0

sha1.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * sha1.h
  3. *
  4. * interface to the Secure Hash Algorithm v.1 (SHA-1), specified in
  5. * FIPS 180-1
  6. *
  7. * David A. McGrew
  8. * Cisco Systems, Inc.
  9. */
  10. /*
  11. *
  12. * Copyright (c) 2001-2017, Cisco Systems, Inc.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *
  19. * Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * Neither the name of the Cisco Systems, Inc. nor the names of its
  28. * contributors may be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  35. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  36. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  37. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  38. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42. * OF THE POSSIBILITY OF SUCH DAMAGE.
  43. *
  44. */
  45. #ifndef SHA1_H
  46. #define SHA1_H
  47. #ifdef HAVE_CONFIG_H
  48. #include <config.h>
  49. #endif
  50. #include "err.h"
  51. #ifdef OPENSSL
  52. #include <openssl/evp.h>
  53. #include <stdint.h>
  54. #else
  55. #include "datatypes.h"
  56. #endif
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. #ifdef OPENSSL
  61. /*
  62. * srtp_sha1_init(&ctx) initializes the SHA1 context ctx
  63. *
  64. * srtp_sha1_update(&ctx, msg, len) hashes the len octets starting at msg
  65. * into the SHA1 context
  66. *
  67. * srtp_sha1_final(&ctx, output) performs the final processing of the SHA1
  68. * context and writes the result to the 20 octets at output
  69. *
  70. * Return values are ignored on the EVP functions since all three
  71. * of these functions return void.
  72. *
  73. */
  74. /* OpenSSL 1.1.0 made EVP_MD_CTX an opaque structure, which must be allocated
  75. using EVP_MD_CTX_new. But this function doesn't exist in OpenSSL 1.0.x. */
  76. #if OPENSSL_VERSION_NUMBER < 0x10100000L || LIBRESSL_VERSION_NUMBER
  77. typedef EVP_MD_CTX srtp_sha1_ctx_t;
  78. static inline void srtp_sha1_init(srtp_sha1_ctx_t *ctx)
  79. {
  80. EVP_MD_CTX_init(ctx);
  81. EVP_DigestInit(ctx, EVP_sha1());
  82. }
  83. static inline void srtp_sha1_update(srtp_sha1_ctx_t *ctx,
  84. const uint8_t *M,
  85. int octets_in_msg)
  86. {
  87. EVP_DigestUpdate(ctx, M, octets_in_msg);
  88. }
  89. static inline void srtp_sha1_final(srtp_sha1_ctx_t *ctx, uint32_t *output)
  90. {
  91. unsigned int len = 0;
  92. EVP_DigestFinal(ctx, (unsigned char *)output, &len);
  93. EVP_MD_CTX_cleanup(ctx);
  94. }
  95. #else
  96. typedef EVP_MD_CTX *srtp_sha1_ctx_t;
  97. static inline void srtp_sha1_init(srtp_sha1_ctx_t *ctx)
  98. {
  99. *ctx = EVP_MD_CTX_new();
  100. EVP_DigestInit(*ctx, EVP_sha1());
  101. }
  102. static inline void srtp_sha1_update(srtp_sha1_ctx_t *ctx,
  103. const uint8_t *M,
  104. int octets_in_msg)
  105. {
  106. EVP_DigestUpdate(*ctx, M, octets_in_msg);
  107. }
  108. static inline void srtp_sha1_final(srtp_sha1_ctx_t *ctx, uint32_t *output)
  109. {
  110. unsigned int len = 0;
  111. EVP_DigestFinal(*ctx, (unsigned char *)output, &len);
  112. EVP_MD_CTX_free(*ctx);
  113. }
  114. #endif
  115. #else
  116. typedef struct {
  117. uint32_t H[5]; /* state vector */
  118. uint32_t M[16]; /* message buffer */
  119. int octets_in_buffer; /* octets of message in buffer */
  120. uint32_t num_bits_in_msg; /* total number of bits in message */
  121. } srtp_sha1_ctx_t;
  122. /*
  123. * srtp_sha1_init(&ctx) initializes the SHA1 context ctx
  124. *
  125. * srtp_sha1_update(&ctx, msg, len) hashes the len octets starting at msg
  126. * into the SHA1 context
  127. *
  128. * srtp_sha1_final(&ctx, output) performs the final processing of the SHA1
  129. * context and writes the result to the 20 octets at output
  130. *
  131. */
  132. void srtp_sha1_init(srtp_sha1_ctx_t *ctx);
  133. void srtp_sha1_update(srtp_sha1_ctx_t *ctx,
  134. const uint8_t *M,
  135. int octets_in_msg);
  136. void srtp_sha1_final(srtp_sha1_ctx_t *ctx, uint32_t output[5]);
  137. /*
  138. * The srtp_sha1_core function is INTERNAL to SHA-1, but it is declared
  139. * here because it is also used by the cipher SEAL 3.0 in its key
  140. * setup algorithm.
  141. */
  142. /*
  143. * srtp_sha1_core(M, H) computes the core sha1 compression function, where M is
  144. * the next part of the message and H is the intermediate state {H0,
  145. * H1, ...}
  146. *
  147. * this function does not do any of the padding required in the
  148. * complete sha1 function
  149. */
  150. void srtp_sha1_core(const uint32_t M[16], uint32_t hash_value[5]);
  151. #endif /* else OPENSSL */
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif /* SHA1_H */