takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers des.h Source File

des.h

00001 /**
00002  * \file des.h
00003  *
00004  * \brief DES block cipher
00005  *
00006  * \warning   DES is considered a weak cipher and its use constitutes a
00007  *            security risk. We recommend considering stronger ciphers
00008  *            instead.
00009  */
00010 /*
00011  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00012  *  SPDX-License-Identifier: Apache-2.0
00013  *
00014  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00015  *  not use this file except in compliance with the License.
00016  *  You may obtain a copy of the License at
00017  *
00018  *  http://www.apache.org/licenses/LICENSE-2.0
00019  *
00020  *  Unless required by applicable law or agreed to in writing, software
00021  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00022  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00023  *  See the License for the specific language governing permissions and
00024  *  limitations under the License.
00025  *
00026  *  This file is part of mbed TLS (https://tls.mbed.org)
00027  *
00028  */
00029 #ifndef MBEDTLS_DES_H
00030 #define MBEDTLS_DES_H
00031 
00032 #if !defined(MBEDTLS_CONFIG_FILE)
00033 #include "config.h"
00034 #else
00035 #include MBEDTLS_CONFIG_FILE
00036 #endif
00037 
00038 #include <stddef.h>
00039 #include <stdint.h>
00040 
00041 #define MBEDTLS_DES_ENCRYPT     1
00042 #define MBEDTLS_DES_DECRYPT     0
00043 
00044 #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH              -0x0032  /**< The data input has an invalid length. */
00045 #define MBEDTLS_ERR_DES_HW_ACCEL_FAILED                   -0x0033  /**< DES hardware accelerator failed. */
00046 
00047 #define MBEDTLS_DES_KEY_SIZE    8
00048 
00049 #ifdef __cplusplus
00050 extern "C" {
00051 #endif
00052 
00053 #if !defined(MBEDTLS_DES_ALT)
00054 // Regular implementation
00055 //
00056 
00057 /**
00058  * \brief          DES context structure
00059  *
00060  * \warning        DES is considered a weak cipher and its use constitutes a
00061  *                 security risk. We recommend considering stronger ciphers
00062  *                 instead.
00063  */
00064 typedef struct mbedtls_des_context
00065 {
00066     uint32_t sk [32];            /*!<  DES subkeys       */
00067 }
00068 mbedtls_des_context;
00069 
00070 /**
00071  * \brief          Triple-DES context structure
00072  */
00073 typedef struct mbedtls_des3_context
00074 {
00075     uint32_t sk [96];            /*!<  3DES subkeys      */
00076 }
00077 mbedtls_des3_context;
00078 
00079 #else  /* MBEDTLS_DES_ALT */
00080 #include "des_alt.h"
00081 #endif /* MBEDTLS_DES_ALT */
00082 
00083 /**
00084  * \brief          Initialize DES context
00085  *
00086  * \param ctx      DES context to be initialized
00087  *
00088  * \warning        DES is considered a weak cipher and its use constitutes a
00089  *                 security risk. We recommend considering stronger ciphers
00090  *                 instead.
00091  */
00092 void mbedtls_des_init( mbedtls_des_context *ctx );
00093 
00094 /**
00095  * \brief          Clear DES context
00096  *
00097  * \param ctx      DES context to be cleared
00098  *
00099  * \warning        DES is considered a weak cipher and its use constitutes a
00100  *                 security risk. We recommend considering stronger ciphers
00101  *                 instead.
00102  */
00103 void mbedtls_des_free( mbedtls_des_context *ctx );
00104 
00105 /**
00106  * \brief          Initialize Triple-DES context
00107  *
00108  * \param ctx      DES3 context to be initialized
00109  */
00110 void mbedtls_des3_init( mbedtls_des3_context *ctx );
00111 
00112 /**
00113  * \brief          Clear Triple-DES context
00114  *
00115  * \param ctx      DES3 context to be cleared
00116  */
00117 void mbedtls_des3_free( mbedtls_des3_context *ctx );
00118 
00119 /**
00120  * \brief          Set key parity on the given key to odd.
00121  *
00122  *                 DES keys are 56 bits long, but each byte is padded with
00123  *                 a parity bit to allow verification.
00124  *
00125  * \param key      8-byte secret key
00126  *
00127  * \warning        DES is considered a weak cipher and its use constitutes a
00128  *                 security risk. We recommend considering stronger ciphers
00129  *                 instead.
00130  */
00131 void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
00132 
00133 /**
00134  * \brief          Check that key parity on the given key is odd.
00135  *
00136  *                 DES keys are 56 bits long, but each byte is padded with
00137  *                 a parity bit to allow verification.
00138  *
00139  * \param key      8-byte secret key
00140  *
00141  * \return         0 is parity was ok, 1 if parity was not correct.
00142  *
00143  * \warning        DES is considered a weak cipher and its use constitutes a
00144  *                 security risk. We recommend considering stronger ciphers
00145  *                 instead.
00146  */
00147 int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
00148 
00149 /**
00150  * \brief          Check that key is not a weak or semi-weak DES key
00151  *
00152  * \param key      8-byte secret key
00153  *
00154  * \return         0 if no weak key was found, 1 if a weak key was identified.
00155  *
00156  * \warning        DES is considered a weak cipher and its use constitutes a
00157  *                 security risk. We recommend considering stronger ciphers
00158  *                 instead.
00159  */
00160 int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
00161 
00162 /**
00163  * \brief          DES key schedule (56-bit, encryption)
00164  *
00165  * \param ctx      DES context to be initialized
00166  * \param key      8-byte secret key
00167  *
00168  * \return         0
00169  *
00170  * \warning        DES is considered a weak cipher and its use constitutes a
00171  *                 security risk. We recommend considering stronger ciphers
00172  *                 instead.
00173  */
00174 int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
00175 
00176 /**
00177  * \brief          DES key schedule (56-bit, decryption)
00178  *
00179  * \param ctx      DES context to be initialized
00180  * \param key      8-byte secret key
00181  *
00182  * \return         0
00183  *
00184  * \warning        DES is considered a weak cipher and its use constitutes a
00185  *                 security risk. We recommend considering stronger ciphers
00186  *                 instead.
00187  */
00188 int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
00189 
00190 /**
00191  * \brief          Triple-DES key schedule (112-bit, encryption)
00192  *
00193  * \param ctx      3DES context to be initialized
00194  * \param key      16-byte secret key
00195  *
00196  * \return         0
00197  */
00198 int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
00199                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
00200 
00201 /**
00202  * \brief          Triple-DES key schedule (112-bit, decryption)
00203  *
00204  * \param ctx      3DES context to be initialized
00205  * \param key      16-byte secret key
00206  *
00207  * \return         0
00208  */
00209 int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
00210                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
00211 
00212 /**
00213  * \brief          Triple-DES key schedule (168-bit, encryption)
00214  *
00215  * \param ctx      3DES context to be initialized
00216  * \param key      24-byte secret key
00217  *
00218  * \return         0
00219  */
00220 int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
00221                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
00222 
00223 /**
00224  * \brief          Triple-DES key schedule (168-bit, decryption)
00225  *
00226  * \param ctx      3DES context to be initialized
00227  * \param key      24-byte secret key
00228  *
00229  * \return         0
00230  */
00231 int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
00232                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
00233 
00234 /**
00235  * \brief          DES-ECB block encryption/decryption
00236  *
00237  * \param ctx      DES context
00238  * \param input    64-bit input block
00239  * \param output   64-bit output block
00240  *
00241  * \return         0 if successful
00242  *
00243  * \warning        DES is considered a weak cipher and its use constitutes a
00244  *                 security risk. We recommend considering stronger ciphers
00245  *                 instead.
00246  */
00247 int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
00248                     const unsigned char input[8],
00249                     unsigned char output[8] );
00250 
00251 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00252 /**
00253  * \brief          DES-CBC buffer encryption/decryption
00254  *
00255  * \note           Upon exit, the content of the IV is updated so that you can
00256  *                 call the function same function again on the following
00257  *                 block(s) of data and get the same result as if it was
00258  *                 encrypted in one call. This allows a "streaming" usage.
00259  *                 If on the other hand you need to retain the contents of the
00260  *                 IV, you should either save it manually or use the cipher
00261  *                 module instead.
00262  *
00263  * \param ctx      DES context
00264  * \param mode     MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
00265  * \param length   length of the input data
00266  * \param iv       initialization vector (updated after use)
00267  * \param input    buffer holding the input data
00268  * \param output   buffer holding the output data
00269  *
00270  * \warning        DES is considered a weak cipher and its use constitutes a
00271  *                 security risk. We recommend considering stronger ciphers
00272  *                 instead.
00273  */
00274 int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
00275                     int mode,
00276                     size_t length,
00277                     unsigned char iv[8],
00278                     const unsigned char *input,
00279                     unsigned char *output );
00280 #endif /* MBEDTLS_CIPHER_MODE_CBC */
00281 
00282 /**
00283  * \brief          3DES-ECB block encryption/decryption
00284  *
00285  * \param ctx      3DES context
00286  * \param input    64-bit input block
00287  * \param output   64-bit output block
00288  *
00289  * \return         0 if successful
00290  */
00291 int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
00292                      const unsigned char input[8],
00293                      unsigned char output[8] );
00294 
00295 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00296 /**
00297  * \brief          3DES-CBC buffer encryption/decryption
00298  *
00299  * \note           Upon exit, the content of the IV is updated so that you can
00300  *                 call the function same function again on the following
00301  *                 block(s) of data and get the same result as if it was
00302  *                 encrypted in one call. This allows a "streaming" usage.
00303  *                 If on the other hand you need to retain the contents of the
00304  *                 IV, you should either save it manually or use the cipher
00305  *                 module instead.
00306  *
00307  * \param ctx      3DES context
00308  * \param mode     MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
00309  * \param length   length of the input data
00310  * \param iv       initialization vector (updated after use)
00311  * \param input    buffer holding the input data
00312  * \param output   buffer holding the output data
00313  *
00314  * \return         0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
00315  */
00316 int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
00317                      int mode,
00318                      size_t length,
00319                      unsigned char iv[8],
00320                      const unsigned char *input,
00321                      unsigned char *output );
00322 #endif /* MBEDTLS_CIPHER_MODE_CBC */
00323 
00324 /**
00325  * \brief          Internal function for key expansion.
00326  *                 (Only exposed to allow overriding it,
00327  *                 see MBEDTLS_DES_SETKEY_ALT)
00328  *
00329  * \param SK       Round keys
00330  * \param key      Base key
00331  *
00332  * \warning        DES is considered a weak cipher and its use constitutes a
00333  *                 security risk. We recommend considering stronger ciphers
00334  *                 instead.
00335  */
00336 void mbedtls_des_setkey( uint32_t SK[32],
00337                          const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
00338 
00339 /**
00340  * \brief          Checkup routine
00341  *
00342  * \return         0 if successful, or 1 if the test failed
00343  */
00344 int mbedtls_des_self_test( int verbose );
00345 
00346 #ifdef __cplusplus
00347 }
00348 #endif
00349 
00350 #endif /* des.h */