From ae9265fa44237d2abb6b459727e164afda819948 Mon Sep 17 00:00:00 2001 From: toddouska Date: Wed, 20 Mar 2013 14:37:05 -0700 Subject: [PATCH] add macpi huffman --- configure.ac | 47 ++++++++------- cyassl/ctaocrypt/compress.h | 2 +- mcapi/crypto.c | 20 +++++++ mcapi/crypto.h | 10 ++++ mcapi/test.c | 113 +++++++++++++++++++++++++++++++++++- 5 files changed, 170 insertions(+), 22 deletions(-) diff --git a/configure.ac b/configure.ac index 4431d7b6c..4c89fb3e0 100644 --- a/configure.ac +++ b/configure.ac @@ -1022,26 +1022,6 @@ fi AM_CONDITIONAL([USE_VALGRIND], [test "x$ENABLED_VALGRIND" = "xyes"]) -# microchip api -AC_ARG_ENABLE([mcapi], - [ --enable-mcapi Enable Microchip API (default: disabled)], - [ ENABLED_MCAPI=$enableval ], - [ ENABLED_MCAPI=no ] - ) - -if test "$ENABLED_MCAPI" = "yes" -then - AM_CFLAGS="$AM_CFLAGS -DHAVE_MCAPI" -fi - -if test "$ENABLED_MCAPI" = "yes" && test "$ENABLED_SHA512" = "no" -then - AC_MSG_ERROR([please enable sha512 if enabling mcapi.]) -fi - -AM_CONDITIONAL([BUILD_MCAPI], [test "x$ENABLED_MCAPI" = "xyes"]) - - # Test certs, use internal cert functions for extra testing AC_ARG_ENABLE([testcert], [ --enable-testcert Enable Test Cert (default: disabled)], @@ -1114,6 +1094,7 @@ AM_CONDITIONAL([BUILD_EXAMPLES], [test "x$ENABLED_EXAMPLES" = "xyes"]) # LIBZ +ENABLED_LIBZ="no" trylibzdir="" AC_ARG_WITH([libz], [ --with-libz=PATH PATH to libz install (default /usr/) ], @@ -1181,6 +1162,32 @@ AC_ARG_WITH([cavium], ) +# microchip api +AC_ARG_ENABLE([mcapi], + [ --enable-mcapi Enable Microchip API (default: disabled)], + [ ENABLED_MCAPI=$enableval ], + [ ENABLED_MCAPI=no ] + ) + +if test "$ENABLED_MCAPI" = "yes" +then + AM_CFLAGS="$AM_CFLAGS -DHAVE_MCAPI" +fi + +if test "$ENABLED_MCAPI" = "yes" && test "$ENABLED_SHA512" = "no" +then + AC_MSG_ERROR([please enable sha512 if enabling mcapi.]) +fi + +if test "$ENABLED_MCAPI" = "yes" && test "$ENABLED_LIBZ" = "no" +then + AC_MSG_ERROR([please use --with-libz if enabling mcapi.]) +fi + +AM_CONDITIONAL([BUILD_MCAPI], [test "x$ENABLED_MCAPI" = "xyes"]) + + + # OPTIMIZE FLAGS if test "$GCC" = "yes" then diff --git a/cyassl/ctaocrypt/compress.h b/cyassl/ctaocrypt/compress.h index 8d2a026ca..60ebed7fd 100644 --- a/cyassl/ctaocrypt/compress.h +++ b/cyassl/ctaocrypt/compress.h @@ -34,7 +34,7 @@ #endif -#define COMPRESS_FIXED 4 +#define COMPRESS_FIXED 1 CYASSL_API int Compress(byte*, word32, const byte*, word32, word32); diff --git a/mcapi/crypto.c b/mcapi/crypto.c index 710d11ca8..2926d9b82 100644 --- a/mcapi/crypto.c +++ b/mcapi/crypto.c @@ -31,6 +31,7 @@ #include #include #include +#include /* Initialize MD5 */ @@ -224,3 +225,22 @@ int CRYPT_HMAC_Finalize(CRYPT_HMAC_CTX* hmac, unsigned char* digest) } +/* Huffman Compression, set flag to do static, otherwise dynamic */ +/* return compressed size, otherwise < 0 for error */ +int CRYPT_HUFFMAN_Compress(unsigned char* out, unsigned int outSz, + const unsigned char* in, unsigned int inSz, + unsigned int flags) +{ + return Compress(out, outSz, in, inSz, flags); +} + + +/* Huffman DeCompression, self determines type */ +/* return decompressed size, otherwise < 0 for error */ +int CRYPT_HUFFMAN_DeCompress(unsigned char* out, unsigned int outSz, + const unsigned char* in, unsigned int inSz) +{ + return DeCompress(out, outSz, in, inSz); +} + + diff --git a/mcapi/crypto.h b/mcapi/crypto.h index 1cb1654d3..c748190fa 100644 --- a/mcapi/crypto.h +++ b/mcapi/crypto.h @@ -119,6 +119,16 @@ enum { }; +/* Huffman */ +int CRYPT_HUFFMAN_Compress(unsigned char*, unsigned int, const unsigned char*, + unsigned int, unsigned int); +int CRYPT_HUFFMAN_DeCompress(unsigned char*, unsigned int, const unsigned char*, + unsigned int); + +enum { + CRYPT_HUFFMAN_COMPRESS_STATIC = 1 +}; + #ifdef __cplusplus diff --git a/mcapi/test.c b/mcapi/test.c index 764b07627..cf2bd01a8 100644 --- a/mcapi/test.c +++ b/mcapi/test.c @@ -33,6 +33,7 @@ #include #include #include +#include /* c stdlib headers */ #include @@ -48,6 +49,7 @@ static int check_sha256(void); static int check_sha384(void); static int check_sha512(void); static int check_hmac(void); +static int check_compress(void); int main(int argc, char** argv) @@ -59,7 +61,7 @@ int main(int argc, char** argv) (void)argv; /* align key pointer */ - key = (byte*)malloc(128); + key = (byte*)XMALLOC(32, NULL, DYNAMIC_TYPE_KEY); if (key == NULL) { printf("mcapi key alloc failed\n"); return -1; @@ -104,8 +106,16 @@ int main(int argc, char** argv) return -1; } + ret = check_compress(); + if (ret != 0) { + printf("mcapi check_comopress failed\n"); + return -1; + } + + XFREE(key, NULL, DYNAMIC_TYPE_KEY); + return 0; } @@ -323,3 +333,104 @@ static int check_hmac(void) } +/* check mcapi compress against internal */ +static int check_compress(void) +{ + const unsigned char text[] = + "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n" + "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n" + "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n" + "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n" + "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n" + "small batch meggings kogi dolore food truck bespoke gastropub.\n" + "\n" + "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n" + "four loko you probably haven't heard of them high life. Messenger bag\n" + "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n" + "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n" + "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n" + "food truck next level, tousled irony non semiotics PBR ethical anim cred\n" + "readymade. Mumblecore brunch lomo odd future, portland organic terry\n" + "four loko whatever street art yr farm-to-table.\n"; + + unsigned int inSz = sizeof(text); + unsigned int outSz; + unsigned char cBuffer[1024]; + unsigned char dBuffer[1024]; + + int ret1, ret2; + + /* dynamic */ + ret1 = CRYPT_HUFFMAN_Compress(cBuffer, sizeof(cBuffer), text, inSz, 0); + ret2 = Compress(dBuffer, sizeof(dBuffer), text, inSz, 0); + + if (ret1 != ret2 || ret1 < 0) { + printf("compress dynamic ret failed\n"); + return -1; + } + + if (memcmp(cBuffer, dBuffer, ret1) != 0) { + printf("compress dynamic cmp failed\n"); + return -1; + } + + outSz = ret1; + + ret1 = CRYPT_HUFFMAN_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz); + + if (memcmp(dBuffer, text, inSz) != 0) { + printf("mcapi decompress dynamic cmp failed\n"); + return -1; + } + + memset(dBuffer, 0, sizeof(dBuffer)); + + ret1 = DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz); + + if (memcmp(dBuffer, text, inSz) != 0) { + printf("decompress dynamic cmp failed\n"); + return -1; + } + + memset(cBuffer, 0, sizeof(cBuffer)); + memset(dBuffer, 0, sizeof(dBuffer)); + + /* static */ + ret1 = CRYPT_HUFFMAN_Compress(cBuffer, sizeof(cBuffer), text, inSz, 1); + ret2 = Compress(dBuffer, sizeof(dBuffer), text, inSz, 1); + + if (ret1 != ret2 || ret1 < 0) { + printf("compress static ret failed\n"); + return -1; + } + + if (memcmp(cBuffer, dBuffer, ret1) != 0) { + printf("compress static cmp failed\n"); + return -1; + } + + outSz = ret1; + + ret1 = CRYPT_HUFFMAN_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz); + + if (memcmp(dBuffer, text, inSz) != 0) { + printf("mcapi decompress static cmp failed\n"); + return -1; + } + + memset(dBuffer, 0, sizeof(dBuffer)); + + ret1 = DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz); + + if (memcmp(dBuffer, text, inSz) != 0) { + printf("decompress static cmp failed\n"); + return -1; + } + + printf("huffman mcapi test passed\n"); + + return 0; +} + + +