pad.c 1.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* pad.c
 *
 */

#include "pad.h"

static int do_pad(struct pad_processor *closure,
		  struct simple_packet *packet)
{
  UINT32 new_size;
  UINT8 padding;

  struct simple_packet *new;
  struct ssh_packet *ssh;

  new_size = 1 + closure->block_size
    * ( (8 + packet->length) / closure->block_size);

  padding = new_size - packet->length - 5;
  assert(ssh->padding_length >= 4);
  
  new = simple_packet_alloc(new_size);
  ssh = (struct ssh_packet *) new->data;

  ssh->length = htonl(new_size);
  ssh->padding_length = padding;

  memcpy(ssh->data, packet->data, packet->length);
  closure->random(closure->state, padding, ssh->data + packet->length);

  simple_packet_free(packet);

  return apply_processor(closure->c->next, new);
}
  

struct packet_processor *make_pad_processor(packet_processor *continuation,
					    unsigned block_size,
					    random_function random,
					    void *state)
{
  struct pad_processor *closure = xalloc(sizeof(struct pad_processor));

  closure->c->p->f = (raw_processor_function) do_pad;
  closure->c->next = continuation;
  closure->block_size = block_size;
  closure->random = random;
  closure->state = state;

Niels Möller's avatar
Niels Möller committed
50
  return (struct packet_processor *) closure;
51
}