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
|
|
|
/**
|
|
|
|
* @file keypair.c
|
|
|
|
* This file handles the creation of the
|
|
|
|
* key pair and possibly common operations on them.
|
|
|
|
* @brief key creation and operations
|
|
|
|
*/
|
|
|
|
|
2014-05-24 21:12:15 +00:00
|
|
|
#include "context.h"
|
|
|
|
#include "keypair.h"
|
|
|
|
#include "poly.h"
|
|
|
|
|
|
|
|
#include <fmpz_poly.h>
|
|
|
|
#include <fmpz.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
bool
|
|
|
|
ntru_create_keypair(
|
2014-05-24 21:12:15 +00:00
|
|
|
fmpz_poly_t f,
|
|
|
|
fmpz_poly_t g,
|
|
|
|
keypair *pair,
|
|
|
|
ntru_context *ctx)
|
|
|
|
{
|
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-05-28 21:11:52 +00:00
|
|
|
if (!f || !g || !ctx)
|
|
|
|
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
|
|
|
|
2014-05-28 21:11:52 +00:00
|
|
|
if (!poly_inverse_poly_q(f, Fq, ctx))
|
|
|
|
goto _cleanup;
|
2014-05-24 21:12:15 +00:00
|
|
|
|
2014-05-28 21:11:52 +00:00
|
|
|
if (!poly_inverse_poly_p(f, Fp, ctx))
|
|
|
|
goto _cleanup;
|
2014-05-24 21:12:15 +00:00
|
|
|
|
2014-05-24 22:58:22 +00:00
|
|
|
poly_starmultiply(Fq, g, pub, ctx, ctx->q);
|
|
|
|
fmpz_poly_scalar_mul_ui(pub, pub, ctx->p);
|
2014-05-30 17:11:57 +00:00
|
|
|
fmpz_poly_mod_unsigned(pub, ctx->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-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
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*/
|