]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal/bcal-ofb.c
optimizing norx32
[avr-crypto-lib.git] / bcal / bcal-ofb.c
1 /* bcal-ofb.c */
2 /*
3  This file is part of the AVR-Crypto-Lib.
4  Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
5
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdint.h>
21 #include <string.h>
22 #include "bcal-ofb.h"
23 #include "bcal-basic.h"
24 #include "memxor.h"
25
26 uint8_t bcal_ofb_init(const bcdesc_t *desc, const void *key, uint16_t keysize_b,
27         bcal_ofb_ctx_t *ctx)
28 {
29     ctx->desc = (bcdesc_t*) desc;
30     ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc) + 7) / 8;
31     ctx->in_block = malloc(ctx->blocksize_B);
32     if (ctx->in_block == NULL) {
33         return 0x11;
34     }
35     return bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx));
36 }
37
38 void bcal_ofb_free(bcal_ofb_ctx_t *ctx)
39 {
40     free(ctx->in_block);
41     bcal_cipher_free(&(ctx->cctx));
42 }
43
44 void bcal_ofb_loadIV(const void *iv, bcal_ofb_ctx_t *ctx)
45 {
46     if (iv) {
47         memcpy(ctx->in_block, iv, ctx->blocksize_B);
48     }
49 }
50
51 void bcal_ofb_encNext(void *block, bcal_ofb_ctx_t *ctx)
52 {
53     bcal_cipher_enc(ctx->in_block, &(ctx->cctx));
54     memxor(block, ctx->in_block, ctx->blocksize_B);
55 }
56
57 void bcal_ofb_decNext(void *block, bcal_ofb_ctx_t *ctx)
58 {
59     bcal_cipher_enc(ctx->in_block, &(ctx->cctx));
60     memxor(block, ctx->in_block, ctx->blocksize_B);
61 }
62
63 void bcal_ofb_encMsg(const void *iv, void *msg, uint32_t msg_len_b,
64         bcal_ofb_ctx_t *ctx)
65 {
66     uint16_t block_len_b;
67     block_len_b = ctx->blocksize_B * 8;
68     bcal_ofb_loadIV(iv, ctx);
69     while (msg_len_b > block_len_b) {
70         bcal_ofb_encNext(msg, ctx);
71         msg_len_b -= block_len_b;
72         msg = (uint8_t*) msg + ctx->blocksize_B;
73     }
74     bcal_cipher_enc(ctx->in_block, &(ctx->cctx));
75     ctx->in_block[msg_len_b / 8] = 0xff00 >> (msg_len_b & 7);
76     memxor(msg, ctx->in_block, (msg_len_b + 7) / 8);
77 }
78
79 void bcal_ofb_decMsg(const void *iv, void *msg, uint32_t msg_len_b,
80         bcal_ofb_ctx_t *ctx)
81 {
82     bcal_ofb_encMsg(iv, msg, msg_len_b, ctx);
83 }
84