c-libp2p/include/libp2p/crypto/rsa.h

80 lines
2.3 KiB
C
Raw Normal View History

2016-11-03 16:15:18 +00:00
#ifndef rsa_h
#define rsa_h
#include <stddef.h>
2016-11-03 16:15:18 +00:00
struct RsaPublicKey {
2018-12-03 13:20:51 +00:00
unsigned char* der;
size_t der_length;
};
2016-11-03 16:15:18 +00:00
struct RsaPrivateKey {
2016-11-10 17:04:48 +00:00
// the basics of a key pair
2016-11-09 16:22:25 +00:00
unsigned long long QP;
unsigned long long DQ;
unsigned long long DP;
unsigned long long Q;
unsigned long long P;
unsigned long long D;
unsigned long long E;
unsigned long long N;
2016-11-10 17:04:48 +00:00
// the keys in DER format
// private
2018-12-03 13:20:51 +00:00
unsigned char* der;
size_t der_length;
2016-11-10 17:04:48 +00:00
// public
char* public_key_der;
size_t public_key_length;
2016-11-03 16:15:18 +00:00
};
/**
* Convert a struct RsaPrivateKey to a struct PrivateKey
* @param in the RsaPrivateKey
* @returns a struct PrivateKey
*/
struct PrivateKey* libp2p_crypto_rsa_to_private_key(struct RsaPrivateKey* in);
/***
* Convert a PrivateKey struct to an RsaPrivateKey struct
* @param in the PrivateKey (NOTE: Must be of type KEYTYPE_RSA
* @returns the RsaPrivateKey or NULL on error
*/
struct RsaPrivateKey* libp2p_crypto_private_key_to_rsa(struct PrivateKey* in);
2016-11-03 16:15:18 +00:00
/**
* generate a new private key
* @param private_key the new private key
* @param num_bits_for_keypair the size of the key (1024 minimum)
* @returns true(1) on success
*/
2016-11-10 17:04:48 +00:00
int libp2p_crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long num_bits_for_keypair);
2016-11-03 16:15:18 +00:00
/**
* Use the private key DER to fill in the public key DER
* @param private_key the private key to use
* @reutrns true(1) on success
*/
int libp2p_crypto_rsa_private_key_fill_public_key(struct RsaPrivateKey* private_key);
/***
* Free resources used by RsaPrivateKey
* @param private_key the resources
* @returns 0
2016-11-07 20:11:58 +00:00
*/
2016-11-10 17:04:48 +00:00
int libp2p_crypto_rsa_rsa_private_key_free(struct RsaPrivateKey* private_key);
2017-02-09 08:34:12 +00:00
struct RsaPrivateKey* libp2p_crypto_rsa_rsa_private_key_new();
/**
* sign a message
* @param private_key the private key
* @param message the message to be signed
* @param message_length the length of message
* @param result the resultant signature. Note: should be pre-allocated and be the size of the private key (i.e. 2048)
* @returns true(1) on successs, otherwise false(0)
*/
2017-02-09 17:50:28 +00:00
int libp2p_crypto_rsa_sign(struct RsaPrivateKey* private_key, const char* message, size_t message_length, unsigned char** result, size_t* result_size);
int libp2p_crypto_rsa_verify(struct RsaPublicKey* public_key, const unsigned char* message, size_t message_length, const unsigned char* signature);
2016-11-03 16:15:18 +00:00
#endif /* rsa_h */