GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/ssh/lib/../cipher-chachapoly.c Lines: 41 43 95.3 %
Date: 2017-11-07 Branches: 12 15 80.0 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
3
 *
4
 * Permission to use, copy, modify, and distribute this software for any
5
 * purpose with or without fee is hereby granted, provided that the above
6
 * copyright notice and this permission notice appear in all copies.
7
 *
8
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
 */
16
17
/* $OpenBSD: cipher-chachapoly.c,v 1.8 2016/08/03 05:41:57 djm Exp $ */
18
19
#include <sys/types.h>
20
#include <stdarg.h> /* needed for log.h */
21
#include <string.h>
22
#include <stdio.h>  /* needed for misc.h */
23
24
#include "log.h"
25
#include "sshbuf.h"
26
#include "ssherr.h"
27
#include "cipher-chachapoly.h"
28
29
int
30
chachapoly_init(struct chachapoly_ctx *ctx,
31
    const u_char *key, u_int keylen)
32
{
33
4
	if (keylen != (32 + 32)) /* 2 x 256 bit keys */
34
		return SSH_ERR_INVALID_ARGUMENT;
35
2
	chacha_keysetup(&ctx->main_ctx, key, 256);
36
2
	chacha_keysetup(&ctx->header_ctx, key + 32, 256);
37
2
	return 0;
38
2
}
39
40
/*
41
 * chachapoly_crypt() operates as following:
42
 * En/decrypt with header key 'aadlen' bytes from 'src', storing result
43
 * to 'dest'. The ciphertext here is treated as additional authenticated
44
 * data for MAC calculation.
45
 * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
46
 * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
47
 * tag. This tag is written on encryption and verified on decryption.
48
 */
49
int
50
chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
51
    const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
52
{
53
114932
	u_char seqbuf[8];
54
	const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */
55
57466
	u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
56
	int r = SSH_ERR_INTERNAL_ERROR;
57
58
	/*
59
	 * Run ChaCha20 once to generate the Poly1305 key. The IV is the
60
	 * packet sequence number.
61
	 */
62
57466
	memset(poly_key, 0, sizeof(poly_key));
63
57466
	POKE_U64(seqbuf, seqnr);
64
57466
	chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL);
65
57466
	chacha_encrypt_bytes(&ctx->main_ctx,
66
	    poly_key, poly_key, sizeof(poly_key));
67
68
	/* If decrypting, check tag before anything else */
69
57466
	if (!do_encrypt) {
70
14817
		const u_char *tag = src + aadlen + len;
71
72
14817
		poly1305_auth(expected_tag, src, aadlen + len, poly_key);
73
14817
		if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
74
			r = SSH_ERR_MAC_INVALID;
75
			goto out;
76
		}
77
14817
	}
78
79
	/* Crypt additional data */
80
57466
	if (aadlen) {
81
57466
		chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
82
57466
		chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen);
83
57466
	}
84
85
	/* Set Chacha's block counter to 1 */
86
57466
	chacha_ivsetup(&ctx->main_ctx, seqbuf, one);
87
114932
	chacha_encrypt_bytes(&ctx->main_ctx, src + aadlen,
88
57466
	    dest + aadlen, len);
89
90
	/* If encrypting, calculate and append tag */
91
57466
	if (do_encrypt) {
92
42649
		poly1305_auth(dest + aadlen + len, dest, aadlen + len,
93
		    poly_key);
94
42649
	}
95
57466
	r = 0;
96
 out:
97
57466
	explicit_bzero(expected_tag, sizeof(expected_tag));
98
57466
	explicit_bzero(seqbuf, sizeof(seqbuf));
99
57466
	explicit_bzero(poly_key, sizeof(poly_key));
100
57466
	return r;
101
57466
}
102
103
/* Decrypt and extract the encrypted packet length */
104
int
105
chachapoly_get_length(struct chachapoly_ctx *ctx,
106
    u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
107
{
108
176442
	u_char buf[4], seqbuf[8];
109
110
88221
	if (len < 4)
111
73404
		return SSH_ERR_MESSAGE_INCOMPLETE;
112
14817
	POKE_U64(seqbuf, seqnr);
113
14817
	chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
114
14817
	chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4);
115
14817
	*plenp = PEEK_U32(buf);
116
14817
	return 0;
117
88221
}