1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/** \ingroup RSA_m
* \file rsa.c
*
* RSA Encryption & signature scheme, code.
*/
/*
* Copyright (c) 2000, 2001, 2002 Virtual Unlimited B.V.
*
* Author: Bob Deblier <bob@virtualunlimited.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "system.h"
#include "rsa.h"
#include "mp.h"
#include "debug.h"
int rsapri(const rsakp* kp, const mpnumber* m, mpnumber* c)
{
register uint32 size = kp->n.size;
register uint32* temp = (uint32*) malloc((4*size+2) * sizeof(*temp));
if (temp)
{
mpnsize(c, size);
mp32bpowmod_w(&kp->n, m->size, m->data, kp->d.size, kp->d.data, c->data, temp);
free(temp);
return 0;
}
return -1;
}
int rsapricrt(const rsakp* kp, const mpnumber* m, mpnumber* c)
{
register uint32 nsize = kp->n.size;
register uint32 psize = kp->p.size;
register uint32 qsize = kp->q.size;
register uint32* ptemp;
register uint32* qtemp;
/* m must be small enough to be exponentiated modulo p and q */
if (m->size > psize || m->size > qsize)
return -1;
ptemp = (uint32*) malloc((6*psize+2)*sizeof(*ptemp));
if (ptemp == NULL)
return -1;
qtemp = (uint32*) malloc((6*qsize+2)*sizeof(*qtemp));
if (qtemp == NULL)
{
free(ptemp);
return -1;
}
/* resize m for powmod p */
mp32setx(psize, ptemp+psize, m->size, m->data);
/* compute j1 = m^d1 mod p, store @ ptemp */
/*@-compdef@*/
mp32bpowmod_w(&kp->p, psize, ptemp+psize, kp->d1.size, kp->d1.data, ptemp, ptemp+2*psize);
/* resize m for powmod p */
mp32setx(qsize, qtemp+psize, m->size, m->data);
/* compute j2 = m^d2 mod q, store @ qtemp */
mp32bpowmod_w(&kp->q, qsize, qtemp+psize, kp->d2.size, kp->d2.data, qtemp, qtemp+2*qsize);
/*@=compdef@*/
/* compute j1-j2 mod p, store @ ptemp */
mp32bsubmod_w(&kp->p, psize, ptemp, qsize, qtemp, ptemp, ptemp+2*psize);
/* compute h = c*(j1-j2) mod p, store @ ptemp */
mp32bmulmod_w(&kp->p, psize, ptemp, psize, kp->c.data, ptemp, ptemp+2*psize);
/* make sure the signature gets the proper size */
mpnsize(c, nsize);
/* compute s = h*q + j2 */
mp32mul(c->data, psize, ptemp, qsize, kp->q.modl);
(void) mp32addx(nsize, c->data, qsize, qtemp);
free(ptemp);
free(qtemp);
return 0;
}
/**
* @return 1 if signature verifies, 0 otherwise (can also indicate errors)
*/
int rsavrfy(const rsapk* pk, const mpnumber* m, const mpnumber* c)
{
int rc;
register uint32 size = pk->n.size;
register uint32* temp = (uint32*) malloc((5*size+2) * sizeof(*temp));
if (temp)
{
mp32bpowmod_w(&pk->n, c->size, c->data, pk->e.size, pk->e.data, temp, temp+size);
rc = mp32eqx(size, temp, m->size, m->data);
free(temp);
return rc;
}
return 0;
}
|