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

64 lines
1.7 KiB
C
Raw Normal View History

#pragma once
#include <stdint.h>
2017-03-08 12:23:32 +00:00
#include "mbedtls/ecdh.h"
/**
* General helpers for ephemeral keys
*/
2017-02-08 16:08:05 +00:00
struct StretchedKey {
2017-03-07 00:03:04 +00:00
unsigned char* iv;
2017-02-08 16:08:05 +00:00
size_t iv_size;
2017-03-07 00:03:04 +00:00
unsigned char* cipher_key;
2017-02-08 16:08:05 +00:00
size_t cipher_size;
2017-03-02 21:14:52 +00:00
unsigned char* mac_key;
2017-02-08 16:08:05 +00:00
size_t mac_size;
};
struct EphemeralPublicKey {
size_t num_bits;
uint64_t x;
uint64_t y;
2017-02-08 16:08:05 +00:00
unsigned char* bytes; // a public key in bytes (the combination of X and Y)
size_t bytes_size;
unsigned char* shared_key;
size_t shared_key_size;
};
struct EphemeralPrivateKey {
size_t num_bits;
uint64_t secret_key;
2017-03-08 12:23:32 +00:00
mbedtls_ecdh_context ctx;
struct EphemeralPublicKey* public_key;
};
/**
* Generate a Ephemeral Public Key as well as a shared key
* @param curve the curve to use (P-256, P-384, or P-521)
* @param private_key where to store the private key
* @reutrns true(1) on success, otherwise false(0)
*/
int libp2p_crypto_ephemeral_keypair_generate(char* curve, struct EphemeralPrivateKey** private_key);
2017-02-08 16:08:05 +00:00
/**
* Generate a shared secret
* @param private_key the context, also where it puts the shared secret
* @param remote_public_key the key the remote gave us
* @param remote_public_key_size the size of the remote public key
* @reutrns true(1) on success, otherwise false(0)
*/
int libp2p_crypto_ephemeral_generate_shared_secret(struct EphemeralPrivateKey* private_key, const unsigned char* remote_public_key, size_t remote_public_key_size);
2017-02-08 17:32:41 +00:00
/***
* Remove resources used by generation of ephemeral private key
* @param in the key to destroy
*/
void libp2p_crypto_ephemeral_key_free( struct EphemeralPrivateKey* in);
2017-02-08 16:08:05 +00:00
/**
* Routines to help with the StretchedKey struct
*/
struct StretchedKey* libp2p_crypto_ephemeral_stretched_key_new();
void libp2p_crypto_ephemeral_stretched_key_free(struct StretchedKey* in);