]> git.cryptolib.org Git - avr-crypto-lib.git/blob - rsa/rsa_pkcs15.c
added rsa pkcs#1 v1.5 encryption
[avr-crypto-lib.git] / rsa / rsa_pkcs15.c
1 /* rsa_pkcs15.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
26 #define DEBUG 0
27
28 #if DEBUG
29 #include "bigint_io.h"
30 #include "cli.h"
31 #endif
32
33 #include "random_dummy.h"
34
35 uint16_t rsa_pkcs15_compute_padlength_B(bigint_t* modulus, uint16_t msg_length_B){
36         return bigint_get_first_set_bit(modulus) / 8 + 1 - msg_length_B - 3;
37 }
38
39 uint8_t rsa_encrypt_pkcs15(void* dest, uint16_t* out_length, const void* src,
40         uint16_t length_B, rsa_publickey_t* key, const void* pad){
41         int16_t pad_length;
42         bigint_t x;
43         pad_length = rsa_pkcs15_compute_padlength_B(key->modulus, length_B);
44         if(pad_length<8){
45 #if DEBUG
46                 cli_putstr_P(PSTR("\r\nERROR: pad_length<8; pad_length: "));
47                 cli_hexdump_rev(&pad_length, 2);
48 #endif
49                 return 2; /* message to long */
50         }
51         if(!pad){
52 #if DEBUG
53                 cli_putstr_P(PSTR("\r\nauto-generating pad ..."));
54 #endif
55                 uint16_t i;
56                 uint8_t c;
57                 for(i=0; i<pad_length; ++i){
58                         do{
59                                 c = prng_get_byte();
60                         }while(c==0);
61                         ((uint8_t*)dest)[i+2] = c;
62                 }
63         }else{
64 #if DEBUG
65                 cli_putstr_P(PSTR("\r\nsupplied pad: "));
66                 cli_hexdump_block(pad, pad_length, 4, 16);
67 #endif
68                 memcpy((uint8_t*)dest + 2, pad, pad_length);
69         }
70         ((uint8_t*)dest)[0] = 0x00;
71         ((uint8_t*)dest)[1] = 0x02;
72         ((uint8_t*)dest)[2+pad_length] = 0x00;
73         memcpy((uint8_t*)dest+3+pad_length, src, length_B);
74         x.wordv = dest;
75         x.length_B = (length_B+pad_length+3+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
76 #if DEBUG
77         cli_putstr_P(PSTR("\r\nx-data: "));
78         cli_hexdump_block(x.wordv, x.length_B * sizeof(bigint_word_t), 4, 16);
79 #endif
80         bigint_adjust(&x);
81         rsa_os2ip(&x, NULL, length_B+pad_length+3);
82         rsa_enc(&x, key);
83         rsa_i2osp(NULL, &x, out_length);
84         return 0;
85 }
86
87 uint8_t rsa_decrypt_pkcs15(void* dest, uint16_t* out_length, const void* src,
88         uint16_t length_B, rsa_privatekey_t* key, void* pad){
89         bigint_t x;
90         uint16_t m_length, pad_length=0, idx=0;
91         x.wordv = dest;
92         rsa_os2ip(&x, src, length_B);
93 #if DEBUG
94         cli_putstr_P(PSTR("\r\ncalling rsa_dec() ..."));
95 #endif
96         rsa_dec(&x, key);
97 #if DEBUG
98         cli_putstr_P(PSTR("\r\nfinished rsa_dec() ..."));
99 #endif
100         rsa_i2osp(NULL, &x, &m_length);
101 #if DEBUG
102         cli_putstr_P(PSTR("\r\ndecoded block:"));
103         cli_hexdump_block(x.wordv, m_length, 4, 16);
104 #endif
105         while(((uint8_t*)x.wordv)[idx]==0 && idx<m_length){
106                 ++idx;
107         }
108         if(((uint8_t*)x.wordv)[idx]!=2 || idx>=m_length){
109                 return 1;
110         }
111         ++idx;
112         while(((uint8_t*)x.wordv)[idx+pad_length]!=0  && (idx+pad_length)<m_length){
113                 ++pad_length;
114         }
115         if(pad_length<8 || (idx+pad_length)>=m_length){
116                 return 2;
117         }
118         *out_length = m_length - idx - pad_length - 1;
119         if(pad){
120 #if DEBUG
121                 cli_putstr_P(PSTR("\r\npadding block:"));
122                 cli_hexdump_block(((uint8_t*)x.wordv)+idx, pad_length, 4, 16);
123                 cli_putstr_P(PSTR("\r\npad @ 0x"));
124                 cli_hexdump_rev(&pad, 2);
125                 cli_putstr_P(PSTR("\r\ndst @ 0x"));
126                 cli_hexdump_rev(&dest, 2);
127 #endif
128                 memcpy(pad, ((uint8_t*)x.wordv)+idx, pad_length);
129         }
130         memmove(dest, ((uint8_t*)x.wordv) + idx + pad_length + 1, m_length - idx - pad_length - 1);
131
132         return 0;
133 }
134