mirror of
https://github.com/jakcron/nstool.git
synced 2024-12-22 10:45:28 +00:00
[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.
This commit is contained in:
parent
724fc26349
commit
01162b8187
|
@ -4,7 +4,7 @@
|
||||||
using namespace crypto::rsa;
|
using namespace crypto::rsa;
|
||||||
using namespace crypto::sha;
|
using namespace crypto::sha;
|
||||||
|
|
||||||
int GetWrappedHashType(HashType type)
|
int getWrappedHashType(HashType type)
|
||||||
{
|
{
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
|
@ -21,7 +21,7 @@ int GetWrappedHashType(HashType type)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t GetWrappedHashSize(HashType type)
|
uint32_t getWrappedHashSize(HashType type)
|
||||||
{
|
{
|
||||||
uint32_t size = 0;
|
uint32_t size = 0;
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ uint32_t GetWrappedHashSize(HashType type)
|
||||||
return size;
|
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;
|
int ret;
|
||||||
rsa_context ctx;
|
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.D, key.priv_exponent, ctx.len);
|
||||||
mpi_read_binary(&ctx.N, key.modulus, 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);
|
rsa_free(&ctx);
|
||||||
|
|
||||||
return ret;
|
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 };
|
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.E, public_exponent, sizeof(public_exponent));
|
||||||
mpi_read_binary(&ctx.N, key.modulus, ctx.len);
|
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);
|
rsa_free(&ctx);
|
||||||
|
|
||||||
return ret;
|
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;
|
int ret;
|
||||||
rsa_context ctx;
|
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.D, key.priv_exponent, ctx.len);
|
||||||
mpi_read_binary(&ctx.N, key.modulus, 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);
|
rsa_free(&ctx);
|
||||||
|
|
||||||
return ret;
|
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 };
|
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.E, public_exponent, sizeof(public_exponent));
|
||||||
mpi_read_binary(&ctx.N, key.modulus, ctx.len);
|
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);
|
rsa_free(&ctx);
|
||||||
|
|
||||||
return ret;
|
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;
|
int ret;
|
||||||
rsa_context ctx;
|
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.D, key.priv_exponent, ctx.len);
|
||||||
mpi_read_binary(&ctx.N, key.modulus, 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);
|
rsa_free(&ctx);
|
||||||
|
|
||||||
return ret;
|
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 };
|
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.E, public_exponent, sizeof(public_exponent));
|
||||||
mpi_read_binary(&ctx.N, key.modulus, ctx.len);
|
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);
|
rsa_free(&ctx);
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,14 @@ namespace crypto
|
||||||
|
|
||||||
bool operator==(const sRsa1024Key& other)
|
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;
|
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
|
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;
|
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)
|
#pragma pack (pop)
|
||||||
|
|
||||||
// rsa1024
|
namespace pkcs
|
||||||
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]);
|
// rsa1024
|
||||||
// rsa2048
|
int rsaSign(const sRsa1024Key& key, sha::HashType hash_type, const uint8_t* hash, uint8_t signature[kRsa1024Size]);
|
||||||
int RsaSign(const sRsa2048Key& key, sha::HashType hash_type, const uint8_t* hash, uint8_t signature[kRsa2048Size]);
|
int rsaVerify(const sRsa1024Key& key, sha::HashType hash_type, const uint8_t* hash, const uint8_t signature[kRsa1024Size]);
|
||||||
int RsaVerify(const sRsa2048Key& key, sha::HashType hash_type, const uint8_t* hash, const uint8_t signature[kRsa2048Size]);
|
// rsa2048
|
||||||
// rsa4096
|
int rsaSign(const sRsa2048Key& key, sha::HashType hash_type, const uint8_t* hash, uint8_t signature[kRsa2048Size]);
|
||||||
int RsaSign(const sRsa4096Key& key, sha::HashType hash_type, const uint8_t* hash, uint8_t signature[kRsa4096Size]);
|
int rsaVerify(const sRsa2048Key& key, sha::HashType hash_type, const uint8_t* hash, const uint8_t signature[kRsa2048Size]);
|
||||||
int RsaVerify(const sRsa4096Key& key, sha::HashType hash_type, const uint8_t* hash, const uint8_t signature[kRsa4096Size]);
|
// 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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue