]> git.cryptolib.org Git - avr-crypto-lib.git/blob - rsa/rsa_basic.c
adding RSA-OAEP
[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->modulus->length_B + 1) * sizeof(bigint_word_t));
56         m2.wordv = malloc(key->components[1]->length_B * sizeof(bigint_word_t));
57         if(!m1.wordv || !m2.wordv){
58 #if DEBUG
59                 cli_putstr_P(PSTR("\r\nERROR: OOM! (" __FILE__ ")"));
60 #endif
61                 free(m1.wordv);
62                 free(m2.wordv);
63                 return 1;
64         }
65 #if DEBUG
66                 cli_putstr_P(PSTR("\r\nexp_mod a + b "));
67 #endif
68         bigint_expmod_u(&m1, data, key->components[2], key->components[0]);
69         bigint_expmod_u(&m2, data, key->components[3], key->components[1]);
70 #if DEBUG
71         cli_putstr_P(PSTR("[done] "));
72 #endif
73         bigint_sub_s(&m1, &m1, &m2);
74 #if DEBUG
75         cli_putstr_P(PSTR("[done2] "));
76 #endif
77         while(BIGINT_NEG_MASK & m1.info){
78 #if DEBUG
79                 cli_putc(',');
80 #endif
81                 bigint_add_s(&m1, &m1, key->components[0]);
82         }
83 #if DEBUG
84                 cli_putstr_P(PSTR("\r\nreduce_mul "));
85 #endif
86         bigint_reduce(&m1, key->components[0]);
87         bigint_mul_u(&m1, &m1, key->components[4]);
88 #if DEBUG
89                 cli_putstr_P(PSTR("[done]"));
90 #endif
91         bigint_reduce(&m1, key->components[0]);
92         bigint_mul_u(&m1, &m1, key->components[1]);
93 #if DEBUG
94                 cli_putstr_P(PSTR(" [done]"));
95 #endif
96         bigint_add_u(data, &m1, &m2);
97         free(m1.wordv);
98         free(m2.wordv);
99         return 0;
100 }
101
102 uint8_t rsa_dec(bigint_t* data, rsa_privatekey_t* key){
103         if(key->n == 1){
104                 bigint_expmod_u(data, data, key->components[0], key->modulus);
105                 return 0;
106         }
107         if(key->n == 5){
108                 if (rsa_dec_crt_mono(data, key)){
109                         return 3;
110                 }
111                 return 0;
112         }
113         if(key->n<8 || (key->n-5)%3 != 0){
114                 return 1;
115         }
116         //rsa_dec_crt_multi(data, key, (key->n-5)/3);
117         return 2;
118 }
119
120 void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){
121         uint8_t off;
122         off = length_B % sizeof(bigint_word_t);
123         if(!data){
124                 if(off){
125                         dest->wordv = realloc(dest->wordv, length_B + sizeof(bigint_word_t) - off);
126                         memmove((uint8_t*)dest->wordv+off, dest->wordv, length_B);
127                         memset(dest->wordv, 0, off);
128                 }
129         }else{
130                 if(off){
131                         memcpy((uint8_t*)dest->wordv + off, data, length_B);
132                         memset(dest, 0, off);
133                 }else{
134                         memcpy(dest->wordv, data, length_B);
135                 }
136         }
137         dest->length_B = (length_B + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
138         bigint_changeendianess(dest);
139         bigint_adjust(dest);
140 }
141
142 void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){
143         *out_length_B = bigint_get_first_set_bit(src) / 8 + 1;
144         if(dest){
145                 uint16_t i;
146                 for(i=*out_length_B; i>0; --i){
147                         *((uint8_t*)dest) = ((uint8_t*)src->wordv)[i-1];
148                         dest = (uint8_t*)dest + 1;
149                 }
150         }else{
151                 uint8_t off;
152                 bigint_changeendianess(src);
153                 bigint_adjust(src);
154
155                 off = bigint_get_last_set_bit(src)/8;
156                 if(off){
157                         memmove(src->wordv, (uint8_t*)src->wordv+off, *out_length_B);
158                 }
159         }
160 }
161