2 Commits

Author SHA1 Message Date
d0794efb1e added test_crypto.cpp with first code for rsa blinding 2023-04-20 22:56:58 +02:00
a257fe227a included crypto++ lib 2023-04-20 22:56:58 +02:00
7 changed files with 208 additions and 69 deletions

View File

@ -1,11 +0,0 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"

View File

@ -19,44 +19,31 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [ubuntu-latest, os: [ubuntu-latest, macos-latest, windows-latest]
macos-latest,
windows-latest]
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Prepare dependencies - name: Prepare dependencies
run: | run: |
if [ "$RUNNER_OS" == "Linux" ]; then if [ "$RUNNER_OS" == "Linux" ]; then
sudo apt-get update && \ sudo apt-get update && \
sudo apt-get install -yq \ sudo apt-get install -yq \
libasio-dev \ libboost-system-dev \
libssl-dev zlib1g-dev \ libboost-date-time-dev \
cmake graphviz doxygen cmake \
elif [ "$RUNNER_OS" == "Windows" ]; then graphviz doxygen
VCPKG_DEFAULT_TRIPLET=x64-windows vcpkg install elif [ "$RUNNER_OS" == "Windows" ]; then
elif [ "$RUNNER_OS" == "macOS" ]; then choco install boost-msvc-14.3 graphviz doxygen.install
brew install asio openssl zlib doxygen graphviz elif [ "$RUNNER_OS" == "macOS" ]; then
else brew install boost graphviz doxygen
echo "$RUNNER_OS not supported" else
exit 1 echo "$RUNNER_OS not supported"
fi exit 1
shell: bash fi
shell: bash
- name: Configure CMake - name: Configure CMake
run: | run: cmake -B build
if [ "$RUNNER_OS" == "Windows" ]; then shell: bash
cmake \
-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake \
-B build
elif [ "$RUNNER_OS" == "macOS" ]; then
cmake \
-B build
else
cmake \
-B build
fi
shell: bash
- name: Build - name: Build
# Build your program with the given configuration # Build your program with the given configuration
run: cmake --build build --config ${{env.BUILD_TYPE}} run: cmake --build build --config ${{env.BUILD_TYPE}}

View File

@ -15,12 +15,7 @@ jobs:
- name: Checkout 🛎️ - name: Checkout 🛎️
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Prepare dependencies - name: Prepare dependencies
run: | run: sudo apt-get update && sudo apt-get install -yq libboost-system-dev libboost-date-time-dev cmake graphviz doxygen
sudo apt-get update && \
sudo apt-get install -yq \
libasio-dev \
libssl-dev zlib1g-dev \
cmake graphviz doxygen
- name: configure - name: configure
run: cmake -B build -S . -DEXPATPP_BUILD_DOCS=ON run: cmake -B build -S . -DEXPATPP_BUILD_DOCS=ON
- name: clean generated docs dir - name: clean generated docs dir

View File

