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