aboutsummaryrefslogtreecommitdiff
path: root/lib/ext/cryptocell-312-runtime/codesafe/src/crypto_api/ec_mont/cc_ec_mont.c
blob: e7d8cf46502a74123d574e4d744dc0e47e6d84ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
 * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <string.h>
#include "cc_pal_types.h"
#include "cc_pal_mem.h"
#include "cc_hash_defs.h"
#include "cc_common.h"
#include "cc_rnd_error.h"
#include "cc_ec_mont_api.h"
#include "mbedtls_cc_ec_mont_edw_error.h"
#include "ec_mont_local.h"
#include "ec_mont.h"
#include "cc_general_defs.h"
#include "md.h"

// RL Debug
#include "pki_dbg.h"

/******************************************************************************/
/*!
   The function performs EC Montgomery scalar multiplication:
         resPoint = scalar * point.<br>
  <br>
         Note: all byte arrays have LE order of bytes, i.e. LS byte is on left most place.<br>
  <br>
  return CCError_t
*/
CEXPORT_C CCError_t CC_EcMontScalarmult(
                                uint8_t       *pResPoint,       /*!< [out] Pointer to the public (secret) key. */
                                size_t        *pResPointSize,   /*!< [in/out] Pointer to the size of the public key in bytes.
                                                                       In  - the size of the buffer. must be at least EC modulus
                                                                             size (for curve25519 - 32 bytes).
                                                                       Out - the actual size. */
                                const uint8_t *pScalar,         /*!< [in] Pointer to the secret (private) key. */
                                size_t         scalarSize,      /*!< [in] Pointer to the size of the secret key in bytes;
                                                                     must be equal to EC order size (for curve25519 - 32 bytes). */
                                const uint8_t *pInPoint,        /*!< [in] Pointer to the input point (compressed). */
                                size_t         inPointSize,     /*!< [in] Size of the point - must be equal to CC_EC_MONT_MOD_SIZE_IN_BYTES. */
                                CCEcMontTempBuff_t *pEcMontTempBuff)  /*!< [in] pointer temp buffer. */
{
        CCError_t err = CC_OK;
        /* pointers to aligned buffers */
        uint32_t *pResPoint32, *pScalar32, *pInPoint32;
        size_t ecOrdSizeBytes, ecModSizeBytes;
        size_t scalarSizeWords;
        /* the pointer to EC domain (curve). */
        const CCEcMontDomain_t *pEcDomain;
        uint32_t ls, ms;


        if (pResPoint == NULL || pResPointSize == NULL ||
            pScalar == NULL || pInPoint == NULL ||
            pEcMontTempBuff == NULL) {
                return CC_EC_MONT_INVALID_INPUT_POINTER_ERROR;
        }

        /* get domain */
        pEcDomain = EcMontGetCurve25519Domain();


        /* set EC domain parameters sizes */
        scalarSizeWords = pEcDomain->ecOrdSizeInWords;
        ecModSizeBytes = pEcDomain->ecModSizeInWords * sizeof(uint32_t);
        ecOrdSizeBytes = pEcDomain->ecOrdSizeInWords * sizeof(uint32_t);

        /* check sizes */
        if (*pResPointSize < ecModSizeBytes ||
            inPointSize != ecModSizeBytes  ||
            scalarSize != ecOrdSizeBytes) {
                return CC_EC_MONT_INVALID_INPUT_SIZE_ERROR;
        }

        /* set pointers to temp buffer */
        pScalar32 = pEcMontTempBuff->ecMontScalar;
        pResPoint32 = pEcMontTempBuff->ecMontResPoint;
        pInPoint32 = pEcMontTempBuff->ecMontInPoint;

        /* convert input byte arrays to LE word arrays */
        CC_CommonConvertLsbMsbBytesToLswMswWords(pScalar32, (uint8_t*)pScalar, scalarSize);
        CC_CommonConvertLsbMsbBytesToLswMswWords(pInPoint32, (uint8_t*)pInPoint, ecModSizeBytes);

        /* revert changed bytes in the scalar */
        ls = pScalar32[0]; ms = pScalar32[scalarSizeWords-1];

        /* set scalar bits according to EC Montgomery curve25519 algorithm:
           byte[31] = (byte[31] & 127) | 64; byte[0] &= 248; */
        pScalar32[scalarSizeWords-1] = (pScalar32[scalarSizeWords-1] & 0x7FFFFFFF) | 0x40000000;
        pScalar32[0] &= 0xFFFFFFF8;

        /* call llf pScalar multiplication function */
        err = EcMontScalarmult(
                              pResPoint32,
                              pScalar32,
                              pInPoint32,
                              pEcDomain);
        if (err) {
                err = CC_ECMONT_INTERNAL_ERROR;
                goto EndWithError;
        }

        /* revert changed bytes in the scalar */
        pScalar32[0] = ls; pScalar32[scalarSizeWords-1] = ms;

        /* output pResPoint to LE bytes array */
        CC_CommonConvertLswMswWordsToLsbMsbBytes(pResPoint, pResPoint32, pEcDomain->ecModSizeInWords);
        *pResPointSize = ecModSizeBytes;

        EndWithError:
        /* zeroing temp buffers, they are not used as input/output */
        if ((uint8_t*)pScalar32 != pScalar) {
                CC_PalMemSetZero((uint8_t*)pScalar32, ecModSizeBytes);
        }
        if ((uint8_t*)pResPoint32 != pResPoint) {
                CC_PalMemSetZero((uint8_t*)pResPoint32, ecModSizeBytes);
        }
        if ((uint8_t*)pInPoint32 != pInPoint) {
                CC_PalMemSetZero((uint8_t*)pInPoint32, ecModSizeBytes);
        }

        CC_PalMemSetZero(&pEcMontTempBuff->ecMontScalrMultTempBuff, sizeof(CCEcMontScalrMultTempBuff_t));
        return err;

}

