mirror of
https://github.com/izzy2lost/dolphin.git
synced 2026-06-19 01:16:48 -07:00
Support PolarSSL on *nix
Removed unneeded PolarSSL directories. Remove duplicate sha1 and md5 files. Define out SSL test file writing. Fix format string bug and warning.
This commit is contained in:
Vendored
+1
-12
@@ -1,8 +1,6 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
project(POLARSSL C)
|
||||
|
||||
enable_testing()
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCC)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -Wall -Wextra -W -Wdeclaration-after-statement")
|
||||
set(CMAKE_C_FLAGS_DEBUG "-g3 -O0")
|
||||
@@ -35,14 +33,5 @@ if(ENABLE_ZLIB_SUPPORT)
|
||||
endif(ENABLE_ZLIB_SUPPORT)
|
||||
|
||||
add_subdirectory(library)
|
||||
add_subdirectory(include)
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCC)
|
||||
add_subdirectory(tests)
|
||||
endif(CMAKE_COMPILER_IS_GNUCC)
|
||||
|
||||
add_subdirectory(programs)
|
||||
|
||||
ADD_CUSTOM_TARGET(apidoc
|
||||
COMMAND doxygen doxygen/polarssl.doxyfile
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
option(INSTALL_POLARSSL_HEADERS "Install PolarSSL headers." ON)
|
||||
|
||||
if(INSTALL_POLARSSL_HEADERS)
|
||||
|
||||
file(GLOB headers "polarssl/*.h")
|
||||
|
||||
install(FILES ${headers}
|
||||
DESTINATION include/polarssl
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
|
||||
|
||||
endif(INSTALL_POLARSSL_HEADERS)
|
||||
@@ -1,39 +0,0 @@
|
||||
*/Makefile
|
||||
aes/aescrypt2
|
||||
aes/crypt_and_hash
|
||||
hash/generic_sum
|
||||
hash/hello
|
||||
hash/md5sum
|
||||
hash/sha1sum
|
||||
hash/sha2sum
|
||||
pkey/dh_client
|
||||
pkey/dh_genprime
|
||||
pkey/dh_server
|
||||
pkey/key_app
|
||||
pkey/key_app_writer
|
||||
pkey/mpi_demo
|
||||
pkey/rsa_decrypt
|
||||
pkey/rsa_encrypt
|
||||
pkey/rsa_genkey
|
||||
pkey/rsa_sign
|
||||
pkey/rsa_sign_pss
|
||||
pkey/rsa_verify
|
||||
pkey/rsa_verify_pss
|
||||
random/gen_entropy
|
||||
random/gen_random_ctr_drbg
|
||||
random/gen_random_havege
|
||||
ssl/ssl_client1
|
||||
ssl/ssl_client2
|
||||
ssl/ssl_fork_server
|
||||
ssl/ssl_mail_client
|
||||
ssl/ssl_server
|
||||
ssl/ssl_server2
|
||||
test/benchmark
|
||||
test/o_p_test
|
||||
test/selftest
|
||||
test/ssl_cert_test
|
||||
test/ssl_test
|
||||
util/strerror
|
||||
x509/cert_app
|
||||
x509/cert_req
|
||||
x509/crl_app
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
add_subdirectory(aes)
|
||||
add_subdirectory(hash)
|
||||
add_subdirectory(pkey)
|
||||
add_subdirectory(random)
|
||||
add_subdirectory(ssl)
|
||||
add_subdirectory(test)
|
||||
add_subdirectory(x509)
|
||||
add_subdirectory(util)
|
||||
Vendored
-211
@@ -1,211 +0,0 @@
|
||||
|
||||
# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS
|
||||
# To compile on MinGW: add "-lws2_32" to LDFLAGS or define WINDOWS in your env
|
||||
# To compile with PKCS11: add "-lpkcs11-helper" to LDFLAGS
|
||||
|
||||
CFLAGS += -I../include -D_FILE_OFFSET_BITS=64 -Wall -W -Wdeclaration-after-statement
|
||||
OFLAGS = -O2
|
||||
LDFLAGS += -L../library -lpolarssl $(SYS_LDFLAGS)
|
||||
|
||||
ifdef DEBUG
|
||||
CFLAGS += -g3
|
||||
endif
|
||||
|
||||
ifdef WINDOWS
|
||||
LDFLAGS += -lws2_32
|
||||
endif
|
||||
|
||||
# Zlib shared library extensions:
|
||||
ifdef ZLIB
|
||||
LDFLAGS += -lz
|
||||
endif
|
||||
|
||||
APPS = aes/aescrypt2 aes/crypt_and_hash \
|
||||
hash/hello hash/generic_sum \
|
||||
hash/md5sum hash/sha1sum \
|
||||
hash/sha2sum pkey/dh_client \
|
||||
pkey/dh_genprime pkey/dh_server \
|
||||
pkey/key_app pkey/key_app_writer \
|
||||
pkey/mpi_demo pkey/rsa_genkey \
|
||||
pkey/rsa_decrypt pkey/rsa_encrypt \
|
||||
pkey/rsa_sign pkey/rsa_verify \
|
||||
pkey/rsa_sign_pss pkey/rsa_verify_pss \
|
||||
ssl/ssl_client1 ssl/ssl_client2 \
|
||||
ssl/ssl_server ssl/ssl_server2 \
|
||||
ssl/ssl_fork_server \
|
||||
ssl/ssl_mail_client random/gen_entropy \
|
||||
random/gen_random_havege \
|
||||
random/gen_random_ctr_drbg \
|
||||
test/ssl_cert_test test/benchmark \
|
||||
test/selftest test/ssl_test \
|
||||
util/strerror \
|
||||
x509/cert_app x509/crl_app \
|
||||
x509/cert_req
|
||||
|
||||
ifdef OPENSSL
|
||||
APPS += test/o_p_test
|
||||
endif
|
||||
|
||||
.SILENT:
|
||||
|
||||
all: $(APPS)
|
||||
|
||||
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 $@
|
||||
|
||||
hash/generic_sum: hash/generic_sum.c ../library/libpolarssl.a
|
||||
echo " CC hash/generic_sum.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) hash/generic_sum.c $(LDFLAGS) -o $@
|
||||
|
||||
hash/md5sum: hash/md5sum.c ../library/libpolarssl.a
|
||||
echo " CC hash/md5sum.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) hash/md5sum.c $(LDFLAGS) -o $@
|
||||
|
||||
hash/sha1sum: hash/sha1sum.c ../library/libpolarssl.a
|
||||
echo " CC hash/sha1sum.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) hash/sha1sum.c $(LDFLAGS) -o $@
|
||||
|
||||
hash/sha2sum: hash/sha2sum.c ../library/libpolarssl.a
|
||||
echo " CC hash/sha2sum.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) hash/sha2sum.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/dh_client: pkey/dh_client.c ../library/libpolarssl.a
|
||||
echo " CC pkey/dh_client.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/dh_client.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/dh_genprime: pkey/dh_genprime.c ../library/libpolarssl.a
|
||||
echo " CC pkey/dh_genprime.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/dh_genprime.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/dh_server: pkey/dh_server.c ../library/libpolarssl.a
|
||||
echo " CC pkey/dh_server.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/dh_server.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/key_app: pkey/key_app.c ../library/libpolarssl.a
|
||||
echo " CC pkey/key_app.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/key_app.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/key_app_writer: pkey/key_app_writer.c ../library/libpolarssl.a
|
||||
echo " CC pkey/key_app_writer.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/key_app_writer.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/mpi_demo: pkey/mpi_demo.c ../library/libpolarssl.a
|
||||
echo " CC pkey/mpi_demo.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/mpi_demo.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_genkey: pkey/rsa_genkey.c ../library/libpolarssl.a
|
||||
echo " CC pkey/rsa_genkey.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/rsa_genkey.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_sign: pkey/rsa_sign.c ../library/libpolarssl.a
|
||||
echo " CC pkey/rsa_sign.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/rsa_sign.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_verify: pkey/rsa_verify.c ../library/libpolarssl.a
|
||||
echo " CC pkey/rsa_verify.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/rsa_verify.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_sign_pss: pkey/rsa_sign_pss.c ../library/libpolarssl.a
|
||||
echo " CC pkey/rsa_sign_pss.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/rsa_sign_pss.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_verify_pss: pkey/rsa_verify_pss.c ../library/libpolarssl.a
|
||||
echo " CC pkey/rsa_verify_pss.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/rsa_verify_pss.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_decrypt: pkey/rsa_decrypt.c ../library/libpolarssl.a
|
||||
echo " CC pkey/rsa_decrypt.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/rsa_decrypt.c $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_encrypt: pkey/rsa_encrypt.c ../library/libpolarssl.a
|
||||
echo " CC pkey/rsa_encrypt.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) pkey/rsa_encrypt.c $(LDFLAGS) -o $@
|
||||
|
||||
random/gen_entropy: random/gen_entropy.c ../library/libpolarssl.a
|
||||
echo " CC random/gen_entropy.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) random/gen_entropy.c $(LDFLAGS) -o $@
|
||||
|
||||
random/gen_random_havege: random/gen_random_havege.c ../library/libpolarssl.a
|
||||
echo " CC random/gen_random_havege.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) random/gen_random_havege.c $(LDFLAGS) -o $@
|
||||
|
||||
random/gen_random_ctr_drbg: random/gen_random_ctr_drbg.c ../library/libpolarssl.a
|
||||
echo " CC random/gen_random_ctr_drbg.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) random/gen_random_ctr_drbg.c $(LDFLAGS) -o $@
|
||||
|
||||
ssl/ssl_client1: ssl/ssl_client1.c ../library/libpolarssl.a
|
||||
echo " CC ssl/ssl_client1.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) ssl/ssl_client1.c $(LDFLAGS) -o $@
|
||||
|
||||
ssl/ssl_client2: ssl/ssl_client2.c ../library/libpolarssl.a
|
||||
echo " CC ssl/ssl_client2.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) ssl/ssl_client2.c $(LDFLAGS) -o $@
|
||||
|
||||
ssl/ssl_server: ssl/ssl_server.c ../library/libpolarssl.a
|
||||
echo " CC ssl/ssl_server.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) ssl/ssl_server.c $(LDFLAGS) -o $@
|
||||
|
||||
ssl/ssl_server2: ssl/ssl_server2.c ../library/libpolarssl.a
|
||||
echo " CC ssl/ssl_server2.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) ssl/ssl_server2.c $(LDFLAGS) -o $@
|
||||
|
||||
ssl/ssl_fork_server: ssl/ssl_fork_server.c ../library/libpolarssl.a
|
||||
echo " CC ssl/ssl_fork_server.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) ssl/ssl_fork_server.c $(LDFLAGS) -o $@
|
||||
|
||||
ssl/ssl_mail_client: ssl/ssl_mail_client.c ../library/libpolarssl.a
|
||||
echo " CC ssl/ssl_mail_client.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) ssl/ssl_mail_client.c $(LDFLAGS) -o $@
|
||||
|
||||
test/ssl_cert_test: test/ssl_cert_test.c ../library/libpolarssl.a
|
||||
echo " CC test/ssl_cert_test.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) test/ssl_cert_test.c $(LDFLAGS) -o $@
|
||||
|
||||
test/benchmark: test/benchmark.c ../library/libpolarssl.a
|
||||
echo " CC test/benchmark.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) test/benchmark.c $(LDFLAGS) -o $@
|
||||
|
||||
test/selftest: test/selftest.c ../library/libpolarssl.a
|
||||
echo " CC test/selftest.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) test/selftest.c $(LDFLAGS) -o $@
|
||||
|
||||
test/ssl_test: test/ssl_test.c ../library/libpolarssl.a
|
||||
echo " CC test/ssl_test.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) test/ssl_test.c $(LDFLAGS) -o $@
|
||||
|
||||
test/o_p_test: test/o_p_test.c ../library/libpolarssl.a
|
||||
echo " CC test/o_p_test.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) test/o_p_test.c $(LDFLAGS) -o $@ -lssl -lcrypto
|
||||
|
||||
util/strerror: util/strerror.c ../library/libpolarssl.a
|
||||
echo " CC util/strerror.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) util/strerror.c $(LDFLAGS) -o $@
|
||||
|
||||
x509/cert_app: x509/cert_app.c ../library/libpolarssl.a
|
||||
echo " CC x509/cert_app.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) x509/cert_app.c $(LDFLAGS) -o $@
|
||||
|
||||
x509/crl_app: x509/crl_app.c ../library/libpolarssl.a
|
||||
echo " CC x509/crl_app.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) x509/crl_app.c $(LDFLAGS) -o $@
|
||||
|
||||
x509/cert_req: x509/cert_req.c ../library/libpolarssl.a
|
||||
echo " CC x509/cert_req.c"
|
||||
$(CC) $(CFLAGS) $(OFLAGS) x509/cert_req.c $(LDFLAGS) -o $@
|
||||
|
||||
clean:
|
||||
ifndef WINDOWS
|
||||
rm -f $(APPS)
|
||||
endif
|
||||
ifdef WINDOWS
|
||||
del /S /Q /F *.o *.exe
|
||||
endif
|
||||
@@ -1,9 +0,0 @@
|
||||
add_executable(aescrypt2 aescrypt2.c)
|
||||
target_link_libraries(aescrypt2 polarssl)
|
||||
|
||||
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)
|
||||
-428
@@ -1,428 +0,0 @@
|
||||
/*
|
||||
* AES-256 file encryption program
|
||||
*
|
||||
* Copyright (C) 2006-2011, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* 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 <windows.h>
|
||||
#if !defined(_WIN32_WCE)
|
||||
#include <io.h>
|
||||
#endif
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#include "polarssl/aes.h"
|
||||
#include "polarssl/sha2.h"
|
||||
|
||||
#define MODE_ENCRYPT 0
|
||||
#define MODE_DECRYPT 1
|
||||
|
||||
#define USAGE \
|
||||
"\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
|
||||
"\n <mode>: 0 = encrypt, 1 = decrypt\n" \
|
||||
"\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
|
||||
"\n"
|
||||
|
||||
#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA2_C)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
printf("POLARSSL_AES_C and/or POLARSSL_SHA2_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1;
|
||||
|
||||
int i, n;
|
||||
int mode, lastn;
|
||||
size_t keylen;
|
||||
FILE *fkey, *fin = NULL, *fout = NULL;
|
||||
|
||||
char *p;
|
||||
unsigned char IV[16];
|
||||
unsigned char key[512];
|
||||
unsigned char digest[32];
|
||||
unsigned char buffer[1024];
|
||||
|
||||
aes_context aes_ctx;
|
||||
sha2_context sha_ctx;
|
||||
|
||||
#if defined(_WIN32_WCE)
|
||||
long filesize, offset;
|
||||
#elif defined(_WIN32)
|
||||
LARGE_INTEGER li_size;
|
||||
__int64 filesize, offset;
|
||||
#else
|
||||
off_t filesize, offset;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Parse the command-line arguments.
|
||||
*/
|
||||
if( argc != 5 )
|
||||
{
|
||||
printf( USAGE );
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( "\n Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mode = atoi( argv[1] );
|
||||
|
||||
if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
|
||||
{
|
||||
fprintf( stderr, "invalide operation mode\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( strcmp( argv[2], argv[3] ) == 0 )
|
||||
{
|
||||
fprintf( stderr, "input and output filenames must differ\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
|
||||
{
|
||||
fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
|
||||
{
|
||||
fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the secret key and clean the command line.
|
||||
*/
|
||||
if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
|
||||
{
|
||||
keylen = fread( key, 1, sizeof( key ), fkey );
|
||||
fclose( fkey );
|
||||
}
|
||||
else
|
||||
{
|
||||
if( memcmp( argv[4], "hex:", 4 ) == 0 )
|
||||
{
|
||||
p = &argv[4][4];
|
||||
keylen = 0;
|
||||
|
||||
while( sscanf( p, "%02X", &n ) > 0 &&
|
||||
keylen < (int) sizeof( key ) )
|
||||
{
|
||||
key[keylen++] = (unsigned char) n;
|
||||
p += 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
keylen = strlen( argv[4] );
|
||||
|
||||
if( keylen > (int) sizeof( key ) )
|
||||
keylen = (int) sizeof( key );
|
||||
|
||||
memcpy( key, argv[4], keylen );
|
||||
}
|
||||
}
|
||||
|
||||
memset( argv[4], 0, strlen( argv[4] ) );
|
||||
|
||||
#if defined(_WIN32_WCE)
|
||||
filesize = fseek( fin, 0L, SEEK_END );
|
||||
#else
|
||||
#if defined(_WIN32)
|
||||
/*
|
||||
* Support large files (> 2Gb) on Win32
|
||||
*/
|
||||
li_size.QuadPart = 0;
|
||||
li_size.LowPart =
|
||||
SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
|
||||
li_size.LowPart, &li_size.HighPart, FILE_END );
|
||||
|
||||
if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
|
||||
{
|
||||
fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
filesize = li_size.QuadPart;
|
||||
#else
|
||||
if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
|
||||
{
|
||||
perror( "lseek" );
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if( fseek( fin, 0, SEEK_SET ) < 0 )
|
||||
{
|
||||
fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mode == MODE_ENCRYPT )
|
||||
{
|
||||
/*
|
||||
* Generate the initialization vector as:
|
||||
* IV = SHA-256( filesize || filename )[0..15]
|
||||
*/
|
||||
for( i = 0; i < 8; i++ )
|
||||
buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
|
||||
|
||||
p = argv[2];
|
||||
|
||||
sha2_starts( &sha_ctx, 0 );
|
||||
sha2_update( &sha_ctx, buffer, 8 );
|
||||
sha2_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
|
||||
sha2_finish( &sha_ctx, digest );
|
||||
|
||||
memcpy( IV, digest, 16 );
|
||||
|
||||
/*
|
||||
* The last four bits in the IV are actually used
|
||||
* to store the file size modulo the AES block size.
|
||||
*/
|
||||
lastn = (int)( filesize & 0x0F );
|
||||
|
||||
IV[15] = (unsigned char)
|
||||
( ( IV[15] & 0xF0 ) | lastn );
|
||||
|
||||
/*
|
||||
* Append the IV at the beginning of the output.
|
||||
*/
|
||||
if( fwrite( IV, 1, 16, fout ) != 16 )
|
||||
{
|
||||
fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Hash the IV and the secret key together 8192 times
|
||||
* using the result to setup the AES context and HMAC.
|
||||
*/
|
||||
memset( digest, 0, 32 );
|
||||
memcpy( digest, IV, 16 );
|
||||
|
||||
for( i = 0; i < 8192; i++ )
|
||||
{
|
||||
sha2_starts( &sha_ctx, 0 );
|
||||
sha2_update( &sha_ctx, digest, 32 );
|
||||
sha2_update( &sha_ctx, key, keylen );
|
||||
sha2_finish( &sha_ctx, digest );
|
||||
}
|
||||
|
||||
memset( key, 0, sizeof( key ) );
|
||||
aes_setkey_enc( &aes_ctx, digest, 256 );
|
||||
sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
|
||||
|
||||
/*
|
||||
* Encrypt and write the ciphertext.
|
||||
*/
|
||||
for( offset = 0; offset < filesize; offset += 16 )
|
||||
{
|
||||
n = ( filesize - offset > 16 ) ? 16 : (int)
|
||||
( filesize - offset );
|
||||
|
||||
if( fread( buffer, 1, n, fin ) != (size_t) n )
|
||||
{
|
||||
fprintf( stderr, "fread(%d bytes) failed\n", n );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 0; i < 16; i++ )
|
||||
buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
|
||||
|
||||
aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
|
||||
sha2_hmac_update( &sha_ctx, buffer, 16 );
|
||||
|
||||
if( fwrite( buffer, 1, 16, fout ) != 16 )
|
||||
{
|
||||
fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memcpy( IV, buffer, 16 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Finally write the HMAC.
|
||||
*/
|
||||
sha2_hmac_finish( &sha_ctx, digest );
|
||||
|
||||
if( fwrite( digest, 1, 32, fout ) != 32 )
|
||||
{
|
||||
fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if( mode == MODE_DECRYPT )
|
||||
{
|
||||
unsigned char tmp[16];
|
||||
|
||||
/*
|
||||
* The encrypted file must be structured as follows:
|
||||
*
|
||||
* 00 .. 15 Initialization Vector
|
||||
* 16 .. 31 AES Encrypted Block #1
|
||||
* ..
|
||||
* N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
|
||||
* (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
|
||||
*/
|
||||
if( filesize < 48 )
|
||||
{
|
||||
fprintf( stderr, "File too short to be encrypted.\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( filesize & 0x0F ) != 0 )
|
||||
{
|
||||
fprintf( stderr, "File size not a multiple of 16.\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Substract the IV + HMAC length.
|
||||
*/
|
||||
filesize -= ( 16 + 32 );
|
||||
|
||||
/*
|
||||
* Read the IV and original filesize modulo 16.
|
||||
*/
|
||||
if( fread( buffer, 1, 16, fin ) != 16 )
|
||||
{
|
||||
fprintf( stderr, "fread(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memcpy( IV, buffer, 16 );
|
||||
lastn = IV[15] & 0x0F;
|
||||
|
||||
/*
|
||||
* Hash the IV and the secret key together 8192 times
|
||||
* using the result to setup the AES context and HMAC.
|
||||
*/
|
||||
memset( digest, 0, 32 );
|
||||
memcpy( digest, IV, 16 );
|
||||
|
||||
for( i = 0; i < 8192; i++ )
|
||||
{
|
||||
sha2_starts( &sha_ctx, 0 );
|
||||
sha2_update( &sha_ctx, digest, 32 );
|
||||
sha2_update( &sha_ctx, key, keylen );
|
||||
sha2_finish( &sha_ctx, digest );
|
||||
}
|
||||
|
||||
memset( key, 0, sizeof( key ) );
|
||||
aes_setkey_dec( &aes_ctx, digest, 256 );
|
||||
sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
|
||||
|
||||
/*
|
||||
* Decrypt and write the plaintext.
|
||||
*/
|
||||
for( offset = 0; offset < filesize; offset += 16 )
|
||||
{
|
||||
if( fread( buffer, 1, 16, fin ) != 16 )
|
||||
{
|
||||
fprintf( stderr, "fread(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memcpy( tmp, buffer, 16 );
|
||||
|
||||
sha2_hmac_update( &sha_ctx, buffer, 16 );
|
||||
aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
|
||||
|
||||
for( i = 0; i < 16; i++ )
|
||||
buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
|
||||
|
||||
memcpy( IV, tmp, 16 );
|
||||
|
||||
n = ( lastn > 0 && offset == filesize - 16 )
|
||||
? lastn : 16;
|
||||
|
||||
if( fwrite( buffer, 1, n, fout ) != (size_t) n )
|
||||
{
|
||||
fprintf( stderr, "fwrite(%d bytes) failed\n", n );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Verify the message authentication code.
|
||||
*/
|
||||
sha2_hmac_finish( &sha_ctx, digest );
|
||||
|
||||
if( fread( buffer, 1, 32, fin ) != 32 )
|
||||
{
|
||||
fprintf( stderr, "fread(%d bytes) failed\n", 32 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( memcmp( digest, buffer, 32 ) != 0 )
|
||||
{
|
||||
fprintf( stderr, "HMAC check failed: wrong key, "
|
||||
"or file corrupted.\n" );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
if( fin )
|
||||
fclose( fin );
|
||||
if( fout )
|
||||
fclose( fout );
|
||||
|
||||
memset( buffer, 0, sizeof( buffer ) );
|
||||
memset( digest, 0, sizeof( digest ) );
|
||||
|
||||
memset( &aes_ctx, 0, sizeof( aes_context ) );
|
||||
memset( &sha_ctx, 0, sizeof( sha2_context ) );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_AES_C && POLARSSL_SHA2_C */
|
||||
-501
File diff suppressed because it is too large
Load Diff
-18
@@ -1,18 +0,0 @@
|
||||
add_executable(hello hello.c)
|
||||
target_link_libraries(hello polarssl)
|
||||
|
||||
add_executable(generic_sum generic_sum.c)
|
||||
target_link_libraries(generic_sum polarssl)
|
||||
|
||||
add_executable(md5sum md5sum.c)
|
||||
target_link_libraries(md5sum polarssl)
|
||||
|
||||
add_executable(sha1sum sha1sum.c)
|
||||
target_link_libraries(sha1sum polarssl)
|
||||
|
||||
add_executable(sha2sum sha2sum.c)
|
||||
target_link_libraries(sha2sum polarssl)
|
||||
|
||||
install(TARGETS hello md5sum sha1sum sha2sum generic_sum
|
||||
DESTINATION "bin"
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
||||
-216
@@ -1,216 +0,0 @@
|
||||
/*
|
||||
* generic message digest layer demonstration program
|
||||
*
|
||||
* Copyright (C) 2006-2011, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* 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
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#include "polarssl/md.h"
|
||||
|
||||
#if !defined(POLARSSL_MD_C)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf("POLARSSL_MD_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned char *sum )
|
||||
{
|
||||
int ret = md_file( md_info, filename, sum );
|
||||
|
||||
if( ret == 1 )
|
||||
fprintf( stderr, "failed to open: %s\n", filename );
|
||||
|
||||
if( ret == 2 )
|
||||
fprintf( stderr, "failed to read: %s\n", filename );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
static int generic_print( const md_info_t *md_info, char *filename )
|
||||
{
|
||||
int i;
|
||||
unsigned char sum[POLARSSL_MD_MAX_SIZE];
|
||||
|
||||
if( generic_wrapper( md_info, filename, sum ) != 0 )
|
||||
return( 1 );
|
||||
|
||||
for( i = 0; i < md_info->size; i++ )
|
||||
printf( "%02x", sum[i] );
|
||||
|
||||
printf( " %s\n", filename );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int generic_check( const md_info_t *md_info, char *filename )
|
||||
{
|
||||
int i;
|
||||
size_t n;
|
||||
FILE *f;
|
||||
int nb_err1, nb_err2;
|
||||
int nb_tot1, nb_tot2;
|
||||
unsigned char sum[POLARSSL_MD_MAX_SIZE];
|
||||
char buf[POLARSSL_MD_MAX_SIZE * 2 + 1], line[1024];
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
{
|
||||
printf( "failed to open: %s\n", filename );
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
nb_err1 = nb_err2 = 0;
|
||||
nb_tot1 = nb_tot2 = 0;
|
||||
|
||||
memset( line, 0, sizeof( line ) );
|
||||
|
||||
n = sizeof( line );
|
||||
|
||||
while( fgets( line, (int) n - 1, f ) != NULL )
|
||||
{
|
||||
n = strlen( line );
|
||||
|
||||
if( n < (size_t) 2 * md_info->size + 4 )
|
||||
{
|
||||
printf("No '%s' hash found on line.\n", md_info->name);
|
||||
continue;
|
||||
}
|
||||
|
||||
if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
|
||||
{
|
||||
printf("No '%s' hash found on line.\n", md_info->name);
|
||||
continue;
|
||||
}
|
||||
|
||||
if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
|
||||
if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
|
||||
|
||||
nb_tot1++;
|
||||
|
||||
if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
|
||||
{
|
||||
nb_err1++;
|
||||
continue;
|
||||
}
|
||||
|
||||
nb_tot2++;
|
||||
|
||||
for( i = 0; i < md_info->size; i++ )
|
||||
sprintf( buf + i * 2, "%02x", sum[i] );
|
||||
|
||||
if( memcmp( line, buf, 2 * md_info->size ) != 0 )
|
||||
{
|
||||
nb_err2++;
|
||||
fprintf( stderr, "wrong checksum: %s\n", line + 66 );
|
||||
}
|
||||
|
||||
n = sizeof( line );
|
||||
}
|
||||
|
||||
if( nb_err1 != 0 )
|
||||
{
|
||||
printf( "WARNING: %d (out of %d) input files could "
|
||||
"not be read\n", nb_err1, nb_tot1 );
|
||||
}
|
||||
|
||||
if( nb_err2 != 0 )
|
||||
{
|
||||
printf( "WARNING: %d (out of %d) computed checksums did "
|
||||
"not match\n", nb_err2, nb_tot2 );
|
||||
}
|
||||
|
||||
return( nb_err1 != 0 || nb_err2 != 0 );
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret, i;
|
||||
const md_info_t *md_info;
|
||||
md_context_t md_ctx;
|
||||
|
||||
memset( &md_ctx, 0, sizeof( md_context_t ));
|
||||
|
||||
if( argc == 1 )
|
||||
{
|
||||
const int *list;
|
||||
|
||||
printf( "print mode: generic_sum <md> <file> <file> ...\n" );
|
||||
printf( "check mode: generic_sum <md> -c <checksum file>\n" );
|
||||
|
||||
printf( "\nAvailable message digests:\n" );
|
||||
list = md_list();
|
||||
while( *list )
|
||||
{
|
||||
md_info = md_info_from_type( *list );
|
||||
printf( " %s\n", md_info->name );
|
||||
list++;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( "\n Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the MD from the command line
|
||||
*/
|
||||
md_info = md_info_from_string( argv[1] );
|
||||
if( md_info == NULL )
|
||||
{
|
||||
fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
|
||||
return( 1 );
|
||||
}
|
||||
if( md_init_ctx( &md_ctx, md_info) )
|
||||
{
|
||||
fprintf( stderr, "Failed to initialize context.\n" );
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
|
||||
{
|
||||
ret |= generic_check( md_info, argv[3] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 2; i < argc; i++ )
|
||||
ret |= generic_print( md_info, argv[i] );
|
||||
|
||||
exit:
|
||||
md_free_ctx( &md_ctx );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_MD_C */
|
||||
-71
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Classic "Hello, world" demonstration program
|
||||
*
|
||||
* Copyright (C) 2006-2011, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* 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
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#include "polarssl/md5.h"
|
||||
|
||||
#if !defined(POLARSSL_MD5_C)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf("POLARSSL_MD5_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int i;
|
||||
unsigned char digest[16];
|
||||
char str[] = "Hello, world!";
|
||||
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf( "\n MD5('%s') = ", str );
|
||||
|
||||
md5( (unsigned char *) str, 13, digest );
|
||||
|
||||
for( i = 0; i < 16; i++ )
|
||||
printf( "%02x", digest[i] );
|
||||
|
||||
printf( "\n\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( " Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_MD5_C */
|
||||
-174
@@ -1,174 +0,0 @@
|
||||
/*
|
||||
* md5sum demonstration program
|
||||
*
|
||||
* Copyright (C) 2006-2011, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* 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
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#include "polarssl/md5.h"
|
||||
|
||||
#if !defined(POLARSSL_MD5_C) || !defined(POLARSSL_FS_IO)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf("POLARSSL_MD5_C and/or POLARSSL_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
static int md5_wrapper( char *filename, unsigned char *sum )
|
||||
{
|
||||
int ret = md5_file( filename, sum );
|
||||
|
||||
if( ret == 1 )
|
||||
fprintf( stderr, "failed to open: %s\n", filename );
|
||||
|
||||
if( ret == 2 )
|
||||
fprintf( stderr, "failed to read: %s\n", filename );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
static int md5_print( char *filename )
|
||||
{
|
||||
int i;
|
||||
unsigned char sum[16];
|
||||
|
||||
if( md5_wrapper( filename, sum ) != 0 )
|
||||
return( 1 );
|
||||
|
||||
for( i = 0; i < 16; i++ )
|
||||
printf( "%02x", sum[i] );
|
||||
|
||||
printf( " %s\n", filename );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int md5_check( char *filename )
|
||||
{
|
||||
int i;
|
||||
size_t n;
|
||||
FILE *f;
|
||||
int nb_err1, nb_err2;
|
||||
int nb_tot1, nb_tot2;
|
||||
unsigned char sum[16];
|
||||
char buf[33], line[1024];
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
{
|
||||
printf( "failed to open: %s\n", filename );
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
nb_err1 = nb_err2 = 0;
|
||||
nb_tot1 = nb_tot2 = 0;
|
||||
|
||||
memset( line, 0, sizeof( line ) );
|
||||
|
||||
n = sizeof( line );
|
||||
|
||||
while( fgets( line, (int) n - 1, f ) != NULL )
|
||||
{
|
||||
n = strlen( line );
|
||||
|
||||
if( n < 36 )
|
||||
continue;
|
||||
|
||||
if( line[32] != ' ' || line[33] != ' ' )
|
||||
continue;
|
||||
|
||||
if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
|
||||
if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
|
||||
|
||||
nb_tot1++;
|
||||
|
||||
if( md5_wrapper( line + 34, sum ) != 0 )
|
||||
{
|
||||
nb_err1++;
|
||||
continue;
|
||||
}
|
||||
|
||||
nb_tot2++;
|
||||
|
||||
for( i = 0; i < 16; i++ )
|
||||
sprintf( buf + i * 2, "%02x", sum[i] );
|
||||
|
||||
if( memcmp( line, buf, 32 ) != 0 )
|
||||
{
|
||||
nb_err2++;
|
||||
fprintf( stderr, "wrong checksum: %s\n", line + 34 );
|
||||
}
|
||||
|
||||
n = sizeof( line );
|
||||
}
|
||||
|
||||
if( nb_err1 != 0 )
|
||||
{
|
||||
printf( "WARNING: %d (out of %d) input files could "
|
||||
"not be read\n", nb_err1, nb_tot1 );
|
||||
}
|
||||
|
||||
if( nb_err2 != 0 )
|
||||
{
|
||||
printf( "WARNING: %d (out of %d) computed checksums did "
|
||||
"not match\n", nb_err2, nb_tot2 );
|
||||
}
|
||||
|
||||
return( nb_err1 != 0 || nb_err2 != 0 );
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret, i;
|
||||
|
||||
if( argc == 1 )
|
||||
{
|
||||
printf( "print mode: md5sum <file> <file> ...\n" );
|
||||
printf( "check mode: md5sum -c <checksum file>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( "\n Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
|
||||
return( md5_check( argv[2] ) );
|
||||
|
||||
ret = 0;
|
||||
for( i = 1; i < argc; i++ )
|
||||
ret |= md5_print( argv[i] );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_MD5_C && POLARSSL_FS_IO */
|
||||
-174
@@ -1,174 +0,0 @@
|
||||
/*
|
||||
* sha1sum demonstration program
|
||||
*
|
||||
* Copyright (C) 2006-2011, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* 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
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#include "polarssl/sha1.h"
|
||||
|
||||
#if !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf("POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
static int sha1_wrapper( char *filename, unsigned char *sum )
|
||||
{
|
||||
int ret = sha1_file( filename, sum );
|
||||
|
||||
if( ret == 1 )
|
||||
fprintf( stderr, "failed to open: %s\n", filename );
|
||||
|
||||
if( ret == 2 )
|
||||
fprintf( stderr, "failed to read: %s\n", filename );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
static int sha1_print( char *filename )
|
||||
{
|
||||
int i;
|
||||
unsigned char sum[20];
|
||||
|
||||
if( sha1_wrapper( filename, sum ) != 0 )
|
||||
return( 1 );
|
||||
|
||||
for( i = 0; i < 20; i++ )
|
||||
printf( "%02x", sum[i] );
|
||||
|
||||
printf( " %s\n", filename );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int sha1_check( char *filename )
|
||||
{
|
||||
int i;
|
||||
size_t n;
|
||||
FILE *f;
|
||||
int nb_err1, nb_err2;
|
||||
int nb_tot1, nb_tot2;
|
||||
unsigned char sum[20];
|
||||
char buf[41], line[1024];
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
{
|
||||
printf( "failed to open: %s\n", filename );
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
nb_err1 = nb_err2 = 0;
|
||||
nb_tot1 = nb_tot2 = 0;
|
||||
|
||||
memset( line, 0, sizeof( line ) );
|
||||
|
||||
n = sizeof( line );
|
||||
|
||||
while( fgets( line, (int) n - 1, f ) != NULL )
|
||||
{
|
||||
n = strlen( line );
|
||||
|
||||
if( n < 44 )
|
||||
continue;
|
||||
|
||||
if( line[40] != ' ' || line[41] != ' ' )
|
||||
continue;
|
||||
|
||||
if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
|
||||
if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
|
||||
|
||||
nb_tot1++;
|
||||
|
||||
if( sha1_wrapper( line + 42, sum ) != 0 )
|
||||
{
|
||||
nb_err1++;
|
||||
continue;
|
||||
}
|
||||
|
||||
nb_tot2++;
|
||||
|
||||
for( i = 0; i < 20; i++ )
|
||||
sprintf( buf + i * 2, "%02x", sum[i] );
|
||||
|
||||
if( memcmp( line, buf, 40 ) != 0 )
|
||||
{
|
||||
nb_err2++;
|
||||
fprintf( stderr, "wrong checksum: %s\n", line + 42 );
|
||||
}
|
||||
|
||||
n = sizeof( line );
|
||||
}
|
||||
|
||||
if( nb_err1 != 0 )
|
||||
{
|
||||
printf( "WARNING: %d (out of %d) input files could "
|
||||
"not be read\n", nb_err1, nb_tot1 );
|
||||
}
|
||||
|
||||
if( nb_err2 != 0 )
|
||||
{
|
||||
printf( "WARNING: %d (out of %d) computed checksums did "
|
||||
"not match\n", nb_err2, nb_tot2 );
|
||||
}
|
||||
|
||||
return( nb_err1 != 0 || nb_err2 != 0 );
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret, i;
|
||||
|
||||
if( argc == 1 )
|
||||
{
|
||||
printf( "print mode: sha1sum <file> <file> ...\n" );
|
||||
printf( "check mode: sha1sum -c <checksum file>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( "\n Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
|
||||
return( sha1_check( argv[2] ) );
|
||||
|
||||
ret = 0;
|
||||
for( i = 1; i < argc; i++ )
|
||||
ret |= sha1_print( argv[i] );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_SHA1_C && POLARSSL_FS_IO */
|
||||
-174
@@ -1,174 +0,0 @@
|
||||
/*
|
||||
* sha2sum demonstration program
|
||||
*
|
||||
* Copyright (C) 2006-2011, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* 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
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#include "polarssl/sha2.h"
|
||||
|
||||
#if !defined(POLARSSL_SHA2_C) || !defined(POLARSSL_FS_IO)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf("POLARSSL_SHA2_C and/or POLARSSL_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
static int sha2_wrapper( char *filename, unsigned char *sum )
|
||||
{
|
||||
int ret = sha2_file( filename, sum, 0 );
|
||||
|
||||
if( ret == 1 )
|
||||
fprintf( stderr, "failed to open: %s\n", filename );
|
||||
|
||||
if( ret == 2 )
|
||||
fprintf( stderr, "failed to read: %s\n", filename );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
static int sha2_print( char *filename )
|
||||
{
|
||||
int i;
|
||||
unsigned char sum[32];
|
||||
|
||||
if( sha2_wrapper( filename, sum ) != 0 )
|
||||
return( 1 );
|
||||
|
||||
for( i = 0; i < 32; i++ )
|
||||
printf( "%02x", sum[i] );
|
||||
|
||||
printf( " %s\n", filename );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int sha2_check( char *filename )
|
||||
{
|
||||
int i;
|
||||
size_t n;
|
||||
FILE *f;
|
||||
int nb_err1, nb_err2;
|
||||
int nb_tot1, nb_tot2;
|
||||
unsigned char sum[32];
|
||||
char buf[65], line[1024];
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
{
|
||||
printf( "failed to open: %s\n", filename );
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
nb_err1 = nb_err2 = 0;
|
||||
nb_tot1 = nb_tot2 = 0;
|
||||
|
||||
memset( line, 0, sizeof( line ) );
|
||||
|
||||
n = sizeof( line );
|
||||
|
||||
while( fgets( line, (int) n - 1, f ) != NULL )
|
||||
{
|
||||
n = strlen( line );
|
||||
|
||||
if( n < 68 )
|
||||
continue;
|
||||
|
||||
if( line[64] != ' ' || line[65] != ' ' )
|
||||
continue;
|
||||
|
||||
if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
|
||||
if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
|
||||
|
||||
nb_tot1++;
|
||||
|
||||
if( sha2_wrapper( line + 66, sum ) != 0 )
|
||||
{
|
||||
nb_err1++;
|
||||
continue;
|
||||
}
|
||||
|
||||
nb_tot2++;
|
||||
|
||||
for( i = 0; i < 32; i++ )
|
||||
sprintf( buf + i * 2, "%02x", sum[i] );
|
||||
|
||||
if( memcmp( line, buf, 64 ) != 0 )
|
||||
{
|
||||
nb_err2++;
|
||||
fprintf( stderr, "wrong checksum: %s\n", line + 66 );
|
||||
}
|
||||
|
||||
n = sizeof( line );
|
||||
}
|
||||
|
||||
if( nb_err1 != 0 )
|
||||
{
|
||||
printf( "WARNING: %d (out of %d) input files could "
|
||||
"not be read\n", nb_err1, nb_tot1 );
|
||||
}
|
||||
|
||||
if( nb_err2 != 0 )
|
||||
{
|
||||
printf( "WARNING: %d (out of %d) computed checksums did "
|
||||
"not match\n", nb_err2, nb_tot2 );
|
||||
}
|
||||
|
||||
return( nb_err1 != 0 || nb_err2 != 0 );
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret, i;
|
||||
|
||||
if( argc == 1 )
|
||||
{
|
||||
printf( "print mode: sha2sum <file> <file> ...\n" );
|
||||
printf( "check mode: sha2sum -c <checksum file>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( "\n Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
|
||||
return( sha2_check( argv[2] ) );
|
||||
|
||||
ret = 0;
|
||||
for( i = 1; i < argc; i++ )
|
||||
ret |= sha2_print( argv[i] );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_SHA2_C && POLARSSL_FS_IO */
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
add_executable(dh_client dh_client.c)
|
||||
target_link_libraries(dh_client polarssl)
|
||||
|
||||
add_executable(dh_genprime dh_genprime.c)
|
||||
target_link_libraries(dh_genprime polarssl)
|
||||
|
||||
add_executable(dh_server dh_server.c)
|
||||
target_link_libraries(dh_server polarssl)
|
||||
|
||||
add_executable(key_app key_app.c)
|
||||
target_link_libraries(key_app polarssl)
|
||||
|
||||
add_executable(key_app_writer key_app_writer.c)
|
||||
target_link_libraries(key_app_writer polarssl)
|
||||
|
||||
add_executable(mpi_demo mpi_demo.c)
|
||||
target_link_libraries(mpi_demo polarssl)
|
||||
|
||||
add_executable(rsa_genkey rsa_genkey.c)
|
||||
target_link_libraries(rsa_genkey polarssl)
|
||||
|
||||
add_executable(rsa_sign rsa_sign.c)
|
||||
target_link_libraries(rsa_sign polarssl)
|
||||
|
||||
add_executable(rsa_verify rsa_verify.c)
|
||||
target_link_libraries(rsa_verify polarssl)
|
||||
|
||||
add_executable(rsa_sign_pss rsa_sign_pss.c)
|
||||
target_link_libraries(rsa_sign_pss polarssl)
|
||||
|
||||
add_executable(rsa_verify_pss rsa_verify_pss.c)
|
||||
target_link_libraries(rsa_verify_pss polarssl)
|
||||
|
||||
add_executable(rsa_encrypt rsa_encrypt.c)
|
||||
target_link_libraries(rsa_encrypt polarssl)
|
||||
|
||||
add_executable(rsa_decrypt rsa_decrypt.c)
|
||||
target_link_libraries(rsa_decrypt polarssl)
|
||||
|
||||
install(TARGETS dh_client dh_genprime dh_server key_app mpi_demo rsa_genkey rsa_sign rsa_verify rsa_encrypt rsa_decrypt
|
||||
DESTINATION "bin"
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
||||
-291
@@ -1,291 +0,0 @@
|
||||
/*
|
||||
* Diffie-Hellman-Merkle key exchange (client side)
|
||||
*
|
||||
* Copyright (C) 2006-2011, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* 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
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#include "polarssl/net.h"
|
||||
#include "polarssl/aes.h"
|
||||
#include "polarssl/dhm.h"
|
||||
#include "polarssl/rsa.h"
|
||||
#include "polarssl/sha1.h"
|
||||
#include "polarssl/entropy.h"
|
||||
#include "polarssl/ctr_drbg.h"
|
||||
|
||||
#define SERVER_NAME "localhost"
|
||||
#define SERVER_PORT 11999
|
||||
|
||||
#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_DHM_C) || \
|
||||
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_NET_C) || \
|
||||
!defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) || \
|
||||
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_ENTROPY_C "
|
||||
"and/or POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
|
||||
"POLARSSL_SHA1_C and/or POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
int ret;
|
||||
size_t n, buflen;
|
||||
int server_fd = -1;
|
||||
|
||||
unsigned char *p, *end;
|
||||
unsigned char buf[2048];
|
||||
unsigned char hash[20];
|
||||
const char *pers = "dh_client";
|
||||
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
rsa_context rsa;
|
||||
dhm_context dhm;
|
||||
aes_context aes;
|
||||
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
memset( &rsa, 0, sizeof( rsa ) );
|
||||
memset( &dhm, 0, sizeof( dhm ) );
|
||||
|
||||
/*
|
||||
* 1. Setup the RNG
|
||||
*/
|
||||
printf( "\n . Seeding the random number generator" );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 2. Read the server's public RSA key
|
||||
*/
|
||||
printf( "\n . Reading public key from rsa_pub.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
||||
|
||||
if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
|
||||
fclose( f );
|
||||
|
||||
/*
|
||||
* 3. Initiate the connection
|
||||
*/
|
||||
printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME,
|
||||
SERVER_PORT );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = net_connect( &server_fd, SERVER_NAME,
|
||||
SERVER_PORT ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! net_connect returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 4a. First get the buffer length
|
||||
*/
|
||||
printf( "\n . Receiving the server's DH parameters" );
|
||||
fflush( stdout );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( ( ret = net_recv( &server_fd, buf, 2 ) ) != 2 )
|
||||
{
|
||||
printf( " failed\n ! net_recv returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
n = buflen = ( buf[0] << 8 ) | buf[1];
|
||||
if( buflen < 1 || buflen > sizeof( buf ) )
|
||||
{
|
||||
printf( " failed\n ! Got an invalid buffer length\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
|
||||
*/
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( ( ret = net_recv( &server_fd, buf, n ) ) != (int) n )
|
||||
{
|
||||
printf( " failed\n ! net_recv returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
p = buf, end = buf + buflen;
|
||||
|
||||
if( ( ret = dhm_read_params( &dhm, &p, end ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! dhm_read_params returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( dhm.len < 64 || dhm.len > 512 )
|
||||
{
|
||||
ret = 1;
|
||||
printf( " failed\n ! Invalid DHM modulus size\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 5. Check that the server's RSA signature matches
|
||||
* the SHA-1 hash of (P,G,Ys)
|
||||
*/
|
||||
printf( "\n . Verifying the server's RSA signature" );
|
||||
fflush( stdout );
|
||||
|
||||
p += 2;
|
||||
|
||||
if( ( n = (size_t) ( end - p ) ) != rsa.len )
|
||||
{
|
||||
ret = 1;
|
||||
printf( " failed\n ! Invalid RSA signature size\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
sha1( buf, (int)( p - 2 - buf ), hash );
|
||||
|
||||
if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
|
||||
0, hash, p ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 6. Send our public value: Yc = G ^ Xc mod P
|
||||
*/
|
||||
printf( "\n . Sending own public value to server" );
|
||||
fflush( stdout );
|
||||
|
||||
n = dhm.len;
|
||||
if( ( ret = dhm_make_public( &dhm, dhm.len, buf, n,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! dhm_make_public returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = net_send( &server_fd, buf, n ) ) != (int) n )
|
||||
{
|
||||
printf( " failed\n ! net_send returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 7. Derive the shared secret: K = Ys ^ Xc mod P
|
||||
*/
|
||||
printf( "\n . Shared secret: " );
|
||||
fflush( stdout );
|
||||
|
||||
n = dhm.len;
|
||||
if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( n = 0; n < 16; n++ )
|
||||
printf( "%02x", buf[n] );
|
||||
|
||||
/*
|
||||
* 8. Setup the AES-256 decryption key
|
||||
*
|
||||
* This is an overly simplified example; best practice is
|
||||
* to hash the shared secret with a random value to derive
|
||||
* the keying material for the encryption/decryption keys,
|
||||
* IVs and MACs.
|
||||
*/
|
||||
printf( "...\n . Receiving and decrypting the ciphertext" );
|
||||
fflush( stdout );
|
||||
|
||||
aes_setkey_dec( &aes, buf, 256 );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( ( ret = net_recv( &server_fd, buf, 16 ) ) != 16 )
|
||||
{
|
||||
printf( " failed\n ! net_recv returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
aes_crypt_ecb( &aes, AES_DECRYPT, buf, buf );
|
||||
buf[16] = '\0';
|
||||
printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
|
||||
|
||||
exit:
|
||||
|
||||
net_close( server_fd );
|
||||
rsa_free( &rsa );
|
||||
dhm_free( &dhm );
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_ENTROPY_C &&
|
||||
POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
|
||||
POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
|
||||
-162
@@ -1,162 +0,0 @@
|
||||
/*
|
||||
* Diffie-Hellman-Merkle key exchange (prime generation)
|
||||
*
|
||||
* Copyright (C) 2006-2012, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* 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
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#include "polarssl/bignum.h"
|
||||
#include "polarssl/entropy.h"
|
||||
#include "polarssl/ctr_drbg.h"
|
||||
|
||||
/*
|
||||
* Note: G = 4 is always a quadratic residue mod P,
|
||||
* so it is a generator of order Q (with P = 2*Q+1).
|
||||
*/
|
||||
#define DH_P_SIZE 1024
|
||||
#define GENERATOR "4"
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
|
||||
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
|
||||
"POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1;
|
||||
|
||||
#if defined(POLARSSL_GENPRIME)
|
||||
mpi G, P, Q;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
const char *pers = "dh_genprime";
|
||||
FILE *fout;
|
||||
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
|
||||
mpi_read_string( &G, 10, GENERATOR );
|
||||
|
||||
printf( "\nWARNING: You should not generate and use your own DHM primes\n" );
|
||||
printf( " unless you are very certain of what you are doing!\n" );
|
||||
printf( " Failing to follow this instruction may result in\n" );
|
||||
printf( " weak security for your connections! Use the\n" );
|
||||
printf( " predefined DHM parameters from dhm.h instead!\n\n" );
|
||||
printf( "============================================================\n\n" );
|
||||
|
||||
printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf( " ok\n . Generating the modulus, please wait..." );
|
||||
fflush( stdout );
|
||||
|
||||
/*
|
||||
* This can take a long time...
|
||||
*/
|
||||
if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! mpi_sub_int returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! mpi_div_int returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! mpi_is_prime returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf( " ok\n . Exporting the value in dh_prime.txt..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
printf( " failed\n ! Could not create dh_prime.txt\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
|
||||
( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
|
||||
{
|
||||
printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf( " ok\n\n" );
|
||||
fclose( fout );
|
||||
|
||||
exit:
|
||||
|
||||
mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
|
||||
#else
|
||||
printf( "\n ! Prime-number generation is not available.\n\n" );
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( " Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_FS_IO &&
|
||||
POLARSSL_CTR_DRBG_C */
|
||||
@@ -1,2 +0,0 @@
|
||||
P = FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF
|
||||
G = 02
|
||||
-292
@@ -1,292 +0,0 @@
|
||||
/*
|
||||
* Diffie-Hellman-Merkle key exchange (server side)
|
||||
*
|
||||
* Copyright (C) 2006-2011, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* 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
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#include "polarssl/net.h"
|
||||
#include "polarssl/aes.h"
|
||||
#include "polarssl/dhm.h"
|
||||
#include "polarssl/rsa.h"
|
||||
#include "polarssl/sha1.h"
|
||||
#include "polarssl/entropy.h"
|
||||
#include "polarssl/ctr_drbg.h"
|
||||
|
||||
#define SERVER_PORT 11999
|
||||
#define PLAINTEXT "==Hello there!=="
|
||||
|
||||
#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_DHM_C) || \
|
||||
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_NET_C) || \
|
||||
!defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) || \
|
||||
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_ENTROPY_C "
|
||||
"and/or POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
|
||||
"POLARSSL_SHA1_C and/or POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_CTR_DBRG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
int ret;
|
||||
size_t n, buflen;
|
||||
int listen_fd = -1;
|
||||
int client_fd = -1;
|
||||
|
||||
unsigned char buf[2048];
|
||||
unsigned char hash[20];
|
||||
unsigned char buf2[2];
|
||||
const char *pers = "dh_server";
|
||||
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
rsa_context rsa;
|
||||
dhm_context dhm;
|
||||
aes_context aes;
|
||||
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
memset( &rsa, 0, sizeof( rsa ) );
|
||||
memset( &dhm, 0, sizeof( dhm ) );
|
||||
|
||||
/*
|
||||
* 1. Setup the RNG
|
||||
*/
|
||||
printf( "\n . Seeding the random number generator" );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 2a. Read the server's private RSA key
|
||||
*/
|
||||
printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
||||
|
||||
if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
|
||||
fclose( f );
|
||||
|
||||
/*
|
||||
* 2b. Get the DHM modulus and generator
|
||||
*/
|
||||
printf( "\n . Reading DH parameters from dh_prime.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
printf( " failed\n ! Could not open dh_prime.txt\n" \
|
||||
" ! Please run dh_genprime first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mpi_read_file( &dhm.P, 16, f ) != 0 ||
|
||||
mpi_read_file( &dhm.G, 16, f ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! Invalid DH parameter file\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
/*
|
||||
* 3. Wait for a client to connect
|
||||
*/
|
||||
printf( "\n . Waiting for a remote connection" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! net_bind returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! net_accept returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 4. Setup the DH parameters (P,G,Ys)
|
||||
*/
|
||||
printf( "\n . Sending the server's DH parameters" );
|
||||
fflush( stdout );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( ( ret = dhm_make_params( &dhm, mpi_size( &dhm.P ), buf, &n,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! dhm_make_params returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 5. Sign the parameters and send them
|
||||
*/
|
||||
sha1( buf, n, hash );
|
||||
|
||||
buf[n ] = (unsigned char)( rsa.len >> 8 );
|
||||
buf[n + 1] = (unsigned char)( rsa.len );
|
||||
|
||||
if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, SIG_RSA_SHA1,
|
||||
0, hash, buf + n + 2 ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
buflen = n + 2 + rsa.len;
|
||||
buf2[0] = (unsigned char)( buflen >> 8 );
|
||||
buf2[1] = (unsigned char)( buflen );
|
||||
|
||||
if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 ||
|
||||
( ret = net_send( &client_fd, buf, buflen ) ) != (int) buflen )
|
||||
{
|
||||
printf( " failed\n ! net_send returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 6. Get the client's public value: Yc = G ^ Xc mod P
|
||||
*/
|
||||
printf( "\n . Receiving the client's public value" );
|
||||
fflush( stdout );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
n = dhm.len;
|
||||
|
||||
if( ( ret = net_recv( &client_fd, buf, n ) ) != (int) n )
|
||||
{
|
||||
printf( " failed\n ! net_recv returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! dhm_read_public returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 7. Derive the shared secret: K = Ys ^ Xc mod P
|
||||
*/
|
||||
printf( "\n . Shared secret: " );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( n = 0; n < 16; n++ )
|
||||
printf( "%02x", buf[n] );
|
||||
|
||||
/*
|
||||
* 8. Setup the AES-256 encryption key
|
||||
*
|
||||
* This is an overly simplified example; best practice is
|
||||
* to hash the shared secret with a random value to derive
|
||||
* the keying material for the encryption/decryption keys
|
||||
* and MACs.
|
||||
*/
|
||||
printf( "...\n . Encrypting and sending the ciphertext" );
|
||||
fflush( stdout );
|
||||
|
||||
aes_setkey_enc( &aes, buf, 256 );
|
||||
memcpy( buf, PLAINTEXT, 16 );
|
||||
aes_crypt_ecb( &aes, AES_ENCRYPT, buf, buf );
|
||||
|
||||
if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 )
|
||||
{
|
||||
printf( " failed\n ! net_send returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf( "\n\n" );
|
||||
|
||||
exit:
|
||||
|
||||
net_close( client_fd );
|
||||
rsa_free( &rsa );
|
||||
dhm_free( &dhm );
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_ENTROPY_C &&
|
||||
POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
|
||||
POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
|
||||
-256
@@ -1,256 +0,0 @@
|
||||
/*
|
||||
* Key reading application
|
||||
*
|
||||
* Copyright (C) 2006-2012, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* 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
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#include "polarssl/error.h"
|
||||
#include "polarssl/rsa.h"
|
||||
#include "polarssl/x509.h"
|
||||
|
||||
#define MODE_NONE 0
|
||||
#define MODE_PRIVATE 1
|
||||
#define MODE_PUBLIC 2
|
||||
|
||||
#define DFL_MODE MODE_NONE
|
||||
#define DFL_FILENAME "keyfile.key"
|
||||
#define DFL_PASSWORD ""
|
||||
#define DFL_PASSWORD_FILE ""
|
||||
#define DFL_DEBUG_LEVEL 0
|
||||
|
||||
/*
|
||||
* global options
|
||||
*/
|
||||
struct options
|
||||
{
|
||||
int mode; /* the mode to run the application in */
|
||||
const char *filename; /* filename of the key file */
|
||||
const char *password; /* password for the private key */
|
||||
const char *password_file; /* password_file for the private key */
|
||||
int debug_level; /* level of debugging */
|
||||
} opt;
|
||||
|
||||
void my_debug( void *ctx, int level, const char *str )
|
||||
{
|
||||
if( level < opt.debug_level )
|
||||
{
|
||||
fprintf( (FILE *) ctx, "%s", str );
|
||||
fflush( (FILE *) ctx );
|
||||
}
|
||||
}
|
||||
|
||||
#define USAGE \
|
||||
"\n usage: key_app param=<>...\n" \
|
||||
"\n acceptable parameters:\n" \
|
||||
" mode=private|public default: none\n" \
|
||||
" filename=%%s default: keyfile.key\n" \
|
||||
" password=%%s default: \"\"\n" \
|
||||
" password_file=%%s default: \"\"\n" \
|
||||
" debug_level=%%d default: 0 (disabled)\n" \
|
||||
"\n"
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
|
||||
!defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
|
||||
"POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
rsa_context rsa;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
|
||||
/*
|
||||
* Set to sane values
|
||||
*/
|
||||
memset( &rsa, 0, sizeof( rsa_context ) );
|
||||
memset( buf, 0, 1024 );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
printf( USAGE );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
opt.mode = DFL_MODE;
|
||||
opt.filename = DFL_FILENAME;
|
||||
opt.password = DFL_PASSWORD;
|
||||
opt.password_file = DFL_PASSWORD_FILE;
|
||||
opt.debug_level = DFL_DEBUG_LEVEL;
|
||||
|
||||
for( i = 1; i < argc; i++ )
|
||||
{
|
||||
p = argv[i];
|
||||
if( ( q = strchr( p, '=' ) ) == NULL )
|
||||
goto usage;
|
||||
*q++ = '\0';
|
||||
|
||||
if( strcmp( p, "mode" ) == 0 )
|
||||
{
|
||||
if( strcmp( q, "private" ) == 0 )
|
||||
opt.mode = MODE_PRIVATE;
|
||||
else if( strcmp( q, "public" ) == 0 )
|
||||
opt.mode = MODE_PUBLIC;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "filename" ) == 0 )
|
||||
opt.filename = q;
|
||||
else if( strcmp( p, "password" ) == 0 )
|
||||
opt.password = q;
|
||||
else if( strcmp( p, "password_file" ) == 0 )
|
||||
opt.password_file = q;
|
||||
else if( strcmp( p, "debug_level" ) == 0 )
|
||||
{
|
||||
opt.debug_level = atoi( q );
|
||||
if( opt.debug_level < 0 || opt.debug_level > 65535 )
|
||||
goto usage;
|
||||
}
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if( opt.mode == MODE_PRIVATE )
|
||||
{
|
||||
if( strlen( opt.password ) && strlen( opt.password_file ) )
|
||||
{
|
||||
printf( "Error: cannot have both password and password_file\n" );
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if( strlen( opt.password_file ) )
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
printf( "\n . Loading the password file ..." );
|
||||
if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
|
||||
{
|
||||
printf( " failed\n ! fopen returned NULL\n" );
|
||||
goto exit;
|
||||
}
|
||||
fgets( buf, 1024, f );
|
||||
fclose( f );
|
||||
|
||||
i = strlen( buf );
|
||||
if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
|
||||
if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
|
||||
opt.password = buf;
|
||||
}
|
||||
|
||||
/*
|
||||
* 1.1. Load the key
|
||||
*/
|
||||
printf( "\n . Loading the private key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = x509parse_keyfile( &rsa, opt.filename, opt.password );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
#ifdef POLARSSL_ERROR_C
|
||||
error_strerror( ret, buf, 1024 );
|
||||
#endif
|
||||
printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf );
|
||||
rsa_free( &rsa );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* 1.2 Print the key
|
||||
*/
|
||||
printf( " . Key information ...\n" );
|
||||
mpi_write_file( "N: ", &rsa.N, 16, NULL );
|
||||
mpi_write_file( "E: ", &rsa.E, 16, NULL );
|
||||
mpi_write_file( "D: ", &rsa.D, 16, NULL );
|
||||
mpi_write_file( "P: ", &rsa.P, 16, NULL );
|
||||
mpi_write_file( "Q: ", &rsa.Q, 16, NULL );
|
||||
mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
|
||||
mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL );
|
||||
mpi_write_file( "QP: ", &rsa.QP, 16, NULL );
|
||||
}
|
||||
else if( opt.mode == MODE_PUBLIC )
|
||||
{
|
||||
/*
|
||||
* 1.1. Load the key
|
||||
*/
|
||||
printf( "\n . Loading the public key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = x509parse_public_keyfile( &rsa, opt.filename );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
#ifdef POLARSSL_ERROR_C
|
||||
error_strerror( ret, buf, 1024 );
|
||||
#endif
|
||||
printf( " failed\n ! x509parse_public_key returned %d - %s\n\n", ret, buf );
|
||||
rsa_free( &rsa );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* 1.2 Print the key
|
||||
*/
|
||||
printf( " . Key information ...\n" );
|
||||
mpi_write_file( "N: ", &rsa.N, 16, NULL );
|
||||
mpi_write_file( "E: ", &rsa.E, 16, NULL );
|
||||
}
|
||||
else
|
||||
goto usage;
|
||||
|
||||
exit:
|
||||
|
||||
rsa_free( &rsa );
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
|
||||
POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user