sha1.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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-2006, 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. typedef EVP_MD_CTX sha1_ctx_t;
  55. /*
  56. * sha1_init(&ctx) initializes the SHA1 context ctx
  57. *
  58. * sha1_update(&ctx, msg, len) hashes the len octets starting at msg
  59. * into the SHA1 context
  60. *
  61. * sha1_final(&ctx, output) performs the final processing of the SHA1
  62. * context and writes the result to the 20 octets at output
  63. *
  64. * Return values are ignored on the EVP functions since all three
  65. * of these functions return void.
  66. *
  67. */
  68. static inline void sha1_init (sha1_ctx_t *ctx)
  69. {
  70. EVP_MD_CTX_init(ctx);
  71. EVP_DigestInit(ctx, EVP_sha1());
  72. }
  73. static inline void sha1_update (sha1_ctx_t *ctx, const uint8_t *M, int octets_in_msg)
  74. {
  75. EVP_DigestUpdate(ctx, M, octets_in_msg);
  76. }
  77. static inline void sha1_final (sha1_ctx_t *ctx, uint32_t *output)
  78. {
  79. unsigned int len = 0;
  80. EVP_DigestFinal(ctx, (unsigned char*)output, &len);
  81. }
  82. #else
  83. #include "datatypes.h"
  84. typedef struct {
  85. uint32_t H[5]; /* state vector */
  86. uint32_t M[16]; /* message buffer */
  87. int octets_in_buffer; /* octets of message in buffer */
  88. uint32_t num_bits_in_msg; /* total number of bits in message */
  89. } sha1_ctx_t;
  90. /*
  91. * sha1(&ctx, msg, len, output) hashes the len octets starting at msg
  92. * into the SHA1 context, then writes the result to the 20 octets at
  93. * output
  94. *
  95. */
  96. void
  97. sha1(const uint8_t *message, int octets_in_msg, uint32_t output[5]);
  98. /*
  99. * sha1_init(&ctx) initializes the SHA1 context ctx
  100. *
  101. * sha1_update(&ctx, msg, len) hashes the len octets starting at msg
  102. * into the SHA1 context
  103. *
  104. * sha1_final(&ctx, output) performs the final processing of the SHA1
  105. * context and writes the result to the 20 octets at output
  106. *
  107. */
  108. void
  109. sha1_init(sha1_ctx_t *ctx);
  110. void
  111. sha1_update(sha1_ctx_t *ctx, const uint8_t *M, int octets_in_msg);
  112. void
  113. sha1_final(sha1_ctx_t *ctx, uint32_t output[5]);
  114. /*
  115. * The sha1_core function is INTERNAL to SHA-1, but it is declared
  116. * here because it is also used by the cipher SEAL 3.0 in its key
  117. * setup algorithm.
  118. */
  119. /*
  120. * sha1_core(M, H) computes the core sha1 compression function, where M is
  121. * the next part of the message and H is the intermediate state {H0,
  122. * H1, ...}
  123. *
  124. * this function does not do any of the padding required in the
  125. * complete sha1 function
  126. */
  127. void
  128. sha1_core(const uint32_t M[16], uint32_t hash_value[5]);
  129. #endif /* else OPENSSL */
  130. #endif /* SHA1_H */