/*********************************************************************/
/*!
@brief The function performs EC Montgomery (Curve25519) scalar multiplication of base point:
       res = scalar * base_point.

       Note: all byte arrays have LE order of bytes, i.e. LS byte is on left most place.


@return CC_OK on success,
@return A non-zero value on failure as defined mbedtls_cc_ec_mont_edw_error.h.
*/

CEXPORT_C CCError_t CC_EcMontScalarmultBase(
                                uint8_t       *pResPoint,      /*!< [out] Pointer to the public (secret) key. */
                                size_t        *pResPointSize,  /*!< [in/out] Pointer to the size of the public key in bytes.
                                                                      In  - the size of buffer must be at least EC modulus size
                                                                          (for curve25519 - 32 bytes);
                                                                      Out - the actual size. */
                                const uint8_t *pScalar,        /*!< [in] Pointer to the secret (private) key. */
                                size_t         scalarSize,     /*!< [in] Pointer to the size of the scalar in bytes -
                                                                    must be equal to EC order size (for curve25519 - 32 bytes). */
                                CCEcMontTempBuff_t *pEcMontTempBuff) /*!< [in] pointer to the temp buffer. */
{
        CCError_t err = CC_OK;
        /* pointers to aligned buffers */
        uint32_t *pResPoint32, *pScalar32;
        size_t ecOrdSizeBytes, ecModSizeBytes;
        size_t scalarSizeWords;
        const CCEcMontDomain_t *pEcDomain;
        uint32_t ls, ms;

        /* check parameters */
        if (pResPoint  == NULL || pResPointSize == NULL ||
            pScalar == NULL || pEcMontTempBuff == NULL) {
                return CC_EC_MONT_INVALID_INPUT_POINTER_ERROR;
        }

        /* get domain */
        pEcDomain = EcMontGetCurve25519Domain();

        /* set EC domain parameters sizes */
        scalarSizeWords = pEcDomain->ecOrdSizeInWords;
        ecModSizeBytes = pEcDomain->ecModSizeInWords * sizeof(uint32_t);
        ecOrdSizeBytes = scalarSizeWords * sizeof(uint32_t);

        /* check sizes */
        if (*pResPointSize < ecModSizeBytes ||
            scalarSize != ecOrdSizeBytes) {
                return CC_EC_MONT_INVALID_INPUT_SIZE_ERROR;
        }

        /* set pointers to temp buffer */
        pScalar32 = pEcMontTempBuff->ecMontScalar;
        pResPoint32 = pEcMontTempBuff->ecMontResPoint;

        /* convert input byte arrays to LE word arrays */
        CC_CommonConvertLsbMsbBytesToLswMswWords(pScalar32, (uint8_t*)pScalar, scalarSize);

        /* revert changed bytes in the scalar */
        ls = pScalar32[0]; ms = pScalar32[scalarSizeWords-1];

        /* set scalar bits according to EC Montgomery curve25519 algorithm:
           byte[31] = (byte[31] & 127) | 64; byte[0] &= 248; */
        pScalar32[scalarSizeWords-1] = (pScalar32[scalarSizeWords-1] & 0x7FFFFFFF) | 0x40000000;
        pScalar32[0] &= 0xFFFFFFF8;

        /* call llf pScalar multiplication function */
        err = EcMontScalarmult(
                              pResPoint32,
                              pScalar32,
                              (uint32_t*)pEcDomain->ecGenX,
                              pEcDomain);
        if (err) {
                err = CC_ECMONT_INTERNAL_ERROR;
                goto EndWithError;
        }

        /* revert changed bytes in the scalar */
        pScalar32[0] = ls; pScalar32[scalarSizeWords-1] = ms;

        /* output pResPoint to LE bytes array */
        CC_CommonConvertLswMswWordsToLsbMsbBytes(pResPoint, pResPoint32,
                                                    pEcDomain->ecModSizeInWords);
        *pResPointSize = ecModSizeBytes;


        EndWithError:
        /* zeroing temp buffers if they are not used as input/output */
        if ((uint8_t*)pScalar32 != pScalar) {
                CC_PalMemSetZero((uint8_t*)pScalar32, ecModSizeBytes);
        }
        if ((uint8_t*)pResPoint32 != pResPoint) {
                CC_PalMemSetZero((uint8_t*)pResPoint32, ecModSizeBytes);
        }

        return err;

}
/********************************************************************/

