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

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

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

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