~sircmpwn/hare-ssh

914063690058b5ffafbbbab9fcea2f40ce37262c — Armin Preiml 9 months ago e127e44
make write functions return number of bytes written
4 files changed, 20 insertions(+), 17 deletions(-)

M format/ssh/ed25519.ha
M format/ssh/key.ha
M format/ssh/rsa.ha
M format/ssh/util.ha
M format/ssh/ed25519.ha => format/ssh/ed25519.ha +4 -3
@@ 110,11 110,12 @@ fn ed25519_sign(
	key: *const key,
	sink: io::handle,
	msg: []u8,
) (void | io::error) = {
) (size | io::error) = {
	let key = key: *ed25519key;
	const signature = ed25519::sign(&key.privkey, msg);
	writestr(sink, "ssh-ed25519")?;
	writeslice(sink, signature)?;
	let n = writestr(sink, "ssh-ed25519")?;
	n += writeslice(sink, signature)?;
	return n;
};

fn ed25519_verify(

M format/ssh/key.ha => format/ssh/key.ha +1 -1
@@ 12,7 12,7 @@ export type encoderawpubfunc = fn(key: *const key, sink: io::handle) (void | io:
export type encoderawprivfunc = fn(key: *const key, sink: io::handle) (void | io::error);
export type decoderawpubfunc = fn(key: *key, src: io::handle) (void | error);
export type decoderawprivfunc = fn(key: *key, src: io::handle) (void | error);
export type signfunc = fn(key: *const key, sink: io::handle, msg: []u8) (void | io::error);
export type signfunc = fn(key: *const key, sink: io::handle, msg: []u8) (size | io::error);
export type verifyfunc = fn(key: *const key, source: io::handle, msg: []u8) (void | error);
export type finishfunc = fn(key: *key) void;


M format/ssh/rsa.ha => format/ssh/rsa.ha +4 -3
@@ 182,7 182,7 @@ fn rsa_decoderawpriv(key: *key, src: io::handle) (void | error) = {
	key.d = alloc(d...);
};

fn rsa_sign(key: *const key, sink: io::handle, msg: []u8) (void | io::error) = {
fn rsa_sign(key: *const key, sink: io::handle, msg: []u8) (size | io::error) = {
	let key = key: *rsakey;

	const nsize = rsa::privkey_nsize(key.privkey);


@@ 204,8 204,9 @@ fn rsa_sign(key: *const key, sink: io::handle, msg: []u8) (void | io::error) = {
		yield;
	};

	writestr(sink, "rsa-sha2-512")?;
	writeslice(sink, sig)?;
	let n = writestr(sink, "rsa-sha2-512")?;
	n += writeslice(sink, sig)?;
	return n;
};

fn rsa_verify(key: *const key, src: io::handle, msg: []u8) (void | error) = {

M format/ssh/util.ha => format/ssh/util.ha +11 -10
@@ 43,35 43,36 @@ fn readslice(src: io::handle) ([]u8 | error) = {
	return buf;
};

fn writeu32(sink: io::handle, v: u32) (void | io::error) = {
fn writeu32(sink: io::handle, v: u32) (size | io::error) = {
	let buf: [4]u8 = [0...];
	endian::beputu32(buf, v);
	io::writeall(sink, buf)?;
	return io::writeall(sink, buf)?;
};

fn writeslice(sink: io::handle, sl: []u8) (void | io::error) = {
fn writeslice(sink: io::handle, sl: []u8) (size | io::error) = {
	assert(len(sl) <= types::U32_MAX, "parameter exceeds maximum length");
	writeu32(sink, len(sl): u32)?;
	io::writeall(sink, sl)?;
	let n = writeu32(sink, len(sl): u32)?;
	return n + io::writeall(sink, sl)?;
};

fn writestr(sink: io::handle, s: str) (void | io::error) = {
fn writestr(sink: io::handle, s: str) (size | io::error) = {
	return writeslice(sink, strings::toutf8(s));
};

// writes an unsigned big integer as mpint
fn writeumpint(sink: io::handle, i: []u8) (void | io::error) = {
fn writeumpint(sink: io::handle, i: []u8) (size | io::error) = {
	assert(len(i) <= types::U32_MAX, "parameter exceeds maximum length");
	let l = len(i);
	if (l > 0 && (i[0] & 0x80) != 0) {
		// need to prepend a 0 otherwise mpint is parsed as negative
		l += 1;
	};
	writeu32(sink, l: u32)?;
	let n = writeu32(sink, l: u32)?;
	if (l > len(i)) {
		io::writeall(sink, [0])?;
		n += io::writeall(sink, [0])?;
	};
	io::writeall(sink, i)?;
	n += io::writeall(sink, i)?;
	return n;
};

// reads a mpint an expects it to be unsigned