summaryrefslogtreecommitdiff
path: root/bufaux.c
diff options
context:
space:
mode:
Diffstat (limited to 'bufaux.c')
-rw-r--r--bufaux.c43
1 files changed, 22 insertions, 21 deletions
diff --git a/bufaux.c b/bufaux.c
index 37cc27ff6..bf148316d 100644
--- a/bufaux.c
+++ b/bufaux.c
@@ -37,7 +37,7 @@
37 */ 37 */
38 38
39#include "includes.h" 39#include "includes.h"
40RCSID("$OpenBSD: bufaux.c,v 1.29 2003/04/08 20:21:28 itojun Exp $"); 40RCSID("$OpenBSD: bufaux.c,v 1.32 2004/02/23 15:12:46 markus Exp $");
41 41
42#include <openssl/bn.h> 42#include <openssl/bn.h>
43#include "bufaux.h" 43#include "bufaux.h"
@@ -50,7 +50,7 @@ RCSID("$OpenBSD: bufaux.c,v 1.29 2003/04/08 20:21:28 itojun Exp $");
50 * by (bits+7)/8 bytes of binary data, msb first. 50 * by (bits+7)/8 bytes of binary data, msb first.
51 */ 51 */
52void 52void
53buffer_put_bignum(Buffer *buffer, BIGNUM *value) 53buffer_put_bignum(Buffer *buffer, const BIGNUM *value)
54{ 54{
55 int bits = BN_num_bits(value); 55 int bits = BN_num_bits(value);
56 int bin_size = (bits + 7) / 8; 56 int bin_size = (bits + 7) / 8;
@@ -80,7 +80,7 @@ buffer_put_bignum(Buffer *buffer, BIGNUM *value)
80void 80void
81buffer_get_bignum(Buffer *buffer, BIGNUM *value) 81buffer_get_bignum(Buffer *buffer, BIGNUM *value)
82{ 82{
83 int bits, bytes; 83 u_int bits, bytes;
84 u_char buf[2], *bin; 84 u_char buf[2], *bin;
85 85
86 /* Get the number for bits. */ 86 /* Get the number for bits. */
@@ -101,48 +101,49 @@ buffer_get_bignum(Buffer *buffer, BIGNUM *value)
101 * Stores an BIGNUM in the buffer in SSH2 format. 101 * Stores an BIGNUM in the buffer in SSH2 format.
102 */ 102 */
103void 103void
104buffer_put_bignum2(Buffer *buffer, BIGNUM *value) 104buffer_put_bignum2(Buffer *buffer, const BIGNUM *value)
105{ 105{
106 int bytes = BN_num_bytes(value) + 1; 106 u_int bytes;
107 u_char *buf = xmalloc(bytes); 107 u_char *buf;
108 int oi; 108 int oi;
109 int hasnohigh = 0; 109 u_int hasnohigh = 0;
110 110
111 if (BN_is_zero(value)) {
112 buffer_put_int(buffer, 0);
113 return;
114 }
115 if (value->neg)
116 fatal("buffer_put_bignum2: negative numbers not supported");
117 bytes = BN_num_bytes(value) + 1; /* extra padding byte */
118 if (bytes < 2)
119 fatal("buffer_put_bignum2: BN too small");
120 buf = xmalloc(bytes);
111 buf[0] = '\0'; 121 buf[0] = '\0';
112 /* Get the value of in binary */ 122 /* Get the value of in binary */
113 oi = BN_bn2bin(value, buf+1); 123 oi = BN_bn2bin(value, buf+1);
114 if (oi != bytes-1) 124 if (oi != bytes-1)
115 fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d", 125 fatal("buffer_put_bignum2: BN_bn2bin() failed: "
116 oi, bytes); 126 "oi %d != bin_size %d", oi, bytes);
117 hasnohigh = (buf[1] & 0x80) ? 0 : 1; 127 hasnohigh = (buf[1] & 0x80) ? 0 : 1;
118 if (value->neg) {
119 /**XXX should be two's-complement */
120 int i, carry;
121 u_char *uc = buf;
122 logit("negativ!");
123 for (i = bytes-1, carry = 1; i>=0; i--) {
124 uc[i] ^= 0xff;
125 if (carry)
126 carry = !++uc[i];
127 }
128 }
129 buffer_put_string(buffer, buf+hasnohigh, bytes-hasnohigh); 128 buffer_put_string(buffer, buf+hasnohigh, bytes-hasnohigh);
130 memset(buf, 0, bytes); 129 memset(buf, 0, bytes);
131 xfree(buf); 130 xfree(buf);
132} 131}
133 132
134/* XXX does not handle negative BNs */
135void 133void
136buffer_get_bignum2(Buffer *buffer, BIGNUM *value) 134buffer_get_bignum2(Buffer *buffer, BIGNUM *value)
137{ 135{
138 u_int len; 136 u_int len;
139 u_char *bin = buffer_get_string(buffer, &len); 137 u_char *bin = buffer_get_string(buffer, &len);
140 138
139 if (len > 0 && (bin[0] & 0x80))
140 fatal("buffer_get_bignum2: negative numbers not supported");
141 if (len > 8 * 1024) 141 if (len > 8 * 1024)
142 fatal("buffer_get_bignum2: cannot handle BN of size %d", len); 142 fatal("buffer_get_bignum2: cannot handle BN of size %d", len);
143 BN_bin2bn(bin, len, value); 143 BN_bin2bn(bin, len, value);
144 xfree(bin); 144 xfree(bin);
145} 145}
146
146/* 147/*
147 * Returns integers from the buffer (msb first). 148 * Returns integers from the buffer (msb first).
148 */ 149 */