wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 7:481bce714567 1 /* wc_encrypt.c
wolfSSL 7:481bce714567 2 *
wolfSSL 7:481bce714567 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 7:481bce714567 4 *
wolfSSL 7:481bce714567 5 * This file is part of wolfSSL.
wolfSSL 7:481bce714567 6 *
wolfSSL 7:481bce714567 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 7:481bce714567 8 * it under the terms of the GNU General Public License as published by
wolfSSL 7:481bce714567 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 7:481bce714567 10 * (at your option) any later version.
wolfSSL 7:481bce714567 11 *
wolfSSL 7:481bce714567 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 7:481bce714567 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 7:481bce714567 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 7:481bce714567 15 * GNU General Public License for more details.
wolfSSL 7:481bce714567 16 *
wolfSSL 7:481bce714567 17 * You should have received a copy of the GNU General Public License
wolfSSL 7:481bce714567 18 * along with this program; if not, write to the Free Software
wolfSSL 7:481bce714567 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 7:481bce714567 20 */
wolfSSL 7:481bce714567 21
wolfSSL 7:481bce714567 22
wolfSSL 7:481bce714567 23 #ifdef HAVE_CONFIG_H
wolfSSL 7:481bce714567 24 #include <config.h>
wolfSSL 7:481bce714567 25 #endif
wolfSSL 7:481bce714567 26
wolfSSL 7:481bce714567 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 7:481bce714567 28 #include <wolfssl/wolfcrypt/aes.h>
wolfSSL 7:481bce714567 29 #include <wolfssl/wolfcrypt/des3.h>
wolfSSL 7:481bce714567 30 #include <wolfssl/wolfcrypt/wc_encrypt.h>
wolfSSL 7:481bce714567 31 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 7:481bce714567 32
wolfSSL 7:481bce714567 33
wolfSSL 7:481bce714567 34 #if !defined(NO_AES) && defined(HAVE_AES_CBC)
wolfSSL 7:481bce714567 35 #ifdef HAVE_AES_DECRYPT
wolfSSL 7:481bce714567 36 int wc_AesCbcDecryptWithKey(byte* out, const byte* in, word32 inSz,
wolfSSL 7:481bce714567 37 const byte* key, word32 keySz, const byte* iv)
wolfSSL 7:481bce714567 38 {
wolfSSL 7:481bce714567 39 int ret = 0;
wolfSSL 7:481bce714567 40 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 41 Aes* aes = NULL;
wolfSSL 7:481bce714567 42 #else
wolfSSL 7:481bce714567 43 Aes aes[1];
wolfSSL 7:481bce714567 44 #endif
wolfSSL 7:481bce714567 45
wolfSSL 7:481bce714567 46 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 47 aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 48 if (aes == NULL)
wolfSSL 7:481bce714567 49 return MEMORY_E;
wolfSSL 7:481bce714567 50 #endif
wolfSSL 7:481bce714567 51
wolfSSL 7:481bce714567 52 ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
wolfSSL 7:481bce714567 53 if (ret == 0)
wolfSSL 7:481bce714567 54 ret = wc_AesCbcDecrypt(aes, out, in, inSz);
wolfSSL 7:481bce714567 55
wolfSSL 7:481bce714567 56 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 57 XFREE(aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 58 #endif
wolfSSL 7:481bce714567 59
wolfSSL 7:481bce714567 60 return ret;
wolfSSL 7:481bce714567 61 }
wolfSSL 7:481bce714567 62 #endif /* HAVE_AES_DECRYPT */
wolfSSL 7:481bce714567 63
wolfSSL 7:481bce714567 64 int wc_AesCbcEncryptWithKey(byte* out, const byte* in, word32 inSz,
wolfSSL 7:481bce714567 65 const byte* key, word32 keySz, const byte* iv)
wolfSSL 7:481bce714567 66 {
wolfSSL 7:481bce714567 67 int ret = 0;
wolfSSL 7:481bce714567 68 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 69 Aes* aes = NULL;
wolfSSL 7:481bce714567 70 #else
wolfSSL 7:481bce714567 71 Aes aes[1];
wolfSSL 7:481bce714567 72 #endif
wolfSSL 7:481bce714567 73
wolfSSL 7:481bce714567 74 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 75 aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 76 if (aes == NULL)
wolfSSL 7:481bce714567 77 return MEMORY_E;
wolfSSL 7:481bce714567 78 #endif
wolfSSL 7:481bce714567 79
wolfSSL 7:481bce714567 80 ret = wc_AesSetKey(aes, key, keySz, iv, AES_ENCRYPTION);
wolfSSL 7:481bce714567 81 if (ret == 0)
wolfSSL 7:481bce714567 82 ret = wc_AesCbcEncrypt(aes, out, in, inSz);
wolfSSL 7:481bce714567 83
wolfSSL 7:481bce714567 84 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 85 XFREE(aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 86 #endif
wolfSSL 7:481bce714567 87
wolfSSL 7:481bce714567 88 return ret;
wolfSSL 7:481bce714567 89 }
wolfSSL 7:481bce714567 90 #endif /* !NO_AES && HAVE_AES_CBC */
wolfSSL 7:481bce714567 91
wolfSSL 7:481bce714567 92
wolfSSL 7:481bce714567 93 #ifndef NO_DES3
wolfSSL 7:481bce714567 94 int wc_Des_CbcEncryptWithKey(byte* out, const byte* in, word32 sz,
wolfSSL 7:481bce714567 95 const byte* key, const byte* iv)
wolfSSL 7:481bce714567 96 {
wolfSSL 7:481bce714567 97 int ret = 0;
wolfSSL 7:481bce714567 98 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 99 Des* des = NULL;
wolfSSL 7:481bce714567 100 #else
wolfSSL 7:481bce714567 101 Des des[1];
wolfSSL 7:481bce714567 102 #endif
wolfSSL 7:481bce714567 103
wolfSSL 7:481bce714567 104 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 105 des = (Des*)XMALLOC(sizeof(Des), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 106 if (des == NULL)
wolfSSL 7:481bce714567 107 return MEMORY_E;
wolfSSL 7:481bce714567 108 #endif
wolfSSL 7:481bce714567 109
wolfSSL 7:481bce714567 110 ret = wc_Des_SetKey(des, key, iv, DES_ENCRYPTION);
wolfSSL 7:481bce714567 111 if (ret == 0)
wolfSSL 7:481bce714567 112 ret = wc_Des_CbcEncrypt(des, out, in, sz);
wolfSSL 7:481bce714567 113
wolfSSL 7:481bce714567 114 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 115 XFREE(des, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 116 #endif
wolfSSL 7:481bce714567 117
wolfSSL 7:481bce714567 118 return ret;
wolfSSL 7:481bce714567 119 }
wolfSSL 7:481bce714567 120
wolfSSL 7:481bce714567 121 int wc_Des_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
wolfSSL 7:481bce714567 122 const byte* key, const byte* iv)
wolfSSL 7:481bce714567 123 {
wolfSSL 7:481bce714567 124 int ret = 0;
wolfSSL 7:481bce714567 125 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 126 Des* des = NULL;
wolfSSL 7:481bce714567 127 #else
wolfSSL 7:481bce714567 128 Des des[1];
wolfSSL 7:481bce714567 129 #endif
wolfSSL 7:481bce714567 130
wolfSSL 7:481bce714567 131 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 132 des = (Des*)XMALLOC(sizeof(Des), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 133 if (des == NULL)
wolfSSL 7:481bce714567 134 return MEMORY_E;
wolfSSL 7:481bce714567 135 #endif
wolfSSL 7:481bce714567 136
wolfSSL 7:481bce714567 137 ret = wc_Des_SetKey(des, key, iv, DES_DECRYPTION);
wolfSSL 7:481bce714567 138 if (ret == 0)
wolfSSL 7:481bce714567 139 ret = wc_Des_CbcDecrypt(des, out, in, sz);
wolfSSL 7:481bce714567 140
wolfSSL 7:481bce714567 141 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 142 XFREE(des, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 143 #endif
wolfSSL 7:481bce714567 144
wolfSSL 7:481bce714567 145 return ret;
wolfSSL 7:481bce714567 146 }
wolfSSL 7:481bce714567 147
wolfSSL 7:481bce714567 148
wolfSSL 7:481bce714567 149 int wc_Des3_CbcEncryptWithKey(byte* out, const byte* in, word32 sz,
wolfSSL 7:481bce714567 150 const byte* key, const byte* iv)
wolfSSL 7:481bce714567 151 {
wolfSSL 7:481bce714567 152 int ret = 0;
wolfSSL 7:481bce714567 153 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 154 Des3* des3 = NULL;
wolfSSL 7:481bce714567 155 #else
wolfSSL 7:481bce714567 156 Des3 des3[1];
wolfSSL 7:481bce714567 157 #endif
wolfSSL 7:481bce714567 158
wolfSSL 7:481bce714567 159 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 160 des3 = (Des3*)XMALLOC(sizeof(Des3), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 161 if (des3 == NULL)
wolfSSL 7:481bce714567 162 return MEMORY_E;
wolfSSL 7:481bce714567 163 #endif
wolfSSL 7:481bce714567 164
wolfSSL 7:481bce714567 165 ret = wc_Des3_SetKey(des3, key, iv, DES_ENCRYPTION);
wolfSSL 7:481bce714567 166 if (ret == 0)
wolfSSL 7:481bce714567 167 ret = wc_Des3_CbcEncrypt(des3, out, in, sz);
wolfSSL 7:481bce714567 168
wolfSSL 7:481bce714567 169 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 170 XFREE(des3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 171 #endif
wolfSSL 7:481bce714567 172
wolfSSL 7:481bce714567 173 return ret;
wolfSSL 7:481bce714567 174 }
wolfSSL 7:481bce714567 175
wolfSSL 7:481bce714567 176
wolfSSL 7:481bce714567 177 int wc_Des3_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
wolfSSL 7:481bce714567 178 const byte* key, const byte* iv)
wolfSSL 7:481bce714567 179 {
wolfSSL 7:481bce714567 180 int ret = 0;
wolfSSL 7:481bce714567 181 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 182 Des3* des3 = NULL;
wolfSSL 7:481bce714567 183 #else
wolfSSL 7:481bce714567 184 Des3 des3[1];
wolfSSL 7:481bce714567 185 #endif
wolfSSL 7:481bce714567 186
wolfSSL 7:481bce714567 187 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 188 des3 = (Des3*)XMALLOC(sizeof(Des3), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 189 if (des3 == NULL)
wolfSSL 7:481bce714567 190 return MEMORY_E;
wolfSSL 7:481bce714567 191 #endif
wolfSSL 7:481bce714567 192
wolfSSL 7:481bce714567 193 ret = wc_Des3_SetKey(des3, key, iv, DES_DECRYPTION);
wolfSSL 7:481bce714567 194 if (ret == 0)
wolfSSL 7:481bce714567 195 ret = wc_Des3_CbcDecrypt(des3, out, in, sz);
wolfSSL 7:481bce714567 196
wolfSSL 7:481bce714567 197 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 198 XFREE(des3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 199 #endif
wolfSSL 7:481bce714567 200
wolfSSL 7:481bce714567 201 return ret;
wolfSSL 7:481bce714567 202 }
wolfSSL 7:481bce714567 203
wolfSSL 7:481bce714567 204 #endif /* !NO_DES3 */
wolfSSL 7:481bce714567 205