MCUX CLNS
MCUX Crypto Library Normal Secure
mcuxClMath_Functions.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------*/
2 /* Copyright 2020-2021 NXP */
3 /* */
4 /* NXP Confidential. This software is owned or controlled by NXP and may */
5 /* only be used strictly in accordance with the applicable license terms. */
6 /* By expressly accepting such terms or by downloading, installing, */
7 /* activating and/or otherwise using the software, you are agreeing that */
8 /* you have read, and that you agree to comply with and are bound by, such */
9 /* license terms. If you do not agree to be bound by the applicable license */
10 /* terms, then you may not retain, install, activate or otherwise use the */
11 /* software. */
12 /*--------------------------------------------------------------------------*/
13 
20 #ifndef MCUXCLMATH_FUNCTIONS_H_
21 #define MCUXCLMATH_FUNCTIONS_H_
22 
23 
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <mcuxCsslFlowProtection.h>
27 
28 #include <mcuxClPkc.h>
29 #include <mcuxClMath_Types.h>
30 
31 
47  uint32_t i3_i2_i1_i0,
48  uint32_t i7_i6_i5_i4,
49  uint16_t *localPtrUptrt,
50  uint8_t noOfIndices,
51  const uint16_t **oldPtrUptrt
52  );
54 #define MCUXCLMATH_FP_INITLOCALUPTRT(i3_i2_i1_i0, i7_i6_i5_i4, localPtrUptrt, noOfIndices) \
55  ({ \
56  const uint16_t *oldPtrUptrt_; \
57  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, mcuxClMath_InitLocalUptrt(i3_i2_i1_i0, i7_i6_i5_i4, localPtrUptrt, noOfIndices, &oldPtrUptrt_)); \
58  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
59  (oldPtrUptrt_); \
60  })
61 
69  uint8_t iX,
70  uint32_t *nLeadingZeros
71  );
73 #define MCUXCLMATH_FP_LEADINGZEROS(iX) \
74  ({ \
75  uint32_t nLeadingZeros_; \
76  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, mcuxClMath_LeadingZeros(iX, &nLeadingZeros_)); \
77  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
78  (nLeadingZeros_); \
79  })
80 
88  uint8_t iX,
89  uint32_t *nTrailingZeros
90  );
92 #define MCUXCLMATH_FP_TRAILINGZEROS(iX) \
93  ({ \
94  uint32_t nTrailingZeros_; \
95  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, mcuxClMath_TrailingZeros(iX, &nTrailingZeros_)); \
96  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
97  (nTrailingZeros_); \
98  })
99 
110  uint16_t iNShifted_iN
111  );
114 #define MCUXCLMATH_SHIFTMODULUS(iNShifted, iN) \
115  mcuxClMath_ShiftModulus(MCUXCLPKC_PACKARGS2(iNShifted, iN))
116 
117 #define MCUXCLMATH_FP_SHIFTMODULUS(iNShifted, iN) \
118  do { \
119  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, MCUXCLMATH_SHIFTMODULUS(iNShifted, iN)); \
120  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
121  } while(false)
122 
132  uint16_t iN_iT
133  );
136 #define MCUXCLMATH_NDASH(iN, iT) \
137  mcuxClMath_NDash(MCUXCLPKC_PACKARGS2(iN, iT))
138 
139 #define MCUXCLMATH_FP_NDASH(iN, iT) \
140  do { \
141  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, MCUXCLMATH_NDASH(iN, iT)); \
142  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
143  } while(false)
144 
153  uint32_t iQDash_iNShifted_iN_iT,
154  uint16_t len
158  );
160 #define MCUXCLMATH_QDASH(iQDash, iNShifted, iN, iT, len) \
161  mcuxClMath_QDash(MCUXCLPKC_PACKARGS4(iQDash, iNShifted, iN, iT), len)
162 
163 #define MCUXCLMATH_FP_QDASH(iQDash, iNShifted, iN, iT, len) \
164  do { \
165  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, MCUXCLMATH_QDASH(iQDash, iNShifted, iN, iT, len)); \
166  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
167  } while(false)
168 
178  uint32_t iQSqr_iNShifted_iN_iT
179  );
184 #define MCUXCLMATH_QSQUARED(iQSqr, iNShifted, iN, iT) \
185  mcuxClMath_QSquared(MCUXCLPKC_PACKARGS4(iQSqr, iNShifted, iN, iT))
186 
187 #define MCUXCLMATH_FP_QSQUARED(iQSqr, iNShifted, iN, iT) \
188  do { \
189  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, MCUXCLMATH_QSQUARED(iQSqr, iNShifted, iN, iT)); \
190  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
191  } while(false)
192 
203  uint32_t iR_iX_iN_iT
204  );
209 #define MCUXCLMATH_MODINV(iR, iX, iN, iT) \
210  mcuxClMath_ModInv(MCUXCLPKC_PACKARGS4(iR, iX, iN, iT))
211 
212 #define MCUXCLMATH_FP_MODINV(iR, iX, iN, iT) \
213  do { \
214  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, MCUXCLMATH_MODINV(iR, iX, iN, iT)); \
215  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
216  } while(false)
217 
226  uint32_t iR_iX_iN_iT0,
227  uint32_t iT1_iT2_iT3
231  );
235 #define MCUXCLMATH_REDUCEMODEVEN(iR, iX, iN, iT0, iT1, iT2, iT3) \
236  mcuxClMath_ReduceModEven(MCUXCLPKC_PACKARGS4(iR, iX, iN, iT0), MCUXCLPKC_PACKARGS4(0u, iT1, iT2, iT3))
237 
238 #define MCUXCLMATH_FP_REDUCEMODEVEN(iR, iX, iN, iT0, iT1, iT2, iT3) \
239  do { \
240  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, MCUXCLMATH_REDUCEMODEVEN(iR, iX, iN, iT0, iT1, iT2, iT3)); \
241  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
242  } while(false)
243 
271  const uint8_t *pExp,
272  uint32_t byteLenExp,
273  uint32_t iR_iX_iN_iT
274  );
276 #define MCUXCLMATH_MODEXP_SQRMULTL2R(pExp, byteLenExp, iR, iX, iN, iT) \
277  mcuxClMath_ModExp_SqrMultL2R(pExp, byteLenExp, MCUXCLPKC_PACKARGS4(iR, iX, iN, iT))
278 
279 #define MCUXCLMATH_FP_MODEXP_SQRMULTL2R(pExp, byteLenExp, iR, iX, iN, iT) \
280  do { \
281  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, MCUXCLMATH_MODEXP_SQRMULTL2R(pExp, byteLenExp, iR, iX, iN, iT)); \
282  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
283  } while(false)
284 
285 
323  const uint8_t *pExp,
324  uint32_t *pExpTemp,
325  uint32_t byteLenExp,
326  uint32_t iT3_iX_iT2_iT1,
327  uint32_t iN_iTE_iT0_iR
328  );
330 #define MCUXCLMATH_SECMODEXP(pExp, pExpTemp, byteLenExp, iR, iX, iN, iTE, iT0, iT1, iT2, iT3) \
331  mcuxClMath_SecModExp(pExp, pExpTemp, byteLenExp, MCUXCLPKC_PACKARGS4(iT3, iX, iT2, iT1), MCUXCLPKC_PACKARGS4(iN, iTE, iT0, iR))
332 
333 #define MCUXCLMATH_FP_SECMODEXP(pExp, pExpTemp, byteLenExp, iR, iX, iN, iTE, iT0, iT1, iT2, iT3) \
334  do { \
335  MCUX_CSSL_FP_FUNCTION_CALL(retVal, MCUXCLMATH_SECMODEXP(pExp, pExpTemp, byteLenExp, iR, iX, iN, iTE, iT0, iT1, iT2, iT3)); \
336  if (MCUXCLMATH_ERRORCODE_OK != retVal) \
337  { \
338  MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClMath_SecModExp, MCUXCLMATH_ERRORCODE_ERROR); \
339  } \
340  } while(false)
341 
342 
352 mcuxClMath_Status_Protected_t mcuxClMath_ExactDivideOdd(uint32_t iR_iX_iY_iT, uint32_t pkcByteLenX, uint32_t pkcByteLenY);
354 #define MCUXCLMATH_EXACTDIVIDEODD(iR, iX, iN, iT, pkcByteLenX, pkcByteLenY) \
355  mcuxClMath_ExactDivideOdd(MCUXCLPKC_PACKARGS4(iR, iX, iN, iT), pkcByteLenX, pkcByteLenY)
356 
357 #define MCUXCLMATH_FP_EXACTDIVIDEODD(iR, iX, iN, iT, pkcByteLenX, pkcByteLenY) \
358  do { \
359  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, MCUXCLMATH_EXACTDIVIDEODD(iR, iX, iN, iT, pkcByteLenX, pkcByteLenY)); \
360  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
361  } while(false)
362 
363 
404 mcuxClMath_Status_Protected_t mcuxClMath_ExactDivide(uint32_t iR_iX_iY_iT, uint32_t pkcByteLenX, uint32_t pkcByteLenY);
406 #define MCUXCLMATH_EXACTDIVIDE(iR, iX, iN, iT, pkcByteLenX, pkcByteLenY) \
407  mcuxClMath_ExactDivide(MCUXCLPKC_PACKARGS4(iR, iX, iN, iT), pkcByteLenX, pkcByteLenY)
408 
409 #define MCUXCLMATH_FP_EXACTDIVIDE(iR, iX, iN, iT, pkcByteLenX, pkcByteLenY) \
410  do { \
411  MCUX_CSSL_FP_FUNCTION_CALL(retValTemp, MCUXCLMATH_EXACTDIVIDE(iR, iX, iN, iT, pkcByteLenX, pkcByteLenY)); \
412  (void) retValTemp; /* Checking is unnecessary, because it always returns OK. */ \
413  } while(false)
414 
415  /* mcuxClMath_Functions */
419 
420 
421 #endif /* MCUXCLMATH_FUNCTIONS_H_ */
mcuxClMath_Status_Protected_t mcuxClMath_SecModExp(const uint8_t *pExp, uint32_t *pExpTemp, uint32_t byteLenExp, uint32_t iT3_iX_iT2_iT1, uint32_t iN_iTE_iT0_iR)
Securely calculates modular exponentiation.
mcuxClMath_Status_Protected_t mcuxClMath_ShiftModulus(uint16_t iNShifted_iN)
Left shifts modulus until most significant bit of the buffer (length = ps1 OPLEN) is 1,...
mcuxClMath_Status_Protected_t mcuxClMath_NDash(uint16_t iN_iT)
Prepares modulus buffer (initializes NDash) to be used in modular multiplication (inversion,...
uint64_t mcuxClMath_Status_Protected_t
Type for error codes used by code-flow protected Math component functions.
Definition: mcuxClMath_Types.h:49
mcuxClMath_Status_Protected_t mcuxClMath_TrailingZeros(uint8_t iX, uint32_t *nTrailingZeros)
Counts number of trailing zeros of operand in the given buffer (length = ps1 OPLEN).
mcuxClMath_Status_Protected_t mcuxClMath_ModExp_SqrMultL2R(const uint8_t *pExp, uint32_t byteLenExp, uint32_t iR_iX_iN_iT)
Calculates modular exponentiation with left-to-right binary square and multiply algorithm.
mcuxClMath_Status_Protected_t mcuxClMath_ModInv(uint32_t iR_iX_iN_iT)
Calculates modular inversion, X^(-1) mod n.
Top level header of mcuxClPkc component (PKC hardware driver)
mcuxClMath_Status_Protected_t mcuxClMath_LeadingZeros(uint8_t iX, uint32_t *nLeadingZeros)
Counts number of leading zeros of operand in the given buffer (length = ps1 OPLEN).
mcuxClMath_Status_Protected_t mcuxClMath_ReduceModEven(uint32_t iR_iX_iN_iT0, uint32_t iT1_iT2_iT3)
Calculates modular reduction with even modulus, x mod n, where n is even.
Provides the API for the CSSL flow protection mechanism.
mcuxClMath_Status_Protected_t mcuxClMath_InitLocalUptrt(uint32_t i3_i2_i1_i0, uint32_t i7_i6_i5_i4, uint16_t *localPtrUptrt, uint8_t noOfIndices, const uint16_t **oldPtrUptrt)
Initializes the new UPTRT and returns the address of existing UPTRT.
mcuxClMath_Status_Protected_t mcuxClMath_ExactDivide(uint32_t iR_iX_iY_iT, uint32_t pkcByteLenX, uint32_t pkcByteLenY)
Exact division.
mcuxClMath_Status_Protected_t mcuxClMath_ExactDivideOdd(uint32_t iR_iX_iY_iT, uint32_t pkcByteLenX, uint32_t pkcByteLenY)
TODO 1.pkcByteLenX and pkcByteLenY shall be a multiple of pkc wordsize.
mcuxClMath_Status_Protected_t mcuxClMath_QSquared(uint32_t iQSqr_iNShifted_iN_iT)
Calculates Q^2 mod n, where Q = 256^(ps1 OPLEN) mod n.
#define MCUX_CSSL_FP_FUNCTION_DECL(id)
Declaration of a flow protected function.
Definition: mcuxCsslFlowProtection.h:119
mcuxClMath_Status_Protected_t mcuxClMath_QDash(uint32_t iQDash_iNShifted_iN_iT, uint16_t len)
Calculates Q * Q' mod n, where Q = 256^(ps1 OPLEN) mod n, and Q' = 256^len mod n.