3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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"
25 #include "rsaes_pkcs1v15.h"
30 #include "bigint_io.h"
34 #include "random_dummy.h"
36 uint16_t rsa_pkcs1v15_compute_padlength_B(bigint_t *modulus, uint16_t msg_length_B){
37 return bigint_get_first_set_bit(modulus) / 8 + 1 - msg_length_B - 3;
40 uint8_t rsa_encrypt_pkcs1v15(void *dest, uint16_t *out_length, const void *src,
41 uint16_t length_B, rsa_publickey_t *key, const void *pad){
44 pad_length = rsa_pkcs1v15_compute_padlength_B(&key->modulus, length_B);
47 cli_putstr_P(PSTR("\r\nERROR: pad_length<8; pad_length: "));
48 cli_hexdump_rev(&pad_length, 2);
50 return 2; /* message to long */
54 cli_putstr_P(PSTR("\r\nauto-generating pad ..."));
58 for(i=0; i<pad_length; ++i){
62 ((uint8_t*)dest)[i+2] = c;
66 cli_putstr_P(PSTR("\r\nsupplied pad: "));
67 cli_hexdump_block(pad, pad_length, 4, 16);
69 memcpy((uint8_t*)dest + 2, pad, pad_length);
71 ((uint8_t*)dest)[0] = 0x00;
72 ((uint8_t*)dest)[1] = 0x02;
73 ((uint8_t*)dest)[2+pad_length] = 0x00;
74 memcpy((uint8_t*)dest+3+pad_length, src, length_B);
76 x.length_W = (length_B+pad_length+3+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
78 cli_putstr_P(PSTR("\r\nx-data: "));
79 cli_hexdump_block(x.wordv, x.length_W * sizeof(bigint_word_t), 4, 16);
82 rsa_os2ip(&x, NULL, length_B + pad_length + 3);
84 rsa_i2osp(NULL, &x, out_length);
88 uint8_t rsa_decrypt_pkcs1v15(void *dest, uint16_t *out_length, const void *src,
89 uint16_t length_B, rsa_privatekey_t *key, void *pad){
91 uint16_t m_length, pad_length=0, idx=0;
93 rsa_os2ip(&x, src, length_B);
95 cli_putstr_P(PSTR("\r\ncalling rsa_dec() with bigint:"));
100 cli_putstr_P(PSTR("\r\nfinished rsa_dec() bigint:"));
101 bigint_print_hex(&x);
103 rsa_i2osp(NULL, &x, &m_length);
105 cli_putstr_P(PSTR("\r\ndecoded block:"));
106 cli_hexdump_block(x.wordv, m_length, 4, 16);
108 while(((uint8_t*)x.wordv)[idx]==0 && idx<m_length){
111 if(((uint8_t*)x.wordv)[idx]!=2 || idx>=m_length){
115 while(((uint8_t*)x.wordv)[idx+pad_length]!=0 && (idx+pad_length)<m_length){
118 if(pad_length<8 || (idx+pad_length)>=m_length){
121 *out_length = m_length - idx - pad_length - 1;
124 cli_putstr_P(PSTR("\r\npadding block:"));
125 cli_hexdump_block(((uint8_t*)x.wordv)+idx, pad_length, 4, 16);
126 cli_putstr_P(PSTR("\r\npad @ 0x"));
127 cli_hexdump_rev(&pad, 2);
128 cli_putstr_P(PSTR("\r\ndst @ 0x"));
129 cli_hexdump_rev(&dest, 2);
131 memcpy(pad, ((uint8_t*)x.wordv)+idx, pad_length);
133 memmove(dest, ((uint8_t*)x.wordv) + idx + pad_length + 1, m_length - idx - pad_length - 1);