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