static CCError_t ecMontKeyPairBase (
                              uint8_t *pPublKey,                    /*!< [out] Pointer to the public key. */
                              size_t  *pPublKeySize,                /*!< [in/out] Pointer to the size of the public key in bytes
                                                                    In  - the size of the buffer must be at least EC order size
                                                                   (for curve25519 - 32 bytes);
                                                                                     Out - the actual size. */
                              uint8_t *pSecrKey,                    /*!< [out] Pointer to the secret key, including. */
                              size_t  *pSecrKeySize,                /*!< [in/out] Pointer to the size of buffer for the secret key in bytes -
                                                                                    must be at least EC order size (for curve25519 - 32 bytes). */
                              const uint8_t *pInPoint,        /*!< [in] Pointer to the input point (compressed). must be of size CC_EC_MONT_MOD_SIZE_IN_BYTES */
                              CCRndContext_t *pRndContext,      /*!< [in/out] Pointer to the RND context buffer. */
                              CCEcMontTempBuff_t *pEcMontTempBuff) /*!< [in] pointer to EC domain (curve). */
{
        /* DEFINITIONS */

        CCError_t err = CC_OK;
        uint32_t ecScalarSizeBytes, ecModSizeBytes;
        CCRndGenerateVectWorkFunc_t RndGenerateVectFunc;
        /* the pointer to EC domain (curve). */
        const CCEcMontDomain_t *pEcDomain;


        /* FUNCTION LOGIC */

        /* check input parameters */
        if (pSecrKey  == NULL || pSecrKeySize == NULL ||
            pPublKey  == NULL || pPublKeySize == NULL ||
            pRndContext == NULL || pEcMontTempBuff == NULL) {
                return CC_EC_MONT_INVALID_INPUT_POINTER_ERROR;
        }

        /* get domain */
        pEcDomain = EcMontGetCurve25519Domain();

        /* EC pScalar size in bytes */
        ecScalarSizeBytes = pEcDomain->ecOrdSizeInWords * sizeof(uint32_t);
        ecModSizeBytes = pEcDomain->ecModSizeInWords * sizeof(uint32_t);

        if (*pSecrKeySize < ecScalarSizeBytes ||
            *pPublKeySize < ecScalarSizeBytes) {
                return CC_EC_MONT_INVALID_INPUT_SIZE_ERROR;
        }

        /* generaate secret key (seed) */
        RndGenerateVectFunc = pRndContext->rndGenerateVectFunc;
        if(RndGenerateVectFunc == NULL)
                return CC_RND_GEN_VECTOR_FUNC_ERROR;

        err = RndGenerateVectFunc(
                        (void *)pRndContext->rndState,
                        (unsigned char *)pSecrKey,
                        (size_t)ecScalarSizeBytes);
        if (err) {
                goto End;
        }

        *pPublKeySize = ecModSizeBytes;

        /* calculate public key by pScalar mult. */
        if (pInPoint != NULL)
        {
            err = CC_EcMontScalarmult(
                                    pPublKey,
                                    pPublKeySize,
                                    pSecrKey,
                                    ecScalarSizeBytes,
                                    pInPoint,
                                    CC_EC_MONT_MOD_SIZE_IN_BYTES,
                                    pEcMontTempBuff);
        }
        else /* use the curve base point*/
        {
            err = CC_EcMontScalarmultBase(
                                    pPublKey,
                                    pPublKeySize,
                                    pSecrKey,
                                    ecScalarSizeBytes,
                                    pEcMontTempBuff);
        }
        if (err) {
                goto End;
        }

        /* output results */
        *pSecrKeySize = ecScalarSizeBytes;

        End:

        CC_PalMemSetZero(pEcMontTempBuff, sizeof(CCEcMontTempBuff_t));

        if (err) {
                CC_PalMemSetZero(pPublKey, ecModSizeBytes);
                CC_PalMemSetZero(pSecrKey, ecScalarSizeBytes);
        }
        return err;

}


