PUB-API: add public API headers

This commit is contained in:
hasufell 2014-06-05 16:33:40 +02:00
parent 12d9bbfa4e
commit 07cc12f649
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
5 changed files with 526 additions and 0 deletions

188
include/ntru.h Normal file
View File

@ -0,0 +1,188 @@
/*
* 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
*/
/**
* @file ntru.h
* This file holds the public API of the most basic
* data types and operations of the pqc NTRU implementation
* and is meant to be installed on the client system.
* @brief public API, basic data types
*/
#ifndef PUBLIC_NTRU_NTRU_H_
#define PUBLIC_NTRU_NTRU_H_
#include <fmpz_poly.h>
#include <fmpz.h>
#include <stdint.h>
typedef struct ntru_params ntru_params;
typedef struct string string;
/**
* NTRU cryptosystem is specified by
* the following triple.
*/
struct ntru_params {
/**
* maximal degree N - 1 for
* all polynomials
*/
uint32_t N;
/**
* large modulus
*/
uint32_t q;
/**
* small modulus
*/
uint32_t p;
};
/**
* Represents a string.
*/
struct string {
/**
* Pointer to the char array,
* holding the actual string.
* THIS IS NOT NULL TERMINATED
* (at least not necessarily,
* don't ever assume it).
*/
char *ptr;
/**
* Length of the string.
*/
size_t len;
};
/**
* Prints the given string to stdout.
*
* @param print_string the print to string
*/
void
prints(const string *print_string);
/**
* Delete the inner structure
* of the string and frees the string
* itself from the heap. Must not be
* called on stack variables.
*
* @param del_string the string to delete
*/
void
string_delete(string *del_string);
/**
* Initializes and builds a polynomial with the
* coefficient values of c[] of size len within NTRU
* parameters and returns a newly allocated polynomial.
* For an empty polynom, both parameters can be NULL/0.
*
* @param new_poly the polynomial to initialize and
* fill with coefficients
* @param c array of polynomial coefficients, can be NULL
* @param len size of the coefficient array, can be 0
* @return newly allocated polynomial pointer, must be freed
* with fmpz_poly_clear()
*/
void
poly_new(fmpz_poly_t new_poly,
int const * const c,
const size_t len);
/**
* This deletes the internal structure of a polynomial,
* and frees the pointer.
*
* @param poly the polynomial to delete
*/
void
poly_delete(fmpz_poly_t poly);
/**
* Delete the internal structure of a polynomial
* array which must be NULL terminated. It is expected
* that poly_array is not on the stack and was obtained
* by a function like ascii_to_poly().
*
* @param poly_array the polynomial array
*/
void
poly_delete_array(fmpz_poly_t **poly_array);
/**
* This deletes the internal structure of all polynomials,
* and frees the pointers.
* You must call this with NULL as last argument!
*
* @param poly the polynomial to delete
* @param ... follow up polynomials
*/
void
poly_delete_all(fmpz_poly_t poly, ...);
/**
* Draws a polynomial to stdout.
*
* @param poly draw this
*/
void
poly_draw(const fmpz_poly_t poly);
/**
* Draws a polynomial to stdout,
* in pretty format.
*
* @param poly draw this
*/
void
poly_draw_pretty(const fmpz_poly_t poly);
/**
* Reads a file and returns a newly allocated string.
*
* @param filename file to open
* @return a newly allocated string which must be freed by the caller
* or NULL on failure
*/
string *
read_file(char const * const filename);
/**
* Write a string to a file. The file will be pruned or created
* if it does not exist.
*
* @param wstring the string to write to the file
* @param filename the name of the file to write to
*/
void
write_file(string const *wstring, char const * const filename);
#endif /* NTRU_NTRU_H_ */

58
include/ntru_decrypt.h Normal file
View File

@ -0,0 +1,58 @@
/*
* 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
*/
/**
* @file ntru_decrypt.h
* This file holds the public API of decryption
* of the pqc NTRU implementation and is
* meant to be installed on the client system.
* @brief public API, decryption
*/
#ifndef PUBLIC_NTRU_DECRYPT_H_
#define PUBLIC_NTRU_DECRYPT_H_
#include "ntru.h"
#include <fmpz_poly.h>
#include <fmpz.h>
/**
* Decryption of a given encrypted string.
*
* @param encr_msg the encrypted message in the form of a string
* @param priv_key the polynom containing the private key to decrypt
* the message
* @param priv_key_inv the inverse polynome to the private key
* @param params the ntru_params
* @return the decrypted string or NULL on failure
*/
string *
ntru_decrypt_string(
const string *encr_msg,
const fmpz_poly_t priv_key,
const fmpz_poly_t priv_key_inv,
const ntru_params *params);
#endif /* PUBLIC_NTRU_DECRYPT_H_ */

59
include/ntru_encrypt.h Normal file
View File

