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
|
/** \ingroup RSA_m
* \file rsa.c
*
* RSA Encryption & signature scheme, code.
*/
/*
* Copyright (c) 2000, 2001 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
*
*/
#define BEECRYPT_DLL_EXPORT
#include "rsa.h"
#include "mp32.h"
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_MALLOC_H
# include <malloc.h>
#endif
int rsapri(const rsakp* kp, const mp32number* m, mp32number* c)
{
register uint32 size = kp->n.size;
register uint32* temp = (uint32*) malloc((4*size+2) * sizeof(*temp));
if (temp)
{
mp32nsize(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;
}
/*@-nullpass -nullptrarith @*/ /* temp may be NULL */
/* this routine doesn't work yet: needs debugging! */
int rsapricrt(const rsakp* kp, const mp32number* m, mp32number* c)
{
register uint32 nsize = kp->n.size;
register uint32 psize = kp->p.size;
register uint32 qsize = kp->q.size;
register uint32* temp = (uint32*) malloc((psize+qsize+(5*nsize+6))*sizeof(*temp));
register uint32* wksp = temp+psize+qsize+nsize;
/* compute j1 = m^d1 mod p */
if (mp32gex(psize, kp->p.modl, m->size, m->data))
{
mp32setx(nsize, temp+psize+qsize, m->size, m->data);
/*@-compdef@*/ /* LCL: temp+psize+qsize */
mp32bmod_w(&kp->p, temp+psize+qsize, temp, wksp);
/*@=compdef@*/
}
else
mp32setx(psize, temp, m->size, m->data);
mp32bpowmod_w(&kp->p, psize, temp, kp->d1.size, kp->d1.data, temp, wksp);
/* compute j2 = m^d2 mod q */
if (mp32gex(qsize, kp->q.modl, m->size, m->data))
{
mp32setx(nsize, temp+psize+qsize, m->size, m->data);
/*@-compdef@*/ /* LCL: temp+psize+qsize */
mp32bmod_w(&kp->q, temp+psize+qsize, temp+psize, wksp);
/*@=compdef@*/
}
else
mp32setx(qsize, temp+psize, m->size, m->data);
mp32bpowmod_w(&kp->q, qsize, temp+psize, kp->d2.size, kp->d2.data, temp+psize, wksp);
/* compute j1-j2 */
(void) mp32subx(psize, temp, qsize, temp+psize);
/* compute h = c*(j1-j2) mod p */
mp32bmulmod_w(&kp->p, psize, temp, psize, kp->c.data, temp, wksp);
/* make sure the signature gets the proper size */
mp32nsize(c, nsize);
/* compute s = h*q + j2 */
mp32mul(c->data, psize, temp, qsize, kp->q.modl);
(void) mp32addx(nsize, c->data, qsize, temp+psize);
free(temp);
return -1;
}
/*@=nullpass =nullptrarith @*/
/**
* @return 1 if signature verifies, 0 otherwise (can also indicate errors)
*/
int rsavrfy(const rsapk* pk, const mp32number* m, const mp32number* 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;
}
|