-
Notifications
You must be signed in to change notification settings - Fork 29
/
stubs-aes.c
69 lines (60 loc) · 2.66 KB
/
stubs-aes.c
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
/***********************************************************************/
/* */
/* The Cryptokit library */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 2002 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. */
/* */
/***********************************************************************/
/* $Id: stubs-aes.c 53 2010-08-30 10:53:00Z gildor-admin $ */
/* Stub code for AES */
#include "rijndael-alg-fst.h"
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#define Cooked_key_NR_offset ((4 * (MAXNR + 1)) * sizeof(u32))
#define Cooked_key_size (Cooked_key_NR_offset + 1)
CAMLprim value camlpdf_caml_aes_cook_encrypt_key(value key)
{
CAMLparam1(key);
value ckey = caml_alloc_string(Cooked_key_size);
int nr =
camlpdf_rijndaelKeySetupEnc((u32 *) String_val(ckey),
(const u8 *) String_val(key),
8 * caml_string_length(key));
Byte(ckey, Cooked_key_NR_offset) = nr;
CAMLreturn(ckey);
}
CAMLprim value camlpdf_caml_aes_cook_decrypt_key(value key)
{
CAMLparam1(key);
value ckey = caml_alloc_string(Cooked_key_size);
int nr =
camlpdf_rijndaelKeySetupDec((u32 *) String_val(ckey),
(const u8 *) String_val(key),
8 * caml_string_length(key));
Byte(ckey, Cooked_key_NR_offset) = nr;
CAMLreturn(ckey);
}
CAMLprim value camlpdf_caml_aes_encrypt(value ckey, value src, value src_ofs,
value dst, value dst_ofs)
{
camlpdf_rijndaelEncrypt((const u32 *) String_val(ckey),
Byte(ckey, Cooked_key_NR_offset),
(const u8 *) &Byte(src, Long_val(src_ofs)),
(u8 *) &Byte(dst, Long_val(dst_ofs)));
return Val_unit;
}
CAMLprim value camlpdf_caml_aes_decrypt(value ckey, value src, value src_ofs,
value dst, value dst_ofs)
{
camlpdf_rijndaelDecrypt((const u32 *) String_val(ckey),
Byte(ckey, Cooked_key_NR_offset),
(const u8 *) &Byte(src, Long_val(src_ofs)),
(u8 *) &Byte(dst, Long_val(dst_ofs)));
return Val_unit;
}