1 |
608 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
2 |
|
|
* ATMEL Microcontroller Software Support
|
3 |
|
|
* ----------------------------------------------------------------------------
|
4 |
|
|
* Copyright (c) 2008, Atmel 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 |
|
|
* Atmel'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 ATMEL "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 ATMEL 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 |
|
|
|
30 |
|
|
#ifndef trace_LEVEL
|
31 |
|
|
#define trace_LEVEL 1
|
32 |
|
|
#endif
|
33 |
|
|
|
34 |
|
|
//------------------------------------------------------------------------------
|
35 |
|
|
// Headers
|
36 |
|
|
//------------------------------------------------------------------------------
|
37 |
|
|
|
38 |
|
|
#include "tdes.h"
|
39 |
|
|
#include <board.h>
|
40 |
|
|
#include <utility/assert.h>
|
41 |
|
|
#include <utility/trace.h>
|
42 |
|
|
|
43 |
|
|
//------------------------------------------------------------------------------
|
44 |
|
|
// Global functions
|
45 |
|
|
//------------------------------------------------------------------------------
|
46 |
|
|
|
47 |
|
|
//------------------------------------------------------------------------------
|
48 |
|
|
/// Configures the triple-DES peripheral to cipher/decipher, use single-DES or
|
49 |
|
|
/// triple-DES, use two or three keys (when in triple-DES mode), start manually,
|
50 |
|
|
/// automatically or via the PDC and use the given operating mode (ECB, CBC,
|
51 |
|
|
/// CFB or OFB).
|
52 |
|
|
/// \param cipher Encrypts if 1, decrypts if 0.
|
53 |
|
|
/// \param tdesmod Single- or triple-DES mode.
|
54 |
|
|
/// \param keymod Use two or three keys (must be 0 in single-DES mode).
|
55 |
|
|
/// \param smod Start mode.
|
56 |
|
|
/// \param opmod Encryption/decryption mode.
|
57 |
|
|
//------------------------------------------------------------------------------
|
58 |
|
|
void TDES_Configure(
|
59 |
|
|
unsigned char cipher,
|
60 |
|
|
unsigned int tdesmod,
|
61 |
|
|
unsigned int keymod,
|
62 |
|
|
unsigned int smod,
|
63 |
|
|
unsigned int opmod)
|
64 |
|
|
{
|
65 |
|
|
trace_LOG(trace_DEBUG, "-D- TDES_Configure()\n\r");
|
66 |
|
|
SANITY_CHECK((cipher & 0xFFFFFFFE) == 0);
|
67 |
|
|
SANITY_CHECK((tdesmod & 0xFFFFFFFD) == 0);
|
68 |
|
|
SANITY_CHECK((keymod & 0xFFFFFFEF) == 0);
|
69 |
|
|
SANITY_CHECK((smod & 0xFFFFFCFF) == 0);
|
70 |
|
|
SANITY_CHECK((opmod & 0xFFFFCFFF) == 0);
|
71 |
|
|
|
72 |
|
|
// Reset peripheral
|
73 |
|
|
AT91C_BASE_TDES->TDES_CR = AT91C_TDES_SWRST;
|
74 |
|
|
|
75 |
|
|
// Configure mode register
|
76 |
|
|
AT91C_BASE_TDES->TDES_MR = cipher | tdesmod | keymod | smod | opmod;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
//------------------------------------------------------------------------------
|
80 |
|
|
/// Starts the encryption or decryption process if the TDES peripheral is
|
81 |
|
|
/// configured in manual or PDC mode.
|
82 |
|
|
//------------------------------------------------------------------------------
|
83 |
|
|
void TDES_Start(void)
|
84 |
|
|
{
|
85 |
|
|
trace_LOG(trace_DEBUG, "-D- TDES_Start()\n\r");
|
86 |
|
|
SANITY_CHECK(((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_SMOD) == AT91C_TDES_SMOD_MANUAL)
|
87 |
|
|
|| ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_SMOD) == AT91C_TDES_SMOD_PDC));
|
88 |
|
|
|
89 |
|
|
// Manual mode
|
90 |
|
|
if ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_SMOD) == AT91C_TDES_SMOD_MANUAL) {
|
91 |
|
|
|
92 |
|
|
AT91C_BASE_TDES->TDES_CR = AT91C_TDES_START;
|
93 |
|
|
}
|
94 |
|
|
// PDC mode
|
95 |
|
|
else {
|
96 |
|
|
|
97 |
|
|
AT91C_BASE_TDES->TDES_PTCR = AT91C_PDC_RXTEN | AT91C_PDC_TXTEN;
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
//------------------------------------------------------------------------------
|
102 |
|
|
/// Returns the current status register value of the TDES peripheral.
|
103 |
|
|
//------------------------------------------------------------------------------
|
104 |
|
|
unsigned int TDES_GetStatus(void)
|
105 |
|
|
{
|
106 |
|
|
trace_LOG(trace_DEBUG, "-D- TDES_GetStatus()\n\r");
|
107 |
|
|
|
108 |
|
|
return AT91C_BASE_TDES->TDES_ISR;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
//------------------------------------------------------------------------------
|
112 |
|
|
/// Sets the 64-bits keys (one, two or three depending on the configuration)
|
113 |
|
|
/// that shall be used by the TDES algorithm.
|
114 |
|
|
/// \param pKey1 Pointer to key #1.
|
115 |
|
|
/// \param pKey2 Pointer to key #2 (shall be 0 in single-DES mode).
|
116 |
|
|
/// \param pKey3 Pointer to key #3 (shall be 0 when using two keys).
|
117 |
|
|
//------------------------------------------------------------------------------
|
118 |
|
|
void TDES_SetKeys(
|
119 |
|
|
const unsigned int *pKey1,
|
120 |
|
|
const unsigned int *pKey2,
|
121 |
|
|
const unsigned int *pKey3)
|
122 |
|
|
{
|
123 |
|
|
trace_LOG(trace_DEBUG, "-D- TDES_SetKeys()\n\r");
|
124 |
|
|
SANITY_CHECK(pKey1);
|
125 |
|
|
SANITY_CHECK((pKey2 && ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_TDESMOD) == AT91C_TDES_TDESMOD))
|
126 |
|
|
|| (!pKey2 && ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_TDESMOD) == 0)));
|
127 |
|
|
SANITY_CHECK((pKey3
|
128 |
|
|
&& ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_TDESMOD) == AT91C_TDES_TDESMOD)
|
129 |
|
|
&& ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_KEYMOD) == 0))
|
130 |
|
|
||
|
131 |
|
|
(!pKey3
|
132 |
|
|
&& ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_TDESMOD) == AT91C_TDES_TDESMOD)
|
133 |
|
|
&& ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_KEYMOD) == AT91C_TDES_KEYMOD))
|
134 |
|
|
||
|
135 |
|
|
(!pKey3
|
136 |
|
|
&& ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_TDESMOD) == 0)
|
137 |
|
|
&& ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_KEYMOD) == 0)));
|
138 |
|
|
|
139 |
|
|
// Write key #1
|
140 |
|
|
if (pKey1) {
|
141 |
|
|
|
142 |
|
|
AT91C_BASE_TDES->TDES_KEY1WxR[0] = pKey1[0];
|
143 |
|
|
AT91C_BASE_TDES->TDES_KEY1WxR[1] = pKey1[1];
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
// Write key #2
|
147 |
|
|
if (pKey1) {
|
148 |
|
|
|
149 |
|
|
AT91C_BASE_TDES->TDES_KEY2WxR[0] = pKey2[0];
|
150 |
|
|
AT91C_BASE_TDES->TDES_KEY2WxR[1] = pKey2[1];
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
// Write key #2
|
154 |
|
|
if (pKey1) {
|
155 |
|
|
|
156 |
|
|
AT91C_BASE_TDES->TDES_KEY3WxR[0] = pKey3[0];
|
157 |
|
|
AT91C_BASE_TDES->TDES_KEY3WxR[1] = pKey3[1];
|
158 |
|
|
}
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
//------------------------------------------------------------------------------
|
162 |
|
|
/// Sets the input data to encrypt/decrypt using TDES.
|
163 |
|
|
/// \param pInput Pointer to the 64-bits input data.
|
164 |
|
|
//------------------------------------------------------------------------------
|
165 |
|
|
void TDES_SetInputData(const unsigned int *pInput)
|
166 |
|
|
{
|
167 |
|
|
trace_LOG(trace_DEBUG, "-D- TDES_SetInputData()\n\r");
|
168 |
|
|
SANITY_CHECK(pInput);
|
169 |
|
|
|
170 |
|
|
AT91C_BASE_TDES->TDES_IDATAxR[0] = pInput[0];
|
171 |
|
|
AT91C_BASE_TDES->TDES_IDATAxR[1] = pInput[1];
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
//------------------------------------------------------------------------------
|
175 |
|
|
/// Sets the input data buffer to encrypt/decrypt when in PDC mode.
|
176 |
|
|
/// \param pInput Pointer to the input data.
|
177 |
|
|
/// \param size Size of buffer in bytes.
|
178 |
|
|
//------------------------------------------------------------------------------
|
179 |
|
|
void TDES_SetInputBuffer(const unsigned int *pInput, unsigned int size)
|
180 |
|
|
{
|
181 |
|
|
trace_LOG(trace_DEBUG, "-D- TDES_SetInputBuffer()\n\r");
|
182 |
|
|
SANITY_CHECK(pInput);
|
183 |
|
|
SANITY_CHECK((size > 0) && ((size % 8) == 0));
|
184 |
|
|
|
185 |
|
|
AT91C_BASE_TDES->TDES_TPR = (unsigned int) pInput;
|
186 |
|
|
AT91C_BASE_TDES->TDES_TCR = size / 4;
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
//------------------------------------------------------------------------------
|
190 |
|
|
/// Stores the output data from the last TDES operation into the given 64-bits
|
191 |
|
|
/// buffers.
|
192 |
|
|
/// \param pOutput Pointer to a 64-bits output buffer.
|
193 |
|
|
//------------------------------------------------------------------------------
|
194 |
|
|
void TDES_GetOutputData(unsigned int *pOutput)
|
195 |
|
|
{
|
196 |
|
|
trace_LOG(trace_DEBUG, "-D- TDES_GetOutputData()\n\r");
|
197 |
|
|
SANITY_CHECK(pOutput);
|
198 |
|
|
|
199 |
|
|
pOutput[0] = AT91C_BASE_TDES->TDES_ODATAxR[0];
|
200 |
|
|
pOutput[1] = AT91C_BASE_TDES->TDES_ODATAxR[1];
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
//------------------------------------------------------------------------------
|
204 |
|
|
/// Sets the output buffer which will receive the encrypted/decrypted data when
|
205 |
|
|
/// using the PDC.
|
206 |
|
|
/// \param pOutput Pointer to the output data.
|
207 |
|
|
/// \param size Size of buffer in bytes.
|
208 |
|
|
//------------------------------------------------------------------------------
|
209 |
|
|
void TDES_SetOutputBuffer(unsigned int *pOutput, unsigned int size)
|
210 |
|
|
{
|
211 |
|
|
trace_LOG(trace_DEBUG, "-D- TDES_SetOutputBuffer()\n\r");
|
212 |
|
|
SANITY_CHECK(pOutput);
|
213 |
|
|
SANITY_CHECK((size > 0) && ((size % 8) == 0));
|
214 |
|
|
|
215 |
|
|
AT91C_BASE_TDES->TDES_RPR = (unsigned int) pOutput;
|
216 |
|
|
AT91C_BASE_TDES->TDES_RCR = size / 4;
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
//------------------------------------------------------------------------------
|
220 |
|
|
/// Sets the initialization vector to use when the TDES algorithm is configured
|
221 |
|
|
/// in a chained block mode (CBC, CFB or OFB).
|
222 |
|
|
/// \param pVector Pointer to the 64-bits vector.
|
223 |
|
|
//------------------------------------------------------------------------------
|
224 |
|
|
void TDES_SetVector(const unsigned int *pVector)
|
225 |
|
|
{
|
226 |
|
|
trace_LOG(trace_DEBUG, "-D- TDES_SetVector()\n\r");
|
227 |
|
|
SANITY_CHECK(pVector);
|
228 |
|
|
|
229 |
|
|
AT91C_BASE_TDES->TDES_IVxR[0] = pVector[0];
|
230 |
|
|
AT91C_BASE_TDES->TDES_IVxR[1] = pVector[1];
|
231 |
|
|
}
|
232 |
|
|
|