[BACK]Return to cipher-chachapoly.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/cipher-chachapoly.c, Revision 1.6

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