From nobody Tue Jan 18 22:50:29 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id D0AAD195EA9B; Tue, 18 Jan 2022 22:50:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JdkWT53fSz3Hhg; Tue, 18 Jan 2022 22:50:29 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642546229; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=p1K8K70q5CNkxw0mq92PWeqQ4dQEzv9myj9gYZLIwt4=; b=UUjBejzyQDl1BsVyudNQbJaNkBKvqvect+exQ18U09U6TW+SuE4HhOBQiV4zE4dJyi1JUp 18LYXSLitqSiwuMwYqCkQe9tMi6wp52RxE9Am40okVkKAVhCbStmQKsby2oMugLHHD0fLl WgB4Zj9Gjp2cHV04qT0vTD7pRuPa+Ut4Bb+JIOQKmm3x6S+2Bajc9OceP02lr4ehoTnP9o JNoP+q0eUwgaGfiN8r2tnDvcegHcurXdf9yLKEGPfbJaYbE2f8v1TytE2e/zz+5SsyE4KV xSm6G5Vk7if5CKi9EOpGDSPc715YZLtQe0oC1cmLc7MPJVyGgxOVIt8WPBHjrQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8DC7216FB; Tue, 18 Jan 2022 22:50:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 20IMoTXC063274; Tue, 18 Jan 2022 22:50:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 20IMoTDY063273; Tue, 18 Jan 2022 22:50:29 GMT (envelope-from git) Date: Tue, 18 Jan 2022 22:50:29 GMT Message-Id: <202201182250.20IMoTDY063273@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: e71680049bb8 - main - crypto: Add a simple API for [X]ChaCha20-Poly1035 on flat buffers. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: e71680049bb8ff395aeaa144377dd9e49331f45e Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642546229; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=p1K8K70q5CNkxw0mq92PWeqQ4dQEzv9myj9gYZLIwt4=; b=AGjbofJ4yAI/IpO4QBKytUM8MvXsQSPnkCOIPDfAaV4Qxe3FQ4vBuabc1qm0ebT0VKoocp CqTM54uy0+SkY/Cj+s0y9YzszYU6TEMtmfE7Dj6j1aBrTUmXt8/Tmt339v9iE1Tczy6OBR 0hP99OVfLoF4LzyBoqyiKVyPe7qey1JG8081qCVYRIGjZwkiahKC83ZR69W2tpCfyqSlb4 Qs0GRdHvPhT6lAf/eLF0VDRn/FNr9XHckBh4sa7U5X4GG3z0DXy78KzpEz91I4FmDezchh n5b/X6B7hdNPc8vGHa+YZwASOIJcopA04KemShCBme48qX1v2XVmzyqsYGvkPA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1642546229; a=rsa-sha256; cv=none; b=eyxffHL/Vb6eTxC4zcTWIQKEH/Y9TgGch2N3bfGYPch0EGBzbmeqPUjOI7cUxk028EPnBk CYq+ID+nbNqh8QR5S58p4NxMqNaPDcfz/gTO8Fofdm/7YUt+hqgp9+VAHtYQJRSAT3uOx9 ZjzAQdjCv+tD/qKRkNuiHOIBRINXjFb6qg2Fhpr0p001VrTEkKVKWlCrDVvoL+fmukiN/S HqPxj+vAn25w5F58u4nFfYGQf2E4SlWR2pz0WY7Txgb6B3YWuN7wVQdLzqoVLI15T3QOvL 9xFBOeOAaKm5yhG4q4qatzjM+Ryiya2p1bPkW3axzla1aTYtz8ASyodxuwbo5A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=e71680049bb8ff395aeaa144377dd9e49331f45e commit e71680049bb8ff395aeaa144377dd9e49331f45e Author: John Baldwin AuthorDate: 2022-01-18 22:47:13 +0000 Commit: John Baldwin CommitDate: 2022-01-18 22:47:13 +0000 crypto: Add a simple API for [X]ChaCha20-Poly1035 on flat buffers. This is a synchronous software API which wraps the existing software implementation shared with OCF. Note that this will not currently use optimized backends (such as ossl(4)) but may be appropriate for operations on small buffers. Reviewed by: markj Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D33524 --- sys/conf/files | 1 + sys/crypto/chacha20_poly1305.c | 241 +++++++++++++++++++++++++++++++++++++++++ sys/crypto/chacha20_poly1305.h | 54 +++++++++ sys/modules/crypto/Makefile | 1 + 4 files changed, 297 insertions(+) diff --git a/sys/conf/files b/sys/conf/files index 0ec329436658..675cf0a6a2a2 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -713,6 +713,7 @@ crypto/camellia/camellia.c optional crypto | ipsec | ipsec_support crypto/camellia/camellia-api.c optional crypto | ipsec | ipsec_support crypto/chacha20/chacha.c standard crypto/chacha20/chacha-sw.c optional crypto | ipsec | ipsec_support +crypto/chacha20_poly1305.c optional crypto crypto/des/des_ecb.c optional netsmb crypto/des/des_setkey.c optional netsmb crypto/openssl/ossl.c optional ossl diff --git a/sys/crypto/chacha20_poly1305.c b/sys/crypto/chacha20_poly1305.c new file mode 100644 index 000000000000..938400c0feef --- /dev/null +++ b/sys/crypto/chacha20_poly1305.c @@ -0,0 +1,241 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 The FreeBSD Foundation + * + * This software was developed by Ararat River Consulting, LLC under + * sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +static const uint8_t zeroes[POLY1305_BLOCK_LEN]; + +void +chacha20_poly1305_encrypt(uint8_t *dst, const uint8_t *src, + const size_t src_len, const uint8_t *aad, const size_t aad_len, + const uint8_t *nonce, const size_t nonce_len, const uint8_t *key) +{ + const struct enc_xform *exf; + void *ctx; + size_t resid, todo; + uint64_t lengths[2]; + + exf = &enc_xform_chacha20_poly1305; + ctx = __builtin_alloca(exf->ctxsize); + exf->setkey(ctx, key, CHACHA20_POLY1305_KEY); + exf->reinit(ctx, nonce, nonce_len); + + exf->update(ctx, aad, aad_len); + if (aad_len % POLY1305_BLOCK_LEN != 0) + exf->update(ctx, zeroes, + POLY1305_BLOCK_LEN - aad_len % POLY1305_BLOCK_LEN); + + resid = src_len; + todo = rounddown2(resid, CHACHA20_NATIVE_BLOCK_LEN); + if (todo > 0) { + exf->encrypt_multi(ctx, src, dst, todo); + exf->update(ctx, dst, todo); + src += todo; + dst += todo; + resid -= todo; + } + if (resid > 0) { + exf->encrypt_last(ctx, src, dst, resid); + exf->update(ctx, dst, resid); + dst += resid; + if (resid % POLY1305_BLOCK_LEN != 0) + exf->update(ctx, zeroes, + POLY1305_BLOCK_LEN - resid % POLY1305_BLOCK_LEN); + } + + lengths[0] = htole64(aad_len); + lengths[1] = htole64(src_len); + exf->update(ctx, lengths, sizeof(lengths)); + exf->final(dst, ctx); + + explicit_bzero(ctx, exf->ctxsize); + explicit_bzero(lengths, sizeof(lengths)); +} + +bool +chacha20_poly1305_decrypt(uint8_t *dst, const uint8_t *src, + const size_t src_len, const uint8_t *aad, const size_t aad_len, + const uint8_t *nonce, const size_t nonce_len, const uint8_t *key) +{ + const struct enc_xform *exf; + void *ctx; + size_t resid, todo; + union { + uint64_t lengths[2]; + char tag[POLY1305_HASH_LEN]; + } u; + bool result; + + if (src_len < POLY1305_HASH_LEN) + return (false); + resid = src_len - POLY1305_HASH_LEN; + + exf = &enc_xform_chacha20_poly1305; + ctx = __builtin_alloca(exf->ctxsize); + exf->setkey(ctx, key, CHACHA20_POLY1305_KEY); + exf->reinit(ctx, nonce, nonce_len); + + exf->update(ctx, aad, aad_len); + if (aad_len % POLY1305_BLOCK_LEN != 0) + exf->update(ctx, zeroes, + POLY1305_BLOCK_LEN - aad_len % POLY1305_BLOCK_LEN); + exf->update(ctx, src, resid); + if (resid % POLY1305_BLOCK_LEN != 0) + exf->update(ctx, zeroes, + POLY1305_BLOCK_LEN - resid % POLY1305_BLOCK_LEN); + + u.lengths[0] = htole64(aad_len); + u.lengths[1] = htole64(resid); + exf->update(ctx, u.lengths, sizeof(u.lengths)); + exf->final(u.tag, ctx); + result = (timingsafe_bcmp(u.tag, src + resid, POLY1305_HASH_LEN) == 0); + if (!result) + goto out; + + todo = rounddown2(resid, CHACHA20_NATIVE_BLOCK_LEN); + if (todo > 0) { + exf->decrypt_multi(ctx, src, dst, todo); + src += todo; + dst += todo; + resid -= todo; + } + if (resid > 0) + exf->decrypt_last(ctx, src, dst, resid); + +out: + explicit_bzero(ctx, exf->ctxsize); + explicit_bzero(&u, sizeof(u)); + return (result); +} + +void +xchacha20_poly1305_encrypt(uint8_t *dst, const uint8_t *src, + const size_t src_len, const uint8_t *aad, const size_t aad_len, + const uint8_t *nonce, const uint8_t *key) +{ + const struct enc_xform *exf; + void *ctx; + size_t resid, todo; + uint64_t lengths[2]; + + exf = &enc_xform_xchacha20_poly1305; + ctx = __builtin_alloca(exf->ctxsize); + exf->setkey(ctx, key, XCHACHA20_POLY1305_KEY); + exf->reinit(ctx, nonce, XCHACHA20_POLY1305_IV_LEN); + + exf->update(ctx, aad, aad_len); + if (aad_len % POLY1305_BLOCK_LEN != 0) + exf->update(ctx, zeroes, + POLY1305_BLOCK_LEN - aad_len % POLY1305_BLOCK_LEN); + + resid = src_len; + todo = rounddown2(resid, CHACHA20_NATIVE_BLOCK_LEN); + if (todo > 0) { + exf->encrypt_multi(ctx, src, dst, todo); + exf->update(ctx, dst, todo); + src += todo; + dst += todo; + resid -= todo; + } + if (resid > 0) { + exf->encrypt_last(ctx, src, dst, resid); + exf->update(ctx, dst, resid); + dst += resid; + if (resid % POLY1305_BLOCK_LEN != 0) + exf->update(ctx, zeroes, + POLY1305_BLOCK_LEN - resid % POLY1305_BLOCK_LEN); + } + + lengths[0] = htole64(aad_len); + lengths[1] = htole64(src_len); + exf->update(ctx, lengths, sizeof(lengths)); + exf->final(dst, ctx); + + explicit_bzero(ctx, exf->ctxsize); + explicit_bzero(lengths, sizeof(lengths)); +} + +bool +xchacha20_poly1305_decrypt(uint8_t *dst, const uint8_t *src, + const size_t src_len, const uint8_t *aad, const size_t aad_len, + const uint8_t *nonce, const uint8_t *key) +{ + const struct enc_xform *exf; + void *ctx; + size_t resid, todo; + union { + uint64_t lengths[2]; + char tag[POLY1305_HASH_LEN]; + } u; + bool result; + + if (src_len < POLY1305_HASH_LEN) + return (false); + resid = src_len - POLY1305_HASH_LEN; + + exf = &enc_xform_xchacha20_poly1305; + ctx = __builtin_alloca(exf->ctxsize); + exf->setkey(ctx, key, XCHACHA20_POLY1305_KEY); + exf->reinit(ctx, nonce, XCHACHA20_POLY1305_IV_LEN); + + exf->update(ctx, aad, aad_len); + if (aad_len % POLY1305_BLOCK_LEN != 0) + exf->update(ctx, zeroes, + POLY1305_BLOCK_LEN - aad_len % POLY1305_BLOCK_LEN); + exf->update(ctx, src, resid); + if (resid % POLY1305_BLOCK_LEN != 0) + exf->update(ctx, zeroes, + POLY1305_BLOCK_LEN - resid % POLY1305_BLOCK_LEN); + + u.lengths[0] = htole64(aad_len); + u.lengths[1] = htole64(resid); + exf->update(ctx, u.lengths, sizeof(u.lengths)); + exf->final(u.tag, ctx); + result = (timingsafe_bcmp(u.tag, src + resid, POLY1305_HASH_LEN) == 0); + if (!result) + goto out; + + todo = rounddown2(resid, CHACHA20_NATIVE_BLOCK_LEN); + if (todo > 0) { + exf->decrypt_multi(ctx, src, dst, todo); + src += todo; + dst += todo; + resid -= todo; + } + if (resid > 0) + exf->decrypt_last(ctx, src, dst, resid); + +out: + explicit_bzero(ctx, exf->ctxsize); + explicit_bzero(&u, sizeof(u)); + return (result); +} diff --git a/sys/crypto/chacha20_poly1305.h b/sys/crypto/chacha20_poly1305.h new file mode 100644 index 000000000000..505a5ef9d6f0 --- /dev/null +++ b/sys/crypto/chacha20_poly1305.h @@ -0,0 +1,54 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 The FreeBSD Foundation + * + * This software was developed by Ararat River Consulting, LLC under + * sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef __CRYPTO_CHACHA20_POLY1305_H__ +#define __CRYPTO_CHACHA20_POLY1305_H__ + +#include + +/* The Poly1305 tag is appended to the cipher text. */ + +void chacha20_poly1305_encrypt(uint8_t *dst, const uint8_t *src, + const size_t src_len, const uint8_t *aad, const size_t aad_len, + const uint8_t *nonce, const size_t nonce_len, const uint8_t *key); + +bool chacha20_poly1305_decrypt(uint8_t *dst, const uint8_t *src, + const size_t src_len, const uint8_t *aad, const size_t aad_len, + const uint8_t *nonce, const size_t nonce_len, const uint8_t *key); + +void xchacha20_poly1305_encrypt(uint8_t *dst, const uint8_t *src, + const size_t src_len, const uint8_t *aad, const size_t aad_len, + const uint8_t *nonce, const uint8_t *key); + +bool xchacha20_poly1305_decrypt(uint8_t *dst, const uint8_t *src, + const size_t src_len, const uint8_t *aad, const size_t aad_len, + const uint8_t *nonce, const uint8_t *key); + +#endif /* !__CRYPTO_CHACHA20_POLY1305_H__ */ diff --git a/sys/modules/crypto/Makefile b/sys/modules/crypto/Makefile index 4b14cd784ebe..f56bb94a4ce6 100644 --- a/sys/modules/crypto/Makefile +++ b/sys/modules/crypto/Makefile @@ -55,6 +55,7 @@ CWARNFLAGS.blake2b-ref.c += -Wno-cast-qual -Wno-unused-function CWARNFLAGS.blake2s-ref.c += -Wno-cast-qual -Wno-unused-function SRCS += chacha.c SRCS += chacha-sw.c +SRCS += chacha20_poly1305.c LIBSODIUM_INC=${LIBSODIUM}/include LIBSODIUM_COMPAT=${SRCTOP}/sys/crypto/libsodium