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