sha3.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. {
  45. /* 1600 bits algorithm hashing state */
  46. uint64_t hash[sha3_max_permutation_size];
  47. /* 1536-bit buffer for leftovers */
  48. uint64_t message[sha3_max_rate_in_qwords];
  49. /* count of bytes in the message[] buffer */
  50. unsigned rest;
  51. /* size of a message block processed at once */
  52. unsigned block_size;
  53. } SHA3_CTX;
  54. /* methods for calculating the hash function */
  55. void sha3_224_Init(SHA3_CTX *ctx);
  56. void sha3_256_Init(SHA3_CTX *ctx);
  57. void sha3_384_Init(SHA3_CTX *ctx);
  58. void sha3_512_Init(SHA3_CTX *ctx);
  59. void sha3_Update(SHA3_CTX *ctx, const unsigned char* msg, size_t size);
  60. void sha3_Final(SHA3_CTX *ctx, unsigned char* result);
  61. #if USE_KECCAK
  62. #define keccak_224_Init sha3_224_Init
  63. #define keccak_256_Init sha3_256_Init
  64. #define keccak_384_Init sha3_384_Init
  65. #define keccak_512_Init sha3_512_Init
  66. #define keccak_Update sha3_Update
  67. void keccak_Final(SHA3_CTX *ctx, unsigned char* result);
  68. void keccak_256(const unsigned char* data, size_t len, unsigned char* digest);
  69. void keccak_512(const unsigned char* data, size_t len, unsigned char* digest);
  70. #endif
  71. void sha3_256(const unsigned char* data, size_t len, unsigned char* digest);
  72. void sha3_512(const unsigned char* data, size_t len, unsigned char* digest);
  73. #ifdef __cplusplus
  74. } /* extern "C" */
  75. #endif /* __cplusplus */
  76. #endif /* __SHA3_H__ */