1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 
9 #ifndef PKA_EXPORT_H
10 #define PKA_EXPORT_H
11 
12 #include "cc_pal_types.h"
13 #include "cc_error.h"
14 
15 #ifdef __cplusplus
16 extern "C"
17 {
18 #endif
19 
20 /* the macro gets two bits [i+1,i] LE words array */
21 #define PKI_GET_TWO_BITS_FROM_WORDS_ARRAY(pArr, i) \
22    ((pArr[(i)>>5] >> ((i)&31)) & 3)
23 /* the macro gets bit[i] from LE words array */
24 #define PKI_GET_BIT_FROM_WORDS_ARRAY(pArr, i) \
25    ((pArr[(i)>>5] >> ((i)&31)) & 1)
26 
27 
28 bool PkiIsModSquareRootExists(void);
29 
30 void PkiClearAllPka(void);
31 
32 void PkiConditionalSecureSwapUint32(uint32_t *x,
33                     uint32_t *y,
34                     uint32_t swp);
35 
36 CCError_t  PkiCalcNp(uint32_t *pNp,
37             uint32_t *pN,
38             uint32_t  sizeNbits);
39 
40 
41 CCError_t  PkiLongNumDiv(uint32_t *pNumA,
42                uint32_t numASizeInWords,
43                uint32_t *pNumB,
44                uint32_t numBSizeInWords,
45                uint32_t *pModRes,
46                uint32_t *pDivRes);
47 
48 
49 CCError_t PkiLongNumMul(uint32_t *pNumA ,
50               uint32_t  ASizeInBits,
51               uint32_t *pNumB ,
52               uint32_t *pRes);
53 
54 /*!< get next two bits of scalar*/
55 uint32_t PkiGetNextTwoMsBits(uint32_t *pScalar, uint32_t *pWord, int32_t i);
56 
57 /*!< the function checks is array equal to 0  *
58 *    if(arr == 0) return 0, else 1.           */
59 bool PkiIsUint8ArrayEqualTo0(const uint8_t *arr, size_t size);
60 
61 /*!< the function compares equality of two buffers of same size:
62      if they are equal - return 1, else 0. */
63 bool PkiAreBuffersEqual(const void *buff1, const void *buff2, size_t sizeInBytes);
64 
65 /************************************************************************/
66 /**
67  * @brief The function copies uint8 big endianness data into PKA register.
68  *
69  *  Notes: Assumed all parameters are checked before calling this function.
70  *         PKA registers used: dstReg.
71  *         The size of pTemp buffer is at least PKA register size appropriate to
72  *         register size lenID and dataSizeBytes <= temp buffer size (in bytes).
73  *
74  * @return  no return value
75  */
76 void PkaCopyBe8DataIntoPkaReg(uint32_t  dstReg,       /* [out] virtual pointer to destination PKA register. */
77                  uint32_t lenId,          /* [in] PKA register length ID.*/
78                  const uint8_t *pSrc,     /* [in] pointer to source buffer. */
79                  uint32_t dataSizeBytes,  /* [in] size of the data in bytes */
80                  uint32_t *pTemp);        /* [in] pointer to the temp buffer of size >= register size. */
81 
82 /************************************************************************/
83 /**
84  * @brief The function copies data from PKA register into uint8 buffer in big endianness order.
85  *
86  *  Notes: Assumed all parameters are checked before calling this function.
87  *         PKA registers used: srcReg.
88  *         The size (in 32-bit words) of pTemp buffer is at least the data size,
89  *         in words (rounded up).
90  *
91  * @return  no return value
92  */
93 void PkaCopyDataFromPkaRegToBe8Buff(
94             uint8_t  *pDst,      /* [out] pointer to the destination buffer of size >= sizeBytes. */
95             uint32_t  srcReg,    /* [in] virtual pointer to source PKA register. */
96             uint32_t  sizeBytes, /* [in] size of the data in PKA register in bytes */
97             uint32_t  *pTemp);   /* [in] pointer to the temp buffer of size (in bytes) >= sizeBytes. */
98 
99 
100 /***********     PkiExecExpBe  function      **********************/
101 /**
102  * @brief Executes the modular exponentiation using PKA.
103  *
104  *  The function input/output arrays are in big endianness order of bytes.
105  *
106  *  Notes: Assumed all parameters are checked before calling this function.
107  *         PKA registers used: r0,r1,r2,r3,r4, r30,r31.
108  *         The size of the pOut and pTemp buffers is at least the modulus size.
109  *
110  * @return  no return value
111  */
112 CCError_t  PkiExecModExpBe(
113                 uint8_t *pOut,        /* [out] pointer to the exponentiation result with size,
114                                                equaled to modulus size in words (including leading zeros. */
115                 uint8_t *pIn,         /* [in]  pointer to the input data. */
116                 uint32_t inSizeBytes, /* [in]  input data size in words */
117                 uint8_t *pMod,        /* [in]  pointer to the modulus. */
118                 uint32_t modSizeBits, /* [in]  modulus size in bits */
119                 uint8_t *pExp,        /* [in]  pointer to the exponent buffer. */
120                 uint32_t expSizeBytes,/* [in]  exponent size in words. */
121                 uint32_t *pTemp);     /* [in]  pointer to the temp buffer of size >= modulus size. */
122 
123 
124 /***********     PkiExecExpLeW  function      **********************/
125 /**
126  * @brief Executes modular exponentiation using PKA for LE input parameters.
127  *
128  *  The function input/output arrays are given as little endianness words arrays.
129  *
130  *  Notes: Assumed all parameters are checked before calling this function.
131  *         All sizes aligned to 32-bit words, leading zeros are present if exist.
132  *         PKA registers used: r0,r1,r2,r3,r4, r30,r31.
133  *
134  * @return  no return value
135  */
136 CCError_t  PkiExecModExpLeW(
137                 uint32_t *pOut,        /* [out] pointer to the exponentiation result with size, equalled
138                                                 to modulus size in words (including leading zeros). */
139                 uint32_t *pIn,         /* [in]  pointer to the input data. */
140                 uint32_t inSizeWords,  /* [in]  input data size in words */
141                 uint32_t *pMod,        /* [in]  pointer to the modulus. */
142                 uint32_t modSizeBits,  /* [in]  modulus size in bits */
143                 uint32_t *pExp,        /* [in]  pointer to the exponent buffer. */
144                 uint32_t expSizeWords);/* [in]  exponent size: should be <= modulus size (in words). */
145 
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif
152