Mbed OS Reference
Loading...
Searching...
No Matches
ecp_helper.h
1/*
2 * Copyright (c) 2022, Nuvoton Technology Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef ECP_HELPER_H
20#define ECP_HELPER_H
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#include "mbedtls/bignum.h"
29
30/**
31 * \brief Check if MPI has been normalized
32 *
33 * \param N Input MPI which is to check
34 * \param P Prime modulus
35 *
36 * \return 0 if not normalized,
37 * 1 if normalized
38 */
39#define ECP_HELPER_MPI_IS_NORM(N, P) \
40 ((mbedtls_mpi_cmp_int(&N, 0) >= 0) && (mbedtls_mpi_cmp_mpi(&N, &P) < 0))
41
42/**
43 * \brief Normalize MPI if it is not normalized yet
44 *
45 * \param R Holds pointer to normalized MPI (N1 or N2)
46 * \param N1 Input MPI which is to normalize
47 * \param N2 Output MPI which holds normalized N1 if N1 is not normalized yet
48 * \param P Prime modulus
49 */
50#define ECP_HELPER_MPI_NORM(R, N1, N2, P) \
51 do { \
52 if (ECP_HELPER_MPI_IS_NORM(N1, P)) { \
53 *R = &N1; \
54 } else { \
55 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&N2, &N1, &P)); \
56 *R = &N2; \
57 } \
58 } while(0)
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
64/**
65 * \brief This function deduces y coordinate from x coordinate
66 * for a ECP point expressed in compact representation.
67 *
68 * \param grp The ECP group to be exported.
69 * This must be initialized and have group parameters
70 * set, for example through mbedtls_ecp_group_load().
71 * \param y Deduced y coordinate which is smaller. The other would be
72 * \p grp->P - \p y. \p y must point to an initialized MPI.
73 *
74 * \return \c 0 on success.
75 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
76 * designate a Curve25519 or Curve448 curve.
77 * \return Another negative error code on other kinds of failure.
78 */
79int ecp_helper_deduce_y(const mbedtls_ecp_group *grp,
80 mbedtls_mpi *y,
81 const mbedtls_mpi *x);
82
83#ifdef __cplusplus
84}
85#endif
86
87#endif /* ECP_HELPER_H */
Multi-precision integer library.
Configuration options (set of defines)
The ECP group structure.
MPI structure.
Definition: bignum.h:185