From 01162b81874f8cdaa1c8d1bea92f441ca5de7738 Mon Sep 17 00:00:00 2001 From: jakcron Date: Thu, 6 Jul 2017 21:11:03 +1000 Subject: [PATCH] [crypto] Added more operators to sRsa*Key, current rsaSign/rsaVerify functions moved to rsa::pkcs namespace, rsa::pss and rsa::oaep to be added in future. --- lib/crypto/rsa.cpp | 28 ++++++++++++++-------------- lib/crypto/rsa.h | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/lib/crypto/rsa.cpp b/lib/crypto/rsa.cpp index b848ddf..55195eb 100644 --- a/lib/crypto/rsa.cpp +++ b/lib/crypto/rsa.cpp @@ -4,7 +4,7 @@ using namespace crypto::rsa; using namespace crypto::sha; -int GetWrappedHashType(HashType type) +int getWrappedHashType(HashType type) { switch (type) { @@ -21,7 +21,7 @@ int GetWrappedHashType(HashType type) return 0; } -uint32_t GetWrappedHashSize(HashType type) +uint32_t getWrappedHashSize(HashType type) { uint32_t size = 0; @@ -39,7 +39,7 @@ uint32_t GetWrappedHashSize(HashType type) return size; } -int crypto::rsa::RsaSign(const sRsa1024Key & key, HashType hash_type, const uint8_t * hash, uint8_t signature[kRsa1024Size]) +int crypto::rsa::pkcs::rsaSign(const sRsa1024Key & key, HashType hash_type, const uint8_t * hash, uint8_t signature[kRsa1024Size]) { int ret; rsa_context ctx; @@ -49,14 +49,14 @@ int crypto::rsa::RsaSign(const sRsa1024Key & key, HashType hash_type, const uint mpi_read_binary(&ctx.D, key.priv_exponent, ctx.len); mpi_read_binary(&ctx.N, key.modulus, ctx.len); - ret = rsa_rsassa_pkcs1_v15_sign(&ctx, RSA_PRIVATE, GetWrappedHashType(hash_type), GetWrappedHashSize(hash_type), hash, signature); + ret = rsa_rsassa_pkcs1_v15_sign(&ctx, RSA_PRIVATE, getWrappedHashType(hash_type), getWrappedHashSize(hash_type), hash, signature); rsa_free(&ctx); return ret; } -int crypto::rsa::RsaVerify(const sRsa1024Key & key, HashType hash_type, const uint8_t * hash, const uint8_t signature[kRsa1024Size]) +int crypto::rsa::pkcs::rsaVerify(const sRsa1024Key & key, HashType hash_type, const uint8_t * hash, const uint8_t signature[kRsa1024Size]) { static const uint8_t public_exponent[3] = { 0x01, 0x00, 0x01 }; @@ -68,14 +68,14 @@ int crypto::rsa::RsaVerify(const sRsa1024Key & key, HashType hash_type, const ui mpi_read_binary(&ctx.E, public_exponent, sizeof(public_exponent)); mpi_read_binary(&ctx.N, key.modulus, ctx.len); - ret = rsa_rsassa_pkcs1_v15_verify(&ctx, RSA_PUBLIC, GetWrappedHashType(hash_type), GetWrappedHashSize(hash_type), hash, signature); + ret = rsa_rsassa_pkcs1_v15_verify(&ctx, RSA_PUBLIC, getWrappedHashType(hash_type), getWrappedHashSize(hash_type), hash, signature); rsa_free(&ctx); return ret; } -int crypto::rsa::RsaSign(const sRsa2048Key & key, HashType hash_type, const uint8_t * hash, uint8_t signature[kRsa2048Size]) +int crypto::rsa::pkcs::rsaSign(const sRsa2048Key & key, HashType hash_type, const uint8_t * hash, uint8_t signature[kRsa2048Size]) { int ret; rsa_context ctx; @@ -85,14 +85,14 @@ int crypto::rsa::RsaSign(const sRsa2048Key & key, HashType hash_type, const uint mpi_read_binary(&ctx.D, key.priv_exponent, ctx.len); mpi_read_binary(&ctx.N, key.modulus, ctx.len); - ret = rsa_rsassa_pkcs1_v15_sign(&ctx, RSA_PRIVATE, GetWrappedHashType(hash_type), GetWrappedHashSize(hash_type), hash, signature); + ret = rsa_rsassa_pkcs1_v15_sign(&ctx, RSA_PRIVATE, getWrappedHashType(hash_type), getWrappedHashSize(hash_type), hash, signature); rsa_free(&ctx); return ret; } -int crypto::rsa::RsaVerify(const sRsa2048Key & key, HashType hash_type, const uint8_t * hash, const uint8_t signature[kRsa2048Size]) +int crypto::rsa::pkcs::rsaVerify(const sRsa2048Key & key, HashType hash_type, const uint8_t * hash, const uint8_t signature[kRsa2048Size]) { static const uint8_t public_exponent[3] = { 0x01, 0x00, 0x01 }; @@ -104,14 +104,14 @@ int crypto::rsa::RsaVerify(const sRsa2048Key & key, HashType hash_type, const ui mpi_read_binary(&ctx.E, public_exponent, sizeof(public_exponent)); mpi_read_binary(&ctx.N, key.modulus, ctx.len); - ret = rsa_rsassa_pkcs1_v15_verify(&ctx, RSA_PUBLIC, GetWrappedHashType(hash_type), GetWrappedHashSize(hash_type), hash, signature); + ret = rsa_rsassa_pkcs1_v15_verify(&ctx, RSA_PUBLIC, getWrappedHashType(hash_type), getWrappedHashSize(hash_type), hash, signature); rsa_free(&ctx); return ret; } -int crypto::rsa::RsaSign(const sRsa4096Key & key, HashType hash_type, const uint8_t * hash, uint8_t signature[kRsa4096Size]) +int crypto::rsa::pkcs::rsaSign(const sRsa4096Key & key, HashType hash_type, const uint8_t * hash, uint8_t signature[kRsa4096Size]) { int ret; rsa_context ctx; @@ -121,14 +121,14 @@ int crypto::rsa::RsaSign(const sRsa4096Key & key, HashType hash_type, const uint mpi_read_binary(&ctx.D, key.priv_exponent, ctx.len); mpi_read_binary(&ctx.N, key.modulus, ctx.len); - ret = rsa_rsassa_pkcs1_v15_sign(&ctx, RSA_PRIVATE, GetWrappedHashType(hash_type), GetWrappedHashSize(hash_type), hash, signature); + ret = rsa_rsassa_pkcs1_v15_sign(&ctx, RSA_PRIVATE, getWrappedHashType(hash_type), getWrappedHashSize(hash_type), hash, signature); rsa_free(&ctx); return ret; } -int crypto::rsa::RsaVerify(const sRsa4096Key & key, HashType hash_type, const uint8_t * hash, const uint8_t signature[kRsa4096Size]) +int crypto::rsa::pkcs::rsaVerify(const sRsa4096Key & key, HashType hash_type, const uint8_t * hash, const uint8_t signature[kRsa4096Size]) { static const uint8_t public_exponent[3] = { 0x01, 0x00, 0x01 }; @@ -140,7 +140,7 @@ int crypto::rsa::RsaVerify(const sRsa4096Key & key, HashType hash_type, const ui mpi_read_binary(&ctx.E, public_exponent, sizeof(public_exponent)); mpi_read_binary(&ctx.N, key.modulus, ctx.len); - ret = rsa_rsassa_pkcs1_v15_verify(&ctx, RSA_PUBLIC, GetWrappedHashType(hash_type), GetWrappedHashSize(hash_type), hash, signature); + ret = rsa_rsassa_pkcs1_v15_verify(&ctx, RSA_PUBLIC, getWrappedHashType(hash_type), getWrappedHashSize(hash_type), hash, signature); rsa_free(&ctx); diff --git a/lib/crypto/rsa.h b/lib/crypto/rsa.h index f87b9e4..d188083 100644 --- a/lib/crypto/rsa.h +++ b/lib/crypto/rsa.h @@ -35,7 +35,14 @@ namespace crypto bool operator==(const sRsa1024Key& other) { - return memcmp(this->modulus, other.modulus, kRsa1024Size) == 0 && memcmp(this->priv_exponent, other.priv_exponent, kRsa1024Size) == 0 && memcpy(this->public_exponent, other.public_exponent, kRsaPublicExponentSize) == 0; + return memcmp(this->modulus, other.modulus, kRsa1024Size) == 0 \ + && memcmp(this->priv_exponent, other.priv_exponent, kRsa1024Size) == 0 \ + && memcpy(this->public_exponent, other.public_exponent, kRsaPublicExponentSize) == 0; + } + + bool operator!=(const sRsa1024Key& other) + { + return !operator==(other); } }; @@ -56,6 +63,11 @@ namespace crypto { return memcmp(this->modulus, other.modulus, kRsa2048Size) == 0 && memcmp(this->priv_exponent, other.priv_exponent, kRsa2048Size) == 0 && memcpy(this->public_exponent, other.public_exponent, kRsaPublicExponentSize) == 0; } + + bool operator!=(const sRsa2048Key& other) + { + return !operator==(other); + } }; struct sRsa4096Key @@ -75,17 +87,25 @@ namespace crypto { return memcmp(this->modulus, other.modulus, kRsa4096Size) == 0 && memcmp(this->priv_exponent, other.priv_exponent, kRsa4096Size) == 0 && memcpy(this->public_exponent, other.public_exponent, kRsaPublicExponentSize) == 0; } + + bool operator!=(const sRsa4096Key& other) + { + return !operator==(other); + } }; #pragma pack (pop) - // rsa1024 - int RsaSign(const sRsa1024Key& key, sha::HashType hash_type, const uint8_t* hash, uint8_t signature[kRsa1024Size]); - int RsaVerify(const sRsa1024Key& key, sha::HashType hash_type, const uint8_t* hash, const uint8_t signature[kRsa1024Size]); - // rsa2048 - int RsaSign(const sRsa2048Key& key, sha::HashType hash_type, const uint8_t* hash, uint8_t signature[kRsa2048Size]); - int RsaVerify(const sRsa2048Key& key, sha::HashType hash_type, const uint8_t* hash, const uint8_t signature[kRsa2048Size]); - // rsa4096 - int RsaSign(const sRsa4096Key& key, sha::HashType hash_type, const uint8_t* hash, uint8_t signature[kRsa4096Size]); - int RsaVerify(const sRsa4096Key& key, sha::HashType hash_type, const uint8_t* hash, const uint8_t signature[kRsa4096Size]); + namespace pkcs + { + // rsa1024 + int rsaSign(const sRsa1024Key& key, sha::HashType hash_type, const uint8_t* hash, uint8_t signature[kRsa1024Size]); + int rsaVerify(const sRsa1024Key& key, sha::HashType hash_type, const uint8_t* hash, const uint8_t signature[kRsa1024Size]); + // rsa2048 + int rsaSign(const sRsa2048Key& key, sha::HashType hash_type, const uint8_t* hash, uint8_t signature[kRsa2048Size]); + int rsaVerify(const sRsa2048Key& key, sha::HashType hash_type, const uint8_t* hash, const uint8_t signature[kRsa2048Size]); + // rsa4096 + int rsaSign(const sRsa4096Key& key, sha::HashType hash_type, const uint8_t* hash, uint8_t signature[kRsa4096Size]); + int rsaVerify(const sRsa4096Key& key, sha::HashType hash_type, const uint8_t* hash, const uint8_t signature[kRsa4096Size]); + } } }