]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal/bcal-ctr.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / bcal / bcal-ctr.c
1 /* bcal-ctr.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-basic.h"
23 #include "bcal-ctr.h"
24 #include "memxor.h"
25
26 static void increment_be(void *block, uint8_t size_B)
27 {
28     uint16_t c = 1;
29     do {
30         --size_B;
31         c += ((uint8_t*) block)[size_B];
32         ((uint8_t*) block)[size_B] = (uint8_t) c;
33         c >>= 8;
34     } while (size_B);
35 }
36
37 uint8_t bcal_ctr_init(const bcdesc_t *desc, const void *key, uint16_t keysize_b,
38         inc_fp_t inc_func, bcal_ctr_ctx_t *ctx)
39 {
40     ctx->desc = (bcdesc_t*) desc;
41     if (inc_func) {
42         ctx->inc_func = inc_func;
43     } else {
44         ctx->inc_func = increment_be;
45     }
46     ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc) + 7) / 8;
47     ctx->in_block = malloc(ctx->blocksize_B);
48     if (ctx->in_block == NULL) {
49         return 0x11;
50     }
51     return bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx));
52 }
53
54 void bcal_ctr_free(bcal_ctr_ctx_t *ctx)
55 {
56     free(ctx->in_block);
57     bcal_cipher_free(&(ctx->cctx));
58 }
59
60 void bcal_ctr_loadIV(const void *iv, bcal_ctr_ctx_t *ctx)
61 {
62     if (iv) {
63         memcpy(ctx->in_block, iv, ctx->blocksize_B);
64     }
65 }
66
67 void bcal_ctr_encNext(void *block, bcal_ctr_ctx_t *ctx)
68 {
69     uint8_t tmp[ctx->blocksize_B];
70     memcpy(tmp, ctx->in_block, ctx->blocksize_B);
71     bcal_cipher_enc(tmp, &(ctx->cctx));
72     memxor(block, tmp, ctx->blocksize_B);
73     ctx->inc_func(ctx->in_block, ctx->blocksize_B);
74 }
75
76 void bcal_ctr_decNext(void *block, bcal_ctr_ctx_t *ctx)
77 {
78     bcal_ctr_encNext(block, ctx);
79 }
80
81 void bcal_ctr_encMsg(const void *iv, void *msg, uint32_t msg_len_b,
82         bcal_ctr_ctx_t *ctx)
83 {
84     bcal_ctr_loadIV(iv, ctx);
85     uint16_t blocksize_b;
86     blocksize_b = ctx->blocksize_B * 8;
87     while (msg_len_b > blocksize_b) {
88         bcal_ctr_encNext(msg, ctx);
89         msg_len_b -= blocksize_b;
90         msg = (uint8_t*) msg + ctx->blocksize_B;
91     }
92     uint8_t tmp[ctx->blocksize_B];
93     memcpy(tmp, ctx->in_block, ctx->blocksize_B);
94     bcal_cipher_enc(tmp, &(ctx->cctx));
95     ctx->inc_func(ctx->in_block, ctx->blocksize_B);
96     tmp[msg_len_b / 8] = 0xff00 >> (msg_len_b & 7);
97     memxor(msg, tmp, (msg_len_b + 7) / 8);
98 }
99
100 void bcal_ctr_decMsg(const void *iv, void *msg, uint32_t msg_len_b,
101         bcal_ctr_ctx_t *ctx)
102 {
103     bcal_ctr_encMsg(iv, msg, msg_len_b, ctx);
104 }
105