wolf SSL / wolfSSL-TLS13-Beta

Fork of wolfSSL by wolf SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wc_encrypt.c Source File

wc_encrypt.c

00001 /* wc_encrypt.c
00002  *
00003  * Copyright (C) 2006-2016 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 #ifdef HAVE_CONFIG_H
00024     #include <config.h>
00025 #endif
00026 
00027 #include <wolfssl/wolfcrypt/settings.h>
00028 #include <wolfssl/wolfcrypt/aes.h>
00029 #include <wolfssl/wolfcrypt/des3.h>
00030 #include <wolfssl/wolfcrypt/wc_encrypt.h>
00031 #include <wolfssl/wolfcrypt/error-crypt.h>
00032 
00033 
00034 #if !defined(NO_AES) && defined(HAVE_AES_CBC)
00035 #ifdef HAVE_AES_DECRYPT
00036 int wc_AesCbcDecryptWithKey(byte* out, const byte* in, word32 inSz,
00037                                   const byte* key, word32 keySz, const byte* iv)
00038 {
00039     int  ret = 0;
00040 #ifdef WOLFSSL_SMALL_STACK
00041     Aes* aes = NULL;
00042 #else
00043     Aes  aes[1];
00044 #endif
00045 
00046 #ifdef WOLFSSL_SMALL_STACK
00047     aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_TMP_BUFFER);
00048     if (aes == NULL)
00049         return MEMORY_E;
00050 #endif
00051 
00052     ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
00053     if (ret == 0)
00054         ret = wc_AesCbcDecrypt(aes, out, in, inSz);
00055 
00056 #ifdef WOLFSSL_SMALL_STACK
00057     XFREE(aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
00058 #endif
00059 
00060     return ret;
00061 }
00062 #endif /* HAVE_AES_DECRYPT */
00063 
00064 int wc_AesCbcEncryptWithKey(byte* out, const byte* in, word32 inSz,
00065                             const byte* key, word32 keySz, const byte* iv)
00066 {
00067     int  ret = 0;
00068 #ifdef WOLFSSL_SMALL_STACK
00069     Aes* aes = NULL;
00070 #else
00071     Aes  aes[1];
00072 #endif
00073 
00074 #ifdef WOLFSSL_SMALL_STACK
00075     aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_TMP_BUFFER);
00076     if (aes == NULL)
00077         return MEMORY_E;
00078 #endif
00079 
00080     ret = wc_AesSetKey(aes, key, keySz, iv, AES_ENCRYPTION);
00081     if (ret == 0)
00082         ret = wc_AesCbcEncrypt(aes, out, in, inSz);
00083 
00084 #ifdef WOLFSSL_SMALL_STACK
00085     XFREE(aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
00086 #endif
00087 
00088     return ret;
00089 }
00090 #endif /* !NO_AES && HAVE_AES_CBC */
00091 
00092 
00093 #ifndef NO_DES3
00094 int wc_Des_CbcEncryptWithKey(byte* out, const byte* in, word32 sz,
00095                              const byte* key, const byte* iv)
00096 {
00097     int ret  = 0;
00098 #ifdef WOLFSSL_SMALL_STACK
00099     Des* des = NULL;
00100 #else
00101     Des  des[1];
00102 #endif
00103 
00104 #ifdef WOLFSSL_SMALL_STACK
00105     des = (Des*)XMALLOC(sizeof(Des), NULL, DYNAMIC_TYPE_TMP_BUFFER);
00106     if (des == NULL)
00107         return MEMORY_E;
00108 #endif
00109 
00110     ret = wc_Des_SetKey(des, key, iv, DES_ENCRYPTION);
00111     if (ret == 0)
00112         ret = wc_Des_CbcEncrypt(des, out, in, sz);
00113 
00114 #ifdef WOLFSSL_SMALL_STACK
00115     XFREE(des, NULL, DYNAMIC_TYPE_TMP_BUFFER);
00116 #endif
00117 
00118     return ret;
00119 }
00120 
00121 int wc_Des_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
00122                              const byte* key, const byte* iv)
00123 {
00124     int ret  = 0;
00125 #ifdef WOLFSSL_SMALL_STACK
00126     Des* des = NULL;
00127 #else
00128     Des  des[1];
00129 #endif
00130 
00131 #ifdef WOLFSSL_SMALL_STACK
00132     des = (Des*)XMALLOC(sizeof(Des), NULL, DYNAMIC_TYPE_TMP_BUFFER);
00133     if (des == NULL)
00134         return MEMORY_E;
00135 #endif
00136 
00137     ret = wc_Des_SetKey(des, key, iv, DES_DECRYPTION);
00138     if (ret == 0)
00139         ret = wc_Des_CbcDecrypt(des, out, in, sz);
00140 
00141 #ifdef WOLFSSL_SMALL_STACK
00142     XFREE(des, NULL, DYNAMIC_TYPE_TMP_BUFFER);
00143 #endif
00144 
00145     return ret;
00146 }
00147 
00148 
00149 int wc_Des3_CbcEncryptWithKey(byte* out, const byte* in, word32 sz,
00150                               const byte* key, const byte* iv)
00151 {
00152     int ret    = 0;
00153 #ifdef WOLFSSL_SMALL_STACK
00154     Des3* des3 = NULL;
00155 #else
00156     Des3  des3[1];
00157 #endif
00158 
00159 #ifdef WOLFSSL_SMALL_STACK
00160     des3 = (Des3*)XMALLOC(sizeof(Des3), NULL, DYNAMIC_TYPE_TMP_BUFFER);
00161     if (des3 == NULL)
00162         return MEMORY_E;
00163 #endif
00164 
00165     ret = wc_Des3_SetKey(des3, key, iv, DES_ENCRYPTION);
00166     if (ret == 0)
00167         ret = wc_Des3_CbcEncrypt(des3, out, in, sz);
00168 
00169 #ifdef WOLFSSL_SMALL_STACK
00170     XFREE(des3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
00171 #endif
00172 
00173     return ret;
00174 }
00175 
00176 
00177 int wc_Des3_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
00178                               const byte* key, const byte* iv)
00179 {
00180     int ret    = 0;
00181 #ifdef WOLFSSL_SMALL_STACK
00182     Des3* des3 = NULL;
00183 #else
00184     Des3  des3[1];
00185 #endif
00186 
00187 #ifdef WOLFSSL_SMALL_STACK
00188     des3 = (Des3*)XMALLOC(sizeof(Des3), NULL, DYNAMIC_TYPE_TMP_BUFFER);
00189     if (des3 == NULL)
00190         return MEMORY_E;
00191 #endif
00192 
00193     ret = wc_Des3_SetKey(des3, key, iv, DES_DECRYPTION);
00194     if (ret == 0)
00195         ret = wc_Des3_CbcDecrypt(des3, out, in, sz);
00196 
00197 #ifdef WOLFSSL_SMALL_STACK
00198     XFREE(des3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
00199 #endif
00200 
00201     return ret;
00202 }
00203 
00204 #endif /* !NO_DES3 */
00205