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