aboutsummaryrefslogtreecommitdiff
path: root/include/libecc/fp
diff options
context:
space:
mode:
authorKyle Evans <kevans@FreeBSD.org>2024-12-17 04:18:42 +0000
committerKyle Evans <kevans@FreeBSD.org>2024-12-17 04:18:42 +0000
commit736d663976d1768533badbf06581481d01fade4c (patch)
tree8760aa186b4bc2953f0e546da6f408b4f5974dc3 /include/libecc/fp
libecc will be used privately for upcoming ECC support in pkg(7). Other crypto consumers should continue using openssl.
Diffstat (limited to 'include/libecc/fp')
-rw-r--r--include/libecc/fp/fp.h98
-rw-r--r--include/libecc/fp/fp_add.h26
-rw-r--r--include/libecc/fp/fp_config.h25
-rw-r--r--include/libecc/fp/fp_montgomery.h30
-rw-r--r--include/libecc/fp/fp_mul.h26
-rw-r--r--include/libecc/fp/fp_mul_redc1.h25
-rw-r--r--include/libecc/fp/fp_pow.h22
-rw-r--r--include/libecc/fp/fp_rand.h22
-rw-r--r--include/libecc/fp/fp_sqrt.h25
9 files changed, 299 insertions, 0 deletions
diff --git a/include/libecc/fp/fp.h b/include/libecc/fp/fp.h
new file mode 100644
index 000000000000..1ead4d5dbce2
--- /dev/null
+++ b/include/libecc/fp/fp.h
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2017 - This file is part of libecc project
+ *
+ * Authors:
+ * Ryad BENADJILA <ryadbenadjila@gmail.com>
+ * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
+ * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
+ *
+ * Contributors:
+ * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
+ * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
+ *
+ * This software is licensed under a dual BSD and GPL v2 license.
+ * See LICENSE file at the root folder of the project.
+ */
+#ifndef __FP_H__
+#define __FP_H__
+
+#include <libecc/nn/nn.h>
+#include <libecc/nn/nn_div_public.h>
+#include <libecc/nn/nn_modinv.h>
+#include <libecc/nn/nn_mul_public.h>
+#include <libecc/nn/nn_mul_redc1.h>
+#include <libecc/fp/fp_config.h>
+
+/*
+ * First, definition of our Fp context, containing all the elements
+ * needed to efficiently implement Fp operations.
+ */
+
+typedef struct {
+ /*
+ * Value of p (extended by one word to handle
+ * overflows in Fp). p_bitlen provides its
+ * length in bit.
+ */
+ nn p;
+ bitcnt_t p_bitlen;
+
+ /* -p^-1 mod 2^(bitsizeof(word_t)) */
+ word_t mpinv;
+
+ /* 2^bitsizeof(p) mod p */
+ nn r;
+
+ /* 2^(2*bitsizeof(p)) mod p */
+ nn r_square;
+
+ /* clz(p) */
+ bitcnt_t p_shift;
+ /* p << p_shift */
+ nn p_normalized;
+ /* floor(B^3/(DMSW(p_normalized) + 1)) - B */
+ word_t p_reciprocal;
+
+ word_t magic;
+} fp_ctx;
+
+typedef fp_ctx *fp_ctx_t;
+typedef const fp_ctx *fp_ctx_src_t;
+
+ATTRIBUTE_WARN_UNUSED_RET int fp_ctx_check_initialized(fp_ctx_src_t ctx);
+ATTRIBUTE_WARN_UNUSED_RET int fp_ctx_init(fp_ctx_t ctx, nn_src_t p, bitcnt_t p_bitlen,
+ nn_src_t r, nn_src_t r_square,
+ word_t mpinv,
+ bitcnt_t p_shift, nn_src_t p_normalized, word_t p_reciprocal);
+ATTRIBUTE_WARN_UNUSED_RET int fp_ctx_init_from_p(fp_ctx_t ctx, nn_src_t p);
+
+/*
+ * Then the definition of our Fp elements
+ */
+
+typedef struct {
+ nn fp_val;
+ fp_ctx_src_t ctx;
+ word_t magic;
+} fp;
+
+typedef fp *fp_t;
+typedef const fp *fp_src_t;
+
+ATTRIBUTE_WARN_UNUSED_RET int fp_check_initialized(fp_src_t in);
+ATTRIBUTE_WARN_UNUSED_RET int fp_init(fp_t A, fp_ctx_src_t fpctx);
+ATTRIBUTE_WARN_UNUSED_RET int fp_init_from_buf(fp_t A, fp_ctx_src_t fpctx, const u8 *buf, u16 buflen);
+void fp_uninit(fp_t A);
+ATTRIBUTE_WARN_UNUSED_RET int fp_set_nn(fp_t out, nn_src_t in);
+ATTRIBUTE_WARN_UNUSED_RET int fp_zero(fp_t out);
+ATTRIBUTE_WARN_UNUSED_RET int fp_one(fp_t out);
+ATTRIBUTE_WARN_UNUSED_RET int fp_set_word_value(fp_t out, word_t val);
+ATTRIBUTE_WARN_UNUSED_RET int fp_cmp(fp_src_t in1, fp_src_t in2, int *cmp);
+ATTRIBUTE_WARN_UNUSED_RET int fp_iszero(fp_src_t in, int *iszero);
+ATTRIBUTE_WARN_UNUSED_RET int fp_copy(fp_t out, fp_src_t in);
+ATTRIBUTE_WARN_UNUSED_RET int fp_tabselect(fp_t out, u8 idx, fp_src_t *tab, u8 tabsize);
+ATTRIBUTE_WARN_UNUSED_RET int fp_eq_or_opp(fp_src_t in1, fp_src_t in2, int *eq_or_opp);
+ATTRIBUTE_WARN_UNUSED_RET int fp_import_from_buf(fp_t out_fp, const u8 *buf, u16 buflen);
+ATTRIBUTE_WARN_UNUSED_RET int fp_export_to_buf(u8 *buf, u16 buflen, fp_src_t in_fp);
+
+#endif /* __FP_H__ */
diff --git a/include/libecc/fp/fp_add.h b/include/libecc/fp/fp_add.h
new file mode 100644
index 000000000000..e23474c110cb
--- /dev/null
+++ b/include/libecc/fp/fp_add.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2017 - This file is part of libecc project
+ *
+ * Authors:
+ * Ryad BENADJILA <ryadbenadjila@gmail.com>
+ * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
+ * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
+ *
+ * Contributors:
+ * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
+ * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
+ *
+ * This software is licensed under a dual BSD and GPL v2 license.
+ * See LICENSE file at the root folder of the project.
+ */
+#ifndef __FP_ADD_H__
+#define __FP_ADD_H__
+#include <libecc/fp/fp.h>
+
+ATTRIBUTE_WARN_UNUSED_RET int fp_add(fp_t out, fp_src_t in1, fp_src_t in2);
+ATTRIBUTE_WARN_UNUSED_RET int fp_inc(fp_t out, fp_src_t in);
+ATTRIBUTE_WARN_UNUSED_RET int fp_sub(fp_t out, fp_src_t in1, fp_src_t in2);
+ATTRIBUTE_WARN_UNUSED_RET int fp_dec(fp_t out, fp_src_t in);
+ATTRIBUTE_WARN_UNUSED_RET int fp_neg(fp_t out, fp_src_t in);
+
+#endif /* __FP_ADD_H__ */
diff --git a/include/libecc/fp/fp_config.h b/include/libecc/fp/fp_config.h
new file mode 100644
index 000000000000..063f321682b5
--- /dev/null
+++ b/include/libecc/fp/fp_config.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2017 - This file is part of libecc project
+ *
+ * Authors:
+ * Ryad BENADJILA <ryadbenadjila@gmail.com>
+ * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
+ * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
+ *
+ * Contributors:
+ * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
+ * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
+ *
+ * This software is licensed under a dual BSD and GPL v2 license.
+ * See LICENSE file at the root folder of the project.
+ */
+#ifndef __FP_CONFIG_H__
+#define __FP_CONFIG_H__
+
+#include <libecc/nn/nn_config.h>
+
+#ifndef FP_MAX_BIT_LEN
+#define FP_MAX_BIT_LEN (NN_MAX_BIT_LEN - 1)
+#endif
+
+#endif /* __FP_CONFIG_H__ */
diff --git a/include/libecc/fp/fp_montgomery.h b/include/libecc/fp/fp_montgomery.h
new file mode 100644
index 000000000000..f3e23e92292f
--- /dev/null
+++ b/include/libecc/fp/fp_montgomery.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2017 - This file is part of libecc project
+ *
+ * Authors:
+ * Ryad BENADJILA <ryadbenadjila@gmail.com>
+ * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
+ * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
+ *
+ * Contributors:
+ * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
+ * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
+ *
+ * This software is licensed under a dual BSD and GPL v2 license.
+ * See LICENSE file at the root folder of the project.
+ */
+#ifndef __FP_MONTGOMERY_H__
+#define __FP_MONTGOMERY_H__
+
+#include <libecc/fp/fp.h>
+#include <libecc/fp/fp_add.h>
+#include <libecc/fp/fp_mul.h>
+#include <libecc/fp/fp_mul_redc1.h>
+
+ATTRIBUTE_WARN_UNUSED_RET int fp_add_monty(fp_t out, fp_src_t in1, fp_src_t in2);
+ATTRIBUTE_WARN_UNUSED_RET int fp_sub_monty(fp_t out, fp_src_t in1, fp_src_t in2);
+ATTRIBUTE_WARN_UNUSED_RET int fp_mul_monty(fp_t out, fp_src_t in1, fp_src_t in2);
+ATTRIBUTE_WARN_UNUSED_RET int fp_sqr_monty(fp_t out, fp_src_t in);
+ATTRIBUTE_WARN_UNUSED_RET int fp_div_monty(fp_t out, fp_src_t in1, fp_src_t in2);
+
+#endif /* __FP_MONTGOMERY_H__ */
diff --git a/include/libecc/fp/fp_mul.h b/include/libecc/fp/fp_mul.h
new file mode 100644
index 000000000000..4adf638ab6d8
--- /dev/null
+++ b/include/libecc/fp/fp_mul.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2017 - This file is part of libecc project
+ *
+ * Authors:
+ * Ryad BENADJILA <ryadbenadjila@gmail.com>
+ * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
+ * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
+ *
+ * Contributors:
+ * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
+ * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
+ *
+ * This software is licensed under a dual BSD and GPL v2 license.
+ * See LICENSE file at the root folder of the project.
+ */
+#ifndef __FP_MUL_H__
+#define __FP_MUL_H__
+#include <libecc/fp/fp.h>
+
+ATTRIBUTE_WARN_UNUSED_RET int fp_mul(fp_t out, fp_src_t in1, fp_src_t in2);
+ATTRIBUTE_WARN_UNUSED_RET int fp_sqr(fp_t out, fp_src_t in);
+ATTRIBUTE_WARN_UNUSED_RET int fp_inv(fp_t out, fp_src_t in);
+ATTRIBUTE_WARN_UNUSED_RET int fp_inv_word(fp_t out, word_t w);
+ATTRIBUTE_WARN_UNUSED_RET int fp_div(fp_t out, fp_src_t num, fp_src_t den);
+
+#endif /* __FP_MUL_H__ */
diff --git a/include/libecc/fp/fp_mul_redc1.h b/include/libecc/fp/fp_mul_redc1.h
new file mode 100644
index 000000000000..0f30c46ec214
--- /dev/null
+++ b/include/libecc/fp/fp_mul_redc1.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2017 - This file is part of libecc project
+ *
+ * Authors:
+ * Ryad BENADJILA <ryadbenadjila@gmail.com>
+ * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
+ * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
+ *
+ * Contributors:
+ * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
+ * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
+ *
+ * This software is licensed under a dual BSD and GPL v2 license.
+ * See LICENSE file at the root folder of the project.
+ */
+#ifndef __FP_MUL_REDC1_H__
+#define __FP_MUL_REDC1_H__
+#include <libecc/fp/fp.h>
+
+ATTRIBUTE_WARN_UNUSED_RET int fp_mul_redc1(fp_t out, fp_src_t in1, fp_src_t in2);
+ATTRIBUTE_WARN_UNUSED_RET int fp_sqr_redc1(fp_t out, fp_src_t in);
+ATTRIBUTE_WARN_UNUSED_RET int fp_redcify(fp_t out, fp_src_t in);
+ATTRIBUTE_WARN_UNUSED_RET int fp_unredcify(fp_t out, fp_src_t in);
+
+#endif /* __FP_MUL_REDC1_H__ */
diff --git a/include/libecc/fp/fp_pow.h b/include/libecc/fp/fp_pow.h
new file mode 100644
index 000000000000..8a634006bfd9
--- /dev/null
+++ b/include/libecc/fp/fp_pow.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2017 - This file is part of libecc project
+ *
+ * Authors:
+ * Ryad BENADJILA <ryadbenadjila@gmail.com>
+ * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
+ * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
+ *
+ * Contributors:
+ * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
+ * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
+ *
+ * This software is licensed under a dual BSD and GPL v2 license.
+ * See LICENSE file at the root folder of the project.
+ */
+#ifndef __FP_POW_H__
+#define __FP_POW_H__
+#include <libecc/fp/fp.h>
+
+ATTRIBUTE_WARN_UNUSED_RET int fp_pow(fp_t out, fp_src_t base, nn_src_t exp);
+
+#endif /* __FP_POW_H__ */
diff --git a/include/libecc/fp/fp_rand.h b/include/libecc/fp/fp_rand.h
new file mode 100644
index 000000000000..827993e9a06a
--- /dev/null
+++ b/include/libecc/fp/fp_rand.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2017 - This file is part of libecc project
+ *
+ * Authors:
+ * Ryad BENADJILA <ryadbenadjila@gmail.com>
+ * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
+ * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
+ *
+ * Contributors:
+ * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
+ * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
+ *
+ * This software is licensed under a dual BSD and GPL v2 license.
+ * See LICENSE file at the root folder of the project.
+ */
+#ifndef __FP_RAND_H__
+#define __FP_RAND_H__
+#include <libecc/fp/fp.h>
+
+ATTRIBUTE_WARN_UNUSED_RET int fp_get_random(fp_t out, fp_ctx_src_t ctx);
+
+#endif /* __FP_RAND_H__ */
diff --git a/include/libecc/fp/fp_sqrt.h b/include/libecc/fp/fp_sqrt.h
new file mode 100644
index 000000000000..91fbc0da0ded
--- /dev/null
+++ b/include/libecc/fp/fp_sqrt.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2017 - This file is part of libecc project
+ *
+ * Authors:
+ * Ryad BENADJILA <ryadbenadjila@gmail.com>
+ * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
+ * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
+ *
+ * Contributors:
+ * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
+ * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
+ *
+ * This software is licensed under a dual BSD and GPL v2 license.
+ * See LICENSE file at the root folder of the project.
+ */
+#ifndef __FP_SQRT_H__
+#define __FP_SQRT_H__
+#include <libecc/fp/fp.h>
+#include <libecc/fp/fp_add.h>
+#include <libecc/fp/fp_mul.h>
+#include <libecc/fp/fp_pow.h>
+
+ATTRIBUTE_WARN_UNUSED_RET int fp_sqrt(fp_t sqrt1, fp_t sqrt2, fp_src_t n);
+
+#endif /* __FP_SQRT_H__ */