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.
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>
32 kripto_stream_desc
*desc
;
33 const kripto_block
*block
;
34 unsigned int block_size
;
37 static size_t ecb_encrypt
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
);
55 static size_t ecb_decrypt
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
);
73 static void ecb_destroy(kripto_stream
*s
)
75 kripto_memwipe(s
, sizeof(kripto_stream
)
77 + sizeof(kripto_stream_desc
)
82 static unsigned int ecb_max_iv(kripto_block_desc
*desc
)
89 static kripto_stream
*ecb_create
91 const kripto_block
*block
,
93 const unsigned int iv_len
98 struct kripto_stream_desc
*stream
;
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
;
120 stream
->destroy
= &ecb_destroy
;
121 stream
->max_key
= kripto_block_max_key(b
);
130 static const kripto_mode_desc ecb
=
136 kripto_mode_desc
*const kripto_mode_ecb
= &ecb
;