#include <stdlib.h>
#include <string.h>
-#include <stdio.h> /* FIXME */
-#include <stdarg.h> /* FIXME */
-#include <windows.h> /* FIXME */
-#include "putty.h" /* FIXME */
-
#include "ssh.h"
unsigned short bnZero[1] = { 0 };
b = (w+7)/8; /* bits -> bytes */
w = (w+15)/16; /* bits -> words */
+ if (!result) /* just return length */
+ return b + 2;
+
bn = newbn(w);
for (i=1; i<=w; i++)
return p - data;
}
+
+/*
+ * Return the bit count of a bignum, for ssh1 encoding.
+ */
+int ssh1_bignum_bitcount(Bignum bn) {
+ int bitcount = bn[0] * 16 - 1;
+
+ while (bitcount >= 0 && (bn[bitcount/16+1] >> (bitcount % 16)) == 0)
+ bitcount--;
+ return bitcount + 1;
+}
+
+/*
+ * Return the byte length of a bignum when ssh1 encoded.
+ */
+int ssh1_bignum_length(Bignum bn) {
+ return 2 + (ssh1_bignum_bitcount(bn)+7)/8;
+}
+
+/*
+ * Return a byte from a bignum; 0 is least significant, etc.
+ */
+int bignum_byte(Bignum bn, int i) {
+ if (i >= 2*bn[0])
+ return 0; /* beyond the end */
+ else if (i & 1)
+ return (bn[i/2+1] >> 8) & 0xFF;
+ else
+ return (bn[i/2+1] ) & 0xFF;
+}
+
+/*
+ * Write a ssh1-format bignum into a buffer. It is assumed the
+ * buffer is big enough. Returns the number of bytes used.
+ */
+int ssh1_write_bignum(void *data, Bignum bn) {
+ unsigned char *p = data;
+ int len = ssh1_bignum_length(bn);
+ int i;
+ int bitc = ssh1_bignum_bitcount(bn);
+
+ *p++ = (bitc >> 8) & 0xFF;
+ *p++ = (bitc ) & 0xFF;
+ for (i = len-2; i-- ;)
+ *p++ = bignum_byte(bn, i);
+ return len;
+}