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