Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
sPymbed
Date:
Tue Nov 19 14:32:16 2019 +0000
Revision:
16:048e5e270a58
Parent:
15:117db924cf7c
working ssl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* des3.h
wolfSSL 15:117db924cf7c 2 *
wolfSSL 15:117db924cf7c 3 * Copyright (C) 2006-2017 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22 /*!
wolfSSL 15:117db924cf7c 23 \file wolfssl/wolfcrypt/des3.h
wolfSSL 15:117db924cf7c 24 */
wolfSSL 15:117db924cf7c 25
wolfSSL 15:117db924cf7c 26 #ifndef WOLF_CRYPT_DES3_H
wolfSSL 15:117db924cf7c 27 #define WOLF_CRYPT_DES3_H
wolfSSL 15:117db924cf7c 28
wolfSSL 15:117db924cf7c 29 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 15:117db924cf7c 30
wolfSSL 15:117db924cf7c 31 #ifndef NO_DES3
wolfSSL 15:117db924cf7c 32
wolfSSL 15:117db924cf7c 33 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 34 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
wolfSSL 15:117db924cf7c 35 #include <wolfssl/wolfcrypt/fips.h>
wolfSSL 15:117db924cf7c 36 #endif /* HAVE_FIPS_VERSION >= 2 */
wolfSSL 15:117db924cf7c 37
wolfSSL 15:117db924cf7c 38 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 39 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
wolfSSL 15:117db924cf7c 40 /* included for fips @wc_fips */
wolfSSL 15:117db924cf7c 41 #include <cyassl/ctaocrypt/des3.h>
wolfSSL 15:117db924cf7c 42 #endif
wolfSSL 15:117db924cf7c 43
wolfSSL 15:117db924cf7c 44 #ifdef __cplusplus
wolfSSL 15:117db924cf7c 45 extern "C" {
wolfSSL 15:117db924cf7c 46 #endif
wolfSSL 15:117db924cf7c 47
wolfSSL 15:117db924cf7c 48 /* these are required for FIPS and non-FIPS */
wolfSSL 15:117db924cf7c 49 enum {
wolfSSL 15:117db924cf7c 50 DES_KEY_SIZE = 8, /* des */
wolfSSL 15:117db924cf7c 51 DES3_KEY_SIZE = 24, /* 3 des ede */
wolfSSL 15:117db924cf7c 52 DES_IV_SIZE = 16,
wolfSSL 15:117db924cf7c 53 };
wolfSSL 15:117db924cf7c 54
wolfSSL 15:117db924cf7c 55
wolfSSL 15:117db924cf7c 56 /* avoid redefinition of structs */
wolfSSL 15:117db924cf7c 57 #if !defined(HAVE_FIPS) || \
wolfSSL 15:117db924cf7c 58 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
wolfSSL 15:117db924cf7c 59
wolfSSL 15:117db924cf7c 60 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 61 #include <wolfssl/wolfcrypt/async.h>
wolfSSL 15:117db924cf7c 62 #endif
wolfSSL 15:117db924cf7c 63
wolfSSL 15:117db924cf7c 64 enum {
wolfSSL 15:117db924cf7c 65 DES_ENC_TYPE = WC_CIPHER_DES, /* cipher unique type */
wolfSSL 15:117db924cf7c 66 DES3_ENC_TYPE = WC_CIPHER_DES3, /* cipher unique type */
wolfSSL 15:117db924cf7c 67
wolfSSL 15:117db924cf7c 68 DES_BLOCK_SIZE = 8,
wolfSSL 15:117db924cf7c 69 DES_KS_SIZE = 32, /* internal DES key buffer size */
wolfSSL 15:117db924cf7c 70
wolfSSL 15:117db924cf7c 71 DES_ENCRYPTION = 0,
wolfSSL 15:117db924cf7c 72 DES_DECRYPTION = 1
wolfSSL 15:117db924cf7c 73 };
wolfSSL 15:117db924cf7c 74
wolfSSL 15:117db924cf7c 75 #define DES_IVLEN 8
wolfSSL 15:117db924cf7c 76 #define DES_KEYLEN 8
wolfSSL 15:117db924cf7c 77 #define DES3_IVLEN 8
wolfSSL 15:117db924cf7c 78 #define DES3_KEYLEN 24
wolfSSL 15:117db924cf7c 79
wolfSSL 15:117db924cf7c 80
wolfSSL 15:117db924cf7c 81 #if defined(STM32_CRYPTO)
wolfSSL 15:117db924cf7c 82 enum {
wolfSSL 15:117db924cf7c 83 DES_CBC = 0,
wolfSSL 15:117db924cf7c 84 DES_ECB = 1
wolfSSL 15:117db924cf7c 85 };
wolfSSL 15:117db924cf7c 86 #endif
wolfSSL 15:117db924cf7c 87
wolfSSL 15:117db924cf7c 88
wolfSSL 15:117db924cf7c 89 /* DES encryption and decryption */
wolfSSL 15:117db924cf7c 90 typedef struct Des {
wolfSSL 15:117db924cf7c 91 word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
wolfSSL 15:117db924cf7c 92 word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */
wolfSSL 15:117db924cf7c 93 word32 key[DES_KS_SIZE];
wolfSSL 15:117db924cf7c 94 } Des;
wolfSSL 15:117db924cf7c 95
wolfSSL 15:117db924cf7c 96
wolfSSL 15:117db924cf7c 97 /* DES3 encryption and decryption */
wolfSSL 15:117db924cf7c 98 typedef struct Des3 {
wolfSSL 15:117db924cf7c 99 word32 key[3][DES_KS_SIZE];
wolfSSL 15:117db924cf7c 100 word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
wolfSSL 15:117db924cf7c 101 word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */
wolfSSL 15:117db924cf7c 102 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 103 const byte* key_raw;
wolfSSL 15:117db924cf7c 104 const byte* iv_raw;
wolfSSL 15:117db924cf7c 105 WC_ASYNC_DEV asyncDev;
wolfSSL 15:117db924cf7c 106 #endif
wolfSSL 15:117db924cf7c 107 void* heap;
wolfSSL 15:117db924cf7c 108 } Des3;
wolfSSL 15:117db924cf7c 109 #endif /* HAVE_FIPS */
wolfSSL 15:117db924cf7c 110
wolfSSL 15:117db924cf7c 111
wolfSSL 15:117db924cf7c 112 WOLFSSL_API int wc_Des_SetKey(Des* des, const byte* key,
wolfSSL 15:117db924cf7c 113 const byte* iv, int dir);
wolfSSL 15:117db924cf7c 114 WOLFSSL_API void wc_Des_SetIV(Des* des, const byte* iv);
wolfSSL 15:117db924cf7c 115 WOLFSSL_API int wc_Des_CbcEncrypt(Des* des, byte* out,
wolfSSL 15:117db924cf7c 116 const byte* in, word32 sz);
wolfSSL 15:117db924cf7c 117 WOLFSSL_API int wc_Des_CbcDecrypt(Des* des, byte* out,
wolfSSL 15:117db924cf7c 118 const byte* in, word32 sz);
wolfSSL 15:117db924cf7c 119 WOLFSSL_API int wc_Des_EcbEncrypt(Des* des, byte* out,
wolfSSL 15:117db924cf7c 120 const byte* in, word32 sz);
wolfSSL 15:117db924cf7c 121 WOLFSSL_API int wc_Des3_EcbEncrypt(Des3* des, byte* out,
wolfSSL 15:117db924cf7c 122 const byte* in, word32 sz);
wolfSSL 15:117db924cf7c 123
wolfSSL 15:117db924cf7c 124 /* ECB decrypt same process as encrypt but with decrypt key */
wolfSSL 15:117db924cf7c 125 #define wc_Des_EcbDecrypt wc_Des_EcbEncrypt
wolfSSL 15:117db924cf7c 126 #define wc_Des3_EcbDecrypt wc_Des3_EcbEncrypt
wolfSSL 15:117db924cf7c 127
wolfSSL 15:117db924cf7c 128 WOLFSSL_API int wc_Des3_SetKey(Des3* des, const byte* key,
wolfSSL 15:117db924cf7c 129 const byte* iv,int dir);
wolfSSL 15:117db924cf7c 130 WOLFSSL_API int wc_Des3_SetIV(Des3* des, const byte* iv);
wolfSSL 15:117db924cf7c 131 WOLFSSL_API int wc_Des3_CbcEncrypt(Des3* des, byte* out,
wolfSSL 15:117db924cf7c 132 const byte* in,word32 sz);
wolfSSL 15:117db924cf7c 133 WOLFSSL_API int wc_Des3_CbcDecrypt(Des3* des, byte* out,
wolfSSL 15:117db924cf7c 134 const byte* in,word32 sz);
wolfSSL 15:117db924cf7c 135
wolfSSL 15:117db924cf7c 136 /* These are only required when using either:
wolfSSL 15:117db924cf7c 137 static memory (WOLFSSL_STATIC_MEMORY) or asynchronous (WOLFSSL_ASYNC_CRYPT) */
wolfSSL 15:117db924cf7c 138 WOLFSSL_API int wc_Des3Init(Des3*, void*, int);
wolfSSL 15:117db924cf7c 139 WOLFSSL_API void wc_Des3Free(Des3*);
wolfSSL 15:117db924cf7c 140
wolfSSL 15:117db924cf7c 141 #ifdef __cplusplus
wolfSSL 15:117db924cf7c 142 } /* extern "C" */
wolfSSL 15:117db924cf7c 143 #endif
wolfSSL 15:117db924cf7c 144
wolfSSL 15:117db924cf7c 145 #endif /* NO_DES3 */
wolfSSL 15:117db924cf7c 146 #endif /* WOLF_CRYPT_DES3_H */
wolfSSL 15:117db924cf7c 147
wolfSSL 15:117db924cf7c 148