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