sha2.h 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Copyright (c) 2000-2001 Aaron D. Gifford
  3. * Copyright (c) 2013-2014 Pavol Rusnak
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holder nor the names of contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #ifndef __SHA2_H__
  31. #define __SHA2_H__
  32. #include <stdint.h>
  33. #include <stddef.h>
  34. #include "byte_order.h"
  35. #define SHA1_BLOCK_LENGTH 64
  36. #define SHA1_DIGEST_LENGTH 20
  37. #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
  38. #define SHA256_BLOCK_LENGTH 64
  39. #define SHA256_DIGEST_LENGTH 32
  40. #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
  41. #define SHA512_BLOCK_LENGTH 128
  42. #define SHA512_DIGEST_LENGTH 64
  43. #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
  44. typedef struct _SHA1_CTX {
  45. uint32_t state[5];
  46. uint64_t bitcount;
  47. uint32_t buffer[SHA1_BLOCK_LENGTH / sizeof(uint32_t)];
  48. } SHA1_CTX;
  49. typedef struct _SHA256_CTX {
  50. uint32_t state[8];
  51. uint64_t bitcount;
  52. uint32_t buffer[SHA256_BLOCK_LENGTH / sizeof(uint32_t)];
  53. } SHA256_CTX;
  54. typedef struct _SHA512_CTX {
  55. uint64_t state[8];
  56. uint64_t bitcount[2];
  57. uint64_t buffer[SHA512_BLOCK_LENGTH / sizeof(uint64_t)];
  58. } SHA512_CTX;
  59. extern const uint32_t sha256_initial_hash_value[8];
  60. extern const uint64_t sha512_initial_hash_value[8];
  61. void sha1_Transform(const uint32_t* state_in, const uint32_t* data, uint32_t* state_out);
  62. void sha1_Init(SHA1_CTX*);
  63. void sha1_Update(SHA1_CTX*, const uint8_t*, size_t);
  64. void sha1_Final(SHA1_CTX*, uint8_t[SHA1_DIGEST_LENGTH]);
  65. char* sha1_End(SHA1_CTX*, char[SHA1_DIGEST_STRING_LENGTH]);
  66. void sha1_Raw(const uint8_t*, size_t, uint8_t[SHA1_DIGEST_LENGTH]);
  67. char* sha1_Data(const uint8_t*, size_t, char[SHA1_DIGEST_STRING_LENGTH]);
  68. void sha256_Transform(const uint32_t* state_in, const uint32_t* data, uint32_t* state_out);
  69. void sha256_Init(SHA256_CTX*);
  70. void sha256_Init_ex(SHA256_CTX*, const uint32_t state[8], uint64_t bitcount);
  71. void sha256_Update(SHA256_CTX*, const uint8_t*, size_t);
  72. void sha256_Final(SHA256_CTX*, uint8_t[SHA256_DIGEST_LENGTH]);
  73. char* sha256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
  74. void sha256_Raw(const uint8_t*, size_t, uint8_t[SHA256_DIGEST_LENGTH]);
  75. char* sha256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
  76. void sha512_Transform(const uint64_t* state_in, const uint64_t* data, uint64_t* state_out);
  77. void sha512_Init(SHA512_CTX*);
  78. void sha512_Update(SHA512_CTX*, const uint8_t*, size_t);
  79. void sha512_Final(SHA512_CTX*, uint8_t[SHA512_DIGEST_LENGTH]);
  80. char* sha512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
  81. void sha512_Raw(const uint8_t*, size_t, uint8_t[SHA512_DIGEST_LENGTH]);
  82. char* sha512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
  83. #endif