2017-02-06 22:11:22 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-06 22:28:11 +00:00
|
|
|
#include <stdint.h>
|
2017-03-08 12:23:32 +00:00
|
|
|
#include "mbedtls/ecdh.h"
|
2017-02-06 22:28:11 +00:00
|
|
|
|
2017-02-06 22:11:22 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|
|
|
|
|
2017-02-06 22:11:22 +00:00
|
|
|
struct EphemeralPublicKey {
|
2017-02-07 00:04:01 +00:00
|
|
|
size_t num_bits;
|
2017-02-06 22:11:22 +00:00
|
|
|
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;
|
2017-02-06 22:11:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct EphemeralPrivateKey {
|
2017-02-07 00:04:01 +00:00
|
|
|
size_t num_bits;
|
2017-02-06 22:11:22 +00:00
|
|
|
uint64_t secret_key;
|
2017-03-08 12:23:32 +00:00
|
|
|
mbedtls_ecdh_context ctx;
|
2017-02-06 22:11:22 +00:00
|
|
|
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)
|
|
|
|
*/
|
2017-02-07 00:04:01 +00:00
|
|
|
int libp2p_crypto_ephemeral_keypair_generate(char* curve, struct EphemeralPrivateKey** private_key);
|
2017-02-08 16:08:05 +00:00
|
|
|
|
2017-03-08 17:18:29 +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);
|