Added mp binary encoding as protocols defines.
authorPekka Riikonen <priikone@silcnet.org>
Mon, 10 Jul 2000 05:34:22 +0000 (05:34 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Mon, 10 Jul 2000 05:34:22 +0000 (05:34 +0000)
lib/silcske/payload.c
lib/silcske/silcske.c

index e9269c8d9e8f4761abc60465f26293399873b5cf..e8e88cb69a6fd6667f8e6f76ab1819cc62b585cc 100644 (file)
@@ -22,6 +22,9 @@
 /*
  * $Id$
  * $Log$
+ * Revision 1.4  2000/07/10 05:34:22  priikone
+ *     Added mp binary encoding as protocols defines.
+ *
  * Revision 1.3  2000/07/07 06:46:43  priikone
  *     Removed ske_verify_public_key function as it is not needed
  *     anymore. Added support to the public key verification as callback
@@ -279,22 +282,19 @@ SilcSKEStatus silc_ske_payload_one_encode(SilcSKE ske,
 {
   SilcBuffer buf;
   unsigned char *e_str;
-  unsigned short e_len;
+  unsigned int e_len;
 
   SILC_LOG_DEBUG(("Encoding KE 1 Payload"));
 
   if (!payload)
     return SILC_SKE_STATUS_ERROR;
 
-  /* Encode the integer into HEX string */
-  e_len = silc_mp_sizeinbase(&payload->e, 16);
-  e_str = silc_calloc(e_len + 1, sizeof(unsigned char));
-  silc_mp_get_str(e_str, 16, &payload->e);
+  /* Encode the integer into binary data */
+  e_str = silc_mp_mp2bin(&payload->e, &e_len);
 
   /* Allocate channel payload buffer. The length of the buffer
      is 2 + e. */
   buf = silc_buffer_alloc(e_len + 2);
-
   silc_buffer_pull_tail(buf, e_len + 2);
 
   /* Encode the payload */
@@ -356,7 +356,7 @@ SilcSKEStatus silc_ske_payload_one_decode(SilcSKE ske,
 
   /* Decode the HEX string to integer */
   silc_mp_init(&payload->e);
-  silc_mp_set_str(&payload->e, buf, 16);
+  silc_mp_bin2mp(buf, e_len, &payload->e);
   memset(buf, 0, sizeof(buf));
 
   /* Return the payload */
@@ -397,9 +397,7 @@ SilcSKEStatus silc_ske_payload_two_encode(SilcSKE ske,
     return SILC_SKE_STATUS_ERROR;
 
   /* Encode the integer into HEX string */
-  f_len = silc_mp_sizeinbase(&payload->f, 16);
-  f_str = silc_calloc(f_len + 1, sizeof(unsigned char));
-  silc_mp_get_str(f_str, 16, &payload->f);
+  f_str = silc_mp_mp2bin(&payload->f, &f_len);
 
   /* Allocate channel payload buffer. The length of the buffer
      is 2 + 2 + public key + 2 + f + 2 + signature. */
@@ -504,7 +502,7 @@ SilcSKEStatus silc_ske_payload_two_decode(SilcSKE ske,
   
   /* Decode the HEX string to integer */
   silc_mp_init(&payload->f);
-  silc_mp_set_str(&payload->f, buf, 16);
+  silc_mp_bin2mp(buf, f_len, &payload->f);
   memset(buf, 0, sizeof(buf));
 
   payload->sign_len -= 2;
index 99be72b4b7e4fa881d7e93b67e039c4573b89fcb..7883285adf771a6ce0d25bb3efe6b9b757ffcd58 100644 (file)
@@ -20,6 +20,9 @@
 /*
  * $Id$
  * $Log$
+ * Revision 1.5  2000/07/10 05:34:22  priikone
+ *     Added mp binary encoding as protocols defines.
+ *
  * Revision 1.4  2000/07/07 06:46:43  priikone
  *     Removed ske_verify_public_key function as it is not needed
  *     anymore. Added support to the public key verification as callback
@@ -1062,10 +1065,10 @@ SilcSKEStatus silc_ske_create_rnd(SilcSKE ske, SilcInt n,
   SILC_LOG_DEBUG(("Creating random number"));
 
   /* Get the random number as string */
-  string = silc_rng_get_rn_string(ske->rng, (len / 8));
+  string = silc_rng_get_rn_data(ske->rng, (len / 8));
 
   /* Decode the string into a MP integer */
-  silc_mp_set_str(rnd, string, 16);
+  silc_mp_bin2mp(string, (len / 8), rnd);
   silc_mp_mod_2exp(rnd, rnd, len);
 
   /* Checks */
@@ -1094,17 +1097,9 @@ SilcSKEStatus silc_ske_make_hash(SilcSKE ske,
 
   SILC_LOG_DEBUG(("Start"));
 
-  e_len = silc_mp_sizeinbase(&ske->ke1_payload->e, 16);
-  e = silc_calloc(e_len + 1, sizeof(unsigned char));
-  silc_mp_get_str(e, 16, &ske->ke1_payload->e);
-
-  f_len = silc_mp_sizeinbase(&ske->ke2_payload->f, 16);
-  f = silc_calloc(f_len + 1, sizeof(unsigned char));
-  silc_mp_get_str(f, 16, &ske->ke2_payload->f);
-
-  KEY_len = silc_mp_sizeinbase(&ske->KEY, 16);
-  KEY = silc_calloc(KEY_len + 1, sizeof(unsigned char));
-  silc_mp_get_str(KEY, 16, &ske->KEY);
+  e = silc_mp_mp2bin(&ske->ke1_payload->e, &e_len);
+  f = silc_mp_mp2bin(&ske->ke2_payload->f, &f_len);
+  KEY = silc_mp_mp2bin(&ske->KEY, &KEY_len);
 
   buf = silc_buffer_alloc(ske->start_payload_copy->len + 
                          ske->pk_len + e_len + f_len + KEY_len);
@@ -1150,9 +1145,8 @@ SilcSKEStatus silc_ske_process_key_material(SilcSKE ske,
                                            unsigned int req_hmac_key_len,
                                            SilcSKEKeyMaterial *key)
 {
-  int i, klen;
+  int klen;
   SilcBuffer buf;
-  SilcInt tmp;
   unsigned char *tmpbuf;
   unsigned char hash[32];
   unsigned int hash_len = ske->prop->hash->hash->hash_len;
@@ -1160,19 +1154,10 @@ SilcSKEStatus silc_ske_process_key_material(SilcSKE ske,
 
   SILC_LOG_DEBUG(("Start"));
 
-  silc_mp_init_set(&tmp, &ske->KEY);
-
-  klen = silc_mp_size(&tmp);
-
-  /* Format the KEY material into binary data */
-  tmpbuf = silc_calloc(klen, sizeof(unsigned char));
-  for (i = klen; i > 0; i--) {
-    tmpbuf[i - 1] = (unsigned char)(silc_mp_get_ui(&tmp) & 0xff);
-    silc_mp_fdiv_q_2exp(&tmp, &tmp, 8);
-  }
+  /* Encode KEY to binary data */
+  tmpbuf = silc_mp_mp2bin(&ske->KEY, &klen);
 
   buf = silc_buffer_alloc(1 + klen + hash_len);
-
   silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
   silc_buffer_format(buf,
                     SILC_STR_UI_CHAR(0),