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 "aes.h"
|
39 |
|
|
#include <board.h>
|
40 |
|
|
#include <utility/trace.h>
|
41 |
|
|
#include <utility/assert.h>
|
42 |
|
|
|
43 |
|
|
//------------------------------------------------------------------------------
|
44 |
|
|
// Global functions
|
45 |
|
|
//------------------------------------------------------------------------------
|
46 |
|
|
|
47 |
|
|
//------------------------------------------------------------------------------
|
48 |
|
|
/// Configures the AES peripheral to encrypt/decrypt, start mode (manual, auto,
|
49 |
|
|
/// PDC) and operating mode (ECB, CBC, OFB, CFB, CTR).
|
50 |
|
|
/// \param cipher Indicates if the peripheral should encrypt or decrypt data.
|
51 |
|
|
/// \param smode Start mode.
|
52 |
|
|
/// \param opmode Operating mode.
|
53 |
|
|
//------------------------------------------------------------------------------
|
54 |
|
|
void AES_Configure(
|
55 |
|
|
unsigned char cipher,
|
56 |
|
|
unsigned int smode,
|
57 |
|
|
unsigned int opmode)
|
58 |
|
|
{
|
59 |
|
|
trace_LOG(trace_DEBUG, "-D- AES_Configure()\n\r");
|
60 |
|
|
SANITY_CHECK((cipher & 0xFFFFFFFE) == 0);
|
61 |
|
|
SANITY_CHECK((smode & 0xFFFFFCFF) == 0);
|
62 |
|
|
SANITY_CHECK((opmode & 0xFFFF8FFF) == 0);
|
63 |
|
|
|
64 |
|
|
// Reset the peripheral first
|
65 |
|
|
AT91C_BASE_AES->AES_CR = AT91C_AES_SWRST;
|
66 |
|
|
|
67 |
|
|
// Configure mode register
|
68 |
|
|
AT91C_BASE_AES->AES_MR = cipher | smode | opmode;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
//------------------------------------------------------------------------------
|
72 |
|
|
/// Sets the key used by the AES algorithm to cipher the plain text or
|
73 |
|
|
/// decipher the encrypted text.
|
74 |
|
|
/// \param pKey Pointer to a 16-bytes cipher key.
|
75 |
|
|
//------------------------------------------------------------------------------
|
76 |
|
|
void AES_SetKey(const unsigned int *pKey)
|
77 |
|
|
{
|
78 |
|
|
trace_LOG(trace_DEBUG, "-D- AES_SetKey()\n\r");
|
79 |
|
|
SANITY_CHECK(pKey);
|
80 |
|
|
|
81 |
|
|
AT91C_BASE_AES->AES_KEYWxR[0] = pKey[0];
|
82 |
|
|
AT91C_BASE_AES->AES_KEYWxR[1] = pKey[1];
|
83 |
|
|
AT91C_BASE_AES->AES_KEYWxR[2] = pKey[2];
|
84 |
|
|
AT91C_BASE_AES->AES_KEYWxR[3] = pKey[3];
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
//------------------------------------------------------------------------------
|
88 |
|
|
/// Sets the initialization vector that is used to encrypt the plain text or
|
89 |
|
|
/// decrypt the cipher text in chained block modes (CBC, CFB, OFB & CTR).
|
90 |
|
|
/// \param pVector Pointer to a 16-bytes initialization vector.
|
91 |
|
|
//------------------------------------------------------------------------------
|
92 |
|
|
void AES_SetVector(const unsigned int *pVector)
|
93 |
|
|
{
|
94 |
|
|
trace_LOG(trace_DEBUG, "-D- AES_SetVector()\n\r");
|
95 |
|
|
SANITY_CHECK(pVector);
|
96 |
|
|
|
97 |
|
|
AT91C_BASE_AES->AES_IVxR[0] = pVector[0];
|
98 |
|
|
AT91C_BASE_AES->AES_IVxR[1] = pVector[1];
|
99 |
|
|
AT91C_BASE_AES->AES_IVxR[2] = pVector[2];
|
100 |
|
|
AT91C_BASE_AES->AES_IVxR[3] = pVector[3];
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
//------------------------------------------------------------------------------
|
104 |
|
|
/// Sets the input data of the AES algorithm (i.e. plain text in cipher mode,
|
105 |
|
|
/// ciphered text in decipher mode). If auto mode is active, the encryption is
|
106 |
|
|
/// started automatically after writing the last word.
|
107 |
|
|
/// \param pData Pointer to the 16-bytes data to cipher/decipher.
|
108 |
|
|
//------------------------------------------------------------------------------
|
109 |
|
|
void AES_SetInputData(const unsigned int *pData)
|
110 |
|
|
{
|
111 |
|
|
trace_LOG(trace_DEBUG, "-D- AES_SetInputData()\n\r");
|
112 |
|
|
SANITY_CHECK(pData);
|
113 |
|
|
|
114 |
|
|
AT91C_BASE_AES->AES_IDATAxR[0] = pData[0];
|
115 |
|
|
AT91C_BASE_AES->AES_IDATAxR[1] = pData[1];
|
116 |
|
|
AT91C_BASE_AES->AES_IDATAxR[2] = pData[2];
|
117 |
|
|
AT91C_BASE_AES->AES_IDATAxR[3] = pData[3];
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
//------------------------------------------------------------------------------
|
121 |
|
|
/// Stores the result of the last AES operation (encrypt/decrypt) in the
|
122 |
|
|
/// provided buffer.
|
123 |
|
|
/// \param pData Pointer to a 16-bytes buffer.
|
124 |
|
|
//------------------------------------------------------------------------------
|
125 |
|
|
void AES_GetOutputData(unsigned int *pData)
|
126 |
|
|
{
|
127 |
|
|
trace_LOG(trace_DEBUG, "-D- AES_GetOutputData()\n\r");
|
128 |
|
|
SANITY_CHECK(pData);
|
129 |
|
|
|
130 |
|
|
pData[0] = AT91C_BASE_AES->AES_ODATAxR[0];
|
131 |
|
|
pData[1] = AT91C_BASE_AES->AES_ODATAxR[1];
|
132 |
|
|
pData[2] = AT91C_BASE_AES->AES_ODATAxR[2];
|
133 |
|
|
pData[3] = AT91C_BASE_AES->AES_ODATAxR[3];
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
//------------------------------------------------------------------------------
|
137 |
|
|
/// Sets the input buffer to use when in PDC mode.
|
138 |
|
|
/// \param pInput Pointer to the input buffer.
|
139 |
|
|
//------------------------------------------------------------------------------
|
140 |
|
|
void AES_SetInputBuffer(const unsigned int *pInput)
|
141 |
|
|
{
|
142 |
|
|
trace_LOG(trace_DEBUG, "-D- AES_SetInputBuffer()\n\r");
|
143 |
|
|
SANITY_CHECK(pInput);
|
144 |
|
|
|
145 |
|
|
AT91C_BASE_AES->AES_TPR = (unsigned int) pInput;
|
146 |
|
|
AT91C_BASE_AES->AES_TCR = 4;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
//------------------------------------------------------------------------------
|
150 |
|
|
/// Sets the output buffer to use when in PDC mode.
|
151 |
|
|
/// \param pOutput Pointer to the output buffer.
|
152 |
|
|
//------------------------------------------------------------------------------
|
153 |
|
|
void AES_SetOutputBuffer(unsigned int *pOutput)
|
154 |
|
|
{
|
155 |
|
|
trace_LOG(trace_DEBUG, "-D- AES_SetOutputBuffer()\n\r");
|
156 |
|
|
SANITY_CHECK(pOutput);
|
157 |
|
|
|
158 |
|
|
AT91C_BASE_AES->AES_RPR = (unsigned int) pOutput;
|
159 |
|
|
AT91C_BASE_AES->AES_RCR = 4;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
//------------------------------------------------------------------------------
|
163 |
|
|
/// Starts the encryption/decryption process when in manual or PDC mode. In
|
164 |
|
|
/// manual mode, the key and input data must have been entered using
|
165 |
|
|
/// AES_SetKey() and AES_SetInputData(). In PDC mode, the key, input & output
|
166 |
|
|
/// buffer must have been set using AES_SetKey(), AES_SetInputBuffer() and
|
167 |
|
|
/// AES_SetOutputBuffer().
|
168 |
|
|
//------------------------------------------------------------------------------
|
169 |
|
|
void AES_Start(void)
|
170 |
|
|
{
|
171 |
|
|
trace_LOG(trace_DEBUG, "AES_Start()\n\r");
|
172 |
|
|
SANITY_CHECK(((AT91C_BASE_AES->AES_MR & AT91C_AES_SMOD) == AT91C_AES_SMOD_MANUAL)
|
173 |
|
|
|| ((AT91C_BASE_AES->AES_MR & AT91C_AES_SMOD) == AT91C_AES_SMOD_PDC));
|
174 |
|
|
|
175 |
|
|
// Manual mode
|
176 |
|
|
if ((AT91C_BASE_AES->AES_MR & AT91C_AES_SMOD) == AT91C_AES_SMOD_MANUAL) {
|
177 |
|
|
|
178 |
|
|
AT91C_BASE_AES->AES_CR = AT91C_AES_START;
|
179 |
|
|
}
|
180 |
|
|
// PDC
|
181 |
|
|
else {
|
182 |
|
|
|
183 |
|
|
AT91C_BASE_AES->AES_PTCR = AT91C_PDC_RXTEN | AT91C_PDC_TXTEN;
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
//------------------------------------------------------------------------------
|
188 |
|
|
/// Returns the current value of the AES interrupt status register.
|
189 |
|
|
//------------------------------------------------------------------------------
|
190 |
|
|
unsigned int AES_GetStatus(void)
|
191 |
|
|
{
|
192 |
|
|
trace_LOG(trace_DEBUG, "-D- AES_GetStatus()\n\r");
|
193 |
|
|
|
194 |
|
|
return AT91C_BASE_AES->AES_ISR;
|
195 |
|
|
}
|
196 |
|
|
|