|
| 1 | +# Constantine |
| 2 | +# Copyright (c) 2018-2019 Status Research & Development GmbH |
| 3 | +# Copyright (c) 2020-Present Mamy André-Ratsimbazafy |
| 4 | +# Licensed and distributed under either of |
| 5 | +# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). |
| 6 | +# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). |
| 7 | +# at your option. This file may not be copied, modified, or distributed except according to those terms. |
| 8 | + |
| 9 | +import constantine/zoo_exports |
| 10 | + |
| 11 | +import |
| 12 | + constantine/platforms/[abstractions, views], |
| 13 | + constantine/serialization/endians, |
| 14 | + ./ripemd160/ripemd160_generic |
| 15 | + |
| 16 | + |
| 17 | +# RIPEMD-160, a hash function from the RIPE family |
| 18 | +# -------------------------------------------------------------------------------- |
| 19 | +# |
| 20 | +# References: |
| 21 | +# - ISO: ISO/IEC 10118-3:2004, https://www.iso.org/standard/67116.html (latest revision) |
| 22 | +# - https://homes.esat.kuleuven.be/~bosselae/ripemd160.html |
| 23 | +# -> Includes a reference implementation in C, however only accessible via the Wayback Machine |
| 24 | +# as of Dec 2024. |
| 25 | +# - Bitcoin implementation: |
| 26 | +# https://github.com/bitcoin-core/btcdeb/blob/e2c2e7b9fe2ecc0884129b53813a733f93a6e2c7/crypto/ripemd160.cpp#L242 |
| 27 | +# |
| 28 | +# Vectors: |
| 29 | +# - https://homes.esat.kuleuven.be/~bosselae/ripemd160.html |
| 30 | +# - [ ] Find Bitcoin vectors |
| 31 | + |
| 32 | +# Types and constants |
| 33 | +# ---------------------------------------------------------------- |
| 34 | + |
| 35 | +type |
| 36 | + ripemd160* = Ripemd160Context # defined in generic file atm |
| 37 | + |
| 38 | +export Ripemd160Context |
| 39 | + |
| 40 | +# Internals |
| 41 | +# ---------------------------------------------------------------- |
| 42 | +# defined in `ripemd160/ripemd160_generic.nim` at the moment |
| 43 | + |
| 44 | +# No exceptions allowed in core cryptographic operations |
| 45 | +{.push raises: [].} |
| 46 | +{.push checks: off.} |
| 47 | + |
| 48 | +# Public API |
| 49 | +# ---------------------------------------------------------------- |
| 50 | + |
| 51 | +template digestSize*(H: type ripemd160): int = |
| 52 | + ## Returns the output size in bytes |
| 53 | + DigestSize |
| 54 | + |
| 55 | +template internalBlockSize*(H: type ripemd160): int = |
| 56 | + ## Returns the byte size of the hash function ingested blocks |
| 57 | + BlockSize |
| 58 | + |
| 59 | +func init*(ctx: var Ripemd160Context) = |
| 60 | + ## Initialize or reinitialize a Ripemd160 context |
| 61 | + ctx.reset() |
| 62 | + |
| 63 | +func update*(ctx: var Ripemd160Context, message: openarray[byte]) = |
| 64 | + ## Append a message to a Ripemd160 context for incremental Ripemd160 computation. |
| 65 | + ## |
| 66 | + ## Security note: the tail of your message might be stored |
| 67 | + ## in an internal buffer. |
| 68 | + ## if sensitive content is used, ensure that |
| 69 | + ## `ctx.finish(...)` and `ctx.clear()` are called as soon as possible. |
| 70 | + ## Additionally ensure that the message(s) passed was(were) stored |
| 71 | + ## in memory considered secure for your threat model. |
| 72 | + ctx.write(message, message.len.uint64) |
| 73 | + |
| 74 | +func finish*(ctx: var Ripemd160Context, digest: var array[DigestSize, byte]) = |
| 75 | + ## Finalize a Ripemd160 computation and output the |
| 76 | + ## message digest to the `digest` buffer. |
| 77 | + ## |
| 78 | + ## Security note: this does not clear the internal buffer. |
| 79 | + ## if sensitive content is used, use "ctx.clear()" |
| 80 | + ## and also make sure that the message(s) passed were stored |
| 81 | + ## in memory considered secure for your threat model. |
| 82 | + ctx.finalize(digest) |
| 83 | + |
| 84 | +func clear*(ctx: var Ripemd160Context) = |
| 85 | + ## Clear the context internal buffers |
| 86 | + # TODO: ensure compiler cannot optimize the code away |
| 87 | + ctx.reset() |
0 commit comments