]> git.cryptolib.org Git - avr-crypto-lib.git/blob - rsa/rsa_basic.c
present looks better now (using unverified testvectors)
[avr-crypto-lib.git] / rsa / rsa_basic.c
1 /* rsa_basic.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-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 <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "bigint.h"
24 #include "bigint_io.h"
25 #include "rsa_basic.h"
26
27 #define DEBUG 0
28
29 #if DEBUG
30 #include "cli.h"
31 #endif
32
33 void rsa_enc(bigint_t* data, rsa_publickey_t* key){
34 /*
35         cli_putstr_P(PSTR("\r\n -->rsa_enc()\r\n m = "));
36         bigint_print_hex(data);
37         cli_putstr_P(PSTR("\r\n e = "));
38         bigint_print_hex(key->exponent);
39         cli_putstr_P(PSTR("\r\n n = "));
40         bigint_print_hex(key->modulus);
41 */
42         bigint_expmod_u(data, data, &key->exponent, &key->modulus);
43 }
44
45 /*
46 (p,q,dp,dq,qinv)
47 m1 = c**dp % p
48 m2 = c**dq % q
49 h = (m1 - m2) * qinv % p
50 m = m2 + q * h
51 */
52
53 uint8_t rsa_dec_crt_mono(bigint_t* data, rsa_privatekey_t* key){
54         bigint_t m1, m2;
55         m1.wordv = malloc((key->components[0].length_B + 1) * sizeof(bigint_word_t));
56         m2.wordv = malloc((key->components[1].length_B + 1) * sizeof(bigint_word_t));
57         if(!m1.wordv || !m2.wordv){
58 #if DEBUG
59                 cli_putstr_P(PSTR("\r\nERROR: OOM!"));
60 #endif
61                 free(m1.wordv);
62                 free(m2.wordv);
63                 return 1;
64         }
65 #if DEBUG
66         cli_putstr_P(PSTR("\r\nDBG: expmod m1 ..."));
67         cli_putstr_P(PSTR("\r\nexpmod("));
68         bigint_print_hex(data);
69         cli_putc(',');
70         bigint_print_hex(&(key->components[2]));
71         cli_putc(',');
72         bigint_print_hex(&(key->components[0]));
73         cli_putstr_P(PSTR(") = "));
74 #endif
75         bigint_expmod_u(&m1, data, &(key->components[2]), &(key->components[0]));
76 #if DEBUG
77         bigint_print_hex(&m1);
78         cli_putstr_P(PSTR("expmod m2 ..."));
79         cli_putstr_P(PSTR("\r\nexpmod("));
80         bigint_print_hex(data);
81         cli_putc(',');
82         bigint_print_hex(&(key->components[3]));
83         cli_putc(',');
84         bigint_print_hex(&(key->components[1]));
85         cli_putstr_P(PSTR(") = "));
86 #endif
87         bigint_expmod_u(&m2, data, &(key->components[3]), &(key->components[1]));
88 #if DEBUG
89         bigint_print_hex(&m2);
90         cli_putstr_P(PSTR("\r\nDBG: sub ..."));
91         cli_putstr_P(PSTR("\r\nsub("));
92         bigint_print_hex(&m1);
93         cli_putc(',');
94         bigint_print_hex(&m2);
95         cli_putstr_P(PSTR(") = "));
96 #endif
97         bigint_sub_s(&m1, &m1, &m2);
98 #if DEBUG
99         bigint_print_hex(&m1);
100 #endif
101         while(BIGINT_NEG_MASK & m1.info){
102 #if DEBUG
103         cli_putstr_P(PSTR("\r\nDBG: adding "));
104         bigint_print_hex(key->components[0]);
105         cli_putstr_P(PSTR("\r\nDBG: to "));
106         bigint_print_hex(&m1);
107 #endif
108                 bigint_add_s(&m1, &m1, &(key->components[0]));
109         }
110 #if DEBUG
111         cli_putstr_P(PSTR("\r\nDBG: reduce-mul ..."));
112         cli_putstr_P(PSTR("\r\nreduce("));
113         bigint_print_hex(&m1);
114         cli_putc(',');
115         bigint_print_hex(&(key->components[0]));
116         cli_putstr_P(PSTR(") = "));
117 #endif
118         bigint_reduce(&m1, &(key->components[0]));
119 #if DEBUG
120         bigint_print_hex(&m1);
121         cli_putstr_P(PSTR("\r\nmul("));
122         bigint_print_hex(&m1);
123         cli_putc(',');
124         bigint_print_hex(&(key->components[4]));
125         cli_putstr_P(PSTR(") = "));
126 #endif
127         bigint_mul_u(data, &m1, &(key->components[4]));
128 #if DEBUG
129         bigint_print_hex(data);
130         cli_putstr_P(PSTR("\r\nreduce("));
131         bigint_print_hex(data);
132         cli_putc(',');
133         bigint_print_hex(&(key->components[0]));
134         cli_putstr_P(PSTR(") = "));
135 #endif
136         bigint_reduce(data, &(key->components[0]));
137 #if DEBUG
138         bigint_print_hex(data);
139         cli_putstr_P(PSTR("\r\nmul("));
140         bigint_print_hex(data);
141         cli_putc(',');
142         bigint_print_hex(&(key->components[1]));
143         cli_putstr_P(PSTR(") = "));
144 #endif
145         bigint_mul_u(data, data, &(key->components[1]));
146 #if DEBUG
147         bigint_print_hex(data);
148         cli_putstr_P(PSTR("\r\nadd("));
149         bigint_print_hex(data);
150         cli_putc(',');
151         bigint_print_hex(&m2);
152         cli_putstr_P(PSTR(") = "));
153 #endif
154         bigint_add_u(data, data, &m2);
155 #if DEBUG
156         bigint_print_hex(data);
157 #endif
158         free(m2.wordv);
159         free(m1.wordv);
160         return 0;
161 }
162
163 uint8_t rsa_dec(bigint_t* data, rsa_privatekey_t* key){
164         if(key->n == 1){
165                 bigint_expmod_u(data, data, &(key->components[0]), &key->modulus);
166                 return 0;
167         }
168         if(key->n == 5){
169                 if (rsa_dec_crt_mono(data, key)){
170                         return 3;
171                 }
172                 return 0;
173         }
174         if(key->n<8 || (key->n-5)%3 != 0){
175                 return 1;
176         }
177         //rsa_dec_crt_multi(data, key, (key->n-5)/3);
178         return 2;
179 }
180
181 void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){
182 #if BIGINT_WORD_SIZE == 8
183         if(data){
184                 memcpy(dest->wordv, data, length_B);
185         }
186         dest->length_B = length_B;
187 #else
188         uint8_t off;
189         off = (sizeof(bigint_word_t) - length_B % sizeof(bigint_word_t)) % sizeof(bigint_word_t);
190 #if DEBUG
191         cli_putstr_P(PSTR("\r\nDBG: off = 0x"));
192         cli_hexdump_byte(off);
193 #endif
194         if(!data){
195                 if(off){
196                         dest->wordv = realloc(dest->wordv, length_B + sizeof(bigint_word_t) - off);
197                         memmove((uint8_t*)dest->wordv+off, dest->wordv, length_B);
198                         memset(dest->wordv, 0, off);
199                 }
200         }else{
201                 memcpy((uint8_t*)dest->wordv + off, data, length_B);
202                 if(off){
203                         memset(dest->wordv, 0, off);
204                 }
205         }
206         dest->length_B = (length_B + off) / sizeof(bigint_word_t);
207 #if DEBUG
208         cli_putstr_P(PSTR("\r\nDBG: dest->length_B = 0x"));
209         cli_hexdump_rev(&(dest->length_B), 2);
210 #endif
211 #endif
212         dest->info = 0;
213         bigint_changeendianess(dest);
214         bigint_adjust(dest);
215 }
216
217 void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){
218 #if BIGINT_WORD_SIZE == 8
219         if(dest){
220                 uint8_t *e = src->wordv + src->length_B;
221                 uint16_t i;
222                 for(i=src->length_B; i>0; --i){
223                         *((uint8_t*)dest) = *--e;
224                         dest = (uint8_t*)dest + 1;
225                 }
226         }else{
227                 bigint_changeendianess(src);
228         }
229
230         *out_length_B = src->length_B;
231 #else
232         *out_length_B = bigint_get_first_set_bit(src) / 8 + 1;
233         if(dest){
234                 uint16_t i;
235                 for(i=*out_length_B; i>0; --i){
236                         *((uint8_t*)dest) = ((uint8_t*)src->wordv)[i-1];
237                         dest = (uint8_t*)dest + 1;
238                 }
239         }else{
240                 uint8_t off;
241                 bigint_changeendianess(src);
242                 bigint_adjust(src);
243
244                 off = bigint_get_last_set_bit(src)/8;
245                 if(off){
246                         memmove(src->wordv, (uint8_t*)src->wordv+off, *out_length_B);
247                 }
248         }
249 #endif
250 }
251