summaryrefslogtreecommitdiff
path: root/bufaux.c
diff options
context:
space:
mode:
Diffstat (limited to 'bufaux.c')
-rw-r--r--bufaux.c35
1 files changed, 18 insertions, 17 deletions
diff --git a/bufaux.c b/bufaux.c
index 339d74435..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.31 2003/11/10 16:23:41 jakob 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"
@@ -103,46 +103,47 @@ buffer_get_bignum(Buffer *buffer, BIGNUM *value)
103void 103void
104buffer_put_bignum2(Buffer *buffer, const BIGNUM *value) 104buffer_put_bignum2(Buffer *buffer, const BIGNUM *value)
105{ 105{
106 u_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 u_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 */