]> git.cryptolib.org Git - arm-crypto-lib.git/blob - rsa/rsa_pkcs15.c
a lot of fixing ...
[arm-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 #include "bigint_io.h"
27 #include "cli.h"
28
29 #include "random_dummy.h"
30
31 uint8_t rsa_encrypt_pkcs15(void* dest, uint16_t* out_length, const void* src,
32         uint16_t length_B, rsa_publickey_t* key, const void* pad){
33         int16_t pad_length;
34         bigint_t x;
35         pad_length = (bigint_get_first_set_bit(key->modulus) + 7) / 8 - length_B - 3;
36         if(pad_length<8){
37                 cli_putstr("\r\nERROR: pad_length<8; pad_length: ");
38                 cli_hexdump_rev(&pad_length, 2);
39                 return 2; /* message to long */
40         }
41         if(!pad){
42                 uint16_t i;
43                 uint8_t c;
44                 for(i=0; i<pad_length; ++i){
45                         do{
46                                 c = prng_get_byte();
47                         }while(c==0);
48                         ((uint8_t*)dest)[i+2] = c;
49                 }
50         }else{
51                 memcpy((uint8_t*)dest + 2, pad, pad_length);
52         }
53         ((uint8_t*)dest)[0] = 0x00;
54         ((uint8_t*)dest)[1] = 0x02;
55         ((uint8_t*)dest)[2+pad_length] = 0x00;
56         memcpy((uint8_t*)dest+3+pad_length, src, length_B);
57         x.wordv = dest;
58         x.length_B = (length_B+pad_length+3+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
59         bigint_adjust(&x);
60         rsa_os2ip(&x, NULL, length_B+pad_length+3);
61         rsa_enc(&x, key);
62         rsa_i2osp(NULL, &x, out_length);
63         return 0;
64 }
65
66 uint8_t rsa_decrypt_pkcs15(void* dest, uint16_t* out_length, const void* src,
67         uint16_t length_B, rsa_privatekey_t* key, void* pad){
68         bigint_t x;
69         uint16_t m_length, pad_length=0, idx=0;
70         x.wordv = dest;
71         rsa_os2ip(&x, src, length_B);
72         cli_putstr("\r\ncalling rsa_dec() ...");
73         rsa_dec(&x, key);
74         cli_putstr("\r\nfinished rsa_dec() ...");
75         rsa_i2osp(NULL, &x, &m_length);
76         while(((uint8_t*)x.wordv)[idx]==0 && idx<m_length){
77                 ++idx;
78         }
79         if(((uint8_t*)x.wordv)[idx]!=2 || idx>=m_length){
80                 return 1;
81         }
82         ++idx;
83         while(((uint8_t*)x.wordv)[idx+pad_length]!=0  && (idx+pad_length)<m_length){
84                 ++pad_length;
85         }
86         if(pad_length<8 || (idx+pad_length)>=m_length){
87                 return 2;
88         }
89         *out_length = m_length - idx - pad_length - 1;
90         memcpy(dest, ((uint8_t*)x.wordv) + idx + pad_length + 1, m_length - idx - pad_length - 1);
91         if(pad){
92                 memcpy(pad, ((uint8_t*)x.wordv)+idx, pad_length);
93         }
94         return 0;
95 }
96