]> git.cryptolib.org Git - arm-crypto-lib.git/blob - rsa/rsa_oaep.c
introducing RSA-OAEP (can encrypt one message correctly)
[arm-crypto-lib.git] / rsa / rsa_oaep.c
1
2 /* rsa_oaep.c */
3 /*
4     This file is part of the ARM-Crypto-Lib.
5     Copyright (C) 2006-2012 Daniel Otte (daniel.otte@rub.de)
6
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.
11
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.
16
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/>.
19 */
20
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "memxor.h"
25 #include "mgf1.h"
26 #include "bigint.h"
27 #include "rsa_basic.h"
28 #include "rsa_oaep.h"
29
30 #include "random_dummy.h"
31
32 #include "hfal/hfal_sha1.h"
33
34 #include "cli.h"
35 #include "uart_lowlevel.h"
36
37 mgf1_parameter_t mgf1_default_parameter = {
38                 &sha1_desc
39 };
40
41 rsa_oaep_parameter_t rsa_oaep_default_parameter = {
42                 mgf1,
43                 &sha1_desc,
44                 &mgf1_default_parameter
45 };
46
47 rsa_label_t rsa_oaep_default_label = {
48                 0, NULL
49 };
50
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){
55
56         if(!p){
57                 p = &rsa_oaep_default_parameter;
58         }
59         if(!label){
60                 label = &rsa_oaep_default_label;
61         }
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 */
65                 return 1;
66         }
67         uint16_t buffer_len = bigint_length_B(key->modulus);
68         uint8_t* buffer = (uint8_t*)dest;
69         uint8_t off;
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.
73          * */
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));
76         uart_flush(0);
77         buffer += off;
78     buffer_len -= off;
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];
84         bigint_t x;
85
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);
91         uart_flush(0);
92         if(seed){
93                 memcpy(seed_buffer, seed, hv_len);
94         }else{
95                 /* generate random seed */
96                 if(!prng_get_byte){
97                         return 2; /* ERROR: no random generator specified */
98                 }
99                 uint16_t i;
100                 for(i=0; i<hv_len; ++i){
101                         seed_buffer[i] = prng_get_byte();
102                 }
103         }
104
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");
112         uart_flush(0);
113
114         x.wordv = dest;
115         x.length_B = key->modulus->length_B;
116         bigint_adjust(&x);
117         cli_putstr("\r\ngot to 118\r\n");
118         uart_flush(0);
119
120         rsa_os2ip(&x, NULL, key->modulus->length_B * sizeof(bigint_word_t));
121         rsa_enc(&x, key);
122         rsa_i2osp(NULL, &x, out_length);
123         return 0;
124 }