summaryrefslogtreecommitdiff
path: root/src/stubs-blake2.c
blob: 2ccd1d386babbfbf7994ce94b0977a0e4463e800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/***********************************************************************/
/*                                                                     */
/*                      The Cryptokit library                          */
/*                                                                     */
/*              Xavier Leroy, Collège de France and Inria              */
/*                                                                     */
/*  Copyright 2020 Institut National de Recherche en Informatique et   */
/*  en Automatique.  All rights reserved.  This file is distributed    */
/*  under the terms of the GNU Library General Public License, with    */
/*  the special exception on linking described in file LICENSE.        */
/*                                                                     */
/***********************************************************************/

#include <stdint.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include "blake2.h"

#define blake2b_val(v) ((struct blake2b *) String_val(v))

CAMLprim value caml_blake2b_init(value hashlen, value key)
{
  value ctx = caml_alloc_string(sizeof(struct blake2b));
  blake2b_init(blake2b_val(ctx),
               Int_val(hashlen),
               caml_string_length(key), &Byte_u(key, 0));
  return ctx;
}

CAMLprim value caml_blake2b_update(value ctx, value src, value ofs, value len)
{
  blake2b_add_data(blake2b_val(ctx), 
                   &Byte_u(src, Long_val(ofs)), Long_val(len));
  return Val_unit;
}

CAMLprim value caml_blake2b_final(value ctx, value hashlen)
{
  CAMLparam1(ctx);
  CAMLlocal1(res);
  int len = Int_val(hashlen);
  res = caml_alloc_string(len);
  blake2b_final(blake2b_val(ctx), len, &Byte_u(res, 0));
  CAMLreturn(res);
}

#define blake2s_val(v) ((struct blake2s *) String_val(v))

CAMLprim value caml_blake2s_init(value hashlen, value key)
{
  value ctx = caml_alloc_string(sizeof(struct blake2s));
  blake2s_init(blake2s_val(ctx),
               Int_val(hashlen),
               caml_string_length(key), &Byte_u(key, 0));
  return ctx;
}

CAMLprim value caml_blake2s_update(value ctx, value src, value ofs, value len)
{
  blake2s_add_data(blake2s_val(ctx), 
                   &Byte_u(src, Long_val(ofs)), Long_val(len));
  return Val_unit;
}

CAMLprim value caml_blake2s_final(value ctx, value hashlen)
{
  CAMLparam1(ctx);
  CAMLlocal1(res);
  int len = Int_val(hashlen);
  res = caml_alloc_string(len);
  blake2s_final(blake2s_val(ctx), len, &Byte_u(res, 0));
  CAMLreturn(res);
}