강현태

add thesis

cmake_minimum_required(VERSION 3.6)
project("crypto_optimization")
add_subdirectory(src)
\ No newline at end of file
test : clean test.o
./test/bin/test.o > report.xml
test.o :
gcc test/src/test.c -L/usr/local/opt/openssl/lib -o test/bin/test.o -lcmocka -lssl -lcrypto
pull :
cd lib; \
git clone https://github.com/openssl/openssl.git; \
./config --prefix=
clean :
rm -f ~/test/bin/test.o report.xml
\ No newline at end of file
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.12
# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target
# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:
#=============================================================================
# Special targets provided by cmake.
# Disable implicit rules so canonical targets will work.
.SUFFIXES:
# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =
.SUFFIXES: .hpux_make_needs_suffix_list
# Suppress display of executed commands.
$(VERBOSE).SILENT:
# A target that is always out of date.
cmake_force:
.PHONY : cmake_force
#=============================================================================
# Set environment variables for the build.
# The shell in which to execute make rules.
SHELL = /bin/sh
# The CMake executable.
CMAKE_COMMAND = /Applications/CMake.app/Contents/bin/cmake
# The command to remove a file.
RM = /Applications/CMake.app/Contents/bin/cmake -E remove -f
# Escaping for special characters.
EQUALS = =
# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /Users/ganghyeontae/crypto-optimization
# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /Users/ganghyeontae/crypto-optimization
#=============================================================================
# Targets provided globally by CMake.
# Special rule for the target rebuild_cache
rebuild_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
/Applications/CMake.app/Contents/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache
# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast
# Special rule for the target edit_cache
edit_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
/Applications/CMake.app/Contents/bin/ccmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache
# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /Users/ganghyeontae/crypto-optimization/CMakeFiles /Users/ganghyeontae/crypto-optimization/CMakeFiles/progress.marks
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /Users/ganghyeontae/crypto-optimization/CMakeFiles 0
.PHONY : all
# The main clean target
clean:
$(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean
# The main clean target
clean/fast: clean
.PHONY : clean/fast
# Prepare targets for installation.
preinstall: all
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall
# Prepare targets for installation.
preinstall/fast:
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast
# clear depends
depend:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend
#=============================================================================
# Target rules for targets named MAIN
# Build rule for target.
MAIN: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 MAIN
.PHONY : MAIN
# fast build rule for target.
MAIN/fast:
$(MAKE) -f src/CMakeFiles/MAIN.dir/build.make src/CMakeFiles/MAIN.dir/build
.PHONY : MAIN/fast
# Help Target
help:
@echo "The following are some of the valid targets for this Makefile:"
@echo "... all (the default if no target is provided)"
@echo "... clean"
@echo "... depend"
@echo "... rebuild_cache"
@echo "... edit_cache"
@echo "... MAIN"
.PHONY : help
#=============================================================================
# Special targets to cleanup operation of make.
# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system
......
CORE_PATH=${PWD}
CORE_PATH=${PWD}
echo ${CORE_PATH}
cd ${CORE_PATH}/src/original
./Configure linux-generic32 no-shared no-threads no-engine no-hw no-asm no-ssl2 no-ssl3 -DL_ENDIAN --prefix=/Users/ganghyeontae/Downloads --openssldir=/Users/ganghyeontae/Downloads
make depend
make install CC=$CC RANLIB=$RANLIB LD=$LD MAKEDEPPROG=$CC PROCESSOR=ARM
cd ${CORE_PATH}/src/optimized
./Configure linux-generic32 no-shared no-threads no-engine no-hw no-asm no-ssl2 no-ssl3 -DL_ENDIAN --prefix=/Users/ganghyeontae/Downloads --openssldir=/Users/ganghyeontae/Downloads
make depend
make install CC=$CC RANLIB=$RANLIB LD=$LD MAKEDEPPROG=$CC PROCESSOR=ARM
This diff could not be displayed because it is too large.
/* WARNING: do not edit! */
/* Generated by Makefile from src/original/crypto/include/internal/bn_conf.h.in */
/*
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#ifndef HEADER_BN_CONF_H
# define HEADER_BN_CONF_H
/*
* The contents of this file are not used in the UEFI build, as
* both 32-bit and 64-bit builds are supported from a single run
* of the Configure script.
*/
/* Should we define BN_DIV2W here? */
/* Only one for the following should be defined */
#undef SIXTY_FOUR_BIT_LONG
#undef SIXTY_FOUR_BIT
#define THIRTY_TWO_BIT
#endif
/* WARNING: do not edit! */
/* Generated by Makefile from src/original/crypto/include/internal/dso_conf.h.in */
/*
* Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#ifndef HEADER_DSO_CONF_H
# define HEADER_DSO_CONF_H
# define DSO_DLFCN
# define HAVE_DLFCN_H
# define DSO_EXTENSION ".so"
#endif
<?xml version="1.0" encoding="UTF-8"?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6"/>
<Project>
<Option title="crypto_optimization"/>
<Option makefile_is_custom="1"/>
<Option compiler="gcc"/>
<Option virtualFolders="CMake Files\;CMake Files\src\;"/>
<Build>
<Target title="all">
<Option working_dir="/Users/ganghyeontae/crypto-optimization"/>
<Option type="4"/>
<MakeCommands>
<Build command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 all"/>
<CompileFile command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 clean"/>
</MakeCommands>
</Target>
<Target title="rebuild_cache">
<Option working_dir="/Users/ganghyeontae/crypto-optimization"/>
<Option type="4"/>
<MakeCommands>
<Build command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 rebuild_cache"/>
<CompileFile command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 clean"/>
</MakeCommands>
</Target>
<Target title="edit_cache">
<Option working_dir="/Users/ganghyeontae/crypto-optimization"/>
<Option type="4"/>
<MakeCommands>
<Build command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 edit_cache"/>
<CompileFile command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/Makefile&quot; VERBOSE=1 clean"/>
</MakeCommands>
</Target>
<Target title="MAIN">
<Option output="/Users/ganghyeontae/crypto-optimization/src/MAIN" prefix_auto="0" extension_auto="0"/>
<Option working_dir="/Users/ganghyeontae/crypto-optimization/src"/>
<Option object_output="./"/>
<Option type="1"/>
<Option compiler="gcc"/>
<Compiler>
<Add directory="/Users/ganghyeontae/crypto-optimization/include"/>
</Compiler>
<MakeCommands>
<Build command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/src/Makefile&quot; VERBOSE=1 MAIN"/>
<CompileFile command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/src/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/src/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/src/Makefile&quot; VERBOSE=1 clean"/>
</MakeCommands>
</Target>
<Target title="MAIN/fast">
<Option output="/Users/ganghyeontae/crypto-optimization/src/MAIN" prefix_auto="0" extension_auto="0"/>
<Option working_dir="/Users/ganghyeontae/crypto-optimization/src"/>
<Option object_output="./"/>
<Option type="1"/>
<Option compiler="gcc"/>
<Compiler>
<Add directory="/Users/ganghyeontae/crypto-optimization/include"/>
</Compiler>
<MakeCommands>
<Build command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/src/Makefile&quot; VERBOSE=1 MAIN/fast"/>
<CompileFile command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/src/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
<Clean command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/src/Makefile&quot; VERBOSE=1 clean"/>
<DistClean command="/usr/bin/make -f &quot;/Users/ganghyeontae/crypto-optimization/src/Makefile&quot; VERBOSE=1 clean"/>
</MakeCommands>
</Target>
</Build>
<Unit filename="/Users/ganghyeontae/crypto-optimization/src/main.c">
<Option target="MAIN"/>
</Unit>
<Unit filename="/Users/ganghyeontae/crypto-optimization/CMakeLists.txt">
<Option virtualFolder="CMake Files\"/>
</Unit>
<Unit filename="/Users/ganghyeontae/crypto-optimization/src/CMakeLists.txt">
<Option virtualFolder="CMake Files\src\"/>
</Unit>
</Project>
</CodeBlocks_project_file>
#!/bin/bash
DEFAULT_DOCKCROSS_IMAGE=thewtex/cross-compiler-linux-armv6
#------------------------------------------------------------------------------
# Helpers
#
err() {
echo -e >&2 ERROR: $@\\n
}
die() {
err $@
exit 1
}
has() {
# eg. has command update
local kind=$1
local name=$2
type -t $kind:$name | grep -q function
}
#------------------------------------------------------------------------------
# Command handlers
#
command:update-image() {
docker pull $FINAL_IMAGE
}
help:update-image() {
echo Pull the latest $FINAL_IMAGE .
}
command:update-script() {
if cmp -s <( docker run $FINAL_IMAGE ) $0; then
echo $0 is up to date
else
echo -n Updating $0 '... '
docker run $FINAL_IMAGE > $0 && echo ok
fi
}
help:update-image() {
echo Update $0 from $FINAL_IMAGE .
}
command:update() {
command:update-image
command:update-script
}
help:update() {
echo Pull the latest $FINAL_IMAGE, and then update $0 from that.
}
command:help() {
if [[ $# != 0 ]]; then
if ! has command $1; then
err \"$1\" is not an dockcross command
command:help
elif ! has help $1; then
err No help found for \"$1\"
else
help:$1
fi
else
cat >&2 <<ENDHELP
Usage: dockcross [options] [--] command [args]
By default, run the given *command* in an dockcross Docker container.
The *options* can be one of:
--args|-a Extra args to the *docker run* command
--image|-i Docker cross-compiler image to use
--config|-c Bash script to source before running this script
Additionally, there are special update commands:
update-image
update-script
update
For update command help use: $0 help <command>
ENDHELP
exit 1
fi
}
#------------------------------------------------------------------------------
# Option processing
#
while [[ $# != 0 ]]; do
case $1 in
--)
break
;;
--args|-a)
ARG_ARGS="$2"
shift 2
;;
--config|-c)
ARG_CONFIG="$2"
shift 2
;;
--image|-i)
ARG_IMAGE="$2"
shift 2
;;
-*)
err Unknown option \"$1\"
command:help
exit
;;
*)
break
;;
esac
done
# The precedence for options is:
# 1. command-line arguments
# 2. environment variables
# 3. defaults
# Source the config file if it exists
DEFAULT_DOCKCROSS_CONFIG=~/.dockcross
FINAL_CONFIG=${ARG_CONFIG-${DOCKCROSS_CONFIG-$DEFAULT_DOCKCROSS_CONFIG}}
[[ -f "$FINAL_CONFIG" ]] && source "$FINAL_CONFIG"
# Set the docker image
FINAL_IMAGE=${ARG_IMAGE-${DOCKCROSS_IMAGE-$DEFAULT_DOCKCROSS_IMAGE}}
# Set the docker run extra args (if any)
FINAL_ARGS=${ARG_ARGS-${DOCKCROSS_ARGS}}
# If we are not running via boot2docker
if [ -z $DOCKER_HOST ]; then
USER_IDS="-e BUILDER_UID=$( id -u ) -e BUILDER_GID=$( id -g ) -e BUILDER_USER=$( id -un ) -e BUILDER_GROUP=$( id -gn )"
fi
#------------------------------------------------------------------------------
# Now, finally, run the command in a container
#
docker run -i -t --rm \
-v $PWD:/build \
$USER_IDS \
$FINAL_ARGS \
$FINAL_IMAGE "$@"
################################################################################
#
# This image is not intended to be run manually.
#
# To create a dockcross helper script for the
# thewtex/cross-compiler-linux-armv7 image, run:
#
# docker run --rm thewtex/cross-compiler-linux-armv7 > dockcross-linux-armv7
# chmod +x dockcross-linux-armv7
#
# You may then wish to move the dockcross script to your PATH.
#
################################################################################
No preview for this file type
/*
* WARNING: do not edit!
* Generated by Makefile from include/openssl/opensslconf.h.in
* Generated by Makefile from src/original/include/openssl/opensslconf.h.in
*
* Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
*
......@@ -24,12 +24,6 @@ extern "C" {
* OpenSSL was configured with the following options:
*/
#ifndef OPENSSL_SYS_MACOSX
# define OPENSSL_SYS_MACOSX 1
#endif
#ifndef OPENSSL_NO_COMP
# define OPENSSL_NO_COMP
#endif
#ifndef OPENSSL_NO_MD2
# define OPENSSL_NO_MD2
#endif
......@@ -54,6 +48,9 @@ extern "C" {
#ifndef OPENSSL_NO_DEVCRYPTOENG
# define OPENSSL_NO_DEVCRYPTOENG
#endif
#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
# define OPENSSL_NO_EC_NISTP_64_GCC_128
#endif
#ifndef OPENSSL_NO_EGD
# define OPENSSL_NO_EGD
#endif
......@@ -93,11 +90,8 @@ extern "C" {
#ifndef OPENSSL_NO_WEAK_SSL_CIPHERS
# define OPENSSL_NO_WEAK_SSL_CIPHERS
#endif
#ifndef OPENSSL_NO_STATIC_ENGINE
# define OPENSSL_NO_STATIC_ENGINE
#endif
#ifndef OPENSSL_NO_AFALGENG
# define OPENSSL_NO_AFALGENG
#ifndef OPENSSL_NO_DYNAMIC_ENGINE
# define OPENSSL_NO_DYNAMIC_ENGINE
#endif
......@@ -184,14 +178,14 @@ extern "C" {
* The following are cipher-specific, but are part of the public API.
*/
#if !defined(OPENSSL_SYS_UEFI)
# undef BN_LLONG
# define BN_LLONG
/* Only one for the following should be defined */
# define SIXTY_FOUR_BIT_LONG
# undef SIXTY_FOUR_BIT_LONG
# undef SIXTY_FOUR_BIT
# undef THIRTY_TWO_BIT
# define THIRTY_TWO_BIT
#endif
#define RC4_INT unsigned int
#define RC4_INT unsigned char
#ifdef __cplusplus
}
......
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/src/optimized)
LINK_LIBRARIES(ssl crypto)
add_executable(MAIN main.c)
//
// Created by 강현태 on 06/10/2018.
//
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <string.h>
#include <stdio.h>
#include <openssl/rsa.h>
#define ASCII_START 32
#define ASCII_END 126
char* generateRandomString(int size) {
int i;
char *res = (char*)malloc(size + 1);
for(i = 0; i < size; i++) {
res[i] = (char) (rand()%(ASCII_END-ASCII_START))+ASCII_START;
}
res[i] = '\0';
return res;
}
static void rsa_normal_test(){
int i;
int bits = 2048; //key size
int buflen = 1024; //buffer suze
unsigned char *plaintext, *ciphertext, *randomstring;
int same;
BIGNUM *bn = BN_new();
BN_set_word(bn, RSA_F4);
//1. rsa구조체 생성
RSA *rsa = RSA_new();
//2. key pair(private,public) 생성
RSA_generate_key_ex(rsa, bits, bn, NULL);
//3. 본인의 public key로 암호화.
randomstring=plaintext=(unsigned char*)generateRandomString(buflen);
RSA_public_encrypt(buflen, plaintext, ciphertext, rsa,RSA_PKCS1_OAEP_PADDING);
//4. 본인의 private key로 복호화.
RSA_private_decrypt(buflen, ciphertext, plaintext, rsa,RSA_PKCS1_OAEP_PADDING);
//5. 원 평문과 일치하는지 확인
same = 1;
for(i=0;i<buflen;i++){
if(plaintext[i]!=randomstring[i]){
same=0;
break;
}
}
printf("%s \n",(same==1)?"통과":"다름");
}
int main(void){
srand(time(NULL));
rsa_normal_test();
}
\ No newline at end of file
......@@ -161,6 +161,7 @@ int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx)
{
printf("[INFO] BN mod exp recp 호출");
int i, j, bits, ret = 0, wstart, wend, window, wvalue;
int start = 1;
BIGNUM *aa;
......@@ -296,6 +297,7 @@ int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
{
printf("[INFO] BN mod exp mont 호출\n");
int i, j, bits, ret = 0, wstart, wend, window, wvalue;
int start = 1;
BIGNUM *d, *r;
......@@ -593,6 +595,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx,
BN_MONT_CTX *in_mont)
{
printf("[INFO] BN mod exp mont consttime 호출\n");
int i, bits, ret = 0, window, wvalue, wmask, window0;
int top;
BN_MONT_CTX *mont = NULL;
......@@ -1127,6 +1130,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
{
printf("[INFO] BN mod exp mont word 호출\n");
BN_MONT_CTX *mont = NULL;
int b, bits, ret = 0;
int r_is_one;
......@@ -1275,6 +1279,7 @@ int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx)
{
printf("[INFO] BN mod exp simple 호출\n");
int i, j, bits, ret = 0, wstart, wend, window, wvalue;
int start = 1;
BIGNUM *d;
......
......@@ -17,6 +17,7 @@ int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,
BN_CTX *ctx, BN_MONT_CTX *in_mont)
{
printf("[INFO] BN mod exp2 mot called!! \n");
int i, j, bits, b, bits1, bits2, ret =
0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;
int r_is_one = 1;
......
......@@ -12,6 +12,7 @@
int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
{
printf("[INFO] BN nnmod called!! \n");
/*
* like BN_mod, but returns non-negative remainder (i.e., 0 <= r < |d|
* always holds)
......@@ -28,6 +29,7 @@ int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
BN_CTX *ctx)
{
printf("[INFO] BN mod add called!! \n");
if (!BN_add(r, a, b))
return 0;
return BN_nnmod(r, r, m, ctx);
......@@ -48,6 +50,7 @@ int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
const BIGNUM *m)
{
printf("[INFO] BN mod add fixed top called!! \n");
size_t i, ai, bi, mtop = m->top;
BN_ULONG storage[1024 / BN_BITS2];
BN_ULONG carry, temp, mask, *rp, *tp = storage;
......@@ -95,6 +98,7 @@ int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
const BIGNUM *m)
{
printf("[INFO] BN mod add quick called!! \n");
int ret = bn_mod_add_fixed_top(r, a, b, m);
if (ret)
......@@ -106,6 +110,7 @@ int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
BN_CTX *ctx)
{
printf("[INFO] BN mod sub called!! \n");
if (!BN_sub(r, a, b))
return 0;
return BN_nnmod(r, r, m, ctx);
......@@ -128,6 +133,7 @@ int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
int bn_mod_sub_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
const BIGNUM *m)
{
printf("[INFO] BN mod sub fixed top called!! \n");
size_t i, ai, bi, mtop = m->top;
BN_ULONG borrow, carry, ta, tb, mask, *rp;
const BN_ULONG *ap, *bp;
......@@ -182,6 +188,7 @@ int bn_mod_sub_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
const BIGNUM *m)
{
printf("[INFO] BN mod sub quick called!! \n");
if (!BN_sub(r, a, b))
return 0;
if (r->neg)
......@@ -193,6 +200,7 @@ int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
BN_CTX *ctx)
{
printf("[INFO] BN mod mul called!! \n");
BIGNUM *t;
int ret = 0;
......@@ -221,6 +229,7 @@ int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
{
printf("[INFO] BN mod sqr called!! \n");
if (!BN_sqr(r, a, ctx))
return 0;
/* r->neg == 0, thus we don't need BN_nnmod */
......
......@@ -10,6 +10,7 @@
#include "internal/cryptlib.h"
#include "internal/bn_int.h"
#include "rsa_locl.h"
#include <stdio.h>
static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
......@@ -67,22 +68,25 @@ const RSA_METHOD *RSA_null_method(void)
static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding)
{
printf("[INFO] default public encrypt called!! \n");
BIGNUM *f, *ret;
int i, num = 0, r = -1;
int i,tmp, num = 0, r = -1;
unsigned char *buf = NULL;
BN_CTX *ctx = NULL;
if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
//만들어진 rsa->n의 비트값이 최댓값보다 더 크다면, 에러!
RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
return -1;
}
if (BN_ucmp(rsa->n, rsa->e) <= 0) {
//n과 e를 절댓값비교후 0보다 같거나 작다면, 에러!
RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
return -1;
}
/* for large moduli, enforce exponent limit */
/* 큰 moduli에 대해, exponent 한계값을 강제함 */
if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
......@@ -90,39 +94,55 @@ static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
}
}
// 컨텍스트 생성.
if ((ctx = BN_CTX_new()) == NULL)
goto err;
BN_CTX_start(ctx);
f = BN_CTX_get(ctx);
ret = BN_CTX_get(ctx);
num = BN_num_bytes(rsa->n);
//num 은 rsa->n의 바이트수
buf = OPENSSL_malloc(num);
//buf는 num만큼 동적할당.
if (ret == NULL || buf == NULL) {
RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);
goto err;
}
switch (padding) {
case RSA_PKCS1_PADDING:
i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
break;
case RSA_PKCS1_OAEP_PADDING:
i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
break;
case RSA_SSLV23_PADDING:
i = RSA_padding_add_SSLv23(buf, num, from, flen);
break;
case RSA_NO_PADDING:
i = RSA_padding_add_none(buf, num, from, flen);
break;
default:
RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
goto err;
//암호화 최소단위가 있을경우 패딩!
printf("[INFO] before padding buffer : ");
for(tmp=0;tmp<num;tmp++){
printf("%x ",buf[tmp]);
}
printf("\n");
switch (padding) {
case RSA_PKCS1_PADDING:
i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
break;
case RSA_PKCS1_OAEP_PADDING:
i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
break;
case RSA_SSLV23_PADDING:
i = RSA_padding_add_SSLv23(buf, num, from, flen);
break;
case RSA_NO_PADDING:
i = RSA_padding_add_none(buf, num, from, flen);
break;
default:
RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
goto err;
}
printf("[INFO] after padding buffer : ");
for(tmp=0;tmp<num;tmp++){
printf("%x ",buf[tmp]);
}
printf("\n");
if (i <= 0)
goto err;
if (BN_bin2bn(buf, num, f) == NULL)
if (BN_bin2bn(buf, num, f) == NULL) //원본, 사이즈, 저장될 bignum
//buf를 2진수 -> bignum으로 바꾼 결과(f에 저장)가 NULL이면, 에러!
goto err;
if (BN_ucmp(f, rsa->n) >= 0) {
......@@ -136,16 +156,20 @@ static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
rsa->n, ctx))
goto err;
// ret = f^e % n을 계산함. (암호화)
if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
rsa->_method_mod_n))
goto err;
/*
* BN_bn2binpad puts in leading 0 bytes if the number is less than
* the length of the modulus.
* BN_bn2binpad는 바이트 0으로 채운다. 숫자가 modulus의 길이보다 작다면
*/
r = BN_bn2binpad(ret, to, num);
printf("[INFO] after mod exp : ");
for(tmp=0;tmp<r;tmp++){
printf("%x ",to[tmp]);
}
printf("\n");
err:
if (ctx != NULL)
BN_CTX_end(ctx);
......@@ -591,6 +615,7 @@ static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
{
printf("[INFO] ossl mod exp called!");
BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];
int ret = 0, i, ex_primes = 0, smooth = 0;
RSA_PRIME_INFO *pinfo;
......
//
// Created by 강현태 on 06/10/2018.
//
#ifndef CRYPTO_OPTIMIZATION_TIMER_H
#define CRYPTO_OPTIMIZATION_TIMER_H
long tic(void);
long toc(long prev);
#endif //CRYPTO_OPTIMIZATION_TIMER_H