/*******************************************************************/
/*!
@brief The function randomly generates  private and public keys for Montgomery
       Curve25519, with a configurable base point.


\note All byte arrays are in LE order of bytes, i.e. LS byte is on the left most place. \par
\note LS and MS bits of the Secret key are set according to EC Montgomery scalar mult. algorithm:
                secrKey[0] &= 248; secrKey[31] &= 127; secrKey[31] |= 64;

@return CC_OK on success,
@return A non-zero value on failure as defined mbedtls_cc_ec_mont_edw_error.h.

*/
CEXPORT_C CCError_t CC_EcMontKeyPairBase(
                                         uint8_t *pPublKey,                    /*!< [out] Pointer to the public key. */
                                         size_t  *pPublKeySize,                /*!< [in/out] Pointer to the size of the public key in bytes.
                                                                                     In  - the size of the buffer must be at least EC order size
                                                                                           (for curve25519 - 32 bytes);
                                                                                     Out - the actual size. */
                                         uint8_t *pSecrKey,                    /*!< [out] Pointer to the secret key, including. */
                                         size_t  *pSecrKeySize,                /*!< [in/out] Pointer to the size of buffer for the secret key in bytes -
                                                                                    must be at least EC order size (for curve25519 - 32 bytes). */
                                         const uint8_t *pInPoint,        /*!< [in] Pointer to the input point (compressed). */
                                         size_t         inPointSize,     /*!< [in] Size of the point - must be equal to CC_EC_MONT_MOD_SIZE_IN_BYTES. */
                                         CCRndContext_t *pRndContext,      /*!< [in/out] Pointer to the RND context buffer. */
                                         CCEcMontTempBuff_t *pEcMontTempBuff) /*!< [in] pointer to EC domain (curve). */
{
    if (inPointSize != CC_EC_MONT_MOD_SIZE_IN_BYTES)
    {
        return CC_EC_MONT_INVALID_INPUT_SIZE_ERROR;
    }

    return ecMontKeyPairBase(pPublKey, pPublKeySize, pSecrKey, pSecrKeySize, pInPoint, pRndContext, pEcMontTempBuff);
}

/*******************************************************************/
/*!
@brief The function randomly generates  private and public keys for Montgomery
       Curve25519. it uses CC_EcMontKeyPair with the Generator point of the Curve


\note All byte arrays are in LE order of bytes, i.e. LS byte is on the left most place. \par
\note LS and MS bits of the Secret key are set according to EC Montgomery scalar mult. algorithm:
                secrKey[0] &= 248; secrKey[31] &= 127; secrKey[31] |= 64;

@return CC_OK on success,
@return A non-zero value on failure as defined mbedtls_cc_ec_mont_edw_error.h.

*/
CEXPORT_C CCError_t CC_EcMontKeyPair(
                                         uint8_t *pPublKey,                    /*!< [out] Pointer to the public key. */
                                         size_t  *pPublKeySize,                /*!< [in/out] Pointer to the size of the public key in bytes.
                                                                                     In  - the size of the buffer must be at least EC order size
                                                                                          (for curve25519 - 32 bytes);
                                                                                     Out - the actual size. */
                                         uint8_t *pSecrKey,                    /*!< [out] Pointer to the secret key, including. */
                                         size_t  *pSecrKeySize,                /*!< [in/out] Pointer to the size of buffer for the secret key in bytes -
                                                                                    must be at least EC order size (for curve25519 - 32 bytes). */
                                         CCRndContext_t *pRndContext,      /*!< [in/out] Pointer to the RND context buffer. */
                                         CCEcMontTempBuff_t *pEcMontTempBuff) /*!< [in] pointer to EC domain (curve). */
{

    return ecMontKeyPairBase(pPublKey, pPublKeySize, pSecrKey, pSecrKeySize, NULL, pRndContext, pEcMontTempBuff);
}

