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