blob: 7f31ff4ce5a1bfea1248c32cb1274f35da16d635 [file] [log] [blame]
Damien Miller0fde8ac2013-11-21 14:12:23 +11001/*
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
markus@openbsd.org60c2c4e2015-01-14 10:24:42 +000017/* $OpenBSD: cipher-chachapoly.c,v 1.7 2015/01/14 10:24:42 markus Exp $ */
Damien Miller0fde8ac2013-11-21 14:12:23 +110018
19#include "includes.h"
20
21#include <sys/types.h>
22#include <stdarg.h> /* needed for log.h */
23#include <string.h>
24#include <stdio.h> /* needed for misc.h */
25
26#include "log.h"
Damien Miller86687062014-07-02 15:28:02 +100027#include "sshbuf.h"
28#include "ssherr.h"
Damien Miller0fde8ac2013-11-21 14:12:23 +110029#include "cipher-chachapoly.h"
30
Damien Miller86687062014-07-02 15:28:02 +100031int chachapoly_init(struct chachapoly_ctx *ctx,
Damien Miller0fde8ac2013-11-21 14:12:23 +110032 const u_char *key, u_int keylen)
33{
34 if (keylen != (32 + 32)) /* 2 x 256 bit keys */
Damien Miller86687062014-07-02 15:28:02 +100035 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller0fde8ac2013-11-21 14:12:23 +110036 chacha_keysetup(&ctx->main_ctx, key, 256);
37 chacha_keysetup(&ctx->header_ctx, key + 32, 256);
Damien Miller86687062014-07-02 15:28:02 +100038 return 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +110039}
40
41/*
42 * chachapoly_crypt() operates as following:
Damien Millerd58a5962013-12-18 17:50:13 +110043 * En/decrypt with header key 'aadlen' bytes from 'src', storing result
44 * to 'dest'. The ciphertext here is treated as additional authenticated
45 * data for MAC calculation.
46 * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
47 * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
48 * tag. This tag is written on encryption and verified on decryption.
Damien Miller0fde8ac2013-11-21 14:12:23 +110049 */
50int
51chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
52 const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
53{
54 u_char seqbuf[8];
Damien Millerd58a5962013-12-18 17:50:13 +110055 const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */
Damien Miller0fde8ac2013-11-21 14:12:23 +110056 u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
Damien Miller86687062014-07-02 15:28:02 +100057 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller0fde8ac2013-11-21 14:12:23 +110058
59 /*
60 * Run ChaCha20 once to generate the Poly1305 key. The IV is the
61 * packet sequence number.
62 */
Damien Miller1d2c4562014-02-04 11:18:20 +110063 memset(poly_key, 0, sizeof(poly_key));
Damien Miller86687062014-07-02 15:28:02 +100064 POKE_U64(seqbuf, seqnr);
Damien Miller0fde8ac2013-11-21 14:12:23 +110065 chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL);
66 chacha_encrypt_bytes(&ctx->main_ctx,
67 poly_key, poly_key, sizeof(poly_key));
Damien Miller0fde8ac2013-11-21 14:12:23 +110068
69 /* If decrypting, check tag before anything else */
70 if (!do_encrypt) {
71 const u_char *tag = src + aadlen + len;
72
73 poly1305_auth(expected_tag, src, aadlen + len, poly_key);
Damien Miller86687062014-07-02 15:28:02 +100074 if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
75 r = SSH_ERR_MAC_INVALID;
Damien Miller0fde8ac2013-11-21 14:12:23 +110076 goto out;
Damien Miller86687062014-07-02 15:28:02 +100077 }
Damien Miller0fde8ac2013-11-21 14:12:23 +110078 }
Damien Millerd2c3cd52014-07-04 08:59:01 +100079
Damien Miller0fde8ac2013-11-21 14:12:23 +110080 /* Crypt additional data */
Damien Millerd58a5962013-12-18 17:50:13 +110081 if (aadlen) {
Damien Miller0fde8ac2013-11-21 14:12:23 +110082 chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
83 chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen);
84 }
Damien Millerd2c3cd52014-07-04 08:59:01 +100085
86 /* Set Chacha's block counter to 1 */
87 chacha_ivsetup(&ctx->main_ctx, seqbuf, one);
Damien Miller0fde8ac2013-11-21 14:12:23 +110088 chacha_encrypt_bytes(&ctx->main_ctx, src + aadlen,
89 dest + aadlen, len);
90
91 /* If encrypting, calculate and append tag */
92 if (do_encrypt) {
93 poly1305_auth(dest + aadlen + len, dest, aadlen + len,
94 poly_key);
95 }
96 r = 0;
Damien Miller0fde8ac2013-11-21 14:12:23 +110097 out:
Damien Miller1d2c4562014-02-04 11:18:20 +110098 explicit_bzero(expected_tag, sizeof(expected_tag));
99 explicit_bzero(seqbuf, sizeof(seqbuf));
100 explicit_bzero(poly_key, sizeof(poly_key));
Damien Miller0fde8ac2013-11-21 14:12:23 +1100101 return r;
102}
103
Damien Millerd58a5962013-12-18 17:50:13 +1100104/* Decrypt and extract the encrypted packet length */
Damien Miller0fde8ac2013-11-21 14:12:23 +1100105int
106chachapoly_get_length(struct chachapoly_ctx *ctx,
107 u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
108{
109 u_char buf[4], seqbuf[8];
110
111 if (len < 4)
Damien Miller86687062014-07-02 15:28:02 +1000112 return SSH_ERR_MESSAGE_INCOMPLETE;
113 POKE_U64(seqbuf, seqnr);
Damien Miller0fde8ac2013-11-21 14:12:23 +1100114 chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
115 chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4);
Damien Miller86687062014-07-02 15:28:02 +1000116 *plenp = PEEK_U32(buf);
Damien Miller0fde8ac2013-11-21 14:12:23 +1100117 return 0;
118}