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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/** \ingroup RSA_m
* \file rsakp.c
*
* RSA Keypair, code.
*/
/*
* <conformance statement for IEEE P1363 needed here>
*
* Copyright (c) 2000 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 "rsakp.h"
#include "mp32prime.h"
#include "mp32.h"
#include "debug.h"
/*@-boundswrite@*/
int rsakpMake(rsakp* kp, randomGeneratorContext* rgc, int nsize)
{
/*
* Generates an RSA Keypair for use with the Chinese Remainder Theorem
*/
register uint32 pqsize = ((uint32)(nsize+1)) >> 1;
register uint32* temp = (uint32*) malloc((16*pqsize+6) * sizeof(*temp));
register uint32 newn = 1;
if (temp)
{
mp32barrett r, psubone, qsubone, phi;
nsize = pqsize << 1;
/* set e */
mp32nsetw(&kp->e, 65535);
/* generate a random prime p and q */
/*@-globs@*/
mp32prnd_w(&kp->p, rgc, pqsize, mp32ptrials(pqsize << 5), &kp->e, temp);
mp32prnd_w(&kp->q, rgc, pqsize, mp32ptrials(pqsize << 5), &kp->e, temp);
/*@-globs@*/
/* if p <= q, perform a swap to make p larger than q */
if (mp32le(pqsize, kp->p.modl, kp->q.modl))
{
/*@-sizeoftype@*/
memcpy(&r, &kp->q, sizeof(mp32barrett));
memcpy(&kp->q, &kp->p, sizeof(mp32barrett));
memcpy(&kp->p, &r, sizeof(mp32barrett));
/*@=sizeoftype@*/
}
mp32bzero(&r);
mp32bzero(&psubone);
mp32bzero(&qsubone);
mp32bzero(&phi);
while (1)
{
mp32mul(temp, pqsize, kp->p.modl, pqsize, kp->q.modl);
if (newn && mp32msbset(nsize, temp))
break;
/* product of p and q doesn't have the required size (one bit short) */
/*@-globs@*/
mp32prnd_w(&r, rgc, pqsize, mp32ptrials(pqsize << 5), &kp->e, temp);
/*@=globs@*/
/*@-usedef -branchstate @*/ /* r is set */
if (mp32le(pqsize, kp->p.modl, r.modl))
{
mp32bfree(&kp->q);
/*@-sizeoftype@*/
memcpy(&kp->q, &kp->p, sizeof(mp32barrett));
memcpy(&kp->p, &r, sizeof(mp32barrett));
/*@=sizeoftype@*/
mp32bzero(&r);
newn = 1;
}
else if (mp32le(pqsize, kp->q.modl, r.modl))
{
mp32bfree(&kp->q);
/*@-sizeoftype@*/
memcpy(&kp->q, &r, sizeof(mp32barrett));
/*@=sizeoftype@*/
mp32bzero(&r);
newn = 1;
}
else
{
mp32bfree(&r);
newn = 0;
}
/*@=usedef =branchstate @*/
}
mp32bset(&kp->n, nsize, temp);
/* compute p-1 */
mp32bsubone(&kp->p, temp);
mp32bset(&psubone, pqsize, temp);
/* compute q-1 */
mp32bsubone(&kp->q, temp);
mp32bset(&qsubone, pqsize, temp);
/*@-usedef@*/ /* psubone/qsubone are set */
/* compute phi = (p-1)*(q-1) */
mp32mul(temp, pqsize, psubone.modl, pqsize, qsubone.modl);
mp32bset(&phi, nsize, temp);
/* compute d = inv(e) mod phi */
mp32nsize(&kp->d, nsize);
(void) mp32binv_w(&phi, kp->e.size, kp->e.data, kp->d.data, temp);
/* compute d1 = d mod (p-1) */
mp32nsize(&kp->d1, pqsize);
mp32bmod_w(&psubone, kp->d.data, kp->d1.data, temp);
/* compute d2 = d mod (q-1) */
mp32nsize(&kp->d2, pqsize);
mp32bmod_w(&qsubone, kp->d.data, kp->d2.data, temp);
/* compute c = inv(q) mod p */
mp32nsize(&kp->c, pqsize);
(void) mp32binv_w(&kp->p, pqsize, kp->q.modl, kp->c.data, temp);
free(temp);
/*@=usedef@*/
return 0;
}
return -1;
}
/*@=boundswrite@*/
/*@-boundswrite@*/
int rsakpInit(rsakp* kp)
{
memset(kp, 0, sizeof(*kp));
/* or
mp32bzero(&kp->n);
mp32nzero(&kp->e);
mp32nzero(&kp->d);
mp32bzero(&kp->p);
mp32bzero(&kp->q);
mp32nzero(&kp->d1);
mp32nzero(&kp->d2);
mp32nzero(&kp->c);
*/
return 0;
}
/*@=boundswrite@*/
int rsakpFree(rsakp* kp)
{
/*@-usereleased -compdef @*/ /* kp->param.{n,p,q}.modl is OK */
mp32bfree(&kp->n);
mp32nfree(&kp->e);
mp32nfree(&kp->d);
mp32bfree(&kp->p);
mp32bfree(&kp->q);
mp32nfree(&kp->d1);
mp32nfree(&kp->d2);
mp32nfree(&kp->c);
return 0;
/*@=usereleased =compdef @*/
}
int rsakpCopy(rsakp* dst, const rsakp* src)
{
mp32bcopy(&dst->n, &src->n);
mp32ncopy(&dst->e, &src->e);
mp32ncopy(&dst->d, &src->d);
mp32bcopy(&dst->p, &src->p);
mp32bcopy(&dst->q, &src->q);
mp32ncopy(&dst->d1, &src->d1);
mp32ncopy(&dst->d2, &src->d2);
mp32ncopy(&dst->c, &src->c);
return 0;
}
|