sha1.h 4.1 KB

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