@ -1,11 +1,8 @@
cmake_minimum_required(VERSION 3.15.0 FATAL_ERROR) cmake_minimum_required(VERSION 3.1.3)
project(oc-issuer VERSION 0.0.2 LANGUAGES CXX)
enable_language(C) enable_language(C)
enable_language(CXX) enable_language(CXX)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS On)
if(POLICY CMP0077) if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW) cmake_policy(SET CMP0077 NEW)
@ -17,6 +14,9 @@ else()
add_compile_options(-Wall -Wextra -pedantic) add_compile_options(-Wall -Wextra -pedantic)
endif() endif()
project(oc-issuer VERSION 0.0.2 LANGUAGES CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include(FetchContent) include(FetchContent)
@ -27,12 +27,11 @@ set(CROW_BUILD_EXAMPLES Off)
set(CROW_BUILD_TOOLS Off) set(CROW_BUILD_TOOLS Off)
set(CROW_BUILD_TESTS Off) set(CROW_BUILD_TESTS Off)
set(CROW_BUILD_DOCS Off) set(CROW_BUILD_DOCS Off)
set(CROW_FEATURES "ssl;compression")
# add crow project to the build # add crow project to the build
FetchContent_Declare(crow FetchContent_Declare(crow
GIT_REPOSITORY https://github.com/CrowCpp/Crow.git GIT_REPOSITORY https://github.com/CrowCpp/Crow.git
GIT_TAG v1.2.0 GIT_TAG v1.0+5
) )
if(NOT crow_POPULATED) if(NOT crow_POPULATED)
@ -49,15 +48,27 @@ if(NOT expected_POPULATED)
FetchContent_Populate(expected) FetchContent_Populate(expected)
endif(NOT expected_POPULATED) endif(NOT expected_POPULATED)
# add crypt++ (+cmake) library
set(CRYPTOPP_BUILD_TESTING Off)
set(CRYPTOPP_INSTALL Off)
if(NOT cryptopp_POPULATED)
FetchContent_Declare(cryptopp
GIT_REPOSITORY https://github.com/abdes/cryptopp-cmake.git
GIT_TAG CRYPTOPP_8_7_0)
FetchContent_Populate(cryptopp)
add_subdirectory(${cryptopp_SOURCE_DIR} ${cryptopp_BINARY_DIR})
endif(NOT cryptopp_POPULATED)
include(CTest) include(CTest)
enable_testing() enable_testing()
set(CATCH_INSTALL_DOCS Off) set(CATCH_INSTALL_DOCS Off)
set(CATCH_INSTALL_EXTRAS Off) set(CATCH_INSTALL_EXTRAS Off)
FetchContent_Declare( FetchContent_Declare(
Catch2 Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.6.0 GIT_TAG v3.3.1
) )
FetchContent_MakeAvailable(Catch2) FetchContent_MakeAvailable(Catch2)
@ -80,14 +91,17 @@ set(LIB_SOURCES
src/big_int.hpp src/big_int.cpp ) src/big_int.hpp src/big_int.cpp )
add_library(oc-mint-lib ${LIB_SOURCES}) add_library(oc-mint-lib ${LIB_SOURCES})
target_link_libraries(oc-mint-lib PUBLIC Crow::Crow) target_link_libraries(oc-mint-lib PUBLIC Crow::Crow cryptopp::cryptopp)
target_include_directories(oc-mint-lib PUBLIC ${expected_SOURCE_DIR}/include src) target_include_directories(oc-mint-lib PUBLIC ${expected_SOURCE_DIR}/include src)
add_executable(${PROJECT_NAME} src/main.cpp) add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE oc-mint-lib INTERFACE tl::expected::expected) target_link_libraries(${PROJECT_NAME} PRIVATE oc-mint-lib INTERFACE tl::expected::expected)
## these are unittests that can be run on any platform ## these are unittests that can be run on any platform
add_executable(tests test/test_big_int.cpp test/test.cpp) add_executable(tests test/test_big_int.cpp
test/test_json_s8n.cpp
test/test_crypto.cpp)
target_link_libraries(tests target_link_libraries(tests
oc-mint-lib oc-mint-lib
Catch2::Catch2WithMain) Catch2::Catch2WithMain)

163
test/test_crypto.cpp Normal file
View File

