KEYGEN: support import/export of keys
This commit is contained in:
parent
a42b65505a
commit
8871b496c2
104
src/keypair.c
104
src/keypair.c
@ -26,14 +26,18 @@
|
||||
* @brief key creation and operations
|
||||
*/
|
||||
|
||||
#include "ascii_poly.h"
|
||||
#include "context.h"
|
||||
#include "file.h"
|
||||
#include "keypair.h"
|
||||
#include "ntru_string.h"
|
||||
#include "poly.h"
|
||||
|
||||
#include <fmpz_poly.h>
|
||||
#include <fmpz.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
@ -87,6 +91,106 @@ _return:
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
void
|
||||
export_public_key(char const * const filename,
|
||||
fmpz_poly_t pub,
|
||||
ntru_context *ctx)
|
||||
{
|
||||
string *pub_string;
|
||||
|
||||
pub_string = poly_to_base64(pub, ctx);
|
||||
write_file(pub_string, filename);
|
||||
|
||||
string_delete(pub_string);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
void
|
||||
export_priv_key(char const * const filename,
|
||||
fmpz_poly_t priv,
|
||||
ntru_context *ctx)
|
||||
{
|
||||
string *priv_string;
|
||||
fmpz_poly_t priv_u;
|
||||
|
||||
fmpz_poly_init(priv_u);
|
||||
fmpz_poly_set(priv_u, priv);
|
||||
fmpz_poly_mod_unsigned(priv_u, ctx->p);
|
||||
|
||||
priv_string = poly_to_base64(priv_u, ctx);
|
||||
write_file(priv_string, filename);
|
||||
|
||||
string_delete(priv_string);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
void
|
||||
import_public_key(char const * const filename,
|
||||
fmpz_poly_t pub,
|
||||
ntru_context *ctx)
|
||||
{
|
||||
string *pub_string;
|
||||
fmpz_poly_t **imported;
|
||||
|
||||
pub_string = read_file(filename);
|
||||
imported = base64_to_poly_arr(pub_string, ctx);
|
||||
|
||||
/* if the array exceeds one element, then something
|
||||
* went horribly wrong */
|
||||
if (*imported[1])
|
||||
NTRU_ABORT("Failed importing public key!\n");
|
||||
|
||||
fmpz_poly_set(pub, **imported);
|
||||
|
||||
string_delete(pub_string);
|
||||
poly_delete(**imported);
|
||||
free(imported);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
void
|
||||
import_priv_key(char const * const filename,
|
||||
fmpz_poly_t priv,
|
||||
fmpz_poly_t priv_inv,
|
||||
ntru_context *ctx)
|
||||
{
|
||||
string *pub_string;
|
||||
fmpz_poly_t **imported,
|
||||
Fp;
|
||||
|
||||
fmpz_poly_init(Fp);
|
||||
|
||||
pub_string = read_file(filename);
|
||||
|
||||
imported = base64_to_poly_arr(pub_string, ctx);
|
||||
fmpz_poly_mod(**imported, ctx->p);
|
||||
|
||||
/* if the array exceeds one element, then something
|
||||
* went horribly wrong */
|
||||
if (*imported[1])
|
||||
NTRU_ABORT("Failed importing public key!\n");
|
||||
|
||||
fmpz_poly_set(priv, **imported);
|
||||
|
||||
if (!poly_inverse_poly_p(priv, Fp, ctx))
|
||||
goto cleanup;
|
||||
|
||||
fmpz_poly_mod(Fp, ctx->p);
|
||||
|
||||
fmpz_poly_set(priv_inv, Fp);
|
||||
fmpz_poly_clear(Fp);
|
||||
|
||||
cleanup:
|
||||
string_delete(pub_string);
|
||||
poly_delete(**imported);
|
||||
free(imported);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
void
|
||||
ntru_delete_keypair(keypair *pair)
|
||||
{
|
||||
|
@ -79,6 +79,56 @@ ntru_create_keypair(
|
||||
keypair *pair,
|
||||
ntru_context *ctx);
|
||||
|
||||
/**
|
||||
* Export the public key to a file.
|
||||
*
|
||||
* @param filename the file to save the public key into
|
||||
* @param pub the public key
|
||||
* @param ctx the NTRU context
|
||||
*/
|
||||
void
|
||||
export_public_key(char const * const filename,
|
||||
fmpz_poly_t pub,
|
||||
ntru_context *ctx);
|
||||
|
||||
/**
|
||||
* Export the private key to a file.
|
||||
*
|
||||
* @param filename the file to save the private key into
|
||||
* @param priv the private key
|
||||
* @param ctx the NTRU context
|
||||
*/
|
||||
void
|
||||
export_priv_key(char const * const filename,
|
||||
fmpz_poly_t priv,
|
||||
ntru_context *ctx);
|
||||
|
||||
/**
|
||||
* Import the public key from a file.
|
||||
* @param filename the file to get the public key from
|
||||
* @param pub where to save the public key [out]
|
||||
* @param ctx the NTRU context
|
||||
*/
|
||||
void
|
||||
import_public_key(char const * const filename,
|
||||
fmpz_poly_t pub,
|
||||
ntru_context *ctx);
|
||||
|
||||
/**
|
||||
* Import the private key from a file and store him
|
||||
* along with his inverse.
|
||||
*
|
||||
* @param filename the file to get the private key from
|
||||
* @param priv where to save the private key [out]
|
||||
* @param priv_inv where to save the inverse of the private key [out]
|
||||
* @param ctx the NTRU context
|
||||
*/
|
||||
void
|
||||
import_priv_key(char const * const filename,
|
||||
fmpz_poly_t priv,
|
||||
fmpz_poly_t priv_inv,
|
||||
ntru_context *ctx);
|
||||
|
||||
/**
|
||||
* Used to free the inner structure
|
||||
* of a keypair. This will not call free()
|
||||
|
Loading…
Reference in New Issue
Block a user