2013-11-21 03:12:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Damien Miller <djm@mindrot.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-08-03 05:41:57 +00:00
|
|
|
/* $OpenBSD: cipher-chachapoly.c,v 1.8 2016/08/03 05:41:57 djm Exp $ */
|
2013-11-21 03:12:23 +00:00
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdarg.h> /* needed for log.h */
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h> /* needed for misc.h */
|
|
|
|
|
|
|
|
#include "log.h"
|
2014-07-02 05:28:02 +00:00
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "ssherr.h"
|
2013-11-21 03:12:23 +00:00
|
|
|
#include "cipher-chachapoly.h"
|
|
|
|
|
2016-08-03 05:41:57 +00:00
|
|
|
int
|
|
|
|
chachapoly_init(struct chachapoly_ctx *ctx,
|
2013-11-21 03:12:23 +00:00
|
|
|
const u_char *key, u_int keylen)
|
|
|
|
{
|
|
|
|
if (keylen != (32 + 32)) /* 2 x 256 bit keys */
|
2014-07-02 05:28:02 +00:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2013-11-21 03:12:23 +00:00
|
|
|
chacha_keysetup(&ctx->main_ctx, key, 256);
|
|
|
|
chacha_keysetup(&ctx->header_ctx, key + 32, 256);
|
2014-07-02 05:28:02 +00:00
|
|
|
return 0;
|
2013-11-21 03:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* chachapoly_crypt() operates as following:
|
2013-12-18 06:50:13 +00:00
|
|
|
* En/decrypt with header key 'aadlen' bytes from 'src', storing result
|
|
|
|
* to 'dest'. The ciphertext here is treated as additional authenticated
|
|
|
|
* data for MAC calculation.
|
|
|
|
* En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
|
|
|
|
* POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
|
|
|
|
* tag. This tag is written on encryption and verified on decryption.
|
2013-11-21 03:12:23 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
|
|
|
|
const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
|
|
|
|
{
|
|
|
|
u_char seqbuf[8];
|
2013-12-18 06:50:13 +00:00
|
|
|
const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */
|
2013-11-21 03:12:23 +00:00
|
|
|
u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
|
2014-07-02 05:28:02 +00:00
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2013-11-21 03:12:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Run ChaCha20 once to generate the Poly1305 key. The IV is the
|
|
|
|
* packet sequence number.
|
|
|
|
*/
|
2014-02-04 00:18:20 +00:00
|
|
|
memset(poly_key, 0, sizeof(poly_key));
|
2014-07-02 05:28:02 +00:00
|
|
|
POKE_U64(seqbuf, seqnr);
|
2013-11-21 03:12:23 +00:00
|
|
|
chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL);
|
|
|
|
chacha_encrypt_bytes(&ctx->main_ctx,
|
|
|
|
poly_key, poly_key, sizeof(poly_key));
|
|
|
|
|
|
|
|
/* If decrypting, check tag before anything else */
|
|
|
|
if (!do_encrypt) {
|
|
|
|
const u_char *tag = src + aadlen + len;
|
|
|
|
|
|
|
|
poly1305_auth(expected_tag, src, aadlen + len, poly_key);
|
2014-07-02 05:28:02 +00:00
|
|
|
if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
|
|
|
|
r = SSH_ERR_MAC_INVALID;
|
2013-11-21 03:12:23 +00:00
|
|
|
goto out;
|
2014-07-02 05:28:02 +00:00
|
|
|
}
|
2013-11-21 03:12:23 +00:00
|
|
|
}
|
2014-07-03 22:59:01 +00:00
|
|
|
|
2013-11-21 03:12:23 +00:00
|
|
|
/* Crypt additional data */
|
2013-12-18 06:50:13 +00:00
|
|
|
if (aadlen) {
|
2013-11-21 03:12:23 +00:00
|
|
|
chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
|
|
|
|
chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen);
|
|
|
|
}
|
2014-07-03 22:59:01 +00:00
|
|
|
|
|
|
|
/* Set Chacha's block counter to 1 */
|
|
|
|
chacha_ivsetup(&ctx->main_ctx, seqbuf, one);
|
2013-11-21 03:12:23 +00:00
|
|
|
chacha_encrypt_bytes(&ctx->main_ctx, src + aadlen,
|
|
|
|
dest + aadlen, len);
|
|
|
|
|
|
|
|
/* If encrypting, calculate and append tag */
|
|
|
|
if (do_encrypt) {
|
|
|
|
poly1305_auth(dest + aadlen + len, dest, aadlen + len,
|
|
|
|
poly_key);
|
|
|
|
}
|
|
|
|
r = 0;
|
|
|
|
out:
|
2014-02-04 00:18:20 +00:00
|
|
|
explicit_bzero(expected_tag, sizeof(expected_tag));
|
|
|
|
explicit_bzero(seqbuf, sizeof(seqbuf));
|
|
|
|
explicit_bzero(poly_key, sizeof(poly_key));
|
2013-11-21 03:12:23 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-12-18 06:50:13 +00:00
|
|
|
/* Decrypt and extract the encrypted packet length */
|
2013-11-21 03:12:23 +00:00
|
|
|
int
|
|
|
|
chachapoly_get_length(struct chachapoly_ctx *ctx,
|
|
|
|
u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
|
|
|
|
{
|
|
|
|
u_char buf[4], seqbuf[8];
|
|
|
|
|
|
|
|
if (len < 4)
|
2014-07-02 05:28:02 +00:00
|
|
|
return SSH_ERR_MESSAGE_INCOMPLETE;
|
|
|
|
POKE_U64(seqbuf, seqnr);
|
2013-11-21 03:12:23 +00:00
|
|
|
chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
|
|
|
|
chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4);
|
2014-07-02 05:28:02 +00:00
|
|
|
*plenp = PEEK_U32(buf);
|
2013-11-21 03:12:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|