/** Verifies and decrypts a packet */ static bool method_decrypt(fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in, bool *reordered) { if (in.len < COMMON_HEADBYTES+sizeof(fastd_block128_t)) return false; if (!method_session_is_valid(session)) return false; uint8_t in_nonce[COMMON_NONCEBYTES]; uint8_t flags; int64_t age; if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age)) return false; if (flags) return false; uint8_t nonce[session->method->cipher_info->iv_length] __attribute__((aligned(8))); fastd_method_expand_nonce(nonce, in_nonce, sizeof(nonce)); size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; *out = fastd_buffer_alloc(in.len, 0, tail_len); int n_blocks = block_count(in.len, sizeof(fastd_block128_t)); fastd_block128_t *inblocks = in.data; fastd_block128_t *outblocks = out->data; fastd_block128_t tag; bool ok = session->cipher->crypt(session->cipher_state, outblocks, inblocks, n_blocks*sizeof(fastd_block128_t), nonce); if (ok) { if (tail_len) memset(in.data+in.len, 0, tail_len); put_size(&inblocks[n_blocks], in.len-sizeof(fastd_block128_t)); ok = session->ghash->digest(session->ghash_state, &tag, inblocks+1, n_blocks*sizeof(fastd_block128_t)); } if (!ok || !block_equal(&tag, &outblocks[0])) { fastd_buffer_free(*out); return false; } fastd_buffer_free(in); fastd_buffer_push_head(out, sizeof(fastd_block128_t)); fastd_tristate_t reorder_check = fastd_method_reorder_check(peer, &session->common, in_nonce, age); if (reorder_check.set) { *reordered = reorder_check.state; } else { fastd_buffer_free(*out); *out = fastd_buffer_alloc(0, 0, 0); } return true; }
/** Encrypts and authenticates a packet */ static bool method_encrypt(UNUSED fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { fastd_buffer_pull_head_zero(&in, sizeof(fastd_block128_t)); size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; *out = fastd_buffer_alloc(in.len, alignto(COMMON_HEADBYTES, 16), sizeof(fastd_block128_t)+tail_len); if (tail_len) memset(in.data+in.len, 0, tail_len); uint8_t nonce[session->method->cipher_info->iv_length] __attribute__((aligned(8))); fastd_method_expand_nonce(nonce, session->common.send_nonce, sizeof(nonce)); int n_blocks = block_count(in.len, sizeof(fastd_block128_t)); fastd_block128_t *inblocks = in.data; fastd_block128_t *outblocks = out->data; fastd_block128_t tag; bool ok = session->cipher->crypt(session->cipher_state, outblocks, inblocks, n_blocks*sizeof(fastd_block128_t), nonce); if (ok) { if (tail_len) memset(out->data+out->len, 0, tail_len); put_size(&outblocks[n_blocks], in.len-sizeof(fastd_block128_t)); ok = session->ghash->digest(session->ghash_state, &tag, outblocks+1, n_blocks*sizeof(fastd_block128_t)); } if (!ok) { fastd_buffer_free(*out); return false; } xor_a(&outblocks[0], &tag); fastd_buffer_free(in); fastd_method_put_common_header(out, session->common.send_nonce, 0); fastd_method_increment_nonce(&session->common); return true; }
void read_in_sizes(const char *from_file) { FILE *f = fopen(from_file, "rt"); if (f==NULL) { return; } char *line = (char *) mc_malloc(10240*sizeof(char)); if (fgets(line, 10240, f) != NULL) { char *ln = trim(line); if (strcmp(ln, VFILESIZE_FILE_TYPE) == 0) { if (fgets(line, 10240, f) != NULL) { char *ln1 = trim(line); if (strcmp(ln1, VFILESIZE_FILE_VERSION) == 0) { while (fgets(line, 10240, f) != NULL) { char *ln2 = trim(line); char *vfile = mc_strdup( ln2 ); fgets(line, 10240, f); char *ln3 = trim( line ); size_t size = (size_t) strtoul(ln3, NULL, 10); fgets(line, 10240, f); char *ln4 = trim (line); time_t mtime = (time_t) strtoul(ln4, NULL, 10); put_size( vfile, size, mtime ); mc_free( vfile); mc_free( ln4 ); mc_free( ln3 ); mc_free( ln2 ); } } mc_free( ln1 ); } } mc_free( ln ); } mc_free(line); fclose(f); }