Path: blob/main/sys/opencrypto/xform_chacha20_poly1305.c
39478 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2020 Netflix Inc.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <opencrypto/xform_auth.h>28#include <opencrypto/xform_enc.h>2930#include <sodium/crypto_core_hchacha20.h>31#include <sodium/crypto_onetimeauth_poly1305.h>32#include <sodium/crypto_stream_chacha20.h>3334struct chacha20_poly1305_ctx {35struct crypto_onetimeauth_poly1305_state auth;36const void *key;37uint32_t ic;38bool ietf;39char nonce[CHACHA20_POLY1305_IV_LEN];40};4142struct xchacha20_poly1305_ctx {43struct chacha20_poly1305_ctx base_ctx; /* must be first */44const void *key;45char derived_key[CHACHA20_POLY1305_KEY];46};4748static int49chacha20_poly1305_setkey(void *vctx, const uint8_t *key, int len)50{51struct chacha20_poly1305_ctx *ctx = vctx;5253if (len != CHACHA20_POLY1305_KEY)54return (EINVAL);5556ctx->key = key;57return (0);58}5960static void61chacha20_poly1305_reinit(void *vctx, const uint8_t *iv, size_t ivlen)62{63struct chacha20_poly1305_ctx *ctx = vctx;64char block[CHACHA20_NATIVE_BLOCK_LEN];6566KASSERT(ivlen == 8 || ivlen == sizeof(ctx->nonce),67("%s: invalid nonce length", __func__));6869memcpy(ctx->nonce, iv, ivlen);70ctx->ietf = (ivlen == CHACHA20_POLY1305_IV_LEN);7172/* Block 0 is used for the poly1305 key. */73if (ctx->ietf)74crypto_stream_chacha20_ietf(block, sizeof(block), iv, ctx->key);75else76crypto_stream_chacha20(block, sizeof(block), iv, ctx->key);77crypto_onetimeauth_poly1305_init(&ctx->auth, block);78explicit_bzero(block, sizeof(block));7980/* Start with block 1 for ciphertext. */81ctx->ic = 1;82}8384static void85chacha20_poly1305_crypt(void *vctx, const uint8_t *in, uint8_t *out)86{87struct chacha20_poly1305_ctx *ctx = vctx;88int error __diagused;8990if (ctx->ietf)91error = crypto_stream_chacha20_ietf_xor_ic(out, in,92CHACHA20_NATIVE_BLOCK_LEN, ctx->nonce, ctx->ic, ctx->key);93else94error = crypto_stream_chacha20_xor_ic(out, in,95CHACHA20_NATIVE_BLOCK_LEN, ctx->nonce, ctx->ic, ctx->key);96KASSERT(error == 0, ("%s failed: %d", __func__, error));97ctx->ic++;98}99100static void101chacha20_poly1305_crypt_multi(void *vctx, const uint8_t *in, uint8_t *out, size_t len)102{103struct chacha20_poly1305_ctx *ctx = vctx;104int error __diagused;105106KASSERT(len % CHACHA20_NATIVE_BLOCK_LEN == 0, ("%s: invalid length",107__func__));108if (ctx->ietf)109error = crypto_stream_chacha20_ietf_xor_ic(out, in, len,110ctx->nonce, ctx->ic, ctx->key);111else112error = crypto_stream_chacha20_xor_ic(out, in, len, ctx->nonce,113ctx->ic, ctx->key);114KASSERT(error == 0, ("%s failed: %d", __func__, error));115ctx->ic += len / CHACHA20_NATIVE_BLOCK_LEN;116}117118static void119chacha20_poly1305_crypt_last(void *vctx, const uint8_t *in, uint8_t *out,120size_t len)121{122struct chacha20_poly1305_ctx *ctx = vctx;123124int error __diagused;125126if (ctx->ietf)127error = crypto_stream_chacha20_ietf_xor_ic(out, in, len,128ctx->nonce, ctx->ic, ctx->key);129else130error = crypto_stream_chacha20_xor_ic(out, in, len, ctx->nonce,131ctx->ic, ctx->key);132KASSERT(error == 0, ("%s failed: %d", __func__, error));133}134135static int136chacha20_poly1305_update(void *vctx, const void *data, u_int len)137{138struct chacha20_poly1305_ctx *ctx = vctx;139140crypto_onetimeauth_poly1305_update(&ctx->auth, data, len);141return (0);142}143144static void145chacha20_poly1305_final(uint8_t *digest, void *vctx)146{147struct chacha20_poly1305_ctx *ctx = vctx;148149crypto_onetimeauth_poly1305_final(&ctx->auth, digest);150}151152const struct enc_xform enc_xform_chacha20_poly1305 = {153.type = CRYPTO_CHACHA20_POLY1305,154.name = "ChaCha20-Poly1305",155.ctxsize = sizeof(struct chacha20_poly1305_ctx),156.blocksize = 1,157.native_blocksize = CHACHA20_NATIVE_BLOCK_LEN,158.ivsize = CHACHA20_POLY1305_IV_LEN,159.minkey = CHACHA20_POLY1305_KEY,160.maxkey = CHACHA20_POLY1305_KEY,161.macsize = POLY1305_HASH_LEN,162.setkey = chacha20_poly1305_setkey,163.reinit = chacha20_poly1305_reinit,164.encrypt = chacha20_poly1305_crypt,165.decrypt = chacha20_poly1305_crypt,166.encrypt_multi = chacha20_poly1305_crypt_multi,167.decrypt_multi = chacha20_poly1305_crypt_multi,168.encrypt_last = chacha20_poly1305_crypt_last,169.decrypt_last = chacha20_poly1305_crypt_last,170.update = chacha20_poly1305_update,171.final = chacha20_poly1305_final,172};173174static int175xchacha20_poly1305_setkey(void *vctx, const uint8_t *key, int len)176{177struct xchacha20_poly1305_ctx *ctx = vctx;178179if (len != XCHACHA20_POLY1305_KEY)180return (EINVAL);181182ctx->key = key;183ctx->base_ctx.key = ctx->derived_key;184return (0);185}186187static void188xchacha20_poly1305_reinit(void *vctx, const uint8_t *iv, size_t ivlen)189{190struct xchacha20_poly1305_ctx *ctx = vctx;191char nonce[CHACHA20_POLY1305_IV_LEN];192193KASSERT(ivlen == XCHACHA20_POLY1305_IV_LEN,194("%s: invalid nonce length", __func__));195196/*197* Use HChaCha20 to derive the internal key used for198* ChaCha20-Poly1305.199*/200crypto_core_hchacha20(ctx->derived_key, iv, ctx->key, NULL);201202memset(nonce, 0, 4);203memcpy(nonce + 4, iv + crypto_core_hchacha20_INPUTBYTES,204sizeof(nonce) - 4);205chacha20_poly1305_reinit(&ctx->base_ctx, nonce, sizeof(nonce));206explicit_bzero(nonce, sizeof(nonce));207}208209const struct enc_xform enc_xform_xchacha20_poly1305 = {210.type = CRYPTO_XCHACHA20_POLY1305,211.name = "XChaCha20-Poly1305",212.ctxsize = sizeof(struct xchacha20_poly1305_ctx),213.blocksize = 1,214.native_blocksize = CHACHA20_NATIVE_BLOCK_LEN,215.ivsize = XCHACHA20_POLY1305_IV_LEN,216.minkey = XCHACHA20_POLY1305_KEY,217.maxkey = XCHACHA20_POLY1305_KEY,218.macsize = POLY1305_HASH_LEN,219.setkey = xchacha20_poly1305_setkey,220.reinit = xchacha20_poly1305_reinit,221.encrypt = chacha20_poly1305_crypt,222.decrypt = chacha20_poly1305_crypt,223.encrypt_multi = chacha20_poly1305_crypt_multi,224.decrypt_multi = chacha20_poly1305_crypt_multi,225.encrypt_last = chacha20_poly1305_crypt_last,226.decrypt_last = chacha20_poly1305_crypt_last,227.update = chacha20_poly1305_update,228.final = chacha20_poly1305_final,229};230231232