1 /* $NetBSD: cipher-chachapoly-libcrypto.c,v 1.2 2021/09/03 10:30:33 christos 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 /* $OpenBSD: cipher-chachapoly-libcrypto.c,v 1.1 2020/04/03 04:32:21 djm Exp $ */ 19 #include "includes.h" 20 __RCSID("$NetBSD: cipher-chachapoly-libcrypto.c,v 1.2 2021/09/03 10:30:33 christos Exp $"); 21 22 #include <sys/types.h> 23 #include <stdarg.h> /* needed for log.h */ 24 #include <string.h> 25 #include <stdio.h> /* needed for misc.h */ 26 27 #include <openssl/evp.h> 28 29 #include "log.h" 30 #include "sshbuf.h" 31 #include "ssherr.h" 32 #include "cipher-chachapoly.h" 33 34 struct chachapoly_ctx { 35 EVP_CIPHER_CTX *main_evp, *header_evp; 36 }; 37 38 struct chachapoly_ctx * 39 chachapoly_new(const u_char *key, u_int keylen) 40 { 41 struct chachapoly_ctx *ctx; 42 43 if (keylen != (32 + 32)) /* 2 x 256 bit keys */ 44 return NULL; 45 if ((ctx = calloc(1, sizeof(*ctx))) == NULL) 46 return NULL; 47 if ((ctx->main_evp = EVP_CIPHER_CTX_new()) == NULL || 48 (ctx->header_evp = EVP_CIPHER_CTX_new()) == NULL) 49 goto fail; 50 if (!EVP_CipherInit(ctx->main_evp, EVP_chacha20(), key, NULL, 1)) 51 goto fail; 52 if (!EVP_CipherInit(ctx->header_evp, EVP_chacha20(), key + 32, NULL, 1)) 53 goto fail; 54 if (EVP_CIPHER_CTX_iv_length(ctx->header_evp) != 16) 55 goto fail; 56 return ctx; 57 fail: 58 chachapoly_free(ctx); 59 return NULL; 60 } 61 62 void 63 chachapoly_free(struct chachapoly_ctx *cpctx) 64 { 65 if (cpctx == NULL) 66 return; 67 EVP_CIPHER_CTX_free(cpctx->main_evp); 68 EVP_CIPHER_CTX_free(cpctx->header_evp); 69 freezero(cpctx, sizeof(*cpctx)); 70 } 71 72 /* 73 * chachapoly_crypt() operates as following: 74 * En/decrypt with header key 'aadlen' bytes from 'src', storing result 75 * to 'dest'. The ciphertext here is treated as additional authenticated 76 * data for MAC calculation. 77 * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use 78 * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication 79 * tag. This tag is written on encryption and verified on decryption. 80 */ 81 int 82 chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest, 83 const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt) 84 { 85 u_char seqbuf[16]; /* layout: u64 counter || u64 seqno */ 86 int r = SSH_ERR_INTERNAL_ERROR; 87 u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN]; 88 89 /* 90 * Run ChaCha20 once to generate the Poly1305 key. The IV is the 91 * packet sequence number. 92 */ 93 memset(seqbuf, 0, sizeof(seqbuf)); 94 POKE_U64(seqbuf + 8, seqnr); 95 memset(poly_key, 0, sizeof(poly_key)); 96 if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) || 97 EVP_Cipher(ctx->main_evp, poly_key, 98 poly_key, sizeof(poly_key)) < 0) { 99 r = SSH_ERR_LIBCRYPTO_ERROR; 100 goto out; 101 } 102 103 /* If decrypting, check tag before anything else */ 104 if (!do_encrypt) { 105 const u_char *tag = src + aadlen + len; 106 107 poly1305_auth(expected_tag, src, aadlen + len, poly_key); 108 if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) { 109 r = SSH_ERR_MAC_INVALID; 110 goto out; 111 } 112 } 113 114 /* Crypt additional data */ 115 if (aadlen) { 116 if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 1) || 117 EVP_Cipher(ctx->header_evp, dest, src, aadlen) < 0) { 118 r = SSH_ERR_LIBCRYPTO_ERROR; 119 goto out; 120 } 121 } 122 123 /* Set Chacha's block counter to 1 */ 124 seqbuf[0] = 1; 125 if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) || 126 EVP_Cipher(ctx->main_evp, dest + aadlen, src + aadlen, len) < 0) { 127 r = SSH_ERR_LIBCRYPTO_ERROR; 128 goto out; 129 } 130 131 /* If encrypting, calculate and append tag */ 132 if (do_encrypt) { 133 poly1305_auth(dest + aadlen + len, dest, aadlen + len, 134 poly_key); 135 } 136 r = 0; 137 out: 138 explicit_bzero(expected_tag, sizeof(expected_tag)); 139 explicit_bzero(seqbuf, sizeof(seqbuf)); 140 explicit_bzero(poly_key, sizeof(poly_key)); 141 return r; 142 } 143 144 /* Decrypt and extract the encrypted packet length */ 145 int 146 chachapoly_get_length(struct chachapoly_ctx *ctx, 147 u_int *plenp, u_int seqnr, const u_char *cp, u_int len) 148 { 149 u_char buf[4], seqbuf[16]; 150 151 if (len < 4) 152 return SSH_ERR_MESSAGE_INCOMPLETE; 153 memset(seqbuf, 0, sizeof(seqbuf)); 154 POKE_U64(seqbuf + 8, seqnr); 155 if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 0)) 156 return SSH_ERR_LIBCRYPTO_ERROR; 157 if (EVP_Cipher(ctx->header_evp, buf, (u_char *)cp, sizeof(buf)) < 0) 158 return SSH_ERR_LIBCRYPTO_ERROR; 159 *plenp = PEEK_U32(buf); 160 return 0; 161 } 162