blob: c98abab6872e9696536eeb805907f810a5838f05 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +02002 * \file cipher_internal.h
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Paul Bakker8123e9d2011-01-06 15:37:30 +00004 * \brief Cipher wrappers.
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
Darryl Greena40a1012018-01-05 15:33:17 +00007 */
8/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02009 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker8123e9d2011-01-06 15:37:30 +000011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#ifndef MBEDTLS_CIPHER_WRAP_H
13#define MBEDTLS_CIPHER_WRAP_H
Paul Bakker8123e9d2011-01-06 15:37:30 +000014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010016#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020017#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020019#endif
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000020
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010021#include "mbedtls/cipher.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000022
Hanno Becker6118e432018-11-09 16:47:20 +000023#if defined(MBEDTLS_USE_PSA_CRYPTO)
24#include "psa/crypto.h"
25#endif /* MBEDTLS_USE_PSA_CRYPTO */
26
Paul Bakker8123e9d2011-01-06 15:37:30 +000027#ifdef __cplusplus
28extern "C" {
29#endif
30
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010031/**
32 * Base cipher information. The non-mode specific functions and values.
33 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010034struct mbedtls_cipher_base_t {
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010035 /** Base Cipher type (e.g. MBEDTLS_CIPHER_ID_AES) */
36 mbedtls_cipher_id_t cipher;
37
38 /** Encrypt using ECB */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039 int (*ecb_func)(void *ctx, mbedtls_operation_t mode,
40 const unsigned char *input, unsigned char *output);
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010041
42#if defined(MBEDTLS_CIPHER_MODE_CBC)
43 /** Encrypt using CBC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044 int (*cbc_func)(void *ctx, mbedtls_operation_t mode, size_t length,
45 unsigned char *iv, const unsigned char *input,
46 unsigned char *output);
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010047#endif
48
49#if defined(MBEDTLS_CIPHER_MODE_CFB)
50 /** Encrypt using CFB (Full length) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 int (*cfb_func)(void *ctx, mbedtls_operation_t mode, size_t length, size_t *iv_off,
52 unsigned char *iv, const unsigned char *input,
53 unsigned char *output);
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010054#endif
55
Simon Butcher8c0fd1e2018-04-22 22:58:07 +010056#if defined(MBEDTLS_CIPHER_MODE_OFB)
57 /** Encrypt using OFB (Full length) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058 int (*ofb_func)(void *ctx, size_t length, size_t *iv_off,
59 unsigned char *iv,
60 const unsigned char *input,
61 unsigned char *output);
Simon Butcher8c0fd1e2018-04-22 22:58:07 +010062#endif
63
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010064#if defined(MBEDTLS_CIPHER_MODE_CTR)
65 /** Encrypt using CTR */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 int (*ctr_func)(void *ctx, size_t length, size_t *nc_off,
67 unsigned char *nonce_counter, unsigned char *stream_block,
68 const unsigned char *input, unsigned char *output);
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010069#endif
70
Jaeden Ameroc6539902018-04-30 17:17:41 +010071#if defined(MBEDTLS_CIPHER_MODE_XTS)
72 /** Encrypt or decrypt using XTS. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 int (*xts_func)(void *ctx, mbedtls_operation_t mode, size_t length,
74 const unsigned char data_unit[16],
75 const unsigned char *input, unsigned char *output);
Jaeden Ameroc6539902018-04-30 17:17:41 +010076#endif
77
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010078#if defined(MBEDTLS_CIPHER_MODE_STREAM)
79 /** Encrypt using STREAM */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 int (*stream_func)(void *ctx, size_t length,
81 const unsigned char *input, unsigned char *output);
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010082#endif
83
84 /** Set key for encryption purposes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 int (*setkey_enc_func)(void *ctx, const unsigned char *key,
86 unsigned int key_bitlen);
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010087
88 /** Set key for decryption purposes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 int (*setkey_dec_func)(void *ctx, const unsigned char *key,
90 unsigned int key_bitlen);
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010091
92 /** Allocate a new context */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 void * (*ctx_alloc_func)(void);
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010094
95 /** Free the given context */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 void (*ctx_free_func)(void *ctx);
Manuel Pégourié-Gonnard5a74e8b2015-05-06 17:10:55 +010097
98};
99
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100typedef struct {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_cipher_type_t type;
102 const mbedtls_cipher_info_t *info;
103} mbedtls_cipher_definition_t;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000104
Hanno Becker6118e432018-11-09 16:47:20 +0000105#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106typedef enum {
Hanno Becker19086552018-11-17 22:11:16 +0000107 MBEDTLS_CIPHER_PSA_KEY_UNSET = 0,
Hanno Becker9de97d72018-11-19 14:05:48 +0000108 MBEDTLS_CIPHER_PSA_KEY_OWNED, /* Used for PSA-based cipher contexts which */
109 /* use raw key material internally imported */
Gilles Peskine11392492019-05-27 14:53:19 +0200110 /* as a volatile key, and which hence need */
111 /* to destroy that key when the context is */
112 /* freed. */
Hanno Becker9de97d72018-11-19 14:05:48 +0000113 MBEDTLS_CIPHER_PSA_KEY_NOT_OWNED, /* Used for PSA-based cipher contexts */
Gilles Peskine11392492019-05-27 14:53:19 +0200114 /* which use a key provided by the */
115 /* user, and which hence will not be */
116 /* destroyed when the context is freed. */
Hanno Becker19086552018-11-17 22:11:16 +0000117} mbedtls_cipher_psa_key_ownership;
118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119typedef struct {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000120 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200121 psa_key_id_t slot;
Hanno Becker19086552018-11-17 22:11:16 +0000122 mbedtls_cipher_psa_key_ownership slot_state;
Hanno Becker6118e432018-11-09 16:47:20 +0000123} mbedtls_cipher_context_psa;
124#endif /* MBEDTLS_USE_PSA_CRYPTO */
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[];
Paul Bakker5e0efa72013-09-08 23:04:04 +0200127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128extern int mbedtls_cipher_supported[];
Paul Bakkerfab5c822012-02-06 16:45:10 +0000129
Paul Bakker8123e9d2011-01-06 15:37:30 +0000130#ifdef __cplusplus
131}
132#endif
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#endif /* MBEDTLS_CIPHER_WRAP_H */