xref: /freebsd/crypto/libecc/src/fp/fp_mul_redc1.c (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1 /*
2  *  Copyright (C) 2017 - This file is part of libecc project
3  *
4  *  Authors:
5  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7  *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8  *
9  *  Contributors:
10  *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11  *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12  *
13  *  This software is licensed under a dual BSD and GPL v2 license.
14  *  See LICENSE file at the root folder of the project.
15  */
16 #include <libecc/fp/fp_mul_redc1.h>
17 
18 /*
19  * Internal helper performing Montgomery multiplication. The function returns
20  * 0 on success, -1 on error.
21  *
22  * CAUTION: the function does not check input parameters. Those checks MUST be
23  * performed by the caller.
24  */
_fp_mul_redc1(nn_t out,nn_src_t in1,nn_src_t in2,fp_ctx_src_t ctx)25 ATTRIBUTE_WARN_UNUSED_RET static inline int _fp_mul_redc1(nn_t out, nn_src_t in1, nn_src_t in2,
26 				 fp_ctx_src_t ctx)
27 {
28 	return nn_mul_redc1(out, in1, in2, &(ctx->p), ctx->mpinv);
29 }
30 
31 /*
32  * Compute out = in1 * in2 mod (p) in redcified form.
33  *
34  * Exported version based on previous one, that sanity checks input parameters.
35  * The function returns 0 on success, -1 on error.
36  *
37  * Aliasing is supported.
38  */
fp_mul_redc1(fp_t out,fp_src_t in1,fp_src_t in2)39 int fp_mul_redc1(fp_t out, fp_src_t in1, fp_src_t in2)
40 {
41 	int ret;
42 
43 	ret = fp_check_initialized(in1); EG(ret, err);
44 	ret = fp_check_initialized(in2); EG(ret, err);
45 	ret = fp_check_initialized(out); EG(ret, err);
46 
47 	MUST_HAVE((out->ctx == in1->ctx), ret, err);
48 	MUST_HAVE((out->ctx == in2->ctx), ret, err);
49 
50 	ret = _fp_mul_redc1(&(out->fp_val), &(in1->fp_val), &(in2->fp_val),
51 			    out->ctx);
52 
53 err:
54 	return ret;
55 }
56 
57 /*
58  * Compute out = in * in mod (p) in redcified form.
59  *
60  * Aliasing is supported.
61  */
fp_sqr_redc1(fp_t out,fp_src_t in)62 int fp_sqr_redc1(fp_t out, fp_src_t in)
63 {
64 	return fp_mul_redc1(out, in, in);
65 }
66 
67 /*
68  * Compute out = redcified form of in.
69  * redcify could be done by shifting and division by p. The function returns 0
70  * on success, -1 on error.
71  *
72  * Aliasing is supported.
73  */
fp_redcify(fp_t out,fp_src_t in)74 int fp_redcify(fp_t out, fp_src_t in)
75 {
76 	int ret;
77 
78 	ret = fp_check_initialized(in); EG(ret, err);
79 	ret = fp_check_initialized(out); EG(ret, err);
80 
81 	MUST_HAVE((out->ctx == in->ctx), ret, err);
82 
83 	ret = _fp_mul_redc1(&(out->fp_val), &(in->fp_val), &(out->ctx->r_square),
84 			    out->ctx);
85 
86 err:
87 	return ret;
88 }
89 
90 /*
91  * Compute out = unredcified form of in.
92  * The function returns 0 on success, -1 on error.
93  *
94  * Aliasing is supported.
95  */
fp_unredcify(fp_t out,fp_src_t in)96 int fp_unredcify(fp_t out, fp_src_t in)
97 {
98 	int ret;
99 	nn one;
100 	one.magic = WORD(0);
101 
102 	ret = fp_check_initialized(in); EG(ret, err);
103 	ret = fp_check_initialized(out); EG(ret, err);
104 	ret = nn_init(&one, 0);  EG(ret, err);
105 	ret = nn_one(&one); EG(ret, err);
106 	ret = _fp_mul_redc1(&(out->fp_val), &(in->fp_val), &one, out->ctx);
107 
108 err:
109 	nn_uninit(&one);
110 
111 	return ret;
112 }
113