comparison dss.c @ 165:0cfba3034be5

Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
author Matt Johnston <matt@ucc.asn.au>
date Sun, 02 Jan 2005 20:25:56 +0000
parents 82fcf3185616
children 161557a9dde8
comparison
equal deleted inserted replaced
161:b9d3f725e00b 165:0cfba3034be5
43 * The key will have the same format as buf_put_dss_key. 43 * The key will have the same format as buf_put_dss_key.
44 * These should be freed with dss_key_free. 44 * These should be freed with dss_key_free.
45 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ 45 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
46 int buf_get_dss_pub_key(buffer* buf, dss_key *key) { 46 int buf_get_dss_pub_key(buffer* buf, dss_key *key) {
47 47
48 TRACE(("enter buf_get_dss_pub_key")); 48 TRACE(("enter buf_get_dss_pub_key"))
49 assert(key != NULL); 49 assert(key != NULL);
50 key->p = m_malloc(sizeof(mp_int)); 50 key->p = m_malloc(sizeof(mp_int));
51 key->q = m_malloc(sizeof(mp_int)); 51 key->q = m_malloc(sizeof(mp_int));
52 key->g = m_malloc(sizeof(mp_int)); 52 key->g = m_malloc(sizeof(mp_int));
53 key->y = m_malloc(sizeof(mp_int)); 53 key->y = m_malloc(sizeof(mp_int));
57 buf_incrpos(buf, 4+SSH_SIGNKEY_DSS_LEN); /* int + "ssh-dss" */ 57 buf_incrpos(buf, 4+SSH_SIGNKEY_DSS_LEN); /* int + "ssh-dss" */
58 if (buf_getmpint(buf, key->p) == DROPBEAR_FAILURE 58 if (buf_getmpint(buf, key->p) == DROPBEAR_FAILURE
59 || buf_getmpint(buf, key->q) == DROPBEAR_FAILURE 59 || buf_getmpint(buf, key->q) == DROPBEAR_FAILURE
60 || buf_getmpint(buf, key->g) == DROPBEAR_FAILURE 60 || buf_getmpint(buf, key->g) == DROPBEAR_FAILURE
61 || buf_getmpint(buf, key->y) == DROPBEAR_FAILURE) { 61 || buf_getmpint(buf, key->y) == DROPBEAR_FAILURE) {
62 TRACE(("leave buf_get_dss_pub_key: failed reading mpints")); 62 TRACE(("leave buf_get_dss_pub_key: failed reading mpints"))
63 return DROPBEAR_FAILURE; 63 return DROPBEAR_FAILURE;
64 } 64 }
65 65
66 if (mp_count_bits(key->p) < MIN_DSS_KEYLEN) { 66 if (mp_count_bits(key->p) < MIN_DSS_KEYLEN) {
67 dropbear_log(LOG_WARNING, "DSS key too short"); 67 dropbear_log(LOG_WARNING, "DSS key too short");
68 TRACE(("leave buf_get_dss_pub_key: short key")); 68 TRACE(("leave buf_get_dss_pub_key: short key"))
69 return DROPBEAR_FAILURE; 69 return DROPBEAR_FAILURE;
70 } 70 }
71 71
72 TRACE(("leave buf_get_dss_pub_key: success")); 72 TRACE(("leave buf_get_dss_pub_key: success"))
73 return DROPBEAR_SUCCESS; 73 return DROPBEAR_SUCCESS;
74 } 74 }
75 75
76 /* Same as buf_get_dss_pub_key, but reads a private "x" key at the end. 76 /* Same as buf_get_dss_pub_key, but reads a private "x" key at the end.
77 * Loads a private dss key from a buffer 77 * Loads a private dss key from a buffer
96 96
97 97
98 /* Clear and free the memory used by a public or private key */ 98 /* Clear and free the memory used by a public or private key */
99 void dss_key_free(dss_key *key) { 99 void dss_key_free(dss_key *key) {
100 100
101 TRACE(("enter dsa_key_free")); 101 TRACE(("enter dsa_key_free"))
102 if (key == NULL) { 102 if (key == NULL) {
103 TRACE(("enter dsa_key_free: key == NULL")); 103 TRACE(("enter dsa_key_free: key == NULL"))
104 return; 104 return;
105 } 105 }
106 if (key->p) { 106 if (key->p) {
107 mp_clear(key->p); 107 mp_clear(key->p);
108 m_free(key->p); 108 m_free(key->p);
122 if (key->x) { 122 if (key->x) {
123 mp_clear(key->x); 123 mp_clear(key->x);
124 m_free(key->x); 124 m_free(key->x);
125 } 125 }
126 m_free(key); 126 m_free(key);
127 TRACE(("leave dsa_key_free")); 127 TRACE(("leave dsa_key_free"))
128 } 128 }
129 129
130 /* put the dss public key into the buffer in the required format: 130 /* put the dss public key into the buffer in the required format:
131 * 131 *
132 * string "ssh-dss" 132 * string "ssh-dss"
169 DEF_MP_INT(val3); 169 DEF_MP_INT(val3);
170 DEF_MP_INT(val4); 170 DEF_MP_INT(val4);
171 char * string = NULL; 171 char * string = NULL;
172 int stringlen; 172 int stringlen;
173 173
174 TRACE(("enter buf_dss_verify")); 174 TRACE(("enter buf_dss_verify"))
175 assert(key != NULL); 175 assert(key != NULL);
176 176
177 m_mp_init_multi(&val1, &val2, &val3, &val4, NULL); 177 m_mp_init_multi(&val1, &val2, &val3, &val4, NULL);
178 178
179 /* get blob, check length */ 179 /* get blob, check length */
193 if (mp_read_unsigned_bin(&val1, &string[SHA1_HASH_SIZE], SHA1_HASH_SIZE) 193 if (mp_read_unsigned_bin(&val1, &string[SHA1_HASH_SIZE], SHA1_HASH_SIZE)
194 != MP_OKAY) { 194 != MP_OKAY) {
195 goto out; 195 goto out;
196 } 196 }
197 if (mp_cmp(&val1, key->q) != MP_LT) { 197 if (mp_cmp(&val1, key->q) != MP_LT) {
198 TRACE(("verify failed, s' >= q")); 198 TRACE(("verify failed, s' >= q"))
199 goto out; 199 goto out;
200 } 200 }
201 /* let val2 = w = (s')^-1 mod q*/ 201 /* let val2 = w = (s')^-1 mod q*/
202 if (mp_invmod(&val1, key->q, &val2) != MP_OKAY) { 202 if (mp_invmod(&val1, key->q, &val2) != MP_OKAY) {
203 goto out; 203 goto out;
218 if (mp_read_unsigned_bin(&val1, &string[0], SHA1_HASH_SIZE) 218 if (mp_read_unsigned_bin(&val1, &string[0], SHA1_HASH_SIZE)
219 != MP_OKAY) { 219 != MP_OKAY) {
220 goto out; 220 goto out;
221 } 221 }
222 if (mp_cmp(&val1, key->q) != MP_LT) { 222 if (mp_cmp(&val1, key->q) != MP_LT) {
223 TRACE(("verify failed, r' >= q")); 223 TRACE(("verify failed, r' >= q"))
224 goto out; 224 goto out;
225 } 225 }
226 /* let val4 = u2 = ((r')w) mod q */ 226 /* let val4 = u2 = ((r')w) mod q */
227 if (mp_mulmod(&val1, &val2, key->q, &val4) != MP_OKAY) { 227 if (mp_mulmod(&val1, &val2, key->q, &val4) != MP_OKAY) {
228 goto out; 228 goto out;
313 DEF_MP_INT(dss_temp2); 313 DEF_MP_INT(dss_temp2);
314 DEF_MP_INT(dss_r); 314 DEF_MP_INT(dss_r);
315 DEF_MP_INT(dss_s); 315 DEF_MP_INT(dss_s);
316 hash_state hs; 316 hash_state hs;
317 317
318 TRACE(("enter buf_put_dss_sign")); 318 TRACE(("enter buf_put_dss_sign"))
319 assert(key != NULL); 319 assert(key != NULL);
320 320
321 /* hash the data */ 321 /* hash the data */
322 sha1_init(&hs); 322 sha1_init(&hs);
323 sha1_process(&hs, data, len); 323 sha1_process(&hs, data, len);
420 mp_clear_multi(&dss_k, &dss_temp1, &dss_temp1, &dss_r, &dss_s, 420 mp_clear_multi(&dss_k, &dss_temp1, &dss_temp1, &dss_r, &dss_s,
421 &dss_m, NULL); 421 &dss_m, NULL);
422 422
423 /* create the signature to return */ 423 /* create the signature to return */
424 424
425 TRACE(("leave buf_put_dss_sign")); 425 TRACE(("leave buf_put_dss_sign"))
426 } 426 }
427 427
428 #endif /* DROPBEAR_DSS */ 428 #endif /* DROPBEAR_DSS */