@ -0,0 +1,59 @@
/*
* 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
*/
/**
* @file ntru_encrypt.h
* This file holds the public API of encryption
* of the pqc NTRU implementation and is
* meant to be installed on the client system.
* @brief public API, encryption
*/
#ifndef PUBLIC_NTRU_ENCRYPT_H_
#define PUBLIC_NTRU_ENCRYPT_H_
#include "ntru.h"
#include <fmpz_poly.h>
#include <fmpz.h>
/**
* Encrypt a message in the form of a null-terminated char array and
* return a string.
*
* @param msg the message
* @param pub_key the public key
* @param rnd the random poly (should have relatively small
* coefficients, but not restricted to {-1, 0, 1})
* @param params ntru_params the ntru context
* @return the newly allocated encrypted string, NULL on failure
*/
string *
ntru_encrypt_string(
const string *msg,
const fmpz_poly_t pub_key,
const fmpz_poly_t rnd,
const ntru_params *params);
#endif /* PUBLIC_NTRU_ENCRYPT_H_ */

145
include/ntru_keypair.h Normal file
View File

@ -0,0 +1,145 @@
/*
* 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
*/
/**
* @file ntru_keypair.h
* This file holds the public API of the most basic
* data types of the pqc NTRU implementation and is
* meant to be installed on the client system.
* @brief public API, basic data types
*/
#ifndef PUBLIC_NTRU_KEYPAIR_H_
#define PUBLIC_NTRU_KEYPAIR_H_
#include "ntru.h"
#include <fmpz_poly.h>
#include <fmpz.h>
#include <stdbool.h>
#include <stdint.h>
typedef struct keypair keypair;
/**
* This struct holds the keypair,
* both private and public components.
*/
struct keypair {
/**
* First part of the private key,
* a random polynom.
*/
fmpz_poly_t priv;
/**
* Second part of the private key,
* the priv polynom inverted.
*/
fmpz_poly_t priv_inv;
/**
* The public key, computed as:
* h = p * (Fq * g) mod q
*/
fmpz_poly_t pub;
};
/**
* Creates an NTRU key pair,
* consisting of public and private
* components.
*
* @param f a random polynomial
* @param g a random polynomial
* @param pair store private and public components here [out]
* @param params the NTRU context
*/
bool
ntru_create_keypair(
fmpz_poly_t f,
fmpz_poly_t g,
keypair *pair,
ntru_params *params);
/**
* Export the public key to a file.
*
* @param filename the file to save the public key into
* @param pub the public key
* @param params the NTRU context
*/
void
export_public_key(char const * const filename,
fmpz_poly_t pub,
ntru_params *params);
/**
* Export the private key to a file.
*
* @param filename the file to save the private key into
* @param priv the private key
* @param params the NTRU context
*/
void
export_priv_key(char const * const filename,
fmpz_poly_t priv,
ntru_params *params);
/**
* 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 params the NTRU context
*/
void
import_public_key(char const * const filename,
fmpz_poly_t pub,
ntru_params *params);
/**
* 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 params the NTRU context
*/
void
import_priv_key(char const * const filename,
fmpz_poly_t priv,
fmpz_poly_t priv_inv,
ntru_params *params);
/**
* Used to free the inner structure
* of a keypair. This will not call free()
* on the pair itself.
*
* @param pair the pair to free the inner structure of
*/
void
ntru_delete_keypair(keypair *pair);
#endif /* PUBLIC_NTRU_KEYPAIR_H_ */

76
include/ntru_rnd.h Normal file
View File

@ -0,0 +1,76 @@
/*
* 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
*/
/**
* @file ntru_encrypt.h
* This file holds the public API of encryption
* of the pqc NTRU implementation and is
* meant to be installed on the client system.
* @brief public API, encryption
*/
#ifndef PUBLIC_NTRU_RND_H_
#define PUBLIC_NTRU_RND_H_
#include "ntru.h"
#include <fmpz_poly.h>
#include <fmpz.h>
#include <stdint.h>
/**
* Get a random integer from /dev/random.
*
* @return random integer
*/
int
get_rnd_int(void);
/**
* Get a pseudo random integer from /dev/urandom.
*
* @return pseudo-random integer.
*/
int
get_urnd_int(void);
/**
* Get a random ternary polynomial with specified numbers
* of 1 coefficients and -1 coefficients.
*
* @param poly the resulting random polynomial [out]
* @param params the NTRU context
* @param num_ones the number of 1 coefficients
* @param num_neg_ones the number of -1 coefficients
* @param rnd_int function callback which should return
* a random integer
*/
void
ntru_get_rnd_tern_poly_num(fmpz_poly_t poly,
const ntru_params *params,
uint32_t num_ones,
uint32_t num_neg_ones,
int (*rnd_int)(void));
#endif /* PUBLIC_NTRU_RND_H_ */