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 "rsa_basic.h"
29 #include "bigint_io.h"
33 #include "random_dummy.h"
35 uint16_t rsa_pkcs15_compute_padlength_B(bigint_t* modulus, uint16_t msg_length_B){
36 return bigint_get_first_set_bit(modulus) / 8 + 1 - msg_length_B - 3;
39 uint8_t rsa_encrypt_pkcs15(void* dest, uint16_t* out_length, const void* src,
40 uint16_t length_B, rsa_publickey_t* key, const void* pad){
43 pad_length = rsa_pkcs15_compute_padlength_B(key->modulus, length_B);
46 cli_putstr("\r\nERROR: pad_length<8; pad_length: ");
47 cli_hexdump_rev(&pad_length, 2);
49 return 2; /* message to long */
53 cli_putstr("\r\nauto-generating pad ...");
57 for(i=0; i<pad_length; ++i){
61 ((uint8_t*)dest)[i+2] = c;
65 cli_putstr("\r\nsupplied pad: ");
66 cli_hexdump_block(pad, pad_length, 4, 16);
68 memcpy((uint8_t*)dest + 2, pad, pad_length);
70 ((uint8_t*)dest)[0] = 0x00;
71 ((uint8_t*)dest)[1] = 0x02;
72 ((uint8_t*)dest)[2+pad_length] = 0x00;
73 memcpy((uint8_t*)dest+3+pad_length, src, length_B);
75 x.length_B = (length_B+pad_length+3+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
77 cli_putstr("\r\nx-data: ");
78 cli_hexdump_block(x.wordv, x.length_B * sizeof(bigint_word_t), 4, 16);
81 rsa_os2ip(&x, NULL, length_B+pad_length+3);
83 rsa_i2osp(NULL, &x, out_length);
87 uint8_t rsa_decrypt_pkcs15(void* dest, uint16_t* out_length, const void* src,
88 uint16_t length_B, rsa_privatekey_t* key, void* pad){
90 uint16_t m_length, pad_length=0, idx=0;
92 rsa_os2ip(&x, src, length_B);
94 cli_putstr("\r\ncalling rsa_dec() ...");
95 cli_putstr("\r\nencoded block (src.len = 0x");
96 cli_hexdump_rev(&length_B, 2);
98 cli_hexdump_block(x.wordv, x.length_B * sizeof(bigint_word_t), 4, 16);
102 cli_putstr("\r\nfinished rsa_dec() ...");
104 rsa_i2osp(NULL, &x, &m_length);
106 cli_putstr("\r\ndecoded block:");
107 cli_hexdump_block(x.wordv, m_length, 4, 16);
109 while(((uint8_t*)x.wordv)[idx]==0 && idx<m_length){
115 if(((uint8_t*)x.wordv)[idx]!=2){
120 while(((uint8_t*)x.wordv)[idx+pad_length]!=0 && (idx+pad_length)<m_length){
123 if(pad_length<8 || (idx+pad_length)>=m_length){
126 *out_length = m_length - idx - pad_length - 1;
129 cli_putstr("\r\npadding block:");
130 cli_hexdump_block(((uint8_t*)x.wordv)+idx, pad_length, 4, 16);
131 cli_putstr("\r\npad @ 0x");
132 cli_hexdump_rev(&pad, 2);
133 cli_putstr("\r\ndst @ 0x");
134 cli_hexdump_rev(&dest, 2);
136 memcpy(pad, ((uint8_t*)x.wordv)+idx, pad_length);
138 memmove(dest, ((uint8_t*)x.wordv) + idx + pad_length + 1, m_length - idx - pad_length - 1);