From 20a7808d130d986b5fa3c8543df03ffa874c7300 Mon Sep 17 00:00:00 2001
From: Paul Bakker
Date: Fri, 21 Jan 2011 09:32:12 +0000
Subject: [PATCH] - Addec crypt_and_hash example program of the generic hash
and cipher layers
---
ChangeLog | 1 +
programs/Makefile | 7 +-
programs/aes/CMakeLists.txt | 5 +-
programs/aes/aescrypt2.c | 4 +-
programs/aes/crypt_and_hash.c | 462 ++++++++++++++++++++++++++++++++++
5 files changed, 476 insertions(+), 3 deletions(-)
create mode 100644 programs/aes/crypt_and_hash.c
diff --git a/ChangeLog b/ChangeLog
index d9ccc6b82..db25d3c6f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -17,6 +17,7 @@ Note: Most of these features have been donated by Fox-IT
status, objects and configuration
+ Added verification callback on certificate chain
verification to allow external blacklisting
+ + Additional example programs to show usage
* Added support for PKCS#11 through the use of the
libpkcs11-helper library
diff --git a/programs/Makefile b/programs/Makefile
index 696ba176e..7380ad2ee 100644
--- a/programs/Makefile
+++ b/programs/Makefile
@@ -7,7 +7,8 @@ CFLAGS = -I../include -D_FILE_OFFSET_BITS=64 -Wall -W -Wdeclaration-after-statem
OFLAGS = -O
LDFLAGS = -L../library -lpolarssl
-APPS = aes/aescrypt2 hash/hello \
+APPS = aes/aescrypt2 aes/crypt_and_hash \
+ hash/hello \
hash/md5sum hash/sha1sum \
hash/sha2sum pkey/dh_client \
pkey/dh_genprime pkey/dh_server \
@@ -26,6 +27,10 @@ aes/aescrypt2: aes/aescrypt2.c ../library/libpolarssl.a
echo " CC aes/aescrypt2.c"
$(CC) $(CFLAGS) $(OFLAGS) aes/aescrypt2.c $(LDFLAGS) -o $@
+aes/crypt_and_hash: aes/crypt_and_hash.c ../library/libpolarssl.a
+ echo " CC aes/crypt_and_hash.c"
+ $(CC) $(CFLAGS) $(OFLAGS) aes/crypt_and_hash.c $(LDFLAGS) -o $@
+
hash/hello: hash/hello.c ../library/libpolarssl.a
echo " CC hash/hello.c"
$(CC) $(CFLAGS) $(OFLAGS) hash/hello.c $(LDFLAGS) -o $@
diff --git a/programs/aes/CMakeLists.txt b/programs/aes/CMakeLists.txt
index 52b5e1223..0695a345f 100644
--- a/programs/aes/CMakeLists.txt
+++ b/programs/aes/CMakeLists.txt
@@ -1,6 +1,9 @@
add_executable(aescrypt2 aescrypt2.c)
target_link_libraries(aescrypt2 polarssl)
-INSTALL(TARGETS aescrypt2
+add_executable(crypt_and_hash crypt_and_hash.c)
+target_link_libraries(crypt_and_hash polarssl)
+
+INSTALL(TARGETS aescrypt2 crypt_and_hash
DESTINATION "bin"
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
diff --git a/programs/aes/aescrypt2.c b/programs/aes/aescrypt2.c
index 1988ec669..c291c1828 100644
--- a/programs/aes/aescrypt2.c
+++ b/programs/aes/aescrypt2.c
@@ -56,7 +56,7 @@ int main( int argc, char *argv[] )
{
int ret = 1, i, n;
int keylen, mode, lastn;
- FILE *fkey, *fin, *fout;
+ FILE *fkey, *fin = NULL, *fout = NULL;
char *p;
unsigned char IV[16];
@@ -390,6 +390,8 @@ int main( int argc, char *argv[] )
ret = 0;
exit:
+ fclose( fin );
+ fclose( fout );
memset( buffer, 0, sizeof( buffer ) );
memset( digest, 0, sizeof( digest ) );
diff --git a/programs/aes/crypt_and_hash.c b/programs/aes/crypt_and_hash.c
new file mode 100644
index 000000000..7014af0bd
--- /dev/null
+++ b/programs/aes/crypt_and_hash.c
@@ -0,0 +1,462 @@
+/*
+ * \brief Generic file encryption program using generic wrappers for configured
+ * security.
+ *
+ * Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ * This file is part of PolarSSL (http://www.polarssl.org)
+ * Lead Maintainer: Paul Bakker
+ *
+ * All rights reserved.
+ *
+ * This program 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.
+ *
+ * This program 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.
+ */
+
+#ifndef _CRT_SECURE_NO_DEPRECATE
+#define _CRT_SECURE_NO_DEPRECATE 1
+#endif
+
+#if defined(WIN32)
+#include
+#include
+#else
+#include
+#include
+#endif
+
+#include
+#include
+#include
+#include
+
+#include "polarssl/cipher.h"
+#include "polarssl/md.h"
+
+#define MODE_ENCRYPT 0
+#define MODE_DECRYPT 1
+
+#define USAGE \
+ "\n crypt_and_hash