summaryrefslogtreecommitdiff
path: root/bsp/nuvoton/libraries/m460/StdDriver/src/nu_keystore.c
blob: 29af4e60ad70fbf0bcb2e1781be2862a3fab89de (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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/**************************************************************************//**
 * @file     keystore.c
 * @version  V3.01
 * @brief    Key store driver source file
 *
 * @copyright SPDX-License-Identifier: Apache-2.0
 * @copyright Copyright (C) 2022 Nuvoton Technology Corp. All rights reserved.
 *****************************************************************************/
#include "NuMicro.h"
/** @addtogroup Standard_Driver Standard Driver
  @{
*/

/** @addtogroup KS_Driver Key Store Driver
  @{
*/

int32_t g_KS_i32ErrCode = 0;       /*!< KS global error code */

/** @addtogroup KS_EXPORTED_FUNCTIONS Key Store Exported Functions
  @{
*/

/**
  * @brief      Initial key store
  * @retval     0           Successful
  * @retval     others      Fail
  * @details    This function is used to initial the key store.
  *             It is necessary to be called before using other APIs of Key Store.
  */
int32_t KS_Open(void)
{
    uint32_t u32TimeOutCount;
    uint32_t au32Key[8] = {0};

    CLK->AHBCLK0 |= CLK_AHBCLK0_KSCKEN_Msk;

    /* Key store initial */
    if ((KS->STS & KS_STS_INITDONE_Msk) == 0)
    {
        /* Waiting for busy */
        u32TimeOutCount = KS_TIMEOUT;
        while (KS->STS & KS_STS_BUSY_Msk)
        {
            if (--u32TimeOutCount == 0)
            {
                return KS_ERR_TIMEOUT;
            }
        }

        /* Start Key Store Initial */
        KS->CTL = KS_CTL_INIT_Msk | KS_CTL_START_Msk;

        /* Waiting for initilization */
        u32TimeOutCount = KS_TIMEOUT;
        while ((KS->STS & KS_STS_INITDONE_Msk) == 0)
        {
            if (--u32TimeOutCount == 0)
            {
                return KS_ERR_TIMEOUT;
            }
        }
    }

    /* Waiting busy to make sure KS is ready. */
    u32TimeOutCount = KS_TIMEOUT;
    while (KS->STS & KS_STS_BUSY_Msk)
    {
        if (--u32TimeOutCount == 0)
        {
            return KS_ERR_TIMEOUT;
        }
    }

    /* Create dummy key for KS Flash and KS SRAM */
    if (KS_Read(KS_FLASH, 0, au32Key, 8) < 0)
    {
        if (KS_Write(KS_FLASH, KS_META_CPU | KS_META_READABLE | KS_META_256, au32Key) != 0)
        {
            return KS_ERR_INIT;
        }
    }

    if (KS_Read(KS_SRAM, 0, au32Key, 8) < 0)
    {
        if (KS_Write(KS_SRAM, KS_META_CPU | KS_META_READABLE | KS_META_256, au32Key) != 0)
        {
            return KS_ERR_INIT;
        }
    }

    return KS_OK;
}


/**
  * @brief      Read key from key store
  * @param[in]  eType       The memory type. It could be:
                            \ref KS_SRAM
                            \ref KS_FLASH
                            \ref KS_OTP
  * @param[in]  i32KeyIdx   The key index to read
  * @param[out] au32Key     The buffer to store the key
  * @param[in]  u32WordCnt  The word (32-bit) count of the key buffer size
  * @retval     0           Successful
  * @retval     -1          Fail
  * @details    This function is used to read the key.
  */

int32_t KS_Read(KS_MEM_Type eType, int32_t i32KeyIdx, uint32_t au32Key[], uint32_t u32WordCnt)
{
    int32_t i32Cnt;
    uint32_t u32Cont;
    int32_t offset, i, cnt;
    uint32_t u32TimeOutCount;

    /* Just return when key store is in busy */
    if (KS->STS & KS_STS_BUSY_Msk)
        return KS_ERR_BUSY;

    /* Specify the key address */
    KS->METADATA = ((uint32_t)eType << KS_METADATA_DST_Pos) | KS_TOMETAKEY(i32KeyIdx);

    /* Clear error flag */
    KS->STS = KS_STS_EIF_Msk;
    offset = 0;
    u32Cont = 0;
    i32Cnt = (int32_t)u32WordCnt;
    do
    {
        /* Clear Status */
        KS->STS = KS_STS_EIF_Msk | KS_STS_IF_Msk;

        /* Trigger to read the key */
        KS->CTL = u32Cont | KS_OP_READ | KS_CTL_START_Msk;
        /* Waiting for key store processing */
        u32TimeOutCount = KS_TIMEOUT;
        while (KS->STS & KS_STS_BUSY_Msk)
        {
            if (--u32TimeOutCount == 0)
                return KS_ERR_TIMEOUT;
        }

        /* Read the key to key buffer */
        cnt = i32Cnt;
        if (cnt > 8)
            cnt = 8;
        for (i = 0; i < cnt; i++)
        {
            au32Key[offset + i] = KS->KEY[i];
        }

        u32Cont = KS_CTL_CONT_Msk;
        i32Cnt -= 8;
        offset += 8;
    }
    while (i32Cnt > 0);

    /* Check error flag */
    if (KS->STS & KS_STS_EIF_Msk)
        return KS_ERR_FAIL;


    return KS_OK;
}

/**
  * @brief      Get the word count of the specified Metadata key length
  * @param[in]  u32Meta     The metadata define of the key length. It could be
                                \ref KS_META_128
                                \ref KS_META_163
                                \ref KS_META_192
                                \ref KS_META_224
                                \ref KS_META_233
                                \ref KS_META_255
                                \ref KS_META_256
                                \ref KS_META_283
                                \ref KS_META_384
                                \ref KS_META_409
                                \ref KS_META_512
                                \ref KS_META_521
                                \ref KS_META_571
                                \ref KS_META_1024
                                \ref KS_META_2048
                                \ref KS_META_4096
  * @return     The word (32-bit) count of the key
  * @details    This function is used to get word counts of the specified metadata key length.
  *             It could be used to know how may words needs to allocate for the key.
  */

uint32_t KS_GetKeyWordCnt(uint32_t u32Meta)
{
    const uint16_t au8CntTbl[21] = { 4, 6, 6, 7, 8, 8, 8, 9, 12, 13, 16, 17, 18, 0, 0, 0, 32, 48, 64, 96, 128 };
    return au8CntTbl[((u32Meta & KS_METADATA_SIZE_Msk) >> KS_METADATA_SIZE_Pos)];
}

/**
  * @brief      Write key to key store
* @param[in]    eType       The memory type. It could be:
                            \ref KS_SRAM
                            \ref KS_FLASH
  * @param[in]  u32Meta     The metadata of the key. It could be the combine of
                                \ref KS_META_AES
                                \ref KS_META_HMAC
                                \ref KS_META_RSA_EXP
                                \ref KS_META_RSA_MID
                                \ref KS_META_ECC
                                \ref KS_META_CPU
                                \ref KS_META_128
                                \ref KS_META_163
                                \ref KS_META_192
                                \ref KS_META_224
                                \ref KS_META_233
                                \ref KS_META_255
                                \ref KS_META_256
                                \ref KS_META_283
                                \ref KS_META_384
                                \ref KS_META_409
                                \ref KS_META_512
                                \ref KS_META_521
                                \ref KS_META_571
                                \ref KS_META_1024
                                \ref KS_META_2048
                                \ref KS_META_4096
                                \ref KS_META_BOOT
                                \ref KS_META_READABLE
                                \ref KS_META_PRIV
                                \ref KS_META_NONPRIV
                                \ref KS_META_SECURE
                                \ref KS_META_NONSECUR

  * @param[out] au32Key     The buffer to store the key
  * @param[in]  u32WordCnt  The word (32-bit) count of the key buffer size
  * @return     Index of the key. Failed when index < 0.
  * @details    This function is used to write a key to key store.
  */

int32_t KS_Write(KS_MEM_Type eType, uint32_t u32Meta, uint32_t au32Key[])
{
    int32_t i32Cnt;
    uint32_t u32Cont;
    int32_t i, cnt;
    volatile int32_t offset;
    uint32_t u32TimeOutCount;

    /* Just return when key store is in busy */
    if (KS->STS & KS_STS_BUSY_Msk)
        return KS_ERR_BUSY;

    /* Specify the key address */
    KS->METADATA = (eType << KS_METADATA_DST_Pos) | u32Meta;

    /* Get size index */
    i32Cnt = (int32_t)KS_GetKeyWordCnt(u32Meta);

    /* Invalid key length */
    if (i32Cnt == 0)
        return KS_ERR_PARAMETER;

    /* OTP only support maximum 256 bits */
    if ((eType == KS_OTP) && (i32Cnt > 8))
        return KS_ERR_PARAMETER;

    /* Check size limit of KS FLASH */
    if (eType == KS_FLASH)
    {
        if ((int32_t)KS_GetRemainSize(KS_FLASH) - i32Cnt * 4 < 4)
            return KS_ERR_FAIL;
    }

    /* Check key count limit of KS SRAM */
    if (eType == KS_SRAM)
    {
        if (KS_GetRemainKeyCount(KS_SRAM) == 1)
            return KS_ERR_FAIL;
    }

    /* Clear error flag */
    KS->STS = KS_STS_EIF_Msk;
    offset = 0;
    u32Cont = 0;
    do
    {
        /* Prepare the key to write */
        cnt = i32Cnt;
        if (cnt > 8)
            cnt = 8;
        for (i = 0; i < cnt; i++)
        {
            KS->KEY[i] = au32Key[offset + i];
        }

        /* Clear Status */
        KS->STS = KS_STS_EIF_Msk | KS_STS_IF_Msk;

        /* Write the key */
        KS->CTL = u32Cont | KS_OP_WRITE | KS_CTL_START_Msk;

        u32Cont = KS_CTL_CONT_Msk;
        i32Cnt -= 8;
        offset += 8;

        /* Waiting for key store processing */
        u32TimeOutCount = KS_TIMEOUT;
        while (KS->STS & KS_STS_BUSY_Msk)
        {
            if (--u32TimeOutCount == 0)
                return KS_ERR_TIMEOUT;
        }

    }
    while (i32Cnt > 0);

    /* Check error flag */
    if (KS->STS & KS_STS_EIF_Msk)
    {
        return KS_ERR_FAIL;
    }

    return KS_TOKEYIDX(KS->METADATA);
}

/**
  * @brief      Erase a key from key store SRAM
  * @param[in]  i32KeyIdx   The key index to read
  * @retval     0           Successful
  * @retval     -1          Fail
  * @details    This function is used to erase a key from SRAM of key store.
   */
int32_t KS_EraseKey(int32_t i32KeyIdx)
{
    uint32_t u32TimeOutCount = KS_TIMEOUT;

    /* Just return when key store is in busy */
    if (KS->STS & KS_STS_BUSY_Msk)
        return KS_ERR_BUSY;

    /* Clear error flag */
    KS->STS = KS_STS_EIF_Msk;

    /* Specify the key address */
    KS->METADATA = (KS_SRAM << KS_METADATA_DST_Pos) | KS_TOMETAKEY(i32KeyIdx);

    /* Clear Status */
    KS->STS = KS_STS_EIF_Msk | KS_STS_IF_Msk;

    /* Erase the key */
    KS->CTL = KS_OP_ERASE | KS_CTL_START_Msk;

    /* Waiting for processing */
    while (KS->STS & KS_STS_BUSY_Msk)
    {
        if (--u32TimeOutCount == 0)
            return KS_ERR_TIMEOUT;
    }

    /* Check error flag */
    if (KS->STS & KS_STS_EIF_Msk)
        return KS_ERR_FAIL;

    return KS_OK;

}


/**
  * @brief      Erase a key from key store OTP
  * @param[in]  i32KeyIdx   The key index to erase
  * @retval     0           Successful
  * @retval     -1          Fail
  * @details    This function is used to erase a key from key store OTP.
   */
int32_t KS_EraseOTPKey(int32_t i32KeyIdx)
{
    uint32_t u32TimeOutCount = KS_TIMEOUT; /* 1 second time-out */

    /* Just return when key store is in busy */
    if (KS->STS & KS_STS_BUSY_Msk)
        return KS_ERR_BUSY;

    /* Clear error flag */
    KS->STS = KS_STS_EIF_Msk;

    /* Specify the key address */
    KS->METADATA = ((uint32_t)KS_OTP << KS_METADATA_DST_Pos) | KS_TOMETAKEY(i32KeyIdx);

    /* Clear Status */
    KS->STS = KS_STS_EIF_Msk | KS_STS_IF_Msk;

    /* Erase the key */
    KS->CTL = KS_OP_ERASE | KS_CTL_START_Msk;

    /* Waiting for processing */
    while (KS->STS & KS_STS_BUSY_Msk)
    {
        if (--u32TimeOutCount == 0)
            return KS_ERR_TIMEOUT;
    }

    /* Check error flag */
    if (KS->STS & KS_STS_EIF_Msk)
        return KS_ERR_FAIL;

    return KS_OK;

}



/**
  * @brief      Lock the OTP key
  * @param[in]  i32KeyIdx   The key index to lock
  * @retval     0           Successful
  * @retval     -1          Fail
  * @details    This function is used to lock a key of KS OTP.
   */
int32_t KS_LockOTPKey(int32_t i32KeyIdx)
{
    uint32_t u32TimeOutCount = KS_TIMEOUT;

    /* Just return when key store is in busy */
    if (KS->STS & KS_STS_BUSY_Msk)
        return KS_ERR_BUSY;

    /* Clear error flag */
    KS->STS = KS_STS_EIF_Msk;

    /* Specify the key address */
    KS->METADATA = ((uint32_t)KS_OTP << KS_METADATA_DST_Pos) | KS_TOMETAKEY(i32KeyIdx);

    /* Clear Status */
    KS->STS = KS_STS_EIF_Msk | KS_STS_IF_Msk;

    /* Erase the key */
    KS->CTL = KS_OP_LOCK | KS_CTL_START_Msk;

    /* Waiting for processing */
    while (KS->STS & KS_STS_BUSY_Msk)
    {
        if (--u32TimeOutCount == 0)
            return KS_ERR_TIMEOUT;
    }

    /* Check error flag */
    if (KS->STS & KS_STS_EIF_Msk)
        return KS_ERR_FAIL;

    return KS_OK;

}

/**
  * @brief      Erase all keys from key store
  * @param[in]  eType       The memory type. It could be:
                            \ref KS_SRAM
                            \ref KS_FLASH
                            \ref KS_OTP
  * @param[in]  i32KeyIdx   The key index to read
  * @retval     0           Successful
  * @retval     -1          Fail
  * @details    This function is used to erase all keys in SRAM or Flash of key store.
  */
int32_t KS_EraseAll(KS_MEM_Type eType)
{
    uint32_t au32Key[8] = { 0 };
    uint32_t u32TimeOutCount = KS_TIMEOUT;

    /* Just return when key store is in busy */
    if (KS->STS & KS_STS_BUSY_Msk)
        return KS_ERR_BUSY;

    /* Clear error flag */
    KS->STS = KS_STS_EIF_Msk;

    /* Specify the key address */
    KS->METADATA = (eType << KS_METADATA_DST_Pos);

    /* Clear Status */
    KS->STS = KS_STS_EIF_Msk | KS_STS_IF_Msk;

    /* Erase the key */
    KS->CTL = KS_OP_ERASE_ALL | KS_CTL_START_Msk;

    /* Waiting for processing */
    while (KS->STS & KS_STS_BUSY_Msk)
    {
        if (--u32TimeOutCount == 0)
            return KS_ERR_TIMEOUT;
    }

    /* Check error flag */
    if (KS->STS & KS_STS_EIF_Msk)
        return KS_ERR_FAIL;

    /* Create dummy key for KS Flash and KS SRAM */
    if (KS_Read(KS_FLASH, 0, au32Key, 8) < 0)
    {
        if (KS_Write(KS_FLASH, KS_META_CPU | KS_META_READABLE | KS_META_256, au32Key) != 0)
        {
            return KS_ERR_FAIL;
        }
    }

    if (KS_Read(KS_SRAM, 0, au32Key, 8) < 0)
    {
        if (KS_Write(KS_SRAM, KS_META_CPU | KS_META_READABLE | KS_META_256, au32Key) != 0)
        {
            return KS_ERR_FAIL;
        }
    }

    return KS_OK;

}



/**
  * @brief      Revoke a key in key store
  * @param[in]  eType       The memory type. It could be:
                            \ref KS_SRAM
                            \ref KS_FLASH
                            \ref KS_OTP
  * @param[in]  i32KeyIdx   The key index to read
  * @retval     0           Successful
  * @retval     -1          Fail
  * @details    This function is used to revoke a key in key store.
  */
int32_t KS_RevokeKey(KS_MEM_Type eType, int32_t i32KeyIdx)
{
    uint32_t u32TimeOutCount = KS_TIMEOUT;

    /* Just return when key store is in busy */
    if (KS->STS & KS_STS_BUSY_Msk)
        return KS_ERR_BUSY;

    /* Clear error flag */
    KS->STS = KS_STS_EIF_Msk;

    /* Specify the key address */
    KS->METADATA = (eType << KS_METADATA_DST_Pos) | KS_TOMETAKEY(i32KeyIdx);

    /* Clear Status */
    KS->STS = KS_STS_EIF_Msk | KS_STS_IF_Msk;

    /* Erase the key */
    KS->CTL = KS_OP_REVOKE | KS_CTL_START_Msk;

    /* Waiting for processing */
    while (KS->STS & KS_STS_BUSY_Msk)
    {
        if (--u32TimeOutCount == 0)
            return KS_ERR_TIMEOUT;
    }

    /* Check error flag */
    if (KS->STS & KS_STS_EIF_Msk)
        return KS_ERR_FAIL;

    return KS_OK;

}


/**
  * @brief      Get remain size of specified Key Store memory
  * @param[in]  eType       The memory type. It could be:
                            \ref KS_SRAM
                            \ref KS_FLASH
  * @retval     remain size of specified Key Store memory
  * @details    This function is used to get remain size of Key Store.
  */
uint32_t KS_GetRemainSize(KS_MEM_Type mem)
{
    uint32_t u32Reg;
    uint32_t u32SramRemain, u32FlashRemain;

    u32Reg = KS->REMAIN;
    //printf("KS Remain 0x%08x\n", u32Reg);
    //printf("SRAM remain %lu bytes, Flash remain %lu bytes\n",(u32Reg&KS_REMAIN_RRMNG_Msk) >> KS_REMAIN_RRMNG_Pos, (u32Reg&KS_REMAIN_FRMNG_Msk) >> KS_REMAIN_FRMNG_Pos);
    u32SramRemain = (u32Reg & KS_REMAIN_RRMNG_Msk) >> KS_REMAIN_RRMNG_Pos;
    u32FlashRemain = (u32Reg & KS_REMAIN_FRMNG_Msk) >> KS_REMAIN_FRMNG_Pos;

    if (mem == KS_SRAM)
        return u32SramRemain;
    else
        return u32FlashRemain;
}



/**
  * @brief      Get remain key count of specified Key Store memory
  * @param[in]  eType       The memory type. It could be:
                            \ref KS_SRAM
                            \ref KS_FLASH
  * @retval     Remain key count in the specified key store memory
  * @details    This function is used to get remain key count in specified key store memory.
  */
uint32_t KS_GetRemainKeyCount(KS_MEM_Type mem)
{
    uint32_t u32Reg;
    uint32_t u32SramRemain, u32FlashRemain;

    u32Reg = KS->REMKCNT;
    u32SramRemain = (u32Reg & KS_REMKCNT_RRMKCNT_Msk) >> KS_REMKCNT_RRMKCNT_Pos;
    u32FlashRemain = (u32Reg & KS_REMKCNT_FRMKCNT_Msk) >> KS_REMKCNT_FRMKCNT_Pos;

    if (mem == KS_SRAM)
        return u32SramRemain;
    else
        return u32FlashRemain;
}



/**
  * @brief      Write OTP key to key store
  * @param[in]  i32KeyIdx   The OTP key index to store the key. It could be 0~7.
                            OTP key index 0 is default for ROTPK.
  * @param[in]  u32Meta     The metadata of the key. It could be the combine of
                                \ref KS_META_AES
                                \ref KS_META_HMAC
                                \ref KS_META_RSA_EXP
                                \ref KS_META_RSA_MID
                                \ref KS_META_ECC
                                \ref KS_META_CPU
                                \ref KS_META_128
                                \ref KS_META_163
                                \ref KS_META_192
                                \ref KS_META_224
                                \ref KS_META_233
                                \ref KS_META_255
                                \ref KS_META_256
                                \ref KS_META_BOOT
                                \ref KS_META_READABLE
                                \ref KS_META_PRIV
                                \ref KS_META_NONPRIV
                                \ref KS_META_SECURE
                                \ref KS_META_NONSECUR

  * @param[out] au32Key     The buffer to store the key
  * @param[in]  u32WordCnt  The word (32-bit) count of the key buffer size
  * @retval     0           Successful
  * @retval     -1          Fail
  * @details    This function is used to write a key to OTP key store.
  */
int32_t KS_WriteOTP(int32_t i32KeyIdx, uint32_t u32Meta, uint32_t au32Key[])
{
    const uint16_t au8CntTbl[7] = {4, 6, 6, 7, 8, 8, 8};
    int32_t i32Cnt;
    uint32_t u32Cont;
    int32_t offset, i, cnt, sidx;
    uint32_t u32TimeOutCount;

    /* Just return when key store is in busy */
    if (KS->STS & KS_STS_BUSY_Msk)
        return KS_ERR_BUSY;

    /* Specify the key address */
    KS->METADATA = ((uint32_t)KS_OTP << KS_METADATA_DST_Pos) | u32Meta | KS_TOMETAKEY(i32KeyIdx);

    /* Get size index */
    sidx = (u32Meta >> KS_METADATA_SIZE_Pos) & 0xful;

    /* OTP only support maximum 256 bits */
    if (sidx >= 7)
        return KS_ERR_PARAMETER;

    i32Cnt = au8CntTbl[sidx];

    /* Clear error flag */
    KS->STS = KS_STS_EIF_Msk;
    offset = 0;
    u32Cont = 0;
    do
    {
        /* Prepare the key to write */
        cnt = i32Cnt;
        if (cnt > 8)
            cnt = 8;
        for (i = 0; i < cnt; i++)
        {
            KS->KEY[i] = au32Key[offset + i];
        }

        /* Clear Status */
        KS->STS = KS_STS_EIF_Msk | KS_STS_IF_Msk;

        /* Write the key */
        KS->CTL = u32Cont | KS_OP_WRITE | KS_CTL_START_Msk;

        u32Cont = KS_CTL_CONT_Msk;
        i32Cnt -= 8;
        offset += 8;

        /* Waiting for key store processing */
        u32TimeOutCount = KS_TIMEOUT;
        while (KS->STS & KS_STS_BUSY_Msk)
        {
            if (--u32TimeOutCount == 0)
                return KS_ERR_TIMEOUT;
        }

    }
    while (i32Cnt > 0);

    /* Check error flag */
    if (KS->STS & KS_STS_EIF_Msk)
    {
        return KS_ERR_FAIL;
    }

    return i32KeyIdx;
}


/**
  * @brief      Trigger to inverse the date in KS_SRAM.
  * @retval     1           The data in KS SRAM is inverted.
  * @retval     0           The data in KS SRAM is non-inverted.
  * @retval     -1          Fail to invert the date in KS SRAM.
  * @details    This function is used to trigger anti-remanence procedure by inverse the data in SRAM.
  *             This won't change the reading key.
  */

int32_t KS_ToggleSRAM(void)
{
    uint32_t u32TimeOutCount = KS_TIMEOUT;

    /* Just return when key store is in busy */
    if (KS->STS & KS_STS_BUSY_Msk)
        return KS_ERR_BUSY;


    /* Specify the key address */
    KS->METADATA = ((uint32_t)KS_SRAM << KS_METADATA_DST_Pos);

    /* Clear error flag */
    KS->STS = KS_STS_EIF_Msk | KS_STS_IF_Msk;
    /* Trigger to do anti-remanence procedure */
    KS->CTL = KS_OP_REMAN | KS_CTL_START_Msk;

    /* Waiting for key store processing */
    while (KS->STS & KS_STS_BUSY_Msk)
    {
        if (--u32TimeOutCount == 0)
            return KS_ERR_TIMEOUT;
    }

    /* Check error flag */
    if (KS->STS & KS_STS_EIF_Msk)
        return KS_ERR_FAIL;

    return ((KS->STS & KS_STS_RAMINV_Msk) > 0);
}


/**@}*/ /* end of group KS_EXPORTED_FUNCTIONS */

/**@}*/ /* end of group KS_Driver */

/**@}*/ /* end of group Standard_Driver */