From: Tobias Brunner Date: Wed, 9 Feb 2022 10:54:28 +0000 (+0100) Subject: crypto: Add interface for key derivation functions X-Git-Tag: 5.9.6rc1~2^2~35 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=40a09613d277acbecab37a7932c2e7b885f2ea64;p=thirdparty%2Fstrongswan.git crypto: Add interface for key derivation functions --- diff --git a/src/libstrongswan/Android.mk b/src/libstrongswan/Android.mk index c1001905cc..fa9f2b2448 100644 --- a/src/libstrongswan/Android.mk +++ b/src/libstrongswan/Android.mk @@ -17,7 +17,7 @@ crypto/prf_plus.c crypto/signers/signer.c \ crypto/signers/mac_signer.c crypto/crypto_factory.c crypto/crypto_tester.c \ crypto/diffie_hellman.c crypto/aead.c crypto/transform.c \ crypto/iv/iv_gen.c crypto/iv/iv_gen_rand.c crypto/iv/iv_gen_seq.c \ -crypto/iv/iv_gen_null.c \ +crypto/iv/iv_gen_null.c crypto/kdfs/kdf.c \ crypto/xofs/xof.c crypto/xofs/xof_bitspender.c \ credentials/credential_factory.c credentials/builder.c \ credentials/cred_encoding.c credentials/keys/private_key.c \ diff --git a/src/libstrongswan/Makefile.am b/src/libstrongswan/Makefile.am index 37fe4b1e51..791b2bcff8 100644 --- a/src/libstrongswan/Makefile.am +++ b/src/libstrongswan/Makefile.am @@ -15,7 +15,7 @@ crypto/prf_plus.c crypto/signers/signer.c \ crypto/signers/mac_signer.c crypto/crypto_factory.c crypto/crypto_tester.c \ crypto/diffie_hellman.c crypto/aead.c crypto/transform.c \ crypto/iv/iv_gen.c crypto/iv/iv_gen_rand.c crypto/iv/iv_gen_seq.c \ -crypto/iv/iv_gen_null.c \ +crypto/iv/iv_gen_null.c crypto/kdfs/kdf.c \ crypto/xofs/xof.c crypto/xofs/xof_bitspender.c \ credentials/credential_factory.c credentials/builder.c \ credentials/cred_encoding.c credentials/keys/private_key.c \ @@ -83,6 +83,7 @@ crypto/crypto_factory.h crypto/crypto_tester.h crypto/diffie_hellman.h \ crypto/aead.h crypto/transform.h crypto/pkcs5.h crypto/iv/iv_gen.h \ crypto/iv/iv_gen_rand.h crypto/iv/iv_gen_seq.h crypto/iv/iv_gen_null.h \ crypto/xofs/xof.h crypto/xofs/xof_bitspender.h crypto/xofs/mgf1.h \ +crypto/kdfs/kdf.h \ credentials/credential_factory.h credentials/builder.h \ credentials/cred_encoding.h credentials/keys/private_key.h \ credentials/keys/public_key.h credentials/keys/shared_key.h \ diff --git a/src/libstrongswan/crypto/kdfs/kdf.c b/src/libstrongswan/crypto/kdfs/kdf.c new file mode 100644 index 0000000000..24dcb424d4 --- /dev/null +++ b/src/libstrongswan/crypto/kdfs/kdf.c @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2022 Tobias Brunner, codelabs GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "kdf.h" + +ENUM(key_derivation_function_names, KDF_UNDEFINED, KDF_PRF_PLUS, + "KDF_UNDEFINED", + "KDF_PRF_PLUS", +); diff --git a/src/libstrongswan/crypto/kdfs/kdf.h b/src/libstrongswan/crypto/kdfs/kdf.h new file mode 100644 index 0000000000..4c1a77ecaa --- /dev/null +++ b/src/libstrongswan/crypto/kdfs/kdf.h @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2022 Tobias Brunner, codelabs GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * @defgroup kdf kdf + * @{ @ingroup crypto + */ + +#ifndef KDF_H_ +#define KDF_H_ + +typedef enum key_derivation_function_t key_derivation_function_t; +typedef enum kdf_param_t kdf_param_t; +typedef struct kdf_t kdf_t; + +#include + +/** + * Key Derivation Functions (KDF). + */ +enum key_derivation_function_t { + + KDF_UNDEFINED, + + /** + * RFC 7296 prf+, expects a pseudo_random_function_t in the constructor, + * parameters are KEY and SALT. + */ + KDF_PRF_PLUS, +}; + +/** + * enum name for key_derivation_function_t. + */ +extern enum_name_t *key_derivation_function_names; + +/** + * Parameters for KDFs. + */ +enum kdf_param_t { + + /** + * Key used for the key derivation (chunk_t). + */ + KDF_PARAM_KEY, + + /** + * Salt used for the key derivation (chunk_t). + */ + KDF_PARAM_SALT, +}; + +/** + * Generic interface for Key Derivation Functions (KDF). + * + * Note that in comparison to xof_t, this interface does not support streaming. + * That is, calling get_bytes() or allocate_bytes() multiple times without + * changing the input parameters will result in the same output. + */ +struct kdf_t { + + /** + * Return the type of KDF. + * + * @return KDF type + */ + key_derivation_function_t (*get_type)(kdf_t *this); + + /** + * Derives a key of the given length and writes it to the buffer. + * + * @param out_len number of key bytes requested + * @param buffer pointer where the derived key will be written + * @return TRUE if key derived successfully + */ + bool (*get_bytes)(kdf_t *this, size_t out_len, + uint8_t *buffer) __attribute__((warn_unused_result)); + + /** + * Derives a key of the given length and allocates space for it. + * + * @param out_len number of key bytes requested + * @param chunk chunk which will hold the derived key + * @return TRUE if key derived successfully + */ + bool (*allocate_bytes)(kdf_t *this, size_t out_len, + chunk_t *chunk) __attribute__((warn_unused_result)); + + /** + * Set a parameter for this KDF. + * + * @param param parameter to set + * @param ... parameter values + * @return TRUE if parameter set successfully + */ + bool (*set_param)(kdf_t *this, kdf_param_t param, + ...) __attribute__((warn_unused_result)); + + /** + * Destroys this KDF object. + */ + void (*destroy)(kdf_t *this); +}; + +#endif /** KDF_H_ @}*/