xref: /freebsd/crypto/libecc/include/libecc/nn/nn.h (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 #ifndef __NN_H__
17 #define __NN_H__
18 
19 #include <libecc/words/words.h>
20 #include <libecc/nn/nn_config.h>
21 #include <libecc/utils/utils.h>
22 
23 /*
24  * For a given amount of bytes (resp. bits), return the minimum number
25  * of words required to store that amount of bytes (respectively bits).
26  */
27 #define BYTE_LEN_WORDS(nbytes) (((nbytes) + WORD_BYTES - 1) / WORD_BYTES)
28 #define BIT_LEN_WORDS(nbits) (((nbits) + WORD_BITS - 1) / WORD_BITS)
29 
30 /*
31  * For a given amount of bytes (resp. bits), return the first number of
32  * bytes (resp. bits) equal or above to that value which is a multiple
33  * of word bytes.
34  */
35 #define BYTE_LEN_CEIL(nbytes) (BYTE_LEN_WORDS(nbytes) * WORD_BYTES)
36 #define BIT_LEN_CEIL(nbits) (BIT_LEN_WORDS(nbits) * WORD_BITS)
37 
38 /*
39  * Our nn type contains an array of words, which is of a fixed given storage
40  * size defined in nn_lib_ecc_config.h.
41  *
42  * Each word in this array is in local endianness whereas the words
43  * in the array are ordered in a little endian way with regard to their
44  * indices. That is: the word at index 0 in the array contains the least
45  * significant word of the nn.
46  *
47  * Except explicitly specified (some functions may provide automatic
48  * initialization of output params), initialization is usually required
49  * before nn can be used.
50  *
51  * After initialization, the 'wlen' attribute provides at each moment
52  * an upper bound on the position of last non-zero word in the array.
53  * All words after that point are always guaranteed to be 0 after any
54  * manipulation by a function of this module.
55  * Functions use this assumption to optimize operations by avoiding to
56  * process leading zeros.
57  * Nevertheless, some functions still access words past the 'wlen' index
58  * and return correct results only if these words are 0.
59  *
60  * Note that functions with parameters not explicitly marked as const may
61  * modify the value of the 'wlen' attribute if they see fit.
62  * And indeed most of them set the output 'wlen' attribute to the maximal
63  * possible value given the inputs 'wlen' attributes.
64  * The most notable exceptions are the logical functions whose result
65  * depends on the preset value of the output 'wlen' attribute.
66  */
67 typedef struct {
68 	word_t val[BIT_LEN_WORDS(NN_MAX_BIT_LEN)];
69 	word_t magic;
70 	u8 wlen;
71 } nn;
72 
73 typedef nn *nn_t;
74 typedef const nn *nn_src_t;
75 
76 ATTRIBUTE_WARN_UNUSED_RET int nn_check_initialized(nn_src_t A);
77 ATTRIBUTE_WARN_UNUSED_RET int nn_is_initialized(nn_src_t A);
78 ATTRIBUTE_WARN_UNUSED_RET int nn_zero(nn_t A);
79 ATTRIBUTE_WARN_UNUSED_RET int nn_one(nn_t A);
80 ATTRIBUTE_WARN_UNUSED_RET int nn_set_word_value(nn_t A, word_t val);
81 void nn_uninit(nn_t A);
82 ATTRIBUTE_WARN_UNUSED_RET int nn_init(nn_t A, u16 len);
83 ATTRIBUTE_WARN_UNUSED_RET int nn_init_from_buf(nn_t A, const u8 *buf, u16 buflen);
84 ATTRIBUTE_WARN_UNUSED_RET int nn_cnd_swap(int cnd, nn_t in1, nn_t in2);
85 ATTRIBUTE_WARN_UNUSED_RET int nn_set_wlen(nn_t A, u8 new_wlen);
86 ATTRIBUTE_WARN_UNUSED_RET int nn_iszero(nn_src_t A, int *iszero);
87 ATTRIBUTE_WARN_UNUSED_RET int nn_isone(nn_src_t A, int *isone);
88 ATTRIBUTE_WARN_UNUSED_RET int nn_isodd(nn_src_t A, int *isodd);
89 ATTRIBUTE_WARN_UNUSED_RET int nn_cmp_word(nn_src_t in, word_t w, int *cmp);
90 ATTRIBUTE_WARN_UNUSED_RET int nn_cmp(nn_src_t A, nn_src_t B, int *cmp);
91 ATTRIBUTE_WARN_UNUSED_RET int nn_copy(nn_t dst_nn, nn_src_t src_nn);
92 ATTRIBUTE_WARN_UNUSED_RET int nn_normalize(nn_t in1);
93 ATTRIBUTE_WARN_UNUSED_RET int nn_export_to_buf(u8 *buf, u16 buflen, nn_src_t in_nn);
94 ATTRIBUTE_WARN_UNUSED_RET int nn_tabselect(nn_t out, u8 idx, nn_src_t *tab, u8 tabsize);
95 
96 #endif /* __NN_H__ */
97