summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/picotls/deps/cifra/src/blockwise.h
blob: a20ff959579585623a29fca211da414315ce771d (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * cifra - embedded cryptography library
 * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
 *
 * To the extent possible under law, the author(s) have dedicated all
 * copyright and related and neighboring rights to this software to the
 * public domain worldwide. This software is distributed without any
 * warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication
 * along with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

#ifndef BLOCKWISE_H
#define BLOCKWISE_H

#include <stdint.h>
#include <stddef.h>

/* Processing function for cf_blockwise_accumulate. */
typedef void (*cf_blockwise_in_fn)(void *ctx, const uint8_t *data);

/* Processing function for cf_blockwise_xor. */
typedef void (*cf_blockwise_out_fn)(void *ctx, uint8_t *data);

/* This function manages the common abstraction of accumulating input in
 * a buffer, and processing it when a full block is available.
 *
 * partial is the buffer (maintained by the caller)
 * on entry, npartial is the currently valid count of used bytes on
 *   the front of partial.
 * on exit, npartial is updated to reflect the status of partial.
 * nblock is the blocksize to accumulate -- partial must be at least
 *   this long!
 * input is the new data to process, of length nbytes.
 * process is the processing function, passed ctx and a pointer
 *   to the data to process (always exactly nblock bytes long!)
 *   which may not neccessarily be the same as partial.
 */
void cf_blockwise_accumulate(uint8_t *partial, size_t *npartial,
                             size_t nblock,
                             const void *input, size_t nbytes,
                             cf_blockwise_in_fn process, 
                             void *ctx);

/* This function manages the common abstraction of accumulating input in
 * a buffer, and processing it when a full block is available.
 * This version supports calling a different processing function for
 * the last block.
 *
 * partial is the buffer (maintained by the caller)
 * on entry, npartial is the currently valid count of used bytes on
 *   the front of partial.
 * on exit, npartial is updated to reflect the status of partial.
 * nblock is the blocksize to accumulate -- partial must be at least
 *   this long!
 * input is the new data to process, of length nbytes.
 * process is the processing function, passed ctx and a pointer
 *   to the data to process (always exactly nblock bytes long!)
 *   which may not neccessarily be the same as partial.
 * process_final is called last (but may not be called at all if
 *   all input is buffered).
 */
void cf_blockwise_accumulate_final(uint8_t *partial, size_t *npartial,
                                   size_t nblock,
                                   const void *input, size_t nbytes,
                                   cf_blockwise_in_fn process, 
                                   cf_blockwise_in_fn process_final,
                                   void *ctx);

/* This function manages XORing an input stream with a keystream
 * to produce an output stream.  The keystream is produced in blocks
 * (ala a block cipher in counter mode).
 *
 * partial is the keystream buffer (maintained by the caller)
 * on entry, *npartial is the currently valid count of bytes in partial:
 *   unused bytes are at the *end*.  So *npartial = 4 means the last four
 *   bytes of partial are usable as keystream.
 * on exit, npartial is updated to reflect the new state of partial.
 * nblock is the blocksize to accumulate -- partial must be at least
 *   this long!
 * input is the new data to process, of length nbytes.
 * output is where to write input xored with the keystream -- also length
 *   nbytes.
 * process is the processing function, passed ctx and partial which it
 *   should fill with fresh key stream.
 */
void cf_blockwise_xor(uint8_t *partial, size_t *npartial,
                      size_t nblock,
                      const void *input, void *output, size_t nbytes,
                      cf_blockwise_out_fn newblock,
                      void *ctx);

/* This function processes a single byte a number of times. It's useful
 * for padding, and more efficient than calling cf_blockwise_accumulate
 * a bunch of times.
 *
 * partial is the buffer (maintained by the caller)
 * on entry, npartial is the currently valid count of used bytes on
 *   the front of partial.
 * on exit, npartial is updated to reflect the status of partial.
 * nblock is the blocksize to accumulate -- partial must be at least
 *   this long!
 * process is the processing function, passed ctx and a pointer
 *   to the data to process (always exactly nblock bytes long!)
 *   which may not neccessarily be the same as partial.
 * byte is the byte to process, nbytes times.
 */
void cf_blockwise_acc_byte(uint8_t *partial, size_t *npartial,
                           size_t nblock,
                           uint8_t byte, size_t nbytes,
                           cf_blockwise_in_fn process,
                           void *ctx);

/* This function attempts to process patterns of bytes common in
 * block cipher padding.
 *
 * This takes three bytes:
 * - a first byte, fbyte,
 * - a middle byte, mbyte,
 * - a last byte, lbyte.
 *
 * If nbytes is zero, nothing happens.
 * If nbytes is one, the byte fbyte ^ lbyte is processed.
 * If nbytes is two, the fbyte then lbyte are processed.
 * If nbytes is three or more, fbyte, then one or more mbytes, then fbyte
 *   is processed.
 *
 * partial is the buffer (maintained by the caller)
 * on entry, npartial is the currently valid count of used bytes on
 *   the front of partial.
 * on exit, npartial is updated to reflect the status of partial.
 * nblock is the blocksize to accumulate -- partial must be at least
 *   this long!
 * process is the processing function, passed ctx and a pointer
 *   to the data to process (always exactly nblock bytes long!)
 *   which may not neccessarily be the same as partial.
 */
void cf_blockwise_acc_pad(uint8_t *partial, size_t *npartial,
                          size_t nblock,
                          uint8_t fbyte, uint8_t mbyte, uint8_t lbyte,
                          size_t nbytes,
                          cf_blockwise_in_fn process,
                          void *ctx);

#endif