m480_crypto_pkc.c
Go to the documentation of this file.
1 /**
2  * @file m480_crypto_pkc.c
3  * @brief M480 public-key hardware accelerator
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "m480.h"
36 #include "core/crypto.h"
39 #include "ecc/ec.h"
40 #include "ecc/ec_misc.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (M480_CRYPTO_PKC_SUPPORT == ENABLED)
45 #if (EC_SUPPORT == ENABLED)
46 
47 /**
48  * @brief Import scalar
49  * @param[in] dest Pointer to the operand
50  * @param[in] length Length of the operand, in bits
51  * @param[in] src Pointer to the scalar
52  **/
53 
54 void eccImportScalar(volatile uint32_t *dest, uint_t length, const uint32_t *src)
55 {
56  uint_t i;
57 
58  //Get the length of the operand, in words
59  length = (length + 31) / 32;
60 
61  //Copy the scalar to the CRYPTO peripheral
62  for(i = 0; i < length; i++)
63  {
64  dest[i] = src[i];
65  }
66 }
67 
68 
69 /**
70  * @brief Export scalar
71  * @param[in] src Pointer to the operand
72  * @param[in] length Length of the operand, in bits
73  * @param[out] dest Pointer to the scalar
74  **/
75 
76 void eccExportScalar(volatile uint32_t *src, uint_t length, uint32_t *dest)
77 {
78  uint_t i;
79 
80  //Get the length of the operand, in words
81  length = (length + 31) / 32;
82 
83  //Copy the scalar from the CRYPTO peripheral
84  for(i = 0; i < length; i++)
85  {
86  dest[i] = src[i];
87  }
88 }
89 
90 
91 /**
92  * @brief Scalar multiplication (fast calculation)
93  * @param[in] curve Elliptic curve parameters
94  * @param[out] r Resulting point R = d.S
95  * @param[in] d An integer d such as 0 <= d < p
96  * @param[in] s EC point
97  * @return Error code
98  **/
99 
100 error_t ecMulFast(const EcCurve *curve, EcPoint3 *r, const uint32_t *d,
101  const EcPoint3 *s)
102 {
103  //Compute R = d.S
104  return ecMulRegular(curve, r, d, s);
105 }
106 
107 
108 /**
109  * @brief Scalar multiplication (regular calculation)
110  * @param[in] curve Elliptic curve parameters
111  * @param[out] r Resulting point R = d.S
112  * @param[in] d An integer d such as 0 <= d < q
113  * @param[in] s EC point
114  * @return Error code
115  **/
116 
117 error_t ecMulRegular(const EcCurve *curve, EcPoint3 *r, const uint32_t *d,
118  const EcPoint3 *s)
119 {
120  error_t error;
121  uint_t modLen;
122  uint_t orderLen;
123 
124  //Get the length of the modulus, in bits
125  modLen = curve->fieldSize;
126  //Get the length of the order, in bits
127  orderLen = curve->orderSize;
128 
129  //Check the length of the operands
130  if(modLen <= 576 && orderLen <= 576)
131  {
132  //Acquire exclusive access to the CRYPTO module
134 
135  //Reset CRYPTO controller
136  SYS->IPRST0 |= SYS_IPRST0_CRPTRST_Msk;
137  SYS->IPRST0 &= ~SYS_IPRST0_CRPTRST_Msk;
138 
139  //Load input arguments
140  eccImportScalar(CRPT->ECC_N, modLen, curve->p);
141  eccImportScalar(CRPT->ECC_A, modLen, curve->a);
142  eccImportScalar(CRPT->ECC_B, modLen, curve->b);
143  eccImportScalar(CRPT->ECC_K, orderLen, d);
144  eccImportScalar(CRPT->ECC_X1, modLen, s->x);
145  eccImportScalar(CRPT->ECC_Y1, modLen, s->y);
146  eccImportScalar(CRPT->ECC_X2, orderLen, curve->q);
147 
148  //Set up a point multiplication operation
149  CRPT->ECC_CTL = (modLen << CRPT_ECC_CTL_CURVEM_Pos) |
150  (0 << CRPT_ECC_CTL_ECCOP_Pos) | CRPT_ECC_CTL_FSEL_Msk;
151 
152  //Clear ECC interrupt flag
153  CRPT->INTSTS = CRPT_INTSTS_ECCIF_Msk;
154  //Enable ECC interrupt
155  CRPT->INTEN = CRPT_INTEN_ECCIEN_Msk;
156  //Start operation
157  CRPT->ECC_CTL |= CRPT_ECC_CTL_START_Msk;
158 
159  //Wait for the operation to complete
160  while((CRPT->INTSTS & CRPT_INTSTS_ECCIF_Msk) == 0)
161  {
162  }
163 
164  //Copy the x-coordinate of the result
166  eccExportScalar(CRPT->ECC_X1, modLen, r->x);
167 
168  //Copy the y-coordinate of the result
170  eccExportScalar(CRPT->ECC_Y1, modLen, r->y);
171 
172  //Set the z-coordinate of the result
174 
175  //Release exclusive access to the CRYPTO module
177 
178  //Successful processing
179  error = NO_ERROR;
180  }
181  else
182  {
183  //Report an error
184  error = ERROR_FAILURE;
185  }
186 
187  //Return status code
188  return error;
189 }
190 
191 
192 /**
193  * @brief Twin multiplication
194  * @param[in] curve Elliptic curve parameters
195  * @param[out] r Resulting point R = d0.S + d1.T
196  * @param[in] d0 An integer d such as 0 <= d0 < p
197  * @param[in] s EC point
198  * @param[in] d1 An integer d such as 0 <= d1 < p
199  * @param[in] t EC point
200  * @return Error code
201  **/
202 
203 error_t ecTwinMul(const EcCurve *curve, EcPoint3 *r, const uint32_t *d0,
204  const EcPoint3 *s, const uint32_t *d1, const EcPoint3 *t)
205 {
206  error_t error;
207  EcPoint3 u;
208 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
209  EcState *state;
210 #else
211  EcState state[1];
212 #endif
213 
214 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
215  //Allocate working state
216  state = cryptoAllocMem(sizeof(EcState));
217  //Failed to allocate memory?
218  if(state == NULL)
219  return ERROR_OUT_OF_MEMORY;
220 #endif
221 
222  //Initialize working state
223  osMemset(state, 0, sizeof(EcState));
224  //Save elliptic curve parameters
225  state->curve = curve;
226 
227  //Compute d0.S
228  error = ecMulFast(curve, r, d0, s);
229 
230  //Check status code
231  if(!error)
232  {
233  //Compute d1.T
234  error = ecMulFast(curve, &u, d1, t);
235  }
236 
237  //Check status code
238  if(!error)
239  {
240  //Compute d0.S + d1.T
241  ecFullAdd(state, r, r, &u);
242  }
243 
244  //Return status code
245  return error;
246 }
247 
248 #endif
249 #endif
error_t ecMulRegular(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (regular calculation)
uint8_t t
Definition: lldp_ext_med.h:212
void ecFullAdd(EcState *state, EcPoint3 *r, const EcPoint3 *s, const EcPoint3 *t)
Point addition.
Definition: ec.c:1094
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
void eccImportScalar(volatile uint32_t *dest, uint_t length, const uint32_t *src)
Import scalar.
uint8_t r
Definition: ndp.h:346
OsMutex m480CryptoMutex
Definition: m480_crypto.c:41
M480 hardware cryptographic accelerator.
error_t
Error codes.
Definition: error.h:43
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
void ecScalarSetInt(uint32_t *a, uint32_t b, uint_t n)
Set integer value.
Definition: ec_misc.c:505
Helper routines for ECC.
General definitions for cryptographic algorithms.
error_t ecMulFast(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (fast calculation)
uint8_t u
Definition: lldp_ext_med.h:213
uint8_t length
Definition: tcp.h:375
void eccExportScalar(volatile uint32_t *src, uint_t length, uint32_t *dest)
Export scalar.
const EcCurve * curve
Definition: ec.h:446
Working state (point addition/subtraction/doubling)
Definition: ec.h:445
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
EC point (projective coordinates)
Definition: ec.h:409
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
#define cryptoAllocMem(size)
Definition: crypto.h:821
uint8_t s
Definition: igmp_common.h:234
#define EcCurve
Definition: ec.h:346
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:138
M480 public-key hardware accelerator.
ECC (Elliptic Curve Cryptography)
#define EC_MAX_MODULUS_SIZE
Definition: ec.h:284
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
error_t ecTwinMul(const EcCurve *curve, EcPoint3 *r, const uint32_t *d0, const EcPoint3 *s, const uint32_t *d1, const EcPoint3 *t)
Twin multiplication.