2009-01-03 21:22:43 +00:00
|
|
|
/**
|
|
|
|
* \file des.h
|
2009-01-04 16:27:10 +00:00
|
|
|
*
|
2011-01-27 15:24:17 +00:00
|
|
|
* \brief DES block cipher
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2018-01-05 15:33:17 +00:00
|
|
|
*/
|
|
|
|
/*
|
2015-07-27 09:11:48 +00:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 12:21:07 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-04 16:27:10 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-01-04 16:27:10 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
#ifndef MBEDTLS_DES_H
|
|
|
|
#define MBEDTLS_DES_H
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2013-06-24 17:20:35 +00:00
|
|
|
#include "config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2013-06-24 17:20:35 +00:00
|
|
|
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <stddef.h>
|
2015-04-15 09:53:16 +00:00
|
|
|
#include <stdint.h>
|
2012-10-01 14:41:15 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_DES_ENCRYPT 1
|
|
|
|
#define MBEDTLS_DES_DECRYPT 0
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
|
2018-10-04 07:59:13 +00:00
|
|
|
|
|
|
|
/* MBEDTLS_ERR_DES_HW_ACCEL_FAILED is deprecated and should not be used. */
|
2018-01-26 16:56:38 +00:00
|
|
|
#define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033 /**< DES hardware accelerator failed. */
|
2010-03-18 21:21:02 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_DES_KEY_SIZE 8
|
2011-01-15 17:32:24 +00:00
|
|
|
|
2013-06-27 12:29:21 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2017-05-18 13:53:08 +00:00
|
|
|
#if !defined(MBEDTLS_DES_ALT)
|
|
|
|
// Regular implementation
|
|
|
|
//
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/**
|
|
|
|
* \brief DES context structure
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2018-07-24 08:02:47 +00:00
|
|
|
typedef struct mbedtls_des_context
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2012-10-01 14:41:15 +00:00
|
|
|
uint32_t sk[32]; /*!< DES subkeys */
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_context;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Triple-DES context structure
|
|
|
|
*/
|
2018-07-24 08:02:47 +00:00
|
|
|
typedef struct mbedtls_des3_context
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2012-10-01 14:41:15 +00:00
|
|
|
uint32_t sk[96]; /*!< 3DES subkeys */
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_context;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2018-04-16 14:40:04 +00:00
|
|
|
#else /* MBEDTLS_DES_ALT */
|
|
|
|
#include "des_alt.h"
|
|
|
|
#endif /* MBEDTLS_DES_ALT */
|
|
|
|
|
2014-06-18 09:12:03 +00:00
|
|
|
/**
|
|
|
|
* \brief Initialize DES context
|
|
|
|
*
|
|
|
|
* \param ctx DES context to be initialized
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2014-06-18 09:12:03 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_des_init( mbedtls_des_context *ctx );
|
2014-06-18 09:12:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Clear DES context
|
|
|
|
*
|
|
|
|
* \param ctx DES context to be cleared
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2014-06-18 09:12:03 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_des_free( mbedtls_des_context *ctx );
|
2014-06-18 09:12:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Initialize Triple-DES context
|
|
|
|
*
|
|
|
|
* \param ctx DES3 context to be initialized
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_des3_init( mbedtls_des3_context *ctx );
|
2014-06-18 09:12:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Clear Triple-DES context
|
|
|
|
*
|
|
|
|
* \param ctx DES3 context to be cleared
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_des3_free( mbedtls_des3_context *ctx );
|
2014-06-18 09:12:03 +00:00
|
|
|
|
2011-01-15 17:32:24 +00:00
|
|
|
/**
|
|
|
|
* \brief Set key parity on the given key to odd.
|
|
|
|
*
|
|
|
|
* DES keys are 56 bits long, but each byte is padded with
|
|
|
|
* a parity bit to allow verification.
|
|
|
|
*
|
|
|
|
* \param key 8-byte secret key
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2011-01-15 17:32:24 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
|
2011-01-15 17:32:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Check that key parity on the given key is odd.
|
|
|
|
*
|
|
|
|
* DES keys are 56 bits long, but each byte is padded with
|
|
|
|
* a parity bit to allow verification.
|
|
|
|
*
|
|
|
|
* \param key 8-byte secret key
|
2011-07-06 14:37:33 +00:00
|
|
|
*
|
|
|
|
* \return 0 is parity was ok, 1 if parity was not correct.
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2011-01-15 17:32:24 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
|
2011-01-15 17:32:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Check that key is not a weak or semi-weak DES key
|
|
|
|
*
|
|
|
|
* \param key 8-byte secret key
|
2011-07-06 14:37:33 +00:00
|
|
|
*
|
2011-08-17 09:40:55 +00:00
|
|
|
* \return 0 if no weak key was found, 1 if a weak key was identified.
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2011-01-15 17:32:24 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
|
2011-01-15 17:32:24 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/**
|
|
|
|
* \brief DES key schedule (56-bit, encryption)
|
|
|
|
*
|
|
|
|
* \param ctx DES context to be initialized
|
|
|
|
* \param key 8-byte secret key
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
|
|
|
* \return 0
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief DES key schedule (56-bit, decryption)
|
|
|
|
*
|
|
|
|
* \param ctx DES context to be initialized
|
|
|
|
* \param key 8-byte secret key
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
|
|
|
* \return 0
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Triple-DES key schedule (112-bit, encryption)
|
|
|
|
*
|
|
|
|
* \param ctx 3DES context to be initialized
|
|
|
|
* \param key 16-byte secret key
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
|
|
|
* \return 0
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
|
|
|
|
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Triple-DES key schedule (112-bit, decryption)
|
|
|
|
*
|
|
|
|
* \param ctx 3DES context to be initialized
|
|
|
|
* \param key 16-byte secret key
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
|
|
|
* \return 0
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
|
|
|
|
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Triple-DES key schedule (168-bit, encryption)
|
|
|
|
*
|
|
|
|
* \param ctx 3DES context to be initialized
|
|
|
|
* \param key 24-byte secret key
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
|
|
|
* \return 0
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
|
|
|
|
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Triple-DES key schedule (168-bit, decryption)
|
|
|
|
*
|
|
|
|
* \param ctx 3DES context to be initialized
|
|
|
|
* \param key 24-byte secret key
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
|
|
|
* \return 0
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
|
|
|
|
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief DES-ECB block encryption/decryption
|
|
|
|
*
|
|
|
|
* \param ctx DES context
|
|
|
|
* \param input 64-bit input block
|
|
|
|
* \param output 64-bit output block
|
2010-03-18 21:21:02 +00:00
|
|
|
*
|
2010-03-21 15:43:59 +00:00
|
|
|
* \return 0 if successful
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
|
2010-03-16 21:09:09 +00:00
|
|
|
const unsigned char input[8],
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char output[8] );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2009-01-03 21:22:43 +00:00
|
|
|
/**
|
|
|
|
* \brief DES-CBC buffer encryption/decryption
|
|
|
|
*
|
2015-01-23 16:19:47 +00:00
|
|
|
* \note Upon exit, the content of the IV is updated so that you can
|
|
|
|
* call the function same function again on the following
|
|
|
|
* block(s) of data and get the same result as if it was
|
|
|
|
* encrypted in one call. This allows a "streaming" usage.
|
|
|
|
* If on the other hand you need to retain the contents of the
|
|
|
|
* IV, you should either save it manually or use the cipher
|
|
|
|
* module instead.
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \param ctx DES context
|
2015-04-08 10:49:31 +00:00
|
|
|
* \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
|
2009-01-03 21:22:43 +00:00
|
|
|
* \param length length of the input data
|
|
|
|
* \param iv initialization vector (updated after use)
|
|
|
|
* \param input buffer holding the input data
|
|
|
|
* \param output buffer holding the output data
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
|
2009-01-03 21:22:43 +00:00
|
|
|
int mode,
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t length,
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char iv[8],
|
2010-03-16 21:09:09 +00:00
|
|
|
const unsigned char *input,
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char *output );
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief 3DES-ECB block encryption/decryption
|
|
|
|
*
|
|
|
|
* \param ctx 3DES context
|
|
|
|
* \param input 64-bit input block
|
|
|
|
* \param output 64-bit output block
|
2010-03-18 21:21:02 +00:00
|
|
|
*
|
2010-03-21 15:43:59 +00:00
|
|
|
* \return 0 if successful
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
|
2010-03-16 21:09:09 +00:00
|
|
|
const unsigned char input[8],
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char output[8] );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2009-01-03 21:22:43 +00:00
|
|
|
/**
|
|
|
|
* \brief 3DES-CBC buffer encryption/decryption
|
|
|
|
*
|
2015-01-23 16:19:47 +00:00
|
|
|
* \note Upon exit, the content of the IV is updated so that you can
|
|
|
|
* call the function same function again on the following
|
|
|
|
* block(s) of data and get the same result as if it was
|
|
|
|
* encrypted in one call. This allows a "streaming" usage.
|
|
|
|
* If on the other hand you need to retain the contents of the
|
|
|
|
* IV, you should either save it manually or use the cipher
|
|
|
|
* module instead.
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \param ctx 3DES context
|
2015-04-08 10:49:31 +00:00
|
|
|
* \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
|
2009-01-03 21:22:43 +00:00
|
|
|
* \param length length of the input data
|
|
|
|
* \param iv initialization vector (updated after use)
|
|
|
|
* \param input buffer holding the input data
|
|
|
|
* \param output buffer holding the output data
|
2010-03-18 21:21:02 +00:00
|
|
|
*
|
2015-04-08 10:49:31 +00:00
|
|
|
* \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
|
2009-01-03 21:22:43 +00:00
|
|
|
int mode,
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t length,
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char iv[8],
|
2010-03-16 21:09:09 +00:00
|
|
|
const unsigned char *input,
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char *output );
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-05-12 13:02:45 +00:00
|
|
|
/**
|
|
|
|
* \brief Internal function for key expansion.
|
|
|
|
* (Only exposed to allow overriding it,
|
|
|
|
* see MBEDTLS_DES_SETKEY_ALT)
|
|
|
|
*
|
|
|
|
* \param SK Round keys
|
|
|
|
* \param key Base key
|
2017-09-25 13:53:51 +00:00
|
|
|
*
|
|
|
|
* \warning DES is considered a weak cipher and its use constitutes a
|
|
|
|
* security risk. We recommend considering stronger ciphers
|
|
|
|
* instead.
|
2015-05-12 13:02:45 +00:00
|
|
|
*/
|
|
|
|
void mbedtls_des_setkey( uint32_t SK[32],
|
|
|
|
const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
|
2013-06-24 17:20:35 +00:00
|
|
|
|
2017-06-20 12:48:46 +00:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
|
2012-11-14 12:39:52 +00:00
|
|
|
/**
|
2009-01-03 21:22:43 +00:00
|
|
|
* \brief Checkup routine
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or 1 if the test failed
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_des_self_test( int verbose );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2017-06-20 12:48:46 +00:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* des.h */
|