]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D4x_EK_IAR/AtmelFiles/libchip_sama5d4x/source/aesb.c
Core kernel files:
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D4x_EK_IAR / AtmelFiles / libchip_sama5d4x / source / aesb.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2013, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\r
8  * Redistribution and use in source and binary forms, with or without\r
9  * modification, are permitted provided that the following conditions are met:\r
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\r
14  * Atmel's name may not be used to endorse or promote products derived from\r
15  * this software without specific prior written permission.\r
16  *\r
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  * ----------------------------------------------------------------------------\r
28  */\r
29 \r
30 /** \addtogroup aesb_module Working with AESB\r
31  * The TWI driver provides the interface to True Random Number Generator (AESB) passes the American NIST Special Publication 800-22 and Diehard\r
32 Random Tests Suites.\r
33 The AESB may be used as an entropy source for seeding an NIST approved DRNG (Deterministic RNG) as required by\r
34 FIPS PUB 140-2 and 140-3. use the TWI\r
35  * peripheral.\r
36  *\r
37  * \section Usage\r
38  * <ul>\r
39  * <li> Configures a TWI peripheral to operate in master mode, at the given\r
40  * frequency (in Hz) using TWI_Configure(). </li>\r
41  * <li> Sends a STOP condition on the TWI using TWI_Stop().</li>\r
42  * <li> Starts a read operation on the TWI bus with the specified slave using\r
43  * TWI_StartRead(). Data must then be read using TWI_ReadByte() whenever\r
44  * a byte is available (poll using TWI_ByteReceived()).</li>\r
45  * <li> Starts a write operation on the TWI to access the selected slave using\r
46  * TWI_StartWrite(). A byte of data must be provided to start the write;\r
47  * other bytes are written next.</li>\r
48  * <li> Sends a byte of data to one of the TWI slaves on the bus using TWI_WriteByte().\r
49  * This function must be called once before TWI_StartWrite() with the first byte of data\r
50  * to send, then it shall be called repeatedly after that to send the remaining bytes.</li>\r
51  * <li> Check if a byte has been received and can be read on the given TWI\r
52  * peripheral using TWI_ByteReceived().<\r
53  * Check if a byte has been sent using TWI_ByteSent().</li>\r
54  * <li> Check if the current transmission is complete (the STOP has been sent)\r
55  * using TWI_TransferComplete().</li>\r
56  * <li> Enables & disable the selected interrupts sources on a TWI peripheral\r
57  * using TWI_EnableIt() and TWI_DisableIt().</li>\r
58  * <li> Get current status register of the given TWI peripheral using\r
59  * TWI_GetStatus(). Get current status register of the given TWI peripheral, but\r
60  * masking interrupt sources which are not currently enabled using\r
61  * TWI_GetMaskedStatus().</li>\r
62  * </ul>\r
63  * For more accurate information, please look at the TWI section of the\r
64  * Datasheet.\r
65  *\r
66  * Related files :\n\r
67  * \ref twi.c\n\r
68  * \ref twi.h.\n\r
69 */\r
70 /*@{*/\r
71 /*@}*/\r
72 \r
73 /**\r
74  * \file\r
75  *\r
76  * Implementation of True Random Number Generator (AESB)\r
77  *\r
78  */\r
79 \r
80 /*----------------------------------------------------------------------------\r
81  *        Headers\r
82  *----------------------------------------------------------------------------*/\r
83 \r
84 #include "chip.h"\r
85 \r
86 /*----------------------------------------------------------------------------\r
87  *        Exported functions\r
88  *----------------------------------------------------------------------------*/\r
89 \r
90 /**\r
91  * \brief Starts Manual encryption/decryption process.\r
92  */\r
93 void AESB_Start(void)\r
94 {\r
95     AESB->AESB_CR = AESB_CR_START;\r
96 }\r
97 \r
98 /**\r
99  * \brief Resets the AESB. A software triggered hardware reset of the AESB interface is performed.\r
100  */\r
101 void AESB_SoftReset(void)\r
102 {\r
103     AESB->AESB_CR = AESB_CR_SWRST;\r
104 }\r
105 \r
106 /**\r
107  * \brief Restarts the countermeasures generator to an internal pre-defined value.\r
108  */\r
109 void AESB_Recount(void)\r
110 {\r
111     AESB->AESB_CR = AESB_CR_LOADSEED;\r
112 }\r
113 \r
114 /**\r
115  * \brief Configures an AESB peripheral with the specified parameters.\r
116  *  \param mode  Desired value for the AESB mode register (see the datasheet).\r
117  */\r
118 void AESB_Configure(uint32_t mode)\r
119 {\r
120     AESB->AESB_MR = mode; \r
121 }\r
122 \r
123 /**\r
124  * \brief Enables the selected interrupts sources on a AESB peripheral.\r
125  * \param sources  Bitwise OR of selected interrupt sources.\r
126  */\r
127 void AESB_EnableIt(uint32_t sources)\r
128 {\r
129     AESB->AESB_IER = sources;\r
130 }\r
131 \r
132 /**\r
133  * \brief Disables the selected interrupts sources on a AESB peripheral.\r
134  * \param sources  Bitwise OR of selected interrupt sources.\r
135  */\r
136 void AESB_DisableIt(uint32_t sources)\r
137 {\r
138     AESB->AESB_IDR = sources;\r
139 }\r
140 \r
141 /**\r
142  * \brief Get the current status register of the given AESB peripheral.\r
143  * \return  AESB status register.\r
144  */\r
145 uint32_t AESB_GetStatus(void)\r
146 {\r
147     return AESB->AESB_ISR;\r
148 }\r
149 \r
150 /**\r
151  * \brief Set the 128-bit cryptographic key used for encryption/decryption.\r
152  * \param pKey Pointer to a 16 bytes cipher key.\r
153  * \param keyLength length of key\r
154  */\r
155 void AESB_WriteKey(const uint32_t *pKey)\r
156 {\r
157     AESB->AESB_KEYWR[0] = pKey[0];\r
158     AESB->AESB_KEYWR[1] = pKey[1];\r
159     AESB->AESB_KEYWR[2] = pKey[2];\r
160     AESB->AESB_KEYWR[3] = pKey[3];\r
161 }\r
162 \r
163 /**\r
164  * \brief Set the for 32-bit input Data allow to set the 128-bit data block used for encryption/decryption.\r
165  * \param data Pointer to the 16-bytes data to cipher/decipher.\r
166  */\r
167 void AESB_SetInput(uint32_t *data)\r
168 {\r
169     uint8_t i;\r
170     for (i = 0; i < 4; i++)\r
171         AESB->AESB_IDATAR[i] = data[i];\r
172 }\r
173 \r
174 /**\r
175  * \brief Get the four 32-bit data contain the 128-bit data block which has been encrypted/decrypted.\r
176  * \param data pointer to the word that has been encrypted/decrypted..\r
177  */\r
178 void AESB_GetOutput(uint32_t *data)\r
179 {\r
180     uint8_t i;\r
181     for (i = 0; i < 4; i++) \r
182         data[i] = AESB->AESB_ODATAR[i];\r
183 }\r
184 \r
185 /**\r
186  * \brief Set four 64-bit initialization vector data block, which is used by some\r
187  * modes of operation as an additional initial input.\r
188  * \param pVector point to the word of the initialization vector.\r
189  */\r
190 void AESB_SetVector(const uint32_t *pVector)\r
191 {\r
192     AESB->AESB_IVR[0] = pVector[0];\r
193     AESB->AESB_IVR[1] = pVector[1];\r
194     AESB->AESB_IVR[2] = pVector[2];\r
195     AESB->AESB_IVR[3] = pVector[3];\r
196 }\r
197 \r