From nobody Wed Jul 13 19:43:24 2022 X-Original-To: dev-commits-src-branches@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 DC8631D22DAB; Wed, 13 Jul 2022 19:43:24 +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 4Ljp2N5vWCz470X; Wed, 13 Jul 2022 19:43:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1657741404; 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=/t7v63CiCyQQExtw2p3n0FAamW2XzboUmf58k1KQdH4=; b=UaoHkZaeietduzge89byR7KaALy1bcFVS9sqFCO57VwLMLcczklbQ/s7rTULXB0PEVaGJH Ssd3QCCccpeJvS25Jc8A3RTkI4+iVGC9l7kkOBuzOwJBHbdXxbF7vlYfUPWtwDtmEHKEHi H4doCZLIIKya3E38x23Yc8OrWU/6HJHJxaAwFP6pwmBzaFulvWd6twMWc3a911MQeSjTT1 Hte6t+kbtbYd9ScGfGxBM3VQYEMBPoQnP19CGmHcKG8gn7VgKOvHqYrOo2gBlLk4z0aKBt OHr7fVjE1topay5aKJiTRfDtqEoY/6/aJjWcvR9LXJucerXZ2kiOwjSqc4Im7Q== 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 4Ljp2N4k1SzbdT; Wed, 13 Jul 2022 19:43:24 +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 26DJhOYD097082; Wed, 13 Jul 2022 19:43:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 26DJhOZv097081; Wed, 13 Jul 2022 19:43:24 GMT (envelope-from git) Date: Wed, 13 Jul 2022 19:43:24 GMT Message-Id: <202207131943.26DJhOZv097081@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: a4eea5804d5c - stable/13 - crypto: Add a simple API for [X]ChaCha20-Poly1035 on flat buffers. List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: a4eea5804d5ced2af3c94723603ff6445082653f Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1657741404; 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=/t7v63CiCyQQExtw2p3n0FAamW2XzboUmf58k1KQdH4=; b=msNz5qFbQ6TEvDy2zf+NeTW8ALK33Ezg+mj43heY5T0byQJcUiwNE/14qG75V/5y/jb6Ws uUN6xp5n8z8U+P0LnqzQXLrEQBKWJF9Ld/OFeM2dVwYWEqOBFz3+JmiF26ozqUi+FNKScw khkIMM1SgZUJZx0YSiUUtnsjSalk8o25C8L+XKWwzB0BbNCajELq/foCNygLdQ52ZEpJV1 GdXhTpwGeO7QKuZ8Ybxqwp5CeBq7iC6xKxuWBexR1N7/0l2oTDxa+JRYcxfICOihB1fiy0 fIphtQinRDrAj0RorXv8XWWLHv8hSgssBInb4nPpY3o2ueUJE3YLKi8cFbAgdA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1657741404; a=rsa-sha256; cv=none; b=ZF17NOCiEsniQK50y0kTZ2CMOTG504ZHr3nnPsofcwmr5dJbXoRB4OZ21rotteToftS6ft RCOYrAQeSukAJUFXu/VjmTQVcucgmPGPLHPrIq0Jn1laa38Wa4NVPYRw5jB2WG80KCpqsE dOcQ3jLr64ELvVitxPxzlvJwAHwnM6RcsjjE58MCKtgIFLk8pM/MXuyAzrw7af7kb385qq ViGWRka+AGYtwO61re3XIaASps3SAJpKAZJdGWtXkdoM5KavGRZBr4rhJ0VdRbg/hizDNU BxxIpy8c/IkFdFgJwfvGU6LyvXN7DVMIfe4dq+nGShWd+rpJ+ys05ux5ou5OQQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=a4eea5804d5ced2af3c94723603ff6445082653f commit a4eea5804d5ced2af3c94723603ff6445082653f Author: John Baldwin AuthorDate: 2022-01-18 22:47:13 +0000 Commit: John Baldwin CommitDate: 2022-07-13 19:25:55 +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 in libsodium. This is different from the code in main in that this uses libsodium directly. The version in main uses the software backend shared with OCF, but main required changes that break the ABI of struct enc_xform that cannot be merged to stable/13. Sponsored by: The FreeBSD Foundation (cherry picked from commit e71680049bb8ff395aeaa144377dd9e49331f45e) --- sys/conf/files | 2 ++ sys/crypto/chacha20_poly1305.c | 82 ++++++++++++++++++++++++++++++++++++++++++ sys/crypto/chacha20_poly1305.h | 54 ++++++++++++++++++++++++++++ sys/modules/crypto/Makefile | 8 +++++ 4 files changed, 146 insertions(+) diff --git a/sys/conf/files b/sys/conf/files index 57afd7c987b0..491528a3d63b 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -740,6 +740,8 @@ crypto/camellia/camellia.c optional crypto crypto/camellia/camellia-api.c optional crypto crypto/chacha20/chacha.c standard crypto/chacha20/chacha-sw.c optional crypto +crypto/chacha20_poly1305.c optional crypto \ + compile-with "${NORMAL_C} -I$S/contrib/libsodium/src/libsodium/include -I$S/crypto/libsodium" 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..9aaa570ccca4 --- /dev/null +++ b/sys/crypto/chacha20_poly1305.c @@ -0,0 +1,82 @@ +/*- + * 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 +#include + +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) +{ + if (nonce_len == crypto_aead_chacha20poly1305_ietf_NPUBBYTES) + crypto_aead_chacha20poly1305_ietf_encrypt(dst, NULL, src, + src_len, aad, aad_len, NULL, nonce, key); + else + crypto_aead_chacha20poly1305_encrypt(dst, NULL, src, + src_len, aad, aad_len, NULL, nonce, 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) +{ + int ret; + + if (nonce_len == crypto_aead_chacha20poly1305_ietf_NPUBBYTES) + ret = crypto_aead_chacha20poly1305_ietf_decrypt(dst, NULL, + NULL, src, src_len, aad, aad_len, nonce, key); + else + ret = crypto_aead_chacha20poly1305_decrypt(dst, NULL, + NULL, src, src_len, aad, aad_len, nonce, key); + return (ret == 0); +} + +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) +{ + crypto_aead_xchacha20poly1305_ietf_encrypt(dst, NULL, src, src_len, + aad, aad_len, NULL, nonce, 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) +{ + return (crypto_aead_xchacha20poly1305_ietf_decrypt(dst, NULL, NULL, + src, src_len, aad, aad_len, nonce, key) == 0); +} 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..c25d8f83929e 100644 --- a/sys/modules/crypto/Makefile +++ b/sys/modules/crypto/Makefile @@ -13,6 +13,8 @@ LIBSODIUM=${SRCTOP}/sys/contrib/libsodium/src/libsodium .PATH: ${SRCTOP}/sys/crypto/blake2 .PATH: ${SRCTOP}/sys/crypto/chacha20 .PATH: ${SRCTOP}/sys/contrib/libb2 +.PATH: ${LIBSODIUM}/crypto_aead/chacha20poly1305/sodium +.PATH: ${LIBSODIUM}/crypto_aead/xchacha20poly1305/sodium .PATH: ${LIBSODIUM}/crypto_core/hchacha20 .PATH: ${LIBSODIUM}/crypto_onetimeauth/poly1305 .PATH: ${LIBSODIUM}/crypto_onetimeauth/poly1305/donna @@ -55,6 +57,8 @@ 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 +CFLAGS.chacha20_poly1305.c += -I${LIBSODIUM_INC} -I${LIBSODIUM_COMPAT} LIBSODIUM_INC=${LIBSODIUM}/include LIBSODIUM_COMPAT=${SRCTOP}/sys/crypto/libsodium @@ -62,6 +66,10 @@ SRCS += xform_chacha20_poly1305.c CFLAGS.xform_chacha20_poly1305.c+= -I${LIBSODIUM_INC} -I${LIBSODIUM_COMPAT} SRCS += xform_poly1305.c CFLAGS.xform_poly1305.c += -I${LIBSODIUM_INC} -I${LIBSODIUM_COMPAT} +SRCS += aead_chacha20poly1305.c +CFLAGS.aead_chacha20poly1305.c += -I${LIBSODIUM_INC}/sodium -I${LIBSODIUM_COMPAT} +SRCS += aead_xchacha20poly1305.c +CFLAGS.aead_xchacha20poly1305.c += -I${LIBSODIUM_INC}/sodium -I${LIBSODIUM_COMPAT} SRCS += core_hchacha20.c CFLAGS.core_hchacha20.c += -I${LIBSODIUM_INC}/sodium -I${LIBSODIUM_COMPAT} SRCS += onetimeauth_poly1305.c