diff --git a/.gitignore b/.gitignore index 959e35261..29be92dd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +ctaocrypt/src/src/ *.swp *.lo *.la @@ -36,6 +37,8 @@ fips_test.c fips ctaocrypt/benchmark/benchmark ctaocrypt/test/testctaocrypt +wolfcrypt/benchmark/benchmark +wolfcrypt/test/testwolfcrypt examples/client/client examples/echoclient/echoclient examples/echoserver/echoserver diff --git a/Makefile.am b/Makefile.am index 86d49e5c1..e1be92556 100644 --- a/Makefile.am +++ b/Makefile.am @@ -60,8 +60,12 @@ EXTRA_DIST+= README.md EXTRA_DIST+= LICENSING #-------------------------------------# -include wolfssl/include.am +if BUILD_FIPS include cyassl/include.am +else +include wolfssl/include.am +endif + #-------------------------------------# include certs/include.am include certs/1024/include.am @@ -72,14 +76,23 @@ include swig/include.am include src/include.am include support/include.am #-------------------------------------# -include wolfcrypt/benchmark/include.am +if BUILD_FIPS include ctaocrypt/benchmark/include.am +else +include wolfcrypt/benchmark/include.am +endif #-------------------------------------# -include wolfcrypt/src/include.am +if BUILD_FIPS include ctaocrypt/src/include.am +else +include wolfcrypt/src/include.am +endif #-------------------------------------# -include wolfcrypt/test/include.am +if BUILD_FIPS include ctaocrypt/test/include.am +else +include wolfcrypt/test/include.am +endif #-------------------------------------# include examples/client/include.am include examples/server/include.am diff --git a/configure.ac b/configure.ac index 192b26adb..68461b6a5 100644 --- a/configure.ac +++ b/configure.ac @@ -1472,7 +1472,7 @@ AC_ARG_ENABLE([smallstack], if test "x$ENABLED_SMALL_STACK" = "xyes" then - AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_SMALL_STACK" + AM_CFLAGS="$AM_CFLAGS -DCYASSL_SMALL_STACK" fi diff --git a/ctaocrypt/src/arc4.c b/ctaocrypt/src/arc4.c new file mode 100644 index 000000000..9fd276b49 --- /dev/null +++ b/ctaocrypt/src/arc4.c @@ -0,0 +1,179 @@ +/* arc4.c + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#ifndef NO_RC4 + +#include + + +#ifdef HAVE_CAVIUM + static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length); + static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in, + word32 length); +#endif + + +void Arc4SetKey(Arc4* arc4, const byte* key, word32 length) +{ + word32 i; + word32 keyIndex = 0, stateIndex = 0; + +#ifdef HAVE_CAVIUM + if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC) + return Arc4CaviumSetKey(arc4, key, length); +#endif + + arc4->x = 1; + arc4->y = 0; + + for (i = 0; i < ARC4_STATE_SIZE; i++) + arc4->state[i] = (byte)i; + + for (i = 0; i < ARC4_STATE_SIZE; i++) { + word32 a = arc4->state[i]; + stateIndex += key[keyIndex] + a; + stateIndex &= 0xFF; + arc4->state[i] = arc4->state[stateIndex]; + arc4->state[stateIndex] = (byte)a; + + if (++keyIndex >= length) + keyIndex = 0; + } +} + + +static INLINE byte MakeByte(word32* x, word32* y, byte* s) +{ + word32 a = s[*x], b; + *y = (*y+a) & 0xff; + + b = s[*y]; + s[*x] = (byte)b; + s[*y] = (byte)a; + *x = (*x+1) & 0xff; + + return s[(a+b) & 0xff]; +} + + +void Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length) +{ + word32 x; + word32 y; + +#ifdef HAVE_CAVIUM + if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC) + return Arc4CaviumProcess(arc4, out, in, length); +#endif + + x = arc4->x; + y = arc4->y; + + while(length--) + *out++ = *in++ ^ MakeByte(&x, &y, arc4->state); + + arc4->x = (byte)x; + arc4->y = (byte)y; +} + + +#ifdef HAVE_CAVIUM + +#include +#include "cavium_common.h" + +/* Initiliaze Arc4 for use with Nitrox device */ +int Arc4InitCavium(Arc4* arc4, int devId) +{ + if (arc4 == NULL) + return -1; + + if (CspAllocContext(CONTEXT_SSL, &arc4->contextHandle, devId) != 0) + return -1; + + arc4->devId = devId; + arc4->magic = CYASSL_ARC4_CAVIUM_MAGIC; + + return 0; +} + + +/* Free Arc4 from use with Nitrox device */ +void Arc4FreeCavium(Arc4* arc4) +{ + if (arc4 == NULL) + return; + + if (arc4->magic != CYASSL_ARC4_CAVIUM_MAGIC) + return; + + CspFreeContext(CONTEXT_SSL, arc4->contextHandle, arc4->devId); + arc4->magic = 0; +} + + +static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length) +{ + word32 requestId; + + if (CspInitializeRc4(CAVIUM_BLOCKING, arc4->contextHandle, length, + (byte*)key, &requestId, arc4->devId) != 0) { + CYASSL_MSG("Bad Cavium Arc4 Init"); + } +} + + +static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in, + word32 length) +{ + cyassl_word offset = 0; + word32 requestId; + + while (length > CYASSL_MAX_16BIT) { + word16 slen = (word16)CYASSL_MAX_16BIT; + if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_UPDATE, + slen, (byte*)in + offset, out + offset, &requestId, + arc4->devId) != 0) { + CYASSL_MSG("Bad Cavium Arc4 Encrypt"); + } + length -= CYASSL_MAX_16BIT; + offset += CYASSL_MAX_16BIT; + } + if (length) { + word16 slen = (word16)length; + if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_UPDATE, + slen, (byte*)in + offset, out + offset, &requestId, + arc4->devId) != 0) { + CYASSL_MSG("Bad Cavium Arc4 Encrypt"); + } + } +} + +#endif /* HAVE_CAVIUM */ + +#endif /* NO_ARC4 */ + diff --git a/ctaocrypt/src/camellia.c b/ctaocrypt/src/camellia.c new file mode 100644 index 000000000..eaed4c90b --- /dev/null +++ b/ctaocrypt/src/camellia.c @@ -0,0 +1,1621 @@ +/* camellia.c ver 1.2.0 + * + * Copyright (c) 2006,2007 + * NTT (Nippon Telegraph and Telephone Corporation) . All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer as + * the first lines of this file unmodified. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NTT ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL NTT BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* camellia.c + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/* + * Algorithm Specification + * http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications.html + */ + + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#ifdef HAVE_CAMELLIA + +#include +#include +#include +#ifdef NO_INLINE + #include +#else + #include +#endif + + +/* u32 must be 32bit word */ +typedef unsigned int u32; +typedef unsigned char u8; + +/* key constants */ + +#define CAMELLIA_SIGMA1L ((u32)0xA09E667FL) +#define CAMELLIA_SIGMA1R ((u32)0x3BCC908BL) +#define CAMELLIA_SIGMA2L ((u32)0xB67AE858L) +#define CAMELLIA_SIGMA2R ((u32)0x4CAA73B2L) +#define CAMELLIA_SIGMA3L ((u32)0xC6EF372FL) +#define CAMELLIA_SIGMA3R ((u32)0xE94F82BEL) +#define CAMELLIA_SIGMA4L ((u32)0x54FF53A5L) +#define CAMELLIA_SIGMA4R ((u32)0xF1D36F1CL) +#define CAMELLIA_SIGMA5L ((u32)0x10E527FAL) +#define CAMELLIA_SIGMA5R ((u32)0xDE682D1DL) +#define CAMELLIA_SIGMA6L ((u32)0xB05688C2L) +#define CAMELLIA_SIGMA6R ((u32)0xB3E6C1FDL) + +/* + * macros + */ + + +#if defined(_MSC_VER) + +# define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) +# define GETU32(p) SWAP(*((u32 *)(p))) +# define PUTU32(ct, st) {*((u32 *)(ct)) = SWAP((st));} + +#else /* not MS-VC */ + +# define GETU32(pt) \ + (((u32)(pt)[0] << 24) \ + ^ ((u32)(pt)[1] << 16) \ + ^ ((u32)(pt)[2] << 8) \ + ^ ((u32)(pt)[3])) + +# define PUTU32(ct, st) { \ + (ct)[0] = (u8)((st) >> 24); \ + (ct)[1] = (u8)((st) >> 16); \ + (ct)[2] = (u8)((st) >> 8); \ + (ct)[3] = (u8)(st); } + +#endif + +#define CamelliaSubkeyL(INDEX) (subkey[(INDEX)*2]) +#define CamelliaSubkeyR(INDEX) (subkey[(INDEX)*2 + 1]) + +/* rotation right shift 1byte */ +#define CAMELLIA_RR8(x) (((x) >> 8) + ((x) << 24)) +/* rotation left shift 1bit */ +#define CAMELLIA_RL1(x) (((x) << 1) + ((x) >> 31)) +/* rotation left shift 1byte */ +#define CAMELLIA_RL8(x) (((x) << 8) + ((x) >> 24)) + +#define CAMELLIA_ROLDQ(ll, lr, rl, rr, w0, w1, bits) \ + do { \ + w0 = ll; \ + ll = (ll << bits) + (lr >> (32 - bits)); \ + lr = (lr << bits) + (rl >> (32 - bits)); \ + rl = (rl << bits) + (rr >> (32 - bits)); \ + rr = (rr << bits) + (w0 >> (32 - bits)); \ + } while(0) + +#define CAMELLIA_ROLDQo32(ll, lr, rl, rr, w0, w1, bits) \ + do { \ + w0 = ll; \ + w1 = lr; \ + ll = (lr << (bits - 32)) + (rl >> (64 - bits)); \ + lr = (rl << (bits - 32)) + (rr >> (64 - bits)); \ + rl = (rr << (bits - 32)) + (w0 >> (64 - bits)); \ + rr = (w0 << (bits - 32)) + (w1 >> (64 - bits)); \ + } while(0) + +#define CAMELLIA_SP1110(INDEX) (camellia_sp1110[(INDEX)]) +#define CAMELLIA_SP0222(INDEX) (camellia_sp0222[(INDEX)]) +#define CAMELLIA_SP3033(INDEX) (camellia_sp3033[(INDEX)]) +#define CAMELLIA_SP4404(INDEX) (camellia_sp4404[(INDEX)]) + +#define CAMELLIA_F(xl, xr, kl, kr, yl, yr, il, ir, t0, t1) \ + do { \ + il = xl ^ kl; \ + ir = xr ^ kr; \ + t0 = il >> 16; \ + t1 = ir >> 16; \ + yl = CAMELLIA_SP1110(ir & 0xff) \ + ^ CAMELLIA_SP0222((t1 >> 8) & 0xff) \ + ^ CAMELLIA_SP3033(t1 & 0xff) \ + ^ CAMELLIA_SP4404((ir >> 8) & 0xff); \ + yr = CAMELLIA_SP1110((t0 >> 8) & 0xff) \ + ^ CAMELLIA_SP0222(t0 & 0xff) \ + ^ CAMELLIA_SP3033((il >> 8) & 0xff) \ + ^ CAMELLIA_SP4404(il & 0xff); \ + yl ^= yr; \ + yr = CAMELLIA_RR8(yr); \ + yr ^= yl; \ + } while(0) + + +/* + * for speed up + * + */ +#define CAMELLIA_FLS(ll, lr, rl, rr, kll, klr, krl, krr, t0, t1, t2, t3) \ + do { \ + t0 = kll; \ + t0 &= ll; \ + lr ^= CAMELLIA_RL1(t0); \ + t1 = klr; \ + t1 |= lr; \ + ll ^= t1; \ + \ + t2 = krr; \ + t2 |= rr; \ + rl ^= t2; \ + t3 = krl; \ + t3 &= rl; \ + rr ^= CAMELLIA_RL1(t3); \ + } while(0) + +#define CAMELLIA_ROUNDSM(xl, xr, kl, kr, yl, yr, il, ir, t0, t1) \ + do { \ + ir = CAMELLIA_SP1110(xr & 0xff) \ + ^ CAMELLIA_SP0222((xr >> 24) & 0xff) \ + ^ CAMELLIA_SP3033((xr >> 16) & 0xff) \ + ^ CAMELLIA_SP4404((xr >> 8) & 0xff); \ + il = CAMELLIA_SP1110((xl >> 24) & 0xff) \ + ^ CAMELLIA_SP0222((xl >> 16) & 0xff) \ + ^ CAMELLIA_SP3033((xl >> 8) & 0xff) \ + ^ CAMELLIA_SP4404(xl & 0xff); \ + il ^= kl; \ + ir ^= kr; \ + ir ^= il; \ + il = CAMELLIA_RR8(il); \ + il ^= ir; \ + yl ^= ir; \ + yr ^= il; \ + } while(0) + + +static const u32 camellia_sp1110[256] = { + 0x70707000,0x82828200,0x2c2c2c00,0xececec00, + 0xb3b3b300,0x27272700,0xc0c0c000,0xe5e5e500, + 0xe4e4e400,0x85858500,0x57575700,0x35353500, + 0xeaeaea00,0x0c0c0c00,0xaeaeae00,0x41414100, + 0x23232300,0xefefef00,0x6b6b6b00,0x93939300, + 0x45454500,0x19191900,0xa5a5a500,0x21212100, + 0xededed00,0x0e0e0e00,0x4f4f4f00,0x4e4e4e00, + 0x1d1d1d00,0x65656500,0x92929200,0xbdbdbd00, + 0x86868600,0xb8b8b800,0xafafaf00,0x8f8f8f00, + 0x7c7c7c00,0xebebeb00,0x1f1f1f00,0xcecece00, + 0x3e3e3e00,0x30303000,0xdcdcdc00,0x5f5f5f00, + 0x5e5e5e00,0xc5c5c500,0x0b0b0b00,0x1a1a1a00, + 0xa6a6a600,0xe1e1e100,0x39393900,0xcacaca00, + 0xd5d5d500,0x47474700,0x5d5d5d00,0x3d3d3d00, + 0xd9d9d900,0x01010100,0x5a5a5a00,0xd6d6d600, + 0x51515100,0x56565600,0x6c6c6c00,0x4d4d4d00, + 0x8b8b8b00,0x0d0d0d00,0x9a9a9a00,0x66666600, + 0xfbfbfb00,0xcccccc00,0xb0b0b000,0x2d2d2d00, + 0x74747400,0x12121200,0x2b2b2b00,0x20202000, + 0xf0f0f000,0xb1b1b100,0x84848400,0x99999900, + 0xdfdfdf00,0x4c4c4c00,0xcbcbcb00,0xc2c2c200, + 0x34343400,0x7e7e7e00,0x76767600,0x05050500, + 0x6d6d6d00,0xb7b7b700,0xa9a9a900,0x31313100, + 0xd1d1d100,0x17171700,0x04040400,0xd7d7d700, + 0x14141400,0x58585800,0x3a3a3a00,0x61616100, + 0xdedede00,0x1b1b1b00,0x11111100,0x1c1c1c00, + 0x32323200,0x0f0f0f00,0x9c9c9c00,0x16161600, + 0x53535300,0x18181800,0xf2f2f200,0x22222200, + 0xfefefe00,0x44444400,0xcfcfcf00,0xb2b2b200, + 0xc3c3c300,0xb5b5b500,0x7a7a7a00,0x91919100, + 0x24242400,0x08080800,0xe8e8e800,0xa8a8a800, + 0x60606000,0xfcfcfc00,0x69696900,0x50505000, + 0xaaaaaa00,0xd0d0d000,0xa0a0a000,0x7d7d7d00, + 0xa1a1a100,0x89898900,0x62626200,0x97979700, + 0x54545400,0x5b5b5b00,0x1e1e1e00,0x95959500, + 0xe0e0e000,0xffffff00,0x64646400,0xd2d2d200, + 0x10101000,0xc4c4c400,0x00000000,0x48484800, + 0xa3a3a300,0xf7f7f700,0x75757500,0xdbdbdb00, + 0x8a8a8a00,0x03030300,0xe6e6e600,0xdadada00, + 0x09090900,0x3f3f3f00,0xdddddd00,0x94949400, + 0x87878700,0x5c5c5c00,0x83838300,0x02020200, + 0xcdcdcd00,0x4a4a4a00,0x90909000,0x33333300, + 0x73737300,0x67676700,0xf6f6f600,0xf3f3f300, + 0x9d9d9d00,0x7f7f7f00,0xbfbfbf00,0xe2e2e200, + 0x52525200,0x9b9b9b00,0xd8d8d800,0x26262600, + 0xc8c8c800,0x37373700,0xc6c6c600,0x3b3b3b00, + 0x81818100,0x96969600,0x6f6f6f00,0x4b4b4b00, + 0x13131300,0xbebebe00,0x63636300,0x2e2e2e00, + 0xe9e9e900,0x79797900,0xa7a7a700,0x8c8c8c00, + 0x9f9f9f00,0x6e6e6e00,0xbcbcbc00,0x8e8e8e00, + 0x29292900,0xf5f5f500,0xf9f9f900,0xb6b6b600, + 0x2f2f2f00,0xfdfdfd00,0xb4b4b400,0x59595900, + 0x78787800,0x98989800,0x06060600,0x6a6a6a00, + 0xe7e7e700,0x46464600,0x71717100,0xbababa00, + 0xd4d4d400,0x25252500,0xababab00,0x42424200, + 0x88888800,0xa2a2a200,0x8d8d8d00,0xfafafa00, + 0x72727200,0x07070700,0xb9b9b900,0x55555500, + 0xf8f8f800,0xeeeeee00,0xacacac00,0x0a0a0a00, + 0x36363600,0x49494900,0x2a2a2a00,0x68686800, + 0x3c3c3c00,0x38383800,0xf1f1f100,0xa4a4a400, + 0x40404000,0x28282800,0xd3d3d300,0x7b7b7b00, + 0xbbbbbb00,0xc9c9c900,0x43434300,0xc1c1c100, + 0x15151500,0xe3e3e300,0xadadad00,0xf4f4f400, + 0x77777700,0xc7c7c700,0x80808000,0x9e9e9e00, +}; + +static const u32 camellia_sp0222[256] = { + 0x00e0e0e0,0x00050505,0x00585858,0x00d9d9d9, + 0x00676767,0x004e4e4e,0x00818181,0x00cbcbcb, + 0x00c9c9c9,0x000b0b0b,0x00aeaeae,0x006a6a6a, + 0x00d5d5d5,0x00181818,0x005d5d5d,0x00828282, + 0x00464646,0x00dfdfdf,0x00d6d6d6,0x00272727, + 0x008a8a8a,0x00323232,0x004b4b4b,0x00424242, + 0x00dbdbdb,0x001c1c1c,0x009e9e9e,0x009c9c9c, + 0x003a3a3a,0x00cacaca,0x00252525,0x007b7b7b, + 0x000d0d0d,0x00717171,0x005f5f5f,0x001f1f1f, + 0x00f8f8f8,0x00d7d7d7,0x003e3e3e,0x009d9d9d, + 0x007c7c7c,0x00606060,0x00b9b9b9,0x00bebebe, + 0x00bcbcbc,0x008b8b8b,0x00161616,0x00343434, + 0x004d4d4d,0x00c3c3c3,0x00727272,0x00959595, + 0x00ababab,0x008e8e8e,0x00bababa,0x007a7a7a, + 0x00b3b3b3,0x00020202,0x00b4b4b4,0x00adadad, + 0x00a2a2a2,0x00acacac,0x00d8d8d8,0x009a9a9a, + 0x00171717,0x001a1a1a,0x00353535,0x00cccccc, + 0x00f7f7f7,0x00999999,0x00616161,0x005a5a5a, + 0x00e8e8e8,0x00242424,0x00565656,0x00404040, + 0x00e1e1e1,0x00636363,0x00090909,0x00333333, + 0x00bfbfbf,0x00989898,0x00979797,0x00858585, + 0x00686868,0x00fcfcfc,0x00ececec,0x000a0a0a, + 0x00dadada,0x006f6f6f,0x00535353,0x00626262, + 0x00a3a3a3,0x002e2e2e,0x00080808,0x00afafaf, + 0x00282828,0x00b0b0b0,0x00747474,0x00c2c2c2, + 0x00bdbdbd,0x00363636,0x00222222,0x00383838, + 0x00646464,0x001e1e1e,0x00393939,0x002c2c2c, + 0x00a6a6a6,0x00303030,0x00e5e5e5,0x00444444, + 0x00fdfdfd,0x00888888,0x009f9f9f,0x00656565, + 0x00878787,0x006b6b6b,0x00f4f4f4,0x00232323, + 0x00484848,0x00101010,0x00d1d1d1,0x00515151, + 0x00c0c0c0,0x00f9f9f9,0x00d2d2d2,0x00a0a0a0, + 0x00555555,0x00a1a1a1,0x00414141,0x00fafafa, + 0x00434343,0x00131313,0x00c4c4c4,0x002f2f2f, + 0x00a8a8a8,0x00b6b6b6,0x003c3c3c,0x002b2b2b, + 0x00c1c1c1,0x00ffffff,0x00c8c8c8,0x00a5a5a5, + 0x00202020,0x00898989,0x00000000,0x00909090, + 0x00474747,0x00efefef,0x00eaeaea,0x00b7b7b7, + 0x00151515,0x00060606,0x00cdcdcd,0x00b5b5b5, + 0x00121212,0x007e7e7e,0x00bbbbbb,0x00292929, + 0x000f0f0f,0x00b8b8b8,0x00070707,0x00040404, + 0x009b9b9b,0x00949494,0x00212121,0x00666666, + 0x00e6e6e6,0x00cecece,0x00ededed,0x00e7e7e7, + 0x003b3b3b,0x00fefefe,0x007f7f7f,0x00c5c5c5, + 0x00a4a4a4,0x00373737,0x00b1b1b1,0x004c4c4c, + 0x00919191,0x006e6e6e,0x008d8d8d,0x00767676, + 0x00030303,0x002d2d2d,0x00dedede,0x00969696, + 0x00262626,0x007d7d7d,0x00c6c6c6,0x005c5c5c, + 0x00d3d3d3,0x00f2f2f2,0x004f4f4f,0x00191919, + 0x003f3f3f,0x00dcdcdc,0x00797979,0x001d1d1d, + 0x00525252,0x00ebebeb,0x00f3f3f3,0x006d6d6d, + 0x005e5e5e,0x00fbfbfb,0x00696969,0x00b2b2b2, + 0x00f0f0f0,0x00313131,0x000c0c0c,0x00d4d4d4, + 0x00cfcfcf,0x008c8c8c,0x00e2e2e2,0x00757575, + 0x00a9a9a9,0x004a4a4a,0x00575757,0x00848484, + 0x00111111,0x00454545,0x001b1b1b,0x00f5f5f5, + 0x00e4e4e4,0x000e0e0e,0x00737373,0x00aaaaaa, + 0x00f1f1f1,0x00dddddd,0x00595959,0x00141414, + 0x006c6c6c,0x00929292,0x00545454,0x00d0d0d0, + 0x00787878,0x00707070,0x00e3e3e3,0x00494949, + 0x00808080,0x00505050,0x00a7a7a7,0x00f6f6f6, + 0x00777777,0x00939393,0x00868686,0x00838383, + 0x002a2a2a,0x00c7c7c7,0x005b5b5b,0x00e9e9e9, + 0x00eeeeee,0x008f8f8f,0x00010101,0x003d3d3d, +}; + +static const u32 camellia_sp3033[256] = { + 0x38003838,0x41004141,0x16001616,0x76007676, + 0xd900d9d9,0x93009393,0x60006060,0xf200f2f2, + 0x72007272,0xc200c2c2,0xab00abab,0x9a009a9a, + 0x75007575,0x06000606,0x57005757,0xa000a0a0, + 0x91009191,0xf700f7f7,0xb500b5b5,0xc900c9c9, + 0xa200a2a2,0x8c008c8c,0xd200d2d2,0x90009090, + 0xf600f6f6,0x07000707,0xa700a7a7,0x27002727, + 0x8e008e8e,0xb200b2b2,0x49004949,0xde00dede, + 0x43004343,0x5c005c5c,0xd700d7d7,0xc700c7c7, + 0x3e003e3e,0xf500f5f5,0x8f008f8f,0x67006767, + 0x1f001f1f,0x18001818,0x6e006e6e,0xaf00afaf, + 0x2f002f2f,0xe200e2e2,0x85008585,0x0d000d0d, + 0x53005353,0xf000f0f0,0x9c009c9c,0x65006565, + 0xea00eaea,0xa300a3a3,0xae00aeae,0x9e009e9e, + 0xec00ecec,0x80008080,0x2d002d2d,0x6b006b6b, + 0xa800a8a8,0x2b002b2b,0x36003636,0xa600a6a6, + 0xc500c5c5,0x86008686,0x4d004d4d,0x33003333, + 0xfd00fdfd,0x66006666,0x58005858,0x96009696, + 0x3a003a3a,0x09000909,0x95009595,0x10001010, + 0x78007878,0xd800d8d8,0x42004242,0xcc00cccc, + 0xef00efef,0x26002626,0xe500e5e5,0x61006161, + 0x1a001a1a,0x3f003f3f,0x3b003b3b,0x82008282, + 0xb600b6b6,0xdb00dbdb,0xd400d4d4,0x98009898, + 0xe800e8e8,0x8b008b8b,0x02000202,0xeb00ebeb, + 0x0a000a0a,0x2c002c2c,0x1d001d1d,0xb000b0b0, + 0x6f006f6f,0x8d008d8d,0x88008888,0x0e000e0e, + 0x19001919,0x87008787,0x4e004e4e,0x0b000b0b, + 0xa900a9a9,0x0c000c0c,0x79007979,0x11001111, + 0x7f007f7f,0x22002222,0xe700e7e7,0x59005959, + 0xe100e1e1,0xda00dada,0x3d003d3d,0xc800c8c8, + 0x12001212,0x04000404,0x74007474,0x54005454, + 0x30003030,0x7e007e7e,0xb400b4b4,0x28002828, + 0x55005555,0x68006868,0x50005050,0xbe00bebe, + 0xd000d0d0,0xc400c4c4,0x31003131,0xcb00cbcb, + 0x2a002a2a,0xad00adad,0x0f000f0f,0xca00caca, + 0x70007070,0xff00ffff,0x32003232,0x69006969, + 0x08000808,0x62006262,0x00000000,0x24002424, + 0xd100d1d1,0xfb00fbfb,0xba00baba,0xed00eded, + 0x45004545,0x81008181,0x73007373,0x6d006d6d, + 0x84008484,0x9f009f9f,0xee00eeee,0x4a004a4a, + 0xc300c3c3,0x2e002e2e,0xc100c1c1,0x01000101, + 0xe600e6e6,0x25002525,0x48004848,0x99009999, + 0xb900b9b9,0xb300b3b3,0x7b007b7b,0xf900f9f9, + 0xce00cece,0xbf00bfbf,0xdf00dfdf,0x71007171, + 0x29002929,0xcd00cdcd,0x6c006c6c,0x13001313, + 0x64006464,0x9b009b9b,0x63006363,0x9d009d9d, + 0xc000c0c0,0x4b004b4b,0xb700b7b7,0xa500a5a5, + 0x89008989,0x5f005f5f,0xb100b1b1,0x17001717, + 0xf400f4f4,0xbc00bcbc,0xd300d3d3,0x46004646, + 0xcf00cfcf,0x37003737,0x5e005e5e,0x47004747, + 0x94009494,0xfa00fafa,0xfc00fcfc,0x5b005b5b, + 0x97009797,0xfe00fefe,0x5a005a5a,0xac00acac, + 0x3c003c3c,0x4c004c4c,0x03000303,0x35003535, + 0xf300f3f3,0x23002323,0xb800b8b8,0x5d005d5d, + 0x6a006a6a,0x92009292,0xd500d5d5,0x21002121, + 0x44004444,0x51005151,0xc600c6c6,0x7d007d7d, + 0x39003939,0x83008383,0xdc00dcdc,0xaa00aaaa, + 0x7c007c7c,0x77007777,0x56005656,0x05000505, + 0x1b001b1b,0xa400a4a4,0x15001515,0x34003434, + 0x1e001e1e,0x1c001c1c,0xf800f8f8,0x52005252, + 0x20002020,0x14001414,0xe900e9e9,0xbd00bdbd, + 0xdd00dddd,0xe400e4e4,0xa100a1a1,0xe000e0e0, + 0x8a008a8a,0xf100f1f1,0xd600d6d6,0x7a007a7a, + 0xbb00bbbb,0xe300e3e3,0x40004040,0x4f004f4f, +}; + +static const u32 camellia_sp4404[256] = { + 0x70700070,0x2c2c002c,0xb3b300b3,0xc0c000c0, + 0xe4e400e4,0x57570057,0xeaea00ea,0xaeae00ae, + 0x23230023,0x6b6b006b,0x45450045,0xa5a500a5, + 0xeded00ed,0x4f4f004f,0x1d1d001d,0x92920092, + 0x86860086,0xafaf00af,0x7c7c007c,0x1f1f001f, + 0x3e3e003e,0xdcdc00dc,0x5e5e005e,0x0b0b000b, + 0xa6a600a6,0x39390039,0xd5d500d5,0x5d5d005d, + 0xd9d900d9,0x5a5a005a,0x51510051,0x6c6c006c, + 0x8b8b008b,0x9a9a009a,0xfbfb00fb,0xb0b000b0, + 0x74740074,0x2b2b002b,0xf0f000f0,0x84840084, + 0xdfdf00df,0xcbcb00cb,0x34340034,0x76760076, + 0x6d6d006d,0xa9a900a9,0xd1d100d1,0x04040004, + 0x14140014,0x3a3a003a,0xdede00de,0x11110011, + 0x32320032,0x9c9c009c,0x53530053,0xf2f200f2, + 0xfefe00fe,0xcfcf00cf,0xc3c300c3,0x7a7a007a, + 0x24240024,0xe8e800e8,0x60600060,0x69690069, + 0xaaaa00aa,0xa0a000a0,0xa1a100a1,0x62620062, + 0x54540054,0x1e1e001e,0xe0e000e0,0x64640064, + 0x10100010,0x00000000,0xa3a300a3,0x75750075, + 0x8a8a008a,0xe6e600e6,0x09090009,0xdddd00dd, + 0x87870087,0x83830083,0xcdcd00cd,0x90900090, + 0x73730073,0xf6f600f6,0x9d9d009d,0xbfbf00bf, + 0x52520052,0xd8d800d8,0xc8c800c8,0xc6c600c6, + 0x81810081,0x6f6f006f,0x13130013,0x63630063, + 0xe9e900e9,0xa7a700a7,0x9f9f009f,0xbcbc00bc, + 0x29290029,0xf9f900f9,0x2f2f002f,0xb4b400b4, + 0x78780078,0x06060006,0xe7e700e7,0x71710071, + 0xd4d400d4,0xabab00ab,0x88880088,0x8d8d008d, + 0x72720072,0xb9b900b9,0xf8f800f8,0xacac00ac, + 0x36360036,0x2a2a002a,0x3c3c003c,0xf1f100f1, + 0x40400040,0xd3d300d3,0xbbbb00bb,0x43430043, + 0x15150015,0xadad00ad,0x77770077,0x80800080, + 0x82820082,0xecec00ec,0x27270027,0xe5e500e5, + 0x85850085,0x35350035,0x0c0c000c,0x41410041, + 0xefef00ef,0x93930093,0x19190019,0x21210021, + 0x0e0e000e,0x4e4e004e,0x65650065,0xbdbd00bd, + 0xb8b800b8,0x8f8f008f,0xebeb00eb,0xcece00ce, + 0x30300030,0x5f5f005f,0xc5c500c5,0x1a1a001a, + 0xe1e100e1,0xcaca00ca,0x47470047,0x3d3d003d, + 0x01010001,0xd6d600d6,0x56560056,0x4d4d004d, + 0x0d0d000d,0x66660066,0xcccc00cc,0x2d2d002d, + 0x12120012,0x20200020,0xb1b100b1,0x99990099, + 0x4c4c004c,0xc2c200c2,0x7e7e007e,0x05050005, + 0xb7b700b7,0x31310031,0x17170017,0xd7d700d7, + 0x58580058,0x61610061,0x1b1b001b,0x1c1c001c, + 0x0f0f000f,0x16160016,0x18180018,0x22220022, + 0x44440044,0xb2b200b2,0xb5b500b5,0x91910091, + 0x08080008,0xa8a800a8,0xfcfc00fc,0x50500050, + 0xd0d000d0,0x7d7d007d,0x89890089,0x97970097, + 0x5b5b005b,0x95950095,0xffff00ff,0xd2d200d2, + 0xc4c400c4,0x48480048,0xf7f700f7,0xdbdb00db, + 0x03030003,0xdada00da,0x3f3f003f,0x94940094, + 0x5c5c005c,0x02020002,0x4a4a004a,0x33330033, + 0x67670067,0xf3f300f3,0x7f7f007f,0xe2e200e2, + 0x9b9b009b,0x26260026,0x37370037,0x3b3b003b, + 0x96960096,0x4b4b004b,0xbebe00be,0x2e2e002e, + 0x79790079,0x8c8c008c,0x6e6e006e,0x8e8e008e, + 0xf5f500f5,0xb6b600b6,0xfdfd00fd,0x59590059, + 0x98980098,0x6a6a006a,0x46460046,0xbaba00ba, + 0x25250025,0x42420042,0xa2a200a2,0xfafa00fa, + 0x07070007,0x55550055,0xeeee00ee,0x0a0a000a, + 0x49490049,0x68680068,0x38380038,0xa4a400a4, + 0x28280028,0x7b7b007b,0xc9c900c9,0xc1c100c1, + 0xe3e300e3,0xf4f400f4,0xc7c700c7,0x9e9e009e, +}; + + +/** + * Stuff related to the Camellia key schedule + */ +#define subl(x) subL[(x)] +#define subr(x) subR[(x)] + +static int camellia_setup128(const unsigned char *key, u32 *subkey) +{ + u32 kll, klr, krl, krr; + u32 il, ir, t0, t1, w0, w1; + u32 kw4l, kw4r, dw, tl, tr; + +#ifdef CYASSL_SMALL_STACK + u32* subL; + u32* subR; + + subL = (u32*) XMALLOC(sizeof(u32) * 26, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (subL == NULL) + return MEMORY_E; + + subR = (u32*) XMALLOC(sizeof(u32) * 26, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (subR == NULL) { + XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER); + return MEMORY_E; + } +#else + u32 subL[26]; + u32 subR[26]; +#endif + + /** + * k == kll || klr || krl || krr (|| is concatination) + */ + kll = GETU32(key ); + klr = GETU32(key + 4); + krl = GETU32(key + 8); + krr = GETU32(key + 12); + /** + * generate KL dependent subkeys + */ + subl(0) = kll; subr(0) = klr; + subl(1) = krl; subr(1) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); + subl(4) = kll; subr(4) = klr; + subl(5) = krl; subr(5) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 30); + subl(10) = kll; subr(10) = klr; + subl(11) = krl; subr(11) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); + subl(13) = krl; subr(13) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17); + subl(16) = kll; subr(16) = klr; + subl(17) = krl; subr(17) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17); + subl(18) = kll; subr(18) = klr; + subl(19) = krl; subr(19) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17); + subl(22) = kll; subr(22) = klr; + subl(23) = krl; subr(23) = krr; + + /* generate KA */ + kll = subl(0); klr = subr(0); + krl = subl(1); krr = subr(1); + CAMELLIA_F(kll, klr, + CAMELLIA_SIGMA1L, CAMELLIA_SIGMA1R, + w0, w1, il, ir, t0, t1); + krl ^= w0; krr ^= w1; + CAMELLIA_F(krl, krr, + CAMELLIA_SIGMA2L, CAMELLIA_SIGMA2R, + kll, klr, il, ir, t0, t1); + CAMELLIA_F(kll, klr, + CAMELLIA_SIGMA3L, CAMELLIA_SIGMA3R, + krl, krr, il, ir, t0, t1); + krl ^= w0; krr ^= w1; + CAMELLIA_F(krl, krr, + CAMELLIA_SIGMA4L, CAMELLIA_SIGMA4R, + w0, w1, il, ir, t0, t1); + kll ^= w0; klr ^= w1; + + /* generate KA dependent subkeys */ + subl(2) = kll; subr(2) = klr; + subl(3) = krl; subr(3) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); + subl(6) = kll; subr(6) = klr; + subl(7) = krl; subr(7) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); + subl(8) = kll; subr(8) = klr; + subl(9) = krl; subr(9) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); + subl(12) = kll; subr(12) = klr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); + subl(14) = kll; subr(14) = klr; + subl(15) = krl; subr(15) = krr; + CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 34); + subl(20) = kll; subr(20) = klr; + subl(21) = krl; subr(21) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17); + subl(24) = kll; subr(24) = klr; + subl(25) = krl; subr(25) = krr; + + + /* absorb kw2 to other subkeys */ + subl(3) ^= subl(1); subr(3) ^= subr(1); + subl(5) ^= subl(1); subr(5) ^= subr(1); + subl(7) ^= subl(1); subr(7) ^= subr(1); + subl(1) ^= subr(1) & ~subr(9); + dw = subl(1) & subl(9), subr(1) ^= CAMELLIA_RL1(dw); + subl(11) ^= subl(1); subr(11) ^= subr(1); + subl(13) ^= subl(1); subr(13) ^= subr(1); + subl(15) ^= subl(1); subr(15) ^= subr(1); + subl(1) ^= subr(1) & ~subr(17); + dw = subl(1) & subl(17), subr(1) ^= CAMELLIA_RL1(dw); + subl(19) ^= subl(1); subr(19) ^= subr(1); + subl(21) ^= subl(1); subr(21) ^= subr(1); + subl(23) ^= subl(1); subr(23) ^= subr(1); + subl(24) ^= subl(1); subr(24) ^= subr(1); + + /* absorb kw4 to other subkeys */ + kw4l = subl(25); kw4r = subr(25); + subl(22) ^= kw4l; subr(22) ^= kw4r; + subl(20) ^= kw4l; subr(20) ^= kw4r; + subl(18) ^= kw4l; subr(18) ^= kw4r; + kw4l ^= kw4r & ~subr(16); + dw = kw4l & subl(16), kw4r ^= CAMELLIA_RL1(dw); + subl(14) ^= kw4l; subr(14) ^= kw4r; + subl(12) ^= kw4l; subr(12) ^= kw4r; + subl(10) ^= kw4l; subr(10) ^= kw4r; + kw4l ^= kw4r & ~subr(8); + dw = kw4l & subl(8), kw4r ^= CAMELLIA_RL1(dw); + subl(6) ^= kw4l; subr(6) ^= kw4r; + subl(4) ^= kw4l; subr(4) ^= kw4r; + subl(2) ^= kw4l; subr(2) ^= kw4r; + subl(0) ^= kw4l; subr(0) ^= kw4r; + + /* key XOR is end of F-function */ + CamelliaSubkeyL(0) = subl(0) ^ subl(2); + CamelliaSubkeyR(0) = subr(0) ^ subr(2); + CamelliaSubkeyL(2) = subl(3); + CamelliaSubkeyR(2) = subr(3); + CamelliaSubkeyL(3) = subl(2) ^ subl(4); + CamelliaSubkeyR(3) = subr(2) ^ subr(4); + CamelliaSubkeyL(4) = subl(3) ^ subl(5); + CamelliaSubkeyR(4) = subr(3) ^ subr(5); + CamelliaSubkeyL(5) = subl(4) ^ subl(6); + CamelliaSubkeyR(5) = subr(4) ^ subr(6); + CamelliaSubkeyL(6) = subl(5) ^ subl(7); + CamelliaSubkeyR(6) = subr(5) ^ subr(7); + tl = subl(10) ^ (subr(10) & ~subr(8)); + dw = tl & subl(8), tr = subr(10) ^ CAMELLIA_RL1(dw); + CamelliaSubkeyL(7) = subl(6) ^ tl; + CamelliaSubkeyR(7) = subr(6) ^ tr; + CamelliaSubkeyL(8) = subl(8); + CamelliaSubkeyR(8) = subr(8); + CamelliaSubkeyL(9) = subl(9); + CamelliaSubkeyR(9) = subr(9); + tl = subl(7) ^ (subr(7) & ~subr(9)); + dw = tl & subl(9), tr = subr(7) ^ CAMELLIA_RL1(dw); + CamelliaSubkeyL(10) = tl ^ subl(11); + CamelliaSubkeyR(10) = tr ^ subr(11); + CamelliaSubkeyL(11) = subl(10) ^ subl(12); + CamelliaSubkeyR(11) = subr(10) ^ subr(12); + CamelliaSubkeyL(12) = subl(11) ^ subl(13); + CamelliaSubkeyR(12) = subr(11) ^ subr(13); + CamelliaSubkeyL(13) = subl(12) ^ subl(14); + CamelliaSubkeyR(13) = subr(12) ^ subr(14); + CamelliaSubkeyL(14) = subl(13) ^ subl(15); + CamelliaSubkeyR(14) = subr(13) ^ subr(15); + tl = subl(18) ^ (subr(18) & ~subr(16)); + dw = tl & subl(16), tr = subr(18) ^ CAMELLIA_RL1(dw); + CamelliaSubkeyL(15) = subl(14) ^ tl; + CamelliaSubkeyR(15) = subr(14) ^ tr; + CamelliaSubkeyL(16) = subl(16); + CamelliaSubkeyR(16) = subr(16); + CamelliaSubkeyL(17) = subl(17); + CamelliaSubkeyR(17) = subr(17); + tl = subl(15) ^ (subr(15) & ~subr(17)); + dw = tl & subl(17), tr = subr(15) ^ CAMELLIA_RL1(dw); + CamelliaSubkeyL(18) = tl ^ subl(19); + CamelliaSubkeyR(18) = tr ^ subr(19); + CamelliaSubkeyL(19) = subl(18) ^ subl(20); + CamelliaSubkeyR(19) = subr(18) ^ subr(20); + CamelliaSubkeyL(20) = subl(19) ^ subl(21); + CamelliaSubkeyR(20) = subr(19) ^ subr(21); + CamelliaSubkeyL(21) = subl(20) ^ subl(22); + CamelliaSubkeyR(21) = subr(20) ^ subr(22); + CamelliaSubkeyL(22) = subl(21) ^ subl(23); + CamelliaSubkeyR(22) = subr(21) ^ subr(23); + CamelliaSubkeyL(23) = subl(22); + CamelliaSubkeyR(23) = subr(22); + CamelliaSubkeyL(24) = subl(24) ^ subl(23); + CamelliaSubkeyR(24) = subr(24) ^ subr(23); + + /* apply the inverse of the last half of P-function */ + dw = CamelliaSubkeyL(2) ^ CamelliaSubkeyR(2), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(2) = CamelliaSubkeyL(2) ^ dw, CamelliaSubkeyL(2) = dw; + dw = CamelliaSubkeyL(3) ^ CamelliaSubkeyR(3), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(3) = CamelliaSubkeyL(3) ^ dw, CamelliaSubkeyL(3) = dw; + dw = CamelliaSubkeyL(4) ^ CamelliaSubkeyR(4), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(4) = CamelliaSubkeyL(4) ^ dw, CamelliaSubkeyL(4) = dw; + dw = CamelliaSubkeyL(5) ^ CamelliaSubkeyR(5), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(5) = CamelliaSubkeyL(5) ^ dw, CamelliaSubkeyL(5) = dw; + dw = CamelliaSubkeyL(6) ^ CamelliaSubkeyR(6), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(6) = CamelliaSubkeyL(6) ^ dw, CamelliaSubkeyL(6) = dw; + dw = CamelliaSubkeyL(7) ^ CamelliaSubkeyR(7), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(7) = CamelliaSubkeyL(7) ^ dw, CamelliaSubkeyL(7) = dw; + dw = CamelliaSubkeyL(10) ^ CamelliaSubkeyR(10), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(10) = CamelliaSubkeyL(10) ^ dw, CamelliaSubkeyL(10) = dw; + dw = CamelliaSubkeyL(11) ^ CamelliaSubkeyR(11), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(11) = CamelliaSubkeyL(11) ^ dw, CamelliaSubkeyL(11) = dw; + dw = CamelliaSubkeyL(12) ^ CamelliaSubkeyR(12), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(12) = CamelliaSubkeyL(12) ^ dw, CamelliaSubkeyL(12) = dw; + dw = CamelliaSubkeyL(13) ^ CamelliaSubkeyR(13), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(13) = CamelliaSubkeyL(13) ^ dw, CamelliaSubkeyL(13) = dw; + dw = CamelliaSubkeyL(14) ^ CamelliaSubkeyR(14), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(14) = CamelliaSubkeyL(14) ^ dw, CamelliaSubkeyL(14) = dw; + dw = CamelliaSubkeyL(15) ^ CamelliaSubkeyR(15), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(15) = CamelliaSubkeyL(15) ^ dw, CamelliaSubkeyL(15) = dw; + dw = CamelliaSubkeyL(18) ^ CamelliaSubkeyR(18), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(18) = CamelliaSubkeyL(18) ^ dw, CamelliaSubkeyL(18) = dw; + dw = CamelliaSubkeyL(19) ^ CamelliaSubkeyR(19), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(19) = CamelliaSubkeyL(19) ^ dw, CamelliaSubkeyL(19) = dw; + dw = CamelliaSubkeyL(20) ^ CamelliaSubkeyR(20), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(20) = CamelliaSubkeyL(20) ^ dw, CamelliaSubkeyL(20) = dw; + dw = CamelliaSubkeyL(21) ^ CamelliaSubkeyR(21), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(21) = CamelliaSubkeyL(21) ^ dw, CamelliaSubkeyL(21) = dw; + dw = CamelliaSubkeyL(22) ^ CamelliaSubkeyR(22), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(22) = CamelliaSubkeyL(22) ^ dw, CamelliaSubkeyL(22) = dw; + dw = CamelliaSubkeyL(23) ^ CamelliaSubkeyR(23), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(23) = CamelliaSubkeyL(23) ^ dw, CamelliaSubkeyL(23) = dw; + +#ifdef CYASSL_SMALL_STACK + XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(subR, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif + + return 0; +} + +static int camellia_setup256(const unsigned char *key, u32 *subkey) +{ + u32 kll,klr,krl,krr; /* left half of key */ + u32 krll,krlr,krrl,krrr; /* right half of key */ + u32 il, ir, t0, t1, w0, w1; /* temporary variables */ + u32 kw4l, kw4r, dw, tl, tr; + +#ifdef CYASSL_SMALL_STACK + u32* subL; + u32* subR; + + subL = (u32*) XMALLOC(sizeof(u32) * 34, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (subL == NULL) + return MEMORY_E; + + subR = (u32*) XMALLOC(sizeof(u32) * 34, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (subR == NULL) { + XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER); + return MEMORY_E; + } +#else + u32 subL[34]; + u32 subR[34]; +#endif + + /** + * key = (kll || klr || krl || krr || krll || krlr || krrl || krrr) + * (|| is concatination) + */ + + kll = GETU32(key ); + klr = GETU32(key + 4); + krl = GETU32(key + 8); + krr = GETU32(key + 12); + krll = GETU32(key + 16); + krlr = GETU32(key + 20); + krrl = GETU32(key + 24); + krrr = GETU32(key + 28); + + /* generate KL dependent subkeys */ + subl(0) = kll; subr(0) = klr; + subl(1) = krl; subr(1) = krr; + CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 45); + subl(12) = kll; subr(12) = klr; + subl(13) = krl; subr(13) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); + subl(16) = kll; subr(16) = klr; + subl(17) = krl; subr(17) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17); + subl(22) = kll; subr(22) = klr; + subl(23) = krl; subr(23) = krr; + CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 34); + subl(30) = kll; subr(30) = klr; + subl(31) = krl; subr(31) = krr; + + /* generate KR dependent subkeys */ + CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 15); + subl(4) = krll; subr(4) = krlr; + subl(5) = krrl; subr(5) = krrr; + CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 15); + subl(8) = krll; subr(8) = krlr; + subl(9) = krrl; subr(9) = krrr; + CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30); + subl(18) = krll; subr(18) = krlr; + subl(19) = krrl; subr(19) = krrr; + CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 34); + subl(26) = krll; subr(26) = krlr; + subl(27) = krrl; subr(27) = krrr; + CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 34); + + /* generate KA */ + kll = subl(0) ^ krll; klr = subr(0) ^ krlr; + krl = subl(1) ^ krrl; krr = subr(1) ^ krrr; + CAMELLIA_F(kll, klr, + CAMELLIA_SIGMA1L, CAMELLIA_SIGMA1R, + w0, w1, il, ir, t0, t1); + krl ^= w0; krr ^= w1; + CAMELLIA_F(krl, krr, + CAMELLIA_SIGMA2L, CAMELLIA_SIGMA2R, + kll, klr, il, ir, t0, t1); + kll ^= krll; klr ^= krlr; + CAMELLIA_F(kll, klr, + CAMELLIA_SIGMA3L, CAMELLIA_SIGMA3R, + krl, krr, il, ir, t0, t1); + krl ^= w0 ^ krrl; krr ^= w1 ^ krrr; + CAMELLIA_F(krl, krr, + CAMELLIA_SIGMA4L, CAMELLIA_SIGMA4R, + w0, w1, il, ir, t0, t1); + kll ^= w0; klr ^= w1; + + /* generate KB */ + krll ^= kll; krlr ^= klr; + krrl ^= krl; krrr ^= krr; + CAMELLIA_F(krll, krlr, + CAMELLIA_SIGMA5L, CAMELLIA_SIGMA5R, + w0, w1, il, ir, t0, t1); + krrl ^= w0; krrr ^= w1; + CAMELLIA_F(krrl, krrr, + CAMELLIA_SIGMA6L, CAMELLIA_SIGMA6R, + w0, w1, il, ir, t0, t1); + krll ^= w0; krlr ^= w1; + + /* generate KA dependent subkeys */ + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); + subl(6) = kll; subr(6) = klr; + subl(7) = krl; subr(7) = krr; + CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 30); + subl(14) = kll; subr(14) = klr; + subl(15) = krl; subr(15) = krr; + subl(24) = klr; subr(24) = krl; + subl(25) = krr; subr(25) = kll; + CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 49); + subl(28) = kll; subr(28) = klr; + subl(29) = krl; subr(29) = krr; + + /* generate KB dependent subkeys */ + subl(2) = krll; subr(2) = krlr; + subl(3) = krrl; subr(3) = krrr; + CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30); + subl(10) = krll; subr(10) = krlr; + subl(11) = krrl; subr(11) = krrr; + CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30); + subl(20) = krll; subr(20) = krlr; + subl(21) = krrl; subr(21) = krrr; + CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 51); + subl(32) = krll; subr(32) = krlr; + subl(33) = krrl; subr(33) = krrr; + + /* absorb kw2 to other subkeys */ + subl(3) ^= subl(1); subr(3) ^= subr(1); + subl(5) ^= subl(1); subr(5) ^= subr(1); + subl(7) ^= subl(1); subr(7) ^= subr(1); + subl(1) ^= subr(1) & ~subr(9); + dw = subl(1) & subl(9), subr(1) ^= CAMELLIA_RL1(dw); + subl(11) ^= subl(1); subr(11) ^= subr(1); + subl(13) ^= subl(1); subr(13) ^= subr(1); + subl(15) ^= subl(1); subr(15) ^= subr(1); + subl(1) ^= subr(1) & ~subr(17); + dw = subl(1) & subl(17), subr(1) ^= CAMELLIA_RL1(dw); + subl(19) ^= subl(1); subr(19) ^= subr(1); + subl(21) ^= subl(1); subr(21) ^= subr(1); + subl(23) ^= subl(1); subr(23) ^= subr(1); + subl(1) ^= subr(1) & ~subr(25); + dw = subl(1) & subl(25), subr(1) ^= CAMELLIA_RL1(dw); + subl(27) ^= subl(1); subr(27) ^= subr(1); + subl(29) ^= subl(1); subr(29) ^= subr(1); + subl(31) ^= subl(1); subr(31) ^= subr(1); + subl(32) ^= subl(1); subr(32) ^= subr(1); + + /* absorb kw4 to other subkeys */ + kw4l = subl(33); kw4r = subr(33); + subl(30) ^= kw4l; subr(30) ^= kw4r; + subl(28) ^= kw4l; subr(28) ^= kw4r; + subl(26) ^= kw4l; subr(26) ^= kw4r; + kw4l ^= kw4r & ~subr(24); + dw = kw4l & subl(24), kw4r ^= CAMELLIA_RL1(dw); + subl(22) ^= kw4l; subr(22) ^= kw4r; + subl(20) ^= kw4l; subr(20) ^= kw4r; + subl(18) ^= kw4l; subr(18) ^= kw4r; + kw4l ^= kw4r & ~subr(16); + dw = kw4l & subl(16), kw4r ^= CAMELLIA_RL1(dw); + subl(14) ^= kw4l; subr(14) ^= kw4r; + subl(12) ^= kw4l; subr(12) ^= kw4r; + subl(10) ^= kw4l; subr(10) ^= kw4r; + kw4l ^= kw4r & ~subr(8); + dw = kw4l & subl(8), kw4r ^= CAMELLIA_RL1(dw); + subl(6) ^= kw4l; subr(6) ^= kw4r; + subl(4) ^= kw4l; subr(4) ^= kw4r; + subl(2) ^= kw4l; subr(2) ^= kw4r; + subl(0) ^= kw4l; subr(0) ^= kw4r; + + /* key XOR is end of F-function */ + CamelliaSubkeyL(0) = subl(0) ^ subl(2); + CamelliaSubkeyR(0) = subr(0) ^ subr(2); + CamelliaSubkeyL(2) = subl(3); + CamelliaSubkeyR(2) = subr(3); + CamelliaSubkeyL(3) = subl(2) ^ subl(4); + CamelliaSubkeyR(3) = subr(2) ^ subr(4); + CamelliaSubkeyL(4) = subl(3) ^ subl(5); + CamelliaSubkeyR(4) = subr(3) ^ subr(5); + CamelliaSubkeyL(5) = subl(4) ^ subl(6); + CamelliaSubkeyR(5) = subr(4) ^ subr(6); + CamelliaSubkeyL(6) = subl(5) ^ subl(7); + CamelliaSubkeyR(6) = subr(5) ^ subr(7); + tl = subl(10) ^ (subr(10) & ~subr(8)); + dw = tl & subl(8), tr = subr(10) ^ CAMELLIA_RL1(dw); + CamelliaSubkeyL(7) = subl(6) ^ tl; + CamelliaSubkeyR(7) = subr(6) ^ tr; + CamelliaSubkeyL(8) = subl(8); + CamelliaSubkeyR(8) = subr(8); + CamelliaSubkeyL(9) = subl(9); + CamelliaSubkeyR(9) = subr(9); + tl = subl(7) ^ (subr(7) & ~subr(9)); + dw = tl & subl(9), tr = subr(7) ^ CAMELLIA_RL1(dw); + CamelliaSubkeyL(10) = tl ^ subl(11); + CamelliaSubkeyR(10) = tr ^ subr(11); + CamelliaSubkeyL(11) = subl(10) ^ subl(12); + CamelliaSubkeyR(11) = subr(10) ^ subr(12); + CamelliaSubkeyL(12) = subl(11) ^ subl(13); + CamelliaSubkeyR(12) = subr(11) ^ subr(13); + CamelliaSubkeyL(13) = subl(12) ^ subl(14); + CamelliaSubkeyR(13) = subr(12) ^ subr(14); + CamelliaSubkeyL(14) = subl(13) ^ subl(15); + CamelliaSubkeyR(14) = subr(13) ^ subr(15); + tl = subl(18) ^ (subr(18) & ~subr(16)); + dw = tl & subl(16), tr = subr(18) ^ CAMELLIA_RL1(dw); + CamelliaSubkeyL(15) = subl(14) ^ tl; + CamelliaSubkeyR(15) = subr(14) ^ tr; + CamelliaSubkeyL(16) = subl(16); + CamelliaSubkeyR(16) = subr(16); + CamelliaSubkeyL(17) = subl(17); + CamelliaSubkeyR(17) = subr(17); + tl = subl(15) ^ (subr(15) & ~subr(17)); + dw = tl & subl(17), tr = subr(15) ^ CAMELLIA_RL1(dw); + CamelliaSubkeyL(18) = tl ^ subl(19); + CamelliaSubkeyR(18) = tr ^ subr(19); + CamelliaSubkeyL(19) = subl(18) ^ subl(20); + CamelliaSubkeyR(19) = subr(18) ^ subr(20); + CamelliaSubkeyL(20) = subl(19) ^ subl(21); + CamelliaSubkeyR(20) = subr(19) ^ subr(21); + CamelliaSubkeyL(21) = subl(20) ^ subl(22); + CamelliaSubkeyR(21) = subr(20) ^ subr(22); + CamelliaSubkeyL(22) = subl(21) ^ subl(23); + CamelliaSubkeyR(22) = subr(21) ^ subr(23); + tl = subl(26) ^ (subr(26) & ~subr(24)); + dw = tl & subl(24), tr = subr(26) ^ CAMELLIA_RL1(dw); + CamelliaSubkeyL(23) = subl(22) ^ tl; + CamelliaSubkeyR(23) = subr(22) ^ tr; + CamelliaSubkeyL(24) = subl(24); + CamelliaSubkeyR(24) = subr(24); + CamelliaSubkeyL(25) = subl(25); + CamelliaSubkeyR(25) = subr(25); + tl = subl(23) ^ (subr(23) & ~subr(25)); + dw = tl & subl(25), tr = subr(23) ^ CAMELLIA_RL1(dw); + CamelliaSubkeyL(26) = tl ^ subl(27); + CamelliaSubkeyR(26) = tr ^ subr(27); + CamelliaSubkeyL(27) = subl(26) ^ subl(28); + CamelliaSubkeyR(27) = subr(26) ^ subr(28); + CamelliaSubkeyL(28) = subl(27) ^ subl(29); + CamelliaSubkeyR(28) = subr(27) ^ subr(29); + CamelliaSubkeyL(29) = subl(28) ^ subl(30); + CamelliaSubkeyR(29) = subr(28) ^ subr(30); + CamelliaSubkeyL(30) = subl(29) ^ subl(31); + CamelliaSubkeyR(30) = subr(29) ^ subr(31); + CamelliaSubkeyL(31) = subl(30); + CamelliaSubkeyR(31) = subr(30); + CamelliaSubkeyL(32) = subl(32) ^ subl(31); + CamelliaSubkeyR(32) = subr(32) ^ subr(31); + + /* apply the inverse of the last half of P-function */ + dw = CamelliaSubkeyL(2) ^ CamelliaSubkeyR(2), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(2) = CamelliaSubkeyL(2) ^ dw, CamelliaSubkeyL(2) = dw; + dw = CamelliaSubkeyL(3) ^ CamelliaSubkeyR(3), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(3) = CamelliaSubkeyL(3) ^ dw, CamelliaSubkeyL(3) = dw; + dw = CamelliaSubkeyL(4) ^ CamelliaSubkeyR(4), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(4) = CamelliaSubkeyL(4) ^ dw, CamelliaSubkeyL(4) = dw; + dw = CamelliaSubkeyL(5) ^ CamelliaSubkeyR(5), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(5) = CamelliaSubkeyL(5) ^ dw, CamelliaSubkeyL(5) = dw; + dw = CamelliaSubkeyL(6) ^ CamelliaSubkeyR(6), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(6) = CamelliaSubkeyL(6) ^ dw, CamelliaSubkeyL(6) = dw; + dw = CamelliaSubkeyL(7) ^ CamelliaSubkeyR(7), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(7) = CamelliaSubkeyL(7) ^ dw, CamelliaSubkeyL(7) = dw; + dw = CamelliaSubkeyL(10) ^ CamelliaSubkeyR(10), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(10) = CamelliaSubkeyL(10) ^ dw, CamelliaSubkeyL(10) = dw; + dw = CamelliaSubkeyL(11) ^ CamelliaSubkeyR(11), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(11) = CamelliaSubkeyL(11) ^ dw, CamelliaSubkeyL(11) = dw; + dw = CamelliaSubkeyL(12) ^ CamelliaSubkeyR(12), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(12) = CamelliaSubkeyL(12) ^ dw, CamelliaSubkeyL(12) = dw; + dw = CamelliaSubkeyL(13) ^ CamelliaSubkeyR(13), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(13) = CamelliaSubkeyL(13) ^ dw, CamelliaSubkeyL(13) = dw; + dw = CamelliaSubkeyL(14) ^ CamelliaSubkeyR(14), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(14) = CamelliaSubkeyL(14) ^ dw, CamelliaSubkeyL(14) = dw; + dw = CamelliaSubkeyL(15) ^ CamelliaSubkeyR(15), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(15) = CamelliaSubkeyL(15) ^ dw, CamelliaSubkeyL(15) = dw; + dw = CamelliaSubkeyL(18) ^ CamelliaSubkeyR(18), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(18) = CamelliaSubkeyL(18) ^ dw, CamelliaSubkeyL(18) = dw; + dw = CamelliaSubkeyL(19) ^ CamelliaSubkeyR(19), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(19) = CamelliaSubkeyL(19) ^ dw, CamelliaSubkeyL(19) = dw; + dw = CamelliaSubkeyL(20) ^ CamelliaSubkeyR(20), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(20) = CamelliaSubkeyL(20) ^ dw, CamelliaSubkeyL(20) = dw; + dw = CamelliaSubkeyL(21) ^ CamelliaSubkeyR(21), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(21) = CamelliaSubkeyL(21) ^ dw, CamelliaSubkeyL(21) = dw; + dw = CamelliaSubkeyL(22) ^ CamelliaSubkeyR(22), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(22) = CamelliaSubkeyL(22) ^ dw, CamelliaSubkeyL(22) = dw; + dw = CamelliaSubkeyL(23) ^ CamelliaSubkeyR(23), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(23) = CamelliaSubkeyL(23) ^ dw, CamelliaSubkeyL(23) = dw; + dw = CamelliaSubkeyL(26) ^ CamelliaSubkeyR(26), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(26) = CamelliaSubkeyL(26) ^ dw, CamelliaSubkeyL(26) = dw; + dw = CamelliaSubkeyL(27) ^ CamelliaSubkeyR(27), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(27) = CamelliaSubkeyL(27) ^ dw, CamelliaSubkeyL(27) = dw; + dw = CamelliaSubkeyL(28) ^ CamelliaSubkeyR(28), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(28) = CamelliaSubkeyL(28) ^ dw, CamelliaSubkeyL(28) = dw; + dw = CamelliaSubkeyL(29) ^ CamelliaSubkeyR(29), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(29) = CamelliaSubkeyL(29) ^ dw, CamelliaSubkeyL(29) = dw; + dw = CamelliaSubkeyL(30) ^ CamelliaSubkeyR(30), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(30) = CamelliaSubkeyL(30) ^ dw, CamelliaSubkeyL(30) = dw; + dw = CamelliaSubkeyL(31) ^ CamelliaSubkeyR(31), dw = CAMELLIA_RL8(dw); + CamelliaSubkeyR(31) = CamelliaSubkeyL(31) ^ dw,CamelliaSubkeyL(31) = dw; + +#ifdef CYASSL_SMALL_STACK + XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(subR, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif + + return 0; +} + +static int camellia_setup192(const unsigned char *key, u32 *subkey) +{ + unsigned char kk[32]; + u32 krll, krlr, krrl,krrr; + + memcpy(kk, key, 24); + memcpy((unsigned char *)&krll, key+16,4); + memcpy((unsigned char *)&krlr, key+20,4); + krrl = ~krll; + krrr = ~krlr; + memcpy(kk+24, (unsigned char *)&krrl, 4); + memcpy(kk+28, (unsigned char *)&krrr, 4); + + return camellia_setup256(kk, subkey); +} + + +/** + * Stuff related to camellia encryption/decryption + * + * "io" must be 4byte aligned and big-endian data. + */ +static void camellia_encrypt128(const u32 *subkey, u32 *io) +{ + u32 il, ir, t0, t1; + + /* pre whitening but absorb kw2*/ + io[0] ^= CamelliaSubkeyL(0); + io[1] ^= CamelliaSubkeyR(0); + /* main iteration */ + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(2),CamelliaSubkeyR(2), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(3),CamelliaSubkeyR(3), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(4),CamelliaSubkeyR(4), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(5),CamelliaSubkeyR(5), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(6),CamelliaSubkeyR(6), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(7),CamelliaSubkeyR(7), + io[0],io[1],il,ir,t0,t1); + + CAMELLIA_FLS(io[0],io[1],io[2],io[3], + CamelliaSubkeyL(8),CamelliaSubkeyR(8), + CamelliaSubkeyL(9),CamelliaSubkeyR(9), + t0,t1,il,ir); + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(10),CamelliaSubkeyR(10), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(11),CamelliaSubkeyR(11), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(12),CamelliaSubkeyR(12), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(13),CamelliaSubkeyR(13), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(14),CamelliaSubkeyR(14), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(15),CamelliaSubkeyR(15), + io[0],io[1],il,ir,t0,t1); + + CAMELLIA_FLS(io[0],io[1],io[2],io[3], + CamelliaSubkeyL(16),CamelliaSubkeyR(16), + CamelliaSubkeyL(17),CamelliaSubkeyR(17), + t0,t1,il,ir); + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(18),CamelliaSubkeyR(18), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(19),CamelliaSubkeyR(19), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(20),CamelliaSubkeyR(20), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(21),CamelliaSubkeyR(21), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(22),CamelliaSubkeyR(22), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(23),CamelliaSubkeyR(23), + io[0],io[1],il,ir,t0,t1); + + /* post whitening but kw4 */ + io[2] ^= CamelliaSubkeyL(24); + io[3] ^= CamelliaSubkeyR(24); + + t0 = io[0]; + t1 = io[1]; + io[0] = io[2]; + io[1] = io[3]; + io[2] = t0; + io[3] = t1; + + return; +} + +static void camellia_decrypt128(const u32 *subkey, u32 *io) +{ + u32 il,ir,t0,t1; /* temporary valiables */ + + /* pre whitening but absorb kw2*/ + io[0] ^= CamelliaSubkeyL(24); + io[1] ^= CamelliaSubkeyR(24); + + /* main iteration */ + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(23),CamelliaSubkeyR(23), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(22),CamelliaSubkeyR(22), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(21),CamelliaSubkeyR(21), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(20),CamelliaSubkeyR(20), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(19),CamelliaSubkeyR(19), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(18),CamelliaSubkeyR(18), + io[0],io[1],il,ir,t0,t1); + + CAMELLIA_FLS(io[0],io[1],io[2],io[3], + CamelliaSubkeyL(17),CamelliaSubkeyR(17), + CamelliaSubkeyL(16),CamelliaSubkeyR(16), + t0,t1,il,ir); + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(15),CamelliaSubkeyR(15), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(14),CamelliaSubkeyR(14), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(13),CamelliaSubkeyR(13), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(12),CamelliaSubkeyR(12), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(11),CamelliaSubkeyR(11), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(10),CamelliaSubkeyR(10), + io[0],io[1],il,ir,t0,t1); + + CAMELLIA_FLS(io[0],io[1],io[2],io[3], + CamelliaSubkeyL(9),CamelliaSubkeyR(9), + CamelliaSubkeyL(8),CamelliaSubkeyR(8), + t0,t1,il,ir); + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(7),CamelliaSubkeyR(7), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(6),CamelliaSubkeyR(6), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(5),CamelliaSubkeyR(5), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(4),CamelliaSubkeyR(4), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(3),CamelliaSubkeyR(3), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(2),CamelliaSubkeyR(2), + io[0],io[1],il,ir,t0,t1); + + /* post whitening but kw4 */ + io[2] ^= CamelliaSubkeyL(0); + io[3] ^= CamelliaSubkeyR(0); + + t0 = io[0]; + t1 = io[1]; + io[0] = io[2]; + io[1] = io[3]; + io[2] = t0; + io[3] = t1; + + return; +} + +/** + * stuff for 192 and 256bit encryption/decryption + */ +static void camellia_encrypt256(const u32 *subkey, u32 *io) +{ + u32 il,ir,t0,t1; /* temporary valiables */ + + /* pre whitening but absorb kw2*/ + io[0] ^= CamelliaSubkeyL(0); + io[1] ^= CamelliaSubkeyR(0); + + /* main iteration */ + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(2),CamelliaSubkeyR(2), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(3),CamelliaSubkeyR(3), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(4),CamelliaSubkeyR(4), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(5),CamelliaSubkeyR(5), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(6),CamelliaSubkeyR(6), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(7),CamelliaSubkeyR(7), + io[0],io[1],il,ir,t0,t1); + + CAMELLIA_FLS(io[0],io[1],io[2],io[3], + CamelliaSubkeyL(8),CamelliaSubkeyR(8), + CamelliaSubkeyL(9),CamelliaSubkeyR(9), + t0,t1,il,ir); + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(10),CamelliaSubkeyR(10), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(11),CamelliaSubkeyR(11), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(12),CamelliaSubkeyR(12), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(13),CamelliaSubkeyR(13), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(14),CamelliaSubkeyR(14), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(15),CamelliaSubkeyR(15), + io[0],io[1],il,ir,t0,t1); + + CAMELLIA_FLS(io[0],io[1],io[2],io[3], + CamelliaSubkeyL(16),CamelliaSubkeyR(16), + CamelliaSubkeyL(17),CamelliaSubkeyR(17), + t0,t1,il,ir); + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(18),CamelliaSubkeyR(18), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(19),CamelliaSubkeyR(19), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(20),CamelliaSubkeyR(20), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(21),CamelliaSubkeyR(21), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(22),CamelliaSubkeyR(22), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(23),CamelliaSubkeyR(23), + io[0],io[1],il,ir,t0,t1); + + CAMELLIA_FLS(io[0],io[1],io[2],io[3], + CamelliaSubkeyL(24),CamelliaSubkeyR(24), + CamelliaSubkeyL(25),CamelliaSubkeyR(25), + t0,t1,il,ir); + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(26),CamelliaSubkeyR(26), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(27),CamelliaSubkeyR(27), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(28),CamelliaSubkeyR(28), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(29),CamelliaSubkeyR(29), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(30),CamelliaSubkeyR(30), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(31),CamelliaSubkeyR(31), + io[0],io[1],il,ir,t0,t1); + + /* post whitening but kw4 */ + io[2] ^= CamelliaSubkeyL(32); + io[3] ^= CamelliaSubkeyR(32); + + t0 = io[0]; + t1 = io[1]; + io[0] = io[2]; + io[1] = io[3]; + io[2] = t0; + io[3] = t1; + + return; +} + +static void camellia_decrypt256(const u32 *subkey, u32 *io) +{ + u32 il,ir,t0,t1; /* temporary valiables */ + + /* pre whitening but absorb kw2*/ + io[0] ^= CamelliaSubkeyL(32); + io[1] ^= CamelliaSubkeyR(32); + + /* main iteration */ + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(31),CamelliaSubkeyR(31), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(30),CamelliaSubkeyR(30), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(29),CamelliaSubkeyR(29), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(28),CamelliaSubkeyR(28), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(27),CamelliaSubkeyR(27), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(26),CamelliaSubkeyR(26), + io[0],io[1],il,ir,t0,t1); + + CAMELLIA_FLS(io[0],io[1],io[2],io[3], + CamelliaSubkeyL(25),CamelliaSubkeyR(25), + CamelliaSubkeyL(24),CamelliaSubkeyR(24), + t0,t1,il,ir); + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(23),CamelliaSubkeyR(23), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(22),CamelliaSubkeyR(22), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(21),CamelliaSubkeyR(21), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(20),CamelliaSubkeyR(20), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(19),CamelliaSubkeyR(19), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(18),CamelliaSubkeyR(18), + io[0],io[1],il,ir,t0,t1); + + CAMELLIA_FLS(io[0],io[1],io[2],io[3], + CamelliaSubkeyL(17),CamelliaSubkeyR(17), + CamelliaSubkeyL(16),CamelliaSubkeyR(16), + t0,t1,il,ir); + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(15),CamelliaSubkeyR(15), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(14),CamelliaSubkeyR(14), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(13),CamelliaSubkeyR(13), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(12),CamelliaSubkeyR(12), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(11),CamelliaSubkeyR(11), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(10),CamelliaSubkeyR(10), + io[0],io[1],il,ir,t0,t1); + + CAMELLIA_FLS(io[0],io[1],io[2],io[3], + CamelliaSubkeyL(9),CamelliaSubkeyR(9), + CamelliaSubkeyL(8),CamelliaSubkeyR(8), + t0,t1,il,ir); + + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(7),CamelliaSubkeyR(7), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(6),CamelliaSubkeyR(6), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(5),CamelliaSubkeyR(5), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(4),CamelliaSubkeyR(4), + io[0],io[1],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[0],io[1], + CamelliaSubkeyL(3),CamelliaSubkeyR(3), + io[2],io[3],il,ir,t0,t1); + CAMELLIA_ROUNDSM(io[2],io[3], + CamelliaSubkeyL(2),CamelliaSubkeyR(2), + io[0],io[1],il,ir,t0,t1); + + /* post whitening but kw4 */ + io[2] ^= CamelliaSubkeyL(0); + io[3] ^= CamelliaSubkeyR(0); + + t0 = io[0]; + t1 = io[1]; + io[0] = io[2]; + io[1] = io[3]; + io[2] = t0; + io[3] = t1; + + return; +} + +/*** + * + * API for compatibility + */ + +static void Camellia_EncryptBlock(const int keyBitLength, + const unsigned char *plaintext, + const KEY_TABLE_TYPE keyTable, + unsigned char *ciphertext) +{ + u32 tmp[4]; + + tmp[0] = GETU32(plaintext); + tmp[1] = GETU32(plaintext + 4); + tmp[2] = GETU32(plaintext + 8); + tmp[3] = GETU32(plaintext + 12); + + switch (keyBitLength) { + case 128: + camellia_encrypt128(keyTable, tmp); + break; + case 192: + /* fall through */ + case 256: + camellia_encrypt256(keyTable, tmp); + break; + default: + break; + } + + PUTU32(ciphertext, tmp[0]); + PUTU32(ciphertext + 4, tmp[1]); + PUTU32(ciphertext + 8, tmp[2]); + PUTU32(ciphertext + 12, tmp[3]); +} + +static void Camellia_DecryptBlock(const int keyBitLength, + const unsigned char *ciphertext, + const KEY_TABLE_TYPE keyTable, + unsigned char *plaintext) +{ + u32 tmp[4]; + + tmp[0] = GETU32(ciphertext); + tmp[1] = GETU32(ciphertext + 4); + tmp[2] = GETU32(ciphertext + 8); + tmp[3] = GETU32(ciphertext + 12); + + switch (keyBitLength) { + case 128: + camellia_decrypt128(keyTable, tmp); + break; + case 192: + /* fall through */ + case 256: + camellia_decrypt256(keyTable, tmp); + break; + default: + break; + } + PUTU32(plaintext, tmp[0]); + PUTU32(plaintext + 4, tmp[1]); + PUTU32(plaintext + 8, tmp[2]); + PUTU32(plaintext + 12, tmp[3]); +} + + + +/* CTaoCrypt wrappers to the Camellia code */ + +int CamelliaSetKey(Camellia* cam, const byte* key, word32 len, const byte* iv) +{ + int ret = 0; + + if (cam == NULL) return BAD_FUNC_ARG; + + XMEMSET(cam->key, 0, sizeof(KEY_TABLE_TYPE)); + + switch (len) { + case 16: + ret = camellia_setup128(key, cam->key); + break; + case 24: + ret = camellia_setup192(key, cam->key); + break; + case 32: + ret = camellia_setup256(key, cam->key); + break; + default: + return BAD_FUNC_ARG; + } + + if (ret != 0) + return ret; + + cam->keySz = len * 8; + + return CamelliaSetIV(cam, iv); +} + + +int CamelliaSetIV(Camellia* cam, const byte* iv) +{ + if (cam == NULL) + return BAD_FUNC_ARG; + + if (iv) + XMEMCPY(cam->reg, iv, CAMELLIA_BLOCK_SIZE); + else + XMEMSET(cam->reg, 0, CAMELLIA_BLOCK_SIZE); + + return 0; +} + + +void CamelliaEncryptDirect(Camellia* cam, byte* out, const byte* in) +{ + Camellia_EncryptBlock(cam->keySz, in, cam->key, out); +} + + +void CamelliaDecryptDirect(Camellia* cam, byte* out, const byte* in) +{ + Camellia_DecryptBlock(cam->keySz, in, cam->key, out); +} + + +void CamelliaCbcEncrypt(Camellia* cam, byte* out, const byte* in, word32 sz) +{ + word32 blocks = sz / CAMELLIA_BLOCK_SIZE; + + while (blocks--) { + xorbuf((byte*)cam->reg, in, CAMELLIA_BLOCK_SIZE); + Camellia_EncryptBlock(cam->keySz, (byte*)cam->reg, + cam->key, (byte*)cam->reg); + XMEMCPY(out, cam->reg, CAMELLIA_BLOCK_SIZE); + + out += CAMELLIA_BLOCK_SIZE; + in += CAMELLIA_BLOCK_SIZE; + } +} + + +void CamelliaCbcDecrypt(Camellia* cam, byte* out, const byte* in, word32 sz) +{ + word32 blocks = sz / CAMELLIA_BLOCK_SIZE; + + while (blocks--) { + XMEMCPY(cam->tmp, in, CAMELLIA_BLOCK_SIZE); + Camellia_DecryptBlock(cam->keySz, (byte*)cam->tmp, cam->key, out); + xorbuf(out, (byte*)cam->reg, CAMELLIA_BLOCK_SIZE); + XMEMCPY(cam->reg, cam->tmp, CAMELLIA_BLOCK_SIZE); + + out += CAMELLIA_BLOCK_SIZE; + in += CAMELLIA_BLOCK_SIZE; + } +} + + +#endif /* HAVE_CAMELLIA */ + diff --git a/ctaocrypt/src/md2.c b/ctaocrypt/src/md2.c index e129cf73c..6bda8054a 100644 --- a/ctaocrypt/src/md2.c +++ b/ctaocrypt/src/md2.c @@ -20,140 +20,140 @@ */ -#ifdef HAVE_CONFIG_H - #include -#endif - -#include - -#ifdef CYASSL_MD2 - -#include -#include - -#ifdef NO_INLINE - #include -#else - #include -#endif - - -void InitMd2(Md2* md2) -{ - XMEMSET(md2->X, 0, MD2_X_SIZE); - XMEMSET(md2->C, 0, MD2_BLOCK_SIZE); - XMEMSET(md2->buffer, 0, MD2_BLOCK_SIZE); - md2->count = 0; -} - - -void Md2Update(Md2* md2, const byte* data, word32 len) -{ - static const byte S[256] = - { - 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, - 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, - 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, - 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, - 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, - 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, - 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, - 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, - 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157, - 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27, - 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, - 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, - 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, - 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, - 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233, - 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228, - 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, - 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 - }; - - while (len) { - word32 L = (MD2_PAD_SIZE - md2->count) < len ? - (MD2_PAD_SIZE - md2->count) : len; - XMEMCPY(md2->buffer + md2->count, data, L); - md2->count += L; - data += L; - len -= L; - - if (md2->count == MD2_PAD_SIZE) { - int i; - byte t; - - md2->count = 0; - XMEMCPY(md2->X + MD2_PAD_SIZE, md2->buffer, MD2_PAD_SIZE); - t = md2->C[15]; - - for(i = 0; i < MD2_PAD_SIZE; i++) { - md2->X[32 + i] = md2->X[MD2_PAD_SIZE + i] ^ md2->X[i]; - t = md2->C[i] ^= S[md2->buffer[i] ^ t]; - } - - t=0; - for(i = 0; i < 18; i++) { - int j; - for(j = 0; j < MD2_X_SIZE; j += 8) { - t = md2->X[j+0] ^= S[t]; - t = md2->X[j+1] ^= S[t]; - t = md2->X[j+2] ^= S[t]; - t = md2->X[j+3] ^= S[t]; - t = md2->X[j+4] ^= S[t]; - t = md2->X[j+5] ^= S[t]; - t = md2->X[j+6] ^= S[t]; - t = md2->X[j+7] ^= S[t]; - } - t = (t + i) & 0xFF; - } - } - } -} - - -void Md2Final(Md2* md2, byte* hash) -{ - byte padding[MD2_BLOCK_SIZE]; - word32 padLen = MD2_PAD_SIZE - md2->count; - word32 i; - - for (i = 0; i < padLen; i++) - padding[i] = (byte)padLen; - - Md2Update(md2, padding, padLen); - Md2Update(md2, md2->C, MD2_BLOCK_SIZE); - - XMEMCPY(hash, md2->X, MD2_DIGEST_SIZE); - - InitMd2(md2); -} - - -int Md2Hash(const byte* data, word32 len, byte* hash) -{ -#ifdef CYASSL_SMALL_STACK - Md2* md2; -#else - Md2 md2[1]; -#endif - -#ifdef CYASSL_SMALL_STACK - md2 = (Md2*)XMALLOC(sizeof(Md2), NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (md2 == NULL) - return MEMORY_E; -#endif - - InitMd2(md2); - Md2Update(md2, data, len); - Md2Final(md2, hash); - -#ifdef CYASSL_SMALL_STACK - XFREE(md2, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - return 0; -} - - -#endif /* CYASSL_MD2 */ +//#ifdef HAVE_CONFIG_H +// #include +//#endif +// +//#include +// +//#ifdef CYASSL_MD2 +// +//#include +//#include +// +//#ifdef NO_INLINE +// #include +//#else +// #include +//#endif +// +// +//void InitMd2(Md2* md2) +//{ +// XMEMSET(md2->X, 0, MD2_X_SIZE); +// XMEMSET(md2->C, 0, MD2_BLOCK_SIZE); +// XMEMSET(md2->buffer, 0, MD2_BLOCK_SIZE); +// md2->count = 0; +//} +// +// +//void Md2Update(Md2* md2, const byte* data, word32 len) +//{ +// static const byte S[256] = +// { +// 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, +// 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, +// 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, +// 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, +// 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, +// 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, +// 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, +// 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, +// 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157, +// 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27, +// 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, +// 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, +// 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, +// 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, +// 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233, +// 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228, +// 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, +// 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 +// }; +// +// while (len) { +// word32 L = (MD2_PAD_SIZE - md2->count) < len ? +// (MD2_PAD_SIZE - md2->count) : len; +// XMEMCPY(md2->buffer + md2->count, data, L); +// md2->count += L; +// data += L; +// len -= L; +// +// if (md2->count == MD2_PAD_SIZE) { +// int i; +// byte t; +// +// md2->count = 0; +// XMEMCPY(md2->X + MD2_PAD_SIZE, md2->buffer, MD2_PAD_SIZE); +// t = md2->C[15]; +// +// for(i = 0; i < MD2_PAD_SIZE; i++) { +// md2->X[32 + i] = md2->X[MD2_PAD_SIZE + i] ^ md2->X[i]; +// t = md2->C[i] ^= S[md2->buffer[i] ^ t]; +// } +// +// t=0; +// for(i = 0; i < 18; i++) { +// int j; +// for(j = 0; j < MD2_X_SIZE; j += 8) { +// t = md2->X[j+0] ^= S[t]; +// t = md2->X[j+1] ^= S[t]; +// t = md2->X[j+2] ^= S[t]; +// t = md2->X[j+3] ^= S[t]; +// t = md2->X[j+4] ^= S[t]; +// t = md2->X[j+5] ^= S[t]; +// t = md2->X[j+6] ^= S[t]; +// t = md2->X[j+7] ^= S[t]; +// } +// t = (t + i) & 0xFF; +// } +// } +// } +//} +// +// +//void Md2Final(Md2* md2, byte* hash) +//{ +// byte padding[MD2_BLOCK_SIZE]; +// word32 padLen = MD2_PAD_SIZE - md2->count; +// word32 i; +// +// for (i = 0; i < padLen; i++) +// padding[i] = (byte)padLen; +// +// Md2Update(md2, padding, padLen); +// Md2Update(md2, md2->C, MD2_BLOCK_SIZE); +// +// XMEMCPY(hash, md2->X, MD2_DIGEST_SIZE); +// +// InitMd2(md2); +//} +// +// +//int Md2Hash(const byte* data, word32 len, byte* hash) +//{ +//#ifdef CYASSL_SMALL_STACK +// Md2* md2; +//#else +// Md2 md2[1]; +//#endif +// +//#ifdef CYASSL_SMALL_STACK +// md2 = (Md2*)XMALLOC(sizeof(Md2), NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (md2 == NULL) +// return MEMORY_E; +//#endif +// +// InitMd2(md2); +// Md2Update(md2, data, len); +// Md2Final(md2, hash); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(md2, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// return 0; +//} +// +// +//#endif /* CYASSL_MD2 */ diff --git a/ctaocrypt/src/md4.c b/ctaocrypt/src/md4.c index d696d6380..50613c4bb 100644 --- a/ctaocrypt/src/md4.c +++ b/ctaocrypt/src/md4.c @@ -19,201 +19,201 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifdef HAVE_CONFIG_H - #include -#endif - -#include - -#ifndef NO_MD4 - -#include -#ifdef NO_INLINE - #include -#else - #include -#endif - - -#ifndef min - - static INLINE word32 min(word32 a, word32 b) - { - return a > b ? b : a; - } - -#endif /* min */ - - -void InitMd4(Md4* md4) -{ - md4->digest[0] = 0x67452301L; - md4->digest[1] = 0xefcdab89L; - md4->digest[2] = 0x98badcfeL; - md4->digest[3] = 0x10325476L; - - md4->buffLen = 0; - md4->loLen = 0; - md4->hiLen = 0; -} - - -static void Transform(Md4* md4) -{ -#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) -#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) -#define H(x, y, z) ((x) ^ (y) ^ (z)) - - /* Copy context->state[] to working vars */ - word32 A = md4->digest[0]; - word32 B = md4->digest[1]; - word32 C = md4->digest[2]; - word32 D = md4->digest[3]; - -#define function(a,b,c,d,k,s) a=rotlFixed(a+F(b,c,d)+md4->buffer[k],s); - function(A,B,C,D, 0, 3); - function(D,A,B,C, 1, 7); - function(C,D,A,B, 2,11); - function(B,C,D,A, 3,19); - function(A,B,C,D, 4, 3); - function(D,A,B,C, 5, 7); - function(C,D,A,B, 6,11); - function(B,C,D,A, 7,19); - function(A,B,C,D, 8, 3); - function(D,A,B,C, 9, 7); - function(C,D,A,B,10,11); - function(B,C,D,A,11,19); - function(A,B,C,D,12, 3); - function(D,A,B,C,13, 7); - function(C,D,A,B,14,11); - function(B,C,D,A,15,19); - -#undef function -#define function(a,b,c,d,k,s) \ - a=rotlFixed(a+G(b,c,d)+md4->buffer[k]+0x5a827999,s); - - function(A,B,C,D, 0, 3); - function(D,A,B,C, 4, 5); - function(C,D,A,B, 8, 9); - function(B,C,D,A,12,13); - function(A,B,C,D, 1, 3); - function(D,A,B,C, 5, 5); - function(C,D,A,B, 9, 9); - function(B,C,D,A,13,13); - function(A,B,C,D, 2, 3); - function(D,A,B,C, 6, 5); - function(C,D,A,B,10, 9); - function(B,C,D,A,14,13); - function(A,B,C,D, 3, 3); - function(D,A,B,C, 7, 5); - function(C,D,A,B,11, 9); - function(B,C,D,A,15,13); - -#undef function -#define function(a,b,c,d,k,s) \ - a=rotlFixed(a+H(b,c,d)+md4->buffer[k]+0x6ed9eba1,s); - - function(A,B,C,D, 0, 3); - function(D,A,B,C, 8, 9); - function(C,D,A,B, 4,11); - function(B,C,D,A,12,15); - function(A,B,C,D, 2, 3); - function(D,A,B,C,10, 9); - function(C,D,A,B, 6,11); - function(B,C,D,A,14,15); - function(A,B,C,D, 1, 3); - function(D,A,B,C, 9, 9); - function(C,D,A,B, 5,11); - function(B,C,D,A,13,15); - function(A,B,C,D, 3, 3); - function(D,A,B,C,11, 9); - function(C,D,A,B, 7,11); - function(B,C,D,A,15,15); - - /* Add the working vars back into digest state[] */ - md4->digest[0] += A; - md4->digest[1] += B; - md4->digest[2] += C; - md4->digest[3] += D; -} - - -static INLINE void AddLength(Md4* md4, word32 len) -{ - word32 tmp = md4->loLen; - if ( (md4->loLen += len) < tmp) - md4->hiLen++; /* carry low to high */ -} - - -void Md4Update(Md4* md4, const byte* data, word32 len) -{ - /* do block size increments */ - byte* local = (byte*)md4->buffer; - - while (len) { - word32 add = min(len, MD4_BLOCK_SIZE - md4->buffLen); - XMEMCPY(&local[md4->buffLen], data, add); - - md4->buffLen += add; - data += add; - len -= add; - - if (md4->buffLen == MD4_BLOCK_SIZE) { - #ifdef BIG_ENDIAN_ORDER - ByteReverseWords(md4->buffer, md4->buffer, MD4_BLOCK_SIZE); - #endif - Transform(md4); - AddLength(md4, MD4_BLOCK_SIZE); - md4->buffLen = 0; - } - } -} - - -void Md4Final(Md4* md4, byte* hash) -{ - byte* local = (byte*)md4->buffer; - - AddLength(md4, md4->buffLen); /* before adding pads */ - - local[md4->buffLen++] = 0x80; /* add 1 */ - - /* pad with zeros */ - if (md4->buffLen > MD4_PAD_SIZE) { - XMEMSET(&local[md4->buffLen], 0, MD4_BLOCK_SIZE - md4->buffLen); - md4->buffLen += MD4_BLOCK_SIZE - md4->buffLen; - - #ifdef BIG_ENDIAN_ORDER - ByteReverseWords(md4->buffer, md4->buffer, MD4_BLOCK_SIZE); - #endif - Transform(md4); - md4->buffLen = 0; - } - XMEMSET(&local[md4->buffLen], 0, MD4_PAD_SIZE - md4->buffLen); - - /* put lengths in bits */ - md4->hiLen = (md4->loLen >> (8*sizeof(md4->loLen) - 3)) + - (md4->hiLen << 3); - md4->loLen = md4->loLen << 3; - - /* store lengths */ - #ifdef BIG_ENDIAN_ORDER - ByteReverseWords(md4->buffer, md4->buffer, MD4_BLOCK_SIZE); - #endif - /* ! length ordering dependent on digest endian type ! */ - XMEMCPY(&local[MD4_PAD_SIZE], &md4->loLen, sizeof(word32)); - XMEMCPY(&local[MD4_PAD_SIZE + sizeof(word32)], &md4->hiLen, sizeof(word32)); - - Transform(md4); - #ifdef BIG_ENDIAN_ORDER - ByteReverseWords(md4->digest, md4->digest, MD4_DIGEST_SIZE); - #endif - XMEMCPY(hash, md4->digest, MD4_DIGEST_SIZE); - - InitMd4(md4); /* reset state */ -} - - -#endif /* NO_MD4 */ - +//#ifdef HAVE_CONFIG_H +// #include +//#endif +// +//#include +// +//#ifndef NO_MD4 +// +//#include +//#ifdef NO_INLINE +// #include +//#else +// #include +//#endif +// +// +//#ifndef min +// +// static INLINE word32 min(word32 a, word32 b) +// { +// return a > b ? b : a; +// } +// +//#endif /* min */ +// +// +//void InitMd4(Md4* md4) +//{ +// md4->digest[0] = 0x67452301L; +// md4->digest[1] = 0xefcdab89L; +// md4->digest[2] = 0x98badcfeL; +// md4->digest[3] = 0x10325476L; +// +// md4->buffLen = 0; +// md4->loLen = 0; +// md4->hiLen = 0; +//} +// +// +//static void Transform(Md4* md4) +//{ +//#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +//#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) +//#define H(x, y, z) ((x) ^ (y) ^ (z)) +// +// /* Copy context->state[] to working vars */ +// word32 A = md4->digest[0]; +// word32 B = md4->digest[1]; +// word32 C = md4->digest[2]; +// word32 D = md4->digest[3]; +// +//#define function(a,b,c,d,k,s) a=rotlFixed(a+F(b,c,d)+md4->buffer[k],s); +// function(A,B,C,D, 0, 3); +// function(D,A,B,C, 1, 7); +// function(C,D,A,B, 2,11); +// function(B,C,D,A, 3,19); +// function(A,B,C,D, 4, 3); +// function(D,A,B,C, 5, 7); +// function(C,D,A,B, 6,11); +// function(B,C,D,A, 7,19); +// function(A,B,C,D, 8, 3); +// function(D,A,B,C, 9, 7); +// function(C,D,A,B,10,11); +// function(B,C,D,A,11,19); +// function(A,B,C,D,12, 3); +// function(D,A,B,C,13, 7); +// function(C,D,A,B,14,11); +// function(B,C,D,A,15,19); +// +//#undef function +//#define function(a,b,c,d,k,s) \ +// a=rotlFixed(a+G(b,c,d)+md4->buffer[k]+0x5a827999,s); +// +// function(A,B,C,D, 0, 3); +// function(D,A,B,C, 4, 5); +// function(C,D,A,B, 8, 9); +// function(B,C,D,A,12,13); +// function(A,B,C,D, 1, 3); +// function(D,A,B,C, 5, 5); +// function(C,D,A,B, 9, 9); +// function(B,C,D,A,13,13); +// function(A,B,C,D, 2, 3); +// function(D,A,B,C, 6, 5); +// function(C,D,A,B,10, 9); +// function(B,C,D,A,14,13); +// function(A,B,C,D, 3, 3); +// function(D,A,B,C, 7, 5); +// function(C,D,A,B,11, 9); +// function(B,C,D,A,15,13); +// +//#undef function +//#define function(a,b,c,d,k,s) \ +// a=rotlFixed(a+H(b,c,d)+md4->buffer[k]+0x6ed9eba1,s); +// +// function(A,B,C,D, 0, 3); +// function(D,A,B,C, 8, 9); +// function(C,D,A,B, 4,11); +// function(B,C,D,A,12,15); +// function(A,B,C,D, 2, 3); +// function(D,A,B,C,10, 9); +// function(C,D,A,B, 6,11); +// function(B,C,D,A,14,15); +// function(A,B,C,D, 1, 3); +// function(D,A,B,C, 9, 9); +// function(C,D,A,B, 5,11); +// function(B,C,D,A,13,15); +// function(A,B,C,D, 3, 3); +// function(D,A,B,C,11, 9); +// function(C,D,A,B, 7,11); +// function(B,C,D,A,15,15); +// +// /* Add the working vars back into digest state[] */ +// md4->digest[0] += A; +// md4->digest[1] += B; +// md4->digest[2] += C; +// md4->digest[3] += D; +//} +// +// +//static INLINE void AddLength(Md4* md4, word32 len) +//{ +// word32 tmp = md4->loLen; +// if ( (md4->loLen += len) < tmp) +// md4->hiLen++; /* carry low to high */ +//} +// +// +//void Md4Update(Md4* md4, const byte* data, word32 len) +//{ +// /* do block size increments */ +// byte* local = (byte*)md4->buffer; +// +// while (len) { +// word32 add = min(len, MD4_BLOCK_SIZE - md4->buffLen); +// XMEMCPY(&local[md4->buffLen], data, add); +// +// md4->buffLen += add; +// data += add; +// len -= add; +// +// if (md4->buffLen == MD4_BLOCK_SIZE) { +// #ifdef BIG_ENDIAN_ORDER +// ByteReverseWords(md4->buffer, md4->buffer, MD4_BLOCK_SIZE); +// #endif +// Transform(md4); +// AddLength(md4, MD4_BLOCK_SIZE); +// md4->buffLen = 0; +// } +// } +//} +// +// +//void Md4Final(Md4* md4, byte* hash) +//{ +// byte* local = (byte*)md4->buffer; +// +// AddLength(md4, md4->buffLen); /* before adding pads */ +// +// local[md4->buffLen++] = 0x80; /* add 1 */ +// +// /* pad with zeros */ +// if (md4->buffLen > MD4_PAD_SIZE) { +// XMEMSET(&local[md4->buffLen], 0, MD4_BLOCK_SIZE - md4->buffLen); +// md4->buffLen += MD4_BLOCK_SIZE - md4->buffLen; +// +// #ifdef BIG_ENDIAN_ORDER +// ByteReverseWords(md4->buffer, md4->buffer, MD4_BLOCK_SIZE); +// #endif +// Transform(md4); +// md4->buffLen = 0; +// } +// XMEMSET(&local[md4->buffLen], 0, MD4_PAD_SIZE - md4->buffLen); +// +// /* put lengths in bits */ +// md4->hiLen = (md4->loLen >> (8*sizeof(md4->loLen) - 3)) + +// (md4->hiLen << 3); +// md4->loLen = md4->loLen << 3; +// +// /* store lengths */ +// #ifdef BIG_ENDIAN_ORDER +// ByteReverseWords(md4->buffer, md4->buffer, MD4_BLOCK_SIZE); +// #endif +// /* ! length ordering dependent on digest endian type ! */ +// XMEMCPY(&local[MD4_PAD_SIZE], &md4->loLen, sizeof(word32)); +// XMEMCPY(&local[MD4_PAD_SIZE + sizeof(word32)], &md4->hiLen, sizeof(word32)); +// +// Transform(md4); +// #ifdef BIG_ENDIAN_ORDER +// ByteReverseWords(md4->digest, md4->digest, MD4_DIGEST_SIZE); +// #endif +// XMEMCPY(hash, md4->digest, MD4_DIGEST_SIZE); +// +// InitMd4(md4); /* reset state */ +//} +// +// +//#endif /* NO_MD4 */ +// diff --git a/ctaocrypt/src/md5.c b/ctaocrypt/src/md5.c index 4a375391d..08b186527 100644 --- a/ctaocrypt/src/md5.c +++ b/ctaocrypt/src/md5.c @@ -20,372 +20,372 @@ */ -#ifdef HAVE_CONFIG_H - #include -#endif - -#include - -#if !defined(NO_MD5) - -#ifdef CYASSL_PIC32MZ_HASH -#define InitMd5 InitMd5_sw -#define Md5Update Md5Update_sw -#define Md5Final Md5Final_sw -#endif - -#include -#include - -#ifdef NO_INLINE - #include -#else - #include -#endif - -#ifdef FREESCALE_MMCAU - #include "cau_api.h" - #define XTRANSFORM(S,B) cau_md5_hash_n((B), 1, (unsigned char*)(S)->digest) -#else - #define XTRANSFORM(S,B) Transform((S)) -#endif - - -#ifdef STM32F2_HASH - /* - * STM32F2 hardware MD5 support through the STM32F2 standard peripheral - * library. Documentation located in STM32F2xx Standard Peripheral Library - * document (See note in README). - */ - #include "stm32f2xx.h" - - void InitMd5(Md5* md5) - { - /* STM32F2 struct notes: - * md5->buffer = first 4 bytes used to hold partial block if needed - * md5->buffLen = num bytes currently stored in md5->buffer - * md5->loLen = num bytes that have been written to STM32 FIFO - */ - XMEMSET(md5->buffer, 0, MD5_REG_SIZE); - - md5->buffLen = 0; - md5->loLen = 0; - - /* initialize HASH peripheral */ - HASH_DeInit(); - - /* configure algo used, algo mode, datatype */ - HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE); - HASH->CR |= (HASH_AlgoSelection_MD5 | HASH_AlgoMode_HASH - | HASH_DataType_8b); - - /* reset HASH processor */ - HASH->CR |= HASH_CR_INIT; - } - - void Md5Update(Md5* md5, const byte* data, word32 len) - { - word32 i = 0; - word32 fill = 0; - word32 diff = 0; - - /* if saved partial block is available */ - if (md5->buffLen > 0) { - fill = 4 - md5->buffLen; - - /* if enough data to fill, fill and push to FIFO */ - if (fill <= len) { - XMEMCPY((byte*)md5->buffer + md5->buffLen, data, fill); - HASH_DataIn(*(uint32_t*)md5->buffer); - - data += fill; - len -= fill; - md5->loLen += 4; - md5->buffLen = 0; - } else { - /* append partial to existing stored block */ - XMEMCPY((byte*)md5->buffer + md5->buffLen, data, len); - md5->buffLen += len; - return; - } - } - - /* write input block in the IN FIFO */ - for (i = 0; i < len; i += 4) - { - diff = len - i; - if (diff < 4) { - /* store incomplete last block, not yet in FIFO */ - XMEMSET(md5->buffer, 0, MD5_REG_SIZE); - XMEMCPY((byte*)md5->buffer, data, diff); - md5->buffLen = diff; - } else { - HASH_DataIn(*(uint32_t*)data); - data+=4; - } - } - - /* keep track of total data length thus far */ - md5->loLen += (len - md5->buffLen); - } - - void Md5Final(Md5* md5, byte* hash) - { - __IO uint16_t nbvalidbitsdata = 0; - - /* finish reading any trailing bytes into FIFO */ - if (md5->buffLen > 0) { - HASH_DataIn(*(uint32_t*)md5->buffer); - md5->loLen += md5->buffLen; - } - - /* calculate number of valid bits in last word of input data */ - nbvalidbitsdata = 8 * (md5->loLen % MD5_REG_SIZE); - - /* configure number of valid bits in last word of the data */ - HASH_SetLastWordValidBitsNbr(nbvalidbitsdata); - - /* start HASH processor */ - HASH_StartDigest(); - - /* wait until Busy flag == RESET */ - while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {} - - /* read message digest */ - md5->digest[0] = HASH->HR[0]; - md5->digest[1] = HASH->HR[1]; - md5->digest[2] = HASH->HR[2]; - md5->digest[3] = HASH->HR[3]; - - ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE); - - XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE); - - InitMd5(md5); /* reset state */ - } - -#else /* CTaoCrypt software implementation */ - -#ifndef min - - static INLINE word32 min(word32 a, word32 b) - { - return a > b ? b : a; - } - -#endif /* min */ - - -void InitMd5(Md5* md5) -{ - md5->digest[0] = 0x67452301L; - md5->digest[1] = 0xefcdab89L; - md5->digest[2] = 0x98badcfeL; - md5->digest[3] = 0x10325476L; - - md5->buffLen = 0; - md5->loLen = 0; - md5->hiLen = 0; -} - -#ifndef FREESCALE_MMCAU - -static void Transform(Md5* md5) -{ -#define F1(x, y, z) (z ^ (x & (y ^ z))) -#define F2(x, y, z) F1(z, x, y) -#define F3(x, y, z) (x ^ y ^ z) -#define F4(x, y, z) (y ^ (x | ~z)) - -#define MD5STEP(f, w, x, y, z, data, s) \ - w = rotlFixed(w + f(x, y, z) + data, s) + x - - /* Copy context->state[] to working vars */ - word32 a = md5->digest[0]; - word32 b = md5->digest[1]; - word32 c = md5->digest[2]; - word32 d = md5->digest[3]; - - MD5STEP(F1, a, b, c, d, md5->buffer[0] + 0xd76aa478, 7); - MD5STEP(F1, d, a, b, c, md5->buffer[1] + 0xe8c7b756, 12); - MD5STEP(F1, c, d, a, b, md5->buffer[2] + 0x242070db, 17); - MD5STEP(F1, b, c, d, a, md5->buffer[3] + 0xc1bdceee, 22); - MD5STEP(F1, a, b, c, d, md5->buffer[4] + 0xf57c0faf, 7); - MD5STEP(F1, d, a, b, c, md5->buffer[5] + 0x4787c62a, 12); - MD5STEP(F1, c, d, a, b, md5->buffer[6] + 0xa8304613, 17); - MD5STEP(F1, b, c, d, a, md5->buffer[7] + 0xfd469501, 22); - MD5STEP(F1, a, b, c, d, md5->buffer[8] + 0x698098d8, 7); - MD5STEP(F1, d, a, b, c, md5->buffer[9] + 0x8b44f7af, 12); - MD5STEP(F1, c, d, a, b, md5->buffer[10] + 0xffff5bb1, 17); - MD5STEP(F1, b, c, d, a, md5->buffer[11] + 0x895cd7be, 22); - MD5STEP(F1, a, b, c, d, md5->buffer[12] + 0x6b901122, 7); - MD5STEP(F1, d, a, b, c, md5->buffer[13] + 0xfd987193, 12); - MD5STEP(F1, c, d, a, b, md5->buffer[14] + 0xa679438e, 17); - MD5STEP(F1, b, c, d, a, md5->buffer[15] + 0x49b40821, 22); - - MD5STEP(F2, a, b, c, d, md5->buffer[1] + 0xf61e2562, 5); - MD5STEP(F2, d, a, b, c, md5->buffer[6] + 0xc040b340, 9); - MD5STEP(F2, c, d, a, b, md5->buffer[11] + 0x265e5a51, 14); - MD5STEP(F2, b, c, d, a, md5->buffer[0] + 0xe9b6c7aa, 20); - MD5STEP(F2, a, b, c, d, md5->buffer[5] + 0xd62f105d, 5); - MD5STEP(F2, d, a, b, c, md5->buffer[10] + 0x02441453, 9); - MD5STEP(F2, c, d, a, b, md5->buffer[15] + 0xd8a1e681, 14); - MD5STEP(F2, b, c, d, a, md5->buffer[4] + 0xe7d3fbc8, 20); - MD5STEP(F2, a, b, c, d, md5->buffer[9] + 0x21e1cde6, 5); - MD5STEP(F2, d, a, b, c, md5->buffer[14] + 0xc33707d6, 9); - MD5STEP(F2, c, d, a, b, md5->buffer[3] + 0xf4d50d87, 14); - MD5STEP(F2, b, c, d, a, md5->buffer[8] + 0x455a14ed, 20); - MD5STEP(F2, a, b, c, d, md5->buffer[13] + 0xa9e3e905, 5); - MD5STEP(F2, d, a, b, c, md5->buffer[2] + 0xfcefa3f8, 9); - MD5STEP(F2, c, d, a, b, md5->buffer[7] + 0x676f02d9, 14); - MD5STEP(F2, b, c, d, a, md5->buffer[12] + 0x8d2a4c8a, 20); - - MD5STEP(F3, a, b, c, d, md5->buffer[5] + 0xfffa3942, 4); - MD5STEP(F3, d, a, b, c, md5->buffer[8] + 0x8771f681, 11); - MD5STEP(F3, c, d, a, b, md5->buffer[11] + 0x6d9d6122, 16); - MD5STEP(F3, b, c, d, a, md5->buffer[14] + 0xfde5380c, 23); - MD5STEP(F3, a, b, c, d, md5->buffer[1] + 0xa4beea44, 4); - MD5STEP(F3, d, a, b, c, md5->buffer[4] + 0x4bdecfa9, 11); - MD5STEP(F3, c, d, a, b, md5->buffer[7] + 0xf6bb4b60, 16); - MD5STEP(F3, b, c, d, a, md5->buffer[10] + 0xbebfbc70, 23); - MD5STEP(F3, a, b, c, d, md5->buffer[13] + 0x289b7ec6, 4); - MD5STEP(F3, d, a, b, c, md5->buffer[0] + 0xeaa127fa, 11); - MD5STEP(F3, c, d, a, b, md5->buffer[3] + 0xd4ef3085, 16); - MD5STEP(F3, b, c, d, a, md5->buffer[6] + 0x04881d05, 23); - MD5STEP(F3, a, b, c, d, md5->buffer[9] + 0xd9d4d039, 4); - MD5STEP(F3, d, a, b, c, md5->buffer[12] + 0xe6db99e5, 11); - MD5STEP(F3, c, d, a, b, md5->buffer[15] + 0x1fa27cf8, 16); - MD5STEP(F3, b, c, d, a, md5->buffer[2] + 0xc4ac5665, 23); - - MD5STEP(F4, a, b, c, d, md5->buffer[0] + 0xf4292244, 6); - MD5STEP(F4, d, a, b, c, md5->buffer[7] + 0x432aff97, 10); - MD5STEP(F4, c, d, a, b, md5->buffer[14] + 0xab9423a7, 15); - MD5STEP(F4, b, c, d, a, md5->buffer[5] + 0xfc93a039, 21); - MD5STEP(F4, a, b, c, d, md5->buffer[12] + 0x655b59c3, 6); - MD5STEP(F4, d, a, b, c, md5->buffer[3] + 0x8f0ccc92, 10); - MD5STEP(F4, c, d, a, b, md5->buffer[10] + 0xffeff47d, 15); - MD5STEP(F4, b, c, d, a, md5->buffer[1] + 0x85845dd1, 21); - MD5STEP(F4, a, b, c, d, md5->buffer[8] + 0x6fa87e4f, 6); - MD5STEP(F4, d, a, b, c, md5->buffer[15] + 0xfe2ce6e0, 10); - MD5STEP(F4, c, d, a, b, md5->buffer[6] + 0xa3014314, 15); - MD5STEP(F4, b, c, d, a, md5->buffer[13] + 0x4e0811a1, 21); - MD5STEP(F4, a, b, c, d, md5->buffer[4] + 0xf7537e82, 6); - MD5STEP(F4, d, a, b, c, md5->buffer[11] + 0xbd3af235, 10); - MD5STEP(F4, c, d, a, b, md5->buffer[2] + 0x2ad7d2bb, 15); - MD5STEP(F4, b, c, d, a, md5->buffer[9] + 0xeb86d391, 21); - - /* Add the working vars back into digest state[] */ - md5->digest[0] += a; - md5->digest[1] += b; - md5->digest[2] += c; - md5->digest[3] += d; -} - -#endif /* FREESCALE_MMCAU */ - - -static INLINE void AddLength(Md5* md5, word32 len) -{ - word32 tmp = md5->loLen; - if ( (md5->loLen += len) < tmp) - md5->hiLen++; /* carry low to high */ -} - - -void Md5Update(Md5* md5, const byte* data, word32 len) -{ - /* do block size increments */ - byte* local = (byte*)md5->buffer; - - while (len) { - word32 add = min(len, MD5_BLOCK_SIZE - md5->buffLen); - XMEMCPY(&local[md5->buffLen], data, add); - - md5->buffLen += add; - data += add; - len -= add; - - if (md5->buffLen == MD5_BLOCK_SIZE) { - #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) - ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); - #endif - XTRANSFORM(md5, local); - AddLength(md5, MD5_BLOCK_SIZE); - md5->buffLen = 0; - } - } -} - - -void Md5Final(Md5* md5, byte* hash) -{ - byte* local = (byte*)md5->buffer; - - AddLength(md5, md5->buffLen); /* before adding pads */ - - local[md5->buffLen++] = 0x80; /* add 1 */ - - /* pad with zeros */ - if (md5->buffLen > MD5_PAD_SIZE) { - XMEMSET(&local[md5->buffLen], 0, MD5_BLOCK_SIZE - md5->buffLen); - md5->buffLen += MD5_BLOCK_SIZE - md5->buffLen; - - #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) - ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); - #endif - XTRANSFORM(md5, local); - md5->buffLen = 0; - } - XMEMSET(&local[md5->buffLen], 0, MD5_PAD_SIZE - md5->buffLen); - - /* put lengths in bits */ - md5->hiLen = (md5->loLen >> (8*sizeof(md5->loLen) - 3)) + - (md5->hiLen << 3); - md5->loLen = md5->loLen << 3; - - /* store lengths */ - #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) - ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); - #endif - /* ! length ordering dependent on digest endian type ! */ - XMEMCPY(&local[MD5_PAD_SIZE], &md5->loLen, sizeof(word32)); - XMEMCPY(&local[MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32)); - - XTRANSFORM(md5, local); - #ifdef BIG_ENDIAN_ORDER - ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE); - #endif - XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE); - - InitMd5(md5); /* reset state */ -} - -#endif /* STM32F2_HASH */ - - -int Md5Hash(const byte* data, word32 len, byte* hash) -{ -#ifdef CYASSL_SMALL_STACK - Md5* md5; -#else - Md5 md5[1]; -#endif - -#ifdef CYASSL_SMALL_STACK - md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (md5 == NULL) - return MEMORY_E; -#endif - - InitMd5(md5); - Md5Update(md5, data, len); - Md5Final(md5, hash); - -#ifdef CYASSL_SMALL_STACK - XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - return 0; -} - -#endif /* NO_MD5 */ +//#ifdef HAVE_CONFIG_H +// #include +//#endif +// +//#include +// +//#if !defined(NO_MD5) +// +//#ifdef CYASSL_PIC32MZ_HASH +//#define InitMd5 InitMd5_sw +//#define Md5Update Md5Update_sw +//#define Md5Final Md5Final_sw +//#endif +// +//#include +//#include +// +//#ifdef NO_INLINE +// #include +//#else +// #include +//#endif +// +//#ifdef FREESCALE_MMCAU +// #include "cau_api.h" +// #define XTRANSFORM(S,B) cau_md5_hash_n((B), 1, (unsigned char*)(S)->digest) +//#else +// #define XTRANSFORM(S,B) Transform((S)) +//#endif +// +// +//#ifdef STM32F2_HASH +// /* +// * STM32F2 hardware MD5 support through the STM32F2 standard peripheral +// * library. Documentation located in STM32F2xx Standard Peripheral Library +// * document (See note in README). +// */ +// #include "stm32f2xx.h" +// +// void InitMd5(Md5* md5) +// { +// /* STM32F2 struct notes: +// * md5->buffer = first 4 bytes used to hold partial block if needed +// * md5->buffLen = num bytes currently stored in md5->buffer +// * md5->loLen = num bytes that have been written to STM32 FIFO +// */ +// XMEMSET(md5->buffer, 0, MD5_REG_SIZE); +// +// md5->buffLen = 0; +// md5->loLen = 0; +// +// /* initialize HASH peripheral */ +// HASH_DeInit(); +// +// /* configure algo used, algo mode, datatype */ +// HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE); +// HASH->CR |= (HASH_AlgoSelection_MD5 | HASH_AlgoMode_HASH +// | HASH_DataType_8b); +// +// /* reset HASH processor */ +// HASH->CR |= HASH_CR_INIT; +// } +// +// void Md5Update(Md5* md5, const byte* data, word32 len) +// { +// word32 i = 0; +// word32 fill = 0; +// word32 diff = 0; +// +// /* if saved partial block is available */ +// if (md5->buffLen > 0) { +// fill = 4 - md5->buffLen; +// +// /* if enough data to fill, fill and push to FIFO */ +// if (fill <= len) { +// XMEMCPY((byte*)md5->buffer + md5->buffLen, data, fill); +// HASH_DataIn(*(uint32_t*)md5->buffer); +// +// data += fill; +// len -= fill; +// md5->loLen += 4; +// md5->buffLen = 0; +// } else { +// /* append partial to existing stored block */ +// XMEMCPY((byte*)md5->buffer + md5->buffLen, data, len); +// md5->buffLen += len; +// return; +// } +// } +// +// /* write input block in the IN FIFO */ +// for (i = 0; i < len; i += 4) +// { +// diff = len - i; +// if (diff < 4) { +// /* store incomplete last block, not yet in FIFO */ +// XMEMSET(md5->buffer, 0, MD5_REG_SIZE); +// XMEMCPY((byte*)md5->buffer, data, diff); +// md5->buffLen = diff; +// } else { +// HASH_DataIn(*(uint32_t*)data); +// data+=4; +// } +// } +// +// /* keep track of total data length thus far */ +// md5->loLen += (len - md5->buffLen); +// } +// +// void Md5Final(Md5* md5, byte* hash) +// { +// __IO uint16_t nbvalidbitsdata = 0; +// +// /* finish reading any trailing bytes into FIFO */ +// if (md5->buffLen > 0) { +// HASH_DataIn(*(uint32_t*)md5->buffer); +// md5->loLen += md5->buffLen; +// } +// +// /* calculate number of valid bits in last word of input data */ +// nbvalidbitsdata = 8 * (md5->loLen % MD5_REG_SIZE); +// +// /* configure number of valid bits in last word of the data */ +// HASH_SetLastWordValidBitsNbr(nbvalidbitsdata); +// +// /* start HASH processor */ +// HASH_StartDigest(); +// +// /* wait until Busy flag == RESET */ +// while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {} +// +// /* read message digest */ +// md5->digest[0] = HASH->HR[0]; +// md5->digest[1] = HASH->HR[1]; +// md5->digest[2] = HASH->HR[2]; +// md5->digest[3] = HASH->HR[3]; +// +// ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE); +// +// XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE); +// +// InitMd5(md5); /* reset state */ +// } +// +//#else /* CTaoCrypt software implementation */ +// +//#ifndef min +// +// static INLINE word32 min(word32 a, word32 b) +// { +// return a > b ? b : a; +// } +// +//#endif /* min */ +// +// +//void InitMd5(Md5* md5) +//{ +// md5->digest[0] = 0x67452301L; +// md5->digest[1] = 0xefcdab89L; +// md5->digest[2] = 0x98badcfeL; +// md5->digest[3] = 0x10325476L; +// +// md5->buffLen = 0; +// md5->loLen = 0; +// md5->hiLen = 0; +//} +// +//#ifndef FREESCALE_MMCAU +// +//static void Transform(Md5* md5) +//{ +//#define F1(x, y, z) (z ^ (x & (y ^ z))) +//#define F2(x, y, z) F1(z, x, y) +//#define F3(x, y, z) (x ^ y ^ z) +//#define F4(x, y, z) (y ^ (x | ~z)) +// +//#define MD5STEP(f, w, x, y, z, data, s) \ +// w = rotlFixed(w + f(x, y, z) + data, s) + x +// +// /* Copy context->state[] to working vars */ +// word32 a = md5->digest[0]; +// word32 b = md5->digest[1]; +// word32 c = md5->digest[2]; +// word32 d = md5->digest[3]; +// +// MD5STEP(F1, a, b, c, d, md5->buffer[0] + 0xd76aa478, 7); +// MD5STEP(F1, d, a, b, c, md5->buffer[1] + 0xe8c7b756, 12); +// MD5STEP(F1, c, d, a, b, md5->buffer[2] + 0x242070db, 17); +// MD5STEP(F1, b, c, d, a, md5->buffer[3] + 0xc1bdceee, 22); +// MD5STEP(F1, a, b, c, d, md5->buffer[4] + 0xf57c0faf, 7); +// MD5STEP(F1, d, a, b, c, md5->buffer[5] + 0x4787c62a, 12); +// MD5STEP(F1, c, d, a, b, md5->buffer[6] + 0xa8304613, 17); +// MD5STEP(F1, b, c, d, a, md5->buffer[7] + 0xfd469501, 22); +// MD5STEP(F1, a, b, c, d, md5->buffer[8] + 0x698098d8, 7); +// MD5STEP(F1, d, a, b, c, md5->buffer[9] + 0x8b44f7af, 12); +// MD5STEP(F1, c, d, a, b, md5->buffer[10] + 0xffff5bb1, 17); +// MD5STEP(F1, b, c, d, a, md5->buffer[11] + 0x895cd7be, 22); +// MD5STEP(F1, a, b, c, d, md5->buffer[12] + 0x6b901122, 7); +// MD5STEP(F1, d, a, b, c, md5->buffer[13] + 0xfd987193, 12); +// MD5STEP(F1, c, d, a, b, md5->buffer[14] + 0xa679438e, 17); +// MD5STEP(F1, b, c, d, a, md5->buffer[15] + 0x49b40821, 22); +// +// MD5STEP(F2, a, b, c, d, md5->buffer[1] + 0xf61e2562, 5); +// MD5STEP(F2, d, a, b, c, md5->buffer[6] + 0xc040b340, 9); +// MD5STEP(F2, c, d, a, b, md5->buffer[11] + 0x265e5a51, 14); +// MD5STEP(F2, b, c, d, a, md5->buffer[0] + 0xe9b6c7aa, 20); +// MD5STEP(F2, a, b, c, d, md5->buffer[5] + 0xd62f105d, 5); +// MD5STEP(F2, d, a, b, c, md5->buffer[10] + 0x02441453, 9); +// MD5STEP(F2, c, d, a, b, md5->buffer[15] + 0xd8a1e681, 14); +// MD5STEP(F2, b, c, d, a, md5->buffer[4] + 0xe7d3fbc8, 20); +// MD5STEP(F2, a, b, c, d, md5->buffer[9] + 0x21e1cde6, 5); +// MD5STEP(F2, d, a, b, c, md5->buffer[14] + 0xc33707d6, 9); +// MD5STEP(F2, c, d, a, b, md5->buffer[3] + 0xf4d50d87, 14); +// MD5STEP(F2, b, c, d, a, md5->buffer[8] + 0x455a14ed, 20); +// MD5STEP(F2, a, b, c, d, md5->buffer[13] + 0xa9e3e905, 5); +// MD5STEP(F2, d, a, b, c, md5->buffer[2] + 0xfcefa3f8, 9); +// MD5STEP(F2, c, d, a, b, md5->buffer[7] + 0x676f02d9, 14); +// MD5STEP(F2, b, c, d, a, md5->buffer[12] + 0x8d2a4c8a, 20); +// +// MD5STEP(F3, a, b, c, d, md5->buffer[5] + 0xfffa3942, 4); +// MD5STEP(F3, d, a, b, c, md5->buffer[8] + 0x8771f681, 11); +// MD5STEP(F3, c, d, a, b, md5->buffer[11] + 0x6d9d6122, 16); +// MD5STEP(F3, b, c, d, a, md5->buffer[14] + 0xfde5380c, 23); +// MD5STEP(F3, a, b, c, d, md5->buffer[1] + 0xa4beea44, 4); +// MD5STEP(F3, d, a, b, c, md5->buffer[4] + 0x4bdecfa9, 11); +// MD5STEP(F3, c, d, a, b, md5->buffer[7] + 0xf6bb4b60, 16); +// MD5STEP(F3, b, c, d, a, md5->buffer[10] + 0xbebfbc70, 23); +// MD5STEP(F3, a, b, c, d, md5->buffer[13] + 0x289b7ec6, 4); +// MD5STEP(F3, d, a, b, c, md5->buffer[0] + 0xeaa127fa, 11); +// MD5STEP(F3, c, d, a, b, md5->buffer[3] + 0xd4ef3085, 16); +// MD5STEP(F3, b, c, d, a, md5->buffer[6] + 0x04881d05, 23); +// MD5STEP(F3, a, b, c, d, md5->buffer[9] + 0xd9d4d039, 4); +// MD5STEP(F3, d, a, b, c, md5->buffer[12] + 0xe6db99e5, 11); +// MD5STEP(F3, c, d, a, b, md5->buffer[15] + 0x1fa27cf8, 16); +// MD5STEP(F3, b, c, d, a, md5->buffer[2] + 0xc4ac5665, 23); +// +// MD5STEP(F4, a, b, c, d, md5->buffer[0] + 0xf4292244, 6); +// MD5STEP(F4, d, a, b, c, md5->buffer[7] + 0x432aff97, 10); +// MD5STEP(F4, c, d, a, b, md5->buffer[14] + 0xab9423a7, 15); +// MD5STEP(F4, b, c, d, a, md5->buffer[5] + 0xfc93a039, 21); +// MD5STEP(F4, a, b, c, d, md5->buffer[12] + 0x655b59c3, 6); +// MD5STEP(F4, d, a, b, c, md5->buffer[3] + 0x8f0ccc92, 10); +// MD5STEP(F4, c, d, a, b, md5->buffer[10] + 0xffeff47d, 15); +// MD5STEP(F4, b, c, d, a, md5->buffer[1] + 0x85845dd1, 21); +// MD5STEP(F4, a, b, c, d, md5->buffer[8] + 0x6fa87e4f, 6); +// MD5STEP(F4, d, a, b, c, md5->buffer[15] + 0xfe2ce6e0, 10); +// MD5STEP(F4, c, d, a, b, md5->buffer[6] + 0xa3014314, 15); +// MD5STEP(F4, b, c, d, a, md5->buffer[13] + 0x4e0811a1, 21); +// MD5STEP(F4, a, b, c, d, md5->buffer[4] + 0xf7537e82, 6); +// MD5STEP(F4, d, a, b, c, md5->buffer[11] + 0xbd3af235, 10); +// MD5STEP(F4, c, d, a, b, md5->buffer[2] + 0x2ad7d2bb, 15); +// MD5STEP(F4, b, c, d, a, md5->buffer[9] + 0xeb86d391, 21); +// +// /* Add the working vars back into digest state[] */ +// md5->digest[0] += a; +// md5->digest[1] += b; +// md5->digest[2] += c; +// md5->digest[3] += d; +//} +// +//#endif /* FREESCALE_MMCAU */ +// +// +//static INLINE void AddLength(Md5* md5, word32 len) +//{ +// word32 tmp = md5->loLen; +// if ( (md5->loLen += len) < tmp) +// md5->hiLen++; /* carry low to high */ +//} +// +// +//void Md5Update(Md5* md5, const byte* data, word32 len) +//{ +// /* do block size increments */ +// byte* local = (byte*)md5->buffer; +// +// while (len) { +// word32 add = min(len, MD5_BLOCK_SIZE - md5->buffLen); +// XMEMCPY(&local[md5->buffLen], data, add); +// +// md5->buffLen += add; +// data += add; +// len -= add; +// +// if (md5->buffLen == MD5_BLOCK_SIZE) { +// #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) +// ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); +// #endif +// XTRANSFORM(md5, local); +// AddLength(md5, MD5_BLOCK_SIZE); +// md5->buffLen = 0; +// } +// } +//} +// +// +//void Md5Final(Md5* md5, byte* hash) +//{ +// byte* local = (byte*)md5->buffer; +// +// AddLength(md5, md5->buffLen); /* before adding pads */ +// +// local[md5->buffLen++] = 0x80; /* add 1 */ +// +// /* pad with zeros */ +// if (md5->buffLen > MD5_PAD_SIZE) { +// XMEMSET(&local[md5->buffLen], 0, MD5_BLOCK_SIZE - md5->buffLen); +// md5->buffLen += MD5_BLOCK_SIZE - md5->buffLen; +// +// #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) +// ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); +// #endif +// XTRANSFORM(md5, local); +// md5->buffLen = 0; +// } +// XMEMSET(&local[md5->buffLen], 0, MD5_PAD_SIZE - md5->buffLen); +// +// /* put lengths in bits */ +// md5->hiLen = (md5->loLen >> (8*sizeof(md5->loLen) - 3)) + +// (md5->hiLen << 3); +// md5->loLen = md5->loLen << 3; +// +// /* store lengths */ +// #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) +// ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); +// #endif +// /* ! length ordering dependent on digest endian type ! */ +// XMEMCPY(&local[MD5_PAD_SIZE], &md5->loLen, sizeof(word32)); +// XMEMCPY(&local[MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32)); +// +// XTRANSFORM(md5, local); +// #ifdef BIG_ENDIAN_ORDER +// ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE); +// #endif +// XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE); +// +// InitMd5(md5); /* reset state */ +//} +// +//#endif /* STM32F2_HASH */ +// +// +//int Md5Hash(const byte* data, word32 len, byte* hash) +//{ +//#ifdef CYASSL_SMALL_STACK +// Md5* md5; +//#else +// Md5 md5[1]; +//#endif +// +//#ifdef CYASSL_SMALL_STACK +// md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (md5 == NULL) +// return MEMORY_E; +//#endif +// +// InitMd5(md5); +// Md5Update(md5, data, len); +// Md5Final(md5, hash); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// return 0; +//} +// +//#endif /* NO_MD5 */ diff --git a/ctaocrypt/src/pkcs7.c b/ctaocrypt/src/pkcs7.c index 772492656..1809f04bc 100644 --- a/ctaocrypt/src/pkcs7.c +++ b/ctaocrypt/src/pkcs7.c @@ -19,1831 +19,1831 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifdef HAVE_CONFIG_H - #include -#endif - -#include - -#ifdef HAVE_PKCS7 - -#include -#include -#include - -#ifndef min - static INLINE word32 min(word32 a, word32 b) - { - return a > b ? b : a; - } -#endif - - -/* placed ASN.1 contentType OID into *output, return idx on success, - * 0 upon failure */ -CYASSL_LOCAL int SetContentType(int pkcs7TypeOID, byte* output) -{ - /* PKCS#7 content types, RFC 2315, section 14 */ - static const byte pkcs7[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, - 0x0D, 0x01, 0x07 }; - static const byte data[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, - 0x0D, 0x01, 0x07, 0x01 }; - static const byte signedData[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, - 0x0D, 0x01, 0x07, 0x02}; - static const byte envelopedData[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, - 0x0D, 0x01, 0x07, 0x03 }; - static const byte signedAndEnveloped[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, - 0x0D, 0x01, 0x07, 0x04 }; - static const byte digestedData[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, - 0x0D, 0x01, 0x07, 0x05 }; - static const byte encryptedData[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, - 0x0D, 0x01, 0x07, 0x06 }; - - int idSz; - int typeSz = 0, idx = 0; - const byte* typeName = 0; - byte ID_Length[MAX_LENGTH_SZ]; - - switch (pkcs7TypeOID) { - case PKCS7_MSG: - typeSz = sizeof(pkcs7); - typeName = pkcs7; - break; - - case DATA: - typeSz = sizeof(data); - typeName = data; - break; - - case SIGNED_DATA: - typeSz = sizeof(signedData); - typeName = signedData; - break; - - case ENVELOPED_DATA: - typeSz = sizeof(envelopedData); - typeName = envelopedData; - break; - - case SIGNED_AND_ENVELOPED_DATA: - typeSz = sizeof(signedAndEnveloped); - typeName = signedAndEnveloped; - break; - - case DIGESTED_DATA: - typeSz = sizeof(digestedData); - typeName = digestedData; - break; - - case ENCRYPTED_DATA: - typeSz = sizeof(encryptedData); - typeName = encryptedData; - break; - - default: - CYASSL_MSG("Unknown PKCS#7 Type"); - return 0; - }; - - idSz = SetLength(typeSz, ID_Length); - output[idx++] = ASN_OBJECT_ID; - XMEMCPY(output + idx, ID_Length, idSz); - idx += idSz; - XMEMCPY(output + idx, typeName, typeSz); - idx += typeSz; - - return idx; - -} - - -/* get ASN.1 contentType OID sum, return 0 on success, <0 on failure */ -int GetContentType(const byte* input, word32* inOutIdx, word32* oid, - word32 maxIdx) -{ - int length; - word32 i = *inOutIdx; - byte b; - *oid = 0; - - CYASSL_ENTER("GetContentType"); - - b = input[i++]; - if (b != ASN_OBJECT_ID) - return ASN_OBJECT_ID_E; - - if (GetLength(input, &i, &length, maxIdx) < 0) - return ASN_PARSE_E; - - while(length--) { - *oid += input[i]; - i++; - } - - *inOutIdx = i; - - return 0; -} - - -/* init PKCS7 struct with recipient cert, decode into DecodedCert */ -int PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz) -{ - int ret = 0; - - XMEMSET(pkcs7, 0, sizeof(PKCS7)); - if (cert != NULL && certSz > 0) { -#ifdef CYASSL_SMALL_STACK - DecodedCert* dCert; - - dCert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (dCert == NULL) - return MEMORY_E; -#else - DecodedCert stack_dCert; - DecodedCert* dCert = &stack_dCert; -#endif - - pkcs7->singleCert = cert; - pkcs7->singleCertSz = certSz; - InitDecodedCert(dCert, cert, certSz, 0); - - ret = ParseCert(dCert, CA_TYPE, NO_VERIFY, 0); - if (ret < 0) { - FreeDecodedCert(dCert); -#ifdef CYASSL_SMALL_STACK - XFREE(dCert, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - - XMEMCPY(pkcs7->publicKey, dCert->publicKey, dCert->pubKeySize); - pkcs7->publicKeySz = dCert->pubKeySize; - XMEMCPY(pkcs7->issuerHash, dCert->issuerHash, SHA_SIZE); - pkcs7->issuer = dCert->issuerRaw; - pkcs7->issuerSz = dCert->issuerRawLen; - XMEMCPY(pkcs7->issuerSn, dCert->serial, dCert->serialSz); - pkcs7->issuerSnSz = dCert->serialSz; - FreeDecodedCert(dCert); - -#ifdef CYASSL_SMALL_STACK - XFREE(dCert, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - } - - return ret; -} - - -/* releases any memory allocated by a PKCS7 initializer */ -void PKCS7_Free(PKCS7* pkcs7) -{ - (void)pkcs7; -} - - -/* build PKCS#7 data content type */ -int PKCS7_EncodeData(PKCS7* pkcs7, byte* output, word32 outputSz) -{ - static const byte oid[] = - { ASN_OBJECT_ID, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, - 0x07, 0x01 }; - byte seq[MAX_SEQ_SZ]; - byte octetStr[MAX_OCTET_STR_SZ]; - word32 seqSz; - word32 octetStrSz; - word32 oidSz = (word32)sizeof(oid); - int idx = 0; - - octetStrSz = SetOctetString(pkcs7->contentSz, octetStr); - seqSz = SetSequence(pkcs7->contentSz + octetStrSz + oidSz, seq); - - if (outputSz < pkcs7->contentSz + octetStrSz + oidSz + seqSz) - return BUFFER_E; - - XMEMCPY(output, seq, seqSz); - idx += seqSz; - XMEMCPY(output + idx, oid, oidSz); - idx += oidSz; - XMEMCPY(output + idx, octetStr, octetStrSz); - idx += octetStrSz; - XMEMCPY(output + idx, pkcs7->content, pkcs7->contentSz); - idx += pkcs7->contentSz; - - return idx; -} - - -typedef struct EncodedAttrib { - byte valueSeq[MAX_SEQ_SZ]; - const byte* oid; - byte valueSet[MAX_SET_SZ]; - const byte* value; - word32 valueSeqSz, oidSz, idSz, valueSetSz, valueSz, totalSz; -} EncodedAttrib; - - -typedef struct ESD { - Sha sha; - byte contentDigest[SHA_DIGEST_SIZE + 2]; /* content only + ASN.1 heading */ - byte contentAttribsDigest[SHA_DIGEST_SIZE]; - byte encContentDigest[512]; - - byte outerSeq[MAX_SEQ_SZ]; - byte outerContent[MAX_EXP_SZ]; - byte innerSeq[MAX_SEQ_SZ]; - byte version[MAX_VERSION_SZ]; - byte digAlgoIdSet[MAX_SET_SZ]; - byte singleDigAlgoId[MAX_ALGO_SZ]; - - byte contentInfoSeq[MAX_SEQ_SZ]; - byte innerContSeq[MAX_EXP_SZ]; - byte innerOctets[MAX_OCTET_STR_SZ]; - - byte certsSet[MAX_SET_SZ]; - - byte signerInfoSet[MAX_SET_SZ]; - byte signerInfoSeq[MAX_SEQ_SZ]; - byte signerVersion[MAX_VERSION_SZ]; - byte issuerSnSeq[MAX_SEQ_SZ]; - byte issuerName[MAX_SEQ_SZ]; - byte issuerSn[MAX_SN_SZ]; - byte signerDigAlgoId[MAX_ALGO_SZ]; - byte digEncAlgoId[MAX_ALGO_SZ]; - byte signedAttribSet[MAX_SET_SZ]; - EncodedAttrib signedAttribs[6]; - byte signerDigest[MAX_OCTET_STR_SZ]; - word32 innerOctetsSz, innerContSeqSz, contentInfoSeqSz; - word32 outerSeqSz, outerContentSz, innerSeqSz, versionSz, digAlgoIdSetSz, - singleDigAlgoIdSz, certsSetSz; - word32 signerInfoSetSz, signerInfoSeqSz, signerVersionSz, - issuerSnSeqSz, issuerNameSz, issuerSnSz, - signerDigAlgoIdSz, digEncAlgoIdSz, signerDigestSz; - word32 encContentDigestSz, signedAttribsSz, signedAttribsCount, - signedAttribSetSz; -} ESD; - - -static int EncodeAttributes(EncodedAttrib* ea, int eaSz, - PKCS7Attrib* attribs, int attribsSz) -{ - int i; - int maxSz = min(eaSz, attribsSz); - int allAttribsSz = 0; - - for (i = 0; i < maxSz; i++) - { - int attribSz = 0; - - ea[i].value = attribs[i].value; - ea[i].valueSz = attribs[i].valueSz; - attribSz += ea[i].valueSz; - ea[i].valueSetSz = SetSet(attribSz, ea[i].valueSet); - attribSz += ea[i].valueSetSz; - ea[i].oid = attribs[i].oid; - ea[i].oidSz = attribs[i].oidSz; - attribSz += ea[i].oidSz; - ea[i].valueSeqSz = SetSequence(attribSz, ea[i].valueSeq); - attribSz += ea[i].valueSeqSz; - ea[i].totalSz = attribSz; - - allAttribsSz += attribSz; - } - return allAttribsSz; -} - - -static int FlattenAttributes(byte* output, EncodedAttrib* ea, int eaSz) -{ - int i, idx; - - idx = 0; - for (i = 0; i < eaSz; i++) { - XMEMCPY(output + idx, ea[i].valueSeq, ea[i].valueSeqSz); - idx += ea[i].valueSeqSz; - XMEMCPY(output + idx, ea[i].oid, ea[i].oidSz); - idx += ea[i].oidSz; - XMEMCPY(output + idx, ea[i].valueSet, ea[i].valueSetSz); - idx += ea[i].valueSetSz; - XMEMCPY(output + idx, ea[i].value, ea[i].valueSz); - idx += ea[i].valueSz; - } - return 0; -} - - -/* build PKCS#7 signedData content type */ -int PKCS7_EncodeSignedData(PKCS7* pkcs7, byte* output, word32 outputSz) -{ - static const byte outerOid[] = - { ASN_OBJECT_ID, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, - 0x07, 0x02 }; - static const byte innerOid[] = - { ASN_OBJECT_ID, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, - 0x07, 0x01 }; - -#ifdef CYASSL_SMALL_STACK - ESD* esd = NULL; -#else - ESD stack_esd; - ESD* esd = &stack_esd; -#endif - - word32 signerInfoSz = 0; - word32 totalSz = 0; - int idx = 0, ret = 0; - byte* flatSignedAttribs = NULL; - word32 flatSignedAttribsSz = 0; - word32 innerOidSz = sizeof(innerOid); - word32 outerOidSz = sizeof(outerOid); - - if (pkcs7 == NULL || pkcs7->content == NULL || pkcs7->contentSz == 0 || - pkcs7->encryptOID == 0 || pkcs7->hashOID == 0 || pkcs7->rng == 0 || - pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0 || - pkcs7->privateKey == NULL || pkcs7->privateKeySz == 0 || - output == NULL || outputSz == 0) - return BAD_FUNC_ARG; - -#ifdef CYASSL_SMALL_STACK - esd = (ESD*)XMALLOC(sizeof(ESD), NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (esd == NULL) - return MEMORY_E; -#endif - - XMEMSET(esd, 0, sizeof(ESD)); - ret = InitSha(&esd->sha); - if (ret != 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - - if (pkcs7->contentSz != 0) - { - ShaUpdate(&esd->sha, pkcs7->content, pkcs7->contentSz); - esd->contentDigest[0] = ASN_OCTET_STRING; - esd->contentDigest[1] = SHA_DIGEST_SIZE; - ShaFinal(&esd->sha, &esd->contentDigest[2]); - } - - esd->innerOctetsSz = SetOctetString(pkcs7->contentSz, esd->innerOctets); - esd->innerContSeqSz = SetExplicit(0, esd->innerOctetsSz + pkcs7->contentSz, - esd->innerContSeq); - esd->contentInfoSeqSz = SetSequence(pkcs7->contentSz + esd->innerOctetsSz + - innerOidSz + esd->innerContSeqSz, - esd->contentInfoSeq); - - esd->issuerSnSz = SetSerialNumber(pkcs7->issuerSn, pkcs7->issuerSnSz, - esd->issuerSn); - signerInfoSz += esd->issuerSnSz; - esd->issuerNameSz = SetSequence(pkcs7->issuerSz, esd->issuerName); - signerInfoSz += esd->issuerNameSz + pkcs7->issuerSz; - esd->issuerSnSeqSz = SetSequence(signerInfoSz, esd->issuerSnSeq); - signerInfoSz += esd->issuerSnSeqSz; - esd->signerVersionSz = SetMyVersion(1, esd->signerVersion, 0); - signerInfoSz += esd->signerVersionSz; - esd->signerDigAlgoIdSz = SetAlgoID(pkcs7->hashOID, esd->signerDigAlgoId, - hashType, 0); - signerInfoSz += esd->signerDigAlgoIdSz; - esd->digEncAlgoIdSz = SetAlgoID(pkcs7->encryptOID, esd->digEncAlgoId, - keyType, 0); - signerInfoSz += esd->digEncAlgoIdSz; - - if (pkcs7->signedAttribsSz != 0) { - byte contentTypeOid[] = - { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, - 0x09, 0x03 }; - byte contentType[] = - { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, - 0x07, 0x01 }; - byte messageDigestOid[] = - { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, - 0x09, 0x04 }; - - PKCS7Attrib cannedAttribs[2] = - { - { contentTypeOid, sizeof(contentTypeOid), - contentType, sizeof(contentType) }, - { messageDigestOid, sizeof(messageDigestOid), - esd->contentDigest, sizeof(esd->contentDigest) } - }; - word32 cannedAttribsCount = sizeof(cannedAttribs)/sizeof(PKCS7Attrib); - - esd->signedAttribsCount += cannedAttribsCount; - esd->signedAttribsSz += EncodeAttributes(&esd->signedAttribs[0], 2, - cannedAttribs, cannedAttribsCount); - - esd->signedAttribsCount += pkcs7->signedAttribsSz; - esd->signedAttribsSz += EncodeAttributes(&esd->signedAttribs[2], 4, - pkcs7->signedAttribs, pkcs7->signedAttribsSz); - - flatSignedAttribs = (byte*)XMALLOC(esd->signedAttribsSz, 0, NULL); - flatSignedAttribsSz = esd->signedAttribsSz; - if (flatSignedAttribs == NULL) { -#ifdef CYASSL_SMALL_STACK - XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return MEMORY_E; - } - FlattenAttributes(flatSignedAttribs, - esd->signedAttribs, esd->signedAttribsCount); - esd->signedAttribSetSz = SetImplicit(ASN_SET, 0, esd->signedAttribsSz, - esd->signedAttribSet); - } - /* Calculate the final hash and encrypt it. */ - { - int result; - word32 scratch = 0; - -#ifdef CYASSL_SMALL_STACK - byte* digestInfo; - RsaKey* privKey; -#else - RsaKey stack_privKey; - RsaKey* privKey = &stack_privKey; - byte digestInfo[MAX_SEQ_SZ + MAX_ALGO_SZ + - MAX_OCTET_STR_SZ + SHA_DIGEST_SIZE]; -#endif - byte digestInfoSeq[MAX_SEQ_SZ]; - byte digestStr[MAX_OCTET_STR_SZ]; - word32 digestInfoSeqSz, digestStrSz; - int digIdx = 0; - - if (pkcs7->signedAttribsSz != 0) { - byte attribSet[MAX_SET_SZ]; - word32 attribSetSz; - - attribSetSz = SetSet(flatSignedAttribsSz, attribSet); - - ret = InitSha(&esd->sha); - if (ret < 0) { - XFREE(flatSignedAttribs, 0, NULL); -#ifdef CYASSL_SMALL_STACK - XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - ShaUpdate(&esd->sha, attribSet, attribSetSz); - ShaUpdate(&esd->sha, flatSignedAttribs, flatSignedAttribsSz); - } - ShaFinal(&esd->sha, esd->contentAttribsDigest); - - digestStrSz = SetOctetString(SHA_DIGEST_SIZE, digestStr); - digestInfoSeqSz = SetSequence(esd->signerDigAlgoIdSz + - digestStrSz + SHA_DIGEST_SIZE, - digestInfoSeq); - -#ifdef CYASSL_SMALL_STACK - digestInfo = (byte*)XMALLOC(MAX_SEQ_SZ + MAX_ALGO_SZ + - MAX_OCTET_STR_SZ + SHA_DIGEST_SIZE, - NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (digestInfo == NULL) { - if (pkcs7->signedAttribsSz != 0) - XFREE(flatSignedAttribs, 0, NULL); - XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); - return MEMORY_E; - } -#endif - - XMEMCPY(digestInfo + digIdx, digestInfoSeq, digestInfoSeqSz); - digIdx += digestInfoSeqSz; - XMEMCPY(digestInfo + digIdx, - esd->signerDigAlgoId, esd->signerDigAlgoIdSz); - digIdx += esd->signerDigAlgoIdSz; - XMEMCPY(digestInfo + digIdx, digestStr, digestStrSz); - digIdx += digestStrSz; - XMEMCPY(digestInfo + digIdx, esd->contentAttribsDigest, - SHA_DIGEST_SIZE); - digIdx += SHA_DIGEST_SIZE; - -#ifdef CYASSL_SMALL_STACK - privKey = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (privKey == NULL) { - if (pkcs7->signedAttribsSz != 0) - XFREE(flatSignedAttribs, 0, NULL); - XFREE(digestInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); - return MEMORY_E; - } -#endif - - result = InitRsaKey(privKey, NULL); - if (result == 0) - result = RsaPrivateKeyDecode(pkcs7->privateKey, &scratch, privKey, - pkcs7->privateKeySz); - if (result < 0) { - if (pkcs7->signedAttribsSz != 0) - XFREE(flatSignedAttribs, 0, NULL); -#ifdef CYASSL_SMALL_STACK - XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(digestInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return PUBLIC_KEY_E; - } - - result = RsaSSL_Sign(digestInfo, digIdx, - esd->encContentDigest, - sizeof(esd->encContentDigest), - privKey, pkcs7->rng); - - FreeRsaKey(privKey); - -#ifdef CYASSL_SMALL_STACK - XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(digestInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - if (result < 0) { - if (pkcs7->signedAttribsSz != 0) - XFREE(flatSignedAttribs, 0, NULL); -#ifdef CYASSL_SMALL_STACK - XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return result; - } - esd->encContentDigestSz = (word32)result; - } - signerInfoSz += flatSignedAttribsSz + esd->signedAttribSetSz; - - esd->signerDigestSz = SetOctetString(esd->encContentDigestSz, - esd->signerDigest); - signerInfoSz += esd->signerDigestSz + esd->encContentDigestSz; - - esd->signerInfoSeqSz = SetSequence(signerInfoSz, esd->signerInfoSeq); - signerInfoSz += esd->signerInfoSeqSz; - esd->signerInfoSetSz = SetSet(signerInfoSz, esd->signerInfoSet); - signerInfoSz += esd->signerInfoSetSz; - - esd->certsSetSz = SetImplicit(ASN_SET, 0, pkcs7->singleCertSz, - esd->certsSet); - - esd->singleDigAlgoIdSz = SetAlgoID(pkcs7->hashOID, esd->singleDigAlgoId, - hashType, 0); - esd->digAlgoIdSetSz = SetSet(esd->singleDigAlgoIdSz, esd->digAlgoIdSet); - - - esd->versionSz = SetMyVersion(1, esd->version, 0); - - totalSz = esd->versionSz + esd->singleDigAlgoIdSz + esd->digAlgoIdSetSz + - esd->contentInfoSeqSz + esd->certsSetSz + pkcs7->singleCertSz + - esd->innerOctetsSz + esd->innerContSeqSz + - innerOidSz + pkcs7->contentSz + - signerInfoSz; - esd->innerSeqSz = SetSequence(totalSz, esd->innerSeq); - totalSz += esd->innerSeqSz; - esd->outerContentSz = SetExplicit(0, totalSz, esd->outerContent); - totalSz += esd->outerContentSz + outerOidSz; - esd->outerSeqSz = SetSequence(totalSz, esd->outerSeq); - totalSz += esd->outerSeqSz; - - if (outputSz < totalSz) { - if (pkcs7->signedAttribsSz != 0) - XFREE(flatSignedAttribs, 0, NULL); -#ifdef CYASSL_SMALL_STACK - XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return BUFFER_E; - } - - idx = 0; - XMEMCPY(output + idx, esd->outerSeq, esd->outerSeqSz); - idx += esd->outerSeqSz; - XMEMCPY(output + idx, outerOid, outerOidSz); - idx += outerOidSz; - XMEMCPY(output + idx, esd->outerContent, esd->outerContentSz); - idx += esd->outerContentSz; - XMEMCPY(output + idx, esd->innerSeq, esd->innerSeqSz); - idx += esd->innerSeqSz; - XMEMCPY(output + idx, esd->version, esd->versionSz); - idx += esd->versionSz; - XMEMCPY(output + idx, esd->digAlgoIdSet, esd->digAlgoIdSetSz); - idx += esd->digAlgoIdSetSz; - XMEMCPY(output + idx, esd->singleDigAlgoId, esd->singleDigAlgoIdSz); - idx += esd->singleDigAlgoIdSz; - XMEMCPY(output + idx, esd->contentInfoSeq, esd->contentInfoSeqSz); - idx += esd->contentInfoSeqSz; - XMEMCPY(output + idx, innerOid, innerOidSz); - idx += innerOidSz; - XMEMCPY(output + idx, esd->innerContSeq, esd->innerContSeqSz); - idx += esd->innerContSeqSz; - XMEMCPY(output + idx, esd->innerOctets, esd->innerOctetsSz); - idx += esd->innerOctetsSz; - XMEMCPY(output + idx, pkcs7->content, pkcs7->contentSz); - idx += pkcs7->contentSz; - XMEMCPY(output + idx, esd->certsSet, esd->certsSetSz); - idx += esd->certsSetSz; - XMEMCPY(output + idx, pkcs7->singleCert, pkcs7->singleCertSz); - idx += pkcs7->singleCertSz; - XMEMCPY(output + idx, esd->signerInfoSet, esd->signerInfoSetSz); - idx += esd->signerInfoSetSz; - XMEMCPY(output + idx, esd->signerInfoSeq, esd->signerInfoSeqSz); - idx += esd->signerInfoSeqSz; - XMEMCPY(output + idx, esd->signerVersion, esd->signerVersionSz); - idx += esd->signerVersionSz; - XMEMCPY(output + idx, esd->issuerSnSeq, esd->issuerSnSeqSz); - idx += esd->issuerSnSeqSz; - XMEMCPY(output + idx, esd->issuerName, esd->issuerNameSz); - idx += esd->issuerNameSz; - XMEMCPY(output + idx, pkcs7->issuer, pkcs7->issuerSz); - idx += pkcs7->issuerSz; - XMEMCPY(output + idx, esd->issuerSn, esd->issuerSnSz); - idx += esd->issuerSnSz; - XMEMCPY(output + idx, esd->signerDigAlgoId, esd->signerDigAlgoIdSz); - idx += esd->signerDigAlgoIdSz; - - /* SignerInfo:Attributes */ - if (pkcs7->signedAttribsSz != 0) { - XMEMCPY(output + idx, esd->signedAttribSet, esd->signedAttribSetSz); - idx += esd->signedAttribSetSz; - XMEMCPY(output + idx, flatSignedAttribs, flatSignedAttribsSz); - idx += flatSignedAttribsSz; - XFREE(flatSignedAttribs, 0, NULL); - } - - XMEMCPY(output + idx, esd->digEncAlgoId, esd->digEncAlgoIdSz); - idx += esd->digEncAlgoIdSz; - XMEMCPY(output + idx, esd->signerDigest, esd->signerDigestSz); - idx += esd->signerDigestSz; - XMEMCPY(output + idx, esd->encContentDigest, esd->encContentDigestSz); - idx += esd->encContentDigestSz; - -#ifdef CYASSL_SMALL_STACK - XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - return idx; -} - - -/* Finds the certificates in the message and saves it. */ -int PKCS7_VerifySignedData(PKCS7* pkcs7, byte* pkiMsg, word32 pkiMsgSz) -{ - word32 idx, contentType; - int length, version, ret; - byte* content = NULL; - byte* sig = NULL; - byte* cert = NULL; - int contentSz = 0, sigSz = 0, certSz = 0; - - if (pkcs7 == NULL || pkiMsg == NULL || pkiMsgSz == 0) - return BAD_FUNC_ARG; - - idx = 0; - - /* Get the contentInfo sequence */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Get the contentInfo contentType */ - if (GetContentType(pkiMsg, &idx, &contentType, pkiMsgSz) < 0) - return ASN_PARSE_E; - - if (contentType != SIGNED_DATA) { - CYASSL_MSG("PKCS#7 input not of type SignedData"); - return PKCS7_OID_E; - } - - /* get the ContentInfo content */ - if (pkiMsg[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) - return ASN_PARSE_E; - - if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Get the signedData sequence */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Get the version */ - if (GetMyVersion(pkiMsg, &idx, &version) < 0) - return ASN_PARSE_E; - - if (version != 1) { - CYASSL_MSG("PKCS#7 signedData needs to be of version 1"); - return ASN_VERSION_E; - } - - /* Get the set of DigestAlgorithmIdentifiers */ - if (GetSet(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Skip the set. */ - idx += length; - - /* Get the inner ContentInfo sequence */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Get the inner ContentInfo contentType */ - if (GetContentType(pkiMsg, &idx, &contentType, pkiMsgSz) < 0) - return ASN_PARSE_E; - - if (contentType != DATA) { - CYASSL_MSG("PKCS#7 inner input not of type Data"); - return PKCS7_OID_E; - } - - if (pkiMsg[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) - return ASN_PARSE_E; - - if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - if (pkiMsg[idx++] != ASN_OCTET_STRING) - return ASN_PARSE_E; - - if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Save the inner data as the content. */ - if (length > 0) { - /* Local pointer for calculating hashes later */ - pkcs7->content = content = &pkiMsg[idx]; - pkcs7->contentSz = contentSz = length; - idx += length; - } - - /* Get the implicit[0] set of certificates */ - if (pkiMsg[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) { - idx++; - if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - if (length > 0) { - /* At this point, idx is at the first certificate in - * a set of certificates. There may be more than one, - * or none, or they may be a PKCS 6 extended - * certificate. We want to save the first cert if it - * is X.509. */ - - word32 certIdx = idx; - - if (pkiMsg[certIdx++] == (ASN_CONSTRUCTED | ASN_SEQUENCE)) { - if (GetLength(pkiMsg, &certIdx, &certSz, pkiMsgSz) < 0) - return ASN_PARSE_E; - - cert = &pkiMsg[idx]; - certSz += (certIdx - idx); - } - PKCS7_InitWithCert(pkcs7, cert, certSz); - } - idx += length; - } - - /* Get the implicit[1] set of crls */ - if (pkiMsg[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1)) { - idx++; - if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Skip the set */ - idx += length; - } - - /* Get the set of signerInfos */ - if (GetSet(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - if (length > 0) { - /* Get the sequence of the first signerInfo */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Get the version */ - if (GetMyVersion(pkiMsg, &idx, &version) < 0) - return ASN_PARSE_E; - - if (version != 1) { - CYASSL_MSG("PKCS#7 signerInfo needs to be of version 1"); - return ASN_VERSION_E; - } - - /* Get the sequence of IssuerAndSerialNumber */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Skip it */ - idx += length; - - /* Get the sequence of digestAlgorithm */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Skip it */ - idx += length; - - /* Get the IMPLICIT[0] SET OF signedAttributes */ - if (pkiMsg[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) { - idx++; - - if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - idx += length; - } - - /* Get the sequence of digestEncryptionAlgorithm */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* Skip it */ - idx += length; - - /* Get the signature */ - if (pkiMsg[idx] == ASN_OCTET_STRING) { - idx++; - - if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* save pointer and length */ - sig = &pkiMsg[idx]; - sigSz = length; - - idx += length; - } - - pkcs7->content = content; - pkcs7->contentSz = contentSz; - - { - word32 scratch = 0; - int plainSz = 0; - int digestSz = MAX_SEQ_SZ + MAX_ALGO_SZ + - MAX_OCTET_STR_SZ + SHA_DIGEST_SIZE; - -#ifdef CYASSL_SMALL_STACK - byte* digest; - RsaKey* key; - - digest = (byte*)XMALLOC(digestSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); - - if (digest == NULL) - return MEMORY_E; - - key = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (key == NULL) { - XFREE(digest, NULL, DYNAMIC_TYPE_TMP_BUFFER); - return MEMORY_E; - } -#else - byte digest[digestSz]; - RsaKey stack_key; - RsaKey* key = &stack_key; -#endif - - XMEMSET(digest, 0, digestSz); - - ret = InitRsaKey(key, NULL); - if (ret != 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(digest, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - if (RsaPublicKeyDecode(pkcs7->publicKey, &scratch, key, - pkcs7->publicKeySz) < 0) { - CYASSL_MSG("ASN RSA key decode error"); -#ifdef CYASSL_SMALL_STACK - XFREE(digest, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return PUBLIC_KEY_E; - } - - plainSz = RsaSSL_Verify(sig, sigSz, digest, digestSz, key); - FreeRsaKey(key); - -#ifdef CYASSL_SMALL_STACK - XFREE(digest, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - if (plainSz < 0) - return plainSz; - } - } - - return 0; -} - - -/* create ASN.1 fomatted RecipientInfo structure, returns sequence size */ -CYASSL_LOCAL int CreateRecipientInfo(const byte* cert, word32 certSz, - int keyEncAlgo, int blockKeySz, - RNG* rng, byte* contentKeyPlain, - byte* contentKeyEnc, - int* keyEncSz, byte* out, word32 outSz) -{ - word32 idx = 0; - int ret = 0, totalSz = 0; - int verSz, issuerSz, snSz, keyEncAlgSz; - int issuerSeqSz, recipSeqSz, issuerSerialSeqSz; - int encKeyOctetStrSz; - - byte ver[MAX_VERSION_SZ]; - byte issuerSerialSeq[MAX_SEQ_SZ]; - byte recipSeq[MAX_SEQ_SZ]; - byte issuerSeq[MAX_SEQ_SZ]; - byte encKeyOctetStr[MAX_OCTET_STR_SZ]; - -#ifdef CYASSL_SMALL_STACK - byte *serial; - byte *keyAlgArray; - - RsaKey* pubKey; - DecodedCert* decoded; - - serial = (byte*)XMALLOC(MAX_SN_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); - keyAlgArray = (byte*)XMALLOC(MAX_SN_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); - decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - - if (decoded == NULL || serial == NULL || keyAlgArray == NULL) { - if (serial) XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (keyAlgArray) XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (decoded) XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); - return MEMORY_E; - } - -#else - byte serial[MAX_SN_SZ]; - byte keyAlgArray[MAX_ALGO_SZ]; - - RsaKey stack_pubKey; - RsaKey* pubKey = &stack_pubKey; - DecodedCert stack_decoded; - DecodedCert* decoded = &stack_decoded; -#endif - - InitDecodedCert(decoded, (byte*)cert, certSz, 0); - ret = ParseCert(decoded, CA_TYPE, NO_VERIFY, 0); - if (ret < 0) { - FreeDecodedCert(decoded); -#ifdef CYASSL_SMALL_STACK - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - - /* version */ - verSz = SetMyVersion(0, ver, 0); - - /* IssuerAndSerialNumber */ - if (decoded->issuerRaw == NULL || decoded->issuerRawLen == 0) { - CYASSL_MSG("DecodedCert lacks raw issuer pointer and length"); - FreeDecodedCert(decoded); -#ifdef CYASSL_SMALL_STACK - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return -1; - } - issuerSz = decoded->issuerRawLen; - issuerSeqSz = SetSequence(issuerSz, issuerSeq); - - if (decoded->serial == NULL || decoded->serialSz == 0) { - CYASSL_MSG("DecodedCert missing serial number"); - FreeDecodedCert(decoded); -#ifdef CYASSL_SMALL_STACK - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return -1; - } - snSz = SetSerialNumber(decoded->serial, decoded->serialSz, serial); - - issuerSerialSeqSz = SetSequence(issuerSeqSz + issuerSz + snSz, - issuerSerialSeq); - - /* KeyEncryptionAlgorithmIdentifier, only support RSA now */ - if (keyEncAlgo != RSAk) { - FreeDecodedCert(decoded); -#ifdef CYASSL_SMALL_STACK - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ALGO_ID_E; - } - - keyEncAlgSz = SetAlgoID(keyEncAlgo, keyAlgArray, keyType, 0); - if (keyEncAlgSz == 0) { - FreeDecodedCert(decoded); -#ifdef CYASSL_SMALL_STACK - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return BAD_FUNC_ARG; - } - -#ifdef CYASSL_SMALL_STACK - pubKey = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (pubKey == NULL) { - FreeDecodedCert(decoded); - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); - return MEMORY_E; - } -#endif - - /* EncryptedKey */ - ret = InitRsaKey(pubKey, 0); - if (ret != 0) { - FreeDecodedCert(decoded); -#ifdef CYASSL_SMALL_STACK - XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - - if (RsaPublicKeyDecode(decoded->publicKey, &idx, pubKey, - decoded->pubKeySize) < 0) { - CYASSL_MSG("ASN RSA key decode error"); - FreeRsaKey(pubKey); - FreeDecodedCert(decoded); -#ifdef CYASSL_SMALL_STACK - XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return PUBLIC_KEY_E; - } - - *keyEncSz = RsaPublicEncrypt(contentKeyPlain, blockKeySz, contentKeyEnc, - MAX_ENCRYPTED_KEY_SZ, pubKey, rng); - FreeRsaKey(pubKey); - -#ifdef CYASSL_SMALL_STACK - XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - if (*keyEncSz < 0) { - CYASSL_MSG("RSA Public Encrypt failed"); - FreeDecodedCert(decoded); -#ifdef CYASSL_SMALL_STACK - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return *keyEncSz; - } - - encKeyOctetStrSz = SetOctetString(*keyEncSz, encKeyOctetStr); - - /* RecipientInfo */ - recipSeqSz = SetSequence(verSz + issuerSerialSeqSz + issuerSeqSz + - issuerSz + snSz + keyEncAlgSz + encKeyOctetStrSz + - *keyEncSz, recipSeq); - - if (recipSeqSz + verSz + issuerSerialSeqSz + issuerSeqSz + snSz + - keyEncAlgSz + encKeyOctetStrSz + *keyEncSz > (int)outSz) { - CYASSL_MSG("RecipientInfo output buffer too small"); - FreeDecodedCert(decoded); -#ifdef CYASSL_SMALL_STACK - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return BUFFER_E; - } - - XMEMCPY(out + totalSz, recipSeq, recipSeqSz); - totalSz += recipSeqSz; - XMEMCPY(out + totalSz, ver, verSz); - totalSz += verSz; - XMEMCPY(out + totalSz, issuerSerialSeq, issuerSerialSeqSz); - totalSz += issuerSerialSeqSz; - XMEMCPY(out + totalSz, issuerSeq, issuerSeqSz); - totalSz += issuerSeqSz; - XMEMCPY(out + totalSz, decoded->issuerRaw, issuerSz); - totalSz += issuerSz; - XMEMCPY(out + totalSz, serial, snSz); - totalSz += snSz; - XMEMCPY(out + totalSz, keyAlgArray, keyEncAlgSz); - totalSz += keyEncAlgSz; - XMEMCPY(out + totalSz, encKeyOctetStr, encKeyOctetStrSz); - totalSz += encKeyOctetStrSz; - XMEMCPY(out + totalSz, contentKeyEnc, *keyEncSz); - totalSz += *keyEncSz; - - FreeDecodedCert(decoded); - -#ifdef CYASSL_SMALL_STACK - XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - return totalSz; -} - - -/* build PKCS#7 envelopedData content type, return enveloped size */ -int PKCS7_EncodeEnvelopedData(PKCS7* pkcs7, byte* output, word32 outputSz) -{ - int i, ret = 0, idx = 0; - int totalSz = 0, padSz = 0, desOutSz = 0; - - int contentInfoSeqSz, outerContentTypeSz, outerContentSz; - byte contentInfoSeq[MAX_SEQ_SZ]; - byte outerContentType[MAX_ALGO_SZ]; - byte outerContent[MAX_SEQ_SZ]; - - int envDataSeqSz, verSz; - byte envDataSeq[MAX_SEQ_SZ]; - byte ver[MAX_VERSION_SZ]; - - RNG rng; - int contentKeyEncSz, blockKeySz; - int dynamicFlag = 0; - byte contentKeyPlain[MAX_CONTENT_KEY_LEN]; -#ifdef CYASSL_SMALL_STACK - byte* contentKeyEnc; -#else - byte contentKeyEnc[MAX_ENCRYPTED_KEY_SZ]; -#endif - byte* plain; - byte* encryptedContent; - - int recipSz, recipSetSz; -#ifdef CYASSL_SMALL_STACK - byte* recip; -#else - byte recip[MAX_RECIP_SZ]; -#endif - byte recipSet[MAX_SET_SZ]; - - int encContentOctetSz, encContentSeqSz, contentTypeSz; - int contentEncAlgoSz, ivOctetStringSz; - byte encContentSeq[MAX_SEQ_SZ]; - byte contentType[MAX_ALGO_SZ]; - byte contentEncAlgo[MAX_ALGO_SZ]; - byte tmpIv[DES_BLOCK_SIZE]; - byte ivOctetString[MAX_OCTET_STR_SZ]; - byte encContentOctet[MAX_OCTET_STR_SZ]; - - if (pkcs7 == NULL || pkcs7->content == NULL || pkcs7->contentSz == 0 || - pkcs7->encryptOID == 0 || pkcs7->singleCert == NULL) - return BAD_FUNC_ARG; - - if (output == NULL || outputSz == 0) - return BAD_FUNC_ARG; - - /* PKCS#7 only supports DES, 3DES for now */ - switch (pkcs7->encryptOID) { - case DESb: - blockKeySz = DES_KEYLEN; - break; - - case DES3b: - blockKeySz = DES3_KEYLEN; - break; - - default: - CYASSL_MSG("Unsupported content cipher type"); - return ALGO_ID_E; - }; - - /* outer content type */ - outerContentTypeSz = SetContentType(ENVELOPED_DATA, outerContentType); - - /* version, defined as 0 in RFC 2315 */ - verSz = SetMyVersion(0, ver, 0); - - /* generate random content encryption key */ - ret = InitRng(&rng); - if (ret != 0) - return ret; - - ret = RNG_GenerateBlock(&rng, contentKeyPlain, blockKeySz); - if (ret != 0) - return ret; - -#ifdef CYASSL_SMALL_STACK - recip = (byte*)XMALLOC(MAX_RECIP_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); - contentKeyEnc = (byte*)XMALLOC(MAX_ENCRYPTED_KEY_SZ, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (contentKeyEnc == NULL || recip == NULL) { - if (recip) XFREE(recip, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (contentKeyEnc) XFREE(contentKeyEnc, NULL, DYNAMIC_TYPE_TMP_BUFFER); - return MEMORY_E; - } - -#endif - - /* build RecipientInfo, only handle 1 for now */ - recipSz = CreateRecipientInfo(pkcs7->singleCert, pkcs7->singleCertSz, RSAk, - blockKeySz, &rng, contentKeyPlain, - contentKeyEnc, &contentKeyEncSz, recip, - MAX_RECIP_SZ); - - XMEMSET(contentKeyEnc, 0, MAX_ENCRYPTED_KEY_SZ); - -#ifdef CYASSL_SMALL_STACK - XFREE(contentKeyEnc, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - if (recipSz < 0) { - CYASSL_MSG("Failed to create RecipientInfo"); -#ifdef CYASSL_SMALL_STACK - XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); -#endif - return recipSz; - } - recipSetSz = SetSet(recipSz, recipSet); - - /* generate IV for block cipher */ - ret = RNG_GenerateBlock(&rng, tmpIv, DES_BLOCK_SIZE); - if (ret != 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - - /* EncryptedContentInfo */ - contentTypeSz = SetContentType(pkcs7->contentOID, contentType); - if (contentTypeSz == 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); -#endif - return BAD_FUNC_ARG; - } - - /* allocate encrypted content buffer, pad if necessary, PKCS#7 padding */ - padSz = DES_BLOCK_SIZE - (pkcs7->contentSz % DES_BLOCK_SIZE); - desOutSz = pkcs7->contentSz + padSz; - - if (padSz != 0) { - plain = (byte*)XMALLOC(desOutSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (plain == NULL) { -#ifdef CYASSL_SMALL_STACK - XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); -#endif - return MEMORY_E; - } - XMEMCPY(plain, pkcs7->content, pkcs7->contentSz); - dynamicFlag = 1; - - for (i = 0; i < padSz; i++) { - plain[pkcs7->contentSz + i] = padSz; - } - - } else { - plain = pkcs7->content; - desOutSz = pkcs7->contentSz; - } - - encryptedContent = (byte*)XMALLOC(desOutSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (encryptedContent == NULL) { - if (dynamicFlag) - XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); -#endif - return MEMORY_E; - } - - /* put together IV OCTET STRING */ - ivOctetStringSz = SetOctetString(DES_BLOCK_SIZE, ivOctetString); - - /* build up our ContentEncryptionAlgorithmIdentifier sequence, - * adding (ivOctetStringSz + DES_BLOCK_SIZE) for IV OCTET STRING */ - contentEncAlgoSz = SetAlgoID(pkcs7->encryptOID, contentEncAlgo, - blkType, ivOctetStringSz + DES_BLOCK_SIZE); - - if (contentEncAlgoSz == 0) { - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (dynamicFlag) - XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); -#endif - return BAD_FUNC_ARG; - } - - /* encrypt content */ - if (pkcs7->encryptOID == DESb) { - Des des; - - ret = Des_SetKey(&des, contentKeyPlain, tmpIv, DES_ENCRYPTION); - - if (ret == 0) - Des_CbcEncrypt(&des, encryptedContent, plain, desOutSz); - - if (ret != 0) { - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (dynamicFlag) - XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - } - else if (pkcs7->encryptOID == DES3b) { - Des3 des3; - - ret = Des3_SetKey(&des3, contentKeyPlain, tmpIv, DES_ENCRYPTION); - - if (ret == 0) - ret = Des3_CbcEncrypt(&des3, encryptedContent, plain, desOutSz); - - if (ret != 0) { - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (dynamicFlag) - XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - } - - encContentOctetSz = SetImplicit(ASN_OCTET_STRING, 0, - desOutSz, encContentOctet); - - encContentSeqSz = SetSequence(contentTypeSz + contentEncAlgoSz + - ivOctetStringSz + DES_BLOCK_SIZE + - encContentOctetSz + desOutSz, encContentSeq); - - /* keep track of sizes for outer wrapper layering */ - totalSz = verSz + recipSetSz + recipSz + encContentSeqSz + contentTypeSz + - contentEncAlgoSz + ivOctetStringSz + DES_BLOCK_SIZE + - encContentOctetSz + desOutSz; - - /* EnvelopedData */ - envDataSeqSz = SetSequence(totalSz, envDataSeq); - totalSz += envDataSeqSz; - - /* outer content */ - outerContentSz = SetExplicit(0, totalSz, outerContent); - totalSz += outerContentTypeSz; - totalSz += outerContentSz; - - /* ContentInfo */ - contentInfoSeqSz = SetSequence(totalSz, contentInfoSeq); - totalSz += contentInfoSeqSz; - - if (totalSz > (int)outputSz) { - CYASSL_MSG("Pkcs7_encrypt output buffer too small"); - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (dynamicFlag) - XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); -#endif - return BUFFER_E; - } - - XMEMCPY(output + idx, contentInfoSeq, contentInfoSeqSz); - idx += contentInfoSeqSz; - XMEMCPY(output + idx, outerContentType, outerContentTypeSz); - idx += outerContentTypeSz; - XMEMCPY(output + idx, outerContent, outerContentSz); - idx += outerContentSz; - XMEMCPY(output + idx, envDataSeq, envDataSeqSz); - idx += envDataSeqSz; - XMEMCPY(output + idx, ver, verSz); - idx += verSz; - XMEMCPY(output + idx, recipSet, recipSetSz); - idx += recipSetSz; - XMEMCPY(output + idx, recip, recipSz); - idx += recipSz; - XMEMCPY(output + idx, encContentSeq, encContentSeqSz); - idx += encContentSeqSz; - XMEMCPY(output + idx, contentType, contentTypeSz); - idx += contentTypeSz; - XMEMCPY(output + idx, contentEncAlgo, contentEncAlgoSz); - idx += contentEncAlgoSz; - XMEMCPY(output + idx, ivOctetString, ivOctetStringSz); - idx += ivOctetStringSz; - XMEMCPY(output + idx, tmpIv, DES_BLOCK_SIZE); - idx += DES_BLOCK_SIZE; - XMEMCPY(output + idx, encContentOctet, encContentOctetSz); - idx += encContentOctetSz; - XMEMCPY(output + idx, encryptedContent, desOutSz); - idx += desOutSz; - -#if defined(HAVE_HASHDRBG) || defined(NO_RC4) - FreeRng(&rng); -#endif - - XMEMSET(contentKeyPlain, 0, MAX_CONTENT_KEY_LEN); - - if (dynamicFlag) - XFREE(plain, NULL, DYNAMMIC_TYPE_TMP_BUFFER); - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); - -#ifdef CYASSL_SMALL_STACK - XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); -#endif - - return idx; -} - -/* unwrap and decrypt PKCS#7 envelopedData object, return decoded size */ -CYASSL_API int PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg, - word32 pkiMsgSz, byte* output, - word32 outputSz) -{ - int recipFound = 0; - int ret, version, length; - word32 savedIdx = 0, idx = 0; - word32 contentType, encOID; - byte issuerHash[SHA_DIGEST_SIZE]; - - int encryptedKeySz, keySz; - byte tmpIv[DES_BLOCK_SIZE]; - byte* decryptedKey = NULL; - -#ifdef CYASSL_SMALL_STACK - mp_int* serialNum; - byte* encryptedKey; - RsaKey* privKey; -#else - mp_int stack_serialNum; - mp_int* serialNum = &stack_serialNum; - byte encryptedKey[MAX_ENCRYPTED_KEY_SZ]; - - RsaKey stack_privKey; - RsaKey* privKey = &stack_privKey; -#endif - int encryptedContentSz; - byte padLen; - byte* encryptedContent = NULL; - - if (pkcs7 == NULL || pkcs7->singleCert == NULL || - pkcs7->singleCertSz == 0 || pkcs7->privateKey == NULL || - pkcs7->privateKeySz == 0) - return BAD_FUNC_ARG; - - if (pkiMsg == NULL || pkiMsgSz == 0 || - output == NULL || outputSz == 0) - return BAD_FUNC_ARG; - - /* read past ContentInfo, verify type is envelopedData */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - if (GetContentType(pkiMsg, &idx, &contentType, pkiMsgSz) < 0) - return ASN_PARSE_E; - - if (contentType != ENVELOPED_DATA) { - CYASSL_MSG("PKCS#7 input not of type EnvelopedData"); - return PKCS7_OID_E; - } - - if (pkiMsg[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) - return ASN_PARSE_E; - - if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - /* remove EnvelopedData and version */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - - if (GetMyVersion(pkiMsg, &idx, &version) < 0) - return ASN_PARSE_E; - - if (version != 0) { - CYASSL_MSG("PKCS#7 envelopedData needs to be of version 0"); - return ASN_VERSION_E; - } - - /* walk through RecipientInfo set, find correct recipient */ - if (GetSet(pkiMsg, &idx, &length, pkiMsgSz) < 0) - return ASN_PARSE_E; - -#ifdef CYASSL_SMALL_STACK - encryptedKey = (byte*)XMALLOC(MAX_ENCRYPTED_KEY_SZ, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (encryptedKey == NULL) - return MEMORY_E; -#endif - - savedIdx = idx; - recipFound = 0; - - /* when looking for next recipient, use first sequence and version to - * indicate there is another, if not, move on */ - while(recipFound == 0) { - - /* remove RecipientInfo, if we don't have a SEQUENCE, back up idx to - * last good saved one */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) { - idx = savedIdx; - break; - } - - if (GetMyVersion(pkiMsg, &idx, &version) < 0) { - idx = savedIdx; - break; - } - - if (version != 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_VERSION_E; - } - - /* remove IssuerAndSerialNumber */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - if (GetNameHash(pkiMsg, &idx, issuerHash, pkiMsgSz) < 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - /* if we found correct recipient, issuer hashes will match */ - if (XMEMCMP(issuerHash, pkcs7->issuerHash, SHA_DIGEST_SIZE) == 0) { - recipFound = 1; - } - -#ifdef CYASSL_SMALL_STACK - serialNum = (mp_int*)XMALLOC(sizeof(mp_int), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (serialNum == NULL) { - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); - return MEMORY_E; - } -#endif - - if (GetInt(serialNum, pkiMsg, &idx, pkiMsgSz) < 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(serialNum, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - mp_clear(serialNum); - -#ifdef CYASSL_SMALL_STACK - XFREE(serialNum, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - if (GetAlgoId(pkiMsg, &idx, &encOID, pkiMsgSz) < 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - /* key encryption algorithm must be RSA for now */ - if (encOID != RSAk) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ALGO_ID_E; - } - - /* read encryptedKey */ - if (pkiMsg[idx++] != ASN_OCTET_STRING) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - if (GetLength(pkiMsg, &idx, &encryptedKeySz, pkiMsgSz) < 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - if (recipFound == 1) - XMEMCPY(encryptedKey, &pkiMsg[idx], encryptedKeySz); - idx += encryptedKeySz; - - /* update good idx */ - savedIdx = idx; - } - - if (recipFound == 0) { - CYASSL_MSG("No recipient found in envelopedData that matches input"); -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return PKCS7_RECIP_E; - } - - /* remove EncryptedContentInfo */ - if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - if (GetContentType(pkiMsg, &idx, &contentType, pkiMsgSz) < 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - if (GetAlgoId(pkiMsg, &idx, &encOID, pkiMsgSz) < 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - /* get block cipher IV, stored in OPTIONAL parameter of AlgoID */ - if (pkiMsg[idx++] != ASN_OCTET_STRING) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - if (length != DES_BLOCK_SIZE) { - CYASSL_MSG("Incorrect IV length, must be of DES_BLOCK_SIZE"); -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - XMEMCPY(tmpIv, &pkiMsg[idx], length); - idx += length; - - /* read encryptedContent, cont[0] */ - if (pkiMsg[idx++] != (ASN_CONTEXT_SPECIFIC | 0)) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - if (GetLength(pkiMsg, &idx, &encryptedContentSz, pkiMsgSz) < 0) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ASN_PARSE_E; - } - - encryptedContent = (byte*)XMALLOC(encryptedContentSz, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (encryptedContent == NULL) { -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return MEMORY_E; - } - - XMEMCPY(encryptedContent, &pkiMsg[idx], encryptedContentSz); - - /* load private key */ -#ifdef CYASSL_SMALL_STACK - privKey = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (privKey == NULL) { - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); return MEMORY_E; - } -#endif - - ret = InitRsaKey(privKey, 0); - if (ret != 0) { - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - - idx = 0; - - ret = RsaPrivateKeyDecode(pkcs7->privateKey, &idx, privKey, - pkcs7->privateKeySz); - if (ret != 0) { - CYASSL_MSG("Failed to decode RSA private key"); - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - - /* decrypt encryptedKey */ - keySz = RsaPrivateDecryptInline(encryptedKey, encryptedKeySz, - &decryptedKey, privKey); - FreeRsaKey(privKey); - -#ifdef CYASSL_SMALL_STACK - XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - if (keySz <= 0) { - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return keySz; - } - - /* decrypt encryptedContent */ - if (encOID == DESb) { - Des des; - ret = Des_SetKey(&des, decryptedKey, tmpIv, DES_DECRYPTION); - - if (ret == 0) - Des_CbcDecrypt(&des, encryptedContent, encryptedContent, - encryptedContentSz); - - if (ret != 0) { - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - } - else if (encOID == DES3b) { - Des3 des; - ret = Des3_SetKey(&des, decryptedKey, tmpIv, DES_DECRYPTION); - if (ret == 0) - ret = Des3_CbcDecrypt(&des, encryptedContent, encryptedContent, - encryptedContentSz); - - if (ret != 0) { - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ret; - } - } else { - CYASSL_MSG("Unsupported content encryption OID type"); - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return ALGO_ID_E; - } - - padLen = encryptedContent[encryptedContentSz-1]; - - /* copy plaintext to output */ - XMEMCPY(output, encryptedContent, encryptedContentSz - padLen); - - /* free memory, zero out keys */ - XMEMSET(encryptedKey, 0, MAX_ENCRYPTED_KEY_SZ); - XMEMSET(encryptedContent, 0, encryptedContentSz); - XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#ifdef CYASSL_SMALL_STACK - XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - return encryptedContentSz - padLen; -} - - -#else /* HAVE_PKCS7 */ - - -#ifdef _MSC_VER - /* 4206 warning for blank file */ - #pragma warning(disable: 4206) -#endif - - -#endif /* HAVE_PKCS7 */ - +//#ifdef HAVE_CONFIG_H +// #include +//#endif +// +//#include +// +//#ifdef HAVE_PKCS7 +// +//#include +//#include +//#include +// +//#ifndef min +// static INLINE word32 min(word32 a, word32 b) +// { +// return a > b ? b : a; +// } +//#endif +// +// +///* placed ASN.1 contentType OID into *output, return idx on success, +// * 0 upon failure */ +//CYASSL_LOCAL int SetContentType(int pkcs7TypeOID, byte* output) +//{ +// /* PKCS#7 content types, RFC 2315, section 14 */ +// static const byte pkcs7[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, +// 0x0D, 0x01, 0x07 }; +// static const byte data[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, +// 0x0D, 0x01, 0x07, 0x01 }; +// static const byte signedData[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, +// 0x0D, 0x01, 0x07, 0x02}; +// static const byte envelopedData[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, +// 0x0D, 0x01, 0x07, 0x03 }; +// static const byte signedAndEnveloped[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, +// 0x0D, 0x01, 0x07, 0x04 }; +// static const byte digestedData[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, +// 0x0D, 0x01, 0x07, 0x05 }; +// static const byte encryptedData[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, +// 0x0D, 0x01, 0x07, 0x06 }; +// +// int idSz; +// int typeSz = 0, idx = 0; +// const byte* typeName = 0; +// byte ID_Length[MAX_LENGTH_SZ]; +// +// switch (pkcs7TypeOID) { +// case PKCS7_MSG: +// typeSz = sizeof(pkcs7); +// typeName = pkcs7; +// break; +// +// case DATA: +// typeSz = sizeof(data); +// typeName = data; +// break; +// +// case SIGNED_DATA: +// typeSz = sizeof(signedData); +// typeName = signedData; +// break; +// +// case ENVELOPED_DATA: +// typeSz = sizeof(envelopedData); +// typeName = envelopedData; +// break; +// +// case SIGNED_AND_ENVELOPED_DATA: +// typeSz = sizeof(signedAndEnveloped); +// typeName = signedAndEnveloped; +// break; +// +// case DIGESTED_DATA: +// typeSz = sizeof(digestedData); +// typeName = digestedData; +// break; +// +// case ENCRYPTED_DATA: +// typeSz = sizeof(encryptedData); +// typeName = encryptedData; +// break; +// +// default: +// CYASSL_MSG("Unknown PKCS#7 Type"); +// return 0; +// }; +// +// idSz = SetLength(typeSz, ID_Length); +// output[idx++] = ASN_OBJECT_ID; +// XMEMCPY(output + idx, ID_Length, idSz); +// idx += idSz; +// XMEMCPY(output + idx, typeName, typeSz); +// idx += typeSz; +// +// return idx; +// +//} +// +// +///* get ASN.1 contentType OID sum, return 0 on success, <0 on failure */ +//int GetContentType(const byte* input, word32* inOutIdx, word32* oid, +// word32 maxIdx) +//{ +// int length; +// word32 i = *inOutIdx; +// byte b; +// *oid = 0; +// +// CYASSL_ENTER("GetContentType"); +// +// b = input[i++]; +// if (b != ASN_OBJECT_ID) +// return ASN_OBJECT_ID_E; +// +// if (GetLength(input, &i, &length, maxIdx) < 0) +// return ASN_PARSE_E; +// +// while(length--) { +// *oid += input[i]; +// i++; +// } +// +// *inOutIdx = i; +// +// return 0; +//} +// +// +///* init PKCS7 struct with recipient cert, decode into DecodedCert */ +//int PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz) +//{ +// int ret = 0; +// +// XMEMSET(pkcs7, 0, sizeof(PKCS7)); +// if (cert != NULL && certSz > 0) { +//#ifdef CYASSL_SMALL_STACK +// DecodedCert* dCert; +// +// dCert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, +// DYNAMIC_TYPE_TMP_BUFFER); +// if (dCert == NULL) +// return MEMORY_E; +//#else +// DecodedCert stack_dCert; +// DecodedCert* dCert = &stack_dCert; +//#endif +// +// pkcs7->singleCert = cert; +// pkcs7->singleCertSz = certSz; +// InitDecodedCert(dCert, cert, certSz, 0); +// +// ret = ParseCert(dCert, CA_TYPE, NO_VERIFY, 0); +// if (ret < 0) { +// FreeDecodedCert(dCert); +//#ifdef CYASSL_SMALL_STACK +// XFREE(dCert, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// +// XMEMCPY(pkcs7->publicKey, dCert->publicKey, dCert->pubKeySize); +// pkcs7->publicKeySz = dCert->pubKeySize; +// XMEMCPY(pkcs7->issuerHash, dCert->issuerHash, SHA_SIZE); +// pkcs7->issuer = dCert->issuerRaw; +// pkcs7->issuerSz = dCert->issuerRawLen; +// XMEMCPY(pkcs7->issuerSn, dCert->serial, dCert->serialSz); +// pkcs7->issuerSnSz = dCert->serialSz; +// FreeDecodedCert(dCert); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(dCert, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// } +// +// return ret; +//} +// +// +///* releases any memory allocated by a PKCS7 initializer */ +//void PKCS7_Free(PKCS7* pkcs7) +//{ +// (void)pkcs7; +//} +// +// +///* build PKCS#7 data content type */ +//int PKCS7_EncodeData(PKCS7* pkcs7, byte* output, word32 outputSz) +//{ +// static const byte oid[] = +// { ASN_OBJECT_ID, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, +// 0x07, 0x01 }; +// byte seq[MAX_SEQ_SZ]; +// byte octetStr[MAX_OCTET_STR_SZ]; +// word32 seqSz; +// word32 octetStrSz; +// word32 oidSz = (word32)sizeof(oid); +// int idx = 0; +// +// octetStrSz = SetOctetString(pkcs7->contentSz, octetStr); +// seqSz = SetSequence(pkcs7->contentSz + octetStrSz + oidSz, seq); +// +// if (outputSz < pkcs7->contentSz + octetStrSz + oidSz + seqSz) +// return BUFFER_E; +// +// XMEMCPY(output, seq, seqSz); +// idx += seqSz; +// XMEMCPY(output + idx, oid, oidSz); +// idx += oidSz; +// XMEMCPY(output + idx, octetStr, octetStrSz); +// idx += octetStrSz; +// XMEMCPY(output + idx, pkcs7->content, pkcs7->contentSz); +// idx += pkcs7->contentSz; +// +// return idx; +//} +// +// +//typedef struct EncodedAttrib { +// byte valueSeq[MAX_SEQ_SZ]; +// const byte* oid; +// byte valueSet[MAX_SET_SZ]; +// const byte* value; +// word32 valueSeqSz, oidSz, idSz, valueSetSz, valueSz, totalSz; +//} EncodedAttrib; +// +// +//typedef struct ESD { +// Sha sha; +// byte contentDigest[SHA_DIGEST_SIZE + 2]; /* content only + ASN.1 heading */ +// byte contentAttribsDigest[SHA_DIGEST_SIZE]; +// byte encContentDigest[512]; +// +// byte outerSeq[MAX_SEQ_SZ]; +// byte outerContent[MAX_EXP_SZ]; +// byte innerSeq[MAX_SEQ_SZ]; +// byte version[MAX_VERSION_SZ]; +// byte digAlgoIdSet[MAX_SET_SZ]; +// byte singleDigAlgoId[MAX_ALGO_SZ]; +// +// byte contentInfoSeq[MAX_SEQ_SZ]; +// byte innerContSeq[MAX_EXP_SZ]; +// byte innerOctets[MAX_OCTET_STR_SZ]; +// +// byte certsSet[MAX_SET_SZ]; +// +// byte signerInfoSet[MAX_SET_SZ]; +// byte signerInfoSeq[MAX_SEQ_SZ]; +// byte signerVersion[MAX_VERSION_SZ]; +// byte issuerSnSeq[MAX_SEQ_SZ]; +// byte issuerName[MAX_SEQ_SZ]; +// byte issuerSn[MAX_SN_SZ]; +// byte signerDigAlgoId[MAX_ALGO_SZ]; +// byte digEncAlgoId[MAX_ALGO_SZ]; +// byte signedAttribSet[MAX_SET_SZ]; +// EncodedAttrib signedAttribs[6]; +// byte signerDigest[MAX_OCTET_STR_SZ]; +// word32 innerOctetsSz, innerContSeqSz, contentInfoSeqSz; +// word32 outerSeqSz, outerContentSz, innerSeqSz, versionSz, digAlgoIdSetSz, +// singleDigAlgoIdSz, certsSetSz; +// word32 signerInfoSetSz, signerInfoSeqSz, signerVersionSz, +// issuerSnSeqSz, issuerNameSz, issuerSnSz, +// signerDigAlgoIdSz, digEncAlgoIdSz, signerDigestSz; +// word32 encContentDigestSz, signedAttribsSz, signedAttribsCount, +// signedAttribSetSz; +//} ESD; +// +// +//static int EncodeAttributes(EncodedAttrib* ea, int eaSz, +// PKCS7Attrib* attribs, int attribsSz) +//{ +// int i; +// int maxSz = min(eaSz, attribsSz); +// int allAttribsSz = 0; +// +// for (i = 0; i < maxSz; i++) +// { +// int attribSz = 0; +// +// ea[i].value = attribs[i].value; +// ea[i].valueSz = attribs[i].valueSz; +// attribSz += ea[i].valueSz; +// ea[i].valueSetSz = SetSet(attribSz, ea[i].valueSet); +// attribSz += ea[i].valueSetSz; +// ea[i].oid = attribs[i].oid; +// ea[i].oidSz = attribs[i].oidSz; +// attribSz += ea[i].oidSz; +// ea[i].valueSeqSz = SetSequence(attribSz, ea[i].valueSeq); +// attribSz += ea[i].valueSeqSz; +// ea[i].totalSz = attribSz; +// +// allAttribsSz += attribSz; +// } +// return allAttribsSz; +//} +// +// +//static int FlattenAttributes(byte* output, EncodedAttrib* ea, int eaSz) +//{ +// int i, idx; +// +// idx = 0; +// for (i = 0; i < eaSz; i++) { +// XMEMCPY(output + idx, ea[i].valueSeq, ea[i].valueSeqSz); +// idx += ea[i].valueSeqSz; +// XMEMCPY(output + idx, ea[i].oid, ea[i].oidSz); +// idx += ea[i].oidSz; +// XMEMCPY(output + idx, ea[i].valueSet, ea[i].valueSetSz); +// idx += ea[i].valueSetSz; +// XMEMCPY(output + idx, ea[i].value, ea[i].valueSz); +// idx += ea[i].valueSz; +// } +// return 0; +//} +// +// +///* build PKCS#7 signedData content type */ +//int PKCS7_EncodeSignedData(PKCS7* pkcs7, byte* output, word32 outputSz) +//{ +// static const byte outerOid[] = +// { ASN_OBJECT_ID, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, +// 0x07, 0x02 }; +// static const byte innerOid[] = +// { ASN_OBJECT_ID, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, +// 0x07, 0x01 }; +// +//#ifdef CYASSL_SMALL_STACK +// ESD* esd = NULL; +//#else +// ESD stack_esd; +// ESD* esd = &stack_esd; +//#endif +// +// word32 signerInfoSz = 0; +// word32 totalSz = 0; +// int idx = 0, ret = 0; +// byte* flatSignedAttribs = NULL; +// word32 flatSignedAttribsSz = 0; +// word32 innerOidSz = sizeof(innerOid); +// word32 outerOidSz = sizeof(outerOid); +// +// if (pkcs7 == NULL || pkcs7->content == NULL || pkcs7->contentSz == 0 || +// pkcs7->encryptOID == 0 || pkcs7->hashOID == 0 || pkcs7->rng == 0 || +// pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0 || +// pkcs7->privateKey == NULL || pkcs7->privateKeySz == 0 || +// output == NULL || outputSz == 0) +// return BAD_FUNC_ARG; +// +//#ifdef CYASSL_SMALL_STACK +// esd = (ESD*)XMALLOC(sizeof(ESD), NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (esd == NULL) +// return MEMORY_E; +//#endif +// +// XMEMSET(esd, 0, sizeof(ESD)); +// ret = InitSha(&esd->sha); +// if (ret != 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// +// if (pkcs7->contentSz != 0) +// { +// ShaUpdate(&esd->sha, pkcs7->content, pkcs7->contentSz); +// esd->contentDigest[0] = ASN_OCTET_STRING; +// esd->contentDigest[1] = SHA_DIGEST_SIZE; +// ShaFinal(&esd->sha, &esd->contentDigest[2]); +// } +// +// esd->innerOctetsSz = SetOctetString(pkcs7->contentSz, esd->innerOctets); +// esd->innerContSeqSz = SetExplicit(0, esd->innerOctetsSz + pkcs7->contentSz, +// esd->innerContSeq); +// esd->contentInfoSeqSz = SetSequence(pkcs7->contentSz + esd->innerOctetsSz + +// innerOidSz + esd->innerContSeqSz, +// esd->contentInfoSeq); +// +// esd->issuerSnSz = SetSerialNumber(pkcs7->issuerSn, pkcs7->issuerSnSz, +// esd->issuerSn); +// signerInfoSz += esd->issuerSnSz; +// esd->issuerNameSz = SetSequence(pkcs7->issuerSz, esd->issuerName); +// signerInfoSz += esd->issuerNameSz + pkcs7->issuerSz; +// esd->issuerSnSeqSz = SetSequence(signerInfoSz, esd->issuerSnSeq); +// signerInfoSz += esd->issuerSnSeqSz; +// esd->signerVersionSz = SetMyVersion(1, esd->signerVersion, 0); +// signerInfoSz += esd->signerVersionSz; +// esd->signerDigAlgoIdSz = SetAlgoID(pkcs7->hashOID, esd->signerDigAlgoId, +// hashType, 0); +// signerInfoSz += esd->signerDigAlgoIdSz; +// esd->digEncAlgoIdSz = SetAlgoID(pkcs7->encryptOID, esd->digEncAlgoId, +// keyType, 0); +// signerInfoSz += esd->digEncAlgoIdSz; +// +// if (pkcs7->signedAttribsSz != 0) { +// byte contentTypeOid[] = +// { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, +// 0x09, 0x03 }; +// byte contentType[] = +// { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, +// 0x07, 0x01 }; +// byte messageDigestOid[] = +// { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, +// 0x09, 0x04 }; +// +// PKCS7Attrib cannedAttribs[2] = +// { +// { contentTypeOid, sizeof(contentTypeOid), +// contentType, sizeof(contentType) }, +// { messageDigestOid, sizeof(messageDigestOid), +// esd->contentDigest, sizeof(esd->contentDigest) } +// }; +// word32 cannedAttribsCount = sizeof(cannedAttribs)/sizeof(PKCS7Attrib); +// +// esd->signedAttribsCount += cannedAttribsCount; +// esd->signedAttribsSz += EncodeAttributes(&esd->signedAttribs[0], 2, +// cannedAttribs, cannedAttribsCount); +// +// esd->signedAttribsCount += pkcs7->signedAttribsSz; +// esd->signedAttribsSz += EncodeAttributes(&esd->signedAttribs[2], 4, +// pkcs7->signedAttribs, pkcs7->signedAttribsSz); +// +// flatSignedAttribs = (byte*)XMALLOC(esd->signedAttribsSz, 0, NULL); +// flatSignedAttribsSz = esd->signedAttribsSz; +// if (flatSignedAttribs == NULL) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return MEMORY_E; +// } +// FlattenAttributes(flatSignedAttribs, +// esd->signedAttribs, esd->signedAttribsCount); +// esd->signedAttribSetSz = SetImplicit(ASN_SET, 0, esd->signedAttribsSz, +// esd->signedAttribSet); +// } +// /* Calculate the final hash and encrypt it. */ +// { +// int result; +// word32 scratch = 0; +// +//#ifdef CYASSL_SMALL_STACK +// byte* digestInfo; +// RsaKey* privKey; +//#else +// RsaKey stack_privKey; +// RsaKey* privKey = &stack_privKey; +// byte digestInfo[MAX_SEQ_SZ + MAX_ALGO_SZ + +// MAX_OCTET_STR_SZ + SHA_DIGEST_SIZE]; +//#endif +// byte digestInfoSeq[MAX_SEQ_SZ]; +// byte digestStr[MAX_OCTET_STR_SZ]; +// word32 digestInfoSeqSz, digestStrSz; +// int digIdx = 0; +// +// if (pkcs7->signedAttribsSz != 0) { +// byte attribSet[MAX_SET_SZ]; +// word32 attribSetSz; +// +// attribSetSz = SetSet(flatSignedAttribsSz, attribSet); +// +// ret = InitSha(&esd->sha); +// if (ret < 0) { +// XFREE(flatSignedAttribs, 0, NULL); +//#ifdef CYASSL_SMALL_STACK +// XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// ShaUpdate(&esd->sha, attribSet, attribSetSz); +// ShaUpdate(&esd->sha, flatSignedAttribs, flatSignedAttribsSz); +// } +// ShaFinal(&esd->sha, esd->contentAttribsDigest); +// +// digestStrSz = SetOctetString(SHA_DIGEST_SIZE, digestStr); +// digestInfoSeqSz = SetSequence(esd->signerDigAlgoIdSz + +// digestStrSz + SHA_DIGEST_SIZE, +// digestInfoSeq); +// +//#ifdef CYASSL_SMALL_STACK +// digestInfo = (byte*)XMALLOC(MAX_SEQ_SZ + MAX_ALGO_SZ + +// MAX_OCTET_STR_SZ + SHA_DIGEST_SIZE, +// NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (digestInfo == NULL) { +// if (pkcs7->signedAttribsSz != 0) +// XFREE(flatSignedAttribs, 0, NULL); +// XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// return MEMORY_E; +// } +//#endif +// +// XMEMCPY(digestInfo + digIdx, digestInfoSeq, digestInfoSeqSz); +// digIdx += digestInfoSeqSz; +// XMEMCPY(digestInfo + digIdx, +// esd->signerDigAlgoId, esd->signerDigAlgoIdSz); +// digIdx += esd->signerDigAlgoIdSz; +// XMEMCPY(digestInfo + digIdx, digestStr, digestStrSz); +// digIdx += digestStrSz; +// XMEMCPY(digestInfo + digIdx, esd->contentAttribsDigest, +// SHA_DIGEST_SIZE); +// digIdx += SHA_DIGEST_SIZE; +// +//#ifdef CYASSL_SMALL_STACK +// privKey = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, +// DYNAMIC_TYPE_TMP_BUFFER); +// if (privKey == NULL) { +// if (pkcs7->signedAttribsSz != 0) +// XFREE(flatSignedAttribs, 0, NULL); +// XFREE(digestInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// return MEMORY_E; +// } +//#endif +// +// result = InitRsaKey(privKey, NULL); +// if (result == 0) +// result = RsaPrivateKeyDecode(pkcs7->privateKey, &scratch, privKey, +// pkcs7->privateKeySz); +// if (result < 0) { +// if (pkcs7->signedAttribsSz != 0) +// XFREE(flatSignedAttribs, 0, NULL); +//#ifdef CYASSL_SMALL_STACK +// XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(digestInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return PUBLIC_KEY_E; +// } +// +// result = RsaSSL_Sign(digestInfo, digIdx, +// esd->encContentDigest, +// sizeof(esd->encContentDigest), +// privKey, pkcs7->rng); +// +// FreeRsaKey(privKey); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(digestInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// if (result < 0) { +// if (pkcs7->signedAttribsSz != 0) +// XFREE(flatSignedAttribs, 0, NULL); +//#ifdef CYASSL_SMALL_STACK +// XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return result; +// } +// esd->encContentDigestSz = (word32)result; +// } +// signerInfoSz += flatSignedAttribsSz + esd->signedAttribSetSz; +// +// esd->signerDigestSz = SetOctetString(esd->encContentDigestSz, +// esd->signerDigest); +// signerInfoSz += esd->signerDigestSz + esd->encContentDigestSz; +// +// esd->signerInfoSeqSz = SetSequence(signerInfoSz, esd->signerInfoSeq); +// signerInfoSz += esd->signerInfoSeqSz; +// esd->signerInfoSetSz = SetSet(signerInfoSz, esd->signerInfoSet); +// signerInfoSz += esd->signerInfoSetSz; +// +// esd->certsSetSz = SetImplicit(ASN_SET, 0, pkcs7->singleCertSz, +// esd->certsSet); +// +// esd->singleDigAlgoIdSz = SetAlgoID(pkcs7->hashOID, esd->singleDigAlgoId, +// hashType, 0); +// esd->digAlgoIdSetSz = SetSet(esd->singleDigAlgoIdSz, esd->digAlgoIdSet); +// +// +// esd->versionSz = SetMyVersion(1, esd->version, 0); +// +// totalSz = esd->versionSz + esd->singleDigAlgoIdSz + esd->digAlgoIdSetSz + +// esd->contentInfoSeqSz + esd->certsSetSz + pkcs7->singleCertSz + +// esd->innerOctetsSz + esd->innerContSeqSz + +// innerOidSz + pkcs7->contentSz + +// signerInfoSz; +// esd->innerSeqSz = SetSequence(totalSz, esd->innerSeq); +// totalSz += esd->innerSeqSz; +// esd->outerContentSz = SetExplicit(0, totalSz, esd->outerContent); +// totalSz += esd->outerContentSz + outerOidSz; +// esd->outerSeqSz = SetSequence(totalSz, esd->outerSeq); +// totalSz += esd->outerSeqSz; +// +// if (outputSz < totalSz) { +// if (pkcs7->signedAttribsSz != 0) +// XFREE(flatSignedAttribs, 0, NULL); +//#ifdef CYASSL_SMALL_STACK +// XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return BUFFER_E; +// } +// +// idx = 0; +// XMEMCPY(output + idx, esd->outerSeq, esd->outerSeqSz); +// idx += esd->outerSeqSz; +// XMEMCPY(output + idx, outerOid, outerOidSz); +// idx += outerOidSz; +// XMEMCPY(output + idx, esd->outerContent, esd->outerContentSz); +// idx += esd->outerContentSz; +// XMEMCPY(output + idx, esd->innerSeq, esd->innerSeqSz); +// idx += esd->innerSeqSz; +// XMEMCPY(output + idx, esd->version, esd->versionSz); +// idx += esd->versionSz; +// XMEMCPY(output + idx, esd->digAlgoIdSet, esd->digAlgoIdSetSz); +// idx += esd->digAlgoIdSetSz; +// XMEMCPY(output + idx, esd->singleDigAlgoId, esd->singleDigAlgoIdSz); +// idx += esd->singleDigAlgoIdSz; +// XMEMCPY(output + idx, esd->contentInfoSeq, esd->contentInfoSeqSz); +// idx += esd->contentInfoSeqSz; +// XMEMCPY(output + idx, innerOid, innerOidSz); +// idx += innerOidSz; +// XMEMCPY(output + idx, esd->innerContSeq, esd->innerContSeqSz); +// idx += esd->innerContSeqSz; +// XMEMCPY(output + idx, esd->innerOctets, esd->innerOctetsSz); +// idx += esd->innerOctetsSz; +// XMEMCPY(output + idx, pkcs7->content, pkcs7->contentSz); +// idx += pkcs7->contentSz; +// XMEMCPY(output + idx, esd->certsSet, esd->certsSetSz); +// idx += esd->certsSetSz; +// XMEMCPY(output + idx, pkcs7->singleCert, pkcs7->singleCertSz); +// idx += pkcs7->singleCertSz; +// XMEMCPY(output + idx, esd->signerInfoSet, esd->signerInfoSetSz); +// idx += esd->signerInfoSetSz; +// XMEMCPY(output + idx, esd->signerInfoSeq, esd->signerInfoSeqSz); +// idx += esd->signerInfoSeqSz; +// XMEMCPY(output + idx, esd->signerVersion, esd->signerVersionSz); +// idx += esd->signerVersionSz; +// XMEMCPY(output + idx, esd->issuerSnSeq, esd->issuerSnSeqSz); +// idx += esd->issuerSnSeqSz; +// XMEMCPY(output + idx, esd->issuerName, esd->issuerNameSz); +// idx += esd->issuerNameSz; +// XMEMCPY(output + idx, pkcs7->issuer, pkcs7->issuerSz); +// idx += pkcs7->issuerSz; +// XMEMCPY(output + idx, esd->issuerSn, esd->issuerSnSz); +// idx += esd->issuerSnSz; +// XMEMCPY(output + idx, esd->signerDigAlgoId, esd->signerDigAlgoIdSz); +// idx += esd->signerDigAlgoIdSz; +// +// /* SignerInfo:Attributes */ +// if (pkcs7->signedAttribsSz != 0) { +// XMEMCPY(output + idx, esd->signedAttribSet, esd->signedAttribSetSz); +// idx += esd->signedAttribSetSz; +// XMEMCPY(output + idx, flatSignedAttribs, flatSignedAttribsSz); +// idx += flatSignedAttribsSz; +// XFREE(flatSignedAttribs, 0, NULL); +// } +// +// XMEMCPY(output + idx, esd->digEncAlgoId, esd->digEncAlgoIdSz); +// idx += esd->digEncAlgoIdSz; +// XMEMCPY(output + idx, esd->signerDigest, esd->signerDigestSz); +// idx += esd->signerDigestSz; +// XMEMCPY(output + idx, esd->encContentDigest, esd->encContentDigestSz); +// idx += esd->encContentDigestSz; +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(esd, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// return idx; +//} +// +// +///* Finds the certificates in the message and saves it. */ +//int PKCS7_VerifySignedData(PKCS7* pkcs7, byte* pkiMsg, word32 pkiMsgSz) +//{ +// word32 idx, contentType; +// int length, version, ret; +// byte* content = NULL; +// byte* sig = NULL; +// byte* cert = NULL; +// int contentSz = 0, sigSz = 0, certSz = 0; +// +// if (pkcs7 == NULL || pkiMsg == NULL || pkiMsgSz == 0) +// return BAD_FUNC_ARG; +// +// idx = 0; +// +// /* Get the contentInfo sequence */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Get the contentInfo contentType */ +// if (GetContentType(pkiMsg, &idx, &contentType, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// if (contentType != SIGNED_DATA) { +// CYASSL_MSG("PKCS#7 input not of type SignedData"); +// return PKCS7_OID_E; +// } +// +// /* get the ContentInfo content */ +// if (pkiMsg[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) +// return ASN_PARSE_E; +// +// if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Get the signedData sequence */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Get the version */ +// if (GetMyVersion(pkiMsg, &idx, &version) < 0) +// return ASN_PARSE_E; +// +// if (version != 1) { +// CYASSL_MSG("PKCS#7 signedData needs to be of version 1"); +// return ASN_VERSION_E; +// } +// +// /* Get the set of DigestAlgorithmIdentifiers */ +// if (GetSet(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Skip the set. */ +// idx += length; +// +// /* Get the inner ContentInfo sequence */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Get the inner ContentInfo contentType */ +// if (GetContentType(pkiMsg, &idx, &contentType, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// if (contentType != DATA) { +// CYASSL_MSG("PKCS#7 inner input not of type Data"); +// return PKCS7_OID_E; +// } +// +// if (pkiMsg[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) +// return ASN_PARSE_E; +// +// if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// if (pkiMsg[idx++] != ASN_OCTET_STRING) +// return ASN_PARSE_E; +// +// if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Save the inner data as the content. */ +// if (length > 0) { +// /* Local pointer for calculating hashes later */ +// pkcs7->content = content = &pkiMsg[idx]; +// pkcs7->contentSz = contentSz = length; +// idx += length; +// } +// +// /* Get the implicit[0] set of certificates */ +// if (pkiMsg[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) { +// idx++; +// if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// if (length > 0) { +// /* At this point, idx is at the first certificate in +// * a set of certificates. There may be more than one, +// * or none, or they may be a PKCS 6 extended +// * certificate. We want to save the first cert if it +// * is X.509. */ +// +// word32 certIdx = idx; +// +// if (pkiMsg[certIdx++] == (ASN_CONSTRUCTED | ASN_SEQUENCE)) { +// if (GetLength(pkiMsg, &certIdx, &certSz, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// cert = &pkiMsg[idx]; +// certSz += (certIdx - idx); +// } +// PKCS7_InitWithCert(pkcs7, cert, certSz); +// } +// idx += length; +// } +// +// /* Get the implicit[1] set of crls */ +// if (pkiMsg[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1)) { +// idx++; +// if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Skip the set */ +// idx += length; +// } +// +// /* Get the set of signerInfos */ +// if (GetSet(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// if (length > 0) { +// /* Get the sequence of the first signerInfo */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Get the version */ +// if (GetMyVersion(pkiMsg, &idx, &version) < 0) +// return ASN_PARSE_E; +// +// if (version != 1) { +// CYASSL_MSG("PKCS#7 signerInfo needs to be of version 1"); +// return ASN_VERSION_E; +// } +// +// /* Get the sequence of IssuerAndSerialNumber */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Skip it */ +// idx += length; +// +// /* Get the sequence of digestAlgorithm */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Skip it */ +// idx += length; +// +// /* Get the IMPLICIT[0] SET OF signedAttributes */ +// if (pkiMsg[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) { +// idx++; +// +// if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// idx += length; +// } +// +// /* Get the sequence of digestEncryptionAlgorithm */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* Skip it */ +// idx += length; +// +// /* Get the signature */ +// if (pkiMsg[idx] == ASN_OCTET_STRING) { +// idx++; +// +// if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* save pointer and length */ +// sig = &pkiMsg[idx]; +// sigSz = length; +// +// idx += length; +// } +// +// pkcs7->content = content; +// pkcs7->contentSz = contentSz; +// +// { +// word32 scratch = 0; +// int plainSz = 0; +// int digestSz = MAX_SEQ_SZ + MAX_ALGO_SZ + +// MAX_OCTET_STR_SZ + SHA_DIGEST_SIZE; +// +//#ifdef CYASSL_SMALL_STACK +// byte* digest; +// RsaKey* key; +// +// digest = (byte*)XMALLOC(digestSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// +// if (digest == NULL) +// return MEMORY_E; +// +// key = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, +// DYNAMIC_TYPE_TMP_BUFFER); +// if (key == NULL) { +// XFREE(digest, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// return MEMORY_E; +// } +//#else +// byte digest[digestSz]; +// RsaKey stack_key; +// RsaKey* key = &stack_key; +//#endif +// +// XMEMSET(digest, 0, digestSz); +// +// ret = InitRsaKey(key, NULL); +// if (ret != 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(digest, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// if (RsaPublicKeyDecode(pkcs7->publicKey, &scratch, key, +// pkcs7->publicKeySz) < 0) { +// CYASSL_MSG("ASN RSA key decode error"); +//#ifdef CYASSL_SMALL_STACK +// XFREE(digest, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return PUBLIC_KEY_E; +// } +// +// plainSz = RsaSSL_Verify(sig, sigSz, digest, digestSz, key); +// FreeRsaKey(key); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(digest, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// if (plainSz < 0) +// return plainSz; +// } +// } +// +// return 0; +//} +// +// +///* create ASN.1 fomatted RecipientInfo structure, returns sequence size */ +//CYASSL_LOCAL int CreateRecipientInfo(const byte* cert, word32 certSz, +// int keyEncAlgo, int blockKeySz, +// RNG* rng, byte* contentKeyPlain, +// byte* contentKeyEnc, +// int* keyEncSz, byte* out, word32 outSz) +//{ +// word32 idx = 0; +// int ret = 0, totalSz = 0; +// int verSz, issuerSz, snSz, keyEncAlgSz; +// int issuerSeqSz, recipSeqSz, issuerSerialSeqSz; +// int encKeyOctetStrSz; +// +// byte ver[MAX_VERSION_SZ]; +// byte issuerSerialSeq[MAX_SEQ_SZ]; +// byte recipSeq[MAX_SEQ_SZ]; +// byte issuerSeq[MAX_SEQ_SZ]; +// byte encKeyOctetStr[MAX_OCTET_STR_SZ]; +// +//#ifdef CYASSL_SMALL_STACK +// byte *serial; +// byte *keyAlgArray; +// +// RsaKey* pubKey; +// DecodedCert* decoded; +// +// serial = (byte*)XMALLOC(MAX_SN_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// keyAlgArray = (byte*)XMALLOC(MAX_SN_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, +// DYNAMIC_TYPE_TMP_BUFFER); +// +// if (decoded == NULL || serial == NULL || keyAlgArray == NULL) { +// if (serial) XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (keyAlgArray) XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (decoded) XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// return MEMORY_E; +// } +// +//#else +// byte serial[MAX_SN_SZ]; +// byte keyAlgArray[MAX_ALGO_SZ]; +// +// RsaKey stack_pubKey; +// RsaKey* pubKey = &stack_pubKey; +// DecodedCert stack_decoded; +// DecodedCert* decoded = &stack_decoded; +//#endif +// +// InitDecodedCert(decoded, (byte*)cert, certSz, 0); +// ret = ParseCert(decoded, CA_TYPE, NO_VERIFY, 0); +// if (ret < 0) { +// FreeDecodedCert(decoded); +//#ifdef CYASSL_SMALL_STACK +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// +// /* version */ +// verSz = SetMyVersion(0, ver, 0); +// +// /* IssuerAndSerialNumber */ +// if (decoded->issuerRaw == NULL || decoded->issuerRawLen == 0) { +// CYASSL_MSG("DecodedCert lacks raw issuer pointer and length"); +// FreeDecodedCert(decoded); +//#ifdef CYASSL_SMALL_STACK +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return -1; +// } +// issuerSz = decoded->issuerRawLen; +// issuerSeqSz = SetSequence(issuerSz, issuerSeq); +// +// if (decoded->serial == NULL || decoded->serialSz == 0) { +// CYASSL_MSG("DecodedCert missing serial number"); +// FreeDecodedCert(decoded); +//#ifdef CYASSL_SMALL_STACK +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return -1; +// } +// snSz = SetSerialNumber(decoded->serial, decoded->serialSz, serial); +// +// issuerSerialSeqSz = SetSequence(issuerSeqSz + issuerSz + snSz, +// issuerSerialSeq); +// +// /* KeyEncryptionAlgorithmIdentifier, only support RSA now */ +// if (keyEncAlgo != RSAk) { +// FreeDecodedCert(decoded); +//#ifdef CYASSL_SMALL_STACK +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ALGO_ID_E; +// } +// +// keyEncAlgSz = SetAlgoID(keyEncAlgo, keyAlgArray, keyType, 0); +// if (keyEncAlgSz == 0) { +// FreeDecodedCert(decoded); +//#ifdef CYASSL_SMALL_STACK +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return BAD_FUNC_ARG; +// } +// +//#ifdef CYASSL_SMALL_STACK +// pubKey = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (pubKey == NULL) { +// FreeDecodedCert(decoded); +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// return MEMORY_E; +// } +//#endif +// +// /* EncryptedKey */ +// ret = InitRsaKey(pubKey, 0); +// if (ret != 0) { +// FreeDecodedCert(decoded); +//#ifdef CYASSL_SMALL_STACK +// XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// +// if (RsaPublicKeyDecode(decoded->publicKey, &idx, pubKey, +// decoded->pubKeySize) < 0) { +// CYASSL_MSG("ASN RSA key decode error"); +// FreeRsaKey(pubKey); +// FreeDecodedCert(decoded); +//#ifdef CYASSL_SMALL_STACK +// XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return PUBLIC_KEY_E; +// } +// +// *keyEncSz = RsaPublicEncrypt(contentKeyPlain, blockKeySz, contentKeyEnc, +// MAX_ENCRYPTED_KEY_SZ, pubKey, rng); +// FreeRsaKey(pubKey); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// if (*keyEncSz < 0) { +// CYASSL_MSG("RSA Public Encrypt failed"); +// FreeDecodedCert(decoded); +//#ifdef CYASSL_SMALL_STACK +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return *keyEncSz; +// } +// +// encKeyOctetStrSz = SetOctetString(*keyEncSz, encKeyOctetStr); +// +// /* RecipientInfo */ +// recipSeqSz = SetSequence(verSz + issuerSerialSeqSz + issuerSeqSz + +// issuerSz + snSz + keyEncAlgSz + encKeyOctetStrSz + +// *keyEncSz, recipSeq); +// +// if (recipSeqSz + verSz + issuerSerialSeqSz + issuerSeqSz + snSz + +// keyEncAlgSz + encKeyOctetStrSz + *keyEncSz > (int)outSz) { +// CYASSL_MSG("RecipientInfo output buffer too small"); +// FreeDecodedCert(decoded); +//#ifdef CYASSL_SMALL_STACK +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return BUFFER_E; +// } +// +// XMEMCPY(out + totalSz, recipSeq, recipSeqSz); +// totalSz += recipSeqSz; +// XMEMCPY(out + totalSz, ver, verSz); +// totalSz += verSz; +// XMEMCPY(out + totalSz, issuerSerialSeq, issuerSerialSeqSz); +// totalSz += issuerSerialSeqSz; +// XMEMCPY(out + totalSz, issuerSeq, issuerSeqSz); +// totalSz += issuerSeqSz; +// XMEMCPY(out + totalSz, decoded->issuerRaw, issuerSz); +// totalSz += issuerSz; +// XMEMCPY(out + totalSz, serial, snSz); +// totalSz += snSz; +// XMEMCPY(out + totalSz, keyAlgArray, keyEncAlgSz); +// totalSz += keyEncAlgSz; +// XMEMCPY(out + totalSz, encKeyOctetStr, encKeyOctetStrSz); +// totalSz += encKeyOctetStrSz; +// XMEMCPY(out + totalSz, contentKeyEnc, *keyEncSz); +// totalSz += *keyEncSz; +// +// FreeDecodedCert(decoded); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(serial, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(keyAlgArray, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// return totalSz; +//} +// +// +///* build PKCS#7 envelopedData content type, return enveloped size */ +//int PKCS7_EncodeEnvelopedData(PKCS7* pkcs7, byte* output, word32 outputSz) +//{ +// int i, ret = 0, idx = 0; +// int totalSz = 0, padSz = 0, desOutSz = 0; +// +// int contentInfoSeqSz, outerContentTypeSz, outerContentSz; +// byte contentInfoSeq[MAX_SEQ_SZ]; +// byte outerContentType[MAX_ALGO_SZ]; +// byte outerContent[MAX_SEQ_SZ]; +// +// int envDataSeqSz, verSz; +// byte envDataSeq[MAX_SEQ_SZ]; +// byte ver[MAX_VERSION_SZ]; +// +// RNG rng; +// int contentKeyEncSz, blockKeySz; +// int dynamicFlag = 0; +// byte contentKeyPlain[MAX_CONTENT_KEY_LEN]; +//#ifdef CYASSL_SMALL_STACK +// byte* contentKeyEnc; +//#else +// byte contentKeyEnc[MAX_ENCRYPTED_KEY_SZ]; +//#endif +// byte* plain; +// byte* encryptedContent; +// +// int recipSz, recipSetSz; +//#ifdef CYASSL_SMALL_STACK +// byte* recip; +//#else +// byte recip[MAX_RECIP_SZ]; +//#endif +// byte recipSet[MAX_SET_SZ]; +// +// int encContentOctetSz, encContentSeqSz, contentTypeSz; +// int contentEncAlgoSz, ivOctetStringSz; +// byte encContentSeq[MAX_SEQ_SZ]; +// byte contentType[MAX_ALGO_SZ]; +// byte contentEncAlgo[MAX_ALGO_SZ]; +// byte tmpIv[DES_BLOCK_SIZE]; +// byte ivOctetString[MAX_OCTET_STR_SZ]; +// byte encContentOctet[MAX_OCTET_STR_SZ]; +// +// if (pkcs7 == NULL || pkcs7->content == NULL || pkcs7->contentSz == 0 || +// pkcs7->encryptOID == 0 || pkcs7->singleCert == NULL) +// return BAD_FUNC_ARG; +// +// if (output == NULL || outputSz == 0) +// return BAD_FUNC_ARG; +// +// /* PKCS#7 only supports DES, 3DES for now */ +// switch (pkcs7->encryptOID) { +// case DESb: +// blockKeySz = DES_KEYLEN; +// break; +// +// case DES3b: +// blockKeySz = DES3_KEYLEN; +// break; +// +// default: +// CYASSL_MSG("Unsupported content cipher type"); +// return ALGO_ID_E; +// }; +// +// /* outer content type */ +// outerContentTypeSz = SetContentType(ENVELOPED_DATA, outerContentType); +// +// /* version, defined as 0 in RFC 2315 */ +// verSz = SetMyVersion(0, ver, 0); +// +// /* generate random content encryption key */ +// ret = InitRng(&rng); +// if (ret != 0) +// return ret; +// +// ret = RNG_GenerateBlock(&rng, contentKeyPlain, blockKeySz); +// if (ret != 0) +// return ret; +// +//#ifdef CYASSL_SMALL_STACK +// recip = (byte*)XMALLOC(MAX_RECIP_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// contentKeyEnc = (byte*)XMALLOC(MAX_ENCRYPTED_KEY_SZ, NULL, +// DYNAMIC_TYPE_TMP_BUFFER); +// if (contentKeyEnc == NULL || recip == NULL) { +// if (recip) XFREE(recip, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (contentKeyEnc) XFREE(contentKeyEnc, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// return MEMORY_E; +// } +// +//#endif +// +// /* build RecipientInfo, only handle 1 for now */ +// recipSz = CreateRecipientInfo(pkcs7->singleCert, pkcs7->singleCertSz, RSAk, +// blockKeySz, &rng, contentKeyPlain, +// contentKeyEnc, &contentKeyEncSz, recip, +// MAX_RECIP_SZ); +// +// XMEMSET(contentKeyEnc, 0, MAX_ENCRYPTED_KEY_SZ); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(contentKeyEnc, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// if (recipSz < 0) { +// CYASSL_MSG("Failed to create RecipientInfo"); +//#ifdef CYASSL_SMALL_STACK +// XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +//#endif +// return recipSz; +// } +// recipSetSz = SetSet(recipSz, recipSet); +// +// /* generate IV for block cipher */ +// ret = RNG_GenerateBlock(&rng, tmpIv, DES_BLOCK_SIZE); +// if (ret != 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// +// /* EncryptedContentInfo */ +// contentTypeSz = SetContentType(pkcs7->contentOID, contentType); +// if (contentTypeSz == 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +//#endif +// return BAD_FUNC_ARG; +// } +// +// /* allocate encrypted content buffer, pad if necessary, PKCS#7 padding */ +// padSz = DES_BLOCK_SIZE - (pkcs7->contentSz % DES_BLOCK_SIZE); +// desOutSz = pkcs7->contentSz + padSz; +// +// if (padSz != 0) { +// plain = (byte*)XMALLOC(desOutSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (plain == NULL) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +//#endif +// return MEMORY_E; +// } +// XMEMCPY(plain, pkcs7->content, pkcs7->contentSz); +// dynamicFlag = 1; +// +// for (i = 0; i < padSz; i++) { +// plain[pkcs7->contentSz + i] = padSz; +// } +// +// } else { +// plain = pkcs7->content; +// desOutSz = pkcs7->contentSz; +// } +// +// encryptedContent = (byte*)XMALLOC(desOutSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (encryptedContent == NULL) { +// if (dynamicFlag) +// XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +//#endif +// return MEMORY_E; +// } +// +// /* put together IV OCTET STRING */ +// ivOctetStringSz = SetOctetString(DES_BLOCK_SIZE, ivOctetString); +// +// /* build up our ContentEncryptionAlgorithmIdentifier sequence, +// * adding (ivOctetStringSz + DES_BLOCK_SIZE) for IV OCTET STRING */ +// contentEncAlgoSz = SetAlgoID(pkcs7->encryptOID, contentEncAlgo, +// blkType, ivOctetStringSz + DES_BLOCK_SIZE); +// +// if (contentEncAlgoSz == 0) { +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (dynamicFlag) +// XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +//#endif +// return BAD_FUNC_ARG; +// } +// +// /* encrypt content */ +// if (pkcs7->encryptOID == DESb) { +// Des des; +// +// ret = Des_SetKey(&des, contentKeyPlain, tmpIv, DES_ENCRYPTION); +// +// if (ret == 0) +// Des_CbcEncrypt(&des, encryptedContent, plain, desOutSz); +// +// if (ret != 0) { +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (dynamicFlag) +// XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// } +// else if (pkcs7->encryptOID == DES3b) { +// Des3 des3; +// +// ret = Des3_SetKey(&des3, contentKeyPlain, tmpIv, DES_ENCRYPTION); +// +// if (ret == 0) +// ret = Des3_CbcEncrypt(&des3, encryptedContent, plain, desOutSz); +// +// if (ret != 0) { +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (dynamicFlag) +// XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// } +// +// encContentOctetSz = SetImplicit(ASN_OCTET_STRING, 0, +// desOutSz, encContentOctet); +// +// encContentSeqSz = SetSequence(contentTypeSz + contentEncAlgoSz + +// ivOctetStringSz + DES_BLOCK_SIZE + +// encContentOctetSz + desOutSz, encContentSeq); +// +// /* keep track of sizes for outer wrapper layering */ +// totalSz = verSz + recipSetSz + recipSz + encContentSeqSz + contentTypeSz + +// contentEncAlgoSz + ivOctetStringSz + DES_BLOCK_SIZE + +// encContentOctetSz + desOutSz; +// +// /* EnvelopedData */ +// envDataSeqSz = SetSequence(totalSz, envDataSeq); +// totalSz += envDataSeqSz; +// +// /* outer content */ +// outerContentSz = SetExplicit(0, totalSz, outerContent); +// totalSz += outerContentTypeSz; +// totalSz += outerContentSz; +// +// /* ContentInfo */ +// contentInfoSeqSz = SetSequence(totalSz, contentInfoSeq); +// totalSz += contentInfoSeqSz; +// +// if (totalSz > (int)outputSz) { +// CYASSL_MSG("Pkcs7_encrypt output buffer too small"); +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (dynamicFlag) +// XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +//#endif +// return BUFFER_E; +// } +// +// XMEMCPY(output + idx, contentInfoSeq, contentInfoSeqSz); +// idx += contentInfoSeqSz; +// XMEMCPY(output + idx, outerContentType, outerContentTypeSz); +// idx += outerContentTypeSz; +// XMEMCPY(output + idx, outerContent, outerContentSz); +// idx += outerContentSz; +// XMEMCPY(output + idx, envDataSeq, envDataSeqSz); +// idx += envDataSeqSz; +// XMEMCPY(output + idx, ver, verSz); +// idx += verSz; +// XMEMCPY(output + idx, recipSet, recipSetSz); +// idx += recipSetSz; +// XMEMCPY(output + idx, recip, recipSz); +// idx += recipSz; +// XMEMCPY(output + idx, encContentSeq, encContentSeqSz); +// idx += encContentSeqSz; +// XMEMCPY(output + idx, contentType, contentTypeSz); +// idx += contentTypeSz; +// XMEMCPY(output + idx, contentEncAlgo, contentEncAlgoSz); +// idx += contentEncAlgoSz; +// XMEMCPY(output + idx, ivOctetString, ivOctetStringSz); +// idx += ivOctetStringSz; +// XMEMCPY(output + idx, tmpIv, DES_BLOCK_SIZE); +// idx += DES_BLOCK_SIZE; +// XMEMCPY(output + idx, encContentOctet, encContentOctetSz); +// idx += encContentOctetSz; +// XMEMCPY(output + idx, encryptedContent, desOutSz); +// idx += desOutSz; +// +//#if defined(HAVE_HASHDRBG) || defined(NO_RC4) +// FreeRng(&rng); +//#endif +// +// XMEMSET(contentKeyPlain, 0, MAX_CONTENT_KEY_LEN); +// +// if (dynamicFlag) +// XFREE(plain, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); +//#endif +// +// return idx; +//} +// +///* unwrap and decrypt PKCS#7 envelopedData object, return decoded size */ +//CYASSL_API int PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg, +// word32 pkiMsgSz, byte* output, +// word32 outputSz) +//{ +// int recipFound = 0; +// int ret, version, length; +// word32 savedIdx = 0, idx = 0; +// word32 contentType, encOID; +// byte issuerHash[SHA_DIGEST_SIZE]; +// +// int encryptedKeySz, keySz; +// byte tmpIv[DES_BLOCK_SIZE]; +// byte* decryptedKey = NULL; +// +//#ifdef CYASSL_SMALL_STACK +// mp_int* serialNum; +// byte* encryptedKey; +// RsaKey* privKey; +//#else +// mp_int stack_serialNum; +// mp_int* serialNum = &stack_serialNum; +// byte encryptedKey[MAX_ENCRYPTED_KEY_SZ]; +// +// RsaKey stack_privKey; +// RsaKey* privKey = &stack_privKey; +//#endif +// int encryptedContentSz; +// byte padLen; +// byte* encryptedContent = NULL; +// +// if (pkcs7 == NULL || pkcs7->singleCert == NULL || +// pkcs7->singleCertSz == 0 || pkcs7->privateKey == NULL || +// pkcs7->privateKeySz == 0) +// return BAD_FUNC_ARG; +// +// if (pkiMsg == NULL || pkiMsgSz == 0 || +// output == NULL || outputSz == 0) +// return BAD_FUNC_ARG; +// +// /* read past ContentInfo, verify type is envelopedData */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// if (GetContentType(pkiMsg, &idx, &contentType, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// if (contentType != ENVELOPED_DATA) { +// CYASSL_MSG("PKCS#7 input not of type EnvelopedData"); +// return PKCS7_OID_E; +// } +// +// if (pkiMsg[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) +// return ASN_PARSE_E; +// +// if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// /* remove EnvelopedData and version */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +// if (GetMyVersion(pkiMsg, &idx, &version) < 0) +// return ASN_PARSE_E; +// +// if (version != 0) { +// CYASSL_MSG("PKCS#7 envelopedData needs to be of version 0"); +// return ASN_VERSION_E; +// } +// +// /* walk through RecipientInfo set, find correct recipient */ +// if (GetSet(pkiMsg, &idx, &length, pkiMsgSz) < 0) +// return ASN_PARSE_E; +// +//#ifdef CYASSL_SMALL_STACK +// encryptedKey = (byte*)XMALLOC(MAX_ENCRYPTED_KEY_SZ, NULL, +// DYNAMIC_TYPE_TMP_BUFFER); +// if (encryptedKey == NULL) +// return MEMORY_E; +//#endif +// +// savedIdx = idx; +// recipFound = 0; +// +// /* when looking for next recipient, use first sequence and version to +// * indicate there is another, if not, move on */ +// while(recipFound == 0) { +// +// /* remove RecipientInfo, if we don't have a SEQUENCE, back up idx to +// * last good saved one */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) { +// idx = savedIdx; +// break; +// } +// +// if (GetMyVersion(pkiMsg, &idx, &version) < 0) { +// idx = savedIdx; +// break; +// } +// +// if (version != 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_VERSION_E; +// } +// +// /* remove IssuerAndSerialNumber */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// if (GetNameHash(pkiMsg, &idx, issuerHash, pkiMsgSz) < 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// /* if we found correct recipient, issuer hashes will match */ +// if (XMEMCMP(issuerHash, pkcs7->issuerHash, SHA_DIGEST_SIZE) == 0) { +// recipFound = 1; +// } +// +//#ifdef CYASSL_SMALL_STACK +// serialNum = (mp_int*)XMALLOC(sizeof(mp_int), NULL, +// DYNAMIC_TYPE_TMP_BUFFER); +// if (serialNum == NULL) { +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// return MEMORY_E; +// } +//#endif +// +// if (GetInt(serialNum, pkiMsg, &idx, pkiMsgSz) < 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(serialNum, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// mp_clear(serialNum); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(serialNum, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// if (GetAlgoId(pkiMsg, &idx, &encOID, pkiMsgSz) < 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// /* key encryption algorithm must be RSA for now */ +// if (encOID != RSAk) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ALGO_ID_E; +// } +// +// /* read encryptedKey */ +// if (pkiMsg[idx++] != ASN_OCTET_STRING) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// if (GetLength(pkiMsg, &idx, &encryptedKeySz, pkiMsgSz) < 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// if (recipFound == 1) +// XMEMCPY(encryptedKey, &pkiMsg[idx], encryptedKeySz); +// idx += encryptedKeySz; +// +// /* update good idx */ +// savedIdx = idx; +// } +// +// if (recipFound == 0) { +// CYASSL_MSG("No recipient found in envelopedData that matches input"); +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return PKCS7_RECIP_E; +// } +// +// /* remove EncryptedContentInfo */ +// if (GetSequence(pkiMsg, &idx, &length, pkiMsgSz) < 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// if (GetContentType(pkiMsg, &idx, &contentType, pkiMsgSz) < 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// if (GetAlgoId(pkiMsg, &idx, &encOID, pkiMsgSz) < 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// /* get block cipher IV, stored in OPTIONAL parameter of AlgoID */ +// if (pkiMsg[idx++] != ASN_OCTET_STRING) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// if (GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// if (length != DES_BLOCK_SIZE) { +// CYASSL_MSG("Incorrect IV length, must be of DES_BLOCK_SIZE"); +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// XMEMCPY(tmpIv, &pkiMsg[idx], length); +// idx += length; +// +// /* read encryptedContent, cont[0] */ +// if (pkiMsg[idx++] != (ASN_CONTEXT_SPECIFIC | 0)) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// if (GetLength(pkiMsg, &idx, &encryptedContentSz, pkiMsgSz) < 0) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ASN_PARSE_E; +// } +// +// encryptedContent = (byte*)XMALLOC(encryptedContentSz, NULL, +// DYNAMIC_TYPE_TMP_BUFFER); +// if (encryptedContent == NULL) { +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return MEMORY_E; +// } +// +// XMEMCPY(encryptedContent, &pkiMsg[idx], encryptedContentSz); +// +// /* load private key */ +//#ifdef CYASSL_SMALL_STACK +// privKey = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_TMP_BUFFER); +// if (privKey == NULL) { +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); return MEMORY_E; +// } +//#endif +// +// ret = InitRsaKey(privKey, 0); +// if (ret != 0) { +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// +// idx = 0; +// +// ret = RsaPrivateKeyDecode(pkcs7->privateKey, &idx, privKey, +// pkcs7->privateKeySz); +// if (ret != 0) { +// CYASSL_MSG("Failed to decode RSA private key"); +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// +// /* decrypt encryptedKey */ +// keySz = RsaPrivateDecryptInline(encryptedKey, encryptedKeySz, +// &decryptedKey, privKey); +// FreeRsaKey(privKey); +// +//#ifdef CYASSL_SMALL_STACK +// XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// if (keySz <= 0) { +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return keySz; +// } +// +// /* decrypt encryptedContent */ +// if (encOID == DESb) { +// Des des; +// ret = Des_SetKey(&des, decryptedKey, tmpIv, DES_DECRYPTION); +// +// if (ret == 0) +// Des_CbcDecrypt(&des, encryptedContent, encryptedContent, +// encryptedContentSz); +// +// if (ret != 0) { +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// } +// else if (encOID == DES3b) { +// Des3 des; +// ret = Des3_SetKey(&des, decryptedKey, tmpIv, DES_DECRYPTION); +// if (ret == 0) +// ret = Des3_CbcDecrypt(&des, encryptedContent, encryptedContent, +// encryptedContentSz); +// +// if (ret != 0) { +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ret; +// } +// } else { +// CYASSL_MSG("Unsupported content encryption OID type"); +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// return ALGO_ID_E; +// } +// +// padLen = encryptedContent[encryptedContentSz-1]; +// +// /* copy plaintext to output */ +// XMEMCPY(output, encryptedContent, encryptedContentSz - padLen); +// +// /* free memory, zero out keys */ +// XMEMSET(encryptedKey, 0, MAX_ENCRYPTED_KEY_SZ); +// XMEMSET(encryptedContent, 0, encryptedContentSz); +// XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#ifdef CYASSL_SMALL_STACK +// XFREE(encryptedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +//#endif +// +// return encryptedContentSz - padLen; +//} +// +// +//#else /* HAVE_PKCS7 */ +// +// +//#ifdef _MSC_VER +// /* 4206 warning for blank file */ +// #pragma warning(disable: 4206) +//#endif +// +// +//#endif /* HAVE_PKCS7 */ +// diff --git a/cyassl-ios.xcodeproj/project.pbxproj b/cyassl-ios.xcodeproj/project.pbxproj index 6c342cc81..782156110 100644 --- a/cyassl-ios.xcodeproj/project.pbxproj +++ b/cyassl-ios.xcodeproj/project.pbxproj @@ -57,7 +57,7 @@ /* Begin PBXFileReference section */ 52397C5C17E0E63200517C9A /* port.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = port.c; path = ctaocrypt/src/port.c; sourceTree = SOURCE_ROOT; }; - 52B1344D16F3C9E800C07B32 /* libcyassl-ios.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libcyassl-ios.a"; sourceTree = BUILT_PRODUCTS_DIR; }; + 52B1344D16F3C9E800C07B32 /* libwolfssl-ios.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libwolfssl-ios.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 52B1347B16F3CCC400C07B32 /* tls.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = tls.c; path = src/tls.c; sourceTree = SOURCE_ROOT; }; 52B1347C16F3CCC400C07B32 /* ssl.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ssl.c; path = src/ssl.c; sourceTree = SOURCE_ROOT; }; 52B1347D16F3CCC400C07B32 /* ocsp.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ocsp.c; path = src/ocsp.c; sourceTree = SOURCE_ROOT; }; @@ -115,7 +115,7 @@ 52B1344E16F3C9E800C07B32 /* Products */ = { isa = PBXGroup; children = ( - 52B1344D16F3C9E800C07B32 /* libcyassl-ios.a */, + 52B1344D16F3C9E800C07B32 /* libwolfssl-ios.a */, ); name = Products; sourceTree = ""; @@ -195,7 +195,7 @@ ); name = "cyassl-ios"; productName = "cyassl-ios"; - productReference = 52B1344D16F3C9E800C07B32 /* libcyassl-ios.a */; + productReference = 52B1344D16F3C9E800C07B32 /* libwolfssl-ios.a */; productType = "com.apple.product-type.library.static"; }; /* End PBXNativeTarget section */ diff --git a/cyassl-iphone.xcodeproj/project.pbxproj b/cyassl-iphone.xcodeproj/project.pbxproj index 53b74bebe..2714c72f1 100644 --- a/cyassl-iphone.xcodeproj/project.pbxproj +++ b/cyassl-iphone.xcodeproj/project.pbxproj @@ -98,7 +98,7 @@ 43CB530D116E9FD5000A264B /* iphone-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "iphone-Info.plist"; sourceTree = ""; }; 43D565640F1EC9A600550C88 /* hc128.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; lineEnding = 2; name = hc128.c; path = ctaocrypt/src/hc128.c; sourceTree = ""; }; 43D565660F1EC9CC00550C88 /* rabbit.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; lineEnding = 2; name = rabbit.c; path = ctaocrypt/src/rabbit.c; sourceTree = ""; }; - D2AAC046055464E500DB518D /* libcyassl.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libcyassl.a; sourceTree = BUILT_PRODUCTS_DIR; }; + D2AAC046055464E500DB518D /* libwolfssl.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libwolfssl.a; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -135,7 +135,7 @@ 1AB674ADFE9D54B511CA2CBB /* Products */ = { isa = PBXGroup; children = ( - D2AAC046055464E500DB518D /* libcyassl.a */, + D2AAC046055464E500DB518D /* libwolfssl.a */, ); name = Products; sourceTree = ""; @@ -220,7 +220,7 @@ ); name = cyassl; productName = cyassl; - productReference = D2AAC046055464E500DB518D /* libcyassl.a */; + productReference = D2AAC046055464E500DB518D /* libwolfssl.a */; productType = "com.apple.product-type.library.static"; }; /* End PBXNativeTarget section */ diff --git a/cyassl/ctaocrypt/arc4.h b/cyassl/ctaocrypt/arc4.h index b20779d8c..a3b1c2499 100644 --- a/cyassl/ctaocrypt/arc4.h +++ b/cyassl/ctaocrypt/arc4.h @@ -24,4 +24,15 @@ #include +/* for arc4 reverse compatibility */ +#ifndef NO_RC4 +#include + #define CYASSL_ARC4_CAVIUM_MAGIC WOLFSSL_ARC4_CAVIUM_MAGIC + #define Arc4Process wc_Arc4Process + #define Arc4SetKey wc_Arc4SetKey + #define Arc4InitCavium wc_Arc4InitCavium + #define Arc4FreeCavium wc_Arc4FreeCavium #endif + +#endif /* CTAO_CRYPT_ARC4_H */ + diff --git a/cyassl/ctaocrypt/blake2.h b/cyassl/ctaocrypt/blake2.h index ba5ec6fd6..596a4bb91 100644 --- a/cyassl/ctaocrypt/blake2.h +++ b/cyassl/ctaocrypt/blake2.h @@ -25,34 +25,13 @@ #ifndef CTAOCRYPT_BLAKE2_H #define CTAOCRYPT_BLAKE2_H -#include +#include -#ifdef __cplusplus - extern "C" { -#endif - -/* in bytes, variable digest size up to 512 bits (64 bytes) */ -enum { - BLAKE2B_ID = 7, /* hash type unique */ - BLAKE2B_256 = 32 /* 256 bit type, SSL default */ -}; - - -/* BLAKE2b digest */ -typedef struct Blake2b { - blake2b_state S[1]; /* our state */ - word32 digestSz; /* digest size used on init */ -} Blake2b; - - -CYASSL_API int InitBlake2b(Blake2b*, word32); -CYASSL_API int Blake2bUpdate(Blake2b*, const byte*, word32); -CYASSL_API int Blake2bFinal(Blake2b*, byte*, word32); - - - -#ifdef __cplusplus - } +/* for blake2 reverse compatibility */ +#ifdef HAVE_BLAKE2 + #define InitBlake2b wc_InitBlake2b + #define Blake2bUpdate wc_Blake2bUpdate + #define Blake2bFinal wc_Blake2bFinal #endif #endif /* CTAOCRYPT_BLAKE2_H */ diff --git a/cyassl/ctaocrypt/camellia.h b/cyassl/ctaocrypt/camellia.h index b42bce43b..831b57f16 100644 --- a/cyassl/ctaocrypt/camellia.h +++ b/cyassl/ctaocrypt/camellia.h @@ -24,4 +24,15 @@ #include +/* for blake2 reverse compatibility */ +#ifdef HAVE_CAMELLIA + #define CamelliaSetKey wc_CamelliaSetKey + #define CamelliaSetIV wc_CamelliaSetIV + #define CamelliaEncryptDirect wc_CamelliaEncryptDirect + #define CamelliaDecryptDirect wc_CamelliaDecryptDirect + #define CamelliaCbcEncrypt wc_CamelliaCbcEncrypt + #define CamelliaCbcDecrypt wc_CamelliaCbcDecrypt #endif + +#endif /* CTAO_CRYPT_CAMELLIA_H */ + diff --git a/cyassl/ctaocrypt/chacha.h b/cyassl/ctaocrypt/chacha.h index 2eb8065fd..74b4c49c2 100644 --- a/cyassl/ctaocrypt/chacha.h +++ b/cyassl/ctaocrypt/chacha.h @@ -22,34 +22,12 @@ #ifndef CTAO_CRYPT_CHACHA_H #define CTAO_CRYPT_CHACHA_H -#include "types.h" +#include -#ifdef __cplusplus - extern "C" { -#endif +/* for chacha reverse compatibility */ +#define Chacha_Process wc_Chacha_Process +#define Chacha_SetKey wc_Chacha_SetKey +#define Chacha_SetIV wc_Chacha_SetIV - -enum { - CHACHA_ENC_TYPE = 7 /* cipher unique type */ -}; - -typedef struct ChaCha { - word32 X[16]; /* state of cipher */ -} ChaCha; - -CYASSL_API int Chacha_Process(ChaCha* ctx, byte* cipher, const byte* plain, - word32 msglen); -CYASSL_API int Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz); - -/** - * IV(nonce) changes with each record - * counter is for what value the block counter should start ... usually 0 - */ -CYASSL_API int Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter); - -#ifdef __cplusplus - } /* extern "C" */ -#endif - -#endif +#endif /* CTAO_CRYPT_CHACHA_H */ diff --git a/cyassl/ctaocrypt/compress.h b/cyassl/ctaocrypt/compress.h index ecf162204..cf5f45627 100644 --- a/cyassl/ctaocrypt/compress.h +++ b/cyassl/ctaocrypt/compress.h @@ -25,27 +25,32 @@ #ifndef CTAO_CRYPT_COMPRESS_H #define CTAO_CRYPT_COMPRESS_H +#include -#include - - -#ifdef __cplusplus - extern "C" { -#endif - - -#define COMPRESS_FIXED 1 - - -CYASSL_API int Compress(byte*, word32, const byte*, word32, word32); -CYASSL_API int DeCompress(byte*, word32, const byte*, word32); - - -#ifdef __cplusplus - } /* extern "C" */ -#endif - +/* reverse compatibility */ +#define Compress wc_Compress +#define DeCompress wc_DeCompress +//#include +// +// +//#ifdef __cplusplus +// extern "C" { +//#endif +// +// +//#define COMPRESS_FIXED 1 +// +// +//CYASSL_API int Compress(byte*, word32, const byte*, word32, word32); +//CYASSL_API int DeCompress(byte*, word32, const byte*, word32); +// +// +//#ifdef __cplusplus +// } /* extern "C" */ +//#endif +// +// #endif /* CTAO_CRYPT_COMPRESS_H */ #endif /* HAVE_LIBZ */ diff --git a/cyassl/ctaocrypt/dh.h b/cyassl/ctaocrypt/dh.h index f700e3332..518d6445e 100644 --- a/cyassl/ctaocrypt/dh.h +++ b/cyassl/ctaocrypt/dh.h @@ -25,41 +25,52 @@ #ifndef CTAO_CRYPT_DH_H #define CTAO_CRYPT_DH_H -#include -#include -#include +#include -#ifdef __cplusplus - extern "C" { -#endif +/* for dh reverse compatibility */ + #define InitDhKey wc_InitDhKey + #define FreeDhKey wc_FreeDhKey + #define DhGenerateKeyPair wc_DhGenerateKeyPair + #define DhAgree wc_DhAgree + #define DhKeyDecode wc_DhKeyDecode + #define DhSetKey wc_DhSetKey + #define DhParamsLoad wc_DhParamsLoad - -/* Diffie-Hellman Key */ -typedef struct DhKey { - mp_int p, g; /* group parameters */ -} DhKey; - - -CYASSL_API void InitDhKey(DhKey* key); -CYASSL_API void FreeDhKey(DhKey* key); - -CYASSL_API int DhGenerateKeyPair(DhKey* key, RNG* rng, byte* priv, - word32* privSz, byte* pub, word32* pubSz); -CYASSL_API int DhAgree(DhKey* key, byte* agree, word32* agreeSz, - const byte* priv, word32 privSz, const byte* otherPub, - word32 pubSz); - -CYASSL_API int DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, - word32); -CYASSL_API int DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g, - word32 gSz); -CYASSL_API int DhParamsLoad(const byte* input, word32 inSz, byte* p, - word32* pInOutSz, byte* g, word32* gInOutSz); - - -#ifdef __cplusplus - } /* extern "C" */ -#endif +//#include +//#include +//#include +// +//#ifdef __cplusplus +// extern "C" { +//#endif +// +// +///* Diffie-Hellman Key */ +//typedef struct DhKey { +// mp_int p, g; /* group parameters */ +//} DhKey; +// +// +//CYASSL_API void InitDhKey(DhKey* key); +//CYASSL_API void FreeDhKey(DhKey* key); +// +//CYASSL_API int DhGenerateKeyPair(DhKey* key, RNG* rng, byte* priv, +// word32* privSz, byte* pub, word32* pubSz); +//CYASSL_API int DhAgree(DhKey* key, byte* agree, word32* agreeSz, +// const byte* priv, word32 privSz, const byte* otherPub, +// word32 pubSz); +// +//CYASSL_API int DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, +// word32); +//CYASSL_API int DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g, +// word32 gSz); +//CYASSL_API int DhParamsLoad(const byte* input, word32 inSz, byte* p, +// word32* pInOutSz, byte* g, word32* gInOutSz); +// +// +//#ifdef __cplusplus +// } /* extern "C" */ +//#endif #endif /* CTAO_CRYPT_DH_H */ diff --git a/cyassl/ctaocrypt/dsa.h b/cyassl/ctaocrypt/dsa.h index 14db625e3..3f2365c04 100644 --- a/cyassl/ctaocrypt/dsa.h +++ b/cyassl/ctaocrypt/dsa.h @@ -25,43 +25,8 @@ #ifndef CTAO_CRYPT_DSA_H #define CTAO_CRYPT_DSA_H -#include -#include -#include +#include -#ifdef __cplusplus - extern "C" { -#endif - - -enum { - DSA_PUBLIC = 0, - DSA_PRIVATE = 1 -}; - -/* DSA */ -typedef struct DsaKey { - mp_int p, q, g, y, x; - int type; /* public or private */ -} DsaKey; - - -CYASSL_API void InitDsaKey(DsaKey* key); -CYASSL_API void FreeDsaKey(DsaKey* key); - -CYASSL_API int DsaSign(const byte* digest, byte* out, DsaKey* key, RNG* rng); -CYASSL_API int DsaVerify(const byte* digest, const byte* sig, DsaKey* key, - int* answer); - -CYASSL_API int DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey*, - word32); -CYASSL_API int DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey*, - word32); - -#ifdef __cplusplus - } /* extern "C" */ -#endif - -#endif /* WOLF_CRYPT_DSA_H */ +#endif /* CTAO_CRYPT_DSA_H */ #endif /* NO_DSA */ diff --git a/cyassl/ctaocrypt/ecc.h b/cyassl/ctaocrypt/ecc.h index 291dddc96..c325e68b6 100644 --- a/cyassl/ctaocrypt/ecc.h +++ b/cyassl/ctaocrypt/ecc.h @@ -25,6 +25,39 @@ #define CTAO_CRYPT_ECC_H #include + +/* for ecc reverse compatibility */ +#ifdef HAVE_ECC + #define ecc_make_key wc_ecc_make_key + #define ecc_shared_secret wc_ecc_shared_secret + #define ecc_sign_hash wc_ecc_sign_hash + #define ecc_verify_hash wc_ecc_verify_hash + #define ecc_init wc_ecc_init + #define ecc_free wc_ecc_free + #define ecc_fp_free wc_ecc_fp_free + #define ecc_export_x963 wc_ecc_export_x963 + #define ecc_size wc_ecc_size + #define ecc_sig_size wc_ecc_sig_size + #define ecc_export_x963_ex wc_ecc_export_x963_ex + #define ecc_import_x963 wc_ecc_import_x963 + #define ecc_import_private_key wc_ecc_import_private_key + #define ecc_rs_to_sig wc_ecc_rs_to_sig + #define ecc_import_raw wc_ecc_import_raw + #define ecc_export_private_only wc_ecc_export_private_only + +#ifdef HAVE_ECC_ENCRYPT + /* ecc encrypt */ + #define ecc_ctx_new wc_ecc_ctx_new + #define ecc_ctx_free wc_ecc_ctx_free + #define ecc_ctx_reset wc_ecc_ctx_reset + #define ecc_ctx_get_own_salt wc_ecc_ctx_get_own_salt + #define ecc_ctx_set_peer_salt wc_ecc_ctx_set_peer_salt + #define ecc_ctx_set_info wc_ecc_ctx_set_info + #define ecc_encrypt wc_ecc_encrypt + #define ecc_decrypt wc_ecc_decrypt +#endif /* HAVE_ECC_ENCRYPT */ +#endif + // //#include //#include diff --git a/cyassl/ctaocrypt/error-crypt.h b/cyassl/ctaocrypt/error-crypt.h index ad6e4966f..3e6f072ce 100644 --- a/cyassl/ctaocrypt/error-crypt.h +++ b/cyassl/ctaocrypt/error-crypt.h @@ -23,6 +23,10 @@ #ifndef CTAO_CRYPT_ERROR_H #define CTAO_CRYPT_ERROR_H +/* for name change and fips compatibility @wc_fips */ +#ifndef HAVE_FIPS + #include +#else #include @@ -150,7 +154,7 @@ CYASSL_API const char* CTaoCryptGetErrorString(int error); #ifdef __cplusplus } /* extern "C" */ #endif - +#endif /* HAVE_FIPS */ #endif /* CTAO_CRYPT_ERROR_H */ diff --git a/cyassl/ctaocrypt/hc128.h b/cyassl/ctaocrypt/hc128.h index 766a79b2e..041298751 100644 --- a/cyassl/ctaocrypt/hc128.h +++ b/cyassl/ctaocrypt/hc128.h @@ -25,36 +25,15 @@ #ifndef CTAO_CRYPT_HC128_H #define CTAO_CRYPT_HC128_H -#include +#include -#ifdef __cplusplus - extern "C" { -#endif - - -enum { - HC128_ENC_TYPE = 6 /* cipher unique type */ -}; - -/* HC-128 stream cipher */ -typedef struct HC128 { - word32 T[1024]; /* P[i] = T[i]; Q[i] = T[1024 + i ]; */ - word32 X[16]; - word32 Y[16]; - word32 counter1024; /* counter1024 = i mod 1024 at the ith step */ - word32 key[8]; - word32 iv[8]; -} HC128; - - -CYASSL_API int Hc128_Process(HC128*, byte*, const byte*, word32); -CYASSL_API int Hc128_SetKey(HC128*, const byte* key, const byte* iv); - - -#ifdef __cplusplus - } /* extern "C" */ +/* for hc128 reverse compatibility */ +#ifdef HAVE_HC128 + #define Hc128_Process wc_Hc128_Process + #define Hc128_SetKey wc_Hc128_SetKey #endif #endif /* CTAO_CRYPT_HC128_H */ #endif /* HAVE_HC128 */ + diff --git a/cyassl/ctaocrypt/logging.h b/cyassl/ctaocrypt/logging.h index a361e8cca..9a3e56d0f 100644 --- a/cyassl/ctaocrypt/logging.h +++ b/cyassl/ctaocrypt/logging.h @@ -25,46 +25,50 @@ #ifndef CYASSL_LOGGING_H #define CYASSL_LOGGING_H - -#ifdef __cplusplus - extern "C" { -#endif - - -enum CYA_Log_Levels { - ERROR_LOG = 0, - INFO_LOG, - ENTER_LOG, - LEAVE_LOG, - OTHER_LOG -}; - -typedef void (*CyaSSL_Logging_cb)(const int logLevel, - const char *const logMessage); - -CYASSL_API int CyaSSL_SetLoggingCb(CyaSSL_Logging_cb log_function); - - -#ifdef DEBUG_CYASSL - - void CYASSL_ENTER(const char* msg); - void CYASSL_LEAVE(const char* msg, int ret); - - void CYASSL_ERROR(int); - void CYASSL_MSG(const char* msg); - -#else /* DEBUG_CYASSL */ - - #define CYASSL_ENTER(m) - #define CYASSL_LEAVE(m, r) - - #define CYASSL_ERROR(e) - #define CYASSL_MSG(m) - -#endif /* DEBUG_CYASSL */ - -#ifdef __cplusplus -} -#endif - +/* for fips compatibility @wc_fips */ +#ifndef HAVE_FIPS + #include +#else + #ifdef __cplusplus + extern "C" { + #endif + + + enum CYA_Log_Levels { + ERROR_LOG = 0, + INFO_LOG, + ENTER_LOG, + LEAVE_LOG, + OTHER_LOG + }; + + typedef void (*CyaSSL_Logging_cb)(const int logLevel, + const char *const logMessage); + + CYASSL_API int CyaSSL_SetLoggingCb(CyaSSL_Logging_cb log_function); + + + #ifdef DEBUG_CYASSL + + void CYASSL_ENTER(const char* msg); + void CYASSL_LEAVE(const char* msg, int ret); + + void CYASSL_ERROR(int); + void CYASSL_MSG(const char* msg); + + #else /* DEBUG_CYASSL */ + + #define CYASSL_ENTER(m) + #define CYASSL_LEAVE(m, r) + + #define CYASSL_ERROR(e) + #define CYASSL_MSG(m) + + #endif /* DEBUG_CYASSL */ + + #ifdef __cplusplus + } + #endif +#endif /* HAVE_FIPS*/ #endif /* CYASSL_MEMORY_H */ + diff --git a/cyassl/ctaocrypt/md2.h b/cyassl/ctaocrypt/md2.h index 2d55cd9ea..5e546186e 100644 --- a/cyassl/ctaocrypt/md2.h +++ b/cyassl/ctaocrypt/md2.h @@ -20,46 +20,53 @@ */ +/* check for old macro */ +#if !defined(CYASSL_MD2) && defined(WOLFSSL_MD2) + #define CYASSL_MD2 +#endif + #ifdef CYASSL_MD2 #ifndef CTAO_CRYPT_MD2_H #define CTAO_CRYPT_MD2_H -#include +#include -#ifdef __cplusplus - extern "C" { -#endif - - -/* in bytes */ -enum { - MD2 = 6, /* hash type unique */ - MD2_BLOCK_SIZE = 16, - MD2_DIGEST_SIZE = 16, - MD2_PAD_SIZE = 16, - MD2_X_SIZE = 48 -}; - - -/* Md2 digest */ -typedef struct Md2 { - word32 count; /* bytes % PAD_SIZE */ - byte X[MD2_X_SIZE]; - byte C[MD2_BLOCK_SIZE]; - byte buffer[MD2_BLOCK_SIZE]; -} Md2; - - -CYASSL_API void InitMd2(Md2*); -CYASSL_API void Md2Update(Md2*, const byte*, word32); -CYASSL_API void Md2Final(Md2*, byte*); -CYASSL_API int Md2Hash(const byte*, word32, byte*); - - -#ifdef __cplusplus - } /* extern "C" */ -#endif +//#include +// +//#ifdef __cplusplus +// extern "C" { +//#endif +// +// +///* in bytes */ +//enum { +// MD2 = 6, /* hash type unique */ +// MD2_BLOCK_SIZE = 16, +// MD2_DIGEST_SIZE = 16, +// MD2_PAD_SIZE = 16, +// MD2_X_SIZE = 48 +//}; +// +// +///* Md2 digest */ +//typedef struct Md2 { +// word32 count; /* bytes % PAD_SIZE */ +// byte X[MD2_X_SIZE]; +// byte C[MD2_BLOCK_SIZE]; +// byte buffer[MD2_BLOCK_SIZE]; +//} Md2; +// +// +//CYASSL_API void InitMd2(Md2*); +//CYASSL_API void Md2Update(Md2*, const byte*, word32); +//CYASSL_API void Md2Final(Md2*, byte*); +//CYASSL_API int Md2Hash(const byte*, word32, byte*); +// +// +//#ifdef __cplusplus +// } /* extern "C" */ +//#endif #endif /* CTAO_CRYPT_MD2_H */ #endif /* CYASSL_MD2 */ diff --git a/cyassl/ctaocrypt/md4.h b/cyassl/ctaocrypt/md4.h index eb5ebb5a4..df6be24bd 100644 --- a/cyassl/ctaocrypt/md4.h +++ b/cyassl/ctaocrypt/md4.h @@ -25,39 +25,41 @@ #ifndef CTAO_CRYPT_MD4_H #define CTAO_CRYPT_MD4_H -#include +#include -#ifdef __cplusplus - extern "C" { -#endif - - -/* in bytes */ -enum { - MD4_BLOCK_SIZE = 64, - MD4_DIGEST_SIZE = 16, - MD4_PAD_SIZE = 56 -}; - - -/* MD4 digest */ -typedef struct Md4 { - word32 buffLen; /* in bytes */ - word32 loLen; /* length in bytes */ - word32 hiLen; /* length in bytes */ - word32 digest[MD4_DIGEST_SIZE / sizeof(word32)]; - word32 buffer[MD4_BLOCK_SIZE / sizeof(word32)]; -} Md4; - - -CYASSL_API void InitMd4(Md4*); -CYASSL_API void Md4Update(Md4*, const byte*, word32); -CYASSL_API void Md4Final(Md4*, byte*); - - -#ifdef __cplusplus - } /* extern "C" */ -#endif +//#include +// +//#ifdef __cplusplus +// extern "C" { +//#endif +// +// +///* in bytes */ +//enum { +// MD4_BLOCK_SIZE = 64, +// MD4_DIGEST_SIZE = 16, +// MD4_PAD_SIZE = 56 +//}; +// +// +///* MD4 digest */ +//typedef struct Md4 { +// word32 buffLen; /* in bytes */ +// word32 loLen; /* length in bytes */ +// word32 hiLen; /* length in bytes */ +// word32 digest[MD4_DIGEST_SIZE / sizeof(word32)]; +// word32 buffer[MD4_BLOCK_SIZE / sizeof(word32)]; +//} Md4; +// +// +//CYASSL_API void InitMd4(Md4*); +//CYASSL_API void Md4Update(Md4*, const byte*, word32); +//CYASSL_API void Md4Final(Md4*, byte*); +// +// +//#ifdef __cplusplus +// } /* extern "C" */ +//#endif #endif /* CTAO_CRYPT_MD4_H */ diff --git a/cyassl/ctaocrypt/md5.h b/cyassl/ctaocrypt/md5.h index f62ede96c..2879e4cee 100644 --- a/cyassl/ctaocrypt/md5.h +++ b/cyassl/ctaocrypt/md5.h @@ -24,51 +24,54 @@ #ifndef CTAO_CRYPT_MD5_H #define CTAO_CRYPT_MD5_H -#include - -#ifdef __cplusplus - extern "C" { -#endif +#include -/* in bytes */ -enum { -#ifdef STM32F2_HASH - MD5_REG_SIZE = 4, /* STM32 register size, bytes */ -#endif - MD5 = 0, /* hash type unique */ - MD5_BLOCK_SIZE = 64, - MD5_DIGEST_SIZE = 16, - MD5_PAD_SIZE = 56 -}; - -#ifdef CYASSL_PIC32MZ_HASH -#include "port/pic32/pic32mz-crypt.h" -#endif - -/* MD5 digest */ -typedef struct Md5 { - word32 buffLen; /* in bytes */ - word32 loLen; /* length in bytes */ - word32 hiLen; /* length in bytes */ - word32 buffer[MD5_BLOCK_SIZE / sizeof(word32)]; - #ifndef CYASSL_PIC32MZ_HASH - word32 digest[MD5_DIGEST_SIZE / sizeof(word32)]; - #else - word32 digest[PIC32_HASH_SIZE / sizeof(word32)]; - pic32mz_desc desc ; /* Crypt Engine descripter */ - #endif -} Md5; - -CYASSL_API void InitMd5(Md5*); -CYASSL_API void Md5Update(Md5*, const byte*, word32); -CYASSL_API void Md5Final(Md5*, byte*); -CYASSL_API int Md5Hash(const byte*, word32, byte*); - - -#ifdef __cplusplus - } /* extern "C" */ -#endif +//#include +// +//#ifdef __cplusplus +// extern "C" { +//#endif +// +// +///* in bytes */ +//enum { +//#ifdef STM32F2_HASH +// MD5_REG_SIZE = 4, /* STM32 register size, bytes */ +//#endif +// MD5 = 0, /* hash type unique */ +// MD5_BLOCK_SIZE = 64, +// MD5_DIGEST_SIZE = 16, +// MD5_PAD_SIZE = 56 +//}; +// +//#ifdef CYASSL_PIC32MZ_HASH +//#include "port/pic32/pic32mz-crypt.h" +//#endif +// +///* MD5 digest */ +//typedef struct Md5 { +// word32 buffLen; /* in bytes */ +// word32 loLen; /* length in bytes */ +// word32 hiLen; /* length in bytes */ +// word32 buffer[MD5_BLOCK_SIZE / sizeof(word32)]; +// #ifndef CYASSL_PIC32MZ_HASH +// word32 digest[MD5_DIGEST_SIZE / sizeof(word32)]; +// #else +// word32 digest[PIC32_HASH_SIZE / sizeof(word32)]; +// pic32mz_desc desc ; /* Crypt Engine descripter */ +// #endif +//} Md5; +// +//CYASSL_API void InitMd5(Md5*); +//CYASSL_API void Md5Update(Md5*, const byte*, word32); +//CYASSL_API void Md5Final(Md5*, byte*); +//CYASSL_API int Md5Hash(const byte*, word32, byte*); +// +// +//#ifdef __cplusplus +// } /* extern "C" */ +//#endif #endif /* CTAO_CRYPT_MD5_H */ #endif /* NO_MD5 */ diff --git a/cyassl/ctaocrypt/memory.h b/cyassl/ctaocrypt/memory.h index 2ecdfd42b..c824826e3 100644 --- a/cyassl/ctaocrypt/memory.h +++ b/cyassl/ctaocrypt/memory.h @@ -26,7 +26,11 @@ #define CYASSL_MEMORY_H #include + +/* for fips compatibility @wc_fips */ +#ifndef HAVE_FIPS #include +#else #ifdef __cplusplus extern "C" { @@ -53,4 +57,6 @@ CYASSL_API void* CyaSSL_Realloc(void *ptr, size_t size); } #endif +#endif /* HAVE_FIPS */ #endif /* CYASSL_MEMORY_H */ + diff --git a/cyassl/ctaocrypt/pkcs7.h b/cyassl/ctaocrypt/pkcs7.h index 63ae2a54c..32d080db3 100644 --- a/cyassl/ctaocrypt/pkcs7.h +++ b/cyassl/ctaocrypt/pkcs7.h @@ -25,97 +25,19 @@ #ifndef CTAO_CRYPT_PKCS7_H #define CTAO_CRYPT_PKCS7_H -#include -#include -#include -#include -#include +#include -#ifdef __cplusplus - extern "C" { -#endif - -/* PKCS#7 content types, ref RFC 2315 (Section 14) */ -enum PKCS7_TYPES { - PKCS7_MSG = 650, /* 1.2.840.113549.1.7 */ - DATA = 651, /* 1.2.840.113549.1.7.1 */ - SIGNED_DATA = 652, /* 1.2.840.113549.1.7.2 */ - ENVELOPED_DATA = 653, /* 1.2.840.113549.1.7.3 */ - SIGNED_AND_ENVELOPED_DATA = 654, /* 1.2.840.113549.1.7.4 */ - DIGESTED_DATA = 655, /* 1.2.840.113549.1.7.5 */ - ENCRYPTED_DATA = 656 /* 1.2.840.113549.1.7.6 */ -}; - -enum Pkcs7_Misc { - PKCS7_NONCE_SZ = 16, - MAX_ENCRYPTED_KEY_SZ = 512, /* max enc. key size, RSA <= 4096 */ - MAX_CONTENT_KEY_LEN = DES3_KEYLEN, /* highest current cipher is 3DES */ - MAX_RECIP_SZ = MAX_VERSION_SZ + - MAX_SEQ_SZ + ASN_NAME_MAX + MAX_SN_SZ + - MAX_SEQ_SZ + MAX_ALGO_SZ + 1 + MAX_ENCRYPTED_KEY_SZ -}; - - -typedef struct PKCS7Attrib { - byte* oid; - word32 oidSz; - byte* value; - word32 valueSz; -} PKCS7Attrib; - - -typedef struct PKCS7 { - byte* content; /* inner content, not owner */ - word32 contentSz; /* content size */ - int contentOID; /* PKCS#7 content type OID sum */ - - RNG* rng; - - int hashOID; - int encryptOID; /* key encryption algorithm OID */ - - byte* singleCert; /* recipient cert, DER, not owner */ - word32 singleCertSz; /* size of recipient cert buffer, bytes */ - byte issuerHash[SHA_SIZE]; /* hash of all alt Names */ - byte* issuer; /* issuer name of singleCert */ - word32 issuerSz; /* length of issuer name */ - byte issuerSn[MAX_SN_SZ]; /* singleCert's serial number */ - word32 issuerSnSz; /* length of serial number */ - byte publicKey[512]; - word32 publicKeySz; - byte* privateKey; /* private key, DER, not owner */ - word32 privateKeySz; /* size of private key buffer, bytes */ - - PKCS7Attrib* signedAttribs; - word32 signedAttribsSz; -} PKCS7; - - -CYASSL_LOCAL int SetContentType(int pkcs7TypeOID, byte* output); -CYASSL_LOCAL int GetContentType(const byte* input, word32* inOutIdx, - word32* oid, word32 maxIdx); -CYASSL_LOCAL int CreateRecipientInfo(const byte* cert, word32 certSz, - int keyEncAlgo, int blockKeySz, - RNG* rng, byte* contentKeyPlain, - byte* contentKeyEnc, - int* keyEncSz, byte* out, word32 outSz); - -CYASSL_API int PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz); -CYASSL_API void PKCS7_Free(PKCS7* pkcs7); -CYASSL_API int PKCS7_EncodeData(PKCS7* pkcs7, byte* output, word32 outputSz); -CYASSL_API int PKCS7_EncodeSignedData(PKCS7* pkcs7, - byte* output, word32 outputSz); -CYASSL_API int PKCS7_VerifySignedData(PKCS7* pkcs7, - byte* pkiMsg, word32 pkiMsgSz); -CYASSL_API int PKCS7_EncodeEnvelopedData(PKCS7* pkcs7, - byte* output, word32 outputSz); -CYASSL_API int PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg, - word32 pkiMsgSz, byte* output, - word32 outputSz); - -#ifdef __cplusplus - } /* extern "C" */ -#endif +/* for pkcs7 reverse compatibility */ +#define SetContentType wc_SetContentType +#define GetContentType wc_GetContentType +#define CreateRecipientInfo wc_CreateRecipientInfo +#define PKCS7_InitWithCert wc_PKCS7_InitWithCert +#define PKCS7_Free wc_PKCS7_Free +#define PKCS7_EncodeData wc_PKCS7_EncodeData +#define PKCS7_EncodeSignedData wc_PKCS7_EncodeSignedData +#define PKCS7_VerifySignedData wc_PKCS7_VerifySignedData +#define PKCS7_EncodeEnvelopedData wc_PKCS7_EncodeEnvelopedData +#define PKCS7_DecodeEnvelopedData wc_PKCS7_DecodeEnvelopedData #endif /* CTAO_CRYPT_PKCS7_H */ diff --git a/cyassl/ctaocrypt/poly1305.h b/cyassl/ctaocrypt/poly1305.h index 600156279..361432fda 100644 --- a/cyassl/ctaocrypt/poly1305.h +++ b/cyassl/ctaocrypt/poly1305.h @@ -25,57 +25,7 @@ #ifndef CTAO_CRYPT_POLY1305_H #define CTAO_CRYPT_POLY1305_H -#include - -#ifdef __cplusplus - extern "C" { -#endif - -/* auto detect between 32bit / 64bit */ -#define HAS_SIZEOF_INT128_64BIT (defined(__SIZEOF_INT128__) && defined(__LP64__)) -#define HAS_MSVC_64BIT (defined(_MSC_VER) && defined(_M_X64)) -#define HAS_GCC_4_4_64BIT (defined(__GNUC__) && defined(__LP64__) && \ - ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4)))) - -#if (HAS_SIZEOF_INT128_64BIT || HAS_MSVC_64BIT || HAS_GCC_4_4_64BIT) -#define POLY130564 -#else -#define POLY130532 -#endif - -enum { - POLY1305 = 7, - POLY1305_BLOCK_SIZE = 16, - POLY1305_DIGEST_SIZE = 16, - POLY1305_PAD_SIZE = 56 -}; - -/* Poly1305 state */ -typedef struct Poly1305 { -#if defined(POLY130564) - word64 r[3]; - word64 h[3]; - word64 pad[2]; -#else - word32 r[5]; - word32 h[5]; - word32 pad[4]; -#endif - size_t leftover; - unsigned char buffer[POLY1305_BLOCK_SIZE]; - unsigned char final; -} Poly1305; - - -/* does init */ - -CYASSL_API int Poly1305SetKey(Poly1305* poly1305, const byte* key, word32 kySz); -CYASSL_API int Poly1305Update(Poly1305* poly1305, const byte*, word32); -CYASSL_API int Poly1305Final(Poly1305* poly1305, byte* tag); - -#ifdef __cplusplus - } /* extern "C" */ -#endif +#include #endif /* CTAO_CRYPT_POLY1305_H */ diff --git a/cyassl/ctaocrypt/rabbit.h b/cyassl/ctaocrypt/rabbit.h index 08da26c83..40eea69c9 100644 --- a/cyassl/ctaocrypt/rabbit.h +++ b/cyassl/ctaocrypt/rabbit.h @@ -25,41 +25,15 @@ #ifndef CTAO_CRYPT_RABBIT_H #define CTAO_CRYPT_RABBIT_H -#include +#include -#ifdef __cplusplus - extern "C" { -#endif - - -enum { - RABBIT_ENC_TYPE = 5 /* cipher unique type */ -}; - - -/* Rabbit Context */ -typedef struct RabbitCtx { - word32 x[8]; - word32 c[8]; - word32 carry; -} RabbitCtx; - - -/* Rabbit stream cipher */ -typedef struct Rabbit { - RabbitCtx masterCtx; - RabbitCtx workCtx; -} Rabbit; - - -CYASSL_API int RabbitProcess(Rabbit*, byte*, const byte*, word32); -CYASSL_API int RabbitSetKey(Rabbit*, const byte* key, const byte* iv); - - -#ifdef __cplusplus - } /* extern "C" */ +/* for rabbit reverse compatibility */ +#ifndef NO_RABBIT + #define RabbitProcess wc_RabbitProcess + #define RabbitSetKey wc_RabbitSetKey #endif #endif /* CTAO_CRYPT_RABBIT_H */ #endif /* NO_RABBIT */ + diff --git a/cyassl/ctaocrypt/ripemd.h b/cyassl/ctaocrypt/ripemd.h index 9df135c17..8472abcdf 100644 --- a/cyassl/ctaocrypt/ripemd.h +++ b/cyassl/ctaocrypt/ripemd.h @@ -27,5 +27,14 @@ #include +/* for ripemd reverse compatibility */ +#ifdef WOLFSSL_RIPEMD + #define CYASSL_RIPEMD /* @TODO */ + #define InitRipeMd wc_InitRipeMd + #define RipeMdUpdate wc_RipeMdUpdate + #define RipeMdFinal wc_RipeMdFinal +#endif + #endif /* CTAO_CRYPT_RIPEMD_H */ #endif /* CYASSL_RIPEMD */ + diff --git a/cyassl/ctaocrypt/settings.h b/cyassl/ctaocrypt/settings.h index e5d76d84b..c02d21c8e 100644 --- a/cyassl/ctaocrypt/settings.h +++ b/cyassl/ctaocrypt/settings.h @@ -100,7 +100,6 @@ /* #define CYASSL_PICOTCP_DEMO */ #include -#include #ifdef IPHONE #define SIZEOF_LONG_LONG 8 diff --git a/cyassl/ssl.h b/cyassl/ssl.h index f97553309..648f6bc8b 100644 --- a/cyassl/ssl.h +++ b/cyassl/ssl.h @@ -88,7 +88,6 @@ #define WOLFSSL_MAX_ERROR_SZ CYASSL_MAX_ERROR_SZ /* src/ssl.c */ -#ifdef CYASSL_SSL_H #define CYASSL_CRL WOLFSSL_CRL #define CYASSL_SSLV3 WOLFSSL_SSLV3 #define CYASSL_TLSV1 WOLFSSL_TLSV1 @@ -121,7 +120,6 @@ #define CyaSSL_CertManagerDisableOCSP wolfSSL_CertManagerDisableOCSP #define CyaSSL_get_current_cipher_suite wolfSSL_get_current_cipher_suite #define CyaSSL_CTX_load_verify_locations wolfSSL_CTX_load_verify_locations -#endif /* CYASSL_SSL_H */ /* io.c */ #define CYASSL_CBIO_ERR_ISR WOLFSSL_CBIO_ERR_ISR @@ -156,9 +154,12 @@ #define CyaSSL_ERR_reason_error_string wolfSSL_ERR_reason_error_string /* fips defines */ -#define WOLFSSL_LEAVE CYASSL_LEAVE -#define WOLFSSL_ERROR CYASSL_ERROR -#define WOLFSSL_GENERAL_ALIGNMENT CYASSL_GENERAL_ALIGNMENT +//defined in logging +//#define WOLFSSL_LEAVE CYASSL_LEAVE +//#define WOLFSSL_ERROR CYASSL_ERROR + +//defined in settings.h +//#define WOLFSSL_GENERAL_ALIGNMENT CYASSL_GENERAL_ALIGNMENT #define wolfcrypt_test ctaocrypt_test @@ -329,10 +330,11 @@ #define CyaSSL_dtls_get_current_timeout wolfSSL_dtls_get_current_timeout /* Memory Abstraction Layer */ -#define CyaSSL_Free wolfSSL_Free -#define CyaSSL_Malloc wolfSSL_Malloc -#define CyaSSL_Realloc wolfSSL_Realloc -#define CyaSSL_SetAllocators wolfSSL_SetAllocators +//#define CyaSSL_Free wolfSSL_Free +//#define CyaSSL_Malloc wolfSSL_Malloc +//#define CyaSSL_Realloc wolfSSL_Realloc +//#define CyaSSL_SetAllocators wolfSSL_SetAllocators +/* moved to wolfssl/wolfcrypt/memory.h */ /* Certificate Manager */ #define CyaSSL_CertManagerNew wolfSSL_CertManagerNew @@ -390,125 +392,15 @@ #undef WOLFSSL_API #define WOLFSSL_API CYASSL_API -#define WOLFSSL_ENTER(x) CYASSL_ENTER(x) /* @TODO*/ #define WOLFSSL_BIT_SIZE CYASSL_BIT_SIZE /* @TODO*/ /* wrapper around macros until they are changed in cyassl code * needs investigation in regards to macros in fips */ #define WOLFSSL_MAX_16BIT CYASSL_MAX_16BIT -#define WOLFSSL_MSG(x) CYASSL_MSG(x) #define NO_WOLFSSL_ALLOC_ALIGN NO_CYASSL_ALLOC_ALIGN /* @TODO*/ -/* for arc4 reverse compatibility */ -#ifndef NO_RC4 -#include - #define CYASSL_ARC4_CAVIUM_MAGIC WOLFSSL_ARC4_CAVIUM_MAGIC - #define Arc4Process wc_Arc4Process - #define Arc4SetKey wc_Arc4SetKey - #define Arc4InitCavium wc_Arc4InitCavium - #define Arc4FreeCavium wc_Arc4FreeCavium -#endif - -/* for chacha reverse compatibility */ -#ifdef HAVE_CHACHA - #define ChachaProcess wc_ChachaProcess - #define ChachaSetKey wc_ChachaSetKey - #define Chacha_SetIV wc_Chacha_SetIV -#endif - - -/* for DH reverse compatibility */ -#ifndef NO_DH - #define InitDhKey wc_InitDhKey - #define FreeDhKey wc_FreeDhKey - #define DhGenerateKeyPair wc_DhGenerateKeyPair -#endif - - -/* for DSA reverse compatibility */ -#ifndef NO_DSA - #define InitDsaKey wc_InitDsaKey - #define FreeDsaKey wc_FreeDsaKey - #define DsaSign wc_DsaSign - #define DsaVerify wc_DsaVerify - #define DsaPublicKeyDecode wc_DsaPublicKeyDecode - #define DsaPrivateKeyDecode wc_DsaPrivateKeyDecode -#endif - -/* for hc128 reverse compatibility */ -#ifdef HAVE_HC128 - #define Hc128_Process wc_Hc128_Process - #define Hc128_SetKey wc_Hc128_SetKey -#endif - - -/* for md2 reverse compatibility */ -#define CYASSL_MD2 WOLFSSL_MD2 /* @TODO */ -//#ifdef WOLFSSL_MD2 - #define InitMd2 wc_InitMd2 - #define Md2Update wc_Md2Update - #define Md2Final wc_Md2Final - #define Md2Hash wc_Md2Hash -//#endif - - -/* for md4 reverse compatibility */ -#ifndef NO_MD4 - #define InitMd4 wc_InitMd4 - #define Md4Update wc_Md4Update - #define Md4Final wc_Md4Final -#endif - - -/* for md5 reverse compatibility */ -#ifndef NO_MD5 - #define InitMd5 wc_InitMd5 - #define Md5Update wc_Md5Update - #define Md5Final wc_Md5Final - #define Md5Hash wc_Md5Hash -#endif - - -/* for poly1305 reverse compatibility */ -#ifdef HAVE_POLY1305 - #define Poly1305SetKey wc_Poly1305SetKey - #define Poly1305Update wc_Poly1305Update - #define Poly1305Final wc_Poly1305Final -#endif - - -/* for rabbit reverse compatibility */ -#ifndef NO_RABBIT - #define RabbitProcess wc_RabbitProcess - #define RabbitSetKey wc_RabbitSetKey -#endif - - -/* for ripemd reverse compatibility */ -#ifdef WOLFSSL_RIPEMD - #define CYASSL_RIPEMD /* @TODO */ - #define InitRipeMd wc_InitRipeMd - #define RipeMdUpdate wc_RipeMdUpdate - #define RipeMdFinal wc_RipeMdFinal -#endif - - -/* for pkcs7 reverse compatibility */ -#ifdef HAVE_PKCS7 - #define SetContentType wc_SetContentType - #define GetContentType wc_GetContentType - #define CreateRecipientInfo wc_CreateRecipientInfo - #define PKCS7_InitWithCert wc_PKCS7_InitWithCert - #define PKCS7_Free wc_PKCS7_Free - #define PKCS7_EncodeData wc_PKCS7_EncodeData - #define PKCS7_EncodeSignedData wc_PKCS7_EncodeSignedData - #define PKCS7_VerifySignedData wc_PKCS7_VerifySignedData - #define PKCS7_EncodeEnvelopedData wc_PKCS7_EncodeEnvelopedData - #define PKCS7_DecodeEnvelopedData wc_PKCS7_DecodeEnvelopedData -#endif - /* for pwdbased reverse compatibility */ #ifndef NO_PWDBASED @@ -518,65 +410,6 @@ #endif -/* for blake2 reverse compatibility */ -#ifdef HAVE_BLAKE2 - #define InitBlake2b wc_InitBlake2b - #define Blake2bUpdate wc_Blake2bUpdate - #define Blake2bFinal wc_Blake2bFinal -#endif - - -/* for blake2 reverse compatibility */ -#ifdef HAVE_CAMELLIA - #define CamelliaSetKey wc_CamelliaSetKey - #define CamelliaSetIV wc_CamelliaSetIV - #define CamelliaEncryptDirect wc_CamelliaEncryptDirect - #define CamelliaDecryptDirect wc_CamelliaDecryptDirect - #define CamelliaCbcEncrypt wc_CamelliaCbcEncrypt - #define CamelliaCbcDecrypt wc_CamelliaCbcDecrypt -#endif - - -/* for chacha reverse compatibility */ -#ifdef HAVE_CHACHA - #define Chacha_Process wc_Chacha_Process - #define Chacha_SetKey wc_Chacha_SetKey - #define Chacha_SetIV wc_Chacha_SetIV -#endif - - -/* for ecc reverse compatibility */ -#ifdef HAVE_ECC -#define ecc_make_key wc_ecc_make_key -#define ecc_shared_secret wc_ecc_shared_secret -#define ecc_sign_hash wc_ecc_sign_hash -#define ecc_verify_hash wc_ecc_verify_hash -#define ecc_init wc_ecc_init -#define ecc_free wc_ecc_free -#define ecc_fp_free wc_ecc_fp_free -#define ecc_export_x963 wc_ecc_export_x963 -#define ecc_export_x963_ex wc_ecc_export_x963_ex -#define ecc_import_x963 wc_ecc_import_x963 -#define ecc_import_private_key wc_ecc_import_private_key -#define ecc_rs_to_sig wc_ecc_rs_to_sig -#define ecc_import_raw wc_ecc_import_raw -#define ecc_export_private_only wc_ecc_export_private_only -#define ecc_size wc_ecc_size -#define ecc_sig_size wc_ecc_sig_size - -#ifdef HAVE_ECC_ENCRYPT -/* ecc encrypt */ -//ecEncCtx* wc_ecc_ctx_new(int flags, RNG* rng); -//void wc_ecc_ctx_free(ecEncCtx*); -//int wc_ecc_ctx_reset(ecEncCtx*, RNG*); /* reset for use again w/o alloc/free */ -//const byte* wc_ecc_ctx_get_own_salt(ecEncCtx*); -//int wc_ecc_ctx_set_peer_salt(ecEncCtx*, const byte* salt); -//int wc_ecc_ctx_set_info(ecEncCtx*, const byte* info, int sz); -//int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg, -//int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg, -#endif /* HAVE_ECC_ENCRYPT */ -#endif - /* examples/client/client.h */ #define CYASSL_THREAD WOLFSSL_THREAD @@ -584,6 +417,11 @@ #define LIBCYASSL_VERSION_STRING LIBWOLFSSL_VERSION_STRING + + + + + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/examples/server/server.c b/examples/server/server.c index d03606e26..073fa4223 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -1,3 +1,4 @@ +#include /* server.c * * Copyright (C) 2006-2014 wolfSSL Inc. diff --git a/mcapi/include.am b/mcapi/include.am index 993ba29c0..77170ac26 100644 --- a/mcapi/include.am +++ b/mcapi/include.am @@ -7,8 +7,8 @@ check_PROGRAMS += mcapi/test noinst_PROGRAMS += mcapi/test mcapi_test_SOURCES = mcapi/crypto.c \ mcapi/mcapi_test.c -mcapi_test_LDADD = src/libcyassl.la -mcapi_test_DEPENDENCIES = src/libcyassl.la +mcapi_test_LDADD = src/libwolfssl.la +mcapi_test_DEPENDENCIES = src/libwolfssl.la endif noinst_HEADERS += mcapi/crypto.h diff --git a/rpm/spec.in b/rpm/spec.in index 9435c6cfa..0a16a85a8 100644 --- a/rpm/spec.in +++ b/rpm/spec.in @@ -66,10 +66,10 @@ mkdir -p $RPM_BUILD_ROOT/ %{_docdir}/cyassl/example/echoclient.c %{_docdir}/cyassl/example/client.c %{_docdir}/cyassl/README.txt -%{_libdir}/libcyassl.la -%{_libdir}/libcyassl.so -%{_libdir}/libcyassl.so.5 -%{_libdir}/libcyassl.so.5.0.5 +%{_libdir}/libwolfssl.la +%{_libdir}/libwolfssl.so +%{_libdir}/libwolfssl.so.5 +%{_libdir}/libwolfssl.so.5.0.5 %files devel %defattr(-,root,root,-) diff --git a/src/include.am b/src/include.am index a9e3b562a..50eb9260d 100644 --- a/src/include.am +++ b/src/include.am @@ -71,13 +71,15 @@ src_libwolfssl_la_SOURCES += \ endif if BUILD_MEMORY -src_libwolfssl_la_SOURCES += ctaocrypt/src/memory.c \ - wolfcrypt/src/memory.c +if BUILD_FIPS +src_libwolfssl_la_SOURCES += ctaocrypt/src/memory.c +else +src_libwolfssl_la_SOURCES += wolfcrypt/src/memory.c +endif endif if BUILD_DH -src_libwolfssl_la_SOURCES += ctaocrypt/src/dh.c \ - wolfcrypt/src/dh.c +src_libwolfssl_la_SOURCES += wolfcrypt/src/dh.c endif if BUILD_ASN @@ -97,8 +99,7 @@ endif endif if BUILD_POLY1305 -src_libwolfssl_la_SOURCES += ctaocrypt/src/poly1305.c \ - wolfcrypt/src/poly1305.c +src_libwolfssl_la_SOURCES += wolfcrypt/src/poly1305.c endif if BUILD_RC4 @@ -106,23 +107,28 @@ src_libwolfssl_la_SOURCES += wolfcrypt/src/arc4.c endif if BUILD_MD4 -src_libwolfssl_la_SOURCES += ctaocrypt/src/md4.c \ - wolfcrypt/src/md4.c +if BUILD_FIPS +src_libwolfssl_la_SOURCES += ctaocrypt/src/md4.c +else +src_libwolfssl_la_SOURCES += wolfcrypt/src/md4.c +endif endif if BUILD_MD5 -src_libwolfssl_la_SOURCES += ctaocrypt/src/md5.c \ - wolfcrypt/src/md5.c +src_libwolfssl_la_SOURCES += ctaocrypt/src/md5.c +src_libwolfssl_la_SOURCES += wolfcrypt/src/md5.c endif if BUILD_PWDBASED -src_libwolfssl_la_SOURCES += ctaocrypt/src/pwdbased.c \ - wolfcrypt/src/pwdbased.c +if BUILD_FIPS +src_libwolfssl_la_SOURCES += ctaocrypt/src/pwdbased.c +else +src_libwolfssl_la_SOURCES += wolfcrypt/src/pwdbased.c +endif endif if BUILD_DSA -src_libwolfssl_la_SOURCES += ctaocrypt/src/dsa.c \ - wolfcrypt/src/dsa.c +src_libwolfssl_la_SOURCES += wolfcrypt/src/dsa.c endif if BUILD_AESNI @@ -130,18 +136,19 @@ src_libwolfssl_la_SOURCES += ctaocrypt/src/aes_asm.s endif if BUILD_CAMELLIA -src_libwolfssl_la_SOURCES += ctaocrypt/src/camellia.c \ - wolfcrypt/src/camellia.c +src_libwolfssl_la_SOURCES += wolfcrypt/src/camellia.c endif if BUILD_MD2 -src_libwolfssl_la_SOURCES += ctaocrypt/src/md2.c \ - wolfcrypt/src/md2.c +if BUILD_FIPS +src_libwolfssl_la_SOURCES += ctaocrypt/src/md2.c +else +src_libwolfssl_la_SOURCES += wolfcrypt/src/md2.c +endif endif if BUILD_RIPEMD -src_libwolfssl_la_SOURCES += ctaocrypt/src/ripemd.c \ - wolfcrypt/src/ripemd.c +src_libwolfssl_la_SOURCES += wolfcrypt/src/ripemd.c endif if BUILD_BLAKE2 @@ -149,33 +156,40 @@ src_libwolfssl_la_SOURCES += wolfcrypt/src/blake2b.c endif if BUILD_HC128 -src_libwolfssl_la_SOURCES += ctaocrypt/src/hc128.c \ - ctaocrypt/src/hc128.c +if BUILD_FIPS +src_libwolfssl_la_SOURCES += ctaocrypt/src/hc128.c +else +src_libwolfssl_la_SOURCES += wolfcrypt/src/hc128.c +endif endif if BUILD_RABBIT -src_libwolfssl_la_SOURCES += ctaocrypt/src/rabbit.c \ - wolfcrypt/src/rabbit.c +src_libwolfssl_la_SOURCES += wolfcrypt/src/rabbit.c endif if BUILD_CHACHA -src_libwolfssl_la_SOURCES += ctaocrypt/src/chacha.c \ - wolfcrypt/src/chacha.c +src_libwolfssl_la_SOURCES += wolfcrypt/src/chacha.c endif if !BUILD_INLINE -src_libwolfssl_la_SOURCES += ctaocrypt/src/misc.c \ - wolfcrypt/src/misc.c +if BUILD_FIPS +src_libwolfssl_la_SOURCES += ctaocrypt/src/misc.c +else +src_libwolfssl_la_SOURCES += wolfcrypt/src/misc.c +endif endif if BUILD_FASTMATH -src_libwolfssl_la_SOURCES += ctaocrypt/src/tfm.c \ - wolfcrypt/src/tfm.c +src_libwolfssl_la_SOURCES += ctaocrypt/src/tfm.c +src_libwolfssl_la_SOURCES += wolfcrypt/src/tfm.c endif if BUILD_SLOWMATH -src_libwolfssl_la_SOURCES += ctaocrypt/src/integer.c \ - wolfcrypt/src/integer.c +if BUILD_FIPS +src_libwolfssl_la_SOURCES += ctaocrypt/src/integer.c +else +src_libwolfssl_la_SOURCES += wolfcrypt/src/integer.c +endif endif if BUILD_ECC @@ -183,13 +197,19 @@ src_libwolfssl_la_SOURCES += wolfcrypt/src/ecc.c endif if BUILD_LIBZ -src_libwolfssl_la_SOURCES += ctaocrypt/src/compress.c \ - wolfcrypt/src/compress.c +if BUILD_FIPS +src_libwolfssl_la_SOURCES += ctaocrypt/src/compress.c +else +src_libwolfssl_la_SOURCES += wolfcrypt/src/compress.c +endif endif if BUILD_PKCS7 -src_libwolfssl_la_SOURCES += ctaocrypt/src/pkcs7.c \ - wolfcrypt/src/pkcs7.c +if BUILD_FIPS +src_libwolfssl_la_SOURCES += ctaocrypt/src/pkcs7.c +else +src_libwolfssl_la_SOURCES += wolfcrypt/src/pkcs7.c +endif endif diff --git a/src/internal.c b/src/internal.c index da378880c..cdf8b9fc4 100644 --- a/src/internal.c +++ b/src/internal.c @@ -20,7 +20,7 @@ */ /* Name change compatibility layer */ -#include +//#include #ifdef HAVE_CONFIG_H @@ -587,8 +587,8 @@ void FreeCiphers(WOLFSSL* ssl) #ifdef BUILD_AES #ifdef HAVE_CAVIUM if (ssl->devId != NO_CAVIUM_DEVICE) { - AesFreeCavium(ssl->encrypt.aes); - AesFreeCavium(ssl->decrypt.aes); + wc_AesFreeCavium(ssl->encrypt.aes); + wc_AesFreeCavium(ssl->decrypt.aes); } #endif XFREE(ssl->encrypt.aes, ssl->heap, DYNAMIC_TYPE_CIPHER); @@ -5662,7 +5662,7 @@ static INLINE int Encrypt(WOLFSSL* ssl, byte* out, const byte* input, word16 sz) #ifdef BUILD_AES case wolfssl_aes: - return AesCbcEncrypt(ssl->encrypt.aes, out, input, sz); + return wc_AesCbcEncrypt(ssl->encrypt.aes, out, input, sz); #endif #ifdef BUILD_AESGCM @@ -5697,7 +5697,7 @@ static INLINE int Encrypt(WOLFSSL* ssl, byte* out, const byte* input, word16 sz) ssl->keys.aead_enc_imp_IV, AEAD_IMP_IV_SZ); XMEMCPY(nonce + AEAD_IMP_IV_SZ, ssl->keys.aead_exp_IV, AEAD_EXP_IV_SZ); - gcmRet = AesGcmEncrypt(ssl->encrypt.aes, + gcmRet = wc_AesGcmEncrypt(ssl->encrypt.aes, out + AEAD_EXP_IV_SZ, input + AEAD_EXP_IV_SZ, sz - AEAD_EXP_IV_SZ - ssl->specs.aead_mac_size, nonce, AEAD_NONCE_SZ, @@ -5743,7 +5743,7 @@ static INLINE int Encrypt(WOLFSSL* ssl, byte* out, const byte* input, word16 sz) ssl->keys.aead_enc_imp_IV, AEAD_IMP_IV_SZ); XMEMCPY(nonce + AEAD_IMP_IV_SZ, ssl->keys.aead_exp_IV, AEAD_EXP_IV_SZ); - AesCcmEncrypt(ssl->encrypt.aes, + wc_AesCcmEncrypt(ssl->encrypt.aes, out + AEAD_EXP_IV_SZ, input + AEAD_EXP_IV_SZ, sz - AEAD_EXP_IV_SZ - ssl->specs.aead_mac_size, nonce, AEAD_NONCE_SZ, @@ -5821,7 +5821,7 @@ static INLINE int Decrypt(WOLFSSL* ssl, byte* plain, const byte* input, #ifdef BUILD_AES case wolfssl_aes: - return AesCbcDecrypt(ssl->decrypt.aes, plain, input, sz); + return wc_AesCbcDecrypt(ssl->decrypt.aes, plain, input, sz); #endif #ifdef BUILD_AESGCM @@ -5848,7 +5848,7 @@ static INLINE int Decrypt(WOLFSSL* ssl, byte* plain, const byte* input, additional + AEAD_LEN_OFFSET); XMEMCPY(nonce, ssl->keys.aead_dec_imp_IV, AEAD_IMP_IV_SZ); XMEMCPY(nonce + AEAD_IMP_IV_SZ, input, AEAD_EXP_IV_SZ); - if (AesGcmDecrypt(ssl->decrypt.aes, + if (wc_AesGcmDecrypt(ssl->decrypt.aes, plain + AEAD_EXP_IV_SZ, input + AEAD_EXP_IV_SZ, sz - AEAD_EXP_IV_SZ - ssl->specs.aead_mac_size, @@ -5889,7 +5889,7 @@ static INLINE int Decrypt(WOLFSSL* ssl, byte* plain, const byte* input, additional + AEAD_LEN_OFFSET); XMEMCPY(nonce, ssl->keys.aead_dec_imp_IV, AEAD_IMP_IV_SZ); XMEMCPY(nonce + AEAD_IMP_IV_SZ, input, AEAD_EXP_IV_SZ); - if (AesCcmDecrypt(ssl->decrypt.aes, + if (wc_AesCcmDecrypt(ssl->decrypt.aes, plain + AEAD_EXP_IV_SZ, input + AEAD_EXP_IV_SZ, sz - AEAD_EXP_IV_SZ - ssl->specs.aead_mac_size, diff --git a/sslSniffer/sslSnifferTest/include.am b/sslSniffer/sslSnifferTest/include.am index 0d3c5a0ca..222777c7f 100644 --- a/sslSniffer/sslSnifferTest/include.am +++ b/sslSniffer/sslSnifferTest/include.am @@ -5,8 +5,8 @@ if BUILD_SNIFFTEST noinst_PROGRAMS += sslSniffer/sslSnifferTest/snifftest sslSniffer_sslSnifferTest_snifftest_SOURCES = sslSniffer/sslSnifferTest/snifftest.c -sslSniffer_sslSnifferTest_snifftest_LDADD = src/libcyassl.la -lpcap -sslSniffer_sslSnifferTest_snifftest_DEPENDENCIES = src/libcyassl.la +sslSniffer_sslSnifferTest_snifftest_LDADD = src/libwolfssl.la -lpcap +sslSniffer_sslSnifferTest_snifftest_DEPENDENCIES = src/libwolfssl.la endif EXTRA_DIST += sslSniffer/sslSniffer.vcproj EXTRA_DIST += sslSniffer/sslSniffer.vcxproj diff --git a/tests/include.am b/tests/include.am index 8fcc542cf..006458523 100644 --- a/tests/include.am +++ b/tests/include.am @@ -14,8 +14,8 @@ tests_unit_test_SOURCES = \ examples/client/client.c \ examples/server/server.c tests_unit_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS) -tests_unit_test_LDADD = src/libcyassl.la -tests_unit_test_DEPENDENCIES = src/libcyassl.la +tests_unit_test_LDADD = src/libwolfssl.la +tests_unit_test_DEPENDENCIES = src/libwolfssl.la endif EXTRA_DIST += tests/unit.h EXTRA_DIST += tests/test.conf \ diff --git a/tests/unit.c b/tests/unit.c index 377bdabef..933a356b9 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -1,4 +1,8 @@ /* unit.c unit tests driver */ + +/* Name change compatibility layer */ +#include + #ifdef HAVE_CONFIG_H #include #endif diff --git a/testsuite/include.am b/testsuite/include.am index e9c5eb5bd..62edb4a30 100644 --- a/testsuite/include.am +++ b/testsuite/include.am @@ -7,15 +7,15 @@ if BUILD_EXAMPLES check_PROGRAMS += testsuite/testsuite.test noinst_PROGRAMS += testsuite/testsuite.test testsuite_testsuite_test_SOURCES = \ - ctaocrypt/test/test.c \ + wolfcrypt/test/test.c \ examples/client/client.c \ examples/echoclient/echoclient.c \ examples/echoserver/echoserver.c \ examples/server/server.c \ testsuite/testsuite.c testsuite_testsuite_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS) -testsuite_testsuite_test_LDADD = src/libcyassl.la -testsuite_testsuite_test_DEPENDENCIES = src/libcyassl.la +testsuite_testsuite_test_LDADD = src/libwolfssl.la +testsuite_testsuite_test_DEPENDENCIES = src/libwolfssl.la endif EXTRA_DIST += testsuite/testsuite.sln EXTRA_DIST += testsuite/testsuite-ntru.vcproj diff --git a/testsuite/testsuite-ntru.vcproj b/testsuite/testsuite-ntru.vcproj index 6ed108a22..0bca0c236 100755 --- a/testsuite/testsuite-ntru.vcproj +++ b/testsuite/testsuite-ntru.vcproj @@ -42,7 +42,7 @@ Name="VCCLCompilerTool" Optimization="0" AdditionalIncludeDirectories="../;../NTRU/include" - PreprocessorDefinitions="NO_MAIN_DRIVER;CYASSL_RIPEMD;CYASSL_SHA512;OPENSSL_EXTRA;HAVE_NTRU;NO_PSK;WIN32" + PreprocessorDefinitions="NO_MAIN_DRIVER;WOLFSSL_RIPEMD;WOLFSSL_SHA512;OPENSSL_EXTRA;HAVE_NTRU;NO_PSK;WIN32" MinimalRebuild="true" BasicRuntimeChecks="3" RuntimeLibrary="3" @@ -118,7 +118,7 @@ Optimization="2" EnableIntrinsicFunctions="true" AdditionalIncludeDirectories="../;../NTRU/include" - PreprocessorDefinitions="NO_MAIN_DRIVER;CYASSL_RIPEMD;CYASSL_SHA512;OPENSSL_EXTRA;HAVE_NTRU;NO_PSK;WIN32" + PreprocessorDefinitions="NO_MAIN_DRIVER;WOLFSSL_RIPEMD;WOLFSSL_SHA512;OPENSSL_EXTRA;HAVE_NTRU;NO_PSK;WIN32" RuntimeLibrary="2" EnableFunctionLevelLinking="true" UsePrecompiledHeader="0" @@ -193,7 +193,7 @@ > #endif -#include +#include -#include -#include "ctaocrypt/test/test.h" +#include +#include "wolfcrypt/test/test.h" #ifndef SINGLE_THREADED -#include -#include +#include +#include #include "examples/echoclient/echoclient.h" #include "examples/echoserver/echoserver.h" @@ -88,12 +88,12 @@ int testsuite_test(int argc, char** argv) server_args.argc = argc; server_args.argv = argv; - CyaSSL_Init(); -#if defined(DEBUG_CYASSL) && !defined(HAVE_VALGRIND) - CyaSSL_Debugging_ON(); + wolfSSL_Init(); +#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) + wolfSSL_Debugging_ON(); #endif -#if !defined(CYASSL_TIRTOS) +#if !defined(WOLFSSL_TIRTOS) if (CurrentDir("testsuite") || CurrentDir("_build")) ChangeDirBack(1); else if (CurrentDir("Debug") || CurrentDir("Release")) @@ -103,18 +103,18 @@ int testsuite_test(int argc, char** argv) /* Debug or Release */ #endif -#ifdef CYASSL_TIRTOS +#ifdef WOLFSSL_TIRTOS fdOpenSession(Task_self()); #endif server_args.signal = &ready; InitTcpReady(&ready); - /* CTaoCrypt test */ - ctaocrypt_test(&server_args); + /* wc_ test */ + wolfcrypt_test(&server_args); if (server_args.return_code != 0) return server_args.return_code; - /* Simple CyaSSL client server test */ + /* Simple wolfSSL client server test */ simple_test(&server_args); if (server_args.return_code != 0) return server_args.return_code; @@ -148,7 +148,7 @@ int testsuite_test(int argc, char** argv) echoclient_test(&echo_args); if (echo_args.return_code != 0) return echo_args.return_code; -#ifdef CYASSL_DTLS +#ifdef WOLFSSL_DTLS wait_tcp_ready(&server_args); #endif /* send quit to echoserver */ @@ -165,7 +165,7 @@ int testsuite_test(int argc, char** argv) { char ciphers[1024]; XMEMSET(ciphers, 0, sizeof(ciphers)); - CyaSSL_get_ciphers(ciphers, sizeof(ciphers)-1); + wolfSSL_get_ciphers(ciphers, sizeof(ciphers)-1); printf("ciphers = %s\n", ciphers); } @@ -180,10 +180,10 @@ int testsuite_test(int argc, char** argv) return EXIT_FAILURE; } - CyaSSL_Cleanup(); + wolfSSL_Cleanup(); FreeTcpReady(&ready); -#ifdef CYASSL_TIRTOS +#ifdef WOLFSSL_TIRTOS fdCloseSession(Task_self()); #endif @@ -237,8 +237,8 @@ void simple_test(func_args* args) cliArgs.return_code = 0; strcpy(svrArgs.argv[0], "SimpleServer"); - #if !defined(USE_WINDOWS_API) && !defined(CYASSL_SNIFFER) && \ - !defined(CYASSL_TIRTOS) + #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_SNIFFER) && \ + !defined(WOLFSSL_TIRTOS) strcpy(svrArgs.argv[svrArgs.argc++], "-p"); strcpy(svrArgs.argv[svrArgs.argc++], "0"); #endif @@ -296,7 +296,7 @@ void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread) #if defined(_POSIX_THREADS) && !defined(__MINGW32__) pthread_create(thread, 0, fun, args); return; -#elif defined(CYASSL_TIRTOS) +#elif defined(WOLFSSL_TIRTOS) /* Initialize the defaults and set the parameters. */ Task_Params taskParams; Task_Params_init(&taskParams); @@ -317,7 +317,7 @@ void join_thread(THREAD_TYPE thread) { #if defined(_POSIX_THREADS) && !defined(__MINGW32__) pthread_join(thread, 0); -#elif defined(CYASSL_TIRTOS) +#elif defined(WOLFSSL_TIRTOS) while(1) { if (Task_getMode(thread) == Task_Mode_TERMINATED) { Task_sleep(5); @@ -420,7 +420,7 @@ int main(int argc, char** argv) /* Relative to Workspace, Build/Products */ /* Debug or Release */ - ctaocrypt_test(&server_args); + wolfcrypt_test(&server_args); if (server_args.return_code != 0) return server_args.return_code; printf("\nAll tests passed!\n"); diff --git a/testsuite/testsuite.vcproj b/testsuite/testsuite.vcproj index a569b1315..ce71e147d 100755 --- a/testsuite/testsuite.vcproj +++ b/testsuite/testsuite.vcproj @@ -42,7 +42,7 @@ Name="VCCLCompilerTool" Optimization="0" AdditionalIncludeDirectories="../" - PreprocessorDefinitions="NO_MAIN_DRIVER;CYASSL_RIPEMD;CYASSL_SHA512;OPENSSL_EXTRA;NO_PSK" + PreprocessorDefinitions="NO_MAIN_DRIVER;WOLFSSL_RIPEMD;WOLFSSL_SHA512;OPENSSL_EXTRA;NO_PSK" MinimalRebuild="true" BasicRuntimeChecks="3" RuntimeLibrary="3" @@ -117,7 +117,7 @@ Optimization="2" EnableIntrinsicFunctions="true" AdditionalIncludeDirectories="../" - PreprocessorDefinitions="NO_MAIN_DRIVER;CYASSL_RIPEMD;CYASSL_SHA512;OPENSSL_EXTRA;NO_PSK" + PreprocessorDefinitions="NO_MAIN_DRIVER;WOLFSSL_RIPEMD;WOLFSSL_SHA512;OPENSSL_EXTRA;NO_PSK" RuntimeLibrary="2" EnableFunctionLevelLinking="true" UsePrecompiledHeader="0" @@ -191,7 +191,7 @@ > Disabled ../;%(AdditionalIncludeDirectories) - NO_MAIN_DRIVER;CYASSL_RIPEMD;CYASSL_SHA512;OPENSSL_EXTRA;NO_PSK;%(PreprocessorDefinitions) + NO_MAIN_DRIVER;WOLFSSL_RIPEMD;WOLFSSL_SHA512;OPENSSL_EXTRA;NO_PSK;%(PreprocessorDefinitions) true EnableFastChecks MultiThreadedDebugDLL @@ -104,7 +104,7 @@ Disabled ../;%(AdditionalIncludeDirectories) - NO_MAIN_DRIVER;CYASSL_RIPEMD;CYASSL_SHA512;OPENSSL_EXTRA;NO_PSK;%(PreprocessorDefinitions) + NO_MAIN_DRIVER;WOLFSSL_RIPEMD;WOLFSSL_SHA512;OPENSSL_EXTRA;NO_PSK;%(PreprocessorDefinitions) EnableFastChecks MultiThreadedDebugDLL @@ -123,7 +123,7 @@ MaxSpeed true ../;%(AdditionalIncludeDirectories) - NO_MAIN_DRIVER;CYASSL_RIPEMD;CYASSL_SHA512;OPENSSL_EXTRA;NO_PSK;%(PreprocessorDefinitions) + NO_MAIN_DRIVER;WOLFSSL_RIPEMD;WOLFSSL_SHA512;OPENSSL_EXTRA;NO_PSK;%(PreprocessorDefinitions) MultiThreadedDLL true @@ -144,7 +144,7 @@ MaxSpeed true ../;%(AdditionalIncludeDirectories) - NO_MAIN_DRIVER;CYASSL_RIPEMD;CYASSL_SHA512;OPENSSL_EXTRA;NO_PSK;%(PreprocessorDefinitions) + NO_MAIN_DRIVER;WOLFSSL_RIPEMD;WOLFSSL_SHA512;OPENSSL_EXTRA;NO_PSK;%(PreprocessorDefinitions) MultiThreadedDLL true @@ -165,11 +165,11 @@ - + - + {73973223-5ee8-41ca-8e88-1d60e89a237b} false diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 5fd341995..3c687b72f 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -19,18 +19,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -/* CTaoCrypt benchmark */ +/* wolfCrypt benchmark */ -/* wolfssl_cyassl compatibility layer */ -#include - - #ifdef HAVE_CONFIG_H #include #endif -#include +#include #include @@ -41,6 +37,7 @@ #include #endif +#include #include #include #include @@ -68,18 +65,18 @@ #include "ntru_crypto.h" #endif -#if defined(CYASSL_MDK_ARM) - extern FILE * CyaSSL_fopen(const char *fname, const char *mode) ; - #define fopen CyaSSL_fopen +#if defined(WOLFSSL_MDK_ARM) + extern FILE * wolfSSL_fopen(const char *fname, const char *mode) ; + #define fopen wolfSSL_fopen #endif #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048) /* include test cert and key buffers for use with NO_FILESYSTEM */ - #if defined(CYASSL_MDK_ARM) + #if defined(WOLFSSL_MDK_ARM) #include "cert_data.h" /* use certs_test.c for initial data, so other commands can share the data. */ #else - #include + #include #endif #endif @@ -152,8 +149,8 @@ static int OpenNitroxDevice(int dma_mode,int dev_id) #endif -#if defined(DEBUG_CYASSL) && !defined(HAVE_VALGRIND) - CYASSL_API int CyaSSL_Debugging_ON(); +#if defined(DEBUG_WOLfSSL) && !defined(HAVE_VALGRIND) + WOLFSSL_API int wolfSSL_Debugging_ON(); #endif /* so embedded projects can pull in tests on their own */ @@ -169,8 +166,8 @@ int benchmark_test(void *args) { #endif - #if defined(DEBUG_CYASSL) && !defined(HAVE_VALGRIND) - CyaSSL_Debugging_ON(); + #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) + wolfSSL_Debugging_ON(); #endif #ifdef HAVE_CAVIUM @@ -188,7 +185,7 @@ int benchmark_test(void *args) bench_aesgcm(); #endif -#ifdef CYASSL_AES_COUNTER +#ifdef WOLFSSL_AES_COUNTER bench_aesctr(); #endif @@ -228,13 +225,13 @@ int benchmark_test(void *args) #ifndef NO_SHA256 bench_sha256(); #endif -#ifdef CYASSL_SHA384 +#ifdef WOLFSSL_SHA384 bench_sha384(); #endif -#ifdef CYASSL_SHA512 +#ifdef WOLFSSL_SHA512 bench_sha512(); #endif -#ifdef CYASSL_RIPEMD +#ifdef WOLFSSL_RIPEMD bench_ripemd(); #endif #ifdef HAVE_BLAKE2 @@ -255,7 +252,7 @@ int benchmark_test(void *args) bench_dh(); #endif -#if defined(CYASSL_KEY_GEN) && !defined(NO_RSA) +#if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) bench_rsaKeyGen(); #endif @@ -335,13 +332,13 @@ void bench_aes(int show) int ret; #ifdef HAVE_CAVIUM - if (AesInitCavium(&enc, CAVIUM_DEV_ID) != 0) { + if (wc_AesInitCavium(&enc, CAVIUM_DEV_ID) != 0) { printf("aes init cavium failed\n"); return; } #endif - ret = AesSetKey(&enc, key, 16, iv, AES_ENCRYPTION); + ret = wc_AesSetKey(&enc, key, 16, iv, AES_ENCRYPTION); if (ret != 0) { printf("AesSetKey failed, ret = %d\n", ret); return; @@ -349,7 +346,7 @@ void bench_aes(int show) start = current_time(1); for(i = 0; i < numBlocks; i++) - AesCbcEncrypt(&enc, plain, cipher, sizeof(plain)); + wc_AesCbcEncrypt(&enc, plain, cipher, sizeof(plain)); total = current_time(0) - start; @@ -363,7 +360,7 @@ void bench_aes(int show) printf("AES %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks, blockType, total, persec); #ifdef HAVE_CAVIUM - AesFreeCavium(&enc); + wc_AesFreeCavium(&enc); #endif } #endif @@ -382,11 +379,11 @@ void bench_aesgcm(void) double start, total, persec; int i; - AesGcmSetKey(&enc, key, 16); + wc_AesGcmSetKey(&enc, key, 16); start = current_time(1); for(i = 0; i < numBlocks; i++) - AesGcmEncrypt(&enc, cipher, plain, sizeof(plain), iv, 12, + wc_AesGcmEncrypt(&enc, cipher, plain, sizeof(plain), iv, 12, tag, 16, additional, 13); total = current_time(0) - start; @@ -402,18 +399,18 @@ void bench_aesgcm(void) } #endif -#ifdef CYASSL_AES_COUNTER +#ifdef WOLFSSL_AES_COUNTER void bench_aesctr(void) { Aes enc; double start, total, persec; int i; - AesSetKeyDirect(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION); + wc_AesSetKeyDirect(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION); start = current_time(1); for(i = 0; i < numBlocks; i++) - AesCtrEncrypt(&enc, plain, cipher, sizeof(plain)); + wc_AesCtrEncrypt(&enc, plain, cipher, sizeof(plain)); total = current_time(0) - start; @@ -437,11 +434,11 @@ void bench_aesccm(void) double start, total, persec; int i; - AesCcmSetKey(&enc, key, 16); + wc_AesCcmSetKey(&enc, key, 16); start = current_time(1); for(i = 0; i < numBlocks; i++) - AesCcmEncrypt(&enc, cipher, plain, sizeof(plain), iv, 12, + wc_AesCcmEncrypt(&enc, cipher, plain, sizeof(plain), iv, 12, tag, 16, additional, 13); total = current_time(0) - start; @@ -468,7 +465,7 @@ void bench_poly1305() int ret; - ret = Poly1305SetKey(&enc, key, 32); + ret = wc_Poly1305SetKey(&enc, key, 32); if (ret != 0) { printf("Poly1305SetKey failed, ret = %d\n", ret); return; @@ -476,9 +473,9 @@ void bench_poly1305() start = current_time(1); for(i = 0; i < numBlocks; i++) - Poly1305Update(&enc, plain, sizeof(plain)); + wc_Poly1305Update(&enc, plain, sizeof(plain)); - Poly1305Final(&enc, mac); + wc_Poly1305Final(&enc, mac); total = current_time(0) - start; persec = 1 / total * numBlocks; @@ -500,7 +497,7 @@ void bench_camellia(void) double start, total, persec; int i, ret; - ret = CamelliaSetKey(&cam, key, 16, iv); + ret = wc_CamelliaSetKey(&cam, key, 16, iv); if (ret != 0) { printf("CamelliaSetKey failed, ret = %d\n", ret); return; @@ -508,7 +505,7 @@ void bench_camellia(void) start = current_time(1); for(i = 0; i < numBlocks; i++) - CamelliaCbcEncrypt(&cam, plain, cipher, sizeof(plain)); + wc_CamelliaCbcEncrypt(&cam, plain, cipher, sizeof(plain)); total = current_time(0) - start; @@ -532,10 +529,10 @@ void bench_des(void) int i, ret; #ifdef HAVE_CAVIUM - if (Des3_InitCavium(&enc, CAVIUM_DEV_ID) != 0) + if (wc_Des3_InitCavium(&enc, CAVIUM_DEV_ID) != 0) printf("des3 init cavium failed\n"); #endif - ret = Des3_SetKey(&enc, key, iv, DES_ENCRYPTION); + ret = wc_Des3_SetKey(&enc, key, iv, DES_ENCRYPTION); if (ret != 0) { printf("Des3_SetKey failed, ret = %d\n", ret); return; @@ -543,7 +540,7 @@ void bench_des(void) start = current_time(1); for(i = 0; i < numBlocks; i++) - Des3_CbcEncrypt(&enc, plain, cipher, sizeof(plain)); + wc_Des3_CbcEncrypt(&enc, plain, cipher, sizeof(plain)); total = current_time(0) - start; @@ -556,7 +553,7 @@ void bench_des(void) printf("3DES %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks, blockType, total, persec); #ifdef HAVE_CAVIUM - Des3_FreeCavium(&enc); + wc_Des3_FreeCavium(&enc); #endif } #endif @@ -603,11 +600,11 @@ void bench_hc128(void) double start, total, persec; int i; - Hc128_SetKey(&enc, key, iv); + wc_Hc128_SetKey(&enc, key, iv); start = current_time(1); for(i = 0; i < numBlocks; i++) - Hc128_Process(&enc, cipher, plain, sizeof(plain)); + wc_Hc128_Process(&enc, cipher, plain, sizeof(plain)); total = current_time(0) - start; persec = 1 / total * numBlocks; @@ -629,11 +626,11 @@ void bench_rabbit(void) double start, total, persec; int i; - RabbitSetKey(&enc, key, iv); + wc_RabbitSetKey(&enc, key, iv); start = current_time(1); for(i = 0; i < numBlocks; i++) - RabbitProcess(&enc, cipher, plain, sizeof(plain)); + wc_RabbitProcess(&enc, cipher, plain, sizeof(plain)); total = current_time(0) - start; persec = 1 / total * numBlocks; @@ -655,12 +652,12 @@ void bench_chacha(void) double start, total, persec; int i; - Chacha_SetKey(&enc, key, 16); + wc_Chacha_SetKey(&enc, key, 16); start = current_time(1); for (i = 0; i < numBlocks; i++) { - Chacha_SetIV(&enc, iv, 0); - Chacha_Process(&enc, cipher, plain, sizeof(plain)); + wc_Chacha_SetIV(&enc, iv, 0); + wc_Chacha_Process(&enc, cipher, plain, sizeof(plain)); } total = current_time(0) - start; persec = 1 / total * numBlocks; @@ -683,13 +680,13 @@ void bench_md5(void) double start, total, persec; int i; - InitMd5(&hash); + wc_InitMd5(&hash); start = current_time(1); for(i = 0; i < numBlocks; i++) - Md5Update(&hash, plain, sizeof(plain)); + wc_Md5Update(&hash, plain, sizeof(plain)); - Md5Final(&hash, digest); + wc_Md5Final(&hash, digest); total = current_time(0) - start; persec = 1 / total * numBlocks; @@ -712,7 +709,7 @@ void bench_sha(void) double start, total, persec; int i, ret; - ret = InitSha(&hash); + ret = wc_InitSha(&hash); if (ret != 0) { printf("InitSha failed, ret = %d\n", ret); return; @@ -720,9 +717,9 @@ void bench_sha(void) start = current_time(1); for(i = 0; i < numBlocks; i++) - ShaUpdate(&hash, plain, sizeof(plain)); + wc_ShaUpdate(&hash, plain, sizeof(plain)); - ShaFinal(&hash, digest); + wc_ShaFinal(&hash, digest); total = current_time(0) - start; persec = 1 / total * numBlocks; @@ -745,7 +742,7 @@ void bench_sha256(void) double start, total, persec; int i, ret; - ret = InitSha256(&hash); + ret = wc_InitSha256(&hash); if (ret != 0) { printf("InitSha256 failed, ret = %d\n", ret); return; @@ -753,14 +750,14 @@ void bench_sha256(void) start = current_time(1); for(i = 0; i < numBlocks; i++) { - ret = Sha256Update(&hash, plain, sizeof(plain)); + ret = wc_Sha256Update(&hash, plain, sizeof(plain)); if (ret != 0) { printf("Sha256Update failed, ret = %d\n", ret); return; } } - ret = Sha256Final(&hash, digest); + ret = wc_Sha256Final(&hash, digest); if (ret != 0) { printf("Sha256Final failed, ret = %d\n", ret); return; @@ -778,7 +775,7 @@ void bench_sha256(void) } #endif -#ifdef CYASSL_SHA384 +#ifdef WOLFSSL_SHA384 void bench_sha384(void) { Sha384 hash; @@ -786,7 +783,7 @@ void bench_sha384(void) double start, total, persec; int i, ret; - ret = InitSha384(&hash); + ret = wc_InitSha384(&hash); if (ret != 0) { printf("InitSha384 failed, ret = %d\n", ret); return; @@ -794,14 +791,14 @@ void bench_sha384(void) start = current_time(1); for(i = 0; i < numBlocks; i++) { - ret = Sha384Update(&hash, plain, sizeof(plain)); + ret = wc_Sha384Update(&hash, plain, sizeof(plain)); if (ret != 0) { printf("Sha384Update failed, ret = %d\n", ret); return; } } - ret = Sha384Final(&hash, digest); + ret = wc_Sha384Final(&hash, digest); if (ret != 0) { printf("Sha384Final failed, ret = %d\n", ret); return; @@ -819,7 +816,7 @@ void bench_sha384(void) } #endif -#ifdef CYASSL_SHA512 +#ifdef WOLFSSL_SHA512 void bench_sha512(void) { Sha512 hash; @@ -827,7 +824,7 @@ void bench_sha512(void) double start, total, persec; int i, ret; - ret = InitSha512(&hash); + ret = wc_InitSha512(&hash); if (ret != 0) { printf("InitSha512 failed, ret = %d\n", ret); return; @@ -835,14 +832,14 @@ void bench_sha512(void) start = current_time(1); for(i = 0; i < numBlocks; i++) { - ret = Sha512Update(&hash, plain, sizeof(plain)); + ret = wc_Sha512Update(&hash, plain, sizeof(plain)); if (ret != 0) { printf("Sha512Update failed, ret = %d\n", ret); return; } } - ret = Sha512Final(&hash, digest); + ret = wc_Sha512Final(&hash, digest); if (ret != 0) { printf("Sha512Final failed, ret = %d\n", ret); return; @@ -860,7 +857,7 @@ void bench_sha512(void) } #endif -#ifdef CYASSL_RIPEMD +#ifdef WOLFSSL_RIPEMD void bench_ripemd(void) { RipeMd hash; @@ -868,13 +865,13 @@ void bench_ripemd(void) double start, total, persec; int i; - InitRipeMd(&hash); + wc_InitRipeMd(&hash); start = current_time(1); for(i = 0; i < numBlocks; i++) - RipeMdUpdate(&hash, plain, sizeof(plain)); + wc_RipeMdUpdate(&hash, plain, sizeof(plain)); - RipeMdFinal(&hash, digest); + wc_RipeMdFinal(&hash, digest); total = current_time(0) - start; persec = 1 / total * numBlocks; @@ -897,7 +894,7 @@ void bench_blake2(void) double start, total, persec; int i, ret; - ret = InitBlake2b(&b2b, 64); + ret = wc_InitBlake2b(&b2b, 64); if (ret != 0) { printf("InitBlake2b failed, ret = %d\n", ret); return; @@ -905,14 +902,14 @@ void bench_blake2(void) start = current_time(1); for(i = 0; i < numBlocks; i++) { - ret = Blake2bUpdate(&b2b, plain, sizeof(plain)); + ret = wc_Blake2bUpdate(&b2b, plain, sizeof(plain)); if (ret != 0) { printf("Blake2bUpdate failed, ret = %d\n", ret); return; } } - ret = Blake2bFinal(&b2b, digest, 64); + ret = wc_Blake2bFinal(&b2b, digest, 64); if (ret != 0) { printf("Blake2bFinal failed, ret = %d\n", ret); return; @@ -932,7 +929,7 @@ void bench_blake2(void) #if !defined(NO_RSA) || !defined(NO_DH) \ - || defined(CYASSL_KEYGEN) || defined(HAVE_ECC) + || defined(WOLFSSL_KEYGEN) || defined(HAVE_ECC) static RNG rng; #endif @@ -940,7 +937,7 @@ static RNG rng; #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) - #if defined(CYASSL_MDK_SHELL) + #if defined(WOLFSSL_MDK_SHELL) static char *certRSAname = "certs/rsa2048.der"; /* set by shell command */ static void set_Bench_RSA_File(char * cert) { certRSAname = cert ; } @@ -988,25 +985,25 @@ void bench_rsa(void) #ifdef HAVE_CAVIUM - if (RsaInitCavium(&rsaKey, CAVIUM_DEV_ID) != 0) + if (wc_RsaInitCavium(&rsaKey, CAVIUM_DEV_ID) != 0) printf("RSA init cavium failed\n"); #endif - ret = InitRng(&rng); + ret = wc_InitRng(&rng); if (ret < 0) { printf("InitRNG failed\n"); return; } - ret = InitRsaKey(&rsaKey, 0); + ret = wc_InitRsaKey(&rsaKey, 0); if (ret < 0) { printf("InitRsaKey failed\n"); return; } - ret = RsaPrivateKeyDecode(tmp, &idx, &rsaKey, (word32)bytes); + ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsaKey, (word32)bytes); start = current_time(1); for (i = 0; i < ntimes; i++) - ret = RsaPublicEncrypt(message,len,enc,sizeof(enc), &rsaKey, &rng); + ret = wc_RsaPublicEncrypt(message,len,enc,sizeof(enc), &rsaKey, &rng); total = current_time(0) - start; each = total / ntimes; /* per second */ @@ -1024,7 +1021,7 @@ void bench_rsa(void) for (i = 0; i < ntimes; i++) { byte out[512]; /* for up to 4096 bit */ - RsaPrivateDecrypt(enc, (word32)ret, out, sizeof(out), &rsaKey); + wc_RsaPrivateDecrypt(enc, (word32)ret, out, sizeof(out), &rsaKey); } total = current_time(0) - start; @@ -1034,9 +1031,9 @@ void bench_rsa(void) printf("RSA %d decryption took %6.3f milliseconds, avg over %d" " iterations\n", rsaKeySz, milliEach, ntimes); - FreeRsaKey(&rsaKey); + wc_FreeRsaKey(&rsaKey); #ifdef HAVE_CAVIUM - RsaFreeCavium(&rsaKey); + wc_RsaFreeCavium(&rsaKey); #endif } #endif @@ -1046,7 +1043,7 @@ void bench_rsa(void) #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) - #if defined(CYASSL_MDK_SHELL) + #if defined(WOLFSSL_MDK_SHELL) static char *certDHname = "certs/dh2048.der"; /* set by shell command */ void set_Bench_DH_File(char * cert) { certDHname = cert ; } @@ -1093,7 +1090,7 @@ void bench_dh(void) return; } - ret = InitRng(&rng); + ret = wc_InitRng(&rng); if (ret < 0) { printf("InitRNG failed\n"); return; @@ -1102,8 +1099,8 @@ void bench_dh(void) #endif /* USE_CERT_BUFFERS */ - InitDhKey(&dhKey); - bytes = DhKeyDecode(tmp, &idx, &dhKey, (word32)bytes); + wc_InitDhKey(&dhKey); + bytes = wc_DhKeyDecode(tmp, &idx, &dhKey, (word32)bytes); if (bytes != 0) { printf("dhekydecode failed, can't benchmark\n"); #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) @@ -1115,7 +1112,7 @@ void bench_dh(void) start = current_time(1); for (i = 0; i < ntimes; i++) - DhGenerateKeyPair(&dhKey, &rng, priv, &privSz, pub, &pubSz); + wc_DhGenerateKeyPair(&dhKey, &rng, priv, &privSz, pub, &pubSz); total = current_time(0) - start; each = total / ntimes; /* per second */ @@ -1124,11 +1121,11 @@ void bench_dh(void) printf("DH %d key generation %6.3f milliseconds, avg over %d" " iterations\n", dhKeySz, milliEach, ntimes); - DhGenerateKeyPair(&dhKey, &rng, priv2, &privSz2, pub2, &pubSz2); + wc_DhGenerateKeyPair(&dhKey, &rng, priv2, &privSz2, pub2, &pubSz2); start = current_time(1); for (i = 0; i < ntimes; i++) - DhAgree(&dhKey, agree, &agreeSz, priv, privSz, pub2, pubSz2); + wc_DhAgree(&dhKey, agree, &agreeSz, priv, privSz, pub2, pubSz2); total = current_time(0) - start; each = total / ntimes; /* per second */ @@ -1140,11 +1137,11 @@ void bench_dh(void) #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) fclose(file); #endif - FreeDhKey(&dhKey); + wc_FreeDhKey(&dhKey); } #endif -#if defined(CYASSL_KEY_GEN) && !defined(NO_RSA) +#if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) void bench_rsaKeyGen(void) { RsaKey genKey; @@ -1155,9 +1152,9 @@ void bench_rsaKeyGen(void) start = current_time(1); for(i = 0; i < genTimes; i++) { - InitRsaKey(&genKey, 0); - MakeRsaKey(&genKey, 1024, 65537, &rng); - FreeRsaKey(&genKey); + wc_InitRsaKey(&genKey, 0); + wc_MakeRsaKey(&genKey, 1024, 65537, &rng); + wc_FreeRsaKey(&genKey); } total = current_time(0) - start; @@ -1171,9 +1168,9 @@ void bench_rsaKeyGen(void) start = current_time(1); for(i = 0; i < genTimes; i++) { - InitRsaKey(&genKey, 0); - MakeRsaKey(&genKey, 2048, 65537, &rng); - FreeRsaKey(&genKey); + wc_InitRsaKey(&genKey, 0); + wc_MakeRsaKey(&genKey, 2048, 65537, &rng); + wc_FreeRsaKey(&genKey); } total = current_time(0) - start; @@ -1182,7 +1179,7 @@ void bench_rsaKeyGen(void) printf("RSA 2048 key generation %6.3f milliseconds, avg over %d" " iterations\n", milliEach, genTimes); } -#endif /* CYASSL_KEY_GEN */ +#endif /* WOLFSSL_KEY_GEN */ #ifdef HAVE_NTRU byte GetEntropy(ENTROPY_CMD cmd, byte* out); @@ -1385,7 +1382,7 @@ void bench_eccKeyGen(void) double start, total, each, milliEach; int i, ret; - ret = InitRng(&rng); + ret = wc_InitRng(&rng); if (ret < 0) { printf("InitRNG failed\n"); return; @@ -1394,8 +1391,8 @@ void bench_eccKeyGen(void) start = current_time(1); for(i = 0; i < genTimes; i++) { - ecc_make_key(&rng, 32, &genKey); - ecc_free(&genKey); + wc_ecc_make_key(&rng, 32, &genKey); + wc_ecc_free(&genKey); } total = current_time(0) - start; @@ -1417,21 +1414,21 @@ void bench_eccKeyAgree(void) byte digest[32]; word32 x = 0; - ecc_init(&genKey); - ecc_init(&genKey2); + wc_ecc_init(&genKey); + wc_ecc_init(&genKey2); - ret = InitRng(&rng); + ret = wc_InitRng(&rng); if (ret < 0) { printf("InitRNG failed\n"); return; } - ret = ecc_make_key(&rng, 32, &genKey); + ret = wc_ecc_make_key(&rng, 32, &genKey); if (ret != 0) { printf("ecc_make_key failed\n"); return; } - ret = ecc_make_key(&rng, 32, &genKey2); + ret = wc_ecc_make_key(&rng, 32, &genKey2); if (ret != 0) { printf("ecc_make_key failed\n"); return; @@ -1442,7 +1439,7 @@ void bench_eccKeyAgree(void) for(i = 0; i < agreeTimes; i++) { x = sizeof(shared); - ret = ecc_shared_secret(&genKey, &genKey2, shared, &x); + ret = wc_ecc_shared_secret(&genKey, &genKey2, shared, &x); if (ret != 0) { printf("ecc_shared_secret failed\n"); return; @@ -1464,7 +1461,7 @@ void bench_eccKeyAgree(void) for(i = 0; i < agreeTimes; i++) { x = sizeof(sig); - ret = ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &genKey); + ret = wc_ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &genKey); if (ret != 0) { printf("ecc_sign_hash failed\n"); return; @@ -1481,7 +1478,7 @@ void bench_eccKeyAgree(void) for(i = 0; i < agreeTimes; i++) { int verify = 0; - ret = ecc_verify_hash(sig, x, digest, sizeof(digest), &verify, &genKey); + ret = wc_ecc_verify_hash(sig, x, digest, sizeof(digest), &verify, &genKey); if (ret != 0) { printf("ecc_verify_hash failed\n"); return; @@ -1494,8 +1491,8 @@ void bench_eccKeyAgree(void) printf("EC-DSA verify time %6.3f milliseconds, avg over %d" " iterations\n", milliEach, agreeTimes); - ecc_free(&genKey2); - ecc_free(&genKey); + wc_ecc_free(&genKey2); + wc_ecc_free(&genKey); } #endif /* HAVE_ECC */ diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 311dc6db9..63b232ec7 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -181,7 +181,6 @@ void wc_AesFreeCavium(Aes* aes) #ifdef HAVE_FIPS - /* fips wrapper calls, user can call direct */ int wc_AesSetKey_fips(Aes* aes, const byte* key, word32 len, const byte* iv, int dir) { @@ -233,17 +232,6 @@ void wc_AesFreeCavium(Aes* aes) return AesGcmDecrypt_fips(aes, out, in, sz, iv, ivSz, authTag, authTagSz, authIn, authInSz); } - #ifndef FIPS_NO_WRAPPERS - /* if not impl or fips.c impl wrapper force fips calls if fips build */ - #define AesSetKey AesSetKey_fips - #define AesSetIV AesSetIV_fips - #define AesCbcEncrypt AesCbcEncrypt_fips - #define AesCbcDecrypt AesCbcDecrypt_fips - #define AesGcmSetKey AesGcmSetKey_fips - #define AesGcmEncrypt AesGcmEncrypt_fips - #define AesGcmDecrypt AesGcmDecrypt_fips - #endif /* FIPS_NO_WRAPPERS */ - #endif /* HAVE_FIPS */ diff --git a/wolfcrypt/src/compress.c b/wolfcrypt/src/compress.c new file mode 100644 index 000000000..4713bc618 --- /dev/null +++ b/wolfcrypt/src/compress.c @@ -0,0 +1,169 @@ +/* compress.c + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + wc_* + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#ifdef HAVE_LIBZ + + +#include +#include +#include +#ifdef NO_INLINE + #include +#else + #include +#endif + +#include + + +/* alloc user allocs to work with zlib */ +static void* myAlloc(void* opaque, unsigned int item, unsigned int size) +{ + (void)opaque; + return XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ); +} + + +static void myFree(void* opaque, void* memory) +{ + (void)opaque; + XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ); +} + + +#ifdef HAVE_MCAPI + #define DEFLATE_DEFAULT_WINDOWBITS 11 + #define DEFLATE_DEFAULT_MEMLEVEL 1 +#else + #define DEFLATE_DEFAULT_WINDOWBITS 15 + #define DEFLATE_DEFAULT_MEMLEVEL 8 +#endif + + +int wc_Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags) +/* + * out - pointer to destination buffer + * outSz - size of destination buffer + * in - pointer to source buffer to compress + * inSz - size of source to compress + * flags - flags to control how compress operates + * + * return: + * negative - error code + * positive - bytes stored in out buffer + * + * Note, the output buffer still needs to be larger than the input buffer. + * The right chunk of data won't compress at all, and the lookup table will + * add to the size of the output. The libz code says the compressed + * buffer should be srcSz + 0.1% + 12. + */ +{ + z_stream stream; + int result = 0; + + stream.next_in = (Bytef*)in; + stream.avail_in = (uInt)inSz; +#ifdef MAXSEG_64K + /* Check for source > 64K on 16-bit machine: */ + if ((uLong)stream.avail_in != inSz) return COMPRESS_INIT_E; +#endif + stream.next_out = out; + stream.avail_out = (uInt)outSz; + if ((uLong)stream.avail_out != outSz) return COMPRESS_INIT_E; + + stream.zalloc = (alloc_func)myAlloc; + stream.zfree = (free_func)myFree; + stream.opaque = (voidpf)0; + + if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, + DEFLATE_DEFAULT_WINDOWBITS, DEFLATE_DEFAULT_MEMLEVEL, + flags ? Z_FIXED : Z_DEFAULT_STRATEGY) != Z_OK) + return COMPRESS_INIT_E; + + if (deflate(&stream, Z_FINISH) != Z_STREAM_END) { + deflateEnd(&stream); + return COMPRESS_E; + } + + result = (int)stream.total_out; + + if (deflateEnd(&stream) != Z_OK) + result = COMPRESS_E; + + return result; +} + + +int wc_DeCompress(byte* out, word32 outSz, const byte* in, word32 inSz) +/* + * out - pointer to destination buffer + * outSz - size of destination buffer + * in - pointer to source buffer to compress + * inSz - size of source to compress + * flags - flags to control how compress operates + * + * return: + * negative - error code + * positive - bytes stored in out buffer + */ +{ + z_stream stream; + int result = 0; + + stream.next_in = (Bytef*)in; + stream.avail_in = (uInt)inSz; + /* Check for source > 64K on 16-bit machine: */ + if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E; + + stream.next_out = out; + stream.avail_out = (uInt)outSz; + if ((uLong)stream.avail_out != outSz) return DECOMPRESS_INIT_E; + + stream.zalloc = (alloc_func)myAlloc; + stream.zfree = (free_func)myFree; + stream.opaque = (voidpf)0; + + if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS) != Z_OK) + return DECOMPRESS_INIT_E; + + if (inflate(&stream, Z_FINISH) != Z_STREAM_END) { + inflateEnd(&stream); + return DECOMPRESS_E; + } + + result = (int)stream.total_out; + + if (inflateEnd(&stream) != Z_OK) + result = DECOMPRESS_E; + + return result; +} + + +#endif /* HAVE_LIBZ */ + diff --git a/wolfcrypt/src/dh.c b/wolfcrypt/src/dh.c index 8319fc983..f2251f705 100644 --- a/wolfcrypt/src/dh.c +++ b/wolfcrypt/src/dh.c @@ -23,12 +23,12 @@ #include #endif -#include +#include #ifndef NO_DH -#include -#include +#include +#include #ifndef USER_MATH_LIB #include @@ -139,7 +139,7 @@ int wc_DhGenerateKeyPair(DhKey* key, RNG* rng, byte* priv, word32* privSz, return (ret != 0) ? ret : GeneratePublic(key, priv, *privSz, pub, pubSz); } -int DhAgree(DhKey* key, byte* agree, word32* agreeSz, const byte* priv, +int wc_DhAgree(DhKey* key, byte* agree, word32* agreeSz, const byte* priv, word32 privSz, const byte* otherPub, word32 pubSz) { int ret = 0; diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index 5c17f591d..e8db39aa5 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -23,19 +23,19 @@ #include #endif -#include +#include /* submitted by eof */ -#include -#include +#include +#include #ifdef __cplusplus extern "C" { #endif - CYASSL_API int CyaSSL_Debugging_ON(void); - CYASSL_API void CyaSSL_Debugging_OFF(void); + WOLFSSL_API int wolfSSL_Debugging_ON(void); + WOLFSSL_API void wolfSSL_Debugging_OFF(void); #ifdef __cplusplus } #endif @@ -44,13 +44,13 @@ #ifdef DEBUG_CYASSL /* Set these to default values initially. */ -static CyaSSL_Logging_cb log_function = 0; +static wolfSSL_Logging_cb log_function = 0; static int loggingEnabled = 0; #endif /* DEBUG_CYASSL */ -int CyaSSL_SetLoggingCb(CyaSSL_Logging_cb f) +int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb f) { #ifdef DEBUG_CYASSL int res = 0; @@ -68,7 +68,7 @@ int CyaSSL_SetLoggingCb(CyaSSL_Logging_cb f) } -int CyaSSL_Debugging_ON(void) +int wolfSSL_Debugging_ON(void) { #ifdef DEBUG_CYASSL loggingEnabled = 1; @@ -79,7 +79,7 @@ int CyaSSL_Debugging_ON(void) } -void CyaSSL_Debugging_OFF(void) +void wolfSSL_Debugging_OFF(void) { #ifdef DEBUG_CYASSL loggingEnabled = 0; @@ -123,40 +123,40 @@ static void cyassl_log(const int logLevel, const char *const logMessage) } -void CYASSL_MSG(const char* msg) +void WOLFSSL_MSG(const char* msg) { if (loggingEnabled) cyassl_log(INFO_LOG , msg); } -void CYASSL_ENTER(const char* msg) +void WOLFSSL_ENTER(const char* msg) { if (loggingEnabled) { char buffer[80]; - sprintf(buffer, "CyaSSL Entering %s", msg); + sprintf(buffer, "wolfSSL Entering %s", msg); cyassl_log(ENTER_LOG , buffer); } } -void CYASSL_LEAVE(const char* msg, int ret) +void WOLFSSL_LEAVE(const char* msg, int ret) { if (loggingEnabled) { char buffer[80]; - sprintf(buffer, "CyaSSL Leaving %s, return %d", msg, ret); + sprintf(buffer, "wolfSSL Leaving %s, return %d", msg, ret); cyassl_log(LEAVE_LOG , buffer); } } -void CYASSL_ERROR(int error) +void WOLFSSL_ERROR(int error) { if (loggingEnabled) { char buffer[80]; - sprintf(buffer, "CyaSSL error occured, error = %d", error); + sprintf(buffer, "wolfSSL error occured, error = %d", error); cyassl_log(ERROR_LOG , buffer); } } -#endif /* DEBUG_CYASSL */ +#endif /* DEBUG_WOLFSSL */ diff --git a/wolfcrypt/src/md2.c b/wolfcrypt/src/md2.c index 28dd5b257..be1e41089 100644 --- a/wolfcrypt/src/md2.c +++ b/wolfcrypt/src/md2.c @@ -26,8 +26,7 @@ #include -//#ifdef WOLFSSL_MD2 -//@TODO +#ifdef WOLFSSL_MD2 #include #include @@ -157,5 +156,5 @@ int wc_Md2Hash(const byte* data, word32 len, byte* hash) } -//@TODO -//#endif /* WOLFSSL_MD2 */ +#endif /* WOLFSSL_MD2 */ + diff --git a/wolfcrypt/src/md4.c b/wolfcrypt/src/md4.c index 40692629c..a0b4a1a12 100644 --- a/wolfcrypt/src/md4.c +++ b/wolfcrypt/src/md4.c @@ -23,15 +23,15 @@ #include #endif -#include +#include #ifndef NO_MD4 -#include +#include #ifdef NO_INLINE - #include + #include #else - #include + #include #endif diff --git a/wolfcrypt/src/memory.c b/wolfcrypt/src/memory.c index cc826b353..7bbbd6902 100644 --- a/wolfcrypt/src/memory.c +++ b/wolfcrypt/src/memory.c @@ -2,14 +2,14 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -25,8 +25,15 @@ #include -//#ifdef USE_WOLFSSL_MEMORY -//@TODO +/* check old macros @wc_fips */ +#if defined(USE_CYASSL_MEMORY) && !defined(USE_WOLFSSL_MEMORY) + #define USE_WOLFSSL_MEMORY +#endif +#if defined(CYASSL_MALLOC_CHECK) && !defined(WOLFSSL_MALLOC_CHECK) + #define WOLFSSL_MALLOC_CHECK +#endif + +#ifdef USE_WOLFSSL_MEMORY #include #include @@ -102,7 +109,7 @@ void* wolfSSL_Realloc(void *ptr, size_t size) return res; } -//#endif /* USE_WOLFSSL_MEMORY */ +#endif /* USE_WOLFSSL_MEMORY */ #ifdef HAVE_IO_POOL diff --git a/wolfcrypt/src/pkcs7.c b/wolfcrypt/src/pkcs7.c index b80c36aff..bccac5b52 100644 --- a/wolfcrypt/src/pkcs7.c +++ b/wolfcrypt/src/pkcs7.c @@ -23,13 +23,13 @@ #include #endif -#include +#include #ifdef HAVE_PKCS7 -#include -#include -#include +#include +#include +#include #ifndef min static INLINE word32 min(word32 a, word32 b) @@ -41,7 +41,7 @@ /* placed ASN.1 contentType OID into *output, return idx on success, * 0 upon failure */ -WOLFSSL_LOCAL int wc_setContentType(int pkcs7TypeOID, byte* output) +WOLFSSL_LOCAL int wc_SetContentType(int pkcs7TypeOID, byte* output) { /* PKCS#7 content types, RFC 2315, section 14 */ static const byte pkcs7[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, @@ -147,7 +147,7 @@ int wc_GetContentType(const byte* input, word32* inOutIdx, word32* oid, /* init PKCS7 struct with recipient cert, decode into DecodedCert */ -int wc_PKS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz) +int wc_PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz) { int ret = 0; @@ -789,7 +789,7 @@ int wc_PKCS7_VerifySignedData(PKCS7* pkcs7, byte* pkiMsg, word32 pkiMsgSz) cert = &pkiMsg[idx]; certSz += (certIdx - idx); } - wc_PKS7_InitWithCert(pkcs7, cert, certSz); + wc_PKCS7_InitWithCert(pkcs7, cert, certSz); } idx += length; } @@ -1222,7 +1222,7 @@ int wc_PKCS7_EncodeEnvelopedData(PKCS7* pkcs7, byte* output, word32 outputSz) }; /* outer content type */ - outerContentTypeSz = wc_setContentType(ENVELOPED_DATA, outerContentType); + outerContentTypeSz = wc_SetContentType(ENVELOPED_DATA, outerContentType); /* version, defined as 0 in RFC 2315 */ verSz = SetMyVersion(0, ver, 0); @@ -1279,7 +1279,7 @@ int wc_PKCS7_EncodeEnvelopedData(PKCS7* pkcs7, byte* output, word32 outputSz) } /* EncryptedContentInfo */ - contentTypeSz = wc_setContentType(pkcs7->contentOID, contentType); + contentTypeSz = wc_SetContentType(pkcs7->contentOID, contentType); if (contentTypeSz == 0) { #ifdef WOLFSSL_SMALL_STACK XFREE(recip, NULL, DYNAMMIC_TYPE_TMP_BUFFER); diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index fcf79a44a..21dc64cd5 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -183,7 +183,7 @@ int wc_RsaFlattenPublicKey(RsaKey* key, byte* a, word32* aSz, byte* b, } - int wc_ RsaSSL_Sign_fips(const byte* in, word32 inLen, byte* out, + int wc_RsaSSL_Sign_fips(const byte* in, word32 inLen, byte* out, word32 outLen, RsaKey* key, RNG* rng) { return RsaSSL_Sign_fips(in, inLen, out, outLen, key, rng); diff --git a/wolfcrypt/src/sha.c b/wolfcrypt/src/sha.c index e69de29bb..ca15b7562 100644 --- a/wolfcrypt/src/sha.c +++ b/wolfcrypt/src/sha.c @@ -0,0 +1,88 @@ +/* sha.c + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#if !defined(NO_SHA) + +#ifdef HAVE_FIPS + /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */ + #define FIPS_NO_WRAPPERS +#endif + +#include +#include +#include + +#ifdef NO_INLINE + #include +#else + #include +#endif + +int wc_InitSha(Sha* sha) +{ + return InitSha(sha); +} + +int wc_ShaUpdate(Sha* sha, const byte* data, word32 len) +{ + return ShaUpdate(sha, data, len); +} + +int wc_ShaFinal(Sha* sha, byte* hash) +{ + return ShaFinal(sha, hash); +} + + +int wc_ShaHash(const byte* data, word32 len, byte* hash) +{ + return ShaHash(data, len, hash); +} + + +/* fips wrapper calls, user can call direct */ +#ifdef HAVE_FIPS + int wc_InitSha_fips(Sha* sha) + { + return InitSha_fips(sha); + } + + + int wc_ShaUpdate_fips(Sha* sha, const byte* data, word32 len) + { + return ShaUpdate_fips(sha, data, len); + } + + + int wc_ShaFinal_fips(Sha* sha, byte* out) + { + return ShaFinal_fips(sha,out); + } +#endif /* HAVE_FIPS */ +#endif /* NO_SHA */ + diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index e69de29bb..d33473edb 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -0,0 +1,58 @@ +/* sha256.c + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + + +/* code submitted by raphael.huck@efixo.com */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include +#include + +#if !defined(NO_SHA256) + +int wc_InitSha256(Sha256* sha) +{ + return InitSha256(sha); +} + + +int wc_Sha256Update(Sha256* sha, const byte* data, word32 len) +{ + return Sha256Update(sha, data, len); +} + + +int wc_Sha256Final(Sha256* sha, byte* out) +{ + return Sha256Final(sha, out); +} + + +int wc_Sha256Hash(const byte* data, word32 len, byte* out) +{ + return Sha256Hash(data, len, out); +} + +#endif + diff --git a/wolfcrypt/src/sha512.c b/wolfcrypt/src/sha512.c new file mode 100644 index 000000000..bb2f1d944 --- /dev/null +++ b/wolfcrypt/src/sha512.c @@ -0,0 +1,90 @@ +/* sha512.c + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include +#include + +#ifdef WOLFSSL_SHA512 + +#ifdef __cplusplus + extern "C" { +#endif + +int wc_InitSha512(Sha512* sha) +{ + return InitSha512(sha); +} + + +int wc_Sha512Update(Sha512* sha, const byte* data, word32 len) +{ + return Sha512Update(sha, data, len); +} + + +int wc_Sha512Final(Sha512* sha, byte* out) +{ + return Sha512Final(sha, out); +} + + +int wc_Sha512Hash(const byte* data, word32 len, byte* out) +{ + return Sha512Hash(data, len, out); +} + +#if defined(WOLFSSL_SHA384) || defined(HAVE_AESGCM) + +int wc_InitSha384(Sha384* sha) +{ + return InitSha384(sha); +} + + +int wc_Sha384Update(Sha384* sha, const byte* data, word32 len) +{ + return Sha384Update(sha, data, len); +} + + +int wc_Sha384Final(Sha384* sha, byte* out) +{ + return Sha384Final(sha, out); +} + + +int wc_Sha384Hash(const byte* data, word32 len, byte* out) +{ + return Sha384Hash(data, len, out); +} + +#endif /* WOLFSSL_SHA384 */ + +#ifdef __cplusplus + } /* extern "C" */ +#endif + +#endif /* WOLFSSL_SHA512 */ + diff --git a/wolfcrypt/test/include.am~ b/wolfcrypt/test/include.am~ deleted file mode 100644 index ff6a7cae1..000000000 --- a/wolfcrypt/test/include.am~ +++ /dev/null @@ -1,11 +0,0 @@ -# vim:ft=automake -# All paths should be given relative to the root - -noinst_PROGRAMS+= wolfcrypt/test/testwolfcrypt -wolfcrypt_test_testwolfcrypt_SOURCES = wolfcrypt/test/test.c -wolfcrypt_test_testwolfcrypt_LDADD = src/libcyassl.la -wolfcrypt_test_testwolfcrypt_DEPENDENCIES = src/libcyassl.la -noinst_HEADERS += wolfcrypt/test/test.h -EXTRA_DIST += wolfcrypt/test/test.sln -EXTRA_DIST += wolfcrypt/test/test.vcproj -DISTCLEANFILES+= wolfcrypt/test/.libs/testwolfcrypt diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 6e9ffc56a..98d099097 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -19,9 +19,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -/* wolfssl_wolfssl compatibility layer */ -#include - #ifdef HAVE_CONFIG_H #include #endif @@ -648,11 +645,11 @@ int md2_test() test_md2[5] = f; test_md2[6] = g; - InitMd2(&md2); + wc_InitMd2(&md2); for (i = 0; i < times; ++i) { - Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen); - Md2Final(&md2, hash); + wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen); + wc_Md2Final(&md2, hash); if (memcmp(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) return -155 - i; @@ -710,11 +707,11 @@ int md5_test(void) test_md5[3] = d; test_md5[4] = e; - InitMd5(&md5); + wc_InitMd5(&md5); for (i = 0; i < times; ++i) { - Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen); - Md5Final(&md5, hash); + wc_Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen); + wc_Md5Final(&md5, hash); if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0) return -5 - i; @@ -788,11 +785,11 @@ int md4_test(void) test_md4[5] = f; test_md4[6] = g; - InitMd4(&md4); + wc_InitMd4(&md4); for (i = 0; i < times; ++i) { - Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen); - Md4Final(&md4, hash); + wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen); + wc_Md4Final(&md4, hash); if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0) return -205 - i; @@ -847,13 +844,13 @@ int sha_test(void) test_sha[2] = c; test_sha[3] = d; - ret = InitSha(&sha); + ret = wc_InitSha(&sha); if (ret != 0) return -4001; for (i = 0; i < times; ++i) { - ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen); - ShaFinal(&sha, hash); + wc_ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen); + wc_ShaFinal(&sha, hash); if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0) return -10 - i; @@ -904,12 +901,12 @@ int ripemd_test(void) test_ripemd[2] = c; test_ripemd[3] = d; - InitRipeMd(&ripemd); + wc_InitRipeMd(&ripemd); for (i = 0; i < times; ++i) { - RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input, + wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input, (word32)test_ripemd[i].inLen); - RipeMdFinal(&ripemd, hash); + wc_RipeMdFinal(&ripemd, hash); if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0) return -10 - i; @@ -972,15 +969,15 @@ int blake2b_test(void) input[i] = (byte)i; for (i = 0; i < BLAKE2_TESTS; i++) { - ret = InitBlake2b(&b2b, 64); + ret = wc_InitBlake2b(&b2b, 64); if (ret != 0) return -4002; - ret = Blake2bUpdate(&b2b, input, i); + ret = wc_Blake2bUpdate(&b2b, input, i); if (ret != 0) return -4003; - ret = Blake2bFinal(&b2b, digest, 64); + ret = wc_Blake2bFinal(&b2b, digest, 64); if (ret != 0) return -4004; @@ -1022,15 +1019,15 @@ int sha256_test(void) test_sha[0] = a; test_sha[1] = b; - ret = InitSha256(&sha); + ret = wc_InitSha256(&sha); if (ret != 0) return -4005; for (i = 0; i < times; ++i) { - ret = Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen); + ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen); if (ret != 0) return -4006; - ret = Sha256Final(&sha, hash); + ret = wc_Sha256Final(&sha, hash); if (ret != 0) return -4007; @@ -1043,7 +1040,7 @@ int sha256_test(void) #endif -#ifdef WOLFSSL_SHA512 +#ifdef CYASSL_SHA512 int sha512_test(void) { Sha512 sha; @@ -1076,16 +1073,16 @@ int sha512_test(void) test_sha[0] = a; test_sha[1] = b; - ret = InitSha512(&sha); + ret = wc_InitSha512(&sha); if (ret != 0) return -4009; for (i = 0; i < times; ++i) { - ret = Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen); + ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen); if (ret != 0) return -4010; - ret = Sha512Final(&sha, hash); + ret = wc_Sha512Final(&sha, hash); if (ret != 0) return -4011; @@ -1098,7 +1095,7 @@ int sha512_test(void) #endif -#ifdef WOLFSSL_SHA384 +#ifdef CYASSL_SHA384 int sha384_test(void) { Sha384 sha; @@ -1129,16 +1126,16 @@ int sha384_test(void) test_sha[0] = a; test_sha[1] = b; - ret = InitSha384(&sha); + ret = wc_InitSha384(&sha); if (ret != 0) return -4012; for (i = 0; i < times; ++i) { - ret = Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen); + ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen); if (ret != 0) return -4013; - ret = Sha384Final(&sha, hash); + ret = wc_Sha384Final(&sha, hash); if (ret != 0) return -4014; @@ -1201,24 +1198,24 @@ int hmac_md5_test(void) continue; /* cavium can't handle short keys, fips not allowed */ #endif #ifdef HAVE_CAVIUM - if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0) + if (wc_HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0) return -20009; #endif - ret = HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i])); + ret = wc_HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i])); if (ret != 0) return -4015; - ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input, + ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input, (word32)test_hmac[i].inLen); if (ret != 0) return -4016; - ret = HmacFinal(&hmac, hash); + ret = wc_HmacFinal(&hmac, hash); if (ret != 0) return -4017; if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0) return -20 - i; #ifdef HAVE_CAVIUM - HmacFreeCavium(&hmac); + wc_HmacFreeCavium(&hmac); #endif } @@ -1278,24 +1275,24 @@ int hmac_sha_test(void) continue; /* cavium can't handle short keys, fips not allowed */ #endif #ifdef HAVE_CAVIUM - if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0) + if (wc_HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0) return -20010; #endif - ret = HmacSetKey(&hmac, SHA, (byte*)keys[i], (word32)strlen(keys[i])); + ret = wc_HmacSetKey(&hmac, SHA, (byte*)keys[i], (word32)strlen(keys[i])); if (ret != 0) return -4018; - ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input, + ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input, (word32)test_hmac[i].inLen); if (ret != 0) return -4019; - ret = HmacFinal(&hmac, hash); + ret = wc_HmacFinal(&hmac, hash); if (ret != 0) return -4020; if (memcmp(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0) return -20 - i; #ifdef HAVE_CAVIUM - HmacFreeCavium(&hmac); + wc_HmacFreeCavium(&hmac); #endif } @@ -1359,24 +1356,24 @@ int hmac_sha256_test(void) continue; /* cavium can't handle short keys, fips not allowed */ #endif #ifdef HAVE_CAVIUM - if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0) + if (wc_HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0) return -20011; #endif - ret = HmacSetKey(&hmac, SHA256, (byte*)keys[i],(word32)strlen(keys[i])); + ret = wc_HmacSetKey(&hmac, SHA256, (byte*)keys[i],(word32)strlen(keys[i])); if (ret != 0) return -4021; - ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input, + ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input, (word32)test_hmac[i].inLen); if (ret != 0) return -4022; - ret = HmacFinal(&hmac, hash); + ret = wc_HmacFinal(&hmac, hash); if (ret != 0) return -4023; if (memcmp(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0) return -20 - i; #ifdef HAVE_CAVIUM - HmacFreeCavium(&hmac); + wc_HmacFreeCavium(&hmac); #endif } @@ -1440,25 +1437,25 @@ int hmac_blake2b_test(void) continue; /* cavium can't handle short keys, fips not allowed */ #endif #ifdef HAVE_CAVIUM - if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0) + if (wc_HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0) return -20011; #endif - ret = HmacSetKey(&hmac, BLAKE2B_ID, (byte*)keys[i], + ret = wc_HmacSetKey(&hmac, BLAKE2B_ID, (byte*)keys[i], (word32)strlen(keys[i])); if (ret != 0) return -4024; - ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input, + ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input, (word32)test_hmac[i].inLen); if (ret != 0) return -4025; - ret = HmacFinal(&hmac, hash); + ret = wc_HmacFinal(&hmac, hash); if (ret != 0) return -4026; if (memcmp(hash, test_hmac[i].output, BLAKE2B_256) != 0) return -20 - i; #ifdef HAVE_CAVIUM - HmacFreeCavium(&hmac); + wc_HmacFreeCavium(&hmac); #endif } @@ -1524,14 +1521,14 @@ int hmac_sha384_test(void) if (i == 1) continue; /* fips not allowed */ #endif - ret = HmacSetKey(&hmac, SHA384, (byte*)keys[i],(word32)strlen(keys[i])); + ret = wc_HmacSetKey(&hmac, SHA384, (byte*)keys[i],(word32)strlen(keys[i])); if (ret != 0) return -4027; - ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input, + ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input, (word32)test_hmac[i].inLen); if (ret != 0) return -4028; - ret = HmacFinal(&hmac, hash); + ret = wc_HmacFinal(&hmac, hash); if (ret != 0) return -4029; @@ -1544,7 +1541,7 @@ int hmac_sha384_test(void) #endif -#if !defined(NO_HMAC) && defined(WOLFSSL_SHA512) +#if !defined(NO_HMAC) && defined(CYASSL_SHA512) int hmac_sha512_test(void) { Hmac hmac; @@ -1604,14 +1601,14 @@ int hmac_sha512_test(void) if (i == 1) continue; /* fips not allowed */ #endif - ret = HmacSetKey(&hmac, SHA512, (byte*)keys[i],(word32)strlen(keys[i])); + ret = wc_HmacSetKey(&hmac, SHA512, (byte*)keys[i],(word32)strlen(keys[i])); if (ret != 0) return -4030; - ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input, + ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input, (word32)test_hmac[i].inLen); if (ret != 0) return -4031; - ret = HmacFinal(&hmac, hash); + ret = wc_HmacFinal(&hmac, hash); if (ret != 0) return -4032; @@ -1767,13 +1764,13 @@ int hc128_test(void) memcpy(plain, keys[i], 16); memcpy(cipher, ivs[i], 16); - Hc128_SetKey(&enc, plain, cipher); - Hc128_SetKey(&dec, plain, cipher); + wc_Hc128_SetKey(&enc, plain, cipher); + wc_Hc128_SetKey(&dec, plain, cipher); /* align input */ memcpy(plain, test_hc128[i].input, test_hc128[i].outLen); - Hc128_Process(&enc, cipher, plain, (word32)test_hc128[i].outLen); - Hc128_Process(&dec, plain, cipher, (word32)test_hc128[i].outLen); + wc_Hc128_Process(&enc, cipher, plain, (word32)test_hc128[i].outLen); + wc_Hc128_Process(&dec, plain, cipher, (word32)test_hc128[i].outLen); if (memcmp(plain, test_hc128[i].input, test_hc128[i].outLen)) return -120 - i; @@ -1843,13 +1840,13 @@ int rabbit_test(void) iv = cipher; } else iv = NULL; - RabbitSetKey(&enc, plain, iv); - RabbitSetKey(&dec, plain, iv); + wc_RabbitSetKey(&enc, plain, iv); + wc_RabbitSetKey(&dec, plain, iv); /* align input */ memcpy(plain, test_rabbit[i].input, test_rabbit[i].outLen); - RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen); - RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen); + wc_RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen); + wc_RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen); if (memcmp(plain, test_rabbit[i].input, test_rabbit[i].outLen)) return -130 - i; @@ -1942,15 +1939,15 @@ int chacha_test(void) XMEMSET(cipher, 0, 32); XMEMCPY(cipher + 4, ivs[i], 8); - Chacha_SetKey(&enc, keys[i], keySz); - Chacha_SetKey(&dec, keys[i], keySz); + wc_Chacha_SetKey(&enc, keys[i], keySz); + wc_Chacha_SetKey(&dec, keys[i], keySz); - Chacha_SetIV(&enc, cipher, 0); - Chacha_SetIV(&dec, cipher, 0); + wc_Chacha_SetIV(&enc, cipher, 0); + wc_Chacha_SetIV(&dec, cipher, 0); XMEMCPY(plain, input, 8); - Chacha_Process(&enc, cipher, plain, (word32)8); - Chacha_Process(&dec, plain, cipher, (word32)8); + wc_Chacha_Process(&enc, cipher, plain, (word32)8); + wc_Chacha_Process(&dec, plain, cipher, (word32)8); if (memcmp(test_chacha[i], cipher, 8)) return -130 - 5 - i; @@ -1998,15 +1995,15 @@ int des_test(void) int ret; - ret = Des_SetKey(&enc, key, iv, DES_ENCRYPTION); + ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION); if (ret != 0) return -31; - Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector)); - ret = Des_SetKey(&dec, key, iv, DES_DECRYPTION); + wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector)); + ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION); if (ret != 0) return -32; - Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher)); + wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher)); if (memcmp(plain, vector, sizeof(plain))) return -33; @@ -2059,21 +2056,21 @@ int des3_test(void) #ifdef HAVE_CAVIUM - if (Des3_InitCavium(&enc, CAVIUM_DEV_ID) != 0) + if (wc_Des3_InitCavium(&enc, CAVIUM_DEV_ID) != 0) return -20005; - if (Des3_InitCavium(&dec, CAVIUM_DEV_ID) != 0) + if (wc_Des3_InitCavium(&dec, CAVIUM_DEV_ID) != 0) return -20006; #endif - ret = Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION); + ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION); if (ret != 0) return -31; - ret = Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION); + ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION); if (ret != 0) return -32; - ret = Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector)); + ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector)); if (ret != 0) return -33; - ret = Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher)); + ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher)); if (ret != 0) return -34; @@ -2084,8 +2081,8 @@ int des3_test(void) return -36; #ifdef HAVE_CAVIUM - Des3_FreeCavium(&enc); - Des3_FreeCavium(&dec); + wc_Des3_FreeCavium(&enc); + wc_Des3_FreeCavium(&dec); #endif return 0; } @@ -2118,22 +2115,22 @@ int aes_test(void) int ret; #ifdef HAVE_CAVIUM - if (AesInitCavium(&enc, CAVIUM_DEV_ID) != 0) + if (wc_AesInitCavium(&enc, CAVIUM_DEV_ID) != 0) return -20003; - if (AesInitCavium(&dec, CAVIUM_DEV_ID) != 0) + if (wc_AesInitCavium(&dec, CAVIUM_DEV_ID) != 0) return -20004; #endif - ret = AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION); + ret = wc_AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION); if (ret != 0) return -1001; - ret = AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION); + ret = wc_AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION); if (ret != 0) return -1002; - ret = AesCbcEncrypt(&enc, cipher, msg, AES_BLOCK_SIZE); + ret = wc_AesCbcEncrypt(&enc, cipher, msg, AES_BLOCK_SIZE); if (ret != 0) return -1005; - ret = AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE); + ret = wc_AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE); if (ret != 0) return -1006; @@ -2144,8 +2141,8 @@ int aes_test(void) return -61; #ifdef HAVE_CAVIUM - AesFreeCavium(&enc); - AesFreeCavium(&dec); + wc_AesFreeCavium(&enc); + wc_AesFreeCavium(&dec); #endif #ifdef WOLFSSL_AES_COUNTER { @@ -2192,12 +2189,12 @@ int aes_test(void) 0xc2 }; - AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION); + wc_AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION); /* Ctr only uses encrypt, even on key setup */ - AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION); + wc_AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION); - AesCtrEncrypt(&enc, cipher, ctrPlain, AES_BLOCK_SIZE*4); - AesCtrEncrypt(&dec, plain, cipher, AES_BLOCK_SIZE*4); + wc_AesCtrEncrypt(&enc, cipher, ctrPlain, AES_BLOCK_SIZE*4); + wc_AesCtrEncrypt(&dec, plain, cipher, AES_BLOCK_SIZE*4); if (memcmp(plain, ctrPlain, AES_BLOCK_SIZE*4)) return -66; @@ -2206,12 +2203,12 @@ int aes_test(void) return -67; /* let's try with just 9 bytes, non block size test */ - AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION); + wc_AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION); /* Ctr only uses encrypt, even on key setup */ - AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION); + wc_AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION); - AesCtrEncrypt(&enc, cipher, ctrPlain, 9); - AesCtrEncrypt(&dec, plain, cipher, 9); + wc_AesCtrEncrypt(&enc, cipher, ctrPlain, 9); + wc_AesCtrEncrypt(&dec, plain, cipher, 9); if (memcmp(plain, ctrPlain, 9)) return -68; @@ -2220,8 +2217,8 @@ int aes_test(void) return -69; /* and an additional 9 bytes to reuse tmp left buffer */ - AesCtrEncrypt(&enc, cipher, ctrPlain, 9); - AesCtrEncrypt(&dec, plain, cipher, 9); + wc_AesCtrEncrypt(&enc, cipher, ctrPlain, 9); + wc_AesCtrEncrypt(&dec, plain, cipher, 9); if (memcmp(plain, ctrPlain, 9)) return -70; @@ -2254,18 +2251,18 @@ int aes_test(void) }; XMEMSET(cipher, 0, AES_BLOCK_SIZE); - ret = AesSetKey(&enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION); + ret = wc_AesSetKey(&enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION); if (ret != 0) return -1003; - AesEncryptDirect(&enc, cipher, niPlain); + wc_AesEncryptDirect(&enc, cipher, niPlain); if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0) return -20006; XMEMSET(plain, 0, AES_BLOCK_SIZE); - ret = AesSetKey(&dec, niKey, sizeof(niKey), plain, AES_DECRYPTION); + ret = wc_AesSetKey(&dec, niKey, sizeof(niKey), plain, AES_DECRYPTION); if (ret != 0) return -1004; - AesDecryptDirect(&dec, plain, niCipher); + wc_AesDecryptDirect(&dec, plain, niCipher); if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0) return -20007; } @@ -2344,15 +2341,15 @@ int poly1305_test(void) const byte* tests[] = {correct, correct2, correct3}; for (i = 0; i < 3; i++) { - ret = Poly1305SetKey(&enc, keys[i], 32); + ret = wc_Poly1305SetKey(&enc, keys[i], 32); if (ret != 0) return -1001; - ret = Poly1305Update(&enc, msgs[i], szm[i]); + ret = wc_Poly1305Update(&enc, msgs[i], szm[i]); if (ret != 0) return -1005; - ret = Poly1305Final(&enc, tag); + ret = wc_Poly1305Final(&enc, tag); if (ret != 0) return -60; @@ -2435,16 +2432,16 @@ int aesgcm_test(void) memset(c2, 0, sizeof(c2)); memset(p2, 0, sizeof(p2)); - AesGcmSetKey(&enc, k, sizeof(k)); + wc_AesGcmSetKey(&enc, k, sizeof(k)); /* AES-GCM encrypt and decrypt both use AES encrypt internally */ - AesGcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv), + wc_AesGcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv), t2, sizeof(t2), a, sizeof(a)); if (memcmp(c, c2, sizeof(c2))) return -68; if (memcmp(t, t2, sizeof(t2))) return -69; - result = AesGcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv), + result = wc_AesGcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv), t2, sizeof(t2), a, sizeof(a)); if (result != 0) return -70; @@ -2523,20 +2520,20 @@ int gmac_test(void) byte tag[16]; memset(tag, 0, sizeof(tag)); - GmacSetKey(&gmac, k1, sizeof(k1)); - GmacUpdate(&gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1)); + wc_GmacSetKey(&gmac, k1, sizeof(k1)); + wc_GmacUpdate(&gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1)); if (memcmp(t1, tag, sizeof(t1)) != 0) return -126; memset(tag, 0, sizeof(tag)); - GmacSetKey(&gmac, k2, sizeof(k2)); - GmacUpdate(&gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2)); + wc_GmacSetKey(&gmac, k2, sizeof(k2)); + wc_GmacUpdate(&gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2)); if (memcmp(t2, tag, sizeof(t2)) != 0) return -127; memset(tag, 0, sizeof(tag)); - GmacSetKey(&gmac, k3, sizeof(k3)); - GmacUpdate(&gmac, iv3, sizeof(iv3), a3, sizeof(a3), tag, sizeof(t3)); + wc_GmacSetKey(&gmac, k3, sizeof(k3)); + wc_GmacUpdate(&gmac, iv3, sizeof(iv3), a3, sizeof(a3), tag, sizeof(t3)); if (memcmp(t3, tag, sizeof(t3)) != 0) return -128; @@ -2598,16 +2595,16 @@ int aesccm_test(void) memset(c2, 0, sizeof(c2)); memset(p2, 0, sizeof(p2)); - AesCcmSetKey(&enc, k, sizeof(k)); + wc_AesCcmSetKey(&enc, k, sizeof(k)); /* AES-CCM encrypt and decrypt both use AES encrypt internally */ - AesCcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv), + wc_AesCcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv), t2, sizeof(t2), a, sizeof(a)); if (memcmp(c, c2, sizeof(c2))) return -107; if (memcmp(t, t2, sizeof(t2))) return -108; - result = AesCcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv), + result = wc_AesCcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv), t2, sizeof(t2), a, sizeof(a)); if (result != 0) return -109; @@ -2616,7 +2613,7 @@ int aesccm_test(void) /* Test the authentication failure */ t2[0]++; /* Corrupt the authentication tag. */ - result = AesCcmDecrypt(&enc, p2, c, sizeof(p2), iv, sizeof(iv), + result = wc_AesCcmDecrypt(&enc, p2, c, sizeof(p2), iv, sizeof(iv), t2, sizeof(t2), a, sizeof(a)); if (result == 0) return -111; @@ -2776,29 +2773,29 @@ int camellia_test(void) testsSz = sizeof(testVectors)/sizeof(test_vector_t); for (i = 0; i < testsSz; i++) { - if (CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz, + if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz, testVectors[i].iv) != 0) return testVectors[i].errorCode; switch (testVectors[i].type) { case CAM_ECB_ENC: - CamelliaEncryptDirect(&cam, out, testVectors[i].plaintext); + wc_CamelliaEncryptDirect(&cam, out, testVectors[i].plaintext); if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; case CAM_ECB_DEC: - CamelliaDecryptDirect(&cam, out, testVectors[i].ciphertext); + wc_CamelliaDecryptDirect(&cam, out, testVectors[i].ciphertext); if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; case CAM_CBC_ENC: - CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext, + wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE); if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; case CAM_CBC_DEC: - CamelliaCbcDecrypt(&cam, out, testVectors[i].ciphertext, + wc_CamelliaCbcDecrypt(&cam, out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE); if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; @@ -2809,25 +2806,25 @@ int camellia_test(void) } /* Setting the IV and checking it was actually set. */ - CamelliaSetIV(&cam, ivc); + wc_CamelliaSetIV(&cam, ivc); if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE)) return -1; /* Setting the IV to NULL should be same as all zeros IV */ - if (CamelliaSetIV(&cam, NULL) != 0 || + if (wc_CamelliaSetIV(&cam, NULL) != 0 || XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE)) return -1; /* First parameter should never be null */ - if (CamelliaSetIV(NULL, NULL) == 0) + if (wc_CamelliaSetIV(NULL, NULL) == 0) return -1; /* First parameter should never be null, check it fails */ - if (CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0) + if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0) return -1; /* Key should have a size of 16, 24, or 32 */ - if (CamelliaSetKey(&cam, k1, 0, NULL) == 0) + if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0) return -1; return 0; @@ -2891,7 +2888,7 @@ int random_test(void) byte output[SHA256_DIGEST_SIZE * 4]; int ret; - ret = RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0, + ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0, output, sizeof(output)); if (ret != 0) return -39; @@ -2899,7 +2896,7 @@ int random_test(void) if (XMEMCMP(test1Output, output, sizeof(output)) != 0) return -40; - ret = RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA), + ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA), test2EntropyB, sizeof(test2EntropyB), output, sizeof(output)); if (ret != 0) @@ -2920,13 +2917,13 @@ int random_test(void) int ret; #ifdef HAVE_CAVIUM - ret = InitRngCavium(&rng, CAVIUM_DEV_ID); + ret = wc_InitRngCavium(&rng, CAVIUM_DEV_ID); if (ret != 0) return -2007; #endif - ret = InitRng(&rng); + ret = wc_InitRng(&rng); if (ret != 0) return -39; - ret = RNG_GenerateBlock(&rng, block, sizeof(block)); + ret = wc_RNG_GenerateBlock(&rng, block, sizeof(block)); if (ret != 0) return -40; return 0; @@ -3055,29 +3052,29 @@ int rsa_test(void) #endif /* USE_CERT_BUFFERS */ #ifdef HAVE_CAVIUM - RsaInitCavium(&key, CAVIUM_DEV_ID); + wc_RsaInitCavium(&key, CAVIUM_DEV_ID); #endif - ret = InitRsaKey(&key, 0); + ret = wc_InitRsaKey(&key, 0); if (ret != 0) return -39; - ret = RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes); + ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes); if (ret != 0) return -41; - ret = InitRng(&rng); + ret = wc_InitRng(&rng); if (ret != 0) return -42; - ret = RsaPublicEncrypt(in, inLen, out, sizeof(out), &key, &rng); + ret = wc_RsaPublicEncrypt(in, inLen, out, sizeof(out), &key, &rng); if (ret < 0) return -43; - ret = RsaPrivateDecrypt(out, ret, plain, sizeof(plain), &key); + ret = wc_RsaPrivateDecrypt(out, ret, plain, sizeof(plain), &key); if (ret < 0) return -44; if (memcmp(plain, in, inLen)) return -45; - ret = RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng); + ret = wc_RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng); if (ret < 0) return -46; memset(plain, 0, sizeof(plain)); - ret = RsaSSL_Verify(out, ret, plain, sizeof(plain), &key); + ret = wc_RsaSSL_Verify(out, ret, plain, sizeof(plain), &key); if (ret < 0) return -47; if (memcmp(plain, in, ret)) return -48; @@ -3128,26 +3125,26 @@ int rsa_test(void) FILE* keyFile; FILE* pemFile; - ret = InitRsaKey(&genKey, 0); + ret = wc_InitRsaKey(&genKey, 0); if (ret != 0) return -300; - ret = MakeRsaKey(&genKey, 1024, 65537, &rng); + ret = wc_MakeRsaKey(&genKey, 1024, 65537, &rng); if (ret != 0) return -301; der = (byte*)malloc(FOURK_BUF); if (der == NULL) { - FreeRsaKey(&genKey); + wc_FreeRsaKey(&genKey); return -307; } pem = (byte*)malloc(FOURK_BUF); if (pem == NULL) { free(der); - FreeRsaKey(&genKey); + wc_FreeRsaKey(&genKey); return -308; } - derSz = RsaKeyToDer(&genKey, der, FOURK_BUF); + derSz = wc_RsaKeyToDer(&genKey, der, FOURK_BUF); if (derSz < 0) { free(der); free(pem); @@ -3158,7 +3155,7 @@ int rsa_test(void) if (!keyFile) { free(der); free(pem); - FreeRsaKey(&genKey); + wc_FreeRsaKey(&genKey); return -303; } ret = (int)fwrite(der, 1, derSz, keyFile); @@ -3166,15 +3163,15 @@ int rsa_test(void) if (ret != derSz) { free(der); free(pem); - FreeRsaKey(&genKey); + wc_FreeRsaKey(&genKey); return -313; } - pemSz = DerToPem(der, derSz, pem, FOURK_BUF, PRIVATEKEY_TYPE); + pemSz = wc_DerToPem(der, derSz, pem, FOURK_BUF, PRIVATEKEY_TYPE); if (pemSz < 0) { free(der); free(pem); - FreeRsaKey(&genKey); + wc_FreeRsaKey(&genKey); return -304; } @@ -3182,7 +3179,7 @@ int rsa_test(void) if (!pemFile) { free(der); free(pem); - FreeRsaKey(&genKey); + wc_FreeRsaKey(&genKey); return -305; } ret = (int)fwrite(pem, 1, pemSz, pemFile); @@ -3190,29 +3187,29 @@ int rsa_test(void) if (ret != pemSz) { free(der); free(pem); - FreeRsaKey(&genKey); + wc_FreeRsaKey(&genKey); return -314; } - ret = InitRsaKey(&derIn, 0); + ret = wc_InitRsaKey(&derIn, 0); if (ret != 0) { free(der); free(pem); - FreeRsaKey(&genKey); + wc_FreeRsaKey(&genKey); return -3060; } idx = 0; - ret = RsaPrivateKeyDecode(der, &idx, &derIn, derSz); + ret = wc_RsaPrivateKeyDecode(der, &idx, &derIn, derSz); if (ret != 0) { free(der); free(pem); - FreeRsaKey(&derIn); - FreeRsaKey(&genKey); + wc_FreeRsaKey(&derIn); + wc_FreeRsaKey(&genKey); return -306; } - FreeRsaKey(&derIn); - FreeRsaKey(&genKey); + wc_FreeRsaKey(&derIn); + wc_FreeRsaKey(&genKey); free(pem); free(der); } @@ -3345,13 +3342,13 @@ int rsa_test(void) bytes3 = fread(tmp, 1, FOURK_BUF, file3); fclose(file3); - ret = InitRsaKey(&caKey, 0); + ret = wc_InitRsaKey(&caKey, 0); if (ret != 0) { free(derCert); free(pem); return -411; } - ret = RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3); + ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3); if (ret != 0) { free(derCert); free(pem); @@ -3488,7 +3485,7 @@ int rsa_test(void) bytes3 = fread(tmp, 1, FOURK_BUF, file3); fclose(file3); - ecc_init(&caKey); + wc_ecc_init(&caKey); ret = EccPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3); if (ret != 0) { free(derCert); @@ -3585,7 +3582,7 @@ int rsa_test(void) fclose(pemFile); free(pem); free(derCert); - ecc_free(&caKey); + wc_ecc_free(&caKey); } #endif /* HAVE_ECC */ #ifdef HAVE_NTRU @@ -3867,14 +3864,14 @@ int rsa_test(void) #endif /* WOLFSSL_CERT_REQ */ #endif /* WOLFSSL_CERT_GEN */ - FreeRsaKey(&key); + wc_FreeRsaKey(&key); #ifdef HAVE_CAVIUM - RsaFreeCavium(&key); + wc_RsaFreeCavium(&key); #endif free(tmp); #if defined(HAVE_HASHDRBG) || defined(NO_RC4) - FreeRng(&rng); + wc_FreeRng(&rng); #endif return 0; @@ -3926,39 +3923,39 @@ int dh_test(void) fclose(file); #endif /* USE_CERT_BUFFERS */ - InitDhKey(&key); - InitDhKey(&key2); - ret = DhKeyDecode(tmp, &idx, &key, bytes); + wc_InitDhKey(&key); + wc_InitDhKey(&key2); + ret = wc_DhKeyDecode(tmp, &idx, &key, bytes); if (ret != 0) return -51; idx = 0; - ret = DhKeyDecode(tmp, &idx, &key2, bytes); + ret = wc_DhKeyDecode(tmp, &idx, &key2, bytes); if (ret != 0) return -52; - ret = InitRng(&rng); + ret = wc_InitRng(&rng); if (ret != 0) return -53; - ret = DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz); - ret += DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2); + ret = wc_DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz); + ret += wc_DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2); if (ret != 0) return -54; - ret = DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2); - ret += DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz); + ret = wc_DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2); + ret += wc_DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz); if (ret != 0) return -55; if (memcmp(agree, agree2, agreeSz)) return -56; - FreeDhKey(&key); - FreeDhKey(&key2); + wc_FreeDhKey(&key); + wc_FreeDhKey(&key2); #if defined(HAVE_HASHDRBG) || defined(NO_RC4) - FreeRng(&rng); + wc_FreeRng(&rng); #endif return 0; @@ -4006,27 +4003,27 @@ int dsa_test(void) fclose(file); #endif /* USE_CERT_BUFFERS */ - ret = InitSha(&sha); + ret = wc_InitSha(&sha); if (ret != 0) return -4002; - ShaUpdate(&sha, tmp, bytes); - ShaFinal(&sha, hash); + wc_ShaUpdate(&sha, tmp, bytes); + wc_ShaFinal(&sha, hash); - InitDsaKey(&key); - ret = DsaPrivateKeyDecode(tmp, &idx, &key, bytes); + wc_InitDsaKey(&key); + ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes); if (ret != 0) return -61; - ret = InitRng(&rng); + ret = wc_InitRng(&rng); if (ret != 0) return -62; - ret = DsaSign(hash, signature, &key, &rng); + ret = wc_DsaSign(hash, signature, &key, &rng); if (ret != 0) return -63; - ret = DsaVerify(hash, signature, &key, &answer); + ret = wc_DsaVerify(hash, signature, &key, &answer); if (ret != 0) return -64; if (answer != 1) return -65; - FreeDsaKey(&key); + wc_FreeDsaKey(&key); return 0; } @@ -4284,7 +4281,7 @@ int pkcs12_test(void) int id = 1; int kLen = 24; int iterations = 1; - int ret = PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8, iterations, + int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8, iterations, kLen, SHA, id); if (ret < 0) @@ -4294,7 +4291,7 @@ int pkcs12_test(void) return -104; iterations = 1000; - ret = PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8, iterations, + ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8, iterations, kLen, SHA, id); if (ret < 0) return -105; @@ -4320,7 +4317,7 @@ int pbkdf2_test(void) }; - int ret = PBKDF2(derived, (byte*)passwd, (int)strlen(passwd), salt, 8, + int ret = wc_PBKDF2(derived, (byte*)passwd, (int)strlen(passwd), salt, 8, iterations, kLen, SHA); if (ret != 0) return ret; @@ -4345,7 +4342,7 @@ int pbkdf1_test(void) 0x4A, 0x3D, 0x2A, 0x20 }; - PBKDF1(derived, (byte*)passwd, (int)strlen(passwd), salt, 8, iterations, + wc_PBKDF1(derived, (byte*)passwd, (int)strlen(passwd), salt, 8, iterations, kLen, SHA); if (memcmp(derived, verify, sizeof(verify)) != 0) @@ -4484,28 +4481,28 @@ int ecc_test(void) if (ret != 0) return -1001; - ecc_init(&userA); - ecc_init(&userB); - ecc_init(&pubKey); + wc_ecc_init(&userA); + wc_ecc_init(&userB); + wc_ecc_init(&pubKey); - ret = ecc_make_key(&rng, 32, &userA); + ret = wc_ecc_make_key(&rng, 32, &userA); if (ret != 0) return -1014; - ret = ecc_make_key(&rng, 32, &userB); + ret = wc_ecc_make_key(&rng, 32, &userB); if (ret != 0) return -1002; x = sizeof(sharedA); - ret = ecc_shared_secret(&userA, &userB, sharedA, &x); + ret = wc_ecc_shared_secret(&userA, &userB, sharedA, &x); if (ret != 0) return -1015; y = sizeof(sharedB); - ret = ecc_shared_secret(&userB, &userA, sharedB, &y); + ret = wc_ecc_shared_secret(&userB, &userA, sharedB, &y); if (ret != 0) return -1003; @@ -4517,17 +4514,17 @@ int ecc_test(void) return -1005; x = sizeof(exportBuf); - ret = ecc_export_x963(&userA, exportBuf, &x); + ret = wc_ecc_export_x963(&userA, exportBuf, &x); if (ret != 0) return -1006; - ret = ecc_import_x963(exportBuf, x, &pubKey); + ret = wc_ecc_import_x963(exportBuf, x, &pubKey); if (ret != 0) return -1007; y = sizeof(sharedB); - ret = ecc_shared_secret(&userB, &pubKey, sharedB, &y); + ret = wc_ecc_shared_secret(&userB, &pubKey, sharedB, &y); if (ret != 0) return -1008; @@ -4538,20 +4535,20 @@ int ecc_test(void) #ifdef HAVE_COMP_KEY /* try compressed export / import too */ x = sizeof(exportBuf); - ret = ecc_export_x963_ex(&userA, exportBuf, &x, 1); + ret = wc_ecc_export_x963_ex(&userA, exportBuf, &x, 1); if (ret != 0) return -1010; - ecc_free(&pubKey); - ecc_init(&pubKey); - ret = ecc_import_x963(exportBuf, x, &pubKey); + wc_ecc_free(&pubKey); + wc_ecc_init(&pubKey); + ret = wc_ecc_import_x963(exportBuf, x, &pubKey); if (ret != 0) return -1011; #endif y = sizeof(sharedB); - ret = ecc_shared_secret(&userB, &pubKey, sharedB, &y); + ret = wc_ecc_shared_secret(&userB, &pubKey, sharedB, &y); if (ret != 0) return -1012; @@ -4564,13 +4561,13 @@ int ecc_test(void) digest[i] = (byte)i; x = sizeof(sig); - ret = ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &userA); + ret = wc_ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &userA); if (ret != 0) return -1014; verify = 0; - ret = ecc_verify_hash(sig, x, digest, sizeof(digest), &verify, &userA); + ret = wc_ecc_verify_hash(sig, x, digest, sizeof(digest), &verify, &userA); if (ret != 0) return -1015; @@ -4579,7 +4576,7 @@ int ecc_test(void) return -1016; x = sizeof(exportBuf); - ret = ecc_export_private_only(&userA, exportBuf, &x); + ret = wc_ecc_export_private_only(&userA, exportBuf, &x); if (ret != 0) return -1017; @@ -4634,30 +4631,30 @@ int ecc_test(void) for (i = 0; i < times; i++) { - ecc_free(&userA); - ecc_init(&userA); + wc_ecc_free(&userA); + wc_ecc_init(&userA); memset(sig, 0, sizeof(sig)); x = sizeof(sig); /* calculate SHA-1 hash of message */ - ret = InitSha(&sha); + ret = wc_InitSha(&sha); if (ret != 0) return -1015 - i; - ShaUpdate(&sha, (byte*)test_ecc[i].msg, (word32)test_ecc[i].msgLen); - ShaFinal(&sha, hash); + wc_ShaUpdate(&sha, (byte*)test_ecc[i].msg, (word32)test_ecc[i].msgLen); + wc_ShaFinal(&sha, hash); - ret = ecc_import_raw(&userA, test_ecc[i].Qx, test_ecc[i].Qy, + ret = wc_ecc_import_raw(&userA, test_ecc[i].Qx, test_ecc[i].Qy, test_ecc[i].d, test_ecc[i].curveName); if (ret != 0) return -1017 - i; - ret = ecc_rs_to_sig(test_ecc[i].R, test_ecc[i].S, sig, &x); + ret = wc_ecc_rs_to_sig(test_ecc[i].R, test_ecc[i].S, sig, &x); if (ret != 0) return -1019 - i; - ret = ecc_verify_hash(sig, x, hash, sizeof(hash), &verify, &userA); + ret = wc_ecc_verify_hash(sig, x, hash, sizeof(hash), &verify, &userA); if (ret != 0) return -1021 - i; @@ -4707,12 +4704,12 @@ int ecc_test(void) } #endif /* WOLFSSL_KEY_GEN */ - ecc_free(&pubKey); - ecc_free(&userB); - ecc_free(&userA); + wc_ecc_free(&pubKey); + wc_ecc_free(&userB); + wc_ecc_free(&userA); #if defined(HAVE_HASHDRBG) || defined(NO_RC4) - FreeRng(&rng); + wc_FreeRng(&rng); #endif return 0; @@ -4732,15 +4729,15 @@ int ecc_encrypt_test(void) word32 plainSz = sizeof(plain); int i; - ret = InitRng(&rng); + ret = wc_InitRng(&rng); if (ret != 0) return -3001; - ecc_init(&userA); - ecc_init(&userB); + wc_ecc_init(&userA); + wc_ecc_init(&userB); - ret = ecc_make_key(&rng, 32, &userA); - ret += ecc_make_key(&rng, 32, &userB); + ret = wc_ecc_make_key(&rng, 32, &userA); + ret += wc_ecc_make_key(&rng, 32, &userB); if (ret != 0) return -3002; @@ -4749,12 +4746,12 @@ int ecc_encrypt_test(void) msg[i] = i; /* encrypt msg to B */ - ret = ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz, NULL); + ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz, NULL); if (ret != 0) return -3003; /* decrypt msg from A */ - ret = ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, NULL); + ret = wc_ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, NULL); if (ret != 0) return -3004; @@ -4763,8 +4760,8 @@ int ecc_encrypt_test(void) { /* let's verify message exchange works, A is client, B is server */ - ecEncCtx* cliCtx = ecc_ctx_new(REQ_RESP_CLIENT, &rng); - ecEncCtx* srvCtx = ecc_ctx_new(REQ_RESP_SERVER, &rng); + ecEncCtx* cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng); + ecEncCtx* srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng); byte cliSalt[EXCHANGE_SALT_SZ]; byte srvSalt[EXCHANGE_SALT_SZ]; @@ -4774,35 +4771,35 @@ int ecc_encrypt_test(void) return -3006; /* get salt to send to peer */ - tmpSalt = ecc_ctx_get_own_salt(cliCtx); + tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx); if (tmpSalt == NULL) return -3007; memcpy(cliSalt, tmpSalt, EXCHANGE_SALT_SZ); - tmpSalt = ecc_ctx_get_own_salt(srvCtx); + tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx); if (tmpSalt == NULL) return -3007; memcpy(srvSalt, tmpSalt, EXCHANGE_SALT_SZ); /* in actual use, we'd get the peer's salt over the transport */ - ret = ecc_ctx_set_peer_salt(cliCtx, srvSalt); - ret += ecc_ctx_set_peer_salt(srvCtx, cliSalt); + ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt); + ret += wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt); - ret += ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11); - ret += ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11); + ret += wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11); + ret += wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11); if (ret != 0) return -3008; /* get encrypted msg (request) to send to B */ outSz = sizeof(out); - ret = ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz,cliCtx); + ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz,cliCtx); if (ret != 0) return -3009; /* B decrypts msg (request) from A */ plainSz = sizeof(plain); - ret = ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, srvCtx); + ret = wc_ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, srvCtx); if (ret != 0) return -3010; @@ -4821,13 +4818,13 @@ int ecc_encrypt_test(void) msg2[i] = i+48; /* get encrypted msg (response) to send to B */ - ret = ecc_encrypt(&userB, &userA, msg2, sizeof(msg2), out2, + ret = wc_ecc_encrypt(&userB, &userA, msg2, sizeof(msg2), out2, &outSz2, srvCtx); if (ret != 0) return -3012; /* A decrypts msg (response) from B */ - ret = ecc_decrypt(&userA, &userB, out2, outSz2, plain2, &plainSz2, + ret = wc_ecc_decrypt(&userA, &userB, out2, outSz2, plain2, &plainSz2, cliCtx); if (ret != 0) return -3013; @@ -4837,13 +4834,13 @@ int ecc_encrypt_test(void) } /* cleanup */ - ecc_ctx_free(srvCtx); - ecc_ctx_free(cliCtx); + wc_ecc_ctx_free(srvCtx); + wc_ecc_ctx_free(cliCtx); } /* cleanup */ - ecc_free(&userB); - ecc_free(&userA); + wc_ecc_free(&userB); + wc_ecc_free(&userA); return 0; } @@ -4945,7 +4942,7 @@ int compress_test(void) if (c == NULL || d == NULL) ret = -300; - if (ret == 0 && (ret = Compress(c, cSz, sample_text, dSz, 0)) < 0) + if (ret == 0 && (ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) ret = -301; if (ret > 0) { @@ -4953,7 +4950,7 @@ int compress_test(void) ret = 0; } - if (ret == 0 && DeCompress(d, dSz, c, cSz) != (int)dSz) + if (ret == 0 && wc_DeCompress(d, dSz, c, cSz) != (int)dSz) ret = -302; if (ret == 0 && memcmp(d, sample_text, dSz)) @@ -5028,7 +5025,7 @@ int pkcs7enveloped_test(void) privKeySz = fread(privKey, 1, FOURK_BUF, keyFile); fclose(keyFile); - PKCS7_InitWithCert(&pkcs7, cert, (word32)certSz); + wc_PKCS7_InitWithCert(&pkcs7, cert, (word32)certSz); pkcs7.content = (byte*)data; pkcs7.contentSz = (word32)sizeof(data); pkcs7.contentOID = DATA; @@ -5037,7 +5034,7 @@ int pkcs7enveloped_test(void) pkcs7.privateKeySz = (word32)privKeySz; /* encode envelopedData */ - envelopedSz = PKCS7_EncodeEnvelopedData(&pkcs7, enveloped, + envelopedSz = wc_PKCS7_EncodeEnvelopedData(&pkcs7, enveloped, sizeof(enveloped)); if (envelopedSz <= 0) { free(cert); @@ -5046,7 +5043,7 @@ int pkcs7enveloped_test(void) } /* decode envelopedData */ - decodedSz = PKCS7_DecodeEnvelopedData(&pkcs7, enveloped, envelopedSz, + decodedSz = wc_PKCS7_DecodeEnvelopedData(&pkcs7, enveloped, envelopedSz, decoded, sizeof(decoded)); if (decodedSz <= 0) { free(cert); @@ -5074,7 +5071,7 @@ int pkcs7enveloped_test(void) free(cert); free(privKey); - PKCS7_Free(&pkcs7); + wc_PKCS7_Free(&pkcs7); if (ret > 0) return 0; @@ -5161,7 +5158,7 @@ int pkcs7signed_test(void) keyDerSz = (word32)fread(keyDer, 1, FOURK_BUF, file); fclose(file); - ret = InitRng(&rng); + ret = wc_InitRng(&rng); if (ret != 0) { free(certDer); free(keyDer); @@ -5172,7 +5169,7 @@ int pkcs7signed_test(void) senderNonce[0] = 0x04; senderNonce[1] = PKCS7_NONCE_SZ; - ret = RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ); + ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ); if (ret != 0) { free(certDer); free(keyDer); @@ -5180,7 +5177,7 @@ int pkcs7signed_test(void) return -211; } - PKCS7_InitWithCert(&msg, certDer, certDerSz); + wc_PKCS7_InitWithCert(&msg, certDer, certDerSz); msg.privateKey = keyDer; msg.privateKeySz = keyDerSz; msg.content = (byte*)data; @@ -5198,26 +5195,26 @@ int pkcs7signed_test(void) transId[0] = 0x13; transId[1] = SHA_DIGEST_SIZE * 2; - ret = InitSha(&sha); + ret = wc_InitSha(&sha); if (ret != 0) { free(certDer); free(keyDer); free(out); return -4003; } - ShaUpdate(&sha, msg.publicKey, msg.publicKeySz); - ShaFinal(&sha, digest); + wc_ShaUpdate(&sha, msg.publicKey, msg.publicKeySz); + wc_ShaFinal(&sha, digest); for (i = 0, j = 2; i < SHA_DIGEST_SIZE; i++, j += 2) { snprintf((char*)&transId[j], 3, "%02x", digest[i]); } } - ret = PKCS7_EncodeSignedData(&msg, out, outSz); + ret = wc_PKCS7_EncodeSignedData(&msg, out, outSz); if (ret < 0) { free(certDer); free(keyDer); free(out); - PKCS7_Free(&msg); + wc_PKCS7_Free(&msg); return -212; } else @@ -5229,7 +5226,7 @@ int pkcs7signed_test(void) free(certDer); free(keyDer); free(out); - PKCS7_Free(&msg); + wc_PKCS7_Free(&msg); return -213; } ret = (int)fwrite(out, 1, outSz, file); @@ -5238,19 +5235,19 @@ int pkcs7signed_test(void) free(certDer); free(keyDer); free(out); - PKCS7_Free(&msg); + wc_PKCS7_Free(&msg); return -218; } - PKCS7_Free(&msg); - PKCS7_InitWithCert(&msg, NULL, 0); + wc_PKCS7_Free(&msg); + wc_PKCS7_InitWithCert(&msg, NULL, 0); - ret = PKCS7_VerifySignedData(&msg, out, outSz); + ret = wc_PKCS7_VerifySignedData(&msg, out, outSz); if (ret < 0) { free(certDer); free(keyDer); free(out); - PKCS7_Free(&msg); + wc_PKCS7_Free(&msg); return -214; } @@ -5258,7 +5255,7 @@ int pkcs7signed_test(void) free(certDer); free(keyDer); free(out); - PKCS7_Free(&msg); + wc_PKCS7_Free(&msg); return -215; } @@ -5267,7 +5264,7 @@ int pkcs7signed_test(void) free(certDer); free(keyDer); free(out); - PKCS7_Free(&msg); + wc_PKCS7_Free(&msg); return -216; } ret = (int)fwrite(msg.singleCert, 1, msg.singleCertSz, file); @@ -5276,10 +5273,10 @@ int pkcs7signed_test(void) free(certDer); free(keyDer); free(out); - PKCS7_Free(&msg); + wc_PKCS7_Free(&msg); #if defined(HAVE_HASHDRBG) || defined(NO_RC4) - FreeRng(&rng); + wc_FreeRng(&rng); #endif if (ret > 0) diff --git a/wolfssl/wolfcrypt/aes.h b/wolfssl/wolfcrypt/aes.h index 5aecc100d..ea276c576 100644 --- a/wolfssl/wolfcrypt/aes.h +++ b/wolfssl/wolfcrypt/aes.h @@ -106,16 +106,16 @@ word32 sz, const byte* iv, word32 ivSz, const byte* authTag, word32 authTagSz, const byte* authIn, word32 authInSz); - #ifndef FIPS_NO_WRAPPERS - /* if not impl or fips.c impl wrapper force fips calls if fips build */ - #define AesSetKey AesSetKey_fips - #define AesSetIV AesSetIV_fips - #define AesCbcEncrypt AesCbcEncrypt_fips - #define AesCbcDecrypt AesCbcDecrypt_fips - #define AesGcmSetKey AesGcmSetKey_fips - #define AesGcmEncrypt AesGcmEncrypt_fips - #define AesGcmDecrypt AesGcmDecrypt_fips - #endif /* FIPS_NO_WRAPPERS */ +// #ifndef FIPS_NO_WRAPPERS +// /* if not impl or fips.c impl wrapper force fips calls if fips build */ +// #define AesSetKey AesSetKey_fips +// #define AesSetIV AesSetIV_fips +// #define AesCbcEncrypt AesCbcEncrypt_fips +// #define AesCbcDecrypt AesCbcDecrypt_fips +// #define AesGcmSetKey AesGcmSetKey_fips +// #define AesGcmEncrypt AesGcmEncrypt_fips +// #define AesGcmDecrypt AesGcmDecrypt_fips +// #endif /* FIPS_NO_WRAPPERS */ #endif /* HAVE_FIPS */ diff --git a/wolfssl/wolfcrypt/asn.h b/wolfssl/wolfcrypt/asn.h index fe45c3693..10316fa53 100644 --- a/wolfssl/wolfcrypt/asn.h +++ b/wolfssl/wolfcrypt/asn.h @@ -2,14 +2,14 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -21,18 +21,18 @@ #ifndef NO_ASN -#ifndef CTAO_CRYPT_ASN_H -#define CTAO_CRYPT_ASN_H +#ifndef WOLF_CRYPT_ASN_H +#define WOLF_CRYPT_ASN_H -#include -#include -#include -#include -#include -#include -#include /* public interface */ +#include +#include +#include +#include +#include +#include +#include /* public interface */ #ifdef HAVE_ECC - #include + #include #endif #ifdef __cplusplus diff --git a/wolfssl/wolfcrypt/compress.h b/wolfssl/wolfcrypt/compress.h index ecf162204..91d9c4a81 100644 --- a/wolfssl/wolfcrypt/compress.h +++ b/wolfssl/wolfcrypt/compress.h @@ -2,14 +2,14 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -22,11 +22,11 @@ #ifdef HAVE_LIBZ -#ifndef CTAO_CRYPT_COMPRESS_H -#define CTAO_CRYPT_COMPRESS_H +#ifndef WOLF_CRYPT_COMPRESS_H +#define WOLF_CRYPT_COMPRESS_H -#include +#include #ifdef __cplusplus @@ -37,8 +37,8 @@ #define COMPRESS_FIXED 1 -CYASSL_API int Compress(byte*, word32, const byte*, word32, word32); -CYASSL_API int DeCompress(byte*, word32, const byte*, word32); +WOLFSSL_API int wc_Compress(byte*, word32, const byte*, word32, word32); +WOLFSSL_API int wc_DeCompress(byte*, word32, const byte*, word32); #ifdef __cplusplus @@ -46,7 +46,7 @@ CYASSL_API int DeCompress(byte*, word32, const byte*, word32); #endif -#endif /* CTAO_CRYPT_COMPRESS_H */ +#endif /* WOLF_CRYPT_COMPRESS_H */ #endif /* HAVE_LIBZ */ diff --git a/wolfssl/wolfcrypt/dsa.h b/wolfssl/wolfcrypt/dsa.h index 424cc648b..269af6ec9 100644 --- a/wolfssl/wolfcrypt/dsa.h +++ b/wolfssl/wolfcrypt/dsa.h @@ -29,6 +29,14 @@ #include #include +/* for DSA reverse compatibility */ +#define InitDsaKey wc_InitDsaKey +#define FreeDsaKey wc_FreeDsaKey +#define DsaSign wc_DsaSign +#define DsaVerify wc_DsaVerify +#define DsaPublicKeyDecode wc_DsaPublicKeyDecode +#define DsaPrivateKeyDecode wc_DsaPrivateKeyDecode + #ifdef __cplusplus extern "C" { #endif diff --git a/wolfssl/wolfcrypt/error-crypt.h b/wolfssl/wolfcrypt/error-crypt.h index 1c5dec1be..49ba8ae21 100644 --- a/wolfssl/wolfcrypt/error-crypt.h +++ b/wolfssl/wolfcrypt/error-crypt.h @@ -2,14 +2,14 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -23,8 +23,11 @@ #ifndef WOLF_CRYPT_ERROR_H #define WOLF_CRYPT_ERROR_H +/* compatibility and fips @wc_fips */ +#ifndef HAVE_FIPS #include - +#define CTaoCryptErrorString wc_CryptErrorString +#define CTaoCryptGetErrorString wc_CryptGetErrorString #ifdef __cplusplus extern "C" { @@ -150,7 +153,11 @@ WOLFSSL_API const char* wc_GetErrorString(int error); #ifdef __cplusplus } /* extern "C" */ #endif - -#endif /* CTAO_CRYPT_ERROR_H */ +#else + #include + #define wc_ErrorString CTaoCryptErrorString + #define wc_GetErrorString CTaoCryptGetErrorString +#endif /* HAVE_FIPS */ +#endif /* WOLF_CRYPT_ERROR_H */ diff --git a/wolfssl/wolfcrypt/hmac.h b/wolfssl/wolfcrypt/hmac.h index 6aae0670d..64f6f69f9 100644 --- a/wolfssl/wolfcrypt/hmac.h +++ b/wolfssl/wolfcrypt/hmac.h @@ -27,9 +27,6 @@ /* for fips */ #include -#if defined(WOLFSSL_SHA512) && !defined(CYASSL_SHA512) - #define CYASSL_SHA512 -#endif #ifdef HAVE_CAVIUM diff --git a/wolfssl/wolfcrypt/logging.h b/wolfssl/wolfcrypt/logging.h index a361e8cca..cb0126cef 100644 --- a/wolfssl/wolfcrypt/logging.h +++ b/wolfssl/wolfcrypt/logging.h @@ -2,14 +2,14 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -22,49 +22,67 @@ /* submitted by eof */ -#ifndef CYASSL_LOGGING_H -#define CYASSL_LOGGING_H +#ifndef WOLFSSL_LOGGING_H +#define WOLFSSL_LOGGING_H +/* for reverse compatibility @wc_fips */ +#ifndef HAVE_FIPS + #include + #define CYASSL_LEAVE WOLFSSL_LEAVE + #define CYASSL_ERROR WOLFSSL_ERROR + #define CYASSL_ENTER WOLFSSL_ENTER + #define CYASSL_MSG WOLFSSL_MSG -#ifdef __cplusplus - extern "C" { + /* check old macros possibly declared */ + #if defined(CYASSL_DEBUG) && !defined(DEBUG_WOLFSSL) + #define DEBUG_WOLFSSL + #endif + + #ifdef __cplusplus + extern "C" { + #endif + + + enum CYA_Log_Levels { + ERROR_LOG = 0, + INFO_LOG, + ENTER_LOG, + LEAVE_LOG, + OTHER_LOG + }; + + typedef void (*wolfSSL_Logging_cb)(const int logLevel, + const char *const logMessage); + + WOLFSSL_API int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb log_function); + + #ifdef DEBUG_WOLFSSL + + void WOLFSSL_ENTER(const char* msg); + void WOLFSSL_LEAVE(const char* msg, int ret); + + void WOLFSSL_ERROR(int); + void WOLFSSL_MSG(const char* msg); + + #else /* DEBUG_WOLFSSL */ + + #define WOLFSSL_ENTER(m) + #define WOLFSSL_LEAVE(m, r) + + #define WOLFSSL_ERROR(e) + #define WOLFSSL_MSG(m) + + #endif /* DEBUG_WOLFSSL */ + + #ifdef __cplusplus + } + #endif +#else /* if using fips use old logging file */ + #include + #define WOLFSSL_LEAVE CYASSL_LEAVE + #define WOLFSSL_ERROR CYASSL_ERROR + #define WOLFSSL_ENTER CYASSL_ENTER + #define WOLFSSL_MSG CYASSL_MSG #endif +#endif /* WOLFSSL_MEMORY_H */ - -enum CYA_Log_Levels { - ERROR_LOG = 0, - INFO_LOG, - ENTER_LOG, - LEAVE_LOG, - OTHER_LOG -}; - -typedef void (*CyaSSL_Logging_cb)(const int logLevel, - const char *const logMessage); - -CYASSL_API int CyaSSL_SetLoggingCb(CyaSSL_Logging_cb log_function); - - -#ifdef DEBUG_CYASSL - - void CYASSL_ENTER(const char* msg); - void CYASSL_LEAVE(const char* msg, int ret); - - void CYASSL_ERROR(int); - void CYASSL_MSG(const char* msg); - -#else /* DEBUG_CYASSL */ - - #define CYASSL_ENTER(m) - #define CYASSL_LEAVE(m, r) - - #define CYASSL_ERROR(e) - #define CYASSL_MSG(m) - -#endif /* DEBUG_CYASSL */ - -#ifdef __cplusplus -} -#endif - -#endif /* CYASSL_MEMORY_H */ diff --git a/wolfssl/wolfcrypt/md2.h b/wolfssl/wolfcrypt/md2.h index af8882c96..e7e211d97 100644 --- a/wolfssl/wolfcrypt/md2.h +++ b/wolfssl/wolfcrypt/md2.h @@ -2,14 +2,14 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -19,15 +19,29 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ +/* check for old macro */ +#if defined(CYASSL_MD2) && !defined(WOLFSSL_MD2) + #define WOLFSSL_MD2 +#endif -//#ifdef WOLFSSL_MD2 -//@TODO +#ifdef WOLFSSL_MD2 #ifndef WOLF_CRYPT_MD2_H #define WOLF_CRYPT_MD2_H #include + +/* for md2 reverse compatibility */ +#ifdef WOLFSSL_MD2 + #define InitMd2 wc_InitMd2 + #define Md2Update wc_Md2Update + #define Md2Final wc_Md2Final + #define Md2Hash wc_Md2Hash +#endif + + + #ifdef __cplusplus extern "C" { #endif @@ -62,6 +76,6 @@ WOLFSSL_API int wc_Md2Hash(const byte*, word32, byte*); } /* extern "C" */ #endif -#endif /* CTAO_CRYPT_MD2_H */ -//@TODO -//#endif /* CYASSL_MD2 */ +#endif /* WOLF_CRYPT_MD2_H */ +#endif /* WOLFSSL_MD2 */ + diff --git a/wolfssl/wolfcrypt/md4.h b/wolfssl/wolfcrypt/md4.h index eb5ebb5a4..75a8976ca 100644 --- a/wolfssl/wolfcrypt/md4.h +++ b/wolfssl/wolfcrypt/md4.h @@ -2,14 +2,14 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -22,10 +22,17 @@ #ifndef NO_MD4 -#ifndef CTAO_CRYPT_MD4_H -#define CTAO_CRYPT_MD4_H +#ifndef WOLF_CRYPT_MD4_H +#define WOLF_CRYPT_MD4_H -#include +#include + +/* for md4 reverse compatibility */ +#ifndef NO_MD4 + #define InitMd4 wc_InitMd4 + #define Md4Update wc_Md4Update + #define Md4Final wc_Md4Final +#endif #ifdef __cplusplus extern "C" { @@ -50,16 +57,16 @@ typedef struct Md4 { } Md4; -CYASSL_API void InitMd4(Md4*); -CYASSL_API void Md4Update(Md4*, const byte*, word32); -CYASSL_API void Md4Final(Md4*, byte*); +WOLFSSL_API void wc_InitMd4(Md4*); +WOLFSSL_API void wc_Md4Update(Md4*, const byte*, word32); +WOLFSSL_API void wc_Md4Final(Md4*, byte*); #ifdef __cplusplus } /* extern "C" */ #endif -#endif /* CTAO_CRYPT_MD4_H */ +#endif /* WOLF_CRYPT_MD4_H */ #endif /* NO_MD4 */ diff --git a/wolfssl/wolfcrypt/md5.h b/wolfssl/wolfcrypt/md5.h index 8b5f0e9cc..9f82748c9 100644 --- a/wolfssl/wolfcrypt/md5.h +++ b/wolfssl/wolfcrypt/md5.h @@ -2,14 +2,14 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -26,41 +26,47 @@ #include -#include +/* for md5 reverse compatibility */ +#ifndef NO_MD5 + #define InitMd5 wc_InitMd5 + #define Md5Update wc_Md5Update + #define Md5Final wc_Md5Final + #define Md5Hash wc_Md5Hash +#endif #ifdef __cplusplus extern "C" { #endif -///* in bytes */ -//enum { -//#ifdef STM32F2_HASH -// MD5_REG_SIZE = 4, /* STM32 register size, bytes */ -//#endif -// MD5 = 0, /* hash type unique */ -// MD5_BLOCK_SIZE = 64, -// MD5_DIGEST_SIZE = 16, -// MD5_PAD_SIZE = 56 -//}; -// -//#ifdef CYASSL_PIC32MZ_HASH -//#include "port/pic32/pic32mz-crypt.h" -//#endif -// -///* MD5 digest */ -//typedef struct Md5 { -// word32 buffLen; /* in bytes */ -// word32 loLen; /* length in bytes */ -// word32 hiLen; /* length in bytes */ -// word32 buffer[MD5_BLOCK_SIZE / sizeof(word32)]; -// #ifndef CYASSL_PIC32MZ_HASH -// word32 digest[MD5_DIGEST_SIZE / sizeof(word32)]; -// #else -// word32 digest[PIC32_HASH_SIZE / sizeof(word32)]; -// pic32mz_desc desc ; /* Crypt Engine descripter */ -// #endif -//} Md5; +/* in bytes */ +enum { +#ifdef STM32F2_HASH + MD5_REG_SIZE = 4, /* STM32 register size, bytes */ +#endif + MD5 = 0, /* hash type unique */ + MD5_BLOCK_SIZE = 64, + MD5_DIGEST_SIZE = 16, + MD5_PAD_SIZE = 56 +}; + +#ifdef CYASSL_PIC32MZ_HASH +#include "port/pic32/pic32mz-crypt.h" +#endif + +/* MD5 digest */ +typedef struct Md5 { + word32 buffLen; /* in bytes */ + word32 loLen; /* length in bytes */ + word32 hiLen; /* length in bytes */ + word32 buffer[MD5_BLOCK_SIZE / sizeof(word32)]; + #ifndef CYASSL_PIC32MZ_HASH + word32 digest[MD5_DIGEST_SIZE / sizeof(word32)]; + #else + word32 digest[PIC32_HASH_SIZE / sizeof(word32)]; + pic32mz_desc desc ; /* Crypt Engine descripter */ + #endif +} Md5; WOLFSSL_API void wc_InitMd5(Md5*); WOLFSSL_API void wc_Md5Update(Md5*, const byte*, word32); @@ -72,5 +78,5 @@ WOLFSSL_API int wc_Md5Hash(const byte*, word32, byte*); } /* extern "C" */ #endif -#endif /* CTAO_CRYPT_MD5_H */ +#endif /* WOLF_CRYPT_MD5_H */ #endif /* NO_MD5 */ diff --git a/wolfssl/wolfcrypt/memory.h b/wolfssl/wolfcrypt/memory.h index 706a4046d..8b7dcd86b 100644 --- a/wolfssl/wolfcrypt/memory.h +++ b/wolfssl/wolfcrypt/memory.h @@ -27,29 +27,47 @@ #include -#ifdef __cplusplus - extern "C" { -#endif +/* compatibility and fips @wc_fips */ +#ifndef HAVE_FIPS + #include + #define CyaSSL_Malloc_cb wolfSSL_Malloc_cb + #define CyaSSL_Free_cb wolfSSL_Free_cb + #define CyaSSL_Realloc_cb wolfSSL_Realloc_cb + #define CyaSSL_SetAllocators wolfSSL_SetAllocators + + /* Public in case user app wants to use XMALLOC/XFREE */ + #define CyaSSL_Malloc wolfSSL_Malloc + #define CyaSSL_Free wolfSSL_Free + #define CyaSSL_Realloc wolfSSL_Realloc -typedef void *(*wolfSSL_Malloc_cb)(size_t size); -typedef void (*wolfSSL_Free_cb)(void *ptr); -typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size); - - -/* Public set function */ -WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb malloc_function, - wolfSSL_Free_cb free_function, - wolfSSL_Realloc_cb realloc_function); - -/* Public in case user app wants to use XMALLOC/XFREE */ -WOLFSSL_API void* wolfSSL_Malloc(size_t size); -WOLFSSL_API void wolfSSL_Free(void *ptr); -WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size); - - -#ifdef __cplusplus -} + typedef void *(*wolfSSL_Malloc_cb)(size_t size); + typedef void (*wolfSSL_Free_cb)(void *ptr); + typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size); + + + /* Public set function */ + WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb malloc_function, + wolfSSL_Free_cb free_function, + wolfSSL_Realloc_cb realloc_function); + + /* Public in case user app wants to use XMALLOC/XFREE */ + WOLFSSL_API void* wolfSSL_Malloc(size_t size); + WOLFSSL_API void wolfSSL_Free(void *ptr); + WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size); +#else + #include + /* when using fips map wolfSSL to CyaSSL*/ + #define wolfSSL_Malloc_cb CyaSSL_Malloc_cb + #define wolfSSL_Free_cb CyaSSL_Free_cb + #define wolfSSL_Realloc_cb CyaSSL_Realloc_cb + #define wolfSSL_SetAllocators CyaSSL_SetAllocators + + /* Public in case user app wants to use XMALLOC/XFREE */ + #define wolfSSL_Malloc CyaSSL_Malloc + #define wolfSSL_Free CyaSSL_Free + #define wolfSSL_Realloc CyaSSL_Realloc #endif #endif /* WOLFSSL_MEMORY_H */ + diff --git a/wolfssl/wolfcrypt/pkcs7.h b/wolfssl/wolfcrypt/pkcs7.h index 63ae2a54c..33ce04b94 100644 --- a/wolfssl/wolfcrypt/pkcs7.h +++ b/wolfssl/wolfcrypt/pkcs7.h @@ -2,15 +2,15 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, - * * but WITHOUT ANY WARRANTY; without even the implied warranty of + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * @@ -22,14 +22,14 @@ #ifdef HAVE_PKCS7 -#ifndef CTAO_CRYPT_PKCS7_H -#define CTAO_CRYPT_PKCS7_H +#ifndef WOLF_CRYPT_PKCS7_H +#define WOLF_CRYPT_PKCS7_H -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #ifdef __cplusplus extern "C" { @@ -91,25 +91,25 @@ typedef struct PKCS7 { } PKCS7; -CYASSL_LOCAL int SetContentType(int pkcs7TypeOID, byte* output); -CYASSL_LOCAL int GetContentType(const byte* input, word32* inOutIdx, +WOLFSSL_LOCAL int wc_SetContentType(int pkcs7TypeOID, byte* output); +WOLFSSL_LOCAL int wc_GetContentType(const byte* input, word32* inOutIdx, word32* oid, word32 maxIdx); -CYASSL_LOCAL int CreateRecipientInfo(const byte* cert, word32 certSz, +WOLFSSL_LOCAL int wc_CreateRecipientInfo(const byte* cert, word32 certSz, int keyEncAlgo, int blockKeySz, RNG* rng, byte* contentKeyPlain, byte* contentKeyEnc, int* keyEncSz, byte* out, word32 outSz); -CYASSL_API int PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz); -CYASSL_API void PKCS7_Free(PKCS7* pkcs7); -CYASSL_API int PKCS7_EncodeData(PKCS7* pkcs7, byte* output, word32 outputSz); -CYASSL_API int PKCS7_EncodeSignedData(PKCS7* pkcs7, +WOLFSSL_API int wc_PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz); +WOLFSSL_API void wc_PKCS7_Free(PKCS7* pkcs7); +WOLFSSL_API int wc_PKCS7_EncodeData(PKCS7* pkcs7, byte* output, word32 outputSz); +WOLFSSL_API int wc_PKCS7_EncodeSignedData(PKCS7* pkcs7, byte* output, word32 outputSz); -CYASSL_API int PKCS7_VerifySignedData(PKCS7* pkcs7, +WOLFSSL_API int wc_PKCS7_VerifySignedData(PKCS7* pkcs7, byte* pkiMsg, word32 pkiMsgSz); -CYASSL_API int PKCS7_EncodeEnvelopedData(PKCS7* pkcs7, +WOLFSSL_API int wc_PKCS7_EncodeEnvelopedData(PKCS7* pkcs7, byte* output, word32 outputSz); -CYASSL_API int PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg, +WOLFSSL_API int wc_PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg, word32 pkiMsgSz, byte* output, word32 outputSz); @@ -117,7 +117,7 @@ CYASSL_API int PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg, } /* extern "C" */ #endif -#endif /* CTAO_CRYPT_PKCS7_H */ +#endif /* WOLF_CRYPT_PKCS7_H */ #endif /* HAVE_PKCS7 */ diff --git a/wolfssl/wolfcrypt/poly1305.h b/wolfssl/wolfcrypt/poly1305.h index 6383b589f..08797b556 100644 --- a/wolfssl/wolfcrypt/poly1305.h +++ b/wolfssl/wolfcrypt/poly1305.h @@ -27,6 +27,11 @@ #include +/* for poly1305 reverse compatibility */ +#define Poly1305SetKey wc_Poly1305SetKey +#define Poly1305Update wc_Poly1305Update +#define Poly1305Final wc_Poly1305Final + #ifdef __cplusplus extern "C" { #endif diff --git a/wolfssl/wolfcrypt/random.h b/wolfssl/wolfcrypt/random.h index 1c2c846af..119a0d75c 100644 --- a/wolfssl/wolfcrypt/random.h +++ b/wolfssl/wolfcrypt/random.h @@ -25,16 +25,6 @@ #include -#if defined(HAVE_HASHDRBG) || defined(NO_RC4) - #ifdef NO_SHA256 - #error "Hash DRBG requires SHA-256." - #endif /* NO_SHA256 */ - - #include -#else /* HAVE_HASHDRBG || NO_RC4 */ - #include -#endif /* HAVE_HASHDRBG || NO_RC4 */ - /* for fips */ #include diff --git a/wolfssl/wolfcrypt/rsa.h b/wolfssl/wolfcrypt/rsa.h index a121e384e..c712823ec 100644 --- a/wolfssl/wolfcrypt/rsa.h +++ b/wolfssl/wolfcrypt/rsa.h @@ -27,11 +27,6 @@ /* for fips */ #include - -//#include -//#include -//#include - #ifdef __cplusplus extern "C" { #endif @@ -75,41 +70,27 @@ WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey*, byte*, word32*, byte*, #ifdef HAVE_FIPS /* fips wrapper calls, user can call direct */ - WOLFSSL_API int InitRsaKey_fips(RsaKey* key, void*); - WOLFSSL_API int FreeRsaKey_fips(RsaKey* key); + WOLFSSL_API int wc_InitRsaKey_fips(RsaKey* key, void*); + WOLFSSL_API int wc_FreeRsaKey_fips(RsaKey* key); - WOLFSSL_API int RsaPublicEncrypt_fips(const byte* in,word32 inLen,byte* out, + WOLFSSL_API int wc_RsaPublicEncrypt_fips(const byte* in,word32 inLen,byte* out, word32 outLen, RsaKey* key, RNG* rng); - WOLFSSL_API int RsaPrivateDecryptInline_fips(byte* in, word32 inLen, + WOLFSSL_API int wc_RsaPrivateDecryptInline_fips(byte* in, word32 inLen, byte** out, RsaKey* key); - WOLFSSL_API int RsaPrivateDecrypt_fips(const byte* in, word32 inLen, + WOLFSSL_API int wc_RsaPrivateDecrypt_fips(const byte* in, word32 inLen, byte* out,word32 outLen,RsaKey* key); - WOLFSSL_API int RsaSSL_Sign_fips(const byte* in, word32 inLen, byte* out, + WOLFSSL_API int wc_RsaSSL_Sign_fips(const byte* in, word32 inLen, byte* out, word32 outLen, RsaKey* key, RNG* rng); - WOLFSSL_API int RsaSSL_VerifyInline_fips(byte* in, word32 inLen, byte** out, + WOLFSSL_API int wc_RsaSSL_VerifyInline_fips(byte* in, word32 inLen, byte** out, RsaKey* key); - WOLFSSL_API int RsaSSL_Verify_fips(const byte* in, word32 inLen, byte* out, + WOLFSSL_API int wc_RsaSSL_Verify_fips(const byte* in, word32 inLen, byte* out, word32 outLen, RsaKey* key); - WOLFSSL_API int RsaEncryptSize_fips(RsaKey* key); + WOLFSSL_API int wc_RsaEncryptSize_fips(RsaKey* key); - WOLFSSL_API int RsaPrivateKeyDecode_fips(const byte* input, word32* inOutIdx, + WOLFSSL_API int wc_RsaPrivateKeyDecode_fips(const byte* input, word32* inOutIdx, RsaKey*, word32); - WOLFSSL_API int RsaPublicKeyDecode_fips(const byte* input, word32* inOutIdx, + WOLFSSL_API int wc_RsaPublicKeyDecode_fips(const byte* input, word32* inOutIdx, RsaKey*, word32); - #ifndef FIPS_NO_WRAPPERS - /* if not impl or fips.c impl wrapper force fips calls if fips build */ - #define InitRsaKey InitRsaKey_fips - #define FreeRsaKey FreeRsaKey_fips - #define RsaPublicEncrypt RsaPublicEncrypt_fips - #define RsaPrivateDecryptInline RsaPrivateDecryptInline_fips - #define RsaPrivateDecrypt RsaPrivateDecrypt_fips - #define RsaSSL_Sign RsaSSL_Sign_fips - #define RsaSSL_VerifyInline RsaSSL_VerifyInline_fips - #define RsaSSL_Verify RsaSSL_Verify_fips - #define RsaEncryptSize RsaEncryptSize_fips - /* no implicit KeyDecodes since in asn.c (not rsa.c) */ - #endif /* FIPS_NO_WRAPPERS */ - #endif /* HAVE_FIPS */ diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 5e8a06af0..db337a196 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -26,7 +26,716 @@ #ifndef WOLF_CRYPT_SETTINGS_H #define WOLF_CRYPT_SETTINGS_H +#ifdef __cplusplus + extern "C" { +#endif + +/* Uncomment next line if using IPHONE */ +/* #define IPHONE */ + +/* Uncomment next line if using ThreadX */ +/* #define THREADX */ + +/* Uncomment next line if using Micrium ucOS */ +/* #define MICRIUM */ + +/* Uncomment next line if using Mbed */ +/* #define MBED */ + +/* Uncomment next line if using Microchip PIC32 ethernet starter kit */ +/* #define MICROCHIP_PIC32 */ + +/* Uncomment next line if using Microchip TCP/IP stack, version 5 */ +/* #define MICROCHIP_TCPIP_V5 */ + +/* Uncomment next line if using Microchip TCP/IP stack, version 6 or later */ +/* #define MICROCHIP_TCPIP */ + +/* Uncomment next line if using PIC32MZ Crypto Engine */ +/* #define WOLFSSL_MICROCHIP_PIC32MZ */ + +/* Uncomment next line if using FreeRTOS */ +/* #define FREERTOS */ + +/* Uncomment next line if using FreeRTOS Windows Simulator */ +/* #define FREERTOS_WINSIM */ + +/* Uncomment next line if using RTIP */ +/* #define EBSNET */ + +/* Uncomment next line if using lwip */ +/* #define WOLFSSL_LWIP */ + +/* Uncomment next line if building wolfSSL for a game console */ +/* #define WOLFSSL_GAME_BUILD */ + +/* Uncomment next line if building wolfSSL for LSR */ +/* #define WOLFSSL_LSR */ + +/* Uncomment next line if building wolfSSL for Freescale MQX/RTCS/MFS */ +/* #define FREESCALE_MQX */ + +/* Uncomment next line if using STM32F2 */ +/* #define WOLFSSL_STM32F2 */ + +/* Uncomment next line if using Comverge settings */ +/* #define COMVERGE */ + +/* Uncomment next line if using QL SEP settings */ +/* #define WOLFSSL_QL */ + +/* Uncomment next line if building for EROAD */ +/* #define WOLFSSL_EROAD */ + +/* Uncomment next line if building for IAR EWARM */ +/* #define WOLFSSL_IAR_ARM */ + +/* Uncomment next line if using TI-RTOS settings */ +/* #define WOLFSSL_TIRTOS */ + +/* Uncomment next line if building with PicoTCP */ +/* #define WOLFSSL_PICOTCP */ + +/* Uncomment next line if building for PicoTCP demo bundle */ +/* #define WOLFSSL_PICOTCP_DEMO */ + #include -#include + +#ifdef IPHONE + #define SIZEOF_LONG_LONG 8 +#endif + + +#ifdef WOLFSSL_USER_SETTINGS + #include +#endif + + +#ifdef COMVERGE + #define THREADX + #define HAVE_NETX + #define WOLFSSL_USER_IO + #define NO_WRITEV + #define NO_DEV_RANDOM + #define NO_FILESYSTEM + #define NO_SHA512 + #define NO_DH + #define NO_DSA + #define NO_HC128 + #define NO_RSA + #define NO_SESSION_CACHE + #define HAVE_ECC +#endif + + +#ifdef THREADX + #define SIZEOF_LONG_LONG 8 +#endif + +#ifdef HAVE_NETX + #include "nx_api.h" +#endif + +#if defined(HAVE_LWIP_NATIVE) /* using LwIP native TCP socket */ + #define WOLFSSL_LWIP + #define NO_WRITEV + #define SINGLE_THREADED + #define WOLFSSL_USER_IO + #define NO_FILESYSTEM +#endif + +#if defined(WOLFSSL_IAR_ARM) + #define NO_MAIN_DRIVER + #define SINGLE_THREADED + #define USE_CERT_BUFFERS_1024 + #define BENCH_EMBEDDED + #define NO_FILESYSTEM + #define NO_WRITEV + #define WOLFSSL_USER_IO + #define BENCH_EMBEDDED +#endif + +#ifdef MICROCHIP_PIC32 + /* #define WOLFSSL_MICROCHIP_PIC32MZ */ + #define SIZEOF_LONG_LONG 8 + #define SINGLE_THREADED + #define WOLFSSL_USER_IO + #define NO_WRITEV + #define NO_DEV_RANDOM + #define NO_FILESYSTEM + #define USE_FAST_MATH + #define TFM_TIMING_RESISTANT +#endif + +#ifdef WOLFSSL_MICROCHIP_PIC32MZ + #define WOLFSSL_PIC32MZ_CE + #define WOLFSSL_PIC32MZ_CRYPT + #define HAVE_AES_ENGINE + #define WOLFSSL_PIC32MZ_RNG + /* #define WOLFSSL_PIC32MZ_HASH */ + #define WOLFSSL_AES_COUNTER + #define HAVE_AESGCM + #define NO_BIG_INT + +#endif + +#ifdef MICROCHIP_TCPIP_V5 + /* include timer functions */ + #include "TCPIP Stack/TCPIP.h" +#endif + +#ifdef MICROCHIP_TCPIP + /* include timer, NTP functions */ + #ifdef MICROCHIP_MPLAB_HARMONY + #include "tcpip/tcpip.h" + #else + #include "system/system_services.h" + #include "tcpip/sntp.h" + #endif +#endif + +#ifdef MBED + #define WOLFSSL_USER_IO + #define NO_FILESYSTEM + #define NO_CERT + #define USE_CERT_BUFFERS_1024 + #define NO_WRITEV + #define NO_DEV_RANDOM + #define NO_SHA512 + #define NO_DH + #define NO_DSA + #define NO_HC128 + #define HAVE_ECC + #define NO_SESSION_CACHE + #define WOLFSSL_CMSIS_RTOS +#endif + + +#ifdef WOLFSSL_EROAD + #define FREESCALE_MQX + #define FREESCALE_MMCAU + #define SINGLE_THREADED + #define NO_STDIO_FILESYSTEM + #define WOLFSSL_LEANPSK + #define HAVE_NULL_CIPHER + #define NO_OLD_TLS + #define NO_ASN + #define NO_BIG_INT + #define NO_RSA + #define NO_DSA + #define NO_DH + #define NO_CERTS + #define NO_PWDBASED + #define NO_DES3 + #define NO_MD4 + #define NO_RC4 + #define NO_MD5 + #define NO_SESSION_CACHE + #define NO_MAIN_DRIVER +#endif + +#ifdef WOLFSSL_PICOTCP + #define errno pico_err + #include "pico_defines.h" + #include "pico_stack.h" + #include "pico_constants.h" + #define CUSTOM_RAND_GENERATE pico_rand +#endif + +#ifdef WOLFSSL_PICOTCP_DEMO + #define WOLFSSL_STM32 + #define USE_FAST_MATH + #define TFM_TIMING_RESISTANT + #define XMALLOC(s, h, type) PICO_ZALLOC((s)) + #define XFREE(p, h, type) PICO_FREE((p)) + #define SINGLE_THREADED + #define NO_WRITEV + #define WOLFSSL_USER_IO + #define NO_DEV_RANDOM + #define NO_FILESYSTEM +#endif + +#ifdef FREERTOS_WINSIM + #define FREERTOS + #define USE_WINDOWS_API +#endif + + +/* Micrium will use Visual Studio for compilation but not the Win32 API */ +#if defined(_WIN32) && !defined(MICRIUM) && !defined(FREERTOS) \ + && !defined(EBSNET) && !defined(WOLFSSL_EROAD) + #define USE_WINDOWS_API +#endif + + +#if defined(WOLFSSL_LEANPSK) && !defined(XMALLOC_USER) + #include + #define XMALLOC(s, h, type) malloc((s)) + #define XFREE(p, h, type) free((p)) + #define XREALLOC(p, n, h, t) realloc((p), (n)) +#endif + +#if defined(XMALLOC_USER) && defined(SSN_BUILDING_LIBYASSL) + #undef XMALLOC + #define XMALLOC yaXMALLOC + #undef XFREE + #define XFREE yaXFREE + #undef XREALLOC + #define XREALLOC yaXREALLOC +#endif + + +#ifdef FREERTOS + #ifndef NO_WRITEV + #define NO_WRITEV + #endif + #ifndef NO_SHA512 + #define NO_SHA512 + #endif + #ifndef NO_DH + #define NO_DH + #endif + #ifndef NO_DSA + #define NO_DSA + #endif + #ifndef NO_HC128 + #define NO_HC128 + #endif + + #ifndef SINGLE_THREADED + #include "FreeRTOS.h" + #include "semphr.h" + #endif +#endif + +#ifdef WOLFSSL_TIRTOS + #define SIZEOF_LONG_LONG 8 + #define NO_WRITEV + #define NO_WOLFSSL_DIR + #define USE_FAST_MATH + #define TFM_TIMING_RESISTANT + #define NO_DEV_RANDOM + #define NO_FILESYSTEM + #define USE_CERT_BUFFERS_2048 + #define NO_ERROR_STRINGS + #define USER_TIME + + #ifdef __IAR_SYSTEMS_ICC__ + #pragma diag_suppress=Pa089 + #elif !defined(__GNUC__) + /* Suppress the sslpro warning */ + #pragma diag_suppress=11 + #endif + + #include +#endif + +#ifdef EBSNET + #include "rtip.h" + + /* #define DEBUG_WOLFSSL */ + #define NO_WOLFSSL_DIR /* tbd */ + + #if (POLLOS) + #define SINGLE_THREADED + #endif + + #if (RTPLATFORM) + #if (!RTP_LITTLE_ENDIAN) + #define BIG_ENDIAN_ORDER + #endif + #else + #if (!KS_LITTLE_ENDIAN) + #define BIG_ENDIAN_ORDER + #endif + #endif + + #if (WINMSP3) + #undef SIZEOF_LONG + #define SIZEOF_LONG_LONG 8 + #else + #sslpro: settings.h - please implement SIZEOF_LONG and SIZEOF_LONG_LONG + #endif + + #define XMALLOC(s, h, type) ((void *)rtp_malloc((s), SSL_PRO_MALLOC)) + #define XFREE(p, h, type) (rtp_free(p)) + #define XREALLOC(p, n, h, t) realloc((p), (n)) + +#endif /* EBSNET */ + +#ifdef WOLFSSL_GAME_BUILD + #define SIZEOF_LONG_LONG 8 + #if defined(__PPU) || defined(__XENON) + #define BIG_ENDIAN_ORDER + #endif +#endif + +#ifdef WOLFSSL_LSR + #define HAVE_WEBSERVER + #define SIZEOF_LONG_LONG 8 + #define WOLFSSL_LOW_MEMORY + #define NO_WRITEV + #define NO_SHA512 + #define NO_DH + #define NO_DSA + #define NO_HC128 + #define NO_DEV_RANDOM + #define NO_WOLFSSL_DIR + #define NO_RABBIT + #ifndef NO_FILESYSTEM + #define LSR_FS + #include "inc/hw_types.h" + #include "fs.h" + #endif + #define WOLFSSL_LWIP + #include /* for tcp errno */ + #define WOLFSSL_SAFERTOS + #if defined(__IAR_SYSTEMS_ICC__) + /* enum uses enum */ + #pragma diag_suppress=Pa089 + #endif +#endif + +#ifdef WOLFSSL_SAFERTOS + #ifndef SINGLE_THREADED + #include "SafeRTOS/semphr.h" + #endif + + #include "SafeRTOS/heap.h" + #define XMALLOC(s, h, type) pvPortMalloc((s)) + #define XFREE(p, h, type) vPortFree((p)) + #define XREALLOC(p, n, h, t) pvPortRealloc((p), (n)) +#endif + +#ifdef WOLFSSL_LOW_MEMORY + #undef RSA_LOW_MEM + #define RSA_LOW_MEM + #undef WOLFSSL_SMALL_STACK + #define WOLFSSL_SMALL_STACK + #undef TFM_TIMING_RESISTANT + #define TFM_TIMING_RESISTANT +#endif + +#ifdef FREESCALE_MQX + #define SIZEOF_LONG_LONG 8 + #define NO_WRITEV + #define NO_DEV_RANDOM + #define NO_RABBIT + #define NO_WOLFSSL_DIR + #define USE_FAST_MATH + #define TFM_TIMING_RESISTANT + #define FREESCALE_K70_RNGA + /* #define FREESCALE_K53_RNGB */ + #include "mqx.h" + #ifndef NO_FILESYSTEM + #include "mfs.h" + #include "fio.h" + #endif + #ifndef SINGLE_THREADED + #include "mutex.h" + #endif + + #define XMALLOC(s, h, t) (void *)_mem_alloc_system((s)) + #define XFREE(p, h, t) {void* xp = (p); if ((xp)) _mem_free((xp));} + /* Note: MQX has no realloc, using fastmath above */ +#endif + +#ifdef WOLFSSL_STM32F2 + #define SIZEOF_LONG_LONG 8 + #define NO_DEV_RANDOM + #define NO_WOLFSSL_DIR + #define NO_RABBIT + #define STM32F2_RNG + #define STM32F2_CRYPTO + #define KEIL_INTRINSICS +#endif + +#ifdef MICRIUM + + #include "stdlib.h" + #include "net_cfg.h" + #include "ssl_cfg.h" + #include "net_secure_os.h" + + #define WOLFSSL_TYPES + + typedef CPU_INT08U byte; + typedef CPU_INT16U word16; + typedef CPU_INT32U word32; + + #if (NET_SECURE_MGR_CFG_WORD_SIZE == CPU_WORD_SIZE_32) + #define SIZEOF_LONG 4 + #undef SIZEOF_LONG_LONG + #else + #undef SIZEOF_LONG + #define SIZEOF_LONG_LONG 8 + #endif + + #define STRING_USER + + #define XSTRLEN(pstr) ((CPU_SIZE_T)Str_Len((CPU_CHAR *)(pstr))) + #define XSTRNCPY(pstr_dest, pstr_src, len_max) \ + ((CPU_CHAR *)Str_Copy_N((CPU_CHAR *)(pstr_dest), \ + (CPU_CHAR *)(pstr_src), (CPU_SIZE_T)(len_max))) + #define XSTRNCMP(pstr_1, pstr_2, len_max) \ + ((CPU_INT16S)Str_Cmp_N((CPU_CHAR *)(pstr_1), \ + (CPU_CHAR *)(pstr_2), (CPU_SIZE_T)(len_max))) + #define XSTRSTR(pstr, pstr_srch) \ + ((CPU_CHAR *)Str_Str((CPU_CHAR *)(pstr), \ + (CPU_CHAR *)(pstr_srch))) + #define XMEMSET(pmem, data_val, size) \ + ((void)Mem_Set((void *)(pmem), (CPU_INT08U) (data_val), \ + (CPU_SIZE_T)(size))) + #define XMEMCPY(pdest, psrc, size) ((void)Mem_Copy((void *)(pdest), \ + (void *)(psrc), (CPU_SIZE_T)(size))) + #define XMEMCMP(pmem_1, pmem_2, size) \ + (((CPU_BOOLEAN)Mem_Cmp((void *)(pmem_1), (void *)(pmem_2), \ + (CPU_SIZE_T)(size))) ? DEF_NO : DEF_YES) + #define XMEMMOVE XMEMCPY + +#if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) + #define MICRIUM_MALLOC + #define XMALLOC(s, h, type) ((void *)NetSecure_BlkGet((CPU_INT08U)(type), \ + (CPU_SIZE_T)(s), (void *)0)) + #define XFREE(p, h, type) (NetSecure_BlkFree((CPU_INT08U)(type), \ + (p), (void *)0)) + #define XREALLOC(p, n, h, t) realloc((p), (n)) +#endif + + #if (NET_SECURE_MGR_CFG_FS_EN == DEF_ENABLED) + #undef NO_FILESYSTEM + #else + #define NO_FILESYSTEM + #endif + + #if (SSL_CFG_TRACE_LEVEL == WOLFSSL_TRACE_LEVEL_DBG) + #define DEBUG_WOLFSSL + #else + #undef DEBUG_WOLFSSL + #endif + + #if (SSL_CFG_OPENSSL_EN == DEF_ENABLED) + #define OPENSSL_EXTRA + #else + #undef OPENSSL_EXTRA + #endif + + #if (SSL_CFG_MULTI_THREAD_EN == DEF_ENABLED) + #undef SINGLE_THREADED + #else + #define SINGLE_THREADED + #endif + + #if (SSL_CFG_DH_EN == DEF_ENABLED) + #undef NO_DH + #else + #define NO_DH + #endif + + #if (SSL_CFG_DSA_EN == DEF_ENABLED) + #undef NO_DSA + #else + #define NO_DSA + #endif + + #if (SSL_CFG_PSK_EN == DEF_ENABLED) + #undef NO_PSK + #else + #define NO_PSK + #endif + + #if (SSL_CFG_3DES_EN == DEF_ENABLED) + #undef NO_DES + #else + #define NO_DES + #endif + + #if (SSL_CFG_AES_EN == DEF_ENABLED) + #undef NO_AES + #else + #define NO_AES + #endif + + #if (SSL_CFG_RC4_EN == DEF_ENABLED) + #undef NO_RC4 + #else + #define NO_RC4 + #endif + + #if (SSL_CFG_RABBIT_EN == DEF_ENABLED) + #undef NO_RABBIT + #else + #define NO_RABBIT + #endif + + #if (SSL_CFG_HC128_EN == DEF_ENABLED) + #undef NO_HC128 + #else + #define NO_HC128 + #endif + + #if (CPU_CFG_ENDIAN_TYPE == CPU_ENDIAN_TYPE_BIG) + #define BIG_ENDIAN_ORDER + #else + #undef BIG_ENDIAN_ORDER + #define LITTLE_ENDIAN_ORDER + #endif + + #if (SSL_CFG_MD4_EN == DEF_ENABLED) + #undef NO_MD4 + #else + #define NO_MD4 + #endif + + #if (SSL_CFG_WRITEV_EN == DEF_ENABLED) + #undef NO_WRITEV + #else + #define NO_WRITEV + #endif + + #if (SSL_CFG_USER_RNG_SEED_EN == DEF_ENABLED) + #define NO_DEV_RANDOM + #else + #undef NO_DEV_RANDOM + #endif + + #if (SSL_CFG_USER_IO_EN == DEF_ENABLED) + #define WOLFSSL_USER_IO + #else + #undef WOLFSSL_USER_IO + #endif + + #if (SSL_CFG_DYNAMIC_BUFFERS_EN == DEF_ENABLED) + #undef LARGE_STATIC_BUFFERS + #undef STATIC_CHUNKS_ONLY + #else + #define LARGE_STATIC_BUFFERS + #define STATIC_CHUNKS_ONLY + #endif + + #if (SSL_CFG_DER_LOAD_EN == DEF_ENABLED) + #define WOLFSSL_DER_LOAD + #else + #undef WOLFSSL_DER_LOAD + #endif + + #if (SSL_CFG_DTLS_EN == DEF_ENABLED) + #define WOLFSSL_DTLS + #else + #undef WOLFSSL_DTLS + #endif + + #if (SSL_CFG_CALLBACKS_EN == DEF_ENABLED) + #define WOLFSSL_CALLBACKS + #else + #undef WOLFSSL_CALLBACKS + #endif + + #if (SSL_CFG_FAST_MATH_EN == DEF_ENABLED) + #define USE_FAST_MATH + #else + #undef USE_FAST_MATH + #endif + + #if (SSL_CFG_TFM_TIMING_RESISTANT_EN == DEF_ENABLED) + #define TFM_TIMING_RESISTANT + #else + #undef TFM_TIMING_RESISTANT + #endif + +#endif /* MICRIUM */ + + +#ifdef WOLFSSL_QL + #ifndef WOLFSSL_SEP + #define WOLFSSL_SEP + #endif + #ifndef OPENSSL_EXTRA + #define OPENSSL_EXTRA + #endif + #ifndef SESSION_CERTS + #define SESSION_CERTS + #endif + #ifndef HAVE_AESCCM + #define HAVE_AESCCM + #endif + #ifndef ATOMIC_USER + #define ATOMIC_USER + #endif + #ifndef WOLFSSL_DER_LOAD + #define WOLFSSL_DER_LOAD + #endif + #ifndef KEEP_PEER_CERT + #define KEEP_PEER_CERT + #endif + #ifndef HAVE_ECC + #define HAVE_ECC + #endif + #ifndef SESSION_INDEX + #define SESSION_INDEX + #endif +#endif /* WOLFSSL_QL */ + + +#if !defined(XMALLOC_USER) && !defined(MICRIUM_MALLOC) && \ + !defined(WOLFSSL_LEANPSK) && !defined(NO_CYASSL_MEMORY) + #define USE_WOLFSSL_MEMORY +#endif + + +#if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) + #undef KEEP_PEER_CERT + #define KEEP_PEER_CERT +#endif + + +/* stream ciphers except arc4 need 32bit alignment, intel ok without */ +#ifndef XSTREAM_ALIGNMENT + #if defined(__x86_64__) || defined(__ia64__) || defined(__i386__) + #define NO_XSTREAM_ALIGNMENT + #else + #define XSTREAM_ALIGNMENT + #endif +#endif + + +/* FreeScale MMCAU hardware crypto has 4 byte alignment */ +#ifdef FREESCALE_MMCAU + #define WOLFSSL_MMCAU_ALIGNMENT 4 +#endif + +/* if using hardware crypto and have alignment requirements, specify the + requirement here. The record header of SSL/TLS will prvent easy alignment. + This hint tries to help as much as possible. */ +#ifndef WOLFSSL_GENERAL_ALIGNMENT + #ifdef WOLFSSL_AESNI + #define WOLFSSL_GENERAL_ALIGNMENT 16 + #elif defined(XSTREAM_ALIGNMENT) + #define WOLFSSL_GENERAL_ALIGNMENT 4 + #elif defined(FREESCALE_MMCAU) + #define WOLFSSL_GENERAL_ALIGNMENT CYASSL_MMCAU_ALIGNMENT + #else + #define WOLFSSL_GENERAL_ALIGNMENT 0 + #endif +#endif + +#ifdef HAVE_CRL + /* not widely supported yet */ + #undef NO_SKID + #define NO_SKID +#endif + + +#ifdef __INTEL_COMPILER + #pragma warning(disable:2259) /* explicit casts to smaller sizes, disable */ +#endif + + +/* Place any other flags or defines here */ + + +#ifdef __cplusplus + } /* extern "C" */ +#endif #endif diff --git a/wolfssl/wolfcrypt/sha.h b/wolfssl/wolfcrypt/sha.h index f1820a6d9..5bd98cf10 100644 --- a/wolfssl/wolfcrypt/sha.h +++ b/wolfssl/wolfcrypt/sha.h @@ -2,14 +2,14 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -22,62 +22,35 @@ #ifndef NO_SHA -#ifndef CTAO_CRYPT_SHA_H -#define CTAO_CRYPT_SHA_H +#ifndef WOLF_CRYPT_SHA_H +#define WOLF_CRYPT_SHA_H -#include + +#include + +/* for fips */ +#include #ifdef __cplusplus extern "C" { #endif - -/* in bytes */ -enum { -#ifdef STM32F2_HASH - SHA_REG_SIZE = 4, /* STM32 register size, bytes */ -#endif - SHA = 1, /* hash type unique */ - SHA_BLOCK_SIZE = 64, - SHA_DIGEST_SIZE = 20, - SHA_PAD_SIZE = 56 -}; - -#ifdef CYASSL_PIC32MZ_HASH -#include "port/pic32/pic32mz-crypt.h" -#endif - -/* Sha digest */ -typedef struct Sha { - word32 buffLen; /* in bytes */ - word32 loLen; /* length in bytes */ - word32 hiLen; /* length in bytes */ - word32 buffer[SHA_BLOCK_SIZE / sizeof(word32)]; - #ifndef CYASSL_PIC32MZ_HASH - word32 digest[SHA_DIGEST_SIZE / sizeof(word32)]; - #else - word32 digest[PIC32_HASH_SIZE / sizeof(word32)]; - pic32mz_desc desc; /* Crypt Engine descripter */ - #endif -} Sha; - - -CYASSL_API int InitSha(Sha*); -CYASSL_API int ShaUpdate(Sha*, const byte*, word32); -CYASSL_API int ShaFinal(Sha*, byte*); -CYASSL_API int ShaHash(const byte*, word32, byte*); +WOLFSSL_API int wc_InitSha(Sha*); +WOLFSSL_API int wc_ShaUpdate(Sha*, const byte*, word32); +WOLFSSL_API int wc_ShaFinal(Sha*, byte*); +WOLFSSL_API int wc_ShaHash(const byte*, word32, byte*); #ifdef HAVE_FIPS /* fips wrapper calls, user can call direct */ - CYASSL_API int InitSha_fips(Sha*); - CYASSL_API int ShaUpdate_fips(Sha*, const byte*, word32); - CYASSL_API int ShaFinal_fips(Sha*, byte*); + WOLFSSL_API int wc_InitSha_fips(Sha*); + WOLFSSL_API int wc_ShaUpdate_fips(Sha*, const byte*, word32); + WOLFSSL_API int wc_ShaFinal_fips(Sha*, byte*); #ifndef FIPS_NO_WRAPPERS /* if not impl or fips.c impl wrapper force fips calls if fips build */ - #define InitSha InitSha_fips - #define ShaUpdate ShaUpdate_fips - #define ShaFinal ShaFinal_fips + #define wc_InitSha wc_InitSha_fips + #define wc_ShaUpdate wc_ShaUpdate_fips + #define wc_ShaFinal wc_ShaFinal_fips #endif /* FIPS_NO_WRAPPERS */ #endif /* HAVE_FIPS */ diff --git a/wolfssl/wolfcrypt/sha256.h b/wolfssl/wolfcrypt/sha256.h index c619461a3..33dbd8154 100644 --- a/wolfssl/wolfcrypt/sha256.h +++ b/wolfssl/wolfcrypt/sha256.h @@ -1,15 +1,13 @@ /* sha256.h * - * Copyright (C) 2006-2014 wolfSSL Inc. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * This file is part of CyaSSL. - * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -25,67 +23,35 @@ #ifndef NO_SHA256 -#ifndef CTAO_CRYPT_SHA256_H -#define CTAO_CRYPT_SHA256_H +#ifndef WOLF_CRYPT_SHA256_H +#define WOLF_CRYPT_SHA256_H -#include +/* for fips */ +#include + +//#ifndef HAVE_FIPS +#include #ifdef __cplusplus extern "C" { #endif -#ifdef CYASSL_PIC32MZ_HASH -#include "port/pic32/pic32mz-crypt.h" -#endif - - -/* in bytes */ -enum { - SHA256 = 2, /* hash type unique */ - SHA256_BLOCK_SIZE = 64, - SHA256_DIGEST_SIZE = 32, - SHA256_PAD_SIZE = 56 -}; - - -/* Sha256 digest */ -typedef struct Sha256 { - word32 buffLen; /* in bytes */ - word32 loLen; /* length in bytes */ - word32 hiLen; /* length in bytes */ - word32 digest[SHA256_DIGEST_SIZE / sizeof(word32)]; - word32 buffer[SHA256_BLOCK_SIZE / sizeof(word32)]; - #ifdef CYASSL_PIC32MZ_HASH - pic32mz_desc desc ; /* Crypt Engine descripter */ - #endif -} Sha256; - - -CYASSL_API int InitSha256(Sha256*); -CYASSL_API int Sha256Update(Sha256*, const byte*, word32); -CYASSL_API int Sha256Final(Sha256*, byte*); -CYASSL_API int Sha256Hash(const byte*, word32, byte*); - - -#ifdef HAVE_FIPS - /* fips wrapper calls, user can call direct */ - CYASSL_API int InitSha256_fips(Sha256*); - CYASSL_API int Sha256Update_fips(Sha256*, const byte*, word32); - CYASSL_API int Sha256Final_fips(Sha256*, byte*); - #ifndef FIPS_NO_WRAPPERS - /* if not impl or fips.c impl wrapper force fips calls if fips build */ - #define InitSha256 InitSha256_fips - #define Sha256Update Sha256Update_fips - #define Sha256Final Sha256Final_fips - #endif /* FIPS_NO_WRAPPERS */ - -#endif /* HAVE_FIPS */ - +WOLFSSL_API int wc_InitSha256(Sha256*); +WOLFSSL_API int wc_Sha256Update(Sha256*, const byte*, word32); +WOLFSSL_API int wc_Sha256Final(Sha256*, byte*); +WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*); #ifdef __cplusplus } /* extern "C" */ #endif +//#else +//#define wc_InitSha256 wc_InitSha256Sha256*); +//#define int wc_Sha256Update(Swc_Sha256Updateha256*, const byte*, word32); +//#define int wc_Sha256Final(wc_Sha256FinalSha256*, byte*); +//#define int wc_Sha256Hash(wc_Sha256Hashconst byte*, word32, byte*); +// +//#endif /* HAVE_FIPS */ -#endif /* CTAO_CRYPT_SHA256_H */ +#endif /* WOLF_CRYPT_SHA256_H */ #endif /* NO_SHA256 */ diff --git a/wolfssl/wolfcrypt/sha512.h b/wolfssl/wolfcrypt/sha512.h index 143402439..75056b40c 100644 --- a/wolfssl/wolfcrypt/sha512.h +++ b/wolfssl/wolfcrypt/sha512.h @@ -2,14 +2,14 @@ * * Copyright (C) 2006-2014 wolfSSL Inc. * - * This file is part of CyaSSL. + * This file is part of wolfSSL. (formerly known as CyaSSL) * - * CyaSSL is free software; you can redistribute it and/or modify + * wolfSSL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * CyaSSL is distributed in the hope that it will be useful, + * wolfSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -19,76 +19,54 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ +/* check for old macro */ +#if defined(CYASSL_SHA512) && !defined(WOLFSSL_SHA512) + #define WOLFSSL_SHA512 +#endif +#if defined(CYASSL_SHA384) && !defined(WOLFSSL_SHA384) + #define WOLFSSL_SHA384 +#endif -#ifdef CYASSL_SHA512 +#ifdef WOLFSSL_SHA512 -#ifndef CTAO_CRYPT_SHA512_H -#define CTAO_CRYPT_SHA512_H +#ifndef WOLF_CRYPT_SHA512_H +#define WOLF_CRYPT_SHA512_H + +#include + +/* since using old code turn on old macros @wc_fips */ +#if !defined(CYASSL_SHA512) + #define CYASSL_SHA512 +#endif +#if !defined(CYASSL_SHA384) && defined(WOLFSSL_SHA384) + #define CYASSL_SHA384 +#endif +/* for fips */ +#include -#include #ifdef __cplusplus extern "C" { #endif +WOLFSSL_API int wc_InitSha512(Sha512*); +WOLFSSL_API int wc_Sha512Update(Sha512*, const byte*, word32); +WOLFSSL_API int wc_Sha512Final(Sha512*, byte*); +WOLFSSL_API int wc_Sha512Hash(const byte*, word32, byte*); -/* in bytes */ -enum { - SHA512 = 4, /* hash type unique */ - SHA512_BLOCK_SIZE = 128, - SHA512_DIGEST_SIZE = 64, - SHA512_PAD_SIZE = 112 -}; +#if defined(WOLFSSL_SHA384) || defined(HAVE_AESGCM) - -/* Sha512 digest */ -typedef struct Sha512 { - word32 buffLen; /* in bytes */ - word32 loLen; /* length in bytes */ - word32 hiLen; /* length in bytes */ - word64 digest[SHA512_DIGEST_SIZE / sizeof(word64)]; - word64 buffer[SHA512_BLOCK_SIZE / sizeof(word64)]; -} Sha512; - - -CYASSL_API int InitSha512(Sha512*); -CYASSL_API int Sha512Update(Sha512*, const byte*, word32); -CYASSL_API int Sha512Final(Sha512*, byte*); -CYASSL_API int Sha512Hash(const byte*, word32, byte*); - - -#if defined(CYASSL_SHA384) || defined(HAVE_AESGCM) - -/* in bytes */ -enum { - SHA384 = 5, /* hash type unique */ - SHA384_BLOCK_SIZE = 128, - SHA384_DIGEST_SIZE = 48, - SHA384_PAD_SIZE = 112 -}; - - -/* Sha384 digest */ -typedef struct Sha384 { - word32 buffLen; /* in bytes */ - word32 loLen; /* length in bytes */ - word32 hiLen; /* length in bytes */ - word64 digest[SHA512_DIGEST_SIZE / sizeof(word64)]; /* for transform 512 */ - word64 buffer[SHA384_BLOCK_SIZE / sizeof(word64)]; -} Sha384; - - -CYASSL_API int InitSha384(Sha384*); -CYASSL_API int Sha384Update(Sha384*, const byte*, word32); -CYASSL_API int Sha384Final(Sha384*, byte*); -CYASSL_API int Sha384Hash(const byte*, word32, byte*); +WOLFSSL_API int wc_InitSha384(Sha384*); +WOLFSSL_API int wc_Sha384Update(Sha384*, const byte*, word32); +WOLFSSL_API int wc_Sha384Final(Sha384*, byte*); +WOLFSSL_API int wc_Sha384Hash(const byte*, word32, byte*); #ifdef HAVE_FIPS /* fips wrapper calls, user can call direct */ - CYASSL_API int InitSha512_fips(Sha512*); - CYASSL_API int Sha512Update_fips(Sha512*, const byte*, word32); - CYASSL_API int Sha512Final_fips(Sha512*, byte*); + WOLFSSL_API int wc_InitSha512_fips(Sha512*); + WOLFSSL_API int wc_Sha512Update_fips(Sha512*, const byte*, word32); + WOLFSSL_API int wc_Sha512Final_fips(Sha512*, byte*); #ifndef FIPS_NO_WRAPPERS /* if not impl or fips.c impl wrapper force fips calls if fips build */ #define InitSha512 InitSha512_fips @@ -97,9 +75,9 @@ CYASSL_API int Sha384Hash(const byte*, word32, byte*); #endif /* FIPS_NO_WRAPPERS */ /* fips wrapper calls, user can call direct */ - CYASSL_API int InitSha384_fips(Sha384*); - CYASSL_API int Sha384Update_fips(Sha384*, const byte*, word32); - CYASSL_API int Sha384Final_fips(Sha384*, byte*); + WOLFSSL_API int wc_InitSha384_fips(Sha384*); + WOLFSSL_API int wc_Sha384Update_fips(Sha384*, const byte*, word32); + WOLFSSL_API int wc_Sha384Final_fips(Sha384*, byte*); #ifndef FIPS_NO_WRAPPERS /* if not impl or fips.c impl wrapper force fips calls if fips build */ #define InitSha384 InitSha384_fips @@ -110,11 +88,11 @@ CYASSL_API int Sha384Hash(const byte*, word32, byte*); #endif /* HAVE_FIPS */ -#endif /* CYASSL_SHA384 */ +#endif /* WOLFSSL_SHA384 */ #ifdef __cplusplus } /* extern "C" */ #endif -#endif /* CTAO_CRYPT_SHA512_H */ -#endif /* CYASSL_SHA512 */ +#endif /* WOLF_CRYPT_SHA512_H */ +#endif /* WOLFSSL_SHA512 */ diff --git a/wolfssl/wolfcrypt/tfm.h b/wolfssl/wolfcrypt/tfm.h index fd825babc..2f139cc56 100644 --- a/wolfssl/wolfcrypt/tfm.h +++ b/wolfssl/wolfcrypt/tfm.h @@ -32,676 +32,681 @@ */ -#ifndef CTAO_CRYPT_TFM_H -#define CTAO_CRYPT_TFM_H - -#include -#ifndef CHAR_BIT - #include -#endif - - -#ifdef __cplusplus - extern "C" { -#endif - -#ifndef MIN - #define MIN(x,y) ((x)<(y)?(x):(y)) -#endif - -#ifndef MAX - #define MAX(x,y) ((x)>(y)?(x):(y)) -#endif - - -#ifndef NO_64BIT -/* autodetect x86-64 and make sure we are using 64-bit digits with x86-64 asm */ -#if defined(__x86_64__) - #if defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) - #error x86-64 detected, x86-32/SSE2/ARM optimizations are not valid! - #endif - #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM) - #define TFM_X86_64 - #endif -#endif -#if defined(TFM_X86_64) - #if !defined(FP_64BIT) - #define FP_64BIT - #endif -#endif -/* use 64-bit digit even if not using asm on x86_64 */ -#if defined(__x86_64__) && !defined(FP_64BIT) - #define FP_64BIT -#endif -/* if intel compiler doesn't provide 128 bit type don't turn on 64bit */ -#if defined(FP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T) - #undef FP_64BIT - #undef TFM_X86_64 -#endif -#endif /* NO_64BIT */ - -/* try to detect x86-32 */ -#if defined(__i386__) && !defined(TFM_SSE2) - #if defined(TFM_X86_64) || defined(TFM_ARM) - #error x86-32 detected, x86-64/ARM optimizations are not valid! - #endif - #if !defined(TFM_X86) && !defined(TFM_NO_ASM) - #define TFM_X86 - #endif -#endif - -/* make sure we're 32-bit for x86-32/sse/arm/ppc32 */ -#if (defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) || defined(TFM_PPC32)) && defined(FP_64BIT) - #warning x86-32, SSE2 and ARM, PPC32 optimizations require 32-bit digits (undefining) - #undef FP_64BIT -#endif - -/* multi asms? */ -#ifdef TFM_X86 - #define TFM_ASM -#endif -#ifdef TFM_X86_64 - #ifdef TFM_ASM - #error TFM_ASM already defined! - #endif - #define TFM_ASM -#endif -#ifdef TFM_SSE2 - #ifdef TFM_ASM - #error TFM_ASM already defined! - #endif - #define TFM_ASM -#endif -#ifdef TFM_ARM - #ifdef TFM_ASM - #error TFM_ASM already defined! - #endif - #define TFM_ASM -#endif -#ifdef TFM_PPC32 - #ifdef TFM_ASM - #error TFM_ASM already defined! - #endif - #define TFM_ASM -#endif -#ifdef TFM_PPC64 - #ifdef TFM_ASM - #error TFM_ASM already defined! - #endif - #define TFM_ASM -#endif -#ifdef TFM_AVR32 - #ifdef TFM_ASM - #error TFM_ASM already defined! - #endif - #define TFM_ASM -#endif - -/* we want no asm? */ -#ifdef TFM_NO_ASM - #undef TFM_X86 - #undef TFM_X86_64 - #undef TFM_SSE2 - #undef TFM_ARM - #undef TFM_PPC32 - #undef TFM_PPC64 - #undef TFM_AVR32 - #undef TFM_ASM -#endif - -/* ECC helpers */ -#ifdef TFM_ECC192 - #ifdef FP_64BIT - #define TFM_MUL3 - #define TFM_SQR3 - #else - #define TFM_MUL6 - #define TFM_SQR6 - #endif -#endif - -#ifdef TFM_ECC224 - #ifdef FP_64BIT - #define TFM_MUL4 - #define TFM_SQR4 - #else - #define TFM_MUL7 - #define TFM_SQR7 - #endif -#endif - -#ifdef TFM_ECC256 - #ifdef FP_64BIT - #define TFM_MUL4 - #define TFM_SQR4 - #else - #define TFM_MUL8 - #define TFM_SQR8 - #endif -#endif - -#ifdef TFM_ECC384 - #ifdef FP_64BIT - #define TFM_MUL6 - #define TFM_SQR6 - #else - #define TFM_MUL12 - #define TFM_SQR12 - #endif -#endif - -#ifdef TFM_ECC521 - #ifdef FP_64BIT - #define TFM_MUL9 - #define TFM_SQR9 - #else - #define TFM_MUL17 - #define TFM_SQR17 - #endif -#endif - - -/* some default configurations. - */ -#if defined(FP_64BIT) - /* for GCC only on supported platforms */ - typedef unsigned long long fp_digit; /* 64bit, 128 uses mode(TI) below */ - typedef unsigned long fp_word __attribute__ ((mode(TI))); -#else - #if defined(_MSC_VER) || defined(__BORLANDC__) - typedef unsigned __int64 ulong64; - #else - typedef unsigned long long ulong64; - #endif - - #ifndef NO_64BIT - typedef unsigned int fp_digit; - typedef ulong64 fp_word; - #define FP_32BIT - #else - /* some procs like coldfire prefer not to place multiply into 64bit type - even though it exists */ - typedef unsigned short fp_digit; - typedef unsigned int fp_word; - #endif -#endif - -/* # of digits this is */ -#define DIGIT_BIT (int)((CHAR_BIT) * sizeof(fp_digit)) - -/* Max size of any number in bits. Basically the largest size you will be - * multiplying should be half [or smaller] of FP_MAX_SIZE-four_digit - * - * It defaults to 4096-bits [allowing multiplications upto 2048x2048 bits ] - */ -#ifndef FP_MAX_BITS - #define FP_MAX_BITS 4096 -#endif -#define FP_MAX_SIZE (FP_MAX_BITS+(8*DIGIT_BIT)) - -/* will this lib work? */ -#if (CHAR_BIT & 7) - #error CHAR_BIT must be a multiple of eight. -#endif -#if FP_MAX_BITS % CHAR_BIT - #error FP_MAX_BITS must be a multiple of CHAR_BIT -#endif - -#define FP_MASK (fp_digit)(-1) -#define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT) - -/* signs */ -#define FP_ZPOS 0 -#define FP_NEG 1 - -/* return codes */ -#define FP_OKAY 0 -#define FP_VAL 1 -#define FP_MEM 2 - -/* equalities */ -#define FP_LT -1 /* less than */ -#define FP_EQ 0 /* equal to */ -#define FP_GT 1 /* greater than */ - -/* replies */ -#define FP_YES 1 /* yes response */ -#define FP_NO 0 /* no response */ - -/* a FP type */ -typedef struct { - fp_digit dp[FP_SIZE]; - int used, - sign; -} fp_int; - -/* externally define this symbol to ignore the default settings, useful for changing the build from the make process */ -#ifndef TFM_ALREADY_SET - -/* do we want the large set of small multiplications ? - Enable these if you are going to be doing a lot of small (<= 16 digit) multiplications say in ECC - Or if you're on a 64-bit machine doing RSA as a 1024-bit integer == 16 digits ;-) - */ -/* need to refactor the function */ -/*#define TFM_SMALL_SET */ - -/* do we want huge code - Enable these if you are doing 20, 24, 28, 32, 48, 64 digit multiplications (useful for RSA) - Less important on 64-bit machines as 32 digits == 2048 bits - */ -#if 0 -#define TFM_MUL3 -#define TFM_MUL4 -#define TFM_MUL6 -#define TFM_MUL7 -#define TFM_MUL8 -#define TFM_MUL9 -#define TFM_MUL12 -#define TFM_MUL17 -#endif -#ifdef TFM_HUGE_SET -#define TFM_MUL20 -#define TFM_MUL24 -#define TFM_MUL28 -#define TFM_MUL32 -#if (FP_MAX_BITS >= 6144) && defined(FP_64BIT) - #define TFM_MUL48 -#endif -#if (FP_MAX_BITS >= 8192) && defined(FP_64BIT) - #define TFM_MUL64 -#endif -#endif - -#if 0 -#define TFM_SQR3 -#define TFM_SQR4 -#define TFM_SQR6 -#define TFM_SQR7 -#define TFM_SQR8 -#define TFM_SQR9 -#define TFM_SQR12 -#define TFM_SQR17 -#endif -#ifdef TFM_HUGE_SET -#define TFM_SQR20 -#define TFM_SQR24 -#define TFM_SQR28 -#define TFM_SQR32 -#define TFM_SQR48 -#define TFM_SQR64 -#endif - -/* do we want some overflow checks - Not required if you make sure your numbers are within range (e.g. by default a modulus for fp_exptmod() can only be upto 2048 bits long) - */ -/* #define TFM_CHECK */ - -/* Is the target a P4 Prescott - */ -/* #define TFM_PRESCOTT */ - -/* Do we want timing resistant fp_exptmod() ? - * This makes it slower but also timing invariant with respect to the exponent - */ -/* #define TFM_TIMING_RESISTANT */ - -#endif /* TFM_ALREADY_SET */ - -/* functions */ - -/* returns a TFM ident string useful for debugging... */ -/*const char *fp_ident(void);*/ - -/* initialize [or zero] an fp int */ -#define fp_init(a) (void)XMEMSET((a), 0, sizeof(fp_int)) -#define fp_zero(a) fp_init(a) - -/* zero/even/odd ? */ -#define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO) -#define fp_iseven(a) (((a)->used >= 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO) -#define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO) - -/* set to a small digit */ -void fp_set(fp_int *a, fp_digit b); - -/* copy from a to b */ -#define fp_copy(a, b) (void)(((a) != (b)) ? ((void)XMEMCPY((b), (a), sizeof(fp_int))) : (void)0) -#define fp_init_copy(a, b) fp_copy(b, a) - -/* clamp digits */ -#define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; } - -/* negate and absolute */ -#define fp_neg(a, b) { fp_copy(a, b); (b)->sign ^= 1; fp_clamp(b); } -#define fp_abs(a, b) { fp_copy(a, b); (b)->sign = 0; } - -/* right shift x digits */ -void fp_rshd(fp_int *a, int x); - -/* right shift x bits */ -void fp_rshb(fp_int *a, int x); - -/* left shift x digits */ -void fp_lshd(fp_int *a, int x); - -/* signed comparison */ -int fp_cmp(fp_int *a, fp_int *b); - -/* unsigned comparison */ -int fp_cmp_mag(fp_int *a, fp_int *b); - -/* power of 2 operations */ -void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d); -void fp_mod_2d(fp_int *a, int b, fp_int *c); -void fp_mul_2d(fp_int *a, int b, fp_int *c); -void fp_2expt (fp_int *a, int b); -void fp_mul_2(fp_int *a, fp_int *c); -void fp_div_2(fp_int *a, fp_int *c); - -/* Counts the number of lsbs which are zero before the first zero bit */ -int fp_cnt_lsb(fp_int *a); - -/* c = a + b */ -void fp_add(fp_int *a, fp_int *b, fp_int *c); - -/* c = a - b */ -void fp_sub(fp_int *a, fp_int *b, fp_int *c); - -/* c = a * b */ -void fp_mul(fp_int *a, fp_int *b, fp_int *c); - -/* b = a*a */ -void fp_sqr(fp_int *a, fp_int *b); - -/* a/b => cb + d == a */ -int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d); - -/* c = a mod b, 0 <= c < b */ -int fp_mod(fp_int *a, fp_int *b, fp_int *c); - -/* compare against a single digit */ -int fp_cmp_d(fp_int *a, fp_digit b); - -/* c = a + b */ -void fp_add_d(fp_int *a, fp_digit b, fp_int *c); - -/* c = a - b */ -void fp_sub_d(fp_int *a, fp_digit b, fp_int *c); - -/* c = a * b */ -void fp_mul_d(fp_int *a, fp_digit b, fp_int *c); - -/* a/b => cb + d == a */ -/*int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);*/ - -/* c = a mod b, 0 <= c < b */ -/*int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);*/ - -/* ---> number theory <--- */ -/* d = a + b (mod c) */ -/*int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/ - -/* d = a - b (mod c) */ -/*int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/ - -/* d = a * b (mod c) */ -int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d); - -/* c = a * a (mod b) */ -int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c); - -/* c = 1/a (mod b) */ -int fp_invmod(fp_int *a, fp_int *b, fp_int *c); - -/* c = (a, b) */ -/*void fp_gcd(fp_int *a, fp_int *b, fp_int *c);*/ - -/* c = [a, b] */ -/*void fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/ - -/* setups the montgomery reduction */ -int fp_montgomery_setup(fp_int *a, fp_digit *mp); - -/* computes a = B**n mod b without division or multiplication useful for - * normalizing numbers in a Montgomery system. - */ -void fp_montgomery_calc_normalization(fp_int *a, fp_int *b); - -/* computes x/R == x (mod N) via Montgomery Reduction */ -void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp); - -/* d = a**b (mod c) */ -int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d); - -/* primality stuff */ - -/* perform a Miller-Rabin test of a to the base b and store result in "result" */ -/*void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result);*/ - -/* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime */ -/*int fp_isprime(fp_int *a);*/ - -/* Primality generation flags */ -/*#define TFM_PRIME_BBS 0x0001 */ /* BBS style prime */ -/*#define TFM_PRIME_SAFE 0x0002 */ /* Safe prime (p-1)/2 == prime */ -/*#define TFM_PRIME_2MSB_OFF 0x0004 */ /* force 2nd MSB to 0 */ -/*#define TFM_PRIME_2MSB_ON 0x0008 */ /* force 2nd MSB to 1 */ - -/* callback for fp_prime_random, should fill dst with random bytes and return how many read [upto len] */ -/*typedef int tfm_prime_callback(unsigned char *dst, int len, void *dat);*/ - -/*#define fp_prime_random(a, t, size, bbs, cb, dat) fp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?TFM_PRIME_BBS:0, cb, dat)*/ - -/*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/ - -/* radix conersions */ -int fp_count_bits(fp_int *a); -int fp_leading_bit(fp_int *a); - -int fp_unsigned_bin_size(fp_int *a); -void fp_read_unsigned_bin(fp_int *a, unsigned char *b, int c); -void fp_to_unsigned_bin(fp_int *a, unsigned char *b); - -/*int fp_signed_bin_size(fp_int *a);*/ -/*void fp_read_signed_bin(fp_int *a, unsigned char *b, int c);*/ -/*void fp_to_signed_bin(fp_int *a, unsigned char *b);*/ - -/*int fp_read_radix(fp_int *a, char *str, int radix);*/ -/*int fp_toradix(fp_int *a, char *str, int radix);*/ -/*int fp_toradix_n(fp_int * a, char *str, int radix, int maxlen);*/ - - -/* VARIOUS LOW LEVEL STUFFS */ -void s_fp_add(fp_int *a, fp_int *b, fp_int *c); -void s_fp_sub(fp_int *a, fp_int *b, fp_int *c); -void fp_reverse(unsigned char *s, int len); - -void fp_mul_comba(fp_int *a, fp_int *b, fp_int *c); - -#ifdef TFM_SMALL_SET -void fp_mul_comba_small(fp_int *a, fp_int *b, fp_int *c); -#endif - -#ifdef TFM_MUL3 -void fp_mul_comba3(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL4 -void fp_mul_comba4(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL6 -void fp_mul_comba6(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL7 -void fp_mul_comba7(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL8 -void fp_mul_comba8(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL9 -void fp_mul_comba9(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL12 -void fp_mul_comba12(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL17 -void fp_mul_comba17(fp_int *a, fp_int *b, fp_int *c); -#endif - -#ifdef TFM_MUL20 -void fp_mul_comba20(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL24 -void fp_mul_comba24(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL28 -void fp_mul_comba28(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL32 -void fp_mul_comba32(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL48 -void fp_mul_comba48(fp_int *a, fp_int *b, fp_int *c); -#endif -#ifdef TFM_MUL64 -void fp_mul_comba64(fp_int *a, fp_int *b, fp_int *c); -#endif - -void fp_sqr_comba(fp_int *a, fp_int *b); - -#ifdef TFM_SMALL_SET -void fp_sqr_comba_small(fp_int *a, fp_int *b); -#endif - -#ifdef TFM_SQR3 -void fp_sqr_comba3(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR4 -void fp_sqr_comba4(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR6 -void fp_sqr_comba6(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR7 -void fp_sqr_comba7(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR8 -void fp_sqr_comba8(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR9 -void fp_sqr_comba9(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR12 -void fp_sqr_comba12(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR17 -void fp_sqr_comba17(fp_int *a, fp_int *b); -#endif - -#ifdef TFM_SQR20 -void fp_sqr_comba20(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR24 -void fp_sqr_comba24(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR28 -void fp_sqr_comba28(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR32 -void fp_sqr_comba32(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR48 -void fp_sqr_comba48(fp_int *a, fp_int *b); -#endif -#ifdef TFM_SQR64 -void fp_sqr_comba64(fp_int *a, fp_int *b); -#endif -/*extern const char *fp_s_rmap;*/ - - -/** - * Used by CyaSSL - */ - -/* Types */ - typedef fp_digit mp_digit; - typedef fp_word mp_word; - typedef fp_int mp_int; - -/* Constants */ - #define MP_LT FP_LT /* less than */ - #define MP_EQ FP_EQ /* equal to */ - #define MP_GT FP_GT /* greater than */ - #define MP_VAL FP_VAL /* invalid */ - #define MP_OKAY FP_OKAY /* ok result */ - #define MP_NO FP_NO /* yes/no result */ - #define MP_YES FP_YES /* yes/no result */ - -/* Prototypes */ -#define mp_zero(a) fp_zero(a) -#define mp_iseven(a) fp_iseven(a) -int mp_init (mp_int * a); -void mp_clear (mp_int * a); -int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, mp_int* f); - -int mp_add (mp_int * a, mp_int * b, mp_int * c); -int mp_sub (mp_int * a, mp_int * b, mp_int * c); -int mp_add_d (mp_int * a, mp_digit b, mp_int * c); - -int mp_mul (mp_int * a, mp_int * b, mp_int * c); -int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d); -int mp_mod(mp_int *a, mp_int *b, mp_int *c); -int mp_invmod(mp_int *a, mp_int *b, mp_int *c); -int mp_exptmod (mp_int * g, mp_int * x, mp_int * p, mp_int * y); - -int mp_cmp(mp_int *a, mp_int *b); -int mp_cmp_d(mp_int *a, mp_digit b); - -int mp_unsigned_bin_size(mp_int * a); -int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c); -int mp_to_unsigned_bin (mp_int * a, unsigned char *b); - -int mp_sub_d(fp_int *a, fp_digit b, fp_int *c); -int mp_copy(fp_int* a, fp_int* b); -int mp_isodd(mp_int* a); -int mp_iszero(mp_int* a); -int mp_count_bits(mp_int *a); -int mp_leading_bit(mp_int *a); -int mp_set_int(fp_int *a, fp_digit b); -void mp_rshb(mp_int *a, int x); - -#ifdef HAVE_ECC - int mp_read_radix(mp_int* a, const char* str, int radix); - int mp_set(fp_int *a, fp_digit b); - int mp_sqr(fp_int *a, fp_int *b); - int mp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp); - int mp_montgomery_setup(fp_int *a, fp_digit *rho); - int mp_div_2(fp_int * a, fp_int * b); - int mp_init_copy(fp_int * a, fp_int * b); -#endif - -#if defined(HAVE_ECC) || defined(CYASSL_KEY_GEN) - int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c); - int mp_montgomery_calc_normalization(mp_int *a, mp_int *b); -#endif - -#ifdef CYASSL_KEY_GEN -int mp_gcd(fp_int *a, fp_int *b, fp_int *c); -int mp_lcm(fp_int *a, fp_int *b, fp_int *c); -int mp_prime_is_prime(mp_int* a, int t, int* result); -#endif /* CYASSL_KEY_GEN */ - -int mp_cnt_lsb(fp_int *a); -int mp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d); -int mp_mod_d(fp_int* a, fp_digit b, fp_digit* c); - -CYASSL_API word32 CheckRunTimeFastMath(void); - -/* If user uses RSA, DH, DSA, or ECC math lib directly then fast math FP_SIZE - must match, return 1 if a match otherwise 0 */ -#define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath()) -#ifdef __cplusplus - } -#endif - - -#endif /* CTAO_CRYPT_TFM_H */ +#ifndef WOLF_CRYPT_TFM_H +#define WOLF_CRYPT_TFM_H + +#include + +#endif + +// +//#include +//#ifndef CHAR_BIT +// #include +//#endif +// +// +//#ifdef __cplusplus +// extern "C" { +//#endif +// +//#ifndef MIN +// #define MIN(x,y) ((x)<(y)?(x):(y)) +//#endif +// +//#ifndef MAX +// #define MAX(x,y) ((x)>(y)?(x):(y)) +//#endif +// +// +//#ifndef NO_64BIT +///* autodetect x86-64 and make sure we are using 64-bit digits with x86-64 asm */ +//#if defined(__x86_64__) +// #if defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) +// #error x86-64 detected, x86-32/SSE2/ARM optimizations are not valid! +// #endif +// #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM) +// #define TFM_X86_64 +// #endif +//#endif +//#if defined(TFM_X86_64) +// #if !defined(FP_64BIT) +// #define FP_64BIT +// #endif +//#endif +///* use 64-bit digit even if not using asm on x86_64 */ +//#if defined(__x86_64__) && !defined(FP_64BIT) +// #define FP_64BIT +//#endif +///* if intel compiler doesn't provide 128 bit type don't turn on 64bit */ +//#if defined(FP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T) +// #undef FP_64BIT +// #undef TFM_X86_64 +//#endif +//#endif /* NO_64BIT */ +// +///* try to detect x86-32 */ +//#if defined(__i386__) && !defined(TFM_SSE2) +// #if defined(TFM_X86_64) || defined(TFM_ARM) +// #error x86-32 detected, x86-64/ARM optimizations are not valid! +// #endif +// #if !defined(TFM_X86) && !defined(TFM_NO_ASM) +// #define TFM_X86 +// #endif +//#endif +// +///* make sure we're 32-bit for x86-32/sse/arm/ppc32 */ +//#if (defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) || defined(TFM_PPC32)) && defined(FP_64BIT) +// #warning x86-32, SSE2 and ARM, PPC32 optimizations require 32-bit digits (undefining) +// #undef FP_64BIT +//#endif +// +///* multi asms? */ +//#ifdef TFM_X86 +// #define TFM_ASM +//#endif +//#ifdef TFM_X86_64 +// #ifdef TFM_ASM +// #error TFM_ASM already defined! +// #endif +// #define TFM_ASM +//#endif +//#ifdef TFM_SSE2 +// #ifdef TFM_ASM +// #error TFM_ASM already defined! +// #endif +// #define TFM_ASM +//#endif +//#ifdef TFM_ARM +// #ifdef TFM_ASM +// #error TFM_ASM already defined! +// #endif +// #define TFM_ASM +//#endif +//#ifdef TFM_PPC32 +// #ifdef TFM_ASM +// #error TFM_ASM already defined! +// #endif +// #define TFM_ASM +//#endif +//#ifdef TFM_PPC64 +// #ifdef TFM_ASM +// #error TFM_ASM already defined! +// #endif +// #define TFM_ASM +//#endif +//#ifdef TFM_AVR32 +// #ifdef TFM_ASM +// #error TFM_ASM already defined! +// #endif +// #define TFM_ASM +//#endif +// +///* we want no asm? */ +//#ifdef TFM_NO_ASM +// #undef TFM_X86 +// #undef TFM_X86_64 +// #undef TFM_SSE2 +// #undef TFM_ARM +// #undef TFM_PPC32 +// #undef TFM_PPC64 +// #undef TFM_AVR32 +// #undef TFM_ASM +//#endif +// +///* ECC helpers */ +//#ifdef TFM_ECC192 +// #ifdef FP_64BIT +// #define TFM_MUL3 +// #define TFM_SQR3 +// #else +// #define TFM_MUL6 +// #define TFM_SQR6 +// #endif +//#endif +// +//#ifdef TFM_ECC224 +// #ifdef FP_64BIT +// #define TFM_MUL4 +// #define TFM_SQR4 +// #else +// #define TFM_MUL7 +// #define TFM_SQR7 +// #endif +//#endif +// +//#ifdef TFM_ECC256 +// #ifdef FP_64BIT +// #define TFM_MUL4 +// #define TFM_SQR4 +// #else +// #define TFM_MUL8 +// #define TFM_SQR8 +// #endif +//#endif +// +//#ifdef TFM_ECC384 +// #ifdef FP_64BIT +// #define TFM_MUL6 +// #define TFM_SQR6 +// #else +// #define TFM_MUL12 +// #define TFM_SQR12 +// #endif +//#endif +// +//#ifdef TFM_ECC521 +// #ifdef FP_64BIT +// #define TFM_MUL9 +// #define TFM_SQR9 +// #else +// #define TFM_MUL17 +// #define TFM_SQR17 +// #endif +//#endif +// +// +///* some default configurations. +// */ +//#if defined(FP_64BIT) +// /* for GCC only on supported platforms */ +// typedef unsigned long long fp_digit; /* 64bit, 128 uses mode(TI) below */ +// typedef unsigned long fp_word __attribute__ ((mode(TI))); +//#else +// #if defined(_MSC_VER) || defined(__BORLANDC__) +// typedef unsigned __int64 ulong64; +// #else +// typedef unsigned long long ulong64; +// #endif +// +// #ifndef NO_64BIT +// typedef unsigned int fp_digit; +// typedef ulong64 fp_word; +// #define FP_32BIT +// #else +// /* some procs like coldfire prefer not to place multiply into 64bit type +// even though it exists */ +// typedef unsigned short fp_digit; +// typedef unsigned int fp_word; +// #endif +//#endif +// +///* # of digits this is */ +//#define DIGIT_BIT (int)((CHAR_BIT) * sizeof(fp_digit)) +// +///* Max size of any number in bits. Basically the largest size you will be +// * multiplying should be half [or smaller] of FP_MAX_SIZE-four_digit +// * +// * It defaults to 4096-bits [allowing multiplications upto 2048x2048 bits ] +// */ +//#ifndef FP_MAX_BITS +// #define FP_MAX_BITS 4096 +//#endif +//#define FP_MAX_SIZE (FP_MAX_BITS+(8*DIGIT_BIT)) +// +///* will this lib work? */ +//#if (CHAR_BIT & 7) +// #error CHAR_BIT must be a multiple of eight. +//#endif +//#if FP_MAX_BITS % CHAR_BIT +// #error FP_MAX_BITS must be a multiple of CHAR_BIT +//#endif +// +//#define FP_MASK (fp_digit)(-1) +//#define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT) +// +///* signs */ +//#define FP_ZPOS 0 +//#define FP_NEG 1 +// +///* return codes */ +//#define FP_OKAY 0 +//#define FP_VAL 1 +//#define FP_MEM 2 +// +///* equalities */ +//#define FP_LT -1 /* less than */ +//#define FP_EQ 0 /* equal to */ +//#define FP_GT 1 /* greater than */ +// +///* replies */ +//#define FP_YES 1 /* yes response */ +//#define FP_NO 0 /* no response */ +// +///* a FP type */ +//typedef struct { +// fp_digit dp[FP_SIZE]; +// int used, +// sign; +//} fp_int; +// +///* externally define this symbol to ignore the default settings, useful for changing the build from the make process */ +//#ifndef TFM_ALREADY_SET +// +///* do we want the large set of small multiplications ? +// Enable these if you are going to be doing a lot of small (<= 16 digit) multiplications say in ECC +// Or if you're on a 64-bit machine doing RSA as a 1024-bit integer == 16 digits ;-) +// */ +///* need to refactor the function */ +///*#define TFM_SMALL_SET */ +// +///* do we want huge code +// Enable these if you are doing 20, 24, 28, 32, 48, 64 digit multiplications (useful for RSA) +// Less important on 64-bit machines as 32 digits == 2048 bits +// */ +//#if 0 +//#define TFM_MUL3 +//#define TFM_MUL4 +//#define TFM_MUL6 +//#define TFM_MUL7 +//#define TFM_MUL8 +//#define TFM_MUL9 +//#define TFM_MUL12 +//#define TFM_MUL17 +//#endif +//#ifdef TFM_HUGE_SET +//#define TFM_MUL20 +//#define TFM_MUL24 +//#define TFM_MUL28 +//#define TFM_MUL32 +//#if (FP_MAX_BITS >= 6144) && defined(FP_64BIT) +// #define TFM_MUL48 +//#endif +//#if (FP_MAX_BITS >= 8192) && defined(FP_64BIT) +// #define TFM_MUL64 +//#endif +//#endif +// +//#if 0 +//#define TFM_SQR3 +//#define TFM_SQR4 +//#define TFM_SQR6 +//#define TFM_SQR7 +//#define TFM_SQR8 +//#define TFM_SQR9 +//#define TFM_SQR12 +//#define TFM_SQR17 +//#endif +//#ifdef TFM_HUGE_SET +//#define TFM_SQR20 +//#define TFM_SQR24 +//#define TFM_SQR28 +//#define TFM_SQR32 +//#define TFM_SQR48 +//#define TFM_SQR64 +//#endif +// +///* do we want some overflow checks +// Not required if you make sure your numbers are within range (e.g. by default a modulus for fp_exptmod() can only be upto 2048 bits long) +// */ +///* #define TFM_CHECK */ +// +///* Is the target a P4 Prescott +// */ +///* #define TFM_PRESCOTT */ +// +///* Do we want timing resistant fp_exptmod() ? +// * This makes it slower but also timing invariant with respect to the exponent +// */ +///* #define TFM_TIMING_RESISTANT */ +// +//#endif /* TFM_ALREADY_SET */ +// +///* functions */ +// +///* returns a TFM ident string useful for debugging... */ +///*const char *fp_ident(void);*/ +// +///* initialize [or zero] an fp int */ +//#define fp_init(a) (void)XMEMSET((a), 0, sizeof(fp_int)) +//#define fp_zero(a) fp_init(a) +// +///* zero/even/odd ? */ +//#define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO) +//#define fp_iseven(a) (((a)->used >= 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO) +//#define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO) +// +///* set to a small digit */ +//void fp_set(fp_int *a, fp_digit b); +// +///* copy from a to b */ +//#define fp_copy(a, b) (void)(((a) != (b)) ? ((void)XMEMCPY((b), (a), sizeof(fp_int))) : (void)0) +//#define fp_init_copy(a, b) fp_copy(b, a) +// +///* clamp digits */ +//#define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; } +// +///* negate and absolute */ +//#define fp_neg(a, b) { fp_copy(a, b); (b)->sign ^= 1; fp_clamp(b); } +//#define fp_abs(a, b) { fp_copy(a, b); (b)->sign = 0; } +// +///* right shift x digits */ +//void fp_rshd(fp_int *a, int x); +// +///* right shift x bits */ +//void fp_rshb(fp_int *a, int x); +// +///* left shift x digits */ +//void fp_lshd(fp_int *a, int x); +// +///* signed comparison */ +//int fp_cmp(fp_int *a, fp_int *b); +// +///* unsigned comparison */ +//int fp_cmp_mag(fp_int *a, fp_int *b); +// +///* power of 2 operations */ +//void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d); +//void fp_mod_2d(fp_int *a, int b, fp_int *c); +//void fp_mul_2d(fp_int *a, int b, fp_int *c); +//void fp_2expt (fp_int *a, int b); +//void fp_mul_2(fp_int *a, fp_int *c); +//void fp_div_2(fp_int *a, fp_int *c); +// +///* Counts the number of lsbs which are zero before the first zero bit */ +//int fp_cnt_lsb(fp_int *a); +// +///* c = a + b */ +//void fp_add(fp_int *a, fp_int *b, fp_int *c); +// +///* c = a - b */ +//void fp_sub(fp_int *a, fp_int *b, fp_int *c); +// +///* c = a * b */ +//void fp_mul(fp_int *a, fp_int *b, fp_int *c); +// +///* b = a*a */ +//void fp_sqr(fp_int *a, fp_int *b); +// +///* a/b => cb + d == a */ +//int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d); +// +///* c = a mod b, 0 <= c < b */ +//int fp_mod(fp_int *a, fp_int *b, fp_int *c); +// +///* compare against a single digit */ +//int fp_cmp_d(fp_int *a, fp_digit b); +// +///* c = a + b */ +//void fp_add_d(fp_int *a, fp_digit b, fp_int *c); +// +///* c = a - b */ +//void fp_sub_d(fp_int *a, fp_digit b, fp_int *c); +// +///* c = a * b */ +//void fp_mul_d(fp_int *a, fp_digit b, fp_int *c); +// +///* a/b => cb + d == a */ +///*int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);*/ +// +///* c = a mod b, 0 <= c < b */ +///*int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);*/ +// +///* ---> number theory <--- */ +///* d = a + b (mod c) */ +///*int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/ +// +///* d = a - b (mod c) */ +///*int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/ +// +///* d = a * b (mod c) */ +//int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d); +// +///* c = a * a (mod b) */ +//int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c); +// +///* c = 1/a (mod b) */ +//int fp_invmod(fp_int *a, fp_int *b, fp_int *c); +// +///* c = (a, b) */ +///*void fp_gcd(fp_int *a, fp_int *b, fp_int *c);*/ +// +///* c = [a, b] */ +///*void fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/ +// +///* setups the montgomery reduction */ +//int fp_montgomery_setup(fp_int *a, fp_digit *mp); +// +///* computes a = B**n mod b without division or multiplication useful for +// * normalizing numbers in a Montgomery system. +// */ +//void fp_montgomery_calc_normalization(fp_int *a, fp_int *b); +// +///* computes x/R == x (mod N) via Montgomery Reduction */ +//void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp); +// +///* d = a**b (mod c) */ +//int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d); +// +///* primality stuff */ +// +///* perform a Miller-Rabin test of a to the base b and store result in "result" */ +///*void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result);*/ +// +///* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime */ +///*int fp_isprime(fp_int *a);*/ +// +///* Primality generation flags */ +///*#define TFM_PRIME_BBS 0x0001 */ /* BBS style prime */ +///*#define TFM_PRIME_SAFE 0x0002 */ /* Safe prime (p-1)/2 == prime */ +///*#define TFM_PRIME_2MSB_OFF 0x0004 */ /* force 2nd MSB to 0 */ +///*#define TFM_PRIME_2MSB_ON 0x0008 */ /* force 2nd MSB to 1 */ +// +///* callback for fp_prime_random, should fill dst with random bytes and return how many read [upto len] */ +///*typedef int tfm_prime_callback(unsigned char *dst, int len, void *dat);*/ +// +///*#define fp_prime_random(a, t, size, bbs, cb, dat) fp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?TFM_PRIME_BBS:0, cb, dat)*/ +// +///*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/ +// +///* radix conersions */ +//int fp_count_bits(fp_int *a); +//int fp_leading_bit(fp_int *a); +// +//int fp_unsigned_bin_size(fp_int *a); +//void fp_read_unsigned_bin(fp_int *a, unsigned char *b, int c); +//void fp_to_unsigned_bin(fp_int *a, unsigned char *b); +// +///*int fp_signed_bin_size(fp_int *a);*/ +///*void fp_read_signed_bin(fp_int *a, unsigned char *b, int c);*/ +///*void fp_to_signed_bin(fp_int *a, unsigned char *b);*/ +// +///*int fp_read_radix(fp_int *a, char *str, int radix);*/ +///*int fp_toradix(fp_int *a, char *str, int radix);*/ +///*int fp_toradix_n(fp_int * a, char *str, int radix, int maxlen);*/ +// +// +///* VARIOUS LOW LEVEL STUFFS */ +//void s_fp_add(fp_int *a, fp_int *b, fp_int *c); +//void s_fp_sub(fp_int *a, fp_int *b, fp_int *c); +//void fp_reverse(unsigned char *s, int len); +// +//void fp_mul_comba(fp_int *a, fp_int *b, fp_int *c); +// +//#ifdef TFM_SMALL_SET +//void fp_mul_comba_small(fp_int *a, fp_int *b, fp_int *c); +//#endif +// +//#ifdef TFM_MUL3 +//void fp_mul_comba3(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL4 +//void fp_mul_comba4(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL6 +//void fp_mul_comba6(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL7 +//void fp_mul_comba7(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL8 +//void fp_mul_comba8(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL9 +//void fp_mul_comba9(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL12 +//void fp_mul_comba12(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL17 +//void fp_mul_comba17(fp_int *a, fp_int *b, fp_int *c); +//#endif +// +//#ifdef TFM_MUL20 +//void fp_mul_comba20(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL24 +//void fp_mul_comba24(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL28 +//void fp_mul_comba28(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL32 +//void fp_mul_comba32(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL48 +//void fp_mul_comba48(fp_int *a, fp_int *b, fp_int *c); +//#endif +//#ifdef TFM_MUL64 +//void fp_mul_comba64(fp_int *a, fp_int *b, fp_int *c); +//#endif +// +//void fp_sqr_comba(fp_int *a, fp_int *b); +// +//#ifdef TFM_SMALL_SET +//void fp_sqr_comba_small(fp_int *a, fp_int *b); +//#endif +// +//#ifdef TFM_SQR3 +//void fp_sqr_comba3(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR4 +//void fp_sqr_comba4(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR6 +//void fp_sqr_comba6(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR7 +//void fp_sqr_comba7(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR8 +//void fp_sqr_comba8(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR9 +//void fp_sqr_comba9(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR12 +//void fp_sqr_comba12(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR17 +//void fp_sqr_comba17(fp_int *a, fp_int *b); +//#endif +// +//#ifdef TFM_SQR20 +//void fp_sqr_comba20(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR24 +//void fp_sqr_comba24(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR28 +//void fp_sqr_comba28(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR32 +//void fp_sqr_comba32(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR48 +//void fp_sqr_comba48(fp_int *a, fp_int *b); +//#endif +//#ifdef TFM_SQR64 +//void fp_sqr_comba64(fp_int *a, fp_int *b); +//#endif +///*extern const char *fp_s_rmap;*/ +// +// +///** +// * Used by CyaSSL +// */ +// +///* Types */ +// typedef fp_digit mp_digit; +// typedef fp_word mp_word; +// typedef fp_int mp_int; +// +///* Constants */ +// #define MP_LT FP_LT /* less than */ +// #define MP_EQ FP_EQ /* equal to */ +// #define MP_GT FP_GT /* greater than */ +// #define MP_VAL FP_VAL /* invalid */ +// #define MP_OKAY FP_OKAY /* ok result */ +// #define MP_NO FP_NO /* yes/no result */ +// #define MP_YES FP_YES /* yes/no result */ +// +///* Prototypes */ +//#define mp_zero(a) fp_zero(a) +//#define mp_iseven(a) fp_iseven(a) +//int mp_init (mp_int * a); +//void mp_clear (mp_int * a); +//int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, mp_int* f); +// +//int mp_add (mp_int * a, mp_int * b, mp_int * c); +//int mp_sub (mp_int * a, mp_int * b, mp_int * c); +//int mp_add_d (mp_int * a, mp_digit b, mp_int * c); +// +//int mp_mul (mp_int * a, mp_int * b, mp_int * c); +//int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d); +//int mp_mod(mp_int *a, mp_int *b, mp_int *c); +//int mp_invmod(mp_int *a, mp_int *b, mp_int *c); +//int mp_exptmod (mp_int * g, mp_int * x, mp_int * p, mp_int * y); +// +//int mp_cmp(mp_int *a, mp_int *b); +//int mp_cmp_d(mp_int *a, mp_digit b); +// +//int mp_unsigned_bin_size(mp_int * a); +//int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c); +//int mp_to_unsigned_bin (mp_int * a, unsigned char *b); +// +//int mp_sub_d(fp_int *a, fp_digit b, fp_int *c); +//int mp_copy(fp_int* a, fp_int* b); +//int mp_isodd(mp_int* a); +//int mp_iszero(mp_int* a); +//int mp_count_bits(mp_int *a); +//int mp_leading_bit(mp_int *a); +//int mp_set_int(fp_int *a, fp_digit b); +//void mp_rshb(mp_int *a, int x); +// +//#ifdef HAVE_ECC +// int mp_read_radix(mp_int* a, const char* str, int radix); +// int mp_set(fp_int *a, fp_digit b); +// int mp_sqr(fp_int *a, fp_int *b); +// int mp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp); +// int mp_montgomery_setup(fp_int *a, fp_digit *rho); +// int mp_div_2(fp_int * a, fp_int * b); +// int mp_init_copy(fp_int * a, fp_int * b); +//#endif +// +//#if defined(HAVE_ECC) || defined(CYASSL_KEY_GEN) +// int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c); +// int mp_montgomery_calc_normalization(mp_int *a, mp_int *b); +//#endif +// +//#ifdef CYASSL_KEY_GEN +//int mp_gcd(fp_int *a, fp_int *b, fp_int *c); +//int mp_lcm(fp_int *a, fp_int *b, fp_int *c); +//int mp_prime_is_prime(mp_int* a, int t, int* result); +//#endif /* CYASSL_KEY_GEN */ +// +//int mp_cnt_lsb(fp_int *a); +//int mp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d); +//int mp_mod_d(fp_int* a, fp_digit b, fp_digit* c); +// +//CYASSL_API word32 CheckRunTimeFastMath(void); +// +///* If user uses RSA, DH, DSA, or ECC math lib directly then fast math FP_SIZE +// must match, return 1 if a match otherwise 0 */ +//#define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath()) +//#ifdef __cplusplus +// } +//#endif +// +// +//#endif /* CTAO_CRYPT_TFM_H */