lpc55s6x_crypto_pkc.c
Go to the documentation of this file.
1 /**
2  * @file lpc55s6x_crypto_pkc.c
3  * @brief LPC55S6x 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 "fsl_device_registers.h"
36 #include "fsl_casper.h"
37 #include "core/crypto.h"
40 #include "ecc/ec.h"
41 #include "ecc/ec_misc.h"
42 #include "debug.h"
43 
44 //Check crypto library configuration
45 #if (LPC55S6X_CRYPTO_PKC_SUPPORT == ENABLED)
46 
47 //Global variables
48 static Lpc55s6xEcArgs ecArgs;
49 
50 
51 /**
52  * @brief Scalar multiplication (fast calculation)
53  * @param[in] curve Elliptic curve parameters
54  * @param[out] r Resulting point R = d.S
55  * @param[in] d An integer d such as 0 <= d < p
56  * @param[in] s EC point
57  * @return Error code
58  **/
59 
60 error_t ecMulFast(const EcCurve *curve, EcPoint3 *r, const uint32_t *d,
61  const EcPoint3 *s)
62 {
63  //Compute R = d.S
64  return ecMulRegular(curve, r, d, s);
65 }
66 
67 
68 /**
69  * @brief Scalar multiplication (regular calculation)
70  * @param[in] curve Elliptic curve parameters
71  * @param[out] r Resulting point R = d.S
72  * @param[in] d An integer d such as 0 <= d < q
73  * @param[in] s EC point
74  * @return Error code
75  **/
76 
77 error_t ecMulRegular(const EcCurve *curve, EcPoint3 *r, const uint32_t *d,
78  const EcPoint3 *s)
79 {
80  error_t error;
81  size_t n;
82  uint_t modLen;
83  uint_t orderLen;
84 
85  //Get the length of the modulus, in words
86  modLen = (curve->fieldSize + 31) / 32;
87  //Get the length of the order, in words
88  orderLen = (curve->orderSize + 31) / 32;
89 
90  //Initialize status code
91  error = NO_ERROR;
92 
93  //Check elliptic curve parameters
94  if(osStrcmp(curve->name, "secp256r1") == 0)
95  {
96  n = 32;
97  }
98  else if(osStrcmp(curve->name, "secp384r1") == 0)
99  {
100  n = 48;
101  }
102  else if(osStrcmp(curve->name, "secp521r1") == 0)
103  {
104  n = 72;
105  }
106  else
107  {
108  return ERROR_FAILURE;
109  }
110 
111  //Acquire exclusive access to the CASPER module
113 
114  //Set scalar value
115  ecScalarExport(d, orderLen, (uint8_t *) ecArgs.d, n,
117 
118  //Set input point
119  ecScalarExport(s->x, modLen, (uint8_t *) ecArgs.sx, n,
121 
122  ecScalarExport(s->y, modLen, (uint8_t *) ecArgs.sy, n,
124 
125  //Initialize curve parameters in CASPER memory
126  if(n == 32)
127  {
128  CASPER_ecc_init(kCASPER_ECC_P256);
129  }
130  else if(n == 48)
131  {
132  CASPER_ecc_init(kCASPER_ECC_P384);
133  }
134  else
135  {
136  CASPER_ecc_init(kCASPER_ECC_P521);
137  }
138 
139  //Perform scalar multiplication
140  if(n == 32)
141  {
142  CASPER_ECC_SECP256R1_Mul(CASPER, ecArgs.rx, ecArgs.ry, ecArgs.sx,
143  ecArgs.sy, ecArgs.d);
144  }
145  else if(n == 48)
146  {
147  CASPER_ECC_SECP384R1_Mul(CASPER, ecArgs.rx, ecArgs.ry, ecArgs.sx,
148  ecArgs.sy, ecArgs.d);
149  }
150  else
151  {
152  CASPER_ECC_SECP521R1_Mul(CASPER, ecArgs.rx, ecArgs.ry, ecArgs.sx,
153  ecArgs.sy, ecArgs.d);
154  }
155 
156  //Copy the x-coordinate of the result
157  error = ecScalarImport(r->x, EC_MAX_MODULUS_SIZE, (uint8_t *) ecArgs.rx, n,
159 
160  //Check status code
161  if(!error)
162  {
163  //Copy the y-coordinate of the result
164  error = ecScalarImport(r->y, EC_MAX_MODULUS_SIZE, (uint8_t *) ecArgs.ry,
166  }
167 
168  //Check status code
169  if(!error)
170  {
171  //Set the z-coordinate of the result
173  }
174 
175  //Release exclusive access to the CASPER module
177 
178  //Return status code
179  return error;
180 }
181 
182 
183 /**
184  * @brief Twin multiplication
185  * @param[in] curve Elliptic curve parameters
186  * @param[out] r Resulting point R = d0.S + d1.T
187  * @param[in] d0 An integer d such as 0 <= d0 < p
188  * @param[in] s EC point
189  * @param[in] d1 An integer d such as 0 <= d1 < p
190  * @param[in] t EC point
191  * @return Error code
192  **/
193 
194 error_t ecTwinMul(const EcCurve *curve, EcPoint3 *r, const uint32_t *d0,
195  const EcPoint3 *s, const uint32_t *d1, const EcPoint3 *t)
196 {
197  error_t error;
198  EcPoint3 u;
199 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
200  EcState *state;
201 #else
202  EcState state[1];
203 #endif
204 
205 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
206  //Allocate working state
207  state = cryptoAllocMem(sizeof(EcState));
208  //Failed to allocate memory?
209  if(state == NULL)
210  return ERROR_OUT_OF_MEMORY;
211 #endif
212 
213  //Initialize working state
214  osMemset(state, 0, sizeof(EcState));
215  //Save elliptic curve parameters
216  state->curve = curve;
217 
218  //Compute d0.S
219  error = ecMulFast(curve, r, d0, s);
220 
221  //Check status code
222  if(!error)
223  {
224  //Compute d1.T
225  error = ecMulFast(curve, &u, d1, t);
226  }
227 
228  //Check status code
229  if(!error)
230  {
231  //Compute d0.S + d1.T
232  ecFullAdd(state, r, r, &u);
233  }
234 
235  //Return status code
236  return error;
237 }
238 
239 #endif
error_t ecScalarImport(uint32_t *r, uint_t n, const uint8_t *input, size_t length, EcScalarFormat format)
Octet string to integer conversion.
Definition: ec_misc.c:54
LPC55S6x public-key hardware accelerator.
error_t ecScalarExport(const uint32_t *a, uint_t n, uint8_t *output, size_t length, EcScalarFormat format)
Integer to octet string conversion.
Definition: ec_misc.c:150
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
LPC55S6x hardware cryptographic accelerator.
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
#define osStrcmp(s1, s2)
Definition: os_port.h:174
uint8_t r
Definition: ndp.h:346
error_t
Error codes.
Definition: error.h:43
error_t ecMulFast(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (fast calculation)
@ 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.
@ EC_SCALAR_FORMAT_LITTLE_ENDIAN
Definition: ec_misc.h:50
General definitions for cryptographic algorithms.
error_t ecMulRegular(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (regular calculation)
uint8_t u
Definition: lldp_ext_med.h:213
const EcCurve * curve
Definition: ec.h:446
Working state (point addition/subtraction/doubling)
Definition: ec.h:445
uint8_t n
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.
OsMutex lpc55s6xCryptoMutex
error_t ecTwinMul(const EcCurve *curve, EcPoint3 *r, const uint32_t *d0, const EcPoint3 *s, const uint32_t *d1, const EcPoint3 *t)
Twin multiplication.
#define cryptoAllocMem(size)
Definition: crypto.h:821
uint8_t s
Definition: igmp_common.h:234
EC primitive arguments.
#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
ECC (Elliptic Curve Cryptography)
#define EC_MAX_MODULUS_SIZE
Definition: ec.h:284
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.