1fc8fc743SJohn Baldwin /*- 2fc8fc743SJohn Baldwin * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fc8fc743SJohn Baldwin * 4fc8fc743SJohn Baldwin * Copyright (c) 2020 Netflix Inc. 5fc8fc743SJohn Baldwin * 6fc8fc743SJohn Baldwin * Redistribution and use in source and binary forms, with or without 7fc8fc743SJohn Baldwin * modification, are permitted provided that the following conditions 8fc8fc743SJohn Baldwin * are met: 9fc8fc743SJohn Baldwin * 1. Redistributions of source code must retain the above copyright 10fc8fc743SJohn Baldwin * notice, this list of conditions and the following disclaimer. 11fc8fc743SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright 12fc8fc743SJohn Baldwin * notice, this list of conditions and the following disclaimer in the 13fc8fc743SJohn Baldwin * documentation and/or other materials provided with the distribution. 14fc8fc743SJohn Baldwin * 15fc8fc743SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 16fc8fc743SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17fc8fc743SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18fc8fc743SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 19fc8fc743SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20fc8fc743SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21fc8fc743SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22fc8fc743SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23fc8fc743SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24fc8fc743SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25fc8fc743SJohn Baldwin * SUCH DAMAGE. 26fc8fc743SJohn Baldwin */ 27fc8fc743SJohn Baldwin 28*dd2e1352SJohn Baldwin #include <opencrypto/xform_auth.h> 29fc8fc743SJohn Baldwin #include <opencrypto/xform_enc.h> 30fc8fc743SJohn Baldwin 31*dd2e1352SJohn Baldwin #include <sodium/crypto_onetimeauth_poly1305.h> 32*dd2e1352SJohn Baldwin #include <sodium/crypto_stream_chacha20.h> 33*dd2e1352SJohn Baldwin 34*dd2e1352SJohn Baldwin struct chacha20_poly1305_cipher_ctx { 35*dd2e1352SJohn Baldwin const void *key; 36*dd2e1352SJohn Baldwin uint32_t ic; 37*dd2e1352SJohn Baldwin char nonce[CHACHA20_POLY1305_IV_LEN]; 38*dd2e1352SJohn Baldwin }; 39*dd2e1352SJohn Baldwin 40*dd2e1352SJohn Baldwin static int 41*dd2e1352SJohn Baldwin chacha20_poly1305_setkey(void *vctx, const uint8_t *key, int len) 42*dd2e1352SJohn Baldwin { 43*dd2e1352SJohn Baldwin struct chacha20_poly1305_cipher_ctx *ctx = vctx; 44*dd2e1352SJohn Baldwin 45*dd2e1352SJohn Baldwin if (len != CHACHA20_POLY1305_KEY) 46*dd2e1352SJohn Baldwin return (EINVAL); 47*dd2e1352SJohn Baldwin 48*dd2e1352SJohn Baldwin ctx->key = key; 49*dd2e1352SJohn Baldwin return (0); 50*dd2e1352SJohn Baldwin } 51*dd2e1352SJohn Baldwin 52*dd2e1352SJohn Baldwin static void 53*dd2e1352SJohn Baldwin chacha20_poly1305_reinit(void *vctx, const uint8_t *iv) 54*dd2e1352SJohn Baldwin { 55*dd2e1352SJohn Baldwin struct chacha20_poly1305_cipher_ctx *ctx = vctx; 56*dd2e1352SJohn Baldwin 57*dd2e1352SJohn Baldwin /* Block 0 is used for the poly1305 key. */ 58*dd2e1352SJohn Baldwin memcpy(ctx->nonce, iv, sizeof(ctx->nonce)); 59*dd2e1352SJohn Baldwin ctx->ic = 1; 60*dd2e1352SJohn Baldwin } 61*dd2e1352SJohn Baldwin 62*dd2e1352SJohn Baldwin static void 63*dd2e1352SJohn Baldwin chacha20_poly1305_crypt(void *vctx, const uint8_t *in, uint8_t *out) 64*dd2e1352SJohn Baldwin { 65*dd2e1352SJohn Baldwin struct chacha20_poly1305_cipher_ctx *ctx = vctx; 66*dd2e1352SJohn Baldwin int error; 67*dd2e1352SJohn Baldwin 68*dd2e1352SJohn Baldwin error = crypto_stream_chacha20_ietf_xor_ic(out, in, 69*dd2e1352SJohn Baldwin CHACHA20_NATIVE_BLOCK_LEN, ctx->nonce, ctx->ic, ctx->key); 70*dd2e1352SJohn Baldwin KASSERT(error == 0, ("%s failed: %d", __func__, error)); 71*dd2e1352SJohn Baldwin ctx->ic++; 72*dd2e1352SJohn Baldwin } 73*dd2e1352SJohn Baldwin 74*dd2e1352SJohn Baldwin static void 75*dd2e1352SJohn Baldwin chacha20_poly1305_crypt_last(void *vctx, const uint8_t *in, uint8_t *out, 76*dd2e1352SJohn Baldwin size_t len) 77*dd2e1352SJohn Baldwin { 78*dd2e1352SJohn Baldwin struct chacha20_poly1305_cipher_ctx *ctx = vctx; 79*dd2e1352SJohn Baldwin 80*dd2e1352SJohn Baldwin int error; 81*dd2e1352SJohn Baldwin 82*dd2e1352SJohn Baldwin error = crypto_stream_chacha20_ietf_xor_ic(out, in, len, ctx->nonce, 83*dd2e1352SJohn Baldwin ctx->ic, ctx->key); 84*dd2e1352SJohn Baldwin KASSERT(error == 0, ("%s failed: %d", __func__, error)); 85*dd2e1352SJohn Baldwin } 86*dd2e1352SJohn Baldwin 87fc8fc743SJohn Baldwin struct enc_xform enc_xform_chacha20_poly1305 = { 88fc8fc743SJohn Baldwin .type = CRYPTO_CHACHA20_POLY1305, 89fc8fc743SJohn Baldwin .name = "ChaCha20-Poly1305", 90*dd2e1352SJohn Baldwin .ctxsize = sizeof(struct chacha20_poly1305_cipher_ctx), 91fc8fc743SJohn Baldwin .blocksize = 1, 92*dd2e1352SJohn Baldwin .native_blocksize = CHACHA20_NATIVE_BLOCK_LEN, 93fc8fc743SJohn Baldwin .ivsize = CHACHA20_POLY1305_IV_LEN, 94fc8fc743SJohn Baldwin .minkey = CHACHA20_POLY1305_KEY, 95fc8fc743SJohn Baldwin .maxkey = CHACHA20_POLY1305_KEY, 96*dd2e1352SJohn Baldwin .encrypt = chacha20_poly1305_crypt, 97*dd2e1352SJohn Baldwin .decrypt = chacha20_poly1305_crypt, 98*dd2e1352SJohn Baldwin .setkey = chacha20_poly1305_setkey, 99*dd2e1352SJohn Baldwin .reinit = chacha20_poly1305_reinit, 100*dd2e1352SJohn Baldwin .encrypt_last = chacha20_poly1305_crypt_last, 101*dd2e1352SJohn Baldwin .decrypt_last = chacha20_poly1305_crypt_last, 102fc8fc743SJohn Baldwin }; 103fc8fc743SJohn Baldwin 104*dd2e1352SJohn Baldwin struct chacha20_poly1305_auth_ctx { 105*dd2e1352SJohn Baldwin struct crypto_onetimeauth_poly1305_state state; 106*dd2e1352SJohn Baldwin const void *key; 107*dd2e1352SJohn Baldwin }; 108*dd2e1352SJohn Baldwin CTASSERT(sizeof(union authctx) >= sizeof(struct chacha20_poly1305_auth_ctx)); 109*dd2e1352SJohn Baldwin 110*dd2e1352SJohn Baldwin static void 111*dd2e1352SJohn Baldwin chacha20_poly1305_Init(void *vctx) 112*dd2e1352SJohn Baldwin { 113*dd2e1352SJohn Baldwin } 114*dd2e1352SJohn Baldwin 115*dd2e1352SJohn Baldwin static void 116*dd2e1352SJohn Baldwin chacha20_poly1305_Setkey(void *vctx, const uint8_t *key, u_int klen) 117*dd2e1352SJohn Baldwin { 118*dd2e1352SJohn Baldwin struct chacha20_poly1305_auth_ctx *ctx = vctx; 119*dd2e1352SJohn Baldwin 120*dd2e1352SJohn Baldwin ctx->key = key; 121*dd2e1352SJohn Baldwin } 122*dd2e1352SJohn Baldwin 123*dd2e1352SJohn Baldwin static void 124*dd2e1352SJohn Baldwin chacha20_poly1305_Reinit(void *vctx, const uint8_t *nonce, u_int noncelen) 125*dd2e1352SJohn Baldwin { 126*dd2e1352SJohn Baldwin struct chacha20_poly1305_auth_ctx *ctx = vctx; 127*dd2e1352SJohn Baldwin char block[CHACHA20_NATIVE_BLOCK_LEN]; 128*dd2e1352SJohn Baldwin 129*dd2e1352SJohn Baldwin crypto_stream_chacha20_ietf(block, sizeof(block), nonce, ctx->key); 130*dd2e1352SJohn Baldwin crypto_onetimeauth_poly1305_init(&ctx->state, block); 131*dd2e1352SJohn Baldwin explicit_bzero(block, sizeof(block)); 132*dd2e1352SJohn Baldwin } 133*dd2e1352SJohn Baldwin 134*dd2e1352SJohn Baldwin static int 135*dd2e1352SJohn Baldwin chacha20_poly1305_Update(void *vctx, const void *data, u_int len) 136*dd2e1352SJohn Baldwin { 137*dd2e1352SJohn Baldwin struct chacha20_poly1305_auth_ctx *ctx = vctx; 138*dd2e1352SJohn Baldwin 139*dd2e1352SJohn Baldwin crypto_onetimeauth_poly1305_update(&ctx->state, data, len); 140*dd2e1352SJohn Baldwin return (0); 141*dd2e1352SJohn Baldwin } 142*dd2e1352SJohn Baldwin 143*dd2e1352SJohn Baldwin static void 144*dd2e1352SJohn Baldwin chacha20_poly1305_Final(uint8_t *digest, void *vctx) 145*dd2e1352SJohn Baldwin { 146*dd2e1352SJohn Baldwin struct chacha20_poly1305_auth_ctx *ctx = vctx; 147*dd2e1352SJohn Baldwin 148*dd2e1352SJohn Baldwin crypto_onetimeauth_poly1305_final(&ctx->state, digest); 149*dd2e1352SJohn Baldwin } 150*dd2e1352SJohn Baldwin 151*dd2e1352SJohn Baldwin struct auth_hash auth_hash_chacha20_poly1305 = { 152*dd2e1352SJohn Baldwin .type = CRYPTO_POLY1305, 153*dd2e1352SJohn Baldwin .name = "ChaCha20-Poly1305", 154*dd2e1352SJohn Baldwin .keysize = POLY1305_KEY_LEN, 155*dd2e1352SJohn Baldwin .hashsize = POLY1305_HASH_LEN, 156*dd2e1352SJohn Baldwin .ctxsize = sizeof(struct chacha20_poly1305_auth_ctx), 157*dd2e1352SJohn Baldwin .blocksize = crypto_onetimeauth_poly1305_BYTES, 158*dd2e1352SJohn Baldwin .Init = chacha20_poly1305_Init, 159*dd2e1352SJohn Baldwin .Setkey = chacha20_poly1305_Setkey, 160*dd2e1352SJohn Baldwin .Reinit = chacha20_poly1305_Reinit, 161*dd2e1352SJohn Baldwin .Update = chacha20_poly1305_Update, 162*dd2e1352SJohn Baldwin .Final = chacha20_poly1305_Final, 163*dd2e1352SJohn Baldwin }; 164