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"
30 #include "random_dummy.h"
32 #include "hfal/hfal_sha1.h"
35 #include "uart_lowlevel.h"
37 mgf1_parameter_t mgf1_default_parameter = {
41 rsa_oaep_parameter_t rsa_oaep_default_parameter = {
44 &mgf1_default_parameter
47 rsa_label_t rsa_oaep_default_label = {
51 uint8_t rsa_encrypt_oaep(void* dest, uint16_t* out_length,
52 const void* src, uint16_t length_B,
53 rsa_publickey_t* key, const rsa_oaep_parameter_t *p,
54 const rsa_label_t* label, const void* seed){
57 p = &rsa_oaep_default_parameter;
60 label = &rsa_oaep_default_label;
62 uint16_t hv_len = (hfal_hash_getHashsize(p->hf)+7)/8;
63 if(length_B > bigint_length_B(key->modulus) - 2*hv_len - 2){
64 /* message too long */
67 uint16_t buffer_len = bigint_length_B(key->modulus);
68 uint8_t* buffer = (uint8_t*)dest;
70 /* the following needs some explanation:
71 * off is the offset which is used for compensating the effect of
72 * changeendian() when it operates on multi-byte words.
74 off = (sizeof(bigint_word_t) -(bigint_get_first_set_bit(key->modulus)/8+1)%(sizeof(bigint_word_t)*8))
75 % (sizeof(bigint_word_t));
79 uint8_t* seed_buffer = buffer + 1;
80 uint16_t db_len = buffer_len - hv_len - 1;
81 uint8_t* db = seed_buffer + hv_len;
82 uint16_t maskbuffer_len = db_len>hv_len?db_len:hv_len;
83 uint8_t maskbuffer[maskbuffer_len];
86 memset(buffer, 0, seed_buffer - buffer);
87 memset(db + hv_len, 0, db_len - hv_len - length_B -1);
88 hfal_hash_mem(p->hf, db, label->label, label->length_b);
89 db[db_len - length_B - 1] = 0x01;
90 memcpy(db+db_len - length_B, src, length_B);
93 memcpy(seed_buffer, seed, hv_len);
95 /* generate random seed */
97 return 2; /* ERROR: no random generator specified */
100 for(i=0; i<hv_len; ++i){
101 seed_buffer[i] = prng_get_byte();
105 //void mgf1(void* dest, const void* seed, uint16_t seed_len_B, uint16_t out_length_B, const mgf1_parameter_t* p);
106 cli_hexdump_block(dest, buffer_len+off, 4, 8);
107 p->mgf(maskbuffer, seed_buffer, hv_len, db_len, p->mgf_parameter);
108 memxor(db, maskbuffer, db_len);
109 p->mgf(maskbuffer, db, db_len, hv_len, p->mgf_parameter);
110 memxor(seed_buffer, maskbuffer, hv_len);
111 cli_putstr("\r\ngot to 112\r\n");
115 x.length_B = key->modulus->length_B;
117 cli_putstr("\r\ngot to 118\r\n");
120 rsa_os2ip(&x, NULL, key->modulus->length_B * sizeof(bigint_word_t));
122 rsa_i2osp(NULL, &x, out_length);