pqc/src/ntru_keypair.c

205 lines
4.6 KiB
C
Raw Normal View History

2014-05-24 21:12:15 +00:00
/*
* Copyright (C) 2014 FH Bielefeld
*
* This file is part of a FH Bielefeld project.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
2014-05-25 00:15:24 +00:00
/**
2014-06-05 13:49:40 +00:00
* @file ntru_keypair.c
2014-05-25 00:15:24 +00:00
* This file handles the creation of the
* key pair and possibly common operations on them.
* @brief key creation and operations
*/
2014-06-05 13:49:40 +00:00
#include "ntru_ascii_poly.h"
#include "ntru_file.h"
#include "ntru_keypair.h"
#include "ntru_params.h"
#include "ntru_poly.h"
#include "ntru_poly_ascii.h"
2014-05-30 17:23:45 +00:00
#include "ntru_string.h"
2014-05-24 21:12:15 +00:00
#include <fmpz_poly.h>
#include <fmpz.h>
#include <stdbool.h>
2014-05-30 17:23:45 +00:00
#include <string.h>
2014-05-24 21:12:15 +00:00
2014-05-27 23:17:19 +00:00
/*------------------------------------------------------------------------*/
2014-05-25 21:04:22 +00:00
bool
ntru_create_keypair(
keypair *pair,
const fmpz_poly_t f,
const fmpz_poly_t g,
const ntru_params *params)
2014-05-24 21:12:15 +00:00
{
2014-05-28 21:11:52 +00:00
bool retval = false;
2014-05-24 21:12:15 +00:00
fmpz_poly_t Fq,
Fp,
2014-05-24 22:58:22 +00:00
pub;
2014-05-24 21:12:15 +00:00
2014-06-05 13:33:57 +00:00
if (!f || !g || !params)
2014-05-28 21:11:52 +00:00
goto _return;
2014-05-24 21:12:15 +00:00
fmpz_poly_init(Fq);
fmpz_poly_init(Fp);
2014-05-24 22:58:22 +00:00
fmpz_poly_init(pub);
2014-05-24 21:12:15 +00:00
if (!poly_inverse_poly_q(Fq, f, params))
2014-05-28 21:11:52 +00:00
goto _cleanup;
2014-05-24 21:12:15 +00:00
if (!poly_inverse_poly_p(Fp, f, params))
2014-05-28 21:11:52 +00:00
goto _cleanup;
2014-05-24 21:12:15 +00:00
poly_starmultiply(pub, Fq, g, params, params->q);
2014-06-05 13:33:57 +00:00
fmpz_poly_scalar_mul_ui(pub, pub, params->p);
fmpz_poly_mod_unsigned(pub, params->q);
2014-05-24 21:12:15 +00:00
fmpz_poly_init(pair->priv);
2014-05-25 02:07:51 +00:00
fmpz_poly_init(pair->priv_inv);
2014-05-24 21:12:15 +00:00
fmpz_poly_init(pair->pub);
2014-05-24 22:58:22 +00:00
fmpz_poly_set(pair->priv, f);
fmpz_poly_set(pair->priv_inv, Fp);
fmpz_poly_set(pair->pub, pub);
2014-05-24 21:12:15 +00:00
2014-05-28 21:11:52 +00:00
retval = true;
_cleanup:
2014-05-24 21:12:15 +00:00
fmpz_poly_clear(Fq);
fmpz_poly_clear(Fp);
2014-05-24 22:58:22 +00:00
fmpz_poly_clear(pub);
2014-05-28 21:11:52 +00:00
_return:
2014-05-24 21:12:15 +00:00
return retval;
}
2014-05-27 23:17:19 +00:00
/*------------------------------------------------------------------------*/
2014-05-30 17:23:45 +00:00
void
export_public_key(char const * const filename,
const fmpz_poly_t pub,
const ntru_params *params)
2014-05-30 17:23:45 +00:00
{
string *pub_string;
2014-06-05 13:33:57 +00:00
pub_string = poly_to_base64(pub, params);
2014-05-30 17:23:45 +00:00
write_file(pub_string, filename);
string_delete(pub_string);
}
/*------------------------------------------------------------------------*/
void
export_priv_key(char const * const filename,
const fmpz_poly_t priv,
const ntru_params *params)
2014-05-30 17:23:45 +00:00
{
string *priv_string;
fmpz_poly_t priv_u;
fmpz_poly_init(priv_u);
fmpz_poly_set(priv_u, priv);
2014-06-05 13:33:57 +00:00
fmpz_poly_mod_unsigned(priv_u, params->p);
2014-05-30 17:23:45 +00:00
2014-06-05 13:33:57 +00:00
priv_string = poly_to_base64(priv_u, params);
2014-05-30 17:23:45 +00:00
write_file(priv_string, filename);
2014-05-30 17:54:50 +00:00
fmpz_poly_clear(priv_u);
2014-05-30 17:23:45 +00:00
string_delete(priv_string);
}
/*------------------------------------------------------------------------*/
void
import_public_key(fmpz_poly_t pub,
char const * const filename,
const ntru_params *params)
2014-05-30 17:23:45 +00:00
{
string *pub_string;
fmpz_poly_t **imported;
pub_string = read_file(filename);
2014-06-05 13:33:57 +00:00
imported = base64_to_poly_arr(pub_string, params);
2014-05-30 17:23:45 +00:00
/* if the array exceeds one element, then something
* went horribly wrong */
if (*imported[1])
2014-06-05 17:13:00 +00:00
NTRU_ABORT_DEBUG("Failed importing public key!");
2014-05-30 17:23:45 +00:00
fmpz_poly_set(pub, **imported);
string_delete(pub_string);
2014-05-30 17:54:50 +00:00
poly_delete_array(imported);
2014-05-30 17:23:45 +00:00
free(imported);
}
/*------------------------------------------------------------------------*/
void
import_priv_key(fmpz_poly_t priv,
2014-05-30 17:23:45 +00:00
fmpz_poly_t priv_inv,
char const * const filename,
const ntru_params *params)
2014-05-30 17:23:45 +00:00
{
string *pub_string;
fmpz_poly_t **imported,
Fp;
fmpz_poly_init(Fp);
pub_string = read_file(filename);
2014-06-05 13:33:57 +00:00
imported = base64_to_poly_arr(pub_string, params);
fmpz_poly_mod(**imported, params->p);
2014-05-30 17:23:45 +00:00
/* if the array exceeds one element, then something
* went horribly wrong */
if (*imported[1])
2014-06-05 17:13:00 +00:00
NTRU_ABORT_DEBUG("Failed importing private key!");
2014-05-30 17:23:45 +00:00
fmpz_poly_set(priv, **imported);
if (!poly_inverse_poly_p(Fp, priv, params))
2014-05-30 17:23:45 +00:00
goto cleanup;
2014-06-05 13:33:57 +00:00
fmpz_poly_mod(Fp, params->p);
2014-05-30 17:23:45 +00:00
fmpz_poly_set(priv_inv, Fp);
fmpz_poly_clear(Fp);
cleanup:
string_delete(pub_string);
2014-05-30 17:54:50 +00:00
poly_delete_array(imported);
2014-05-30 17:23:45 +00:00
free(imported);
}
/*------------------------------------------------------------------------*/
2014-05-25 21:04:22 +00:00
void
ntru_delete_keypair(keypair *pair)
2014-05-25 02:06:24 +00:00
{
fmpz_poly_clear(pair->priv_inv);
fmpz_poly_clear(pair->priv);
fmpz_poly_clear(pair->pub);
}
2014-05-27 23:17:19 +00:00
/*------------------------------------------------------------------------*/