-
Notifications
You must be signed in to change notification settings - Fork 0
/
vnrw_gmp.c
387 lines (284 loc) · 8.98 KB
/
vnrw_gmp.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
*
* Williams Encryption scheme
*
* See "A modification of the RSA Public-Key Encryption Procedure" by H. C. Williams
*
*/
#include "vncrypt_gmp.h"
#include "vnrw_gmp.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
typedef struct tagVNRW_GMP_Ctx {
VNAsymCryptCtx_t mCtx;
mpz_t mN;
mpz_t mP;
mpz_t mQ;
mpz_t mD;
} VNRW_GMP_Ctx_t;
static VNAsymCryptCtx_t * VNRW_GMP_CtxNewImpl()
{
VNRW_GMP_Ctx_t * gmpCtx = (VNRW_GMP_Ctx_t *)calloc( sizeof( VNRW_GMP_Ctx_t ), 1 );
gmpCtx->mCtx.mMethod.mCtxFree = VNRW_GMP_CtxFree;
gmpCtx->mCtx.mMethod.mGenKeys = VNRW_GMP_GenKeys;
gmpCtx->mCtx.mMethod.mClearKeys = VNRW_GMP_ClearKeys;
gmpCtx->mCtx.mMethod.mDumpPubKey = VNRW_GMP_DumpPubKey;
gmpCtx->mCtx.mMethod.mDumpPrivKey = VNRW_GMP_DumpPrivKey;
gmpCtx->mCtx.mMethod.mLoadPubKey = VNRW_GMP_LoadPubKey;
gmpCtx->mCtx.mMethod.mLoadPrivKey = VNRW_GMP_LoadPrivKey;
gmpCtx->mCtx.mMethod.mPrivEncrypt = VNRW_GMP_PrivEncrypt;
gmpCtx->mCtx.mMethod.mPubDecrypt = VNRW_GMP_PubDecrypt;
gmpCtx->mCtx.mMethod.mPrivDecrypt = VNRW_GMP_PrivDecrypt;
gmpCtx->mCtx.mMethod.mPubEncrypt = VNRW_GMP_PubEncrypt;
mpz_init( gmpCtx->mN );
mpz_init( gmpCtx->mP );
mpz_init( gmpCtx->mQ );
mpz_init( gmpCtx->mD );
return &( gmpCtx->mCtx );
}
VNAsymCryptCtx_t * VNRWSign_GMP_CtxNew()
{
VNAsymCryptCtx_t * ctx = VNRW_GMP_CtxNewImpl();
ctx->mType = VN_TYPE_VNRWSign_GMP;
return ctx;
}
VNAsymCryptCtx_t * VNRWEnc_GMP_CtxNew()
{
VNAsymCryptCtx_t * ctx = VNRW_GMP_CtxNewImpl();
ctx->mType = VN_TYPE_VNRWEnc_GMP;
return ctx;
}
void VNRW_GMP_CtxFree( VNAsymCryptCtx_t * ctx )
{
VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
mpz_clear( gmpCtx->mN );
mpz_clear( gmpCtx->mP );
mpz_clear( gmpCtx->mQ );
mpz_clear( gmpCtx->mD );
free( gmpCtx );
}
static int VNRW_CalcD( VNAsymCryptCtx_t * ctx )
{
mpz_t zp, zq;
VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
mpz_init( zp );
mpz_init( zq );
mpz_set( zp, gmpCtx->mP );
mpz_set( zq, gmpCtx->mQ );
mpz_sub_ui( zp, zp, 1 );
mpz_sub_ui( zq, zq, 1 );
mpz_mul( gmpCtx->mD, zp, zq );
mpz_cdiv_q_ui( gmpCtx->mD, gmpCtx->mD, 4 );
mpz_add_ui( gmpCtx->mD, gmpCtx->mD, 1 );
mpz_cdiv_q_ui( gmpCtx->mD, gmpCtx->mD, 2 );
mpz_clear( zp );
mpz_clear( zq );
return 0;
}
int VNRW_GMP_GenKeys( VNAsymCryptCtx_t * ctx, int keyBits )
{
mpz_t zrand;
gmp_randstate_t state;
struct vn_iovec seed;
VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
keyBits = ( keyBits + 1 ) / 2;
mpz_init( zrand );
VNIovecGetRandomBuffer( &seed, ( keyBits / 8 + 1 ) );
mpz_import( zrand, seed.i.iov_len, 1, 1, 0, 0, seed.i.iov_base );
gmp_randinit_default( state );
gmp_randseed( state, zrand );
do {
mpz_urandomb( zrand, state, keyBits );
} while( keyBits != mpz_sizeinbase( zrand, 2 ) );
mpz_set( gmpCtx->mP, zrand );
do {
mpz_nextprime( gmpCtx->mP, gmpCtx->mP );
} while ( mpz_fdiv_ui( gmpCtx->mP, 8 ) != 3 );
mpz_set( gmpCtx->mQ, gmpCtx->mP );
do {
mpz_nextprime( gmpCtx->mQ, gmpCtx->mQ );
} while ( mpz_fdiv_ui( gmpCtx->mQ, 8 ) != 7 );
/* compute public key n */
mpz_mul( gmpCtx->mN, gmpCtx->mP, gmpCtx->mQ );
VNRW_CalcD( ctx );
gmp_randclear( state );
VNIovecFreeBufferAndTail( &seed );
mpz_clear( zrand );
return 0;
}
void VNRW_GMP_ClearKeys( VNAsymCryptCtx_t * ctx )
{
VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
mpz_set_ui( gmpCtx->mN, 0 );
mpz_set_ui( gmpCtx->mP, 0 );
mpz_set_ui( gmpCtx->mQ, 0 );
mpz_set_ui( gmpCtx->mD, 0 );
}
int VNRW_GMP_DumpPubKey( const VNAsymCryptCtx_t * ctx,
struct vn_iovec * hexPubKey )
{
const VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
VN_GMP_dump_hex( gmpCtx->mN, hexPubKey );
return 0;
}
int VNRW_GMP_DumpPrivKey( const VNAsymCryptCtx_t * ctx,
struct vn_iovec * hexPubKey, struct vn_iovec * hexPrivKey )
{
const VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
VNRW_GMP_DumpPubKey( ctx, hexPubKey );
VN_GMP_dump_hex( gmpCtx->mP, hexPrivKey );
hexPrivKey->next = (struct vn_iovec*)calloc( sizeof( struct vn_iovec ), 1 );
VN_GMP_dump_hex( gmpCtx->mQ, hexPrivKey->next );
return 0;
}
int VNRW_GMP_LoadPubKey( VNAsymCryptCtx_t * ctx,
const struct vn_iovec * hexPubKey )
{
VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
VN_GMP_load_hex( hexPubKey, gmpCtx->mN );
return 0;
}
int VNRW_GMP_LoadPrivKey( VNAsymCryptCtx_t * ctx,
const struct vn_iovec * hexPubKey, const struct vn_iovec * hexPrivKey )
{
VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
VNRW_GMP_LoadPubKey( ctx, hexPubKey );
VN_GMP_load_hex( hexPrivKey, gmpCtx->mP );
VN_GMP_load_hex( hexPrivKey->next, gmpCtx->mQ );
VNRW_CalcD( ctx );
return 0;
}
static int VNRW_GMP_E1( const VNAsymCryptCtx_t * ctx, mpz_t zm )
{
int ret = 0, jacobi = 0;
VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
mpz_mul_ui( zm, zm, 2 );
mpz_add_ui( zm, zm, 1 );
jacobi = mpz_jacobi( zm, gmpCtx->mN );
if( 1 == jacobi )
{
mpz_mul_ui( zm, zm, 4 );
} else if( -1 == jacobi ) {
mpz_mul_ui( zm, zm, 2 );
} else {
ret = 0;
}
return ret;
}
static int VNRW_GMP_E2( const VNAsymCryptCtx_t * ctx, mpz_t zm )
{
VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
mpz_powm_ui( zm, zm, 2, gmpCtx->mN );
return 0;
}
static int VNRW_GMP_D1( const VNAsymCryptCtx_t * ctx, mpz_t zm )
{
int mod = 0;
VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
mod = mpz_fdiv_ui( zm, 4 );
if( 0 == mod )
{
mpz_cdiv_q_ui( zm, zm, 4 );
mpz_sub_ui( zm, zm, 1 );
} else if( 1 == mod ) {
mpz_sub( zm, gmpCtx->mN, zm );
mpz_cdiv_q_ui( zm, zm, 4 );
mpz_sub_ui( zm, zm, 1 );
} else if( 2 == mod ) {
mpz_cdiv_q_ui( zm, zm, 2 );
mpz_sub_ui( zm, zm, 1 );
} else if( 3 == mod ) {
mpz_sub( zm, gmpCtx->mN, zm );
mpz_cdiv_q_ui( zm, zm, 2 );
mpz_sub_ui( zm, zm, 1 );
}
mpz_cdiv_q_ui( zm, zm, 2 );
return 0;
}
static int VNRW_GMP_D2( const VNAsymCryptCtx_t * ctx, mpz_t zm )
{
VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
mpz_powm( zm, zm, gmpCtx->mD, gmpCtx->mN );
return 0;
}
int VNRW_GMP_PrivEncrypt( const VNAsymCryptCtx_t * ctx,
const unsigned char * plainText, int length,
struct vn_iovec * cipherText )
{
mpz_t zm;
const VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
mpz_init( zm );
mpz_import( zm, length, 1, 1, 0, 0, plainText );
if( ( mpz_sizeinbase( gmpCtx->mN, 2 ) - mpz_sizeinbase( zm, 2 ) ) < 4 )
{
mpz_clear( zm );
return VN_ERR_PLAINTEXT_TOO_LONG;
}
VNRW_GMP_E1( ctx, zm );
VNRW_GMP_D2( ctx, zm );
VN_GMP_dump_bin( zm, cipherText );
mpz_clear( zm );
return 0;
}
int VNRW_GMP_PubDecrypt( const VNAsymCryptCtx_t * ctx,
const unsigned char * cipherText, int length,
struct vn_iovec * plainText )
{
mpz_t zm;
mpz_init( zm );
mpz_import( zm, length, 1, 1, 0, 0, cipherText );
VNRW_GMP_E2( ctx, zm );
VNRW_GMP_D1( ctx, zm );
VN_GMP_dump_bin( zm, plainText );
mpz_clear( zm );
return 0;
}
int VNRW_GMP_PrivDecrypt( const VNAsymCryptCtx_t * ctx,
const unsigned char * cipherText, int length,
struct vn_iovec * plainText )
{
mpz_t zm;
mpz_init( zm );
mpz_import( zm, length, 1, 1, 0, 0, cipherText );
VNRW_GMP_D2( ctx, zm );
VNRW_GMP_D1( ctx, zm );
VN_GMP_dump_bin( zm, plainText );
mpz_clear( zm );
return 0;
}
int VNRW_GMP_PubEncrypt( const VNAsymCryptCtx_t * ctx,
const unsigned char * plainText, int length,
struct vn_iovec * cipherText )
{
mpz_t zm;
const VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );
mpz_init( zm );
mpz_import( zm, length, 1, 1, 0, 0, plainText );
if( ( mpz_sizeinbase( gmpCtx->mN, 2 ) - mpz_sizeinbase( zm, 2 ) ) < 4 )
{
mpz_clear( zm );
return VN_ERR_PLAINTEXT_TOO_LONG;
}
VNRW_GMP_E1( ctx, zm );
VNRW_GMP_E2( ctx, zm );
VN_GMP_dump_bin( zm, cipherText );
mpz_clear( zm );
return 0;
}