/*******************************************************************/
/**
@brief The function generates private and public keys for Montgomery Curve25519
       using given seed.

       Note: all byte arrays have LE order of bytes, i.e. LS byte is on left most place.

@return CC_OK on success,
@return a non-zero value on failure as defined mbedtls_cc_ec_mont_edw_error.h.
*/
CEXPORT_C CCError_t CC_EcMontSeedKeyPair (
                      uint8_t       *pPublKey,       /*!< [out] Pointer to the public (secret) key. */
                      size_t        *pPublKeySize,   /*!< [in/out] Pointer to the size of the public key in bytes.
                                                             In  - the size of buffer must be at least EC order size
                                                                (for curve25519 - 32 bytes);
                                                             Out - the actual size. */
                      uint8_t       *pSecrKey,       /*!< [out] Pointer to the secret (private) key. */
                      size_t        *pSecrKeySize,   /*!< [in/out] Pointer to the size of the secret key in bytes
                                                              In  - the size of buffer must be at least EC order size
                                                                (for curve25519 - 32 bytes);
                                                              Out - the actual size. */
                      const uint8_t *pSeed,          /*!< [in] Pointer to the given seed - 32 bytes. */
                      size_t         seedSize,       /*!< [in/] Size of the seed in bytes (must be equal to CC_EC_MONT_SEEDBYTES). */
                      CCEcMontTempBuff_t *pEcMontTempBuff)  /*!< [in] pointer temp buffer. */
{
        /* DEFINITIONS */

        CCError_t err = CC_OK;
        uint8_t *pScalar, *pRes;
        CCHashResultBuf_t *pHashResult; /*takes 64 bytes in pEcMontTempBuff*/
        uint32_t ecOrdSizeBytes, ecMontSizeBytes;
        /* the pointer to EC domain (curve). */
        const CCEcMontDomain_t *pEcDomain;
        const mbedtls_md_info_t *md_info=NULL;

        /* FUNCTION LOGIC */


        /* check input parameters */
        if (pSecrKey  == NULL || pSecrKeySize == NULL ||
            pPublKey == NULL || pPublKeySize == NULL ||
            pSeed == NULL || pEcMontTempBuff == NULL) {
                return CC_EC_MONT_INVALID_INPUT_POINTER_ERROR;
        }

        /* get domain */
        pEcDomain = EcMontGetCurve25519Domain();

        ecOrdSizeBytes = pEcDomain->ecOrdSizeInWords*sizeof(uint32_t);
        ecMontSizeBytes = pEcDomain->ecModSizeInWords*sizeof(uint32_t);

        if (*pSecrKeySize < ecOrdSizeBytes ||
             seedSize != ecOrdSizeBytes) {
                return CC_EC_MONT_INVALID_INPUT_SIZE_ERROR;
        }

        pHashResult = (CCHashResultBuf_t*)pEcMontTempBuff;
        pScalar = (uint8_t*)&pEcMontTempBuff->ecMontScalar;
        pRes = (uint8_t*)&pEcMontTempBuff->ecMontResPoint;

        /* copy seed into buffer with phys. memory (HW HASH requirement) */
        CC_PalMemCopy(pScalar, pSeed, seedSize);

        /* calculate secret key; note pScalar points  *
        *  to the same mem. as pHashResult            */
        md_info = mbedtls_md_info_from_string( HashAlgMode2mbedtlsString[CC_HASH_SHA512_mode] );
        if (NULL == md_info) {
             err = CC_EC_MONT_INVALID_INPUT_SIZE_ERROR;
             goto End;

        }
        err = mbedtls_md(md_info,
                         pScalar,
                         seedSize,
                        (unsigned char *)(*pHashResult));
        if (err) {
                goto End;
        }

        /* calculate the public key */
        err = CC_EcMontScalarmultBase(
                                (uint8_t*)&pEcMontTempBuff->ecMontResPoint[0],
                                pPublKeySize,
                                pScalar,  // check endianness !
                                ecOrdSizeBytes,
                                pEcMontTempBuff);

        if (err) {
                goto End;
        }

        /* set actual sizes of Secr. and publ. keys */
        *pSecrKeySize = ecOrdSizeBytes;
        *pPublKeySize = ecMontSizeBytes;

        /* output secret and publ. keys */
        CC_PalMemCopy(pSecrKey, (uint8_t*)pScalar, *pSecrKeySize);
        CC_PalMemCopy(pPublKey, pRes, *pPublKeySize);

        End:
        /* clean the temp buffers */
        CC_PalMemSetZero((uint8_t*)pEcMontTempBuff, sizeof(CCEcMontTempBuff_t));
        if (err) {
                CC_PalMemSetZero(pSecrKey, ecOrdSizeBytes);
                CC_PalMemSetZero(pPublKey, ecMontSizeBytes);
        }

        return err;
}