| 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 |
#include "ngtcp2_ppe.h" |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
#include <assert.h> |
| 29 |
|
| 30 |
#include "ngtcp2_str.h" |
| 31 |
#include "ngtcp2_conv.h" |
| 32 |
#include "ngtcp2_macro.h" |
| 33 |
|
| 34 |
void ngtcp2_ppe_init(ngtcp2_ppe *ppe, uint8_t *out, size_t outlen, |
| 35 |
size_t dgram_offset, ngtcp2_crypto_cc *cc) { |
| 36 |
ngtcp2_buf_init(&ppe->buf, out, outlen); |
| 37 |
|
| 38 |
ppe->dgram_offset = dgram_offset; |
| 39 |
ppe->hdlen = 0; |
| 40 |
ppe->len_offset = 0; |
| 41 |
ppe->pkt_num_offset = 0; |
| 42 |
ppe->pkt_numlen = 0; |
| 43 |
ppe->pkt_num = 0; |
| 44 |
ppe->cc = cc; |
| 45 |
} |
| 46 |
|
| 47 |
int ngtcp2_ppe_encode_hd(ngtcp2_ppe *ppe, const ngtcp2_pkt_hd *hd) { |
| 48 |
ngtcp2_ssize rv; |
| 49 |
ngtcp2_buf *buf = &ppe->buf; |
| 50 |
size_t buf_left = ngtcp2_buf_left(buf); |
| 51 |
ngtcp2_crypto_cc *cc = ppe->cc; |
| 52 |
|
| 53 |
if (buf_left <= cc->aead.max_overhead) { |
| 54 |
return NGTCP2_ERR_NOBUF; |
| 55 |
} |
| 56 |
|
| 57 |
if (hd->flags & NGTCP2_PKT_FLAG_LONG_FORM) { |
| 58 |
ppe->len_offset = 1 + 4 + 1 + hd->dcid.datalen + 1 + hd->scid.datalen; |
| 59 |
|
| 60 |
if (hd->type == NGTCP2_PKT_INITIAL) { |
| 61 |
ppe->len_offset += ngtcp2_put_uvarintlen(hd->tokenlen) + hd->tokenlen; |
| 62 |
} |
| 63 |
|
| 64 |
ppe->pkt_num_offset = ppe->len_offset + NGTCP2_PKT_LENGTHLEN; |
| 65 |
|
| 66 |
rv = ngtcp2_pkt_encode_hd_long(buf->last, buf_left - cc->aead.max_overhead, |
| 67 |
hd); |
| 68 |
} else { |
| 69 |
ppe->pkt_num_offset = 1 + hd->dcid.datalen; |
| 70 |
|
| 71 |
rv = ngtcp2_pkt_encode_hd_short(buf->last, buf_left - cc->aead.max_overhead, |
| 72 |
hd); |
| 73 |
} |
| 74 |
|
| 75 |
if (rv < 0) { |
| 76 |
return (int)rv; |
| 77 |
} |
| 78 |
|
| 79 |
buf->last += rv; |
| 80 |
|
| 81 |
ppe->pkt_numlen = hd->pkt_numlen; |
| 82 |
ppe->hdlen = (size_t)rv; |
| 83 |
ppe->pkt_num = hd->pkt_num; |
| 84 |
|
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
int ngtcp2_ppe_encode_frame(ngtcp2_ppe *ppe, ngtcp2_frame *fr) { |
| 89 |
ngtcp2_ssize rv; |
| 90 |
ngtcp2_buf *buf = &ppe->buf; |
| 91 |
size_t buf_left = ngtcp2_buf_left(buf); |
| 92 |
ngtcp2_crypto_cc *cc = ppe->cc; |
| 93 |
|
| 94 |
if (buf_left <= cc->aead.max_overhead) { |
| 95 |
return NGTCP2_ERR_NOBUF; |
| 96 |
} |
| 97 |
|
| 98 |
rv = ngtcp2_pkt_encode_frame(buf->last, buf_left - cc->aead.max_overhead, fr); |
| 99 |
if (rv < 0) { |
| 100 |
return (int)rv; |
| 101 |
} |
| 102 |
|
| 103 |
buf->last += rv; |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
static size_t ppe_sample_offset(ngtcp2_ppe *ppe) { |
| 113 |
return ppe->pkt_num_offset + 4; |
| 114 |
} |
| 115 |
|
| 116 |
ngtcp2_ssize ngtcp2_ppe_final(ngtcp2_ppe *ppe, const uint8_t **ppkt) { |
| 117 |
ngtcp2_buf *buf = &ppe->buf; |
| 118 |
ngtcp2_crypto_cc *cc = ppe->cc; |
| 119 |
uint8_t *payload = buf->begin + ppe->hdlen; |
| 120 |
size_t payloadlen = ngtcp2_buf_len(buf) - ppe->hdlen; |
| 121 |
uint8_t mask[NGTCP2_HP_SAMPLELEN]; |
| 122 |
uint8_t *p; |
| 123 |
size_t i; |
| 124 |
int rv; |
| 125 |
|
| 126 |
assert(cc->encrypt); |
| 127 |
assert(cc->hp_mask); |
| 128 |
|
| 129 |
if (ppe->len_offset) { |
| 130 |
ngtcp2_put_uvarint30( |
| 131 |
buf->begin + ppe->len_offset, |
| 132 |
(uint16_t)(payloadlen + ppe->pkt_numlen + cc->aead.max_overhead)); |
| 133 |
} |
| 134 |
|
| 135 |
ngtcp2_crypto_create_nonce(ppe->nonce, cc->ckm->iv.base, cc->ckm->iv.len, |
| 136 |
ppe->pkt_num); |
| 137 |
|
| 138 |
rv = cc->encrypt(payload, &cc->aead, &cc->ckm->aead_ctx, payload, payloadlen, |
| 139 |
ppe->nonce, cc->ckm->iv.len, buf->begin, ppe->hdlen); |
| 140 |
if (rv != 0) { |
| 141 |
return NGTCP2_ERR_CALLBACK_FAILURE; |
| 142 |
} |
| 143 |
|
| 144 |
buf->last = payload + payloadlen + cc->aead.max_overhead; |
| 145 |
|
| 146 |
|
| 147 |
assert(ppe_sample_offset(ppe) + NGTCP2_HP_SAMPLELEN <= ngtcp2_buf_len(buf)); |
| 148 |
|
| 149 |
rv = cc->hp_mask(mask, &cc->hp, &cc->hp_ctx, |
| 150 |
buf->begin + ppe_sample_offset(ppe)); |
| 151 |
if (rv != 0) { |
| 152 |
return NGTCP2_ERR_CALLBACK_FAILURE; |
| 153 |
} |
| 154 |
|
| 155 |
p = buf->begin; |
| 156 |
if (*p & NGTCP2_HEADER_FORM_BIT) { |
| 157 |
*p = (uint8_t)(*p ^ (mask[0] & 0x0f)); |
| 158 |
} else { |
| 159 |
*p = (uint8_t)(*p ^ (mask[0] & 0x1f)); |
| 160 |
} |
| 161 |
|
| 162 |
p = buf->begin + ppe->pkt_num_offset; |
| 163 |
for (i = 0; i < ppe->pkt_numlen; ++i) { |
| 164 |
*(p + i) ^= mask[i + 1]; |
| 165 |
} |
| 166 |
|
| 167 |
if (ppkt != NULL) { |
| 168 |
*ppkt = buf->begin; |
| 169 |
} |
| 170 |
|
| 171 |
return (ngtcp2_ssize)ngtcp2_buf_len(buf); |
| 172 |
} |
| 173 |
|
| 174 |
size_t ngtcp2_ppe_left(const ngtcp2_ppe *ppe) { |
| 175 |
ngtcp2_crypto_cc *cc = ppe->cc; |
| 176 |
size_t buf_left = ngtcp2_buf_left(&ppe->buf); |
| 177 |
|
| 178 |
if (buf_left <= cc->aead.max_overhead) { |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
return buf_left - cc->aead.max_overhead; |
| 183 |
} |
| 184 |
|
| 185 |
size_t ngtcp2_ppe_padding_size(ngtcp2_ppe *ppe, size_t n) { |
| 186 |
ngtcp2_crypto_cc *cc = ppe->cc; |
| 187 |
ngtcp2_buf *buf = &ppe->buf; |
| 188 |
size_t pktlen = ngtcp2_buf_len(buf) + cc->aead.max_overhead; |
| 189 |
size_t len = 0; |
| 190 |
size_t max_samplelen; |
| 191 |
|
| 192 |
n = ngtcp2_min_size(n, ngtcp2_buf_cap(buf)); |
| 193 |
if (pktlen < n) { |
| 194 |
len = n - pktlen; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
max_samplelen = |
| 199 |
ngtcp2_buf_len(buf) + cc->aead.max_overhead - ppe_sample_offset(ppe); |
| 200 |
|
| 201 |
if (max_samplelen < NGTCP2_HP_SAMPLELEN) { |
| 202 |
len = ngtcp2_max_size(len, NGTCP2_HP_SAMPLELEN - max_samplelen); |
| 203 |
} |
| 204 |
|
| 205 |
assert(ngtcp2_buf_left(buf) >= len + cc->aead.max_overhead); |
| 206 |
|
| 207 |
if (len == 0) { |
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
buf->last = ngtcp2_setmem(buf->last, 0, len); |
| 212 |
|
| 213 |
return len; |
| 214 |
} |
| 215 |
|
| 216 |
size_t ngtcp2_ppe_dgram_padding(ngtcp2_ppe *ppe) { |
| 217 |
return ngtcp2_ppe_dgram_padding_size(ppe, NGTCP2_MAX_UDP_PAYLOAD_SIZE); |
| 218 |
} |
| 219 |
|
| 220 |
size_t ngtcp2_ppe_dgram_padding_size(ngtcp2_ppe *ppe, size_t n) { |
| 221 |
ngtcp2_crypto_cc *cc = ppe->cc; |
| 222 |
ngtcp2_buf *buf = &ppe->buf; |
| 223 |
size_t dgramlen = |
| 224 |
ppe->dgram_offset + ngtcp2_buf_len(buf) + cc->aead.max_overhead; |
| 225 |
size_t len; |
| 226 |
|
| 227 |
n = ngtcp2_min_size(n, ppe->dgram_offset + ngtcp2_buf_cap(buf)); |
| 228 |
|
| 229 |
if (dgramlen >= n) { |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
len = n - dgramlen; |
| 234 |
buf->last = ngtcp2_setmem(buf->last, 0, len); |
| 235 |
|
| 236 |
return len; |
| 237 |
} |
| 238 |
|
| 239 |
int ngtcp2_ppe_ensure_hp_sample(ngtcp2_ppe *ppe) { |
| 240 |
ngtcp2_buf *buf = &ppe->buf; |
| 241 |
|
| 242 |
return ngtcp2_buf_left(buf) >= (4 - ppe->pkt_numlen) + NGTCP2_HP_SAMPLELEN; |
| 243 |
} |