]> git.cryptolib.org Git - arm-crypto-lib.git/blob - scal/scal-basic.c
fixing some header comments
[arm-crypto-lib.git] / scal / scal-basic.c
1 /* scal-basic.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2011  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 <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include "streamcipher_descriptor.h"
24 #include "keysize_descriptor.h"
25 #include "scal-basic.h"
26
27 uint8_t scal_cipher_init(const scdesc_t* cipher_descriptor,
28                          const void* key, uint16_t keysize_b,
29                          const void* iv,  uint16_t ivsize_b, scgen_ctx_t* ctx){
30         ctx->buffer = NULL;
31         ctx->ctx = NULL;
32
33         if(!is_valid_keysize_P(cipher_descriptor->valid_keysize_desc, keysize_b)){
34                 return 1;
35         }
36         if(!is_valid_keysize_P(cipher_descriptor->valid_ivsize_desc, ivsize_b)){
37                 return 2;
38         }
39         uint8_t flags;
40         sc_init_fpt init_fpt;
41         flags = cipher_descriptor->flags;
42         ctx->desc_ptr = cipher_descriptor;
43         ctx->keysize = keysize_b;
44         ctx->ivsize  = ivsize_b;
45         ctx->ctx = malloc(cipher_descriptor->ctxsize_B);
46         if(ctx->ctx==NULL){
47                 return 3;
48         }
49         init_fpt = (cipher_descriptor->init);
50         switch(flags&SC_INIT_TYPE){
51                 case SC_INIT_TYPE_1:
52                         init_fpt.init1(key, ctx->ctx);
53                         break;
54                 case SC_INIT_TYPE_2:
55                         init_fpt.init2(key, iv, ctx->ctx);
56                         break;
57                 case SC_INIT_TYPE_3:
58                         init_fpt.init3(key, keysize_b, ctx->ctx);
59                         break;
60                 case SC_INIT_TYPE_4:
61                         init_fpt.init4(key, keysize_b, iv, ctx->ctx);
62                         break;
63                 case SC_INIT_TYPE_5:
64                         init_fpt.init5(key, keysize_b, iv, ivsize_b, ctx->ctx);
65                         break;
66                 default:
67                         return 4;
68         }
69         uint16_t blocksize_b;
70         blocksize_b = cipher_descriptor->gensize_b;
71         if(blocksize_b>8){
72                 ctx->buffer=malloc((blocksize_b+7)/8);
73                 if(ctx->buffer==NULL){
74                         return 5;
75                 }
76                 ctx->index=0;
77         }
78         return 0;
79 }
80
81
82 void scal_cipher_free(scgen_ctx_t* ctx){
83         if(ctx->buffer){
84                 free(ctx->buffer);
85         }
86         if(ctx->ctx){
87                 free(ctx->ctx);
88         }
89 }
90
91 uint8_t scal_cipher_gen_byte(scgen_ctx_t* ctx){
92         uint8_t flags;
93         uint16_t blocksize_b;
94         void_fpt gen_fpt;
95         flags = ctx->desc_ptr->flags;
96         blocksize_b = ctx->desc_ptr->gensize_b;
97         gen_fpt = (void_fpt)(ctx->desc_ptr->gen.genvoid);
98
99         if(blocksize_b==8){
100                 if((flags&SC_GEN_TYPE)==SC_GEN_TYPE_1){
101                         return ((sc_gen1_fpt)gen_fpt)(ctx->ctx);
102                 }else{
103                         uint8_t r;
104                         ((sc_gen2_fpt)gen_fpt)(&r, ctx->ctx);
105                         return r;
106                 }
107         }
108         if(blocksize_b<8){
109                 uint8_t r=0;
110                 uint8_t fill=0;
111                 do{
112                         r |= ((((sc_gen1_fpt)gen_fpt)(ctx->ctx))&(0xff<<(8-blocksize_b)))>>fill;
113                         fill += blocksize_b;
114                 }while(fill<8);
115                 return r;
116         }else{
117                 uint8_t r;
118                 if(ctx->index==0){
119                         ((sc_gen2_fpt)gen_fpt)(ctx->buffer, ctx->ctx);
120                         ctx->index = blocksize_b;
121                 }
122                 r=ctx->buffer[(blocksize_b-ctx->index)/8];
123                 ctx->index -= 8;
124                 return r;
125         }
126 }
127
128 void scal_cipher_gen_block(void* block, scgen_ctx_t* ctx){
129         uint8_t flags;
130 /*      uint16_t blocksize_b; */
131         sc_gen_fpt gen_fpt;
132         flags = ctx->desc_ptr->flags;
133 /*      blocksize_b = ctx->desc_ptr->gensize_b; */
134         gen_fpt = ctx->desc_ptr->gen;
135         if((flags&SC_GEN_TYPE)==SC_GEN_TYPE_1){
136                 *((uint8_t*)block) = gen_fpt.gen1(ctx->ctx);
137         }else{
138                 gen_fpt.gen2(block, ctx->ctx);
139         }
140 }
141
142 void scal_cipher_gen_fillblock(void* block, uint16_t blocksize_B, scgen_ctx_t* ctx){
143         while(blocksize_B){
144                 *((uint8_t*)block) = scal_cipher_gen_byte(ctx);
145                 block = (uint8_t*)block + 1;
146                 blocksize_B -= 1;
147         }
148 }
149
150 uint16_t scal_cipher_getBlocksize_b(const scdesc_t* desc){
151         uint16_t blocksize_b;
152         blocksize_b = desc->gensize_b;
153         return blocksize_b;
154 }
155
156 void* scal_cipher_getKeysizeDesc(const scdesc_t* desc){
157         return (void*)(desc->valid_keysize_desc);
158 }
159
160 void* scal_cipher_getIVsizeDesc(const scdesc_t* desc){
161         return (void*)(desc->valid_ivsize_desc);
162 }
163
164