3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2011 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
24 #include "bigint_io.h"
25 #include "rsa_basic.h"
33 void rsa_enc(bigint_t* data, const rsa_publickey_t* key){
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);
42 bigint_expmod_u(data, data, &key->exponent, &key->modulus);
49 h = (m1 - m2) * qinv % p
53 uint8_t rsa_dec_crt_mono(bigint_t* data, const rsa_privatekey_t* key){
55 m1.wordv = malloc((key->components[0].length_W /* + 1 */) * sizeof(bigint_word_t));
56 m2.wordv = malloc((key->components[1].length_W /* + 1 */) * sizeof(bigint_word_t));
57 if(!m1.wordv || !m2.wordv){
59 cli_putstr_P(PSTR("\r\nERROR: OOM!"));
66 cli_putstr_P(PSTR("\r\nDBG: expmod m1 ..."));
67 cli_putstr_P(PSTR("\r\nexpmod("));
68 bigint_print_hex(data);
70 bigint_print_hex(&(key->components[2]));
72 bigint_print_hex(&(key->components[0]));
73 cli_putstr_P(PSTR(") = "));
75 bigint_expmod_u(&m1, data, &(key->components[2]), &(key->components[0]));
77 bigint_print_hex(&m1);
78 cli_putstr_P(PSTR("expmod m2 ..."));
79 cli_putstr_P(PSTR("\r\nexpmod("));
80 bigint_print_hex(data);
82 bigint_print_hex(&(key->components[3]));
84 bigint_print_hex(&(key->components[1]));
85 cli_putstr_P(PSTR(") = "));
87 bigint_expmod_u(&m2, data, &(key->components[3]), &(key->components[1]));
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);
94 bigint_print_hex(&m2);
95 cli_putstr_P(PSTR(") = "));
97 bigint_sub_s(&m1, &m1, &m2);
99 bigint_print_hex(&m1);
101 while(BIGINT_NEG_MASK & m1.info){
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);
108 bigint_add_s(&m1, &m1, &(key->components[0]));
111 cli_putstr_P(PSTR("\r\nDBG: reduce-mul ..."));
112 cli_putstr_P(PSTR("\r\nreduce("));
113 bigint_print_hex(&m1);
115 bigint_print_hex(&(key->components[0]));
116 cli_putstr_P(PSTR(") = "));
118 bigint_reduce(&m1, &(key->components[0]));
120 bigint_print_hex(&m1);
121 cli_putstr_P(PSTR("\r\nmul("));
122 bigint_print_hex(&m1);
124 bigint_print_hex(&(key->components[4]));
125 cli_putstr_P(PSTR(") = "));
127 bigint_mul_u(data, &m1, &(key->components[4]));
129 bigint_print_hex(data);
130 cli_putstr_P(PSTR("\r\nreduce("));
131 bigint_print_hex(data);
133 bigint_print_hex(&(key->components[0]));
134 cli_putstr_P(PSTR(") = "));
136 bigint_reduce(data, &(key->components[0]));
138 bigint_print_hex(data);
139 cli_putstr_P(PSTR("\r\nmul("));
140 bigint_print_hex(data);
142 bigint_print_hex(&(key->components[1]));
143 cli_putstr_P(PSTR(") = "));
145 bigint_mul_u(data, data, &(key->components[1]));
147 bigint_print_hex(data);
148 cli_putstr_P(PSTR("\r\nadd("));
149 bigint_print_hex(data);
151 bigint_print_hex(&m2);
152 cli_putstr_P(PSTR(") = "));
154 bigint_add_u(data, data, &m2);
156 bigint_print_hex(data);
163 uint8_t rsa_dec(bigint_t* data, const rsa_privatekey_t* key){
165 bigint_expmod_u(data, data, &(key->components[0]), &key->modulus);
169 if (rsa_dec_crt_mono(data, key)){
174 if(key->n<8 || (key->n-5)%3 != 0){
177 //rsa_dec_crt_multi(data, key, (key->n-5)/3);
181 void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){
182 #if BIGINT_WORD_SIZE == 8
184 memcpy(dest->wordv, data, length_B);
186 dest->length_W = length_B;
189 off = (sizeof(bigint_word_t) - length_B % sizeof(bigint_word_t)) % sizeof(bigint_word_t);
191 cli_putstr_P(PSTR("\r\nDBG: off = 0x"));
192 cli_hexdump_byte(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);
201 memcpy((uint8_t*)dest->wordv + off, data, length_B);
203 memset(dest->wordv, 0, off);
206 dest->length_W = (length_B + off) / sizeof(bigint_word_t);
208 cli_putstr_P(PSTR("\r\nDBG: dest->length_W = 0x"));
209 cli_hexdump_rev(&(dest->length_W), 2);
213 bigint_changeendianess(dest);
217 void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){
218 #if BIGINT_WORD_SIZE == 8
220 uint8_t *e = src->wordv + src->length_W;
222 for(i=src->length_W; i>0; --i){
223 *((uint8_t*)dest) = *--e;
224 dest = (uint8_t*)dest + 1;
227 bigint_changeendianess(src);
230 *out_length_B = src->length_W;
232 *out_length_B = bigint_get_first_set_bit(src) / 8 + 1;
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;
241 bigint_changeendianess(src);
244 off = bigint_get_last_set_bit(src)/8;
246 memmove(src->wordv, (uint8_t*)src->wordv+off, *out_length_B);