encrypt.c 1.3 KB
Newer Older
1
2
3
4
5
/* encrypt.c
 *
 */

#include "encrypt.h"
Niels Möller's avatar
Niels Möller committed
6
#include "xalloc.h"
7

8
static int do_encrypt(struct encrypt_processor **c,
Niels Möller's avatar
Niels Möller committed
9
		      struct simple_packet *packet)
10
{
11
12
13
  struct encrypt_processor *closure
    = (struct encrypt_processor *) *c;
  
Niels Möller's avatar
Niels Möller committed
14
  /* FIXME: Use ssh_format() */
15
  struct simple_packet *new
Niels Möller's avatar
Niels Möller committed
16
    = lsh_string_alloc(packet->length + closure->mac_size);
17
18

  closure->encrypt_function(closure->encrypt_state,
Niels Möller's avatar
Niels Möller committed
19
20
			    packet->length,
			    packet->data, new->data);
21

Niels Möller's avatar
Niels Möller committed
22
23
  if (closure->mac_size)
    closure->mac_function(closure->mac_state,
Niels Möller's avatar
Niels Möller committed
24
			  packet->length,
Niels Möller's avatar
Niels Möller committed
25
26
			  packet->data, new->data + packet->length);
  
Niels Möller's avatar
Niels Möller committed
27
  lsh_string_free(packet);
28

Niels Möller's avatar
Niels Möller committed
29
  return apply_processor(closure->c.next, new);
30
31
}

Niels Möller's avatar
Niels Möller committed
32
33
struct abstract_write *
make_packet_encrypt(struct abstract_write *continuation,
34
35
36
37
38
39
		       unsigned mac_size,
		       transform_function mac_function,
		       void *mac_state,
		       transform_function encrypt_function,
		       void *encrypt_state)
{
Niels Möller's avatar
Niels Möller committed
40
  struct encrypt_processor *closure = xalloc(sizeof(struct encrypt_processor));
41

42
  closure->super.super.write = do_encrypt;
Niels Möller's avatar
Niels Möller committed
43
  closure->c.next = continuation;
44
45
46
47
48
49
  closure->mac_size = mac_size;
  closure->mac_function = mac_function;
  closure->mac_state = mac_state;
  closure->encrypt_function = encrypt_function;
  closure->encrypt_state = encrypt_state;

50
  return &closure->super.super;
51
52
53
}