renamed header for better consistency
[rofl0r-kripto.git] / lib / mode / ofb.c
blobc2b5f9d07250dc74608dbda15199bf77351ce3a9
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/ofb.h>
30 struct kripto_stream
32 kripto_stream_desc *desc;
33 const kripto_block *block;
34 unsigned int block_size;
35 uint8_t *prev;
36 unsigned int used;
39 static size_t ofb_crypt
41 kripto_stream *s,
42 const void *in,
43 void *out,
44 const size_t len
47 size_t i;
49 assert(in);
50 assert(out);
52 for(i = 0; i < len; i++)
54 if(s->used == s->block_size)
56 kripto_block_encrypt(s->block, s->prev, s->prev);
57 s->used = 0;
60 U8(out)[i] = CU8(in)[i] ^ s->prev[s->used++];
63 return i;
66 static size_t ofb_prng
68 kripto_stream *s,
69 void *out,
70 const size_t len
73 size_t i;
75 assert(out);
77 for(i = 0; i < len; i++)
79 if(s->used == s->block_size)
81 kripto_block_encrypt(s->block, s->prev, s->prev);
82 s->used = 0;
85 U8(out)[i] = s->prev[s->used++];
88 return i;
91 static void ofb_destroy(kripto_stream *s)
93 kripto_memwipe(s, sizeof(struct kripto_stream)
94 + sizeof(struct kripto_stream_desc)
95 + s->block_size
97 free(s);
100 static kripto_stream *ofb_create
102 const kripto_block *block,
103 const void *iv,
104 const unsigned int iv_len
107 kripto_stream *s;
108 kripto_block_desc *b;
109 struct kripto_stream_desc *stream;
111 assert(block);
113 b = kripto_block_get_desc(block);
115 assert(iv_len > kripto_block_size(b));
117 s = malloc(sizeof(struct kripto_stream)
118 + sizeof(struct kripto_stream_desc)
119 + kripto_block_size(b)
121 if(!s) return 0;
123 s->block_size = kripto_block_size(b);
125 stream = (struct kripto_stream_desc *)
126 ((uint8_t *)s + sizeof(struct kripto_stream));
128 s->prev = (uint8_t *)stream + sizeof(struct kripto_stream_desc);
130 stream->encrypt = &ofb_crypt;
131 stream->decrypt = &ofb_crypt;
132 stream->prng = &ofb_prng;
133 stream->create = 0;
134 stream->destroy = &ofb_destroy;
135 stream->max_key = kripto_block_max_key(b);
136 stream->max_iv = s->block_size;
138 s->desc = stream;
140 if(iv_len) memcpy(s->prev, iv, iv_len);
141 memset(s->prev + iv_len, 0, s->block_size - iv_len);
143 s->used = s->block_size;
144 s->block = block;
146 return s;
149 static const struct kripto_mode_desc ofb =
151 &ofb_create,
152 &kripto_block_size
155 kripto_mode_desc *const kripto_mode_ofb = &ofb;