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