• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file cipher_internal.h
3  *
4  * \brief Cipher wrappers.
5  *
6  * \author Adriaan de Jong <dejong@fox-it.com>
7  */
8 /*
9  *  Copyright The Mbed TLS Contributors
10  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
11  */
12 #ifndef MBEDTLS_CIPHER_WRAP_H
13 #define MBEDTLS_CIPHER_WRAP_H
14 
15 #if !defined(MBEDTLS_CONFIG_FILE)
16 #include "mbedtls/config.h"
17 #else
18 #include MBEDTLS_CONFIG_FILE
19 #endif
20 
21 #include "mbedtls/cipher.h"
22 
23 #if defined(MBEDTLS_USE_PSA_CRYPTO)
24 #include "psa/crypto.h"
25 #endif /* MBEDTLS_USE_PSA_CRYPTO */
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /**
32  * Base cipher information. The non-mode specific functions and values.
33  */
34 struct mbedtls_cipher_base_t {
35     /** Base Cipher type (e.g. MBEDTLS_CIPHER_ID_AES) */
36     mbedtls_cipher_id_t cipher;
37 
38     /** Encrypt using ECB */
39     int (*ecb_func)(void *ctx, mbedtls_operation_t mode,
40                     const unsigned char *input, unsigned char *output);
41 
42 #if defined(MBEDTLS_CIPHER_MODE_CBC)
43     /** Encrypt using CBC */
44     int (*cbc_func)(void *ctx, mbedtls_operation_t mode, size_t length,
45                     unsigned char *iv, const unsigned char *input,
46                     unsigned char *output);
47 #endif
48 
49 #if defined(MBEDTLS_CIPHER_MODE_CFB)
50     /** Encrypt using CFB (Full length) */
51     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);
54 #endif
55 
56 #if defined(MBEDTLS_CIPHER_MODE_OFB)
57     /** Encrypt using OFB (Full length) */
58     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);
62 #endif
63 
64 #if defined(MBEDTLS_CIPHER_MODE_CTR)
65     /** Encrypt using CTR */
66     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);
69 #endif
70 
71 #if defined(MBEDTLS_CIPHER_MODE_XTS)
72     /** Encrypt or decrypt using XTS. */
73     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);
76 #endif
77 
78 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
79     /** Encrypt using STREAM */
80     int (*stream_func)(void *ctx, size_t length,
81                        const unsigned char *input, unsigned char *output);
82 #endif
83 
84     /** Set key for encryption purposes */
85     int (*setkey_enc_func)(void *ctx, const unsigned char *key,
86                            unsigned int key_bitlen);
87 
88     /** Set key for decryption purposes */
89     int (*setkey_dec_func)(void *ctx, const unsigned char *key,
90                            unsigned int key_bitlen);
91 
92     /** Allocate a new context */
93     void * (*ctx_alloc_func)(void);
94 
95     /** Free the given context */
96     void (*ctx_free_func)(void *ctx);
97 
98 };
99 
100 typedef struct {
101     mbedtls_cipher_type_t type;
102     const mbedtls_cipher_info_t *info;
103 } mbedtls_cipher_definition_t;
104 
105 #if defined(MBEDTLS_USE_PSA_CRYPTO)
106 typedef enum {
107     MBEDTLS_CIPHER_PSA_KEY_UNSET = 0,
108     MBEDTLS_CIPHER_PSA_KEY_OWNED, /* Used for PSA-based cipher contexts which */
109                                   /* use raw key material internally imported */
110                                   /* as a volatile key, and which hence need  */
111                                   /* to destroy that key when the context is  */
112                                   /* freed.                                   */
113     MBEDTLS_CIPHER_PSA_KEY_NOT_OWNED, /* Used for PSA-based cipher contexts   */
114                                       /* which use a key provided by the      */
115                                       /* user, and which hence will not be    */
116                                       /* destroyed when the context is freed. */
117 } mbedtls_cipher_psa_key_ownership;
118 
119 typedef struct {
120     psa_algorithm_t alg;
121     psa_key_id_t slot;
122     mbedtls_cipher_psa_key_ownership slot_state;
123 } mbedtls_cipher_context_psa;
124 #endif /* MBEDTLS_USE_PSA_CRYPTO */
125 
126 extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[];
127 
128 extern int mbedtls_cipher_supported[];
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 #endif /* MBEDTLS_CIPHER_WRAP_H */
135