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