keccak.odin 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. package keccak implements the Keccak hash algorithm family.
  3. During the SHA-3 standardization process, the padding scheme was changed
  4. thus Keccac and SHA-3 produce different outputs. Most users should use
  5. SHA-3 and/or SHAKE instead, however the legacy algorithm is provided for
  6. backward compatibility purposes.
  7. */
  8. package keccak
  9. /*
  10. Copyright 2021 zhibog
  11. Made available under the BSD-3 license.
  12. List of contributors:
  13. zhibog, dotbmp: Initial implementation.
  14. */
  15. import "../../_sha3"
  16. // DIGEST_SIZE_224 is the Keccak-224 digest size.
  17. DIGEST_SIZE_224 :: 28
  18. // DIGEST_SIZE_256 is the Keccak-256 digest size.
  19. DIGEST_SIZE_256 :: 32
  20. // DIGEST_SIZE_384 is the Keccak-384 digest size.
  21. DIGEST_SIZE_384 :: 48
  22. // DIGEST_SIZE_512 is the Keccak-512 digest size.
  23. DIGEST_SIZE_512 :: 64
  24. // BLOCK_SIZE_224 is the Keccak-224 block size in bytes.
  25. BLOCK_SIZE_224 :: _sha3.RATE_224
  26. // BLOCK_SIZE_256 is the Keccak-256 block size in bytes.
  27. BLOCK_SIZE_256 :: _sha3.RATE_256
  28. // BLOCK_SIZE_384 is the Keccak-384 block size in bytes.
  29. BLOCK_SIZE_384 :: _sha3.RATE_384
  30. // BLOCK_SIZE_512 is the Keccak-512 block size in bytes.
  31. BLOCK_SIZE_512 :: _sha3.RATE_512
  32. // Context is a Keccak instance.
  33. Context :: distinct _sha3.Context
  34. // init_224 initializes a Context for Keccak-224.
  35. init_224 :: proc(ctx: ^Context) {
  36. ctx.mdlen = DIGEST_SIZE_224
  37. _init(ctx)
  38. }
  39. // init_256 initializes a Context for Keccak-256.
  40. init_256 :: proc(ctx: ^Context) {
  41. ctx.mdlen = DIGEST_SIZE_256
  42. _init(ctx)
  43. }
  44. // init_384 initializes a Context for Keccak-384.
  45. init_384 :: proc(ctx: ^Context) {
  46. ctx.mdlen = DIGEST_SIZE_384
  47. _init(ctx)
  48. }
  49. // init_512 initializes a Context for Keccak-512.
  50. init_512 :: proc(ctx: ^Context) {
  51. ctx.mdlen = DIGEST_SIZE_512
  52. _init(ctx)
  53. }
  54. @(private)
  55. _init :: proc(ctx: ^Context) {
  56. ctx.dsbyte = _sha3.DS_KECCAK
  57. _sha3.init(transmute(^_sha3.Context)(ctx))
  58. }
  59. // update adds more data to the Context.
  60. update :: proc(ctx: ^Context, data: []byte) {
  61. _sha3.update(transmute(^_sha3.Context)(ctx), data)
  62. }
  63. // final finalizes the Context, writes the digest to hash, and calls
  64. // reset on the Context.
  65. //
  66. // Iff finalize_clone is set, final will work on a copy of the Context,
  67. // which is useful for for calculating rolling digests.
  68. final :: proc(ctx: ^Context, hash: []byte, finalize_clone: bool = false) {
  69. _sha3.final(transmute(^_sha3.Context)(ctx), hash, finalize_clone)
  70. }
  71. // clone clones the Context other into ctx.
  72. clone :: proc(ctx, other: ^Context) {
  73. _sha3.clone(transmute(^_sha3.Context)(ctx), transmute(^_sha3.Context)(other))
  74. }
  75. // reset sanitizes the Context. The Context must be re-initialized to
  76. // be used again.
  77. reset :: proc(ctx: ^Context) {
  78. _sha3.reset(transmute(^_sha3.Context)(ctx))
  79. }