max32690_crypto_pkc.c
Go to the documentation of this file.
1 /**
2  * @file max32690_crypto_pkc.c
3  * @brief MAX32690 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 "mxc_device.h"
36 #include "mxc_sys.h"
37 #include "ctb.h"
38 #include "core/crypto.h"
41 #include "ecc/ec.h"
42 #include "ecc/ec_misc.h"
43 #include "ecc/curve25519.h"
44 #include "ecc/curve448.h"
45 #include "mpi/mpi.h"
46 #include "debug.h"
47 
48 //Check crypto library configuration
49 #if (MAX32690_CRYPTO_PKC_SUPPORT == ENABLED)
50 
51 
52 /**
53  * @brief Set operand value
54  * @param[in] offset MAA memory offset
55  * @param[in] value Value of the operand
56  * @param[in] length Length of the operand, in bits
57  **/
58 
59 void maaSetValue(uint_t offset, uint32_t value, uint_t length)
60 {
61  uint_t i;
62  volatile uint32_t *p;
63 
64  //Point to the specified MAA memory segment
65  p = (uint32_t *) (MXC_BASE_CTB + offset);
66 
67  //Get the length of the operand, in 64-bit words
68  length = (length + 63) / 64;
69 
70  //Set the value of the operand
71  p[0] = value;
72 
73  //Clear MAA memory
74  for(i = 1; i < (length * 2); i++)
75  {
76  p[i] = 0;
77  }
78 }
79 
80 
81 /**
82  * @brief Import scalar
83  * @param[in] offset MAA memory offset
84  * @param[in] src Pointer to the scalar
85  * @param[in] length Length of the operand, in bits
86  **/
87 
88 void maaImportScalar(uint_t offset, const uint32_t *src, uint_t length)
89 {
90  uint_t i;
91  volatile uint32_t *p;
92 
93  //Point to the specified MAA memory segment
94  p = (uint32_t *) (MXC_BASE_CTB + offset);
95 
96  //Get the length of the operand, in 32-bit words
97  length = (length + 31) / 32;
98 
99  //Copy the scalar to the MAA memory
100  for(i = 0; i < length; i++)
101  {
102  p[i] = src[i];
103  }
104 
105  //Pad the operand with zeroes
106  if((i % 2) != 0)
107  {
108  p[i] = 0;
109  }
110 }
111 
112 
113 /**
114  * @brief Import multiple-precision integer
115  * @param[in] offset MAA memory offset
116  * @param[in] src Pointer to the multiple-precision integer
117  * @param[in] length Length of the operand, in bits
118  **/
119 
120 void maaImportMpi(uint_t offset, const Mpi *src, uint_t length)
121 {
122  uint_t i;
123  volatile uint32_t *p;
124 
125  //Point to the specified MAA memory segment
126  p = (uint32_t *) (MXC_BASE_CTB + offset);
127 
128  //Get the length of the operand, in 64-bit words
129  length = (length + 63) / 64;
130 
131  //Copy the multiple-precision integer to the MAA memory
132  for(i = 0; i < src->size && i < (length * 2); i++)
133  {
134  p[i] = src->data[i];
135  }
136 
137  //Pad the operand with zeroes
138  while(i < (length * 2))
139  {
140  p[i++] = 0;
141  }
142 }
143 
144 
145 /**
146  * @brief Export scalar
147  * @param[in] offset MAA memory offset
148  * @param[out] dest Pointer to the scalar
149  * @param[in] length Length of the operand, in bits
150  **/
151 
152 void maaExportScalar(uint_t offset, uint32_t *dest, uint_t length)
153 {
154  uint_t i;
155  volatile uint32_t *p;
156 
157  //Point to the specified MAA memory segment
158  p = (uint32_t *) (MXC_BASE_CTB + offset);
159 
160  //Get the length of the operand, in 32-bit words
161  length = (length + 31) / 32;
162 
163  //Copy the multiple-precision integer from the PKA RAM
164  for(i = 0; i < length; i++)
165  {
166  dest[i] = p[i];
167  }
168 }
169 
170 
171 /**
172  * @brief Export multiple-precision integer
173  * @param[in] offset MAA memory offset
174  * @param[out] dest Pointer to the multiple-precision integer
175  * @param[in] length Length of the operand, in bits
176  * @return Error code
177  **/
178 
180 {
181  error_t error;
182  uint_t i;
183  volatile uint32_t *p;
184 
185  //Point to the specified MAA memory segment
186  p = (uint32_t *) (MXC_BASE_CTB + offset);
187 
188  //Get the length of the operand, in 32-bit words
189  length = (length + 31) / 32;
190 
191  //Ajust the size of the multiple precision integer
192  error = mpiGrow(dest, length);
193 
194  //Check status code
195  if(!error)
196  {
197  //Copy the multiple-precision integer from the PKA RAM
198  for(i = 0; i < length; i++)
199  {
200  dest->data[i] = p[i];
201  }
202 
203  //Pad the resulting value with zeroes
204  for(; i < dest->size; i++)
205  {
206  dest->data[i] = 0;
207  }
208 
209  //Set the sign
210  dest->sign = 1;
211  }
212 
213  //Return status code
214  return error;
215 }
216 
217 
218 #if (MPI_SUPPORT == ENABLED)
219 
220 /**
221  * @brief Modular exponentiation (fast calculation)
222  * @param[out] r Resulting integer R = A ^ E mod P
223  * @param[in] a Pointer to a multiple precision integer
224  * @param[in] e Exponent
225  * @param[in] p Modulus
226  * @return Error code
227  **/
228 
229 error_t mpiExpModFast(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
230 {
231  error_t error;
232  uint_t modLen;
233 
234  //Get the length of the modulus, in bits
235  modLen = mpiGetBitLength(p);
236 
237  //The accelerator supports operand lengths up to 2048 bits
238  if(modLen <= 2048)
239  {
240  //Acquire exclusive access to the CTB module
242 
243  //Reset the engine by setting CTB_CTRL.rst
244  MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
245 
246  //Software must poll the CTB_CTRL.rst bit until it is set to 1 by
247  //hardware, indicating the cryptographic accelerator is ready for use
248  while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
249  {
250  }
251 
252  //Legacy support for the access behavior of the done flags
253  MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
254 
255  //Specify the size of the modular operation
256  MXC_CTB->maa_maws = modLen;
257 
258  //Copy the operand
260  //For exponentiation operations, b memory must contain the value of 1
262  //The exponent is always stored in memory instance 4
264  //The modulus is always stored in memory instance 5
266 
267  //Set up a modular exponentiation operation (speed-optimized calculation)
268  MXC_CTB->maa_ctrl = CTB_MAA_CTRL_TMA(6) | CTB_MAA_CTRL_RMA(4) |
271 
272  //Start MAA operation
273  MXC_CTB->maa_ctrl |= CTB_MAA_CTRL_START_Msk;
274 
275  //Wait for the MAA operation to complete
276  while((MXC_CTB->ctrl & TB_CTRL_MAA_DONE_Msk) == 0)
277  {
278  }
279 
280  //Copy the result
281  error = maaExportMpi(CTB_MAA_MEM_INSTANCE_2, r, modLen);
282 
283  //Release exclusive access to the CTB module
285  }
286  else
287  {
288  //Perform modular exponentiation (r = a ^ e mod p)
289  error = mpiExpMod(r, a, e, p);
290  }
291 
292  //Return status code
293  return error;
294 }
295 
296 
297 /**
298  * @brief Modular exponentiation (regular calculation)
299  * @param[out] r Resulting integer R = A ^ E mod P
300  * @param[in] a Pointer to a multiple precision integer
301  * @param[in] e Exponent
302  * @param[in] p Modulus
303  * @return Error code
304  **/
305 
306 error_t mpiExpModRegular(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
307 {
308  error_t error;
309  uint_t modLen;
310 
311  //Get the length of the modulus, in bits
312  modLen = mpiGetBitLength(p);
313 
314  //The accelerator supports operand lengths up to 2048 bits
315  if(modLen <= 2048)
316  {
317  //Acquire exclusive access to the CTB module
319 
320  //Reset the engine by setting CTB_CTRL.rst
321  MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
322 
323  //Software must poll the CTB_CTRL.rst bit until it is set to 1 by
324  //hardware, indicating the cryptographic accelerator is ready for use
325  while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
326  {
327  }
328 
329  //Legacy support for the access behavior of the done flags
330  MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
331 
332  //Specify the size of the modular operation
333  MXC_CTB->maa_maws = modLen;
334 
335  //Copy the operand
337  //For exponentiation operations, b memory must contain the value of 1
339  //The exponent is always stored in memory instance 4
341  //The modulus is always stored in memory instance 5
343 
344  //Set up a modular exponentiation operation
345  MXC_CTB->maa_ctrl = CTB_MAA_CTRL_TMA(6) | CTB_MAA_CTRL_RMA(4) |
348 
349  //Start MAA operation
350  MXC_CTB->maa_ctrl |= CTB_MAA_CTRL_START_Msk;
351 
352  //Wait for the MAA operation to complete
353  while((MXC_CTB->ctrl & TB_CTRL_MAA_DONE_Msk) == 0)
354  {
355  }
356 
357  //Copy the result
358  error = maaExportMpi(CTB_MAA_MEM_INSTANCE_2, r, modLen);
359 
360  //Release exclusive access to the CTB module
362  }
363  else
364  {
365  //Perform modular exponentiation (r = a ^ e mod p)
366  error = mpiExpMod(r, a, e, p);
367  }
368 
369  //Return status code
370  return error;
371 }
372 
373 #endif
374 #if (EC_SUPPORT == ENABLED)
375 
376 /**
377  * @brief Modular addition
378  * @param[in] curve Elliptic curve parameters
379  * @param[out] r Resulting integer R = (A + B) mod p
380  * @param[in] a An integer such as 0 <= A < p
381  * @param[in] b An integer such as 0 <= B < p
382  **/
383 
384 void ecFieldAddMod2(const EcCurve *curve, uint32_t *r, const uint32_t *a,
385  const uint32_t *b)
386 {
387  uint_t modLen;
388 
389  //Get the length of the modulus, in bits
390  modLen = curve->fieldSize;
391 
392  //Acquire exclusive access to the CTB module
394 
395  //Reset the engine by setting CTB_CTRL.rst
396  MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
397 
398  //Software must poll the CTB_CTRL.rst bit until it is set to 1 by hardware,
399  //indicating the cryptographic accelerator is ready for use
400  while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
401  {
402  }
403 
404  //Legacy support for the access behavior of the done flags
405  MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
406 
407  //Specify the size of the modular operation
408  MXC_CTB->maa_maws = modLen;
409 
410  //Copy the first operand
412  //Copy the second operand
414  //The modulus is always stored in memory instance 5
415  maaImportScalar(CTB_MAA_MEM_INSTANCE_5, curve->p, modLen);
416 
417  //Set up a modular addition operation
418  MXC_CTB->maa_ctrl = CTB_MAA_CTRL_TMA(6) | CTB_MAA_CTRL_RMA(4) |
421 
422  //Start MAA operation
423  MXC_CTB->maa_ctrl |= CTB_MAA_CTRL_START_Msk;
424 
425  //Wait for the MAA operation to complete
426  while((MXC_CTB->ctrl & TB_CTRL_MAA_DONE_Msk) == 0)
427  {
428  }
429 
430  //Copy the result
432 
433  //Release exclusive access to the CTB module
435 }
436 
437 
438 /**
439  * @brief Modular subtraction
440  * @param[in] curve Elliptic curve parameters
441  * @param[out] r Resulting integer R = (A - B) mod p
442  * @param[in] a An integer such as 0 <= A < p
443  * @param[in] b An integer such as 0 <= B < p
444  **/
445 
446 void ecFieldSubMod2(const EcCurve *curve, uint32_t *r, const uint32_t *a,
447  const uint32_t *b)
448 {
449  uint_t modLen;
450 
451  //Get the length of the modulus, in bits
452  modLen = curve->fieldSize;
453 
454  //Acquire exclusive access to the CTB module
456 
457  //Reset the engine by setting CTB_CTRL.rst
458  MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
459 
460  //Software must poll the CTB_CTRL.rst bit until it is set to 1 by hardware,
461  //indicating the cryptographic accelerator is ready for use
462  while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
463  {
464  }
465 
466  //Legacy support for the access behavior of the done flags
467  MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
468 
469  //Specify the size of the modular operation
470  MXC_CTB->maa_maws = modLen;
471 
472  //Copy the first operand
474  //Copy the second operand
476  //The modulus is always stored in memory instance 5
477  maaImportScalar(CTB_MAA_MEM_INSTANCE_5, curve->p, modLen);
478 
479  //Set up a modular subtraction operation
480  MXC_CTB->maa_ctrl = CTB_MAA_CTRL_TMA(6) | CTB_MAA_CTRL_RMA(4) |
483 
484  //Start MAA operation
485  MXC_CTB->maa_ctrl |= CTB_MAA_CTRL_START_Msk;
486 
487  //Wait for the MAA operation to complete
488  while((MXC_CTB->ctrl & TB_CTRL_MAA_DONE_Msk) == 0)
489  {
490  }
491 
492  //Copy the result
494 
495  //Release exclusive access to the CTB module
497 }
498 
499 
500 /**
501  * @brief Modular multiplication
502  * @param[in] curve Elliptic curve parameters
503  * @param[out] r Resulting integer R = (A * B) mod p
504  * @param[in] a An integer such as 0 <= A < p
505  * @param[in] b An integer such as 0 <= B < p
506  **/
507 
508 void ecFieldMulMod(const EcCurve *curve, uint32_t *r, const uint32_t *a,
509  const uint32_t *b)
510 {
511  uint_t modLen;
512 
513  //Check elliptic curve parameters
514  if(curve->fieldInv == NULL)
515  {
516  //Get the length of the modulus, in bits
517  modLen = curve->fieldSize;
518 
519  //Acquire exclusive access to the CTB module
521 
522  //Reset the engine by setting CTB_CTRL.rst
523  MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
524 
525  //Software must poll the CTB_CTRL.rst bit until it is set to 1 by hardware,
526  //indicating the cryptographic accelerator is ready for use
527  while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
528  {
529  }
530 
531  //Legacy support for the access behavior of the done flags
532  MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
533 
534  //Specify the size of the modular operation
535  MXC_CTB->maa_maws = modLen;
536 
537  //Copy the first operand
539  //Copy the second operand
541  //The modulus is always stored in memory instance 5
542  maaImportScalar(CTB_MAA_MEM_INSTANCE_5, curve->p, modLen);
543 
544  //Set up a modular multiplication operation
545  MXC_CTB->maa_ctrl = CTB_MAA_CTRL_TMA(6) | CTB_MAA_CTRL_RMA(4) |
548 
549  //Start MAA operation
550  MXC_CTB->maa_ctrl |= CTB_MAA_CTRL_START_Msk;
551 
552  //Wait for the MAA operation to complete
553  while((MXC_CTB->ctrl & TB_CTRL_MAA_DONE_Msk) == 0)
554  {
555  }
556 
557  //Copy the result
559 
560  //Release exclusive access to the CTB module
562  }
563  else
564  {
565  uint32_t u[EC_MAX_MODULUS_SIZE * 2];
566 
567  //Get the length of the modulus, in words
568  modLen = (curve->fieldSize + 31) / 32;
569 
570  //Compute R = (A * B) mod p
571  ecScalarMul(u, u + modLen, a, b, modLen);
572  curve->fieldMod(curve, r, u);
573  }
574 }
575 
576 
577 /**
578  * @brief Modular squaring
579  * @param[in] curve Elliptic curve parameters
580  * @param[out] r Resulting integer R = A^2 mod p
581  * @param[in] a An integer such as 0 <= A < p
582  **/
583 
584 void ecFieldSqrMod(const EcCurve *curve, uint32_t *r, const uint32_t *a)
585 {
586  uint_t modLen;
587 
588  //Check elliptic curve parameters
589  if(curve->fieldInv == NULL)
590  {
591  //Get the length of the modulus, in bits
592  modLen = curve->fieldSize;
593 
594  //Acquire exclusive access to the CTB module
596 
597  //Reset the engine by setting CTB_CTRL.rst
598  MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
599 
600  //Software must poll the CTB_CTRL.rst bit until it is set to 1 by hardware,
601  //indicating the cryptographic accelerator is ready for use
602  while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
603  {
604  }
605 
606  //Legacy support for the access behavior of the done flags
607  MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
608 
609  //Specify the size of the modular operation
610  MXC_CTB->maa_maws = modLen;
611 
612  //Copy the operand
614  //The modulus is always stored in memory instance 5
615  maaImportScalar(CTB_MAA_MEM_INSTANCE_5, curve->p, modLen);
616 
617  //Set up a modular square operation
618  MXC_CTB->maa_ctrl = CTB_MAA_CTRL_TMA(6) | CTB_MAA_CTRL_RMA(4) |
621 
622  //Start MAA operation
623  MXC_CTB->maa_ctrl |= CTB_MAA_CTRL_START_Msk;
624 
625  //Wait for the MAA operation to complete
626  while((MXC_CTB->ctrl & TB_CTRL_MAA_DONE_Msk) == 0)
627  {
628  }
629 
630  //Copy the result
632 
633  //Release exclusive access to the CTB module
635  }
636  else
637  {
638  uint32_t u[EC_MAX_MODULUS_SIZE * 2];
639 
640  //Get the length of the modulus, in words
641  modLen = (curve->fieldSize + 31) / 32;
642 
643  //Compute R = (A ^ 2) mod p
644  ecScalarSqr(u, a, modLen);
645  curve->fieldMod(curve, r, u);
646  }
647 }
648 
649 
650 /**
651  * @brief Modular multiplication
652  * @param[in] curve Elliptic curve parameters
653  * @param[out] r Resulting integer R = (A * B) mod q
654  * @param[in] a An integer such as 0 <= A < q
655  * @param[in] b An integer such as 0 <= B < q
656  **/
657 
658 void ecScalarMulMod(const EcCurve *curve, uint32_t *r, const uint32_t *a,
659  const uint32_t *b)
660 {
661  uint_t orderLen;
662 
663  //Get the length of the order, in bits
664  orderLen = curve->orderSize;
665 
666  //Acquire exclusive access to the CTB module
668 
669  //Reset the engine by setting CTB_CTRL.rst
670  MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
671 
672  //Software must poll the CTB_CTRL.rst bit until it is set to 1 by hardware,
673  //indicating the cryptographic accelerator is ready for use
674  while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
675  {
676  }
677 
678  //Legacy support for the access behavior of the done flags
679  MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
680 
681  //Specify the size of the modular operation
682  MXC_CTB->maa_maws = orderLen;
683 
684  //Copy the first operand
686  //Copy the second operand
688  //The modulus is always stored in memory instance 5
689  maaImportScalar(CTB_MAA_MEM_INSTANCE_5, curve->q, orderLen);
690 
691  //Set up a modular multiplication operation
692  MXC_CTB->maa_ctrl = CTB_MAA_CTRL_TMA(6) | CTB_MAA_CTRL_RMA(4) |
695 
696  //Start MAA operation
697  MXC_CTB->maa_ctrl |= CTB_MAA_CTRL_START_Msk;
698 
699  //Wait for the MAA operation to complete
700  while((MXC_CTB->ctrl & TB_CTRL_MAA_DONE_Msk) == 0)
701  {
702  }
703 
704  //Copy the result
706 
707  //Release exclusive access to the CTB module
709 }
710 
711 
712 /**
713  * @brief Modular squaring
714  * @param[in] curve Elliptic curve parameters
715  * @param[out] r Resulting integer R = A^2 mod q
716  * @param[in] a An integer such as 0 <= A < q
717  **/
718 
719 void ecScalarSqrMod(const EcCurve *curve, uint32_t *r, const uint32_t *a)
720 {
721  uint_t orderLen;
722 
723  //Get the length of the order, in bits
724  orderLen = curve->orderSize;
725 
726  //Acquire exclusive access to the CTB module
728 
729  //Reset the engine by setting CTB_CTRL.rst
730  MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
731 
732  //Software must poll the CTB_CTRL.rst bit until it is set to 1 by hardware,
733  //indicating the cryptographic accelerator is ready for use
734  while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
735  {
736  }
737 
738  //Legacy support for the access behavior of the done flags
739  MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
740 
741  //Specify the size of the modular operation
742  MXC_CTB->maa_maws = orderLen;
743 
744  //Copy the operand
746  //The modulus is always stored in memory instance 5
747  maaImportScalar(CTB_MAA_MEM_INSTANCE_5, curve->q, orderLen);
748 
749  //Set up a modular square operation
750  MXC_CTB->maa_ctrl = CTB_MAA_CTRL_TMA(6) | CTB_MAA_CTRL_RMA(4) |
753 
754  //Start MAA operation
755  MXC_CTB->maa_ctrl |= CTB_MAA_CTRL_START_Msk;
756 
757  //Wait for the MAA operation to complete
758  while((MXC_CTB->ctrl & TB_CTRL_MAA_DONE_Msk) == 0)
759  {
760  }
761 
762  //Copy the result
764 
765  //Release exclusive access to the CTB module
767 }
768 
769 #endif
770 #if (X25519_SUPPORT == ENABLED || ED25519_SUPPORT == ENABLED)
771 
772 /**
773  * @brief Modular multiplication
774  * @param[out] r Resulting integer R = (A * B) mod p
775  * @param[in] a An integer such as 0 <= A < p
776  * @param[in] b An integer such as 0 <= B < p
777  **/
778 
779 void curve25519Mul2(uint32_t *r, const uint32_t *a, const uint32_t *b)
780 {
781  volatile uint32_t *p;
782 
783  //Acquire exclusive access to the CTB module
785 
786  //Reset the engine by setting CTB_CTRL.rst
787  MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
788 
789  //Software must poll the CTB_CTRL.rst bit until it is set to 1 by hardware,
790  //indicating the cryptographic accelerator is ready for use
791  while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
792  {
793  }
794 
795  //Legacy support for the access behavior of the done flags
796  MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
797 
798  //Specify the size of the modular operation
799  MXC_CTB->maa_maws = 255;
800 
801  //Copy the first operand
802  p = (uint32_t *) (MXC_BASE_CTB + CTB_MAA_MEM_INSTANCE_0);
803  p[0] = a[0];
804  p[1] = a[1];
805  p[2] = a[2];
806  p[3] = a[3];
807  p[4] = a[4];
808  p[5] = a[5];
809  p[6] = a[6];
810  p[7] = a[7];
811 
812  //Copy the second operand
813  p = (uint32_t *) (MXC_BASE_CTB + CTB_MAA_MEM_INSTANCE_1);
814  p[0] = b[0];
815  p[1] = b[1];
816  p[2] = b[2];
817  p[3] = b[3];
818  p[4] = b[4];
819  p[5] = b[5];
820  p[6] = b[6];
821  p[7] = b[7];
822 
823  //The modulus is always stored in memory instance 5
824  p = (uint32_t *) (MXC_BASE_CTB + CTB_MAA_MEM_INSTANCE_5);
825  p[0] = 0xFFFFFFED;
826  p[1] = 0xFFFFFFFF;
827  p[2] = 0xFFFFFFFF;
828  p[3] = 0xFFFFFFFF;
829  p[4] = 0xFFFFFFFF;
830  p[5] = 0xFFFFFFFF;
831  p[6] = 0xFFFFFFFF;
832  p[7] = 0x7FFFFFFF;
833 
834  //Set up a modular multiplication operation
835  MXC_CTB->maa_ctrl = CTB_MAA_CTRL_TMA(6) | CTB_MAA_CTRL_RMA(4) |
838 
839  //Start MAA operation
840  MXC_CTB->maa_ctrl |= CTB_MAA_CTRL_START_Msk;
841 
842  //Wait for the MAA operation to complete
843  while((MXC_CTB->ctrl & TB_CTRL_MAA_DONE_Msk) == 0)
844  {
845  }
846 
847  //Copy the result
848  p = (uint32_t *) (MXC_BASE_CTB + CTB_MAA_MEM_INSTANCE_2);
849  r[0] = p[0];
850  r[1] = p[1];
851  r[2] = p[2];
852  r[3] = p[3];
853  r[4] = p[4];
854  r[5] = p[5];
855  r[6] = p[6];
856  r[7] = p[7];
857 
858  //Release exclusive access to the CTB module
860 }
861 
862 #endif
863 #if (X448_SUPPORT == ENABLED || ED448_SUPPORT == ENABLED)
864 
865 /**
866  * @brief Modular multiplication
867  * @param[out] r Resulting integer R = (A * B) mod p
868  * @param[in] a An integer such as 0 <= A < p
869  * @param[in] b An integer such as 0 <= B < p
870  **/
871 
872 void curve448Mul2(uint32_t *r, const uint32_t *a, const uint32_t *b)
873 {
874  volatile uint32_t *p;
875 
876  //Acquire exclusive access to the CTB module
878 
879  //Reset the engine by setting CTB_CTRL.rst
880  MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
881 
882  //Software must poll the CTB_CTRL.rst bit until it is set to 1 by hardware,
883  //indicating the cryptographic accelerator is ready for use
884  while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
885  {
886  }
887 
888  //Legacy support for the access behavior of the done flags
889  MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
890 
891  //Specify the size of the modular operation
892  MXC_CTB->maa_maws = 448;
893 
894  //Copy the first operand
895  p = (uint32_t *) (MXC_BASE_CTB + CTB_MAA_MEM_INSTANCE_0);
896  p[0] = (a[1] << 28) | a[0];
897  p[1] = (a[2] << 24) | (a[1] >> 4);
898  p[2] = (a[3] << 20) | (a[2] >> 8);
899  p[3] = (a[4] << 16) | (a[3] >> 12);
900  p[4] = (a[5] << 12) | (a[4] >> 16);
901  p[5] = (a[6] << 8) | (a[5] >> 20);
902  p[6] = (a[7] << 4) | (a[6] >> 24);
903  p[7] = (a[9] << 28) | a[8];
904  p[8] = (a[10] << 24) | (a[9] >> 4);
905  p[9] = (a[11] << 20) | (a[10] >> 8);
906  p[10] = (a[12] << 16) | (a[11] >> 12);
907  p[11] = (a[13] << 12) | (a[12] >> 16);
908  p[12] = (a[14] << 8) | (a[13] >> 20);
909  p[13] = (a[15] << 4) | (a[14] >> 24);
910 
911  //Copy the second operand
912  p = (uint32_t *) (MXC_BASE_CTB + CTB_MAA_MEM_INSTANCE_1);
913  p[0] = (b[1] << 28) | b[0];
914  p[1] = (b[2] << 24) | (b[1] >> 4);
915  p[2] = (b[3] << 20) | (b[2] >> 8);
916  p[3] = (b[4] << 16) | (b[3] >> 12);
917  p[4] = (b[5] << 12) | (b[4] >> 16);
918  p[5] = (b[6] << 8) | (b[5] >> 20);
919  p[6] = (b[7] << 4) | (b[6] >> 24);
920  p[7] = (b[9] << 28) | b[8];
921  p[8] = (b[10] << 24) | (b[9] >> 4);
922  p[9] = (b[11] << 20) | (b[10] >> 8);
923  p[10] = (b[12] << 16) | (b[11] >> 12);
924  p[11] = (b[13] << 12) | (b[12] >> 16);
925  p[12] = (b[14] << 8) | (b[13] >> 20);
926  p[13] = (b[15] << 4) | (b[14] >> 24);
927 
928  //The modulus is always stored in memory instance 5
929  p = (uint32_t *) (MXC_BASE_CTB + CTB_MAA_MEM_INSTANCE_5);
930  p[0] = 0xFFFFFFFF;
931  p[1] = 0xFFFFFFFF;
932  p[2] = 0xFFFFFFFF;
933  p[3] = 0xFFFFFFFF;
934  p[4] = 0xFFFFFFFF;
935  p[5] = 0xFFFFFFFF;
936  p[6] = 0xFFFFFFFF;
937  p[7] = 0xFFFFFFFE;
938  p[8] = 0xFFFFFFFF;
939  p[9] = 0xFFFFFFFF;
940  p[10] = 0xFFFFFFFF;
941  p[11] = 0xFFFFFFFF;
942  p[12] = 0xFFFFFFFF;
943  p[13] = 0xFFFFFFFF;
944 
945  //Set up a modular multiplication operation
946  MXC_CTB->maa_ctrl = CTB_MAA_CTRL_TMA(6) | CTB_MAA_CTRL_RMA(4) |
949 
950  //Start MAA operation
951  MXC_CTB->maa_ctrl |= CTB_MAA_CTRL_START_Msk;
952 
953  //Wait for the MAA operation to complete
954  while((MXC_CTB->ctrl & TB_CTRL_MAA_DONE_Msk) == 0)
955  {
956  }
957 
958  //Copy the result
959  p = (uint32_t *) (MXC_BASE_CTB + CTB_MAA_MEM_INSTANCE_2);
960  r[0] = p[0] & 0x0FFFFFFF;
961  r[1] = p[0] >> 28 | ((p[1] << 4) & 0x0FFFFFF0);
962  r[2] = p[1] >> 24 | ((p[2] << 8) & 0x0FFFFF00);
963  r[3] = p[2] >> 20 | ((p[3] << 12) & 0x0FFFF000);
964  r[4] = p[3] >> 16 | ((p[4] << 16) & 0x0FFF0000);
965  r[5] = p[4] >> 12 | ((p[5] << 20) & 0x0FF00000);
966  r[6] = p[5] >> 8 | ((p[6] << 24) & 0x0F000000);
967  r[7] = p[6] >> 4;
968  r[8] = p[7] & 0x0FFFFFFF;
969  r[9] = p[7] >> 28 | ((p[8] << 4) & 0x0FFFFFF0);
970  r[10] = p[8] >> 24 | ((p[9] << 8) & 0x0FFFFF00);
971  r[11] = p[9] >> 20 | ((p[10] << 12) & 0x0FFFF000);
972  r[12] = p[10] >> 16 | ((p[11] << 16) & 0x0FFF0000);
973  r[13] = p[11] >> 12 | ((p[12] << 20) & 0x0FF00000);
974  r[14] = p[12] >> 8 | ((p[13] << 24) & 0x0F000000);
975  r[15] = p[13] >> 4;
976 
977  //Release exclusive access to the CTB module
979 }
980 
981 #endif
982 #endif
void curve25519Mul2(uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular multiplication.
Curve448 elliptic curve (constant-time implementation)
#define CTB_MAA_CTRL_AMA(n)
uint8_t b
Definition: nbns_common.h:104
void maaSetValue(uint_t offset, uint32_t value, uint_t length)
Set operand value.
__weak_func void ecScalarSqr(uint32_t *r, const uint32_t *a, uint_t n)
Squaring operation.
Definition: ec_misc.c:832
MAX32690 hardware cryptographic accelerator.
uint8_t a
Definition: ndp.h:411
Arbitrary precision integer.
Definition: mpi.h:102
uint8_t p
Definition: ndp.h:300
#define CTB_MAA_CTRL_CALC_MOD_EXP_Val
#define CTB_MAA_CTRL_OPT_Msk
#define CTB_MAA_CTRL_CALC_MOD_SUB_Val
void ecFieldSubMod2(const EcCurve *curve, uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular subtraction.
#define CTB_MAA_CTRL_START_Msk
uint8_t r
Definition: ndp.h:346
void ecFieldAddMod2(const EcCurve *curve, uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular addition.
#define CTB_MAA_MEM_INSTANCE_1
void ecScalarSqrMod(const EcCurve *curve, uint32_t *r, const uint32_t *a)
Modular squaring.
error_t maaExportMpi(uint_t offset, Mpi *dest, uint_t length)
Export multiple-precision integer.
error_t
Error codes.
Definition: error.h:43
#define CTB_MAA_CTRL_RMA(n)
error_t mpiExpModFast(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation (fast calculation)
void ecScalarMulMod(const EcCurve *curve, uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular multiplication.
MAX32690 public-key hardware accelerator.
Helper routines for ECC.
MPI (Multiple Precision Integer Arithmetic)
#define CTB_MAA_CTRL_CALC_MOD_ADD_Val
General definitions for cryptographic algorithms.
#define CTB_MAA_MEM_INSTANCE_5
uint8_t u
Definition: lldp_ext_med.h:213
uint8_t length
Definition: tcp.h:375
uint_t mpiGetBitLength(const Mpi *a)
Get the actual length in bits.
Definition: mpi.c:254
#define CTB_MAA_MEM_INSTANCE_2
void maaImportScalar(uint_t offset, const uint32_t *src, uint_t length)
Import scalar.
void maaExportScalar(uint_t offset, uint32_t *dest, uint_t length)
Export scalar.
#define CTB_MAA_MEM_INSTANCE_0
#define CTB_MAA_MEM_INSTANCE_4
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void maaImportMpi(uint_t offset, const Mpi *src, uint_t length)
Import multiple-precision integer.
uint_t size
Definition: mpi.h:104
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
#define CTB_MAA_CTRL_TMA(n)
#define CTB_MAA_CTRL_CALC_MOD_MUL_Val
#define CTB_MAA_CTRL_CALC_MOD_SQR_Val
uint8_t value[]
Definition: tcp.h:376
Curve25519 elliptic curve (constant-time implementation)
OsMutex max32690CryptoMutex
#define TB_CTRL_MAA_DONE_Msk
#define EcCurve
Definition: ec.h:346
void ecFieldMulMod(const EcCurve *curve, uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular multiplication.
#define CTB_MAA_CTRL_BMA(n)
__weak_func void ecScalarMul(uint32_t *rl, uint32_t *rh, const uint32_t *a, const uint32_t *b, uint_t n)
Multiplication of two integers.
Definition: ec_misc.c:766
unsigned int uint_t
Definition: compiler_port.h:57
ECC (Elliptic Curve Cryptography)
mpi_word_t * data
Definition: mpi.h:106
error_t mpiGrow(Mpi *r, uint_t size)
Adjust the size of multiple precision integer.
Definition: mpi.c:102
#define CTB_MAA_CTRL_CALC(n)
void ecFieldSqrMod(const EcCurve *curve, uint32_t *r, const uint32_t *a)
Modular squaring.
error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation.
#define EC_MAX_MODULUS_SIZE
Definition: ec.h:284
Debugging facilities.
int_t sign
Definition: mpi.h:103
void curve448Mul2(uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular multiplication.
error_t mpiExpModRegular(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation (regular calculation)