comparison gendss.c @ 640:76097ec1a29a dropbear-tfm

- Bring in original tomsfastmath patch against 0.52 from Peter Turczak in 2008
author Matt Johnston <matt@ucc.asn.au>
date Mon, 21 Nov 2011 19:19:57 +0800
parents c9483550701b
children 2b1bb792cd4d
comparison
equal deleted inserted replaced
518:ce104c8b0be1 640:76097ec1a29a
47 47
48 dss_key *key; 48 dss_key *key;
49 49
50 key = (dss_key*)m_malloc(sizeof(dss_key)); 50 key = (dss_key*)m_malloc(sizeof(dss_key));
51 51
52 key->p = (mp_int*)m_malloc(sizeof(mp_int)); 52 key->p = (fp_int*)m_malloc(sizeof(fp_int));
53 key->q = (mp_int*)m_malloc(sizeof(mp_int)); 53 key->q = (fp_int*)m_malloc(sizeof(fp_int));
54 key->g = (mp_int*)m_malloc(sizeof(mp_int)); 54 key->g = (fp_int*)m_malloc(sizeof(fp_int));
55 key->y = (mp_int*)m_malloc(sizeof(mp_int)); 55 key->y = (fp_int*)m_malloc(sizeof(fp_int));
56 key->x = (mp_int*)m_malloc(sizeof(mp_int)); 56 key->x = (fp_int*)m_malloc(sizeof(fp_int));
57 m_mp_init_multi(key->p, key->q, key->g, key->y, key->x, NULL); 57 m_fp_init_multi(key->p, key->q, key->g, key->y, key->x, NULL);
58 58
59 seedrandom(); 59 seedrandom();
60 60
61 getq(key); 61 getq(key);
62 getp(key, size); 62 getp(key, size);
75 /* 160 bit prime */ 75 /* 160 bit prime */
76 genrandom(buf, QSIZE); 76 genrandom(buf, QSIZE);
77 buf[0] |= 0x80; /* top bit high */ 77 buf[0] |= 0x80; /* top bit high */
78 buf[QSIZE-1] |= 0x01; /* bottom bit high */ 78 buf[QSIZE-1] |= 0x01; /* bottom bit high */
79 79
80 bytes_to_mp(key->q, buf, QSIZE); 80 bytes_to_fp(key->q, buf, QSIZE);
81 81
82 /* 18 rounds are required according to HAC */ 82 /* 18 rounds are required according to HAC */
83 if (mp_prime_next_prime(key->q, 18, 0) != MP_OKAY) { 83 if (fp_prime_next_prime(key->q, 18, 0) != FP_OKAY) {
84 fprintf(stderr, "dss key generation failed\n"); 84 fprintf(stderr, "dss key generation failed\n");
85 exit(1); 85 exit(1);
86 } 86 }
87 } 87 }
88 88
89 static void getp(dss_key *key, unsigned int size) { 89 static void getp(dss_key *key, unsigned int size) {
90 90
91 DEF_MP_INT(tempX); 91 DEF_FP_INT(tempX);
92 DEF_MP_INT(tempC); 92 DEF_FP_INT(tempC);
93 DEF_MP_INT(tempP); 93 DEF_FP_INT(tempP);
94 DEF_MP_INT(temp2q); 94 DEF_FP_INT(temp2q);
95 int result; 95 int result;
96 unsigned char *buf; 96 unsigned char *buf;
97 97
98 m_mp_init_multi(&tempX, &tempC, &tempP, &temp2q, NULL); 98 m_fp_init_multi(&tempX, &tempC, &tempP, &temp2q, NULL);
99 99
100 100
101 /* 2*q */ 101 /* 2*q */
102 if (mp_mul_d(key->q, 2, &temp2q) != MP_OKAY) { 102 fp_mul_d(key->q, 2, &temp2q);
103 fprintf(stderr, "dss key generation failed\n");
104 exit(1);
105 }
106 103
107 buf = (unsigned char*)m_malloc(size); 104 buf = (unsigned char*)m_malloc(size);
108 105
109 result = 0; 106 result = 0;
110 do { 107 do {
111 108
112 genrandom(buf, size); 109 genrandom(buf, size);
113 buf[0] |= 0x80; /* set the top bit high */ 110 buf[0] |= 0x80; /* set the top bit high */
114 111
115 /* X is a random mp_int */ 112 /* X is a random fp_int */
116 bytes_to_mp(&tempX, buf, size); 113 bytes_to_fp(&tempX, buf, size);
117 114
118 /* C = X mod 2q */ 115 /* C = X mod 2q */
119 if (mp_mod(&tempX, &temp2q, &tempC) != MP_OKAY) { 116 if (fp_mod(&tempX, &temp2q, &tempC) != FP_OKAY) {
120 fprintf(stderr, "dss key generation failed\n"); 117 fprintf(stderr, "dss key generation failed\n");
121 exit(1); 118 exit(1);
122 } 119 }
123 120
124 /* P = X - (C - 1) = X - C + 1*/ 121 /* P = X - (C - 1) = X - C + 1*/
125 if (mp_sub(&tempX, &tempC, &tempP) != MP_OKAY) { 122 fp_sub(&tempX, &tempC, &tempP);
126 fprintf(stderr, "dss key generation failed\n");
127 exit(1);
128 }
129 123
130 if (mp_add_d(&tempP, 1, key->p) != MP_OKAY) { 124 fp_add_d(&tempP, 1, key->p);
131 fprintf(stderr, "dss key generation failed\n");
132 exit(1);
133 }
134 125
135 /* now check for prime, 5 rounds is enough according to HAC */ 126 /* now check for prime, 5 rounds is enough according to HAC */
136 /* result == 1 => p is prime */ 127 /* result == 1 => p is prime */
137 if (mp_prime_is_prime(key->p, 5, &result) != MP_OKAY) { 128 if (fp_prime_is_prime(key->p, 5, &result) != FP_OKAY) {
138 fprintf(stderr, "dss key generation failed\n"); 129 fprintf(stderr, "dss key generation failed\n");
139 exit(1); 130 exit(1);
140 } 131 }
141 } while (!result); 132 } while (!result);
142 133
143 mp_clear_multi(&tempX, &tempC, &tempP, &temp2q, NULL); 134 fp_zero(&tempX);
135 fp_zero(&tempC);
136 fp_zero(&tempP);
137 fp_zero(&temp2q);
144 m_burn(buf, size); 138 m_burn(buf, size);
145 m_free(buf); 139 m_free(buf);
146 } 140 }
147 141
148 static void getg(dss_key * key) { 142 static void getg(dss_key * key) {
149 143
150 DEF_MP_INT(div); 144 DEF_FP_INT(div);
151 DEF_MP_INT(h); 145 DEF_FP_INT(h);
152 DEF_MP_INT(val); 146 DEF_FP_INT(val);
153 147
154 m_mp_init_multi(&div, &h, &val, NULL); 148 m_fp_init_multi(&div, &h, &val, NULL);
155 149
156 /* get div=(p-1)/q */ 150 /* get div=(p-1)/q */
157 if (mp_sub_d(key->p, 1, &val) != MP_OKAY) { 151 fp_sub_d(key->p, 1, &val);
158 fprintf(stderr, "dss key generation failed\n"); 152 fp_div(&val, key->q, &div, NULL);
159 exit(1);
160 }
161 if (mp_div(&val, key->q, &div, NULL) != MP_OKAY) {
162 fprintf(stderr, "dss key generation failed\n");
163 exit(1);
164 }
165 153
166 /* initialise h=1 */ 154 /* initialise h=1 */
167 mp_set(&h, 1); 155 fp_set(&h, 1);
168 do { 156 do {
169 /* now keep going with g=h^div mod p, until g > 1 */ 157 /* now keep going with g=h^div mod p, until g > 1 */
170 if (mp_exptmod(&h, &div, key->p, key->g) != MP_OKAY) { 158 if (fp_exptmod(&h, &div, key->p, key->g) != FP_OKAY) {
171 fprintf(stderr, "dss key generation failed\n"); 159 fprintf(stderr, "dss key generation failed\n");
172 exit(1); 160 exit(1);
173 } 161 }
174 162
175 if (mp_add_d(&h, 1, &h) != MP_OKAY) { 163 fp_add_d(&h, 1, &h);
176 fprintf(stderr, "dss key generation failed\n");
177 exit(1);
178 }
179 164
180 } while (mp_cmp_d(key->g, 1) != MP_GT); 165 } while (fp_cmp_d(key->g, 1) != FP_GT);
181 166
182 mp_clear_multi(&div, &h, &val, NULL); 167 fp_zero(&div);
168 fp_zero(&h);
169 fp_zero(&val);
183 } 170 }
184 171
185 static void getx(dss_key *key) { 172 static void getx(dss_key *key) {
186 173
187 gen_random_mpint(key->q, key->x); 174 gen_random_fpint(key->q, key->x);
188 } 175 }
189 176
190 static void gety(dss_key *key) { 177 static void gety(dss_key *key) {
191 178
192 if (mp_exptmod(key->g, key->x, key->p, key->y) != MP_OKAY) { 179 if (fp_exptmod(key->g, key->x, key->p, key->y) != FP_OKAY) {
193 fprintf(stderr, "dss key generation failed\n"); 180 fprintf(stderr, "dss key generation failed\n");
194 exit(1); 181 exit(1);
195 } 182 }
196 } 183 }
197 184