1 /*****************************************************************************
2 *         NationS Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2020, NationS Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * NationS's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ****************************************************************************/
28 /*****************************************************************************
29 * File Name: n32l40x_aes.h
30 * Function: Declaring AES algorithm library API
31 * version: V1.2.0
32 * Author: zhang.zhenshan
33 * date: 2020-4-8
34 * ****************************************************************************/
35 
36 #ifndef __N32L40X_AES_H__
37 #define __N32L40X_AES_H__
38 
39 #include <stdint.h>
40 
41 #define AES_ECB (0x11111111)
42 #define AES_CBC (0x22222222)
43 #define AES_CTR (0x33333333)
44 
45 #define AES_ENC  (0x44444444)
46 #define AES_DEC  (0x55555555)
47 
48 enum
49 {
50     AES_Crypto_OK = 0x0,   //AES opreation success
51     AES_Init_OK = 0x0,   //AES Init opreation success
52     AES_Crypto_ModeError = 0x5a5a5a5a,   //Working mode error(Neither ECB nor CBC nor CTR)
53     AES_Crypto_EnOrDeError,    //En&De error(Neither encryption nor decryption)
54     AES_Crypto_ParaNull,    // the part of input(output/iv) Null
55     AES_Crypto_LengthError,     // if Working mode is ECB or CBC,the length of input message must be 4 times and cannot be zero;
56                                 //if Working mode is CTR,the length of input message cannot be zero; othets: return AES_Crypto_LengthError
57 
58     AES_Crypto_KeyLengthError, //the keyWordLen must be 4 or 6 or 8; othets:return AES_Crypto_KeyLengthError
59   AES_Crypto_UnInitError, //AES uninitialized
60 };
61 
62 typedef struct
63 {
64     uint32_t *in;       // the part of input to be encrypted or decrypted
65     uint32_t *iv;       // the part of initial vector
66     uint32_t *out;      // the part of out
67     uint32_t *key;      // the part of key
68     uint32_t keyWordLen;   // the length(by word) of key
69     uint32_t inWordLen; // the length(by word) of plaintext or cipher
70     uint32_t En_De; // 0x44444444- encrypt, 0x55555555 - decrypt
71     uint32_t Mode;  // 0x11111111 - ECB, 0x22222222 - CBC, 0x33333333 - CTR
72 }AES_PARM;
73 
74  /**
75  * @brief AES_Init
76  * @return AES_Init_OK, AES Init success; othets: AES Init fail
77  * @note
78  */
79 
80 uint32_t  AES_Init(AES_PARM *parm);
81 
82 /**
83  * @brief AES crypto
84  * @param[in] parm pointer to AES context and the detail please refer to struct AES_PARM in AES.h
85  * @return AES_Crypto_OK, AES crypto success; othets: AES crypto fail(reference to the definition by enum variation)
86  * @note  1.Please refer to the demo in user guidance before using this function
87  *        2.Input and output can be the same buffer
88  *        3. IV can be NULL when ECB mode
89  *        4. If Working mode is ECB or CBC,the length of input message must be 4 times and cannot be zero;
90  *           if Working mode is CTR,the length of input message cannot be zero;
91  *        5. If the input is in byte, make sure align by word.
92  */
93 uint32_t AES_Crypto(AES_PARM *parm);
94 
95 /**
96  * @brief AES close
97  * @return none
98  * @note if you want to close AES algorithm, this function can be recalled.
99  */
100 void AES_Close(void);
101 
102 /**
103  * @brief Get AES lib version
104  * @param[out] type pointer one byte type information represents the type of the lib, like Commercial version.\
105  * @Bits 0~4 stands for Commercial (C), Security (S), Normal (N), Evaluation (E), Test (T), Bits 5~7 are reserved. e.g. 0x09 stands for CE version.
106  * @param[out] customer pointer one byte customer information represents customer ID. for example, 0x00 stands for standard version, 0x01 is for Tianyu customized version...
107  * @param[out] date pointer array which include three bytes date information. If the returned bytes are 18,9,13,this denotes September 13,2018
108  * @param[out] version pointer one byte version information represents develop version of the lib. e.g. 0x12 denotes version 1.2.
109  * @return none
110  * @1.You can recall this function to get AES lib information
111  */
112 void AES_Version(uint8_t *type, uint8_t *customer, uint8_t date[3], uint8_t *version);
113 
114 
115 
116 
117 #endif
118 
119 
120