4 This file is part of the ARM-Crypto-Lib.
5 Copyright (C) 2006-2012 Daniel Otte (daniel.otte@rub.de)
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "rsa_basic.h"
28 #include "rsaes_oaep.h"
30 #include "random_dummy.h"
32 #include "hfal/hfal_sha1.h"
40 mgf1_parameter_t mgf1_default_parameter = {
44 rsa_oaep_parameter_t rsa_oaep_default_parameter = {
47 &mgf1_default_parameter
50 rsa_label_t rsa_oaep_default_label = {
54 uint8_t rsa_encrypt_oaep(void* dest, uint16_t* out_length,
55 const void* src, uint16_t length_B,
56 rsa_publickey_t* key, const rsa_oaep_parameter_t *p,
57 const rsa_label_t* label, const void* seed){
60 p = &rsa_oaep_default_parameter;
63 label = &rsa_oaep_default_label;
65 uint16_t hv_len = (hfal_hash_getHashsize(p->hf)+7)/8;
66 if(length_B > bigint_length_B(&key->modulus) - 2*hv_len - 2){
67 /* message too long */
70 uint16_t buffer_len = bigint_length_B(&key->modulus);
72 cli_putstr("\r\n buffer_len = ");
73 cli_hexdump_rev(&buffer_len, 2);
74 cli_putstr("\r\n modulus_len = ");
75 cli_hexdump_rev(&key->modulus.length_W, 2);
77 uint8_t* buffer = (uint8_t*)dest;
79 /* the following needs some explanation:
80 * off is the offset which is used for compensating the effect of
81 * changeendian() when it operates on multi-byte words.
83 off = (sizeof(bigint_word_t) - (bigint_get_first_set_bit(&key->modulus)/8+1) % sizeof(bigint_word_t))
84 % (sizeof(bigint_word_t));
87 uint8_t* seed_buffer = buffer + 1;
88 uint16_t db_len = buffer_len - hv_len - 1;
89 uint8_t* db = seed_buffer + hv_len;
90 uint16_t maskbuffer_len = db_len>hv_len?db_len:hv_len;
91 uint8_t maskbuffer[maskbuffer_len];
94 memset(dest, 0, seed_buffer - buffer + off);
95 memset(db + hv_len, 0, db_len - hv_len - length_B -1);
96 hfal_hash_mem(p->hf, db, label->label, label->length_b);
97 db[db_len - length_B - 1] = 0x01;
98 memcpy(db+db_len - length_B, src, length_B);
100 memcpy(seed_buffer, seed, hv_len);
102 /* generate random seed */
104 return 2; /* ERROR: no random generator specified */
107 for(i=0; i<hv_len; ++i){
108 seed_buffer[i] = prng_get_byte();
112 cli_putstr("\r\n msg (raw, pre-feistel):\r\n");
113 cli_hexdump_block(dest, bigint_length_B(&key->modulus), 4, 16);
115 p->mgf(maskbuffer, seed_buffer, hv_len, db_len, p->mgf_parameter);
116 memxor(db, maskbuffer, db_len);
117 p->mgf(maskbuffer, db, db_len, hv_len, p->mgf_parameter);
118 memxor(seed_buffer, maskbuffer, hv_len);
120 cli_putstr("\r\n msg (raw, post-feistel):\r\n");
121 cli_hexdump_block(dest, bigint_length_B(&key->modulus), 4, 16);
124 x.length_W = key->modulus.length_W;
127 rsa_os2ip(&x, NULL, bigint_length_B(&key->modulus));
129 cli_putstr("\r\ninput-msg (pre enc):\r\n");
130 cli_hexdump_rev(&src, 2);
131 cli_hexdump_block(src, length_B, 4, 16);
135 cli_putstr("\r\ninput-msg (post enc):\r\n");
136 cli_hexdump_rev(&src, 2);
137 cli_hexdump_block(src, length_B, 4, 16);
139 rsa_i2osp(NULL, &x, out_length);
143 uint8_t rsa_decrypt_oaep(void* dest, uint16_t* out_length,
144 const void* src, uint16_t length_B,
145 rsa_privatekey_t* key, const rsa_oaep_parameter_t *p,
146 const rsa_label_t* label, void* seed){
148 // cli_putstr("\r\n -->rsa_decrypt_oaep()"); uart_flush(0);
150 label = &rsa_oaep_default_label;
153 p = &rsa_oaep_default_parameter;
155 uint16_t x_len, data_len;
157 uint16_t hv_len = hfal_hash_getHashsize(p->hf)/8;
158 uint8_t label_hv[hv_len];
159 uint16_t msg_len = bigint_get_first_set_bit(&key->modulus) / 8 + 1;
160 uint16_t db_len = msg_len - hv_len - 1;
161 uint8_t maskbuffer[db_len>hv_len?db_len:hv_len];
163 uint8_t *seed_buffer = dest;
164 uint8_t *db_buffer = seed_buffer + hv_len;
166 x_len = bigint_get_first_set_bit(&key->modulus)/8;
167 memset(dest, 0, bigint_length_B(&key->modulus) - length_B);
168 memcpy((uint8_t*)dest + bigint_length_B(&key->modulus) - length_B, src, length_B);
170 // cli_putc('a'); uart_flush(0);
173 x.length_W = key->modulus.length_W;
178 // cli_putc('b'); uart_flush(0);
179 rsa_os2ip(&x, NULL, bigint_length_B(&key->modulus));
181 cli_putstr_P(PSTR("\r\n rsa decrypting ..."));
185 cli_putstr_P(PSTR(" [done]"));
187 rsa_i2osp(NULL, &x, &data_len);
189 // cli_putstr("\r\n msg (raw, pre-move):\r\n");
190 // cli_hexdump_block(dest, bigint_length_B(key->modulus), 4, 16);
192 if(data_len > x_len){
196 cli_putstr("\r\n moving some bytes; x_len = ");
197 cli_hexdump_rev(&x_len, 2);
198 cli_putstr(" data_len = ");
199 cli_hexdump_rev(&data_len, 2);
202 if(x_len != data_len){
203 memmove((uint8_t*)dest + x_len - data_len, dest, data_len);
204 // cli_putstr(" (oh, not dead yet?!)");
206 memset(dest, 0, x_len - data_len);
209 hfal_hash_mem(p->hf, label_hv, label->label, label->length_b);
211 cli_putstr("\r\n msg (raw, pre-feistel):\r\n");
212 cli_hexdump_block(seed_buffer, bigint_length_B(key->modulus), 4, 16);
215 p->mgf(maskbuffer, db_buffer, db_len, hv_len, p->mgf_parameter);
216 memxor(seed_buffer, maskbuffer, hv_len);
217 p->mgf(maskbuffer, seed_buffer, hv_len, db_len, p->mgf_parameter);
218 memxor(db_buffer, maskbuffer, db_len);
220 if(memcmp(label_hv, db_buffer, hv_len)){
221 // cli_putstr("\r\nDBG: DB:\r\n");
222 // cli_hexdump_block(db_buffer, db_len, 4, 16);
227 while(db_buffer[hv_len + ps_len++] == 0)
231 if(db_buffer[hv_len + ps_len] != 1){
236 memcpy(seed, seed_buffer, hv_len);
239 msg_len = db_len - hv_len - 1 - ps_len;
240 memmove(dest, db_buffer + hv_len + ps_len + 1, msg_len);
242 *out_length = msg_len;