keccak stream ciphers added
[rofl0r-kripto.git] / lib / stream / keccak1600.c
blobe1aa0f8e478f63af4895c15fb968a58323ff6fc8
1 /*
2 * Copyright (C) 2013 Gregor Pintar <grpintar@gmail.com>
4 * Permission is granted to deal in this work without any restriction,
5 * including unlimited rights to use, publicly perform, publish,
6 * reproduce, relicence, modify, merge, and/or distribute in any form,
7 * for any purpose, with or without fee, and by any means.
9 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind,
10 * to the utmost extent permitted by applicable law. In no event
11 * shall a licensor, author or contributor be held liable for any
12 * issues arising in any way out of dealing in the work.
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include <limits.h>
19 #include <kripto/cast.h>
20 #include <kripto/memwipe.h>
21 #include <kripto/hash.h>
22 #include <kripto/hash/keccak1600.h>
23 #include <kripto/stream.h>
24 #include <kripto/desc/stream.h>
26 #include <kripto/stream/keccak1600.h>
28 struct kripto_stream
30 const kripto_stream_desc *stream;
31 kripto_hash *hash;
34 static kripto_stream *keccak1600_recreate
36 kripto_stream *s,
37 unsigned int r,
38 const void *key,
39 unsigned int key_len,
40 const void *iv,
41 unsigned int iv_len
44 (void)kripto_hash_recreate(s->hash, r, key_len);
46 kripto_hash_input(s->hash, key, key_len);
47 kripto_hash_input(s->hash, iv, iv_len);
49 return s;
52 static void keccak1600_crypt
54 kripto_stream *s,
55 const void *in,
56 void *out,
57 size_t len
60 size_t i;
61 uint8_t buf[64];
62 unsigned int n;
64 for(i = 0, n = 64; i < len; i++)
66 if(n == 64)
68 if(len - i < 64)
69 kripto_hash_output(s->hash, buf, len - i);
70 else
71 kripto_hash_output(s->hash, buf, 64);
73 n = 0;
76 U8(out)[i] = CU8(in)[i] ^ buf[n++];
79 kripto_memwipe(buf, 64);
82 static void keccak1600_prng(kripto_stream *s, void *out, size_t len)
84 kripto_hash_output(s->hash, out, len);
87 static kripto_stream *keccak1600_create
89 const kripto_stream_desc *desc,
90 unsigned int r,
91 const void *key,
92 unsigned int key_len,
93 const void *iv,
94 unsigned int iv_len
97 kripto_stream *s;
99 (void)desc;
101 s = malloc(sizeof(kripto_stream));
102 if(!s) return 0;
104 s->stream = kripto_stream_keccak1600;
106 s->hash = kripto_hash_create(kripto_hash_keccak1600, r, key_len);
107 if(!s->hash)
109 free(s);
110 return 0;
113 kripto_hash_input(s->hash, key, key_len);
114 kripto_hash_input(s->hash, iv, iv_len);
116 return s;
119 static void keccak1600_destroy(kripto_stream *s)
121 kripto_hash_destroy(s->hash);
122 free(s);
125 static const kripto_stream_desc keccak1600 =
127 &keccak1600_create,
128 &keccak1600_recreate,
129 &keccak1600_crypt,
130 &keccak1600_crypt,
131 &keccak1600_prng,
132 &keccak1600_destroy,
134 99, /* max key */
135 UINT_MAX /* max iv */
138 const kripto_stream_desc *const kripto_stream_keccak1600 = &keccak1600;