1*254cc503Sdjm /* $OpenBSD: cipher-chachapoly.c,v 1.10 2023/07/17 05:26:38 djm Exp $ */
21edbfe23Sdjm /*
31edbfe23Sdjm * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
41edbfe23Sdjm *
51edbfe23Sdjm * Permission to use, copy, modify, and distribute this software for any
61edbfe23Sdjm * purpose with or without fee is hereby granted, provided that the above
71edbfe23Sdjm * copyright notice and this permission notice appear in all copies.
81edbfe23Sdjm *
91edbfe23Sdjm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
101edbfe23Sdjm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
111edbfe23Sdjm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
121edbfe23Sdjm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
131edbfe23Sdjm * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
141edbfe23Sdjm * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
151edbfe23Sdjm * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
161edbfe23Sdjm */
171edbfe23Sdjm
181edbfe23Sdjm #include <sys/types.h>
191edbfe23Sdjm #include <stdarg.h> /* needed for log.h */
201edbfe23Sdjm #include <string.h>
211edbfe23Sdjm #include <stdio.h> /* needed for misc.h */
221edbfe23Sdjm
231edbfe23Sdjm #include "log.h"
24ea2d8289Sdjm #include "sshbuf.h"
25ea2d8289Sdjm #include "ssherr.h"
261edbfe23Sdjm #include "cipher-chachapoly.h"
271edbfe23Sdjm
284ab9dea6Sdjm struct chachapoly_ctx {
294ab9dea6Sdjm struct chacha_ctx main_ctx, header_ctx;
304ab9dea6Sdjm };
314ab9dea6Sdjm
324ab9dea6Sdjm struct chachapoly_ctx *
chachapoly_new(const u_char * key,u_int keylen)334ab9dea6Sdjm chachapoly_new(const u_char *key, u_int keylen)
341edbfe23Sdjm {
354ab9dea6Sdjm struct chachapoly_ctx *ctx;
364ab9dea6Sdjm
371edbfe23Sdjm if (keylen != (32 + 32)) /* 2 x 256 bit keys */
384ab9dea6Sdjm return NULL;
394ab9dea6Sdjm if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
404ab9dea6Sdjm return NULL;
411edbfe23Sdjm chacha_keysetup(&ctx->main_ctx, key, 256);
421edbfe23Sdjm chacha_keysetup(&ctx->header_ctx, key + 32, 256);
434ab9dea6Sdjm return ctx;
444ab9dea6Sdjm }
454ab9dea6Sdjm
464ab9dea6Sdjm void
chachapoly_free(struct chachapoly_ctx * cpctx)474ab9dea6Sdjm chachapoly_free(struct chachapoly_ctx *cpctx)
484ab9dea6Sdjm {
494ab9dea6Sdjm freezero(cpctx, sizeof(*cpctx));
501edbfe23Sdjm }
511edbfe23Sdjm
521edbfe23Sdjm /*
531edbfe23Sdjm * chachapoly_crypt() operates as following:
545f4c1c01Sdjm * En/decrypt with header key 'aadlen' bytes from 'src', storing result
555f4c1c01Sdjm * to 'dest'. The ciphertext here is treated as additional authenticated
565f4c1c01Sdjm * data for MAC calculation.
575f4c1c01Sdjm * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
585f4c1c01Sdjm * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
595f4c1c01Sdjm * tag. This tag is written on encryption and verified on decryption.
601edbfe23Sdjm */
611edbfe23Sdjm 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)621edbfe23Sdjm chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
631edbfe23Sdjm const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
641edbfe23Sdjm {
651edbfe23Sdjm u_char seqbuf[8];
665f4c1c01Sdjm const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */
671edbfe23Sdjm u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
68ea2d8289Sdjm int r = SSH_ERR_INTERNAL_ERROR;
691edbfe23Sdjm
701edbfe23Sdjm /*
711edbfe23Sdjm * Run ChaCha20 once to generate the Poly1305 key. The IV is the
721edbfe23Sdjm * packet sequence number.
731edbfe23Sdjm */
7469881b76Stedu memset(poly_key, 0, sizeof(poly_key));
75ea2d8289Sdjm POKE_U64(seqbuf, seqnr);
761edbfe23Sdjm chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL);
771edbfe23Sdjm chacha_encrypt_bytes(&ctx->main_ctx,
781edbfe23Sdjm poly_key, poly_key, sizeof(poly_key));
791edbfe23Sdjm
801edbfe23Sdjm /* If decrypting, check tag before anything else */
811edbfe23Sdjm if (!do_encrypt) {
821edbfe23Sdjm const u_char *tag = src + aadlen + len;
831edbfe23Sdjm
841edbfe23Sdjm poly1305_auth(expected_tag, src, aadlen + len, poly_key);
85ea2d8289Sdjm if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
86ea2d8289Sdjm r = SSH_ERR_MAC_INVALID;
871edbfe23Sdjm goto out;
881edbfe23Sdjm }
89ea2d8289Sdjm }
9081ceb99fSjsing
911edbfe23Sdjm /* Crypt additional data */
921edbfe23Sdjm if (aadlen) {
931edbfe23Sdjm chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
941edbfe23Sdjm chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen);
951edbfe23Sdjm }
9681ceb99fSjsing
9781ceb99fSjsing /* Set Chacha's block counter to 1 */
9881ceb99fSjsing chacha_ivsetup(&ctx->main_ctx, seqbuf, one);
991edbfe23Sdjm chacha_encrypt_bytes(&ctx->main_ctx, src + aadlen,
1001edbfe23Sdjm dest + aadlen, len);
1011edbfe23Sdjm
1021edbfe23Sdjm /* If encrypting, calculate and append tag */
1031edbfe23Sdjm if (do_encrypt) {
1041edbfe23Sdjm poly1305_auth(dest + aadlen + len, dest, aadlen + len,
1051edbfe23Sdjm poly_key);
1061edbfe23Sdjm }
1071edbfe23Sdjm r = 0;
1081edbfe23Sdjm out:
10969881b76Stedu explicit_bzero(expected_tag, sizeof(expected_tag));
11069881b76Stedu explicit_bzero(seqbuf, sizeof(seqbuf));
11169881b76Stedu explicit_bzero(poly_key, sizeof(poly_key));
1121edbfe23Sdjm return r;
1131edbfe23Sdjm }
1141edbfe23Sdjm
1155f4c1c01Sdjm /* Decrypt and extract the encrypted packet length */
1161edbfe23Sdjm int
chachapoly_get_length(struct chachapoly_ctx * ctx,u_int * plenp,u_int seqnr,const u_char * cp,u_int len)1171edbfe23Sdjm chachapoly_get_length(struct chachapoly_ctx *ctx,
1181edbfe23Sdjm u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
1191edbfe23Sdjm {
1201edbfe23Sdjm u_char buf[4], seqbuf[8];
1211edbfe23Sdjm
1221edbfe23Sdjm if (len < 4)
123ea2d8289Sdjm return SSH_ERR_MESSAGE_INCOMPLETE;
124ea2d8289Sdjm POKE_U64(seqbuf, seqnr);
1251edbfe23Sdjm chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
1261edbfe23Sdjm chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4);
127ea2d8289Sdjm *plenp = PEEK_U32(buf);
1281edbfe23Sdjm return 0;
1291edbfe23Sdjm }
130