]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D4x_EK_IAR/AtmelFiles/libchip_sama5d4x/source/tdes.c
Core kernel files:
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D4x_EK_IAR / AtmelFiles / libchip_sama5d4x / source / tdes.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 tdes_module Working with TDES\r
31  * \ingroup peripherals_module\r
32  * The ADC driver provides the interface to configure and use the TDES peripheral.\r
33  * \n\r
34  *\r
35  * The Data Encryption Standard (DES) and the Triple Data Encryption Algorithm (TDES) specify\r
36  * FIPS-approved cryptographic algorithms that can be used to protect electronic data. The TDES\r
37  * bit in the TDES Mode Register (TDES_MR) is used to select either the single DES or the Triple\r
38  * DES mode by function TDES_Configure().\r
39  * A DES is capable of using cryptographic keys of 64 bits to encrypt and decrypt data in blocks of\r
40  * 64 bits, Date input can be set with TDES_SetInput(). This 64-bit key is defined in the Key 1 Word \r
41  * Registers (TDES_KEY1WRx) and set by TDES_WriteKey1. A TDES key consists of three DES keys, \r
42  * which is also referred to as a key bundle. These three 64-bit keys are defined, respectively, \r
43  * in the Key 1, 2 and 3 Word Registers (TDES_KEY1WRx, TDES_KEY2WRx and TDES_KEY3WRx). \r
44  * In Triple DES mode (TDESMOD set to 1), the KEYMOD bit in the TDES Mode Register is used to choose between a two- and a three-key\r
45  * algorithm:\r
46  *\r
47  * To Enable a TDES encryption and decrypt,the user has to follow these few steps:\r
48  * <ul>\r
49  * <li> A software triggered hardware reset of the TDES interface is performed by TDES_SoftReset().</li>\r
50  * <li> Configure TDES algorithm mode, key mode, start mode and operation mode by TDES_Configure(). </li>\r
51  * <li> Set DES key with fucntion TDES_WriteKey1(), TDES_WriteKey2(), or TDES_WriteKey3(). </li>\r
52  * <li> To start the encryption or the decryption process with TDES_Start()</li>\r
53  * <li> To get the encryption or decryption reslut by TDES_GetOutput() </li>\r
54  * </ul>\r
55  *\r
56  * For more accurate information, please look at the TDES section of the\r
57  * Datasheet.\r
58  *\r
59  * Related files :\n\r
60  * \ref tdes.c\n\r
61  * \ref tdes.h\n\r
62  */\r
63 /*@{*/\r
64 /*@}*/\r
65 /**\r
66  * \file\r
67  *\r
68  * Implementation of Triple Data Encryption Standard (TDES).\r
69  *\r
70  */\r
71 \r
72 \r
73 /*----------------------------------------------------------------------------\r
74  *        Headers\r
75  *----------------------------------------------------------------------------*/\r
76 \r
77 #include "chip.h"\r
78 \r
79 /*----------------------------------------------------------------------------\r
80  *        Exported functions\r
81  *----------------------------------------------------------------------------*/\r
82 \r
83 /**\r
84  * \brief Starts Manual encryption/decryption process.\r
85  */\r
86 void TDES_Start(void)\r
87 {\r
88     TDES->TDES_CR = TDES_CR_START;\r
89 }\r
90 \r
91 /**\r
92  * \brief Resets the TDES. A software triggered hardware reset of the TDES interface is performed.\r
93  */\r
94 void TDES_SoftReset(void)\r
95 {\r
96     TDES->TDES_CR = TDES_CR_SWRST;\r
97 }\r
98 \r
99 /**\r
100  * \brief Restarts the countermeasures generator to an internal pre-defined value.\r
101  */\r
102 void TDES_Recount(void)\r
103 {\r
104     TDES->TDES_CR = TDES_CR_LOADSEED;\r
105 }\r
106 \r
107 /**\r
108  * \brief Configures an TDES peripheral with the specified parameters.\r
109  *  \param mode  Desired value for the TDES mode register (see the datasheet).\r
110  */\r
111 void TDES_Configure(uint32_t mode)\r
112 {\r
113     TDES->TDES_MR = mode; \r
114 }\r
115 \r
116 /**\r
117  * \brief Enables the selected interrupts sources on a TDES peripheral.\r
118  * \param sources  Bitwise OR of selected interrupt sources.\r
119  */\r
120 void TDES_EnableIt(uint32_t sources)\r
121 {\r
122     TDES->TDES_IER = sources;\r
123 }\r
124 \r
125 /**\r
126  * \brief Disables the selected interrupts sources on a TDES peripheral.\r
127  * \param sources  Bitwise OR of selected interrupt sources.\r
128  */\r
129 void TDES_DisableIt(uint32_t sources)\r
130 {\r
131     TDES->TDES_IDR = sources;\r
132 }\r
133 \r
134 /**\r
135  * \brief Get the current status register of the given TDES peripheral.\r
136  * \return  TDES status register.\r
137  */\r
138 uint32_t TDES_GetStatus(void)\r
139 {\r
140     return TDES->TDES_ISR;\r
141 }\r
142 \r
143 /**\r
144  * \brief Set KEY1.\r
145  * \param keyword0 key word 0\r
146  * \param keyword0 key word 1\r
147  */\r
148 void TDES_WriteKey1(uint32_t keyword0, uint32_t keyword1)\r
149 {\r
150     TDES->TDES_KEY1WR[0] = keyword0;\r
151     TDES->TDES_KEY1WR[1] = keyword1;\r
152 }\r
153 \r
154 /**\r
155  * \brief Set KEY2.\r
156  * \param keyword0 key word 0\r
157  * \param keyword0 key word 1\r
158  */\r
159 void TDES_WriteKey2(uint32_t keyword0, uint32_t keyword1)\r
160 {\r
161     TDES->TDES_KEY2WR[0] = keyword0;\r
162     TDES->TDES_KEY2WR[1] = keyword1;\r
163 }\r
164 \r
165 /**\r
166  * \brief Set KEY3.\r
167  * \param keyword0 key word 0\r
168  * \param keyword0 key word 1\r
169  */\r
170 void TDES_WriteKey3(uint32_t keyword0, uint32_t keyword1)\r
171 {\r
172     TDES->TDES_KEY3WR[0] = keyword0;\r
173     TDES->TDES_KEY3WR[1] = keyword1;\r
174 }\r
175 \r
176 /**\r
177  * \brief Set the two 32-bit input Data allow to set the 64-bit data block used for encryption/decryption.\r
178  * \param data0 corresponds to the first word of the data to be encrypted/decrypted\r
179  * \param data1 corresponds to the last word of the data to be encrypted/decrypted\r
180  */\r
181 void TDES_SetInput(uint32_t data0, uint32_t data1)\r
182 {\r
183     TDES->TDES_IDATAR[0] = data0;\r
184     TDES->TDES_IDATAR[1] = data1;\r
185 }\r
186 \r
187 /**\r
188  * \brief Get the two 32-bit data contain the 64-bit data block which has been encrypted/decrypted.\r
189  * \param data0 point to the first word.\r
190  * \param data1 point to the last word.\r
191  */\r
192 void TDES_GetOutput(uint32_t *data0, uint32_t *data1)\r
193 {\r
194     *data0 = TDES->TDES_ODATAR[0];\r
195     *data1 = TDES->TDES_ODATAR[1];\r
196 }\r
197 \r
198 /**\r
199  * \brief Set the 64-bit initialization vector data block, which is used by some\r
200  * modes of operation as an additional initial input.\r
201  * \param v0 corresponds to the first word of the initialization vector.\r
202  * \param v1 corresponds to the last word  of the initialization vector.\r
203  */\r
204 void TDES_SetVector(uint32_t v0, uint32_t v1)\r
205 {\r
206     TDES->TDES_IVR[0] = v0;\r
207     TDES->TDES_IVR[1] = v1;\r
208 }\r
209 \r
210 /**\r
211  * \brief Set the 6-bit complete rounds.\r
212  * \param rounds corresponds to rounds+1 complete round.\r
213  */\r
214 void TDES_SetXteaRounds(uint32_t rounds)\r
215 {\r
216     TDES->TDES_XTEA_RNDR = TDES_XTEA_RNDR_XTEA_RNDS(rounds);\r
217 }\r
218 \r