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