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.9 2020/04/03 04:27:03 djm Exp $ */ 18 #include "includes.h" 19 __RCSID("$NetBSD: cipher-chachapoly.c,v 1.6 2020/05/28 17:05:49 christos Exp $"); 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" 27 #include "sshbuf.h" 28 #include "ssherr.h" 29 #include "cipher-chachapoly.h" 30 31 struct chachapoly_ctx { 32 struct chacha_ctx main_ctx, header_ctx; 33 }; 34 35 struct chachapoly_ctx * 36 chachapoly_new(const u_char *key, u_int keylen) 37 { 38 struct chachapoly_ctx *ctx; 39 40 if (keylen != (32 + 32)) /* 2 x 256 bit keys */ 41 return NULL; 42 if ((ctx = calloc(1, sizeof(*ctx))) == NULL) 43 return NULL; 44 chacha_keysetup(&ctx->main_ctx, key, 256); 45 chacha_keysetup(&ctx->header_ctx, key + 32, 256); 46 return ctx; 47 } 48 49 void 50 chachapoly_free(struct chachapoly_ctx *cpctx) 51 { 52 freezero(cpctx, sizeof(*cpctx)); 53 } 54 55 /* 56 * chachapoly_crypt() operates as following: 57 * En/decrypt with header key 'aadlen' bytes from 'src', storing result 58 * to 'dest'. The ciphertext here is treated as additional authenticated 59 * data for MAC calculation. 60 * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use 61 * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication 62 * tag. This tag is written on encryption and verified on decryption. 63 */ 64 int 65 chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest, 66 const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt) 67 { 68 u_char seqbuf[8]; 69 const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */ 70 u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN]; 71 int r = SSH_ERR_INTERNAL_ERROR; 72 73 /* 74 * Run ChaCha20 once to generate the Poly1305 key. The IV is the 75 * packet sequence number. 76 */ 77 memset(poly_key, 0, sizeof(poly_key)); 78 POKE_U64(seqbuf, seqnr); 79 chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL); 80 chacha_encrypt_bytes(&ctx->main_ctx, 81 poly_key, poly_key, sizeof(poly_key)); 82 83 /* If decrypting, check tag before anything else */ 84 if (!do_encrypt) { 85 const u_char *tag = src + aadlen + len; 86 87 poly1305_auth(expected_tag, src, aadlen + len, poly_key); 88 if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) { 89 r = SSH_ERR_MAC_INVALID; 90 goto out; 91 } 92 } 93 94 /* Crypt additional data */ 95 if (aadlen) { 96 chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); 97 chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen); 98 } 99 100 /* Set Chacha's block counter to 1 */ 101 chacha_ivsetup(&ctx->main_ctx, seqbuf, one); 102 chacha_encrypt_bytes(&ctx->main_ctx, src + aadlen, 103 dest + aadlen, len); 104 105 /* If encrypting, calculate and append tag */ 106 if (do_encrypt) { 107 poly1305_auth(dest + aadlen + len, dest, aadlen + len, 108 poly_key); 109 } 110 r = 0; 111 out: 112 explicit_bzero(expected_tag, sizeof(expected_tag)); 113 explicit_bzero(seqbuf, sizeof(seqbuf)); 114 explicit_bzero(poly_key, sizeof(poly_key)); 115 return r; 116 } 117 118 /* Decrypt and extract the encrypted packet length */ 119 int 120 chachapoly_get_length(struct chachapoly_ctx *ctx, 121 u_int *plenp, u_int seqnr, const u_char *cp, u_int len) 122 { 123 u_char buf[4], seqbuf[8]; 124 125 if (len < 4) 126 return SSH_ERR_MESSAGE_INCOMPLETE; 127 POKE_U64(seqbuf, seqnr); 128 chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); 129 chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4); 130 *plenp = PEEK_U32(buf); 131 return 0; 132 } 133