xref: /freebsd/crypto/libecc/src/fp/fp_rand.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
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 
17 #include <libecc/fp/fp_rand.h>
18 #include <libecc/nn/nn_rand.h>
19 
20 /*
21  * Initialize given Fp element in 'out' storage space to a Fp value chosen
22  * uniformly at random in [1, p-1] where p is provided by 'ctx'. The function
23  * returns 0 on success, -1 on error.
24  */
25 int fp_get_random(fp_t out, fp_ctx_src_t ctx)
26 {
27 	int ret;
28 
29 	ret = fp_init(out, ctx); EG(ret, err);
30 	ret = nn_get_random_mod(&(out->fp_val), &(ctx->p));
31 
32 err:
33 	return ret;
34 }
35