source: UsbWattMeter/trunk/wolfssl-3.7.0/wolfcrypt/src/wc_encrypt.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 7 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

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