sha3.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* sha3.h - an implementation of Secure Hash Algorithm 3 (Keccak).
  2. * based on the
  3. * The Keccak SHA-3 submission. Submission to NIST (Round 3), 2011
  4. * by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche
  5. *
  6. * Copyright: 2013 Aleksey Kravchenko <rhash.admin@gmail.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
  18. */
  19. #ifndef __SHA3_H__
  20. #define __SHA3_H__
  21. #include <stdint.h>
  22. #include "options.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #define sha3_224_hash_size 28
  27. #define sha3_256_hash_size 32
  28. #define sha3_384_hash_size 48
  29. #define sha3_512_hash_size 64
  30. #define sha3_max_permutation_size 25
  31. #define sha3_max_rate_in_qwords 24
  32. #define SHA3_224_BLOCK_LENGTH 144
  33. #define SHA3_256_BLOCK_LENGTH 136
  34. #define SHA3_384_BLOCK_LENGTH 104
  35. #define SHA3_512_BLOCK_LENGTH 72
  36. #define SHA3_224_DIGEST_LENGTH sha3_224_hash_size
  37. #define SHA3_256_DIGEST_LENGTH sha3_256_hash_size
  38. #define SHA3_384_DIGEST_LENGTH sha3_384_hash_size
  39. #define SHA3_512_DIGEST_LENGTH sha3_512_hash_size
  40. /**
  41. * SHA3 Algorithm context.
  42. */
  43. typedef struct SHA3_CTX {
  44. /* 1600 bits algorithm hashing state */
  45. uint64_t hash[sha3_max_permutation_size];
  46. /* 1536-bit buffer for leftovers */
  47. uint64_t message[sha3_max_rate_in_qwords];
  48. /* count of bytes in the message[] buffer */
  49. unsigned rest;
  50. /* size of a message block processed at once */
  51. unsigned block_size;
  52. } SHA3_CTX;
  53. /* methods for calculating the hash function */
  54. void sha3_224_Init(SHA3_CTX* ctx);
  55. void sha3_256_Init(SHA3_CTX* ctx);
  56. void sha3_384_Init(SHA3_CTX* ctx);
  57. void sha3_512_Init(SHA3_CTX* ctx);
  58. void sha3_Update(SHA3_CTX* ctx, const unsigned char* msg, size_t size);
  59. void sha3_Final(SHA3_CTX* ctx, unsigned char* result);
  60. #if USE_KECCAK
  61. #define keccak_224_Init sha3_224_Init
  62. #define keccak_256_Init sha3_256_Init
  63. #define keccak_384_Init sha3_384_Init
  64. #define keccak_512_Init sha3_512_Init
  65. #define keccak_Update sha3_Update
  66. void keccak_Final(SHA3_CTX* ctx, unsigned char* result);
  67. void keccak_256(const unsigned char* data, size_t len, unsigned char* digest);
  68. void keccak_512(const unsigned char* data, size_t len, unsigned char* digest);
  69. #endif
  70. void sha3_256(const unsigned char* data, size_t len, unsigned char* digest);
  71. void sha3_512(const unsigned char* data, size_t len, unsigned char* digest);
  72. #ifdef __cplusplus
  73. } /* extern "C" */
  74. #endif /* __cplusplus */
  75. #endif /* __SHA3_H__ */