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 28dd2e1352SJohn Baldwin #include <opencrypto/xform_auth.h> 29fc8fc743SJohn Baldwin #include <opencrypto/xform_enc.h> 30fc8fc743SJohn Baldwin 31dd2e1352SJohn Baldwin #include <sodium/crypto_onetimeauth_poly1305.h> 32dd2e1352SJohn Baldwin #include <sodium/crypto_stream_chacha20.h> 33dd2e1352SJohn Baldwin 34*ab91fb6cSJohn Baldwin struct chacha20_poly1305_ctx { 35*ab91fb6cSJohn Baldwin struct crypto_onetimeauth_poly1305_state auth; 36dd2e1352SJohn Baldwin const void *key; 37dd2e1352SJohn Baldwin uint32_t ic; 3842dcd395SJohn Baldwin bool ietf; 39dd2e1352SJohn Baldwin char nonce[CHACHA20_POLY1305_IV_LEN]; 40dd2e1352SJohn Baldwin }; 41dd2e1352SJohn Baldwin 42dd2e1352SJohn Baldwin static int 43dd2e1352SJohn Baldwin chacha20_poly1305_setkey(void *vctx, const uint8_t *key, int len) 44dd2e1352SJohn Baldwin { 45*ab91fb6cSJohn Baldwin struct chacha20_poly1305_ctx *ctx = vctx; 46dd2e1352SJohn Baldwin 47dd2e1352SJohn Baldwin if (len != CHACHA20_POLY1305_KEY) 48dd2e1352SJohn Baldwin return (EINVAL); 49dd2e1352SJohn Baldwin 50dd2e1352SJohn Baldwin ctx->key = key; 51dd2e1352SJohn Baldwin return (0); 52dd2e1352SJohn Baldwin } 53dd2e1352SJohn Baldwin 54dd2e1352SJohn Baldwin static void 551833d604SJohn Baldwin chacha20_poly1305_reinit(void *vctx, const uint8_t *iv, size_t ivlen) 56dd2e1352SJohn Baldwin { 57*ab91fb6cSJohn Baldwin struct chacha20_poly1305_ctx *ctx = vctx; 58*ab91fb6cSJohn Baldwin char block[CHACHA20_NATIVE_BLOCK_LEN]; 59dd2e1352SJohn Baldwin 60442ad83eSJohn Baldwin KASSERT(ivlen == 8 || ivlen == sizeof(ctx->nonce), 611833d604SJohn Baldwin ("%s: invalid nonce length", __func__)); 621833d604SJohn Baldwin 6342dcd395SJohn Baldwin memcpy(ctx->nonce, iv, ivlen); 6442dcd395SJohn Baldwin ctx->ietf = (ivlen == CHACHA20_POLY1305_IV_LEN); 65*ab91fb6cSJohn Baldwin 66*ab91fb6cSJohn Baldwin /* Block 0 is used for the poly1305 key. */ 67*ab91fb6cSJohn Baldwin if (ctx->ietf) 68*ab91fb6cSJohn Baldwin crypto_stream_chacha20_ietf(block, sizeof(block), iv, ctx->key); 69*ab91fb6cSJohn Baldwin else 70*ab91fb6cSJohn Baldwin crypto_stream_chacha20(block, sizeof(block), iv, ctx->key); 71*ab91fb6cSJohn Baldwin crypto_onetimeauth_poly1305_init(&ctx->auth, block); 72*ab91fb6cSJohn Baldwin explicit_bzero(block, sizeof(block)); 73*ab91fb6cSJohn Baldwin 74*ab91fb6cSJohn Baldwin /* Start with block 1 for ciphertext. */ 75dd2e1352SJohn Baldwin ctx->ic = 1; 76dd2e1352SJohn Baldwin } 77dd2e1352SJohn Baldwin 78dd2e1352SJohn Baldwin static void 79dd2e1352SJohn Baldwin chacha20_poly1305_crypt(void *vctx, const uint8_t *in, uint8_t *out) 80dd2e1352SJohn Baldwin { 81*ab91fb6cSJohn Baldwin struct chacha20_poly1305_ctx *ctx = vctx; 825a052b61SScott Long int error __diagused; 83dd2e1352SJohn Baldwin 8442dcd395SJohn Baldwin if (ctx->ietf) 85dd2e1352SJohn Baldwin error = crypto_stream_chacha20_ietf_xor_ic(out, in, 86dd2e1352SJohn Baldwin CHACHA20_NATIVE_BLOCK_LEN, ctx->nonce, ctx->ic, ctx->key); 8742dcd395SJohn Baldwin else 8842dcd395SJohn Baldwin error = crypto_stream_chacha20_xor_ic(out, in, 8942dcd395SJohn Baldwin CHACHA20_NATIVE_BLOCK_LEN, ctx->nonce, ctx->ic, ctx->key); 90dd2e1352SJohn Baldwin KASSERT(error == 0, ("%s failed: %d", __func__, error)); 91dd2e1352SJohn Baldwin ctx->ic++; 92dd2e1352SJohn Baldwin } 93dd2e1352SJohn Baldwin 94dd2e1352SJohn Baldwin static void 95dd2e1352SJohn Baldwin chacha20_poly1305_crypt_last(void *vctx, const uint8_t *in, uint8_t *out, 96dd2e1352SJohn Baldwin size_t len) 97dd2e1352SJohn Baldwin { 98*ab91fb6cSJohn Baldwin struct chacha20_poly1305_ctx *ctx = vctx; 99dd2e1352SJohn Baldwin 1005a052b61SScott Long int error __diagused; 101dd2e1352SJohn Baldwin 10242dcd395SJohn Baldwin if (ctx->ietf) 10342dcd395SJohn Baldwin error = crypto_stream_chacha20_ietf_xor_ic(out, in, len, 10442dcd395SJohn Baldwin ctx->nonce, ctx->ic, ctx->key); 10542dcd395SJohn Baldwin else 10642dcd395SJohn Baldwin error = crypto_stream_chacha20_xor_ic(out, in, len, ctx->nonce, 107dd2e1352SJohn Baldwin ctx->ic, ctx->key); 108dd2e1352SJohn Baldwin KASSERT(error == 0, ("%s failed: %d", __func__, error)); 109dd2e1352SJohn Baldwin } 110dd2e1352SJohn Baldwin 111*ab91fb6cSJohn Baldwin static int 112*ab91fb6cSJohn Baldwin chacha20_poly1305_update(void *vctx, const void *data, u_int len) 113*ab91fb6cSJohn Baldwin { 114*ab91fb6cSJohn Baldwin struct chacha20_poly1305_ctx *ctx = vctx; 115*ab91fb6cSJohn Baldwin 116*ab91fb6cSJohn Baldwin crypto_onetimeauth_poly1305_update(&ctx->auth, data, len); 117*ab91fb6cSJohn Baldwin return (0); 118*ab91fb6cSJohn Baldwin } 119*ab91fb6cSJohn Baldwin 120*ab91fb6cSJohn Baldwin static void 121*ab91fb6cSJohn Baldwin chacha20_poly1305_final(uint8_t *digest, void *vctx) 122*ab91fb6cSJohn Baldwin { 123*ab91fb6cSJohn Baldwin struct chacha20_poly1305_ctx *ctx = vctx; 124*ab91fb6cSJohn Baldwin 125*ab91fb6cSJohn Baldwin crypto_onetimeauth_poly1305_final(&ctx->auth, digest); 126*ab91fb6cSJohn Baldwin } 127*ab91fb6cSJohn Baldwin 128d8787d4fSMark Johnston const struct enc_xform enc_xform_chacha20_poly1305 = { 129fc8fc743SJohn Baldwin .type = CRYPTO_CHACHA20_POLY1305, 130fc8fc743SJohn Baldwin .name = "ChaCha20-Poly1305", 131*ab91fb6cSJohn Baldwin .ctxsize = sizeof(struct chacha20_poly1305_ctx), 132fc8fc743SJohn Baldwin .blocksize = 1, 133dd2e1352SJohn Baldwin .native_blocksize = CHACHA20_NATIVE_BLOCK_LEN, 134fc8fc743SJohn Baldwin .ivsize = CHACHA20_POLY1305_IV_LEN, 135fc8fc743SJohn Baldwin .minkey = CHACHA20_POLY1305_KEY, 136fc8fc743SJohn Baldwin .maxkey = CHACHA20_POLY1305_KEY, 137*ab91fb6cSJohn Baldwin .macsize = POLY1305_HASH_LEN, 138dd2e1352SJohn Baldwin .encrypt = chacha20_poly1305_crypt, 139dd2e1352SJohn Baldwin .decrypt = chacha20_poly1305_crypt, 140dd2e1352SJohn Baldwin .setkey = chacha20_poly1305_setkey, 141dd2e1352SJohn Baldwin .reinit = chacha20_poly1305_reinit, 142dd2e1352SJohn Baldwin .encrypt_last = chacha20_poly1305_crypt_last, 143dd2e1352SJohn Baldwin .decrypt_last = chacha20_poly1305_crypt_last, 144*ab91fb6cSJohn Baldwin .update = chacha20_poly1305_update, 145*ab91fb6cSJohn Baldwin .final = chacha20_poly1305_final, 146dd2e1352SJohn Baldwin }; 147