Skip to content

Commit

Permalink
Merge pull request #77 from clecat/hash-whirlpool
Browse files Browse the repository at this point in the history
Hash whirlpool
  • Loading branch information
dinosaure authored Nov 9, 2018
2 parents b2764d2 + 169e4b5 commit d1f7d86
Show file tree
Hide file tree
Showing 14 changed files with 1,684 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Digestif is a toolbox which implements hashes:
* SHA256
* SHA384
* SHA512
* WHIRLPOOL
* BLAKE2B
* BLAKE2S
* RIPEMD160
Expand Down
13 changes: 13 additions & 0 deletions src-c/digestif.ml
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,17 @@ module SHA512 : S with type kind = [`SHA512] =
let kind = `SHA512
end)

module WHIRLPOOL : S with type kind = [`WHIRLPOOL] =
Make
(Native.WHIRLPOOL)
(struct
let digest_size, block_size = 64, 64

type kind = [`WHIRLPOOL]

let kind = `WHIRLPOOL
end)

module BLAKE2B : sig
include S with type kind = [`BLAKE2B]
module Keyed : MAC with type t = t
Expand Down Expand Up @@ -537,6 +548,7 @@ let module_of : type k. k hash -> (module S with type kind = k) =
| SHA256 -> (module SHA256)
| SHA384 -> (module SHA384)
| SHA512 -> (module SHA512)
| WHIRLPOOL -> (module WHIRLPOOL)
| BLAKE2B digest_size -> (
match Hashtbl.find b2b digest_size with
| exception Not_found ->
Expand Down Expand Up @@ -688,6 +700,7 @@ let of_sha224 hash = of_raw_string sha224 (SHA224.to_raw_string hash)
let of_sha256 hash = of_raw_string sha256 (SHA256.to_raw_string hash)
let of_sha384 hash = of_raw_string sha384 (SHA384.to_raw_string hash)
let of_sha512 hash = of_raw_string sha512 (SHA512.to_raw_string hash)
let of_whirlpool hash = of_raw_string whirlpool (WHIRLPOOL.to_raw_string hash)

let of_blake2b hash =
of_raw_string (blake2b BLAKE2B.digest_size) (BLAKE2B.to_raw_string hash)
Expand Down
33 changes: 33 additions & 0 deletions src-c/digestif_native.ml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,39 @@ module SHA512 = struct
external ctx_size : unit -> int = "caml_digestif_sha512_ctx_size" [@@noalloc]
end

module WHIRLPOOL = struct
type kind = [`WHIRLPOOL]

module Bigstring = struct
external init : ctx -> unit = "caml_digestif_whirlpool_ba_init" [@@noalloc]

external update :
ctx -> ba -> off -> size -> unit
= "caml_digestif_whirlpool_ba_update"

external finalize :
ctx -> ba -> off -> unit
= "caml_digestif_whirlpool_ba_finalize"
[@@noalloc]
end

module Bytes = struct
external init : ctx -> unit = "caml_digestif_whirlpool_st_init" [@@noalloc]

external update :
ctx -> st -> off -> size -> unit
= "caml_digestif_whirlpool_st_update"
[@@noalloc]

external finalize :
ctx -> st -> off -> unit
= "caml_digestif_whirlpool_st_finalize"
[@@noalloc]
end

external ctx_size : unit -> int = "caml_digestif_whirlpool_ctx_size" [@@noalloc]
end

module BLAKE2B = struct
type kind = [`BLAKE2B]

Expand Down
2 changes: 1 addition & 1 deletion src-c/native/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(library
(name rakia)
(public_name digestif.rakia)
(c_names blake2b blake2s md5 ripemd160 sha1 sha256 sha512 misc stubs)
(c_names blake2b blake2s md5 ripemd160 sha1 sha256 sha512 whirlpool misc stubs)
(c_flags (-I.)))
3 changes: 2 additions & 1 deletion src-c/native/freestanding/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(public_name digestif.rakia.freestanding)
(optional)
(libraries ocaml-freestanding)
(c_names blake2b blake2s md5 ripemd160 sha1 sha256 sha512 misc stubs)
(c_names blake2b blake2s md5 ripemd160 sha1 sha256 sha512 whirlpool misc stubs)
(c_flags (:include cflags.sexp)))

(rule (copy# ../blake2b.c blake2b.c))
Expand All @@ -13,6 +13,7 @@
(rule (copy# ../sha1.c sha1.c))
(rule (copy# ../sha256.c sha256.c))
(rule (copy# ../sha512.c sha512.c))
(rule (copy# ../whirlpool.c whirlpool.c))
(rule (copy# ../misc.c misc.c))
(rule (copy# ../stubs.c stubs.c))

Expand Down
2 changes: 2 additions & 0 deletions src-c/native/stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "sha1.h"
#include "sha256.h"
#include "sha512.h"
#include "whirlpool.h"
#include "blake2b.h"
#include "blake2s.h"
#include "ripemd160.h"
Expand Down Expand Up @@ -78,6 +79,7 @@ __define_hash (sha224, SHA224)
__define_hash (sha256, SHA256)
__define_hash (sha384, SHA384)
__define_hash (sha512, SHA512)
__define_hash (whirlpool, WHIRLPOOL)
__define_hash (blake2b, BLAKE2B)
__define_hash (blake2s, BLAKE2S)
__define_hash (rmd160, RMD160)
Expand Down
729 changes: 729 additions & 0 deletions src-c/native/whirlpool.c

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions src-c/native/whirlpool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* whirlpool.c - an implementation of the Whirlpool Hash Function.
*
* Copyright: 2009-2012 Aleksey Kravchenko <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
*
* Documentation:
* P. S. L. M. Barreto, V. Rijmen, ``The Whirlpool hashing function,''
* NESSIE submission, 2000 (tweaked version, 2001)
*
* The algorithm is named after the Whirlpool Galaxy in Canes Venatici.
*/
#ifndef CRYPTOHASH_WHIRLPOOL_H
#define CRYPTOHASH_WHIRLPOOL_H

#include <stdint.h>

struct whirlpool_ctx
{
uint64_t sz;
uint8_t buf[64];
uint64_t h[8];
};

#define WHIRLPOOL_DIGEST_SIZE 64
#define WHIRLPOOL_CTX_SIZE sizeof(struct whirlpool_ctx)

void digestif_whirlpool_init(struct whirlpool_ctx* ctx);
void digestif_whirlpool_update(struct whirlpool_ctx* ctx, uint8_t *data, uint32_t len);
void digestif_whirlpool_finalize(struct whirlpool_ctx* ctx, uint8_t *out);

#endif
3 changes: 2 additions & 1 deletion src-c/native/xen/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(public_name digestif.rakia.xen)
(optional)
(libraries mirage-xen-posix)
(c_names blake2b blake2s md5 ripemd160 sha1 sha256 sha512 misc stubs)
(c_names blake2b blake2s md5 ripemd160 sha1 sha256 sha512 whirlpool misc stubs)
(c_flags (:include cflags.sexp)))

(rule (copy# ../blake2b.c blake2b.c))
Expand All @@ -13,6 +13,7 @@
(rule (copy# ../sha1.c sha1.c))
(rule (copy# ../sha256.c sha256.c))
(rule (copy# ../sha512.c sha512.c))
(rule (copy# ../whirlpool.c whirlpool.c))
(rule (copy# ../misc.c misc.c))
(rule (copy# ../stubs.c stubs.c))

Expand Down
Loading

0 comments on commit d1f7d86

Please sign in to comment.