]> git.cryptolib.org Git - arm-crypto-lib.git/blob - rsa/rsa_basic.c
first idea of RSA (PKCS#1 v1.5)
[arm-crypto-lib.git] / rsa / rsa_basic.c
1 /* rsa_basic.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 "cli.h"
27
28 void rsa_enc(bigint_t* data, rsa_publickey_t* key){
29         bigint_expmod_u(data, data, key->exponent, key->modulus);
30 }
31
32 void rsa_dec(bigint_t* data, rsa_privatekey_t* key){
33         bigint_expmod_u(data, data, key->exponent, key->modulus);
34 }
35
36 void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){
37         uint8_t off;
38         off = length_B % sizeof(bigint_word_t);
39         if(!data){
40                 if(off){
41                         dest->wordv = realloc(dest->wordv, length_B + sizeof(bigint_word_t) - off);
42                         memmove((uint8_t*)dest->wordv+off, dest->wordv, length_B);
43                         memset(dest->wordv, 0, off);
44                 }
45         }else{
46                 if(off){
47                         memcpy((uint8_t*)dest->wordv + off, data, length_B);
48                         memset(dest, 0, off);
49                 }else{
50                         memcpy(dest->wordv, data, length_B);
51                 }
52         }
53         dest->length_B = (length_B + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
54         bigint_changeendianess(dest);
55         bigint_adjust(dest);
56 }
57
58 void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){
59         *out_length_B = bigint_get_first_set_bit(src) / 8 + 1;
60         if(dest){
61                 uint16_t i;
62                 for(i=*out_length_B; i>0; --i){
63                         *((uint8_t*)dest) = ((uint8_t*)src->wordv)[i-1];
64                         dest = (uint8_t*)dest + 1;
65                 }
66         }else{
67                 uint8_t off;
68                 bigint_changeendianess(src);
69                 bigint_adjust(src);
70
71                 off = bigint_get_last_set_bit(src)/8;
72                 if(off){
73                         memmove(src->wordv, (uint8_t*)src->wordv+off, *out_length_B);
74                 }
75         }
76 }
77