]> git.cryptolib.org Git - avr-crypto-lib.git/blob - rsa/rsaes_pkcs1v15.c
fixing ecdsa signature generation (stupid me confused p and n)
[avr-crypto-lib.git] / rsa / rsaes_pkcs1v15.c
1 /* rsa_pkcs1v15.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2011 Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "bigint.h"
24 #include "rsa_basic.h"
25 #include "rsaes_pkcs1v15.h"
26
27 #define DEBUG 0
28
29 #if DEBUG
30 #include "bigint_io.h"
31 #include "cli.h"
32 #endif
33
34 #include "random_dummy.h"
35
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;
38 }
39
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){
42         int16_t pad_length;
43         bigint_t x;
44         pad_length = rsa_pkcs1v15_compute_padlength_B(&key->modulus, length_B);
45         if(pad_length<8){
46 #if DEBUG
47                 cli_putstr_P(PSTR("\r\nERROR: pad_length<8; pad_length: "));
48                 cli_hexdump_rev(&pad_length, 2);
49 #endif
50                 return 2; /* message to long */
51         }
52         if(!pad){
53 #if DEBUG
54                 cli_putstr_P(PSTR("\r\nauto-generating pad ..."));
55 #endif
56                 uint16_t i;
57                 uint8_t c;
58                 for(i=0; i<pad_length; ++i){
59                         do{
60                                 c = prng_get_byte();
61                         }while(c==0);
62                         ((uint8_t*)dest)[i+2] = c;
63                 }
64         }else{
65 #if DEBUG
66                 cli_putstr_P(PSTR("\r\nsupplied pad: "));
67                 cli_hexdump_block(pad, pad_length, 4, 16);
68 #endif
69                 memcpy((uint8_t*)dest + 2, pad, pad_length);
70         }
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);
75         x.wordv = dest;
76         x.length_W = (length_B+pad_length+3+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
77 #if DEBUG
78         cli_putstr_P(PSTR("\r\nx-data: "));
79         cli_hexdump_block(x.wordv, x.length_W * sizeof(bigint_word_t), 4, 16);
80 #endif
81         bigint_adjust(&x);
82         rsa_os2ip(&x, NULL, length_B + pad_length + 3);
83         rsa_enc(&x, key);
84         rsa_i2osp(NULL, &x, out_length);
85         return 0;
86 }
87
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){
90         bigint_t x;
91         uint16_t m_length, pad_length=0, idx=0;
92         x.wordv = dest;
93         rsa_os2ip(&x, src, length_B);
94 #if DEBUG
95         cli_putstr_P(PSTR("\r\ncalling rsa_dec() with bigint:"));
96         bigint_print_hex(&x);
97 #endif
98         rsa_dec(&x, key);
99 #if DEBUG
100         cli_putstr_P(PSTR("\r\nfinished rsa_dec() bigint:"));
101         bigint_print_hex(&x);
102 #endif
103         rsa_i2osp(NULL, &x, &m_length);
104 #if DEBUG
105         cli_putstr_P(PSTR("\r\ndecoded block:"));
106         cli_hexdump_block(x.wordv, m_length, 4, 16);
107 #endif
108         while(((uint8_t*)x.wordv)[idx]==0 && idx<m_length){
109                 ++idx;
110         }
111         if(((uint8_t*)x.wordv)[idx]!=2 || idx>=m_length){
112                 return 1;
113         }
114         ++idx;
115         while(((uint8_t*)x.wordv)[idx+pad_length]!=0  && (idx+pad_length)<m_length){
116                 ++pad_length;
117         }
118         if(pad_length<8 || (idx+pad_length)>=m_length){
119                 return 2;
120         }
121         *out_length = m_length - idx - pad_length - 1;
122         if(pad){
123 #if DEBUG
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);
130 #endif
131                 memcpy(pad, ((uint8_t*)x.wordv)+idx, pad_length);
132         }
133         memmove(dest, ((uint8_t*)x.wordv) + idx + pad_length + 1, m_length - idx - pad_length - 1);
134
135         return 0;
136 }
137