removed cryptopp, openssl shall be used instead for blinding (but not implemented yet)

This commit is contained in:
Gulliver 2024-07-14 10:18:41 +02:00
parent 96d25a0a9a
commit 2a33e41376
2 changed files with 200 additions and 201 deletions

View File

@ -7,6 +7,7 @@ enable_language(CXX)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS On) set(CMAKE_EXPORT_COMPILE_COMMANDS On)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
if(POLICY CMP0077) if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW) cmake_policy(SET CMP0077 NEW)
endif() endif()
@ -52,16 +53,16 @@ if(NOT expected_POPULATED)
endif(NOT expected_POPULATED) endif(NOT expected_POPULATED)
# add crypt++ (+cmake) library # add crypt++ (+cmake) library
set(CRYPTOPP_BUILD_TESTING Off) #set(CRYPTOPP_BUILD_TESTING Off)
set(CRYPTOPP_INSTALL Off) #set(CRYPTOPP_INSTALL Off)
if(NOT cryptopp_POPULATED) #if(NOT cryptopp_POPULATED)
FetchContent_Declare(cryptopp # FetchContent_Declare(cryptopp
GIT_REPOSITORY https://github.com/abdes/cryptopp-cmake.git # GIT_REPOSITORY https://github.com/abdes/cryptopp-cmake.git
GIT_TAG CRYPTOPP_8_7_0) # GIT_TAG CRYPTOPP_8_7_0_1)
FetchContent_Populate(cryptopp) # FetchContent_Populate(cryptopp)
add_subdirectory(${cryptopp_SOURCE_DIR} ${cryptopp_BINARY_DIR}) # add_subdirectory(${cryptopp_SOURCE_DIR} ${cryptopp_BINARY_DIR})
endif(NOT cryptopp_POPULATED) #endif(NOT cryptopp_POPULATED)
include(CTest) include(CTest)
enable_testing() enable_testing()
@ -94,7 +95,7 @@ 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 cryptopp::cryptopp) 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)

View File

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