]> git.cryptolib.org Git - arm-crypto-lib.git/blob - rsa/rsa_basic.c
updated bigint & rsa
[arm-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 #include "uart_lowlevel.h"
32 #endif
33
34 void rsa_enc(bigint_t* data, rsa_publickey_t* key){
35 /*
36         cli_putstr("\r\n -->rsa_enc()\r\n m = ");
37         bigint_print_hex(data);
38         cli_putstr("\r\n e = ");
39         bigint_print_hex(key->exponent);
40         cli_putstr("\r\n n = ");
41         bigint_print_hex(key->modulus);
42 */
43         bigint_expmod_u(data, data, key->exponent, key->modulus);
44 }
45
46 /*
47 (p,q,dp,dq,qinv)
48 m1 = c**dp % p
49 m2 = c**dq % q
50 h = (m1 - m2) * qinv % p
51 m = m2 + q * h
52 */
53
54 uint8_t rsa_dec_crt_mono(bigint_t* data, rsa_privatekey_t* key){
55         bigint_t m1, m2;
56         m1.wordv = malloc(key->components[0]->length_B * sizeof(bigint_word_t));
57         m2.wordv = malloc(key->components[1]->length_B * sizeof(bigint_word_t));
58         if(!m1.wordv || !m2.wordv){
59 #if DEBUG
60                 cli_putstr("\r\nERROR: OOM!");
61 #endif
62                 free(m1.wordv);
63                 free(m2.wordv);
64                 return 1;
65         }
66 #if DEBUG
67         cli_putstr("\r\nDBG: expmod m1 ...");
68 #endif
69         bigint_expmod_u(&m1, data, key->components[2], key->components[0]);
70 #if DEBUG
71         cli_putstr("expmod m2 ...");
72 #endif
73         bigint_expmod_u(&m2, data, key->components[3], key->components[1]);
74         bigint_sub_s(&m1, &m1, &m2);
75         while(BIGINT_NEG_MASK & m1.info){
76                 bigint_add_s(&m1, &m1, key->components[0]);
77         }
78
79 #if DEBUG
80         cli_putstr("\r\nDBG: reduce-mul ...");
81 #endif
82         bigint_reduce(&m1, key->components[0]);
83         bigint_mul_u(data, &m1, key->components[4]);
84         bigint_reduce(data, key->components[0]);
85         bigint_mul_u(data, data, key->components[1]);
86         bigint_add_u(data, data, &m2);
87         free(m1.wordv);
88         free(m2.wordv);
89         return 0;
90 }
91
92 uint8_t rsa_dec(bigint_t* data, rsa_privatekey_t* key){
93         if(key->n == 1){
94                 bigint_expmod_u(data, data, key->components[0], key->modulus);
95                 return 0;
96         }
97         if(key->n == 5){
98                 if (rsa_dec_crt_mono(data, key)){
99                         return 3;
100                 }
101                 return 0;
102         }
103         if(key->n<8 || (key->n-5)%3 != 0){
104                 return 1;
105         }
106         //rsa_dec_crt_multi(data, key, (key->n-5)/3);
107         return 2;
108 }
109
110 void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){
111         uint8_t off;
112         off = length_B % sizeof(bigint_word_t);
113         if(!data){
114                 if(off){
115                         dest->wordv = realloc(dest->wordv, length_B + sizeof(bigint_word_t) - off);
116                         memmove((uint8_t*)dest->wordv+off, dest->wordv, length_B);
117                         memset(dest->wordv, 0, off);
118                 }
119         }else{
120                 if(off){
121                         memcpy((uint8_t*)dest->wordv + off, data, length_B);
122                         memset(dest, 0, off);
123                 }else{
124                         memcpy(dest->wordv, data, length_B);
125                 }
126         }
127         dest->length_B = (length_B + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
128         bigint_changeendianess(dest);
129         bigint_adjust(dest);
130 }
131
132 void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){
133         *out_length_B = bigint_get_first_set_bit(src) / 8 + 1;
134         if(dest){
135                 uint16_t i;
136                 for(i=*out_length_B; i>0; --i){
137                         *((uint8_t*)dest) = ((uint8_t*)src->wordv)[i-1];
138                         dest = (uint8_t*)dest + 1;
139                 }
140         }else{
141                 uint8_t off;
142                 bigint_changeendianess(src);
143                 bigint_adjust(src);
144
145                 off = bigint_get_last_set_bit(src)/8;
146                 if(off){
147                         memmove(src->wordv, (uint8_t*)src->wordv+off, *out_length_B);
148                 }
149         }
150 }
151