comparison gendss.c @ 641:2b1bb792cd4d dropbear-tfm

- Update tfm changes to current default tip
author Matt Johnston <matt@ucc.asn.au>
date Mon, 21 Nov 2011 19:52:28 +0800
parents 76097ec1a29a a98a2138364a
children
comparison
equal deleted inserted replaced
640:76097ec1a29a 641:2b1bb792cd4d
35 35
36 /* This is just a test */ 36 /* This is just a test */
37 37
38 #ifdef DROPBEAR_DSS 38 #ifdef DROPBEAR_DSS
39 39
40 static void getq(dss_key *key); 40 static void getq(dropbear_dss_key *key);
41 static void getp(dss_key *key, unsigned int size); 41 static void getp(dropbear_dss_key *key, unsigned int size);
42 static void getg(dss_key *key); 42 static void getg(dropbear_dss_key *key);
43 static void getx(dss_key *key); 43 static void getx(dropbear_dss_key *key);
44 static void gety(dss_key *key); 44 static void gety(dropbear_dss_key *key);
45 45
46 dss_key * gen_dss_priv_key(unsigned int size) { 46 dropbear_dss_key * gen_dss_priv_key(unsigned int size) {
47 47
48 dss_key *key; 48 dropbear_dss_key *key;
49 49
50 key = (dss_key*)m_malloc(sizeof(dss_key)); 50 key = m_malloc(sizeof(*key));
51 51
52 key->p = (fp_int*)m_malloc(sizeof(fp_int)); 52 key->p = (fp_int*)m_malloc(sizeof(fp_int));
53 key->q = (fp_int*)m_malloc(sizeof(fp_int)); 53 key->q = (fp_int*)m_malloc(sizeof(fp_int));
54 key->g = (fp_int*)m_malloc(sizeof(fp_int)); 54 key->g = (fp_int*)m_malloc(sizeof(fp_int));
55 key->y = (fp_int*)m_malloc(sizeof(fp_int)); 55 key->y = (fp_int*)m_malloc(sizeof(fp_int));
66 66
67 return key; 67 return key;
68 68
69 } 69 }
70 70
71 static void getq(dss_key *key) { 71 static void getq(dropbear_dss_key *key) {
72 72
73 char buf[QSIZE]; 73 char buf[QSIZE];
74 74
75 /* 160 bit prime */ 75 /* 160 bit prime */
76 genrandom(buf, QSIZE); 76 genrandom(buf, QSIZE);
79 79
80 bytes_to_fp(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 (fp_prime_next_prime(key->q, 18, 0) != FP_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(dropbear_dss_key *key, unsigned int size) {
90 90
91 DEF_FP_INT(tempX); 91 DEF_FP_INT(tempX);
92 DEF_FP_INT(tempC); 92 DEF_FP_INT(tempC);
93 DEF_FP_INT(tempP); 93 DEF_FP_INT(tempP);
94 DEF_FP_INT(temp2q); 94 DEF_FP_INT(temp2q);
112 /* X is a random fp_int */ 112 /* X is a random fp_int */
113 bytes_to_fp(&tempX, buf, size); 113 bytes_to_fp(&tempX, buf, size);
114 114
115 /* C = X mod 2q */ 115 /* C = X mod 2q */
116 if (fp_mod(&tempX, &temp2q, &tempC) != FP_OKAY) { 116 if (fp_mod(&tempX, &temp2q, &tempC) != FP_OKAY) {
117 fprintf(stderr, "dss key generation failed\n"); 117 fprintf(stderr, "DSS key generation failed\n");
118 exit(1); 118 exit(1);
119 } 119 }
120 120
121 /* P = X - (C - 1) = X - C + 1*/ 121 /* P = X - (C - 1) = X - C + 1*/
122 fp_sub(&tempX, &tempC, &tempP); 122 fp_sub(&tempX, &tempC, &tempP);
124 fp_add_d(&tempP, 1, key->p); 124 fp_add_d(&tempP, 1, key->p);
125 125
126 /* now check for prime, 5 rounds is enough according to HAC */ 126 /* now check for prime, 5 rounds is enough according to HAC */
127 /* result == 1 => p is prime */ 127 /* result == 1 => p is prime */
128 if (fp_prime_is_prime(key->p, 5, &result) != FP_OKAY) { 128 if (fp_prime_is_prime(key->p, 5, &result) != FP_OKAY) {
129 fprintf(stderr, "dss key generation failed\n"); 129 fprintf(stderr, "DSS key generation failed\n");
130 exit(1); 130 exit(1);
131 } 131 }
132 } while (!result); 132 } while (!result);
133 133
134 fp_zero(&tempX); 134 fp_zero(&tempX);
137 fp_zero(&temp2q); 137 fp_zero(&temp2q);
138 m_burn(buf, size); 138 m_burn(buf, size);
139 m_free(buf); 139 m_free(buf);
140 } 140 }
141 141
142 static void getg(dss_key * key) { 142 static void getg(dropbear_dss_key * key) {
143 143
144 DEF_FP_INT(div); 144 DEF_FP_INT(div);
145 DEF_FP_INT(h); 145 DEF_FP_INT(h);
146 DEF_FP_INT(val); 146 DEF_FP_INT(val);
147 147
154 /* initialise h=1 */ 154 /* initialise h=1 */
155 fp_set(&h, 1); 155 fp_set(&h, 1);
156 do { 156 do {
157 /* 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 */
158 if (fp_exptmod(&h, &div, key->p, key->g) != FP_OKAY) { 158 if (fp_exptmod(&h, &div, key->p, key->g) != FP_OKAY) {
159 fprintf(stderr, "dss key generation failed\n"); 159 fprintf(stderr, "DSS key generation failed\n");
160 exit(1); 160 exit(1);
161 } 161 }
162 162
163 fp_add_d(&h, 1, &h); 163 fp_add_d(&h, 1, &h);
164 164
167 fp_zero(&div); 167 fp_zero(&div);
168 fp_zero(&h); 168 fp_zero(&h);
169 fp_zero(&val); 169 fp_zero(&val);
170 } 170 }
171 171
172 static void getx(dss_key *key) { 172 static void getx(dropbear_dss_key *key) {
173 173
174 gen_random_fpint(key->q, key->x); 174 gen_random_fpint(key->q, key->x);
175 } 175 }
176 176
177 static void gety(dss_key *key) { 177 static void gety(dropbear_dss_key *key) {
178 178
179 if (fp_exptmod(key->g, key->x, key->p, key->y) != FP_OKAY) { 179 if (fp_exptmod(key->g, key->x, key->p, key->y) != FP_OKAY) {
180 fprintf(stderr, "dss key generation failed\n"); 180 fprintf(stderr, "DSS key generation failed\n");
181 exit(1); 181 exit(1);
182 } 182 }
183 } 183 }
184 184
185 #endif /* DROPBEAR_DSS */ 185 #endif /* DROPBEAR_DSS */