renamed header for better consistency
[rofl0r-kripto.git] / lib / mode / ecb.c
blobc1c05d385f2df33d3cbdda22d5ec03a486a5004b
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 <string.h>
17 #include <stdlib.h>
18 #include <assert.h>
20 #include <kripto/macros.h>
21 #include <kripto/memwipe.h>
22 #include <kripto/block.h>
23 #include <kripto/mode.h>
24 #include <kripto/mode_desc.h>
25 #include <kripto/stream.h>
26 #include <kripto/stream_desc.h>
28 #include <kripto/mode/ecb.h>
30 struct kripto_stream
32 kripto_stream_desc *desc;
33 const kripto_block *block;
34 unsigned int block_size;
37 static size_t ecb_encrypt
39 kripto_stream *s,
40 const void *pt,
41 void *ct,
42 const size_t len
45 size_t i;
47 if(len & (s->block_size - 1)) return 0;
49 for(i = 0; i < len; i += s->block_size)
50 kripto_block_encrypt(s->block, CU8(pt) + i, U8(ct) + i);
52 return i;
55 static size_t ecb_decrypt
57 kripto_stream *s,
58 const void *ct,
59 void *pt,
60 const size_t len
63 size_t i;
65 if(len & (s->block_size - 1)) return 0;
67 for(i = 0; i < len; i += s->block_size)
68 kripto_block_decrypt(s->block, CU8(ct) + i, U8(pt) + i);
70 return i;
73 static void ecb_destroy(kripto_stream *s)
75 kripto_memwipe(s, sizeof(kripto_stream)
76 + s->block_size
77 + sizeof(kripto_stream_desc)
79 free(s);
82 static unsigned int ecb_max_iv(kripto_block_desc *desc)
84 (void)desc;
86 return 0;
89 static kripto_stream *ecb_create
91 const kripto_block *block,
92 const void *iv,
93 const unsigned int iv_len
96 kripto_stream *s;
97 kripto_block_desc *b;
98 struct kripto_stream_desc *stream;
100 (void)iv;
101 (void)iv_len;
102 assert(block);
103 assert(!iv_len);
105 b = kripto_block_get_desc(block);
107 s = malloc(sizeof(kripto_stream)
108 + sizeof(kripto_stream_desc)
111 s->block_size = kripto_block_size(b);
113 stream = (struct kripto_stream_desc *)
114 ((uint8_t *)s + sizeof(kripto_stream));
116 stream->encrypt = &ecb_encrypt;
117 stream->decrypt = &ecb_decrypt;
118 stream->prng = 0;
119 stream->create = 0;
120 stream->destroy = &ecb_destroy;
121 stream->max_key = kripto_block_max_key(b);
122 stream->max_iv = 0;
124 s->desc = stream;
125 s->block = block;
127 return s;
130 static const kripto_mode_desc ecb =
132 &ecb_create,
133 &ecb_max_iv
136 kripto_mode_desc *const kripto_mode_ecb = &ecb;