mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-03 20:42:48 -05:00
Make ssh_hash and ssh_mac expose a BinarySink.
Just as I did a few commits ago with the low-level SHA_Bytes type functions, the ssh_hash and ssh_mac abstract types now no longer have a direct foo->bytes() update method at all. Instead, each one has a foo->sink() function that returns a BinarySink with the same lifetime as the hash context, and then the caller can feed data into that in the usual way. This lets me get rid of a couple more duplicate marshalling routines in ssh.c: hash_string(), hash_uint32(), hash_mpint().
This commit is contained in:
32
sshccp.c
32
sshccp.c
@ -864,6 +864,8 @@ struct ccp_context {
|
||||
unsigned char mac_iv[8];
|
||||
|
||||
struct poly1305 mac;
|
||||
|
||||
BinarySink_IMPLEMENTATION;
|
||||
};
|
||||
|
||||
static void *poly_make_context(void *ctx)
|
||||
@ -890,9 +892,10 @@ static void poly_start(void *handle)
|
||||
poly1305_init(&ctx->mac);
|
||||
}
|
||||
|
||||
static void poly_bytes(void *handle, unsigned char const *blk, int len)
|
||||
static void poly_BinarySink_write(BinarySink *bs, const void *blkv, size_t len)
|
||||
{
|
||||
struct ccp_context *ctx = (struct ccp_context *)handle;
|
||||
struct ccp_context *ctx = BinarySink_DOWNCAST(bs, struct ccp_context);
|
||||
const unsigned char *blk = (const unsigned char *)blkv;
|
||||
|
||||
/* First 4 bytes are the IV */
|
||||
while (ctx->mac_initialised < 4 && len) {
|
||||
@ -922,6 +925,12 @@ static void poly_bytes(void *handle, unsigned char const *blk, int len)
|
||||
}
|
||||
}
|
||||
|
||||
static BinarySink *poly_sink(void *handle)
|
||||
{
|
||||
struct ccp_context *ctx = (struct ccp_context *)handle;
|
||||
return BinarySink_UPCAST(ctx);
|
||||
}
|
||||
|
||||
static void poly_genresult(void *handle, unsigned char *blk)
|
||||
{
|
||||
struct ccp_context *ctx = (struct ccp_context *)handle;
|
||||
@ -941,13 +950,11 @@ static int poly_verresult(void *handle, unsigned char const *blk)
|
||||
/* The generic poly operation used before generate and verify */
|
||||
static void poly_op(void *handle, unsigned char *blk, int len, unsigned long seq)
|
||||
{
|
||||
unsigned char iv[4];
|
||||
poly_start(handle);
|
||||
PUT_32BIT_MSB_FIRST(iv, seq);
|
||||
/* poly_bytes expects the first 4 bytes to be the IV */
|
||||
poly_bytes(handle, iv, 4);
|
||||
smemclr(iv, sizeof(iv));
|
||||
poly_bytes(handle, blk, len);
|
||||
struct ccp_context *ctx = (struct ccp_context *)handle;
|
||||
poly_start(ctx);
|
||||
/* the data receiver expects the first 4 bytes to be the IV */
|
||||
put_uint32(ctx, seq);
|
||||
put_data(ctx, blk, len);
|
||||
}
|
||||
|
||||
static void poly_generate(void *handle, unsigned char *blk, int len, unsigned long seq)
|
||||
@ -970,7 +977,7 @@ static const struct ssh_mac ssh2_poly1305 = {
|
||||
poly_generate, poly_verify,
|
||||
|
||||
/* partial-packet operations */
|
||||
poly_start, poly_bytes, poly_genresult, poly_verresult,
|
||||
poly_start, poly_sink, poly_genresult, poly_verresult,
|
||||
|
||||
"", "", /* Not selectable individually, just part of ChaCha20-Poly1305 */
|
||||
16, 0, "Poly1305"
|
||||
@ -979,9 +986,8 @@ static const struct ssh_mac ssh2_poly1305 = {
|
||||
static void *ccp_make_context(void)
|
||||
{
|
||||
struct ccp_context *ctx = snew(struct ccp_context);
|
||||
if (ctx) {
|
||||
poly1305_init(&ctx->mac);
|
||||
}
|
||||
BinarySink_INIT(ctx, poly_BinarySink_write);
|
||||
poly1305_init(&ctx->mac);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user