2011-01-06 16:37:30 +01:00
|
|
|
/**
|
2021-03-09 18:03:29 +01:00
|
|
|
* \file cipher_wrap.h
|
2014-05-01 13:03:14 +02:00
|
|
|
*
|
2011-01-06 16:37:30 +01:00
|
|
|
* \brief Cipher wrappers.
|
|
|
|
*
|
|
|
|
* \author Adriaan de Jong <dejong@fox-it.com>
|
2018-01-05 16:33:17 +01:00
|
|
|
*/
|
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2011-01-06 16:37:30 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#ifndef MBEDTLS_CIPHER_WRAP_H
|
|
|
|
#define MBEDTLS_CIPHER_WRAP_H
|
2011-01-06 16:37:30 +01:00
|
|
|
|
2021-05-27 11:25:03 +02:00
|
|
|
#include "mbedtls/build_info.h"
|
2015-02-13 15:12:07 +01:00
|
|
|
|
2019-07-04 21:01:14 +02:00
|
|
|
#include "mbedtls/cipher.h"
|
2011-01-06 16:37:30 +01:00
|
|
|
|
2018-11-09 17:47:20 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2011-01-06 16:37:30 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2023-10-11 12:46:16 +02:00
|
|
|
/* Support for GCM either through Mbed TLS SW implementation or PSA */
|
|
|
|
#if defined(MBEDTLS_GCM_C) || \
|
|
|
|
(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_GCM))
|
2023-10-16 11:35:57 +02:00
|
|
|
#define MBEDTLS_CIPHER_HAVE_GCM_VIA_LEGACY_OR_USE_PSA
|
2023-10-11 12:46:16 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)) || \
|
|
|
|
(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_GCM) && defined(PSA_WANT_KEY_TYPE_AES))
|
2023-10-16 11:35:57 +02:00
|
|
|
#define MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA
|
2023-10-11 12:46:16 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CCM_C) || \
|
|
|
|
(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_CCM))
|
2023-10-16 11:35:57 +02:00
|
|
|
#define MBEDTLS_CIPHER_HAVE_CCM_VIA_LEGACY_OR_USE_PSA
|
2023-10-11 12:46:16 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)) || \
|
|
|
|
(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_CCM) && defined(PSA_WANT_KEY_TYPE_AES))
|
2023-10-16 11:35:57 +02:00
|
|
|
#define MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA
|
2023-10-11 12:46:16 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CCM_C) || \
|
|
|
|
(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_CCM_STAR_NO_TAG))
|
2023-10-16 11:35:57 +02:00
|
|
|
#define MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_VIA_LEGACY_OR_USE_PSA
|
2023-10-11 12:46:16 +02:00
|
|
|
#endif
|
|
|
|
|
2023-10-12 18:48:24 +02:00
|
|
|
#if (defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)) || \
|
|
|
|
(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_CCM_STAR_NO_TAG) && \
|
|
|
|
defined(PSA_WANT_KEY_TYPE_AES))
|
|
|
|
#define MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_AES_VIA_LEGACY_OR_USE_PSA
|
|
|
|
#endif
|
|
|
|
|
2023-10-11 12:46:16 +02:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C) || \
|
|
|
|
(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_ALG_CHACHA20_POLY1305))
|
2023-10-16 11:35:57 +02:00
|
|
|
#define MBEDTLS_CIPHER_HAVE_CHACHAPOLY_VIA_LEGACY_OR_USE_PSA
|
2023-10-11 12:46:16 +02:00
|
|
|
#endif
|
|
|
|
|
2023-10-16 11:35:57 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_HAVE_GCM_VIA_LEGACY_OR_USE_PSA) || \
|
|
|
|
defined(MBEDTLS_CIPHER_HAVE_CCM_VIA_LEGACY_OR_USE_PSA) || \
|
|
|
|
defined(MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_VIA_LEGACY_OR_USE_PSA) || \
|
|
|
|
defined(MBEDTLS_CIPHER_HAVE_CHACHAPOLY_VIA_LEGACY_OR_USE_PSA)
|
|
|
|
#define MBEDTLS_CIPHER_HAVE_SOME_AEAD_VIA_LEGACY_OR_USE_PSA
|
2023-10-11 12:46:16 +02:00
|
|
|
#endif
|
|
|
|
|
2015-05-06 18:10:55 +02:00
|
|
|
/**
|
|
|
|
* Base cipher information. The non-mode specific functions and values.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
struct mbedtls_cipher_base_t {
|
2015-05-06 18:10:55 +02:00
|
|
|
/** Base Cipher type (e.g. MBEDTLS_CIPHER_ID_AES) */
|
|
|
|
mbedtls_cipher_id_t cipher;
|
|
|
|
|
|
|
|
/** Encrypt using ECB */
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*ecb_func)(void *ctx, mbedtls_operation_t mode,
|
|
|
|
const unsigned char *input, unsigned char *output);
|
2015-05-06 18:10:55 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
|
|
/** Encrypt using CBC */
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*cbc_func)(void *ctx, mbedtls_operation_t mode, size_t length,
|
|
|
|
unsigned char *iv, const unsigned char *input,
|
|
|
|
unsigned char *output);
|
2015-05-06 18:10:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
|
|
|
/** Encrypt using CFB (Full length) */
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*cfb_func)(void *ctx, mbedtls_operation_t mode, size_t length, size_t *iv_off,
|
|
|
|
unsigned char *iv, const unsigned char *input,
|
|
|
|
unsigned char *output);
|
2015-05-06 18:10:55 +02:00
|
|
|
#endif
|
|
|
|
|
2018-04-22 23:58:07 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_OFB)
|
|
|
|
/** Encrypt using OFB (Full length) */
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*ofb_func)(void *ctx, size_t length, size_t *iv_off,
|
|
|
|
unsigned char *iv,
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output);
|
2018-04-22 23:58:07 +02:00
|
|
|
#endif
|
|
|
|
|
2015-05-06 18:10:55 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
|
|
|
/** Encrypt using CTR */
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*ctr_func)(void *ctx, size_t length, size_t *nc_off,
|
|
|
|
unsigned char *nonce_counter, unsigned char *stream_block,
|
|
|
|
const unsigned char *input, unsigned char *output);
|
2018-04-30 18:17:41 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
|
|
|
/** Encrypt or decrypt using XTS. */
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*xts_func)(void *ctx, mbedtls_operation_t mode, size_t length,
|
|
|
|
const unsigned char data_unit[16],
|
|
|
|
const unsigned char *input, unsigned char *output);
|
2015-05-06 18:10:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
|
|
|
|
/** Encrypt using STREAM */
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*stream_func)(void *ctx, size_t length,
|
|
|
|
const unsigned char *input, unsigned char *output);
|
2015-05-06 18:10:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Set key for encryption purposes */
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*setkey_enc_func)(void *ctx, const unsigned char *key,
|
|
|
|
unsigned int key_bitlen);
|
2015-05-06 18:10:55 +02:00
|
|
|
|
|
|
|
/** Set key for decryption purposes */
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*setkey_dec_func)(void *ctx, const unsigned char *key,
|
|
|
|
unsigned int key_bitlen);
|
2015-05-06 18:10:55 +02:00
|
|
|
|
|
|
|
/** Allocate a new context */
|
2023-01-11 14:50:10 +01:00
|
|
|
void * (*ctx_alloc_func)(void);
|
2015-05-06 18:10:55 +02:00
|
|
|
|
|
|
|
/** Free the given context */
|
2023-01-11 14:50:10 +01:00
|
|
|
void (*ctx_free_func)(void *ctx);
|
2015-05-06 18:10:55 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_cipher_type_t type;
|
|
|
|
const mbedtls_cipher_info_t *info;
|
|
|
|
} mbedtls_cipher_definition_t;
|
2011-01-06 16:37:30 +01:00
|
|
|
|
2018-11-09 17:47:20 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef enum {
|
2018-11-17 23:11:16 +01:00
|
|
|
MBEDTLS_CIPHER_PSA_KEY_UNSET = 0,
|
2018-11-19 15:05:48 +01:00
|
|
|
MBEDTLS_CIPHER_PSA_KEY_OWNED, /* Used for PSA-based cipher contexts which */
|
|
|
|
/* use raw key material internally imported */
|
2019-05-27 14:53:19 +02:00
|
|
|
/* as a volatile key, and which hence need */
|
|
|
|
/* to destroy that key when the context is */
|
|
|
|
/* freed. */
|
2018-11-19 15:05:48 +01:00
|
|
|
MBEDTLS_CIPHER_PSA_KEY_NOT_OWNED, /* Used for PSA-based cipher contexts */
|
2019-05-27 14:53:19 +02:00
|
|
|
/* which use a key provided by the */
|
|
|
|
/* user, and which hence will not be */
|
|
|
|
/* destroyed when the context is freed. */
|
2018-11-17 23:11:16 +01:00
|
|
|
} mbedtls_cipher_psa_key_ownership;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct {
|
2018-11-12 12:59:30 +01:00
|
|
|
psa_algorithm_t alg;
|
2022-01-03 12:53:24 +01:00
|
|
|
mbedtls_svc_key_id_t slot;
|
2018-11-17 23:11:16 +01:00
|
|
|
mbedtls_cipher_psa_key_ownership slot_state;
|
2018-11-09 17:47:20 +01:00
|
|
|
} mbedtls_cipher_context_psa;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[];
|
2013-09-08 23:04:04 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
extern int mbedtls_cipher_supported[];
|
2012-02-06 17:45:10 +01:00
|
|
|
|
2023-06-24 14:25:06 +02:00
|
|
|
extern const mbedtls_cipher_base_t *mbedtls_cipher_base_lookup_table[];
|
2023-06-24 13:51:06 +02:00
|
|
|
|
2011-01-06 16:37:30 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_CIPHER_WRAP_H */
|