1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

New BinarySink function 'put_padding'.

It is to put_data what memset is to memcpy. Several places
in the code wanted it already, but not _quite_ enough for me to
have written it with the rest of the BinarySink infrastructure
originally.
This commit is contained in:
Simon Tatham 2018-06-09 09:01:07 +01:00
parent 72c2b70736
commit 8b98fea4ae
5 changed files with 19 additions and 6 deletions

View File

@ -1010,8 +1010,7 @@ int openssh_pem_write(const Filename *filename, struct ssh2_userkey *key,
origlen = outblob->len;
outlen = (origlen + 8) &~ 7;
pad = outlen - origlen;
for (i = 0; i < pad; i++)
put_byte(outblob, pad);
put_padding(outblob, pad, pad);
/*
* Invent an iv. Then derive encryption key from passphrase

View File

@ -11,6 +11,17 @@ void BinarySink_put_data(BinarySink *bs, const void *data, size_t len)
bs->write(bs, data, len);
}
void BinarySink_put_padding(BinarySink *bs, unsigned char padbyte, size_t len)
{
char buf[16];
memset(buf, padbyte, sizeof(buf));
while (len > 0) {
size_t thislen = len < sizeof(buf) ? len : sizeof(buf);
bs->write(bs, buf, thislen);
len -= thislen;
}
}
void BinarySink_put_byte(BinarySink *bs, unsigned char val)
{
bs->write(bs, &val, 1);

View File

@ -110,6 +110,10 @@ struct BinarySink {
#define put_mp_ssh2(bs, val) \
BinarySink_put_mp_ssh2(BinarySink_UPCAST(bs), val)
/* Padding with a specified byte. */
#define put_padding(bs, padbyte, len) \
BinarySink_put_padding(BinarySink_UPCAST(bs), padbyte, len)
/* Fallback: just emit raw data bytes, using a syntax that matches the
* rest of these macros. */
#define put_data(bs, val, len) \
@ -126,6 +130,7 @@ struct BinarySink {
* declaration(s) of their other parameter type(s) are in scope.
*/
void BinarySink_put_data(BinarySink *, const void *data, size_t len);
void BinarySink_put_padding(BinarySink *, unsigned char padbyte, size_t len);
void BinarySink_put_byte(BinarySink *, unsigned char);
void BinarySink_put_bool(BinarySink *, int);
void BinarySink_put_uint16(BinarySink *, unsigned long);

3
ssh.c
View File

@ -2803,8 +2803,7 @@ static void ssh2_add_sigblob(Ssh ssh, PktOut *pkt,
strbuf *substr = strbuf_new();
put_data(substr, sigblob, sig_prefix_len);
put_uint32(substr, mod_mp.len);
while (mod_mp.len-- > sig_mp.len)
put_byte(substr, 0);
put_padding(substr, mod_mp.len - sig_mp.len, 0);
put_data(substr, sig_mp.ptr, sig_mp.len);
put_stringsb(pkt, substr);
return;

View File

@ -356,8 +356,7 @@ int rsa_ssh1_savekey(const Filename *filename, struct RSAKey *key,
* Now write zeros until the encrypted portion is a multiple of
* 8 bytes.
*/
while ((buf->len - estart) % 8)
put_byte(buf, 0);
put_padding(buf, (estart - buf->len) & 7, 0);
/*
* Now encrypt the encrypted portion.