From 8871b496c28accb79030bf395c3258b3d04bcf60 Mon Sep 17 00:00:00 2001 From: hasufell Date: Fri, 30 May 2014 19:23:45 +0200 Subject: [PATCH] KEYGEN: support import/export of keys --- src/keypair.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/keypair.h | 50 ++++++++++++++++++++++++ 2 files changed, 154 insertions(+) diff --git a/src/keypair.c b/src/keypair.c index b7eb23c..f47c056 100644 --- a/src/keypair.c +++ b/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 #include #include +#include /*------------------------------------------------------------------------*/ @@ -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) { diff --git a/src/keypair.h b/src/keypair.h index 307e65f..46926e6 100644 --- a/src/keypair.h +++ b/src/keypair.h @@ -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()