lpc55xx_crypto_pkc.c
Go to the documentation of this file.
1 /**
2  * @file lpc55xx_crypto_pkc.c
3  * @brief LPC5500 public-key hardware accelerator
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2023 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.3.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 (LPC55XX_CRYPTO_PKC_SUPPORT == ENABLED)
45 
46 //Global variables
47 static Lpc55xxEcArgs 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"))
70  {
71  n = 32;
72  }
73  else if(!osStrcmp(params->name, "secp384r1"))
74  {
75  n = 48;
76  }
77  else if(!osStrcmp(params->name, "secp521r1"))
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 #endif
General definitions for cryptographic algorithms.
Debugging facilities.
uint8_t n
ECC (Elliptic Curve Cryptography)
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
OsMutex lpc55xxCryptoMutex
LPC5500 hardware cryptographic accelerator.
error_t ecMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d, const EcPoint *s)
Scalar multiplication.
LPC5500 public-key hardware accelerator.
error_t mpiSetValue(Mpi *r, int_t a)
Set the value of a multiple precision integer.
Definition: mpi.c:484
error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format)
Octet string to integer conversion.
Definition: mpi.c:624
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
uint8_t r
Definition: ndp.h:346
uint8_t s
Definition: ndp.h:345
#define osStrcmp(s1, s2)
Definition: os_port.h:171
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
EC domain parameters.
Definition: ec.h:76
const char_t * name
Curve name.
Definition: ec.h:77
EC point.
Definition: ec.h:64
EC primitive arguments.
uint32_t rx[18]
uint32_t sx[18]
uint32_t sy[18]
uint32_t d[18]
uint32_t ry[18]
Arbitrary precision integer.
Definition: mpi.h:80