1 /* $OpenBSD: cipher-chachapoly.c,v 1.10 2023/07/17 05:26:38 djm Exp $ */ 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 */ 17 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" 24 #include "sshbuf.h" 25 #include "ssherr.h" 26 #include "cipher-chachapoly.h" 27 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) 34 { 35 struct chachapoly_ctx *ctx; 36 37 if (keylen != (32 + 32)) /* 2 x 256 bit keys */ 38 return NULL; 39 if ((ctx = calloc(1, sizeof(*ctx))) == NULL) 40 return NULL; 41 chacha_keysetup(&ctx->main_ctx, key, 256); 42 chacha_keysetup(&ctx->header_ctx, key + 32, 256); 43 return ctx; 44 } 45 46 void 47 chachapoly_free(struct chachapoly_ctx *cpctx) 48 { 49 freezero(cpctx, sizeof(*cpctx)); 50 } 51 52 /* 53 * chachapoly_crypt() operates as following: 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. 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]; 66 const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */ 67 u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN]; 68 int r = SSH_ERR_INTERNAL_ERROR; 69 70 /* 71 * Run ChaCha20 once to generate the Poly1305 key. The IV is the 72 * packet sequence number. 73 */ 74 memset(poly_key, 0, sizeof(poly_key)); 75 POKE_U64(seqbuf, seqnr); 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); 85 if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) { 86 r = SSH_ERR_MAC_INVALID; 87 goto out; 88 } 89 } 90 91 /* Crypt additional data */ 92 if (aadlen) { 93 chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); 94 chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen); 95 } 96 97 /* Set Chacha's block counter to 1 */ 98 chacha_ivsetup(&ctx->main_ctx, seqbuf, one); 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: 109 explicit_bzero(expected_tag, sizeof(expected_tag)); 110 explicit_bzero(seqbuf, sizeof(seqbuf)); 111 explicit_bzero(poly_key, sizeof(poly_key)); 112 return r; 113 } 114 115 /* Decrypt and extract the encrypted packet length */ 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) 123 return SSH_ERR_MESSAGE_INCOMPLETE; 124 POKE_U64(seqbuf, seqnr); 125 chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); 126 chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4); 127 *plenp = PEEK_U32(buf); 128 return 0; 129 } 130