@ -0,0 +1,163 @@
#include <catch2/catch_test_macros.hpp>
#include "cryptlib.h"
#include "integer.h"
#include "nbtheory.h"
#include "osrng.h"
#include "rsa.h"
#include "sha.h"
#include <iostream>
#include <stdexcept>
CryptoPP::Integer blind_signature(unsigned char *msg,
size_t msg_len,
const CryptoPP::RSA::PublicKey& pub_key,
const CryptoPP::RSA::PrivateKey& priv_key) {
using namespace CryptoPP;
using std::cout;
using std::endl;
AutoSeededRandomPool prng;
// Convenience
const Integer &n = pub_key.GetModulus();
const Integer &e = pub_key.GetPublicExponent();
const Integer &d = priv_key.GetPrivateExponent();
// For sizing the hashed message buffer. This should be SHA256 size.
const size_t sig_size = UnsignedMin(SHA256::BLOCKSIZE, n.ByteCount());
// Scratch
SecByteBlock buff_1, buff_2, buff_3;
SecByteBlock orig(msg, msg_len);
Integer m(orig.data(), orig.size());
cout << "Message: " << std::hex << m << endl;
// Hash message per Rabin (1979)
buff_1.resize(sig_size);
SHA256 hash_1;
hash_1.CalculateTruncatedDigest(buff_1, buff_1.size(), orig, orig.size());
// H(m) as Integer
Integer hm(buff_1.data(), buff_1.size());
cout << "H(m): " << std::hex << hm << endl;
// Alice blinding
Integer r;
do {
r.Randomize(prng, Integer::One(), n - Integer::One());
} while (!RelativelyPrime(r, n));
// Blinding factor
Integer b = a_exp_b_mod_c(r, e, n);
cout << "Random: " << std::hex << b << endl;
// Alice blinded message
Integer mm = a_times_b_mod_c(hm, b, n);
cout << "Blind msg: " << std::hex << mm << endl;
// Bob sign
Integer ss = priv_key.CalculateInverse(prng, mm);
cout << "Blind sign: " << ss << endl;
return ss;
}
TEST_CASE("cryptopp", "[crypto]") {
using namespace CryptoPP;
using std::cout;
using std::endl;
using std::runtime_error;
// Bob artificially small key pair
AutoSeededRandomPool prng;
RSA::PrivateKey priv_key;
priv_key.GenerateRandomWithKeySize(prng, 64U);
RSA::PublicKey pub_key(priv_key);
// Convenience
const Integer &n = pub_key.GetModulus();
const Integer &e = pub_key.GetPublicExponent();
const Integer &d = priv_key.GetPrivateExponent();
// Print params
cout << "Pub mod: " << std::hex << pub_key.GetModulus() << endl;
cout << "Pub exp: " << std::hex << e << endl;
cout << "Priv mod: " << std::hex << priv_key.GetModulus() << endl;
cout << "Priv exp: " << std::hex << d << endl;
// For sizing the hashed message buffer. This should be SHA256 size.
const size_t sig_size = UnsignedMin(SHA256::BLOCKSIZE, n.ByteCount());
// Scratch
SecByteBlock buff_1, buff_2, buff_3;
// Alice original message to be signed by Bob
SecByteBlock orig((const byte *)"secret", 6U);
Integer m(orig.data(), orig.size());
cout << "Message: " << std::hex << m << endl;
// Hash message per Rabin (1979)
buff_1.resize(sig_size);
SHA256 hash_1;
hash_1.CalculateTruncatedDigest(buff_1, buff_1.size(), orig, orig.size());
// H(m) as Integer
Integer hm(buff_1.data(), buff_1.size());
cout << "H(m): " << std::hex << hm << endl;
// Alice blinding
Integer r;
do {
r.Randomize(prng, Integer::One(), n - Integer::One());
} while (!RelativelyPrime(r, n));
// Blinding factor
Integer b = a_exp_b_mod_c(r, e, n);
cout << "Random: " << std::hex << b << endl;
// Alice blinded message
Integer mm = a_times_b_mod_c(hm, b, n);
cout << "Blind msg: " << std::hex << mm << endl;
// Bob sign
Integer ss = priv_key.CalculateInverse(prng, mm);
cout << "Blind sign: " << ss << endl;
// Alice checks s(s'(x)) = x. This is from Chaum's paper
Integer c = pub_key.ApplyFunction(ss);
cout << "Check sign: " << c << endl;
if (c != mm) {
throw runtime_error("Alice cross-check failed");
}
// Alice remove blinding
Integer s = a_times_b_mod_c(ss, r.InverseMod(n), n);
cout << "Unblind sign: " << s << endl;
// Eve verifies
Integer v = pub_key.ApplyFunction(s);
cout << "Verify: " << std::hex << v << endl;
// Convert to a string
size_t req = v.MinEncodedSize();
buff_2.resize(req);
v.Encode(&buff_2[0], buff_2.size());
// Hash message per Rabin (1979)
buff_3.resize(sig_size);
SHA256 hash_2;
hash_2.CalculateTruncatedDigest(buff_3, buff_3.size(), orig, orig.size());
// Constant time compare
bool equal = buff_2.size() == buff_3.size() &&
VerifyBufsEqual(buff_2.data(), buff_3.data(), buff_3.size());
if (!equal) {
throw runtime_error("Eve verified failed");
}
cout << "Verified signature" << endl;
}

View File

@ -12,7 +12,7 @@ TEST_CASE( "PublicKey::to_json", "[to_json]" ) {
REQUIRE( json["modulus"].dump() == "\"" + k.modulus.to_string() + "\"" ); REQUIRE( json["modulus"].dump() == "\"" + k.modulus.to_string() + "\"" );
REQUIRE( json["public_exponent"].dump() == "\"" + k.public_exponent.to_string()+"\"" ); REQUIRE( json["public_exponent"].dump() == "\"" + k.public_exponent.to_string()+"\"" );
REQUIRE( json["type"].dump() == "\"rsa public key\"" ); REQUIRE( json["type"].dump() == "\"rsa public key\"" );
REQUIRE( json.keys().size() == 3 ); REQUIRE( json.keys().size() == 3U );
} }
TEST_CASE("RequestCDDCSerial::from_string", "[from_string]") { TEST_CASE("RequestCDDCSerial::from_string", "[from_string]") {

View File

@ -1,9 +0,0 @@
{
"name": "oc-mint-cpp",
"version-string": "master",
"dependencies": [
"asio",
"openssl",
"zlib"
]
}