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-2024 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.4.4
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 "debug.h"
42 
43 //Check crypto library configuration
44 #if (LPC55S6X_CRYPTO_PKC_SUPPORT == ENABLED)
45 
46 //Global variables
47 static Lpc55s6xEcArgs ecArgs;
48 
49 
50 /**
51  * @brief Scalar multiplication
52  * @param[in] params EC domain parameters
53  * @param[out] r Resulting point R = d.S
54  * @param[in] d An integer d such as 0 <= d < p
55  * @param[in] s EC point
56  * @return Error code
57  **/
58 
59 error_t ecMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d,
60  const EcPoint *s)
61 {
62  error_t error;
63  size_t n;
64 
65  //Initialize status code
66  error = NO_ERROR;
67 
68  //Check elliptic curve parameters
69  if(osStrcmp(params->name, "secp256r1") == 0)
70  {
71  n = 32;
72  }
73  else if(osStrcmp(params->name, "secp384r1") == 0)
74  {
75  n = 48;
76  }
77  else if(osStrcmp(params->name, "secp521r1") == 0)
78  {
79  n = 72;
80  }
81  else
82  {
83  return ERROR_FAILURE;
84  }
85 
86  //Acquire exclusive access to the CASPER module
88 
89  //Set scalar value
90  mpiExport(d, (uint8_t *) ecArgs.d, n, MPI_FORMAT_LITTLE_ENDIAN);
91 
92  //Set input point
93  mpiExport(&s->x, (uint8_t *) ecArgs.sx, n, MPI_FORMAT_LITTLE_ENDIAN);
94  mpiExport(&s->y, (uint8_t *) ecArgs.sy, n, MPI_FORMAT_LITTLE_ENDIAN);
95 
96  //Initialize curve parameters in CASPER memory
97  if(n == 32)
98  {
99  CASPER_ecc_init(kCASPER_ECC_P256);
100  }
101  else if(n == 48)
102  {
103  CASPER_ecc_init(kCASPER_ECC_P384);
104  }
105  else
106  {
107  CASPER_ecc_init(kCASPER_ECC_P521);
108  }
109 
110  //Perform scalar multiplication
111  if(n == 32)
112  {
113  CASPER_ECC_SECP256R1_Mul(CASPER, ecArgs.rx, ecArgs.ry, ecArgs.sx,
114  ecArgs.sy, ecArgs.d);
115  }
116  else if(n == 48)
117  {
118  CASPER_ECC_SECP384R1_Mul(CASPER, ecArgs.rx, ecArgs.ry, ecArgs.sx,
119  ecArgs.sy, ecArgs.d);
120  }
121  else
122  {
123  CASPER_ECC_SECP521R1_Mul(CASPER, ecArgs.rx, ecArgs.ry, ecArgs.sx,
124  ecArgs.sy, ecArgs.d);
125  }
126 
127  //Copy the x-coordinate of the result
128  error = mpiImport(&r->x, (uint8_t *) ecArgs.rx, n,
130 
131  //Check status code
132  if(!error)
133  {
134  //Copy the y-coordinate of the result
135  error = mpiImport(&r->y, (uint8_t *) ecArgs.ry, n,
137  }
138 
139  //Check status code
140  if(!error)
141  {
142  //Set the z-coordinate of the result
143  error = mpiSetValue(&r->z, 1);
144  }
145 
146  //Release exclusive access to the CASPER module
148 
149  //Return status code
150  return error;
151 }
152 
153 
154 /**
155  * @brief Twin multiplication
156  * @param[in] params EC domain parameters
157  * @param[out] r Resulting point R = d0.S + d1.T
158  * @param[in] d0 An integer d such as 0 <= d0 < p
159  * @param[in] s EC point
160  * @param[in] d1 An integer d such as 0 <= d1 < p
161  * @param[in] t EC point
162  * @return Error code
163  **/
164 
166  const Mpi *d0, const EcPoint *s, const Mpi *d1, const EcPoint *t)
167 {
168  error_t error;
169  EcPoint u;
170 
171  //Initialize EC point
172  ecInit(&u);
173 
174  //Compute d0.S
175  error = ecMult(params, r, d0, s);
176 
177  //Check status code
178  if(!error)
179  {
180  //Compute d1.T
181  error = ecMult(params, &u, d1, t);
182  }
183 
184  //Check status code
185  if(!error)
186  {
187  //Compute d0.S + d1.T
188  error = ecAdd(params, r, r, &u);
189  }
190 
191  //Release EC point
192  ecFree(&u);
193 
194  //Return status code
195  return error;
196 }
197 
198 #endif
LPC55S6x public-key hardware accelerator.
Arbitrary precision integer.
Definition: mpi.h:80
uint8_t t
Definition: lldp_ext_med.h:212
LPC55S6x hardware cryptographic accelerator.
error_t mpiSetValue(Mpi *r, int_t a)
Set the value of a multiple precision integer.
Definition: mpi.c:484
#define osStrcmp(s1, s2)
Definition: os_port.h:171
error_t ecAdd(const EcDomainParameters *params, EcPoint *r, const EcPoint *s, const EcPoint *t)
Point addition (helper routine)
Definition: ec.c:739
error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format)
Octet string to integer conversion.
Definition: mpi.c:624
EC domain parameters.
Definition: ec.h:76
uint8_t r
Definition: ndp.h:346
error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format)
Integer to octet string conversion.
Definition: mpi.c:709
@ MPI_FORMAT_LITTLE_ENDIAN
Definition: mpi.h:70
error_t
Error codes.
Definition: error.h:43
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
void ecInit(EcPoint *r)
Initialize elliptic curve point.
Definition: ec.c:307
General definitions for cryptographic algorithms.
error_t ecMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d, const EcPoint *s)
Scalar multiplication.
const char_t * name
Curve name.
Definition: ec.h:77
uint8_t u
Definition: lldp_ext_med.h:213
EC point.
Definition: ec.h:64
uint8_t n
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
error_t ecTwinMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d0, const EcPoint *s, const Mpi *d1, const EcPoint *t)
Twin multiplication.
OsMutex lpc55s6xCryptoMutex
uint8_t s
Definition: igmp_common.h:234
EC primitive arguments.
void ecFree(EcPoint *r)
Release an elliptic curve point.
Definition: ec.c:321
ECC (Elliptic Curve Cryptography)
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.