blob: 32928610afe82197055ccf561e8f7fedf986d1c7 [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
2 * Internal bignum functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
22#if defined(MBEDTLS_BIGNUM_C)
23
24#include "mbedtls/error.h"
25#include "mbedtls/bignum.h"
26#include "bignum_mod.h"
27
28#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
29#define biL (ciL << 3) /* bits in limb */
30#define biH (ciL << 2) /* half limb size */
31
32/*
33 * Convert between bits/chars and number of limbs
34 * Divide first in order to avoid potential overflows
35 */
36#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
37#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
38
39/*
40 * Count leading zero bits in a given integer
41 */
42static size_t mpi_clz( const mbedtls_mpi_uint x )
43{
44 size_t j;
45 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
46
47 for( j = 0; j < biL; j++ )
48 {
49 if( x & mask ) break;
50
51 mask >>= 1;
52 }
53
54 return j;
55}
56
57/*
58 * Return the number of bits
59 */
60static size_t mpi_bitlen( const mbedtls_mpi_uint *X, size_t nx )
61{
62 size_t i, j;
63
64 if( nx == 0 )
65 return( 0 );
66
67 for( i = nx - 1; i > 0; i-- )
68 if( X[i] != 0 )
69 break;
70
71 j = biL - mpi_clz( X[i] );
72
73 return( ( i * biL ) + j );
74}
75
76void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
77{
78 if ( r == NULL )
79 return;
80
81 r->n = 0;
82 r->p = NULL;
83}
84
85int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
86 mbedtls_mpi_mod_modulus *m,
87 mbedtls_mpi_uint *X )
88{
89 if( X == NULL || m == NULL || r == NULL || X >= m->p)
90 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
91
92 r->n = m->n;
93 r->p = X;
94
95 return( 0 );
96}
97
98void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
99{
100 if ( m == NULL )
101 return;
102
103 m->rep.mont = 0;
104}
105
106void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
107{
108 if ( m == NULL )
109 return;
110
111 m->p = NULL;
112 m->n = 0;
113 m->plen = 0;
114 m->ext_rep = 0;
115 m->int_rep = 0;
116 m->rep.mont = NULL;
117 m->rep.ored = NULL;
118}
119
120int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
121 mbedtls_mpi_uint *X,
122 size_t nx,
123 int ext_rep,
124 int int_rep )
125{
126 if ( X == NULL || m == NULL )
127 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
128
129 m->p = X;
130 m->n = nx;
131 m->ext_rep = ext_rep;
132 m->int_rep = int_rep;
133 m->plen = mpi_bitlen( X, nx );
134
135 return( 0 );
136}
137
138#endif /* MBEDTLS_BIGNUM_C */