13
13
* <em>ISO/IEC 18033-2:2006: Information technology -- Security
14
14
* techniques -- Encryption algorithms -- Part 2: Asymmetric
15
15
* ciphers</em>.
16
+ *
17
+ * The AES-XTS block mode is standardized by NIST SP 800-38E
18
+ * <https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38e.pdf>
19
+ * and described in detail by IEEE P1619
20
+ * <https://ieeexplore.ieee.org/servlet/opac?punumber=4375278>.
16
21
*/
17
22
18
23
/* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
@@ -74,7 +79,7 @@ extern "C" {
74
79
/**
75
80
* \brief The AES context-type definition.
76
81
*/
77
- typedef struct
82
+ typedef struct mbedtls_aes_context
78
83
{
79
84
int nr ; /*!< The number of rounds. */
80
85
uint32_t * rk ; /*!< AES round keys. */
@@ -89,6 +94,19 @@ typedef struct
89
94
}
90
95
mbedtls_aes_context ;
91
96
97
+ #if defined(MBEDTLS_CIPHER_MODE_XTS )
98
+ /**
99
+ * \brief The AES XTS context-type definition.
100
+ */
101
+ typedef struct mbedtls_aes_xts_context
102
+ {
103
+ mbedtls_aes_context crypt ; /*!< The AES context to use for AES block
104
+ encryption or decryption. */
105
+ mbedtls_aes_context tweak ; /*!< The AES context used for tweak
106
+ computation. */
107
+ } mbedtls_aes_xts_context ;
108
+ #endif /* MBEDTLS_CIPHER_MODE_XTS */
109
+
92
110
#else /* MBEDTLS_AES_ALT */
93
111
#include "aes_alt.h"
94
112
#endif /* MBEDTLS_AES_ALT */
@@ -110,6 +128,25 @@ void mbedtls_aes_init( mbedtls_aes_context *ctx );
110
128
*/
111
129
void mbedtls_aes_free ( mbedtls_aes_context * ctx );
112
130
131
+ #if defined(MBEDTLS_CIPHER_MODE_XTS )
132
+ /**
133
+ * \brief This function initializes the specified AES XTS context.
134
+ *
135
+ * It must be the first API called before using
136
+ * the context.
137
+ *
138
+ * \param ctx The AES XTS context to initialize.
139
+ */
140
+ void mbedtls_aes_xts_init ( mbedtls_aes_xts_context * ctx );
141
+
142
+ /**
143
+ * \brief This function releases and clears the specified AES XTS context.
144
+ *
145
+ * \param ctx The AES XTS context to clear.
146
+ */
147
+ void mbedtls_aes_xts_free ( mbedtls_aes_xts_context * ctx );
148
+ #endif /* MBEDTLS_CIPHER_MODE_XTS */
149
+
113
150
/**
114
151
* \brief This function sets the encryption key.
115
152
*
@@ -142,6 +179,44 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
142
179
int mbedtls_aes_setkey_dec ( mbedtls_aes_context * ctx , const unsigned char * key ,
143
180
unsigned int keybits );
144
181
182
+ #if defined(MBEDTLS_CIPHER_MODE_XTS )
183
+ /**
184
+ * \brief This function prepares an XTS context for encryption and
185
+ * sets the encryption key.
186
+ *
187
+ * \param ctx The AES XTS context to which the key should be bound.
188
+ * \param key The encryption key. This is comprised of the XTS key1
189
+ * concatenated with the XTS key2.
190
+ * \param keybits The size of \p key passed in bits. Valid options are:
191
+ * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
192
+ * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
193
+ *
194
+ * \return \c 0 on success.
195
+ * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
196
+ */
197
+ int mbedtls_aes_xts_setkey_enc ( mbedtls_aes_xts_context * ctx ,
198
+ const unsigned char * key ,
199
+ unsigned int keybits );
200
+
201
+ /**
202
+ * \brief This function prepares an XTS context for decryption and
203
+ * sets the decryption key.
204
+ *
205
+ * \param ctx The AES XTS context to which the key should be bound.
206
+ * \param key The decryption key. This is comprised of the XTS key1
207
+ * concatenated with the XTS key2.
208
+ * \param keybits The size of \p key passed in bits. Valid options are:
209
+ * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
210
+ * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
211
+ *
212
+ * \return \c 0 on success.
213
+ * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
214
+ */
215
+ int mbedtls_aes_xts_setkey_dec ( mbedtls_aes_xts_context * ctx ,
216
+ const unsigned char * key ,
217
+ unsigned int keybits );
218
+ #endif /* MBEDTLS_CIPHER_MODE_XTS */
219
+
145
220
/**
146
221
* \brief This function performs an AES single-block encryption or
147
222
* decryption operation.
@@ -213,6 +288,49 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
213
288
unsigned char * output );
214
289
#endif /* MBEDTLS_CIPHER_MODE_CBC */
215
290
291
+ #if defined(MBEDTLS_CIPHER_MODE_XTS )
292
+ /**
293
+ * \brief This function performs an AES-XTS encryption or decryption
294
+ * operation for an entire XTS data unit.
295
+ *
296
+ * AES-XTS encrypts or decrypts blocks based on their location as
297
+ * defined by a data unit number. The data unit number must be
298
+ * provided by \p data_unit.
299
+ *
300
+ * NIST SP 800-38E limits the maximum size of a data unit to 2^20
301
+ * AES blocks. If the data unit is larger than this, this function
302
+ * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
303
+ *
304
+ * \param ctx The AES XTS context to use for AES XTS operations.
305
+ * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
306
+ * #MBEDTLS_AES_DECRYPT.
307
+ * \param length The length of a data unit in bytes. This can be any
308
+ * length between 16 bytes and 2^24 bytes inclusive
309
+ * (between 1 and 2^20 block cipher blocks).
310
+ * \param data_unit The address of the data unit encoded as an array of 16
311
+ * bytes in little-endian format. For disk encryption, this
312
+ * is typically the index of the block device sector that
313
+ * contains the data.
314
+ * \param input The buffer holding the input data (which is an entire
315
+ * data unit). This function reads \p length bytes from \p
316
+ * input.
317
+ * \param output The buffer holding the output data (which is an entire
318
+ * data unit). This function writes \p length bytes to \p
319
+ * output.
320
+ *
321
+ * \return \c 0 on success.
322
+ * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
323
+ * smaller than an AES block in size (16 bytes) or if \p
324
+ * length is larger than 2^20 blocks (16 MiB).
325
+ */
326
+ int mbedtls_aes_crypt_xts ( mbedtls_aes_xts_context * ctx ,
327
+ int mode ,
328
+ size_t length ,
329
+ const unsigned char data_unit [16 ],
330
+ const unsigned char * input ,
331
+ unsigned char * output );
332
+ #endif /* MBEDTLS_CIPHER_MODE_XTS */
333
+
216
334
#if defined(MBEDTLS_CIPHER_MODE_CFB )
217
335
/**
218
336
* \brief This function performs an AES-CFB128 encryption or decryption
@@ -296,6 +414,56 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
296
414
unsigned char * output );
297
415
#endif /*MBEDTLS_CIPHER_MODE_CFB */
298
416
417
+ #if defined(MBEDTLS_CIPHER_MODE_OFB )
418
+ /**
419
+ * \brief This function performs an AES-OFB (Output Feedback Mode)
420
+ * encryption or decryption operation.
421
+ *
422
+ * For OFB, you must set up the context with
423
+ * mbedtls_aes_setkey_enc(), regardless of whether you are
424
+ * performing an encryption or decryption operation. This is
425
+ * because OFB mode uses the same key schedule for encryption and
426
+ * decryption.
427
+ *
428
+ * The OFB operation is identical for encryption or decryption,
429
+ * therefore no operation mode needs to be specified.
430
+ *
431
+ * \note Upon exit, the content of iv, the Initialisation Vector, is
432
+ * updated so that you can call the same function again on the next
433
+ * block(s) of data and get the same result as if it was encrypted
434
+ * in one call. This allows a "streaming" usage, by initialising
435
+ * iv_off to 0 before the first call, and preserving its value
436
+ * between calls.
437
+ *
438
+ * For non-streaming use, the iv should be initialised on each call
439
+ * to a unique value, and iv_off set to 0 on each call.
440
+ *
441
+ * If you need to retain the contents of the initialisation vector,
442
+ * you must either save it manually or use the cipher module
443
+ * instead.
444
+ *
445
+ * \warning For the OFB mode, the initialisation vector must be unique
446
+ * every encryption operation. Reuse of an initialisation vector
447
+ * will compromise security.
448
+ *
449
+ * \param ctx The AES context to use for encryption or decryption.
450
+ * \param length The length of the input data.
451
+ * \param iv_off The offset in IV (updated after use).
452
+ * \param iv The initialization vector (updated after use).
453
+ * \param input The buffer holding the input data.
454
+ * \param output The buffer holding the output data.
455
+ *
456
+ * \return \c 0 on success.
457
+ */
458
+ int mbedtls_aes_crypt_ofb ( mbedtls_aes_context * ctx ,
459
+ size_t length ,
460
+ size_t * iv_off ,
461
+ unsigned char iv [16 ],
462
+ const unsigned char * input ,
463
+ unsigned char * output );
464
+
465
+ #endif /* MBEDTLS_CIPHER_MODE_OFB */
466
+
299
467
#if defined(MBEDTLS_CIPHER_MODE_CTR )
300
468
/**
301
469
* \brief This function performs an AES-CTR encryption or decryption
0 commit comments