Xuyi Wang / wolfcrypt

Dependents:   OS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers des3.h Source File

des3.h

00001 /* des3.h
00002  *
00003  * Copyright (C) 2006-2017 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 /*!
00023     \file wolfssl/wolfcrypt/des3.h
00024 */
00025 
00026 #ifndef WOLF_CRYPT_DES3_H
00027 #define WOLF_CRYPT_DES3_H
00028 
00029 #include <wolfcrypt/types.h>
00030 
00031 #ifndef NO_DES3
00032 
00033 #if defined(HAVE_FIPS) && \
00034     defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
00035     #include <wolfcrypt/fips.h>
00036 #endif /* HAVE_FIPS_VERSION >= 2 */
00037 
00038 #if defined(HAVE_FIPS) && \
00039     (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
00040 /* included for fips @wc_fips */
00041 #include <cyassl/ctaocrypt/des3.h>
00042 #endif
00043 
00044 #ifdef __cplusplus
00045     extern "C" {
00046 #endif
00047 
00048 /* these are required for FIPS and non-FIPS */
00049 enum {
00050     DES_KEY_SIZE        =  8,  /* des                     */
00051     DES3_KEY_SIZE       = 24,  /* 3 des ede               */
00052     DES_IV_SIZE         = 16,
00053 };
00054 
00055 
00056 /* avoid redefinition of structs */
00057 #if !defined(HAVE_FIPS) || \
00058     (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
00059 
00060 #ifdef WOLFSSL_ASYNC_CRYPT
00061     #include <wolfcrypt/async.h>
00062 #endif
00063 
00064 enum {
00065     DES_ENC_TYPE    = WC_CIPHER_DES,     /* cipher unique type */
00066     DES3_ENC_TYPE   = WC_CIPHER_DES3,    /* cipher unique type */
00067 
00068     DES_BLOCK_SIZE  = 8,
00069     DES_KS_SIZE     = 32,    /* internal DES key buffer size */
00070 
00071     DES_ENCRYPTION  = 0,
00072     DES_DECRYPTION  = 1
00073 };
00074 
00075 #define DES_IVLEN 8
00076 #define DES_KEYLEN 8
00077 #define DES3_IVLEN 8
00078 #define DES3_KEYLEN 24
00079 
00080 
00081 #if defined(STM32_CRYPTO)
00082 enum {
00083     DES_CBC = 0,
00084     DES_ECB = 1
00085 };
00086 #endif
00087 
00088 
00089 /* DES encryption and decryption */
00090 typedef struct Des {
00091     word32 reg[DES_BLOCK_SIZE / sizeof(word32)];      /* for CBC mode */
00092     word32 tmp[DES_BLOCK_SIZE / sizeof(word32)];      /* same         */
00093     word32 key[DES_KS_SIZE];
00094 } Des;
00095 
00096 
00097 /* DES3 encryption and decryption */
00098 typedef struct Des3 {
00099     word32 key[3][DES_KS_SIZE];
00100     word32 reg[DES_BLOCK_SIZE / sizeof(word32)];      /* for CBC mode */
00101     word32 tmp[DES_BLOCK_SIZE / sizeof(word32)];      /* same         */
00102 #ifdef WOLFSSL_ASYNC_CRYPT
00103     const byte* key_raw;
00104     const byte* iv_raw;
00105     WC_ASYNC_DEV asyncDev;
00106 #endif
00107     void* heap;
00108 } Des3;
00109 #endif /* HAVE_FIPS */
00110 
00111 
00112 WOLFSSL_API int  wc_Des_SetKey(Des* des, const byte* key,
00113                                const byte* iv, int dir);
00114 WOLFSSL_API void wc_Des_SetIV(Des* des, const byte* iv);
00115 WOLFSSL_API int  wc_Des_CbcEncrypt(Des* des, byte* out,
00116                                    const byte* in, word32 sz);
00117 WOLFSSL_API int  wc_Des_CbcDecrypt(Des* des, byte* out,
00118                                    const byte* in, word32 sz);
00119 WOLFSSL_API int  wc_Des_EcbEncrypt(Des* des, byte* out,
00120                                    const byte* in, word32 sz);
00121 WOLFSSL_API int wc_Des3_EcbEncrypt(Des3* des, byte* out,
00122                                    const byte* in, word32 sz);
00123 
00124 /* ECB decrypt same process as encrypt but with decrypt key */
00125 #define wc_Des_EcbDecrypt  wc_Des_EcbEncrypt
00126 #define wc_Des3_EcbDecrypt wc_Des3_EcbEncrypt
00127 
00128 WOLFSSL_API int  wc_Des3_SetKey(Des3* des, const byte* key,
00129                                 const byte* iv,int dir);
00130 WOLFSSL_API int  wc_Des3_SetIV(Des3* des, const byte* iv);
00131 WOLFSSL_API int  wc_Des3_CbcEncrypt(Des3* des, byte* out,
00132                                     const byte* in,word32 sz);
00133 WOLFSSL_API int  wc_Des3_CbcDecrypt(Des3* des, byte* out,
00134                                     const byte* in,word32 sz);
00135 
00136 /* These are only required when using either:
00137   static memory (WOLFSSL_STATIC_MEMORY) or asynchronous (WOLFSSL_ASYNC_CRYPT) */
00138 WOLFSSL_API int  wc_Des3Init(Des3*, void*, int);
00139 WOLFSSL_API void wc_Des3Free(Des3*);
00140 
00141 #ifdef __cplusplus
00142     } /* extern "C" */
00143 #endif
00144 
00145 #endif /* NO_DES3 */
00146 #endif /* WOLF_CRYPT_DES3_H */
00147 
00148