Annotation of OpenXM/src/ox_ntl/crypt/rsa/gmprsatest.c, Revision 1.2
1.2 ! iwane 1: /* $OpenXM: OpenXM/src/ox_ntl/crypt/rsa/gmprsatest.c,v 1.1 2004/08/16 03:59:58 iwane Exp $ */
1.1 iwane 2:
3: #include <stdio.h>
4: #include <stdlib.h>
5: #include <string.h>
6: #include <gmp.h>
7:
8: #include "gmprsa.h"
9:
10: /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
11: * ALLOC
12: *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
13: #define RSA_KEY_DEBUG 0
14: #define RSA_KEY_PRINT 1
15:
16:
17: static int
18: rsa_alloc_cnt(int n)
19: {
20: static int cnt = 0;
21:
22: cnt += n;
23:
24: return (cnt);
25: }
26:
27:
28: static void *
29: rsa_malloc(size_t size)
30: {
31: void *ptr;
32:
33: ptr = malloc(size);
34: if (ptr) {
35: rsa_alloc_cnt(1);
36: #if RSA_KEY_DEBUG
37: printf("%2d: %p: alloc [%d]\n", rsa_alloc_cnt(0), ptr, size);
38: #endif
39: }
40:
41: return (ptr);
42: }
43:
44: static void
45: rsa_free(void *ptr, size_t size)
46: {
47: free(ptr);
48: rsa_alloc_cnt(-1);
49: #if RSA_KEY_DEBUG
50: printf("%2d: %p: free\n", rsa_alloc_cnt(0) + 1, ptr);
51: #endif
52: }
53:
54:
55: void *
56: rsa_realloc(void *org, size_t old, size_t size)
57: {
58: void *ptr;
59:
60: ptr = malloc(size);
61: memcpy(ptr, org, old);
62:
63: #if RSA_KEY_DEBUG
64: printf("%2d: -%p: realloc [%d]\n", rsa_alloc_cnt(0), org, old);
65: printf("%2d: +%p: realloc [%d]\n", rsa_alloc_cnt(0), ptr, size);
66: #endif
67:
68: free(org);
69: return (ptr);
70: }
71:
72: void
73: rsa_set_memory_functions(void)
74: {
75: mp_set_memory_functions(rsa_malloc, rsa_realloc, rsa_free);
76: }
77:
78:
79:
80: int
81: main(int argc, char *argv[])
82: {
1.2 ! iwane 83: mpz_t a, b, c;
1.1 iwane 84: int add = 1024;
85: rsa_key rsa;
86:
87: char *msg = "OpenXM Project.";
88:
89: char *msgp = msg;
90: int total = strlen(msg) + 1;
91: unsigned char eb[9024], *ebp = eb;
92: unsigned char db[9024], *dbp = db;
93: int x;
1.2 ! iwane 94: int a1, a2 = 1;
1.1 iwane 95:
96: if (argc >= 2)
97: add = atoi(argv[1]);
98:
99: rsa_set_memory_functions();
100:
101: mpz_init(a);
102: mpz_init(b);
1.2 ! iwane 103: mpz_init(c);
! 104:
! 105: mpz_fac_ui(a, 120);
1.1 iwane 106:
107: rsa_init(&rsa);
108:
1.2 ! iwane 109: printf("keygen\n"); fflush(stdout);
! 110: for (a1 = 0; a1 < a2; a1++) {
! 111: x = rsa_keygen(&rsa, a, b, 1024, 80);
! 112: printf("\nkeygen = %d, a1=%d\n", x, a1); fflush(stdout);
! 113: mpz_add_ui(a, a, a1);
! 114: mpz_sub_ui(b, b, a1);
! 115: }
1.1 iwane 116:
1.2 ! iwane 117: #if 1
1.1 iwane 118: gmp_printf("n=%Zx\n", rsa.mod);
119: gmp_printf("p=%Zx\n", rsa.p);
120: gmp_printf("q=%Zx\n", rsa.q);
121: #endif
122:
123:
124: #define ENC 0
125:
126:
127: for (x = 0; total > 0; x++) {
128: #if ENC
129: a1 = rsa_encrypt_by_public_key(&rsa, ebp, msgp, strlen(msgp) + 1, RSA_PKCS_1_PADDING);
130: #else
131: a1 = rsa_encrypt_by_private_key(&rsa, ebp, msgp, strlen(msgp) + 1, RSA_PKCS_1_PADDING);
132: #endif
133:
134: printf("a1 = %d, total = %d\n", a1, total);
135: if (a1 < 0)
136: exit(0);
137:
138: msgp += a1;
139: ebp += rsa.k;
140:
141: total -= a1;
142: }
143:
144: ebp = eb;
145: for (; x > 0; x--) {
146: #if ENC
147: a2 = rsa_decrypt_by_private_key(&rsa, dbp, ebp, RSA_PKCS_1_PADDING);
148: #else
149: a2 = rsa_decrypt_by_public_key(&rsa, dbp, ebp, RSA_PKCS_1_PADDING);
150: #endif
151:
152: printf("a2 = %d: x = %d\n", a2, x);
1.2 ! iwane 153: if (a2 < 0) {
! 154: printf("decode failed\n");
1.1 iwane 155: exit(0);
1.2 ! iwane 156: }
1.1 iwane 157:
158: dbp += a2;
159: ebp += rsa.k;
160: }
161:
162: printf("db = %s\n", db);
163:
164: mpz_clear(a);
165: mpz_clear(b);
1.2 ! iwane 166: mpz_clear(c);
1.1 iwane 167:
168: rsa_clear(&rsa);
169:
1.2 ! iwane 170: printf("gmp_info: %d, %d -- leak = %d\n", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, rsa_alloc_cnt(0));
1.1 iwane 171:
172: return (0);
173: }
174:
175:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>