]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal/bcal-cfb_byte.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / bcal / bcal-cfb_byte.c
1 /* bcal-cfb_byte.c */
2 /*
3  This file is part of the AVR-Crypto-Lib.
4  Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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-cfb_byte.h"
23 #include "bcal-basic.h"
24 #include "memxor.h"
25
26 uint8_t bcal_cfb_B_init(const bcdesc_t *desc, const void *key,
27         uint16_t keysize_b, uint16_t size_b, bcal_cfb_B_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     if (size_b & 7) {
36         return 0x12;
37     }
38     ctx->size_B = size_b / 8;
39     return bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx));
40 }
41
42 void bcal_cfb_B_free(bcal_cfb_B_ctx_t *ctx)
43 {
44     free(ctx->in_block);
45     bcal_cipher_free(&(ctx->cctx));
46 }
47
48 void bcal_cfb_B_loadIV(const void *iv, bcal_cfb_B_ctx_t *ctx)
49 {
50     if (iv) {
51         memcpy(ctx->in_block, iv, ctx->blocksize_B);
52     }
53 }
54
55 void bcal_cfb_B_encNext(void *block, bcal_cfb_B_ctx_t *ctx)
56 {
57     uint8_t tmp[ctx->blocksize_B];
58     memcpy(tmp, ctx->in_block, ctx->blocksize_B);
59     bcal_cipher_enc(tmp, &(ctx->cctx));
60     memxor(block, tmp, ctx->size_B);
61     memmove(ctx->in_block, ctx->in_block + ctx->size_B, ctx->blocksize_B
62             - ctx->size_B);
63     memcpy(ctx->in_block + ctx->blocksize_B - ctx->size_B, block, ctx->size_B);
64 }
65
66 void bcal_cfb_B_decNext(void *block, bcal_cfb_B_ctx_t *ctx)
67 {
68     uint8_t tmp[ctx->blocksize_B];
69     uint8_t xblock[ctx->size_B];
70     memcpy(xblock, block, ctx->size_B);
71     memcpy(tmp, ctx->in_block, ctx->blocksize_B);
72     bcal_cipher_enc(tmp, &(ctx->cctx));
73     memxor(block, tmp, ctx->size_B);
74     memmove(ctx->in_block, ctx->in_block + ctx->size_B, ctx->blocksize_B
75             - ctx->size_B);
76     memcpy(ctx->in_block + ctx->blocksize_B - ctx->size_B, xblock, ctx->size_B);
77 }
78
79 void bcal_cfb_B_encMsg(const void *iv, void *msg, uint16_t msg_blocks,
80         bcal_cfb_B_ctx_t *ctx)
81 {
82     bcal_cfb_B_loadIV(iv, ctx);
83     while (msg_blocks--) {
84         bcal_cfb_B_encNext(msg, ctx);
85         msg = (uint8_t*) msg + ctx->size_B;
86     }
87 }
88
89 void bcal_cfb_B_decMsg(const void *iv, void *msg, uint16_t msg_blocks,
90         bcal_cfb_B_ctx_t *ctx)
91 {
92     bcal_cfb_B_loadIV(iv, ctx);
93     while (msg_blocks--) {
94         bcal_cfb_B_decNext(msg, ctx);
95         msg = (uint8_t*) msg + ctx->size_B;
96     }
97 }
98