summaryrefslogtreecommitdiff
path: root/peripherals/tdes/tdes.c
blob: 4d78e1aca77d2bc006f9067eb5e16d65727e6ec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support 
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include "tdes.h"
#include <board.h>
#include <utility/assert.h>
#include <utility/trace.h>

//------------------------------------------------------------------------------
//         Global functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Configures the triple-DES peripheral to cipher/decipher, use single-DES or
/// triple-DES, use two or three keys (when in triple-DES mode), start manually,
/// automatically or via the PDC and use the given operating mode (ECB, CBC,
/// CFB or OFB).
/// \param cipher  Encrypts if 1, decrypts if 0.
/// \param tdesmod  Single- or triple-DES mode.
/// \param keymod  Use two or three keys (must be 0 in single-DES mode).
/// \param smod  Start mode.
/// \param opmod  Encryption/decryption mode.
//------------------------------------------------------------------------------
void TDES_Configure(
    unsigned char cipher,
    unsigned int tdesmod,
    unsigned int keymod,
    unsigned int smod,
    unsigned int opmod)
{
    TRACE_DEBUG("TDES_Configure()\n\r");
    SANITY_CHECK((cipher & 0xFFFFFFFE) == 0);
    SANITY_CHECK((tdesmod & 0xFFFFFFFD) == 0);
    SANITY_CHECK((keymod & 0xFFFFFFEF) == 0);
    SANITY_CHECK((smod & 0xFFFFFCFF) == 0);
    SANITY_CHECK((opmod & 0xFFFFCFFF) == 0);

    // Reset peripheral
    AT91C_BASE_TDES->TDES_CR = AT91C_TDES_SWRST;

    // Configure mode register
    AT91C_BASE_TDES->TDES_MR = cipher | tdesmod | keymod | smod | opmod;
}

//------------------------------------------------------------------------------
/// Starts the encryption or decryption process if the TDES peripheral is
/// configured in manual or PDC mode.
//------------------------------------------------------------------------------
void TDES_Start(void)
{
    TRACE_DEBUG("TDES_Start()\n\r");
    SANITY_CHECK(((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_SMOD) == AT91C_TDES_SMOD_MANUAL)
                 || ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_SMOD) == AT91C_TDES_SMOD_PDC));

    // Manual mode
    if ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_SMOD) == AT91C_TDES_SMOD_MANUAL) {

        AT91C_BASE_TDES->TDES_CR = AT91C_TDES_START;
    }
    // PDC mode
    else {

        AT91C_BASE_TDES->TDES_PTCR = AT91C_PDC_RXTEN | AT91C_PDC_TXTEN;
    }
}

//------------------------------------------------------------------------------
/// Returns the current status register value of the TDES peripheral.
//------------------------------------------------------------------------------
unsigned int TDES_GetStatus(void)
{
    TRACE_DEBUG("TDES_GetStatus()\n\r");

    return AT91C_BASE_TDES->TDES_ISR;
}

//------------------------------------------------------------------------------
/// Sets the 64-bits keys (one, two or three depending on the configuration)
/// that shall be used by the TDES algorithm.
/// \param pKey1  Pointer to key #1.
/// \param pKey2  Pointer to key #2 (shall be 0 in single-DES mode).
/// \param pKey3  Pointer to key #3 (shall be 0 when using two keys).
//------------------------------------------------------------------------------
void TDES_SetKeys(
    const unsigned int *pKey1,
    const unsigned int *pKey2,
    const unsigned int *pKey3)
{
    TRACE_DEBUG("TDES_SetKeys()\n\r");
    SANITY_CHECK(pKey1);
    SANITY_CHECK((pKey2 && ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_TDESMOD) == AT91C_TDES_TDESMOD))
                 || (!pKey2 && ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_TDESMOD) == 0)));
    SANITY_CHECK((pKey3
                  && ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_TDESMOD) == AT91C_TDES_TDESMOD)
                  && ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_KEYMOD) == 0))
                 ||
                 (!pKey3
                  && ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_TDESMOD) == AT91C_TDES_TDESMOD)
                  && ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_KEYMOD) == AT91C_TDES_KEYMOD))
                 ||
                 (!pKey3
                  && ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_TDESMOD) == 0)
                  && ((AT91C_BASE_TDES->TDES_MR & AT91C_TDES_KEYMOD) == 0)));

    // Write key #1
    if (pKey1) {

        AT91C_BASE_TDES->TDES_KEY1WxR[0] = pKey1[0];
        AT91C_BASE_TDES->TDES_KEY1WxR[1] = pKey1[1];
    }

    // Write key #2
    if (pKey1) {

        AT91C_BASE_TDES->TDES_KEY2WxR[0] = pKey2[0];
        AT91C_BASE_TDES->TDES_KEY2WxR[1] = pKey2[1];
    }

    // Write key #2
    if (pKey1) {

        AT91C_BASE_TDES->TDES_KEY3WxR[0] = pKey3[0];
        AT91C_BASE_TDES->TDES_KEY3WxR[1] = pKey3[1];
    }
}

//------------------------------------------------------------------------------
/// Sets the input data to encrypt/decrypt using TDES.
/// \param pInput  Pointer to the 64-bits input data.
//------------------------------------------------------------------------------
void TDES_SetInputData(const unsigned int *pInput)
{
    TRACE_DEBUG("TDES_SetInputData()\n\r");
    SANITY_CHECK(pInput);

    AT91C_BASE_TDES->TDES_IDATAxR[0] = pInput[0];
    AT91C_BASE_TDES->TDES_IDATAxR[1] = pInput[1];
}

//------------------------------------------------------------------------------
/// Sets the input data buffer to encrypt/decrypt when in PDC mode.
/// \param pInput  Pointer to the input data.
/// \param size  Size of buffer in bytes.
//------------------------------------------------------------------------------
void TDES_SetInputBuffer(const unsigned int *pInput, unsigned int size)
{
    TRACE_DEBUG("TDES_SetInputBuffer()\n\r");
    SANITY_CHECK(pInput);
    SANITY_CHECK((size > 0) && ((size % 8) == 0));

    AT91C_BASE_TDES->TDES_TPR = (unsigned int) pInput;
    AT91C_BASE_TDES->TDES_TCR = size / 4;
}

//------------------------------------------------------------------------------
/// Stores the output data from the last TDES operation into the given 64-bits
/// buffers.
/// \param pOutput  Pointer to a 64-bits output buffer.
//------------------------------------------------------------------------------
void TDES_GetOutputData(unsigned int *pOutput)
{
    TRACE_DEBUG("TDES_GetOutputData()\n\r");
    SANITY_CHECK(pOutput);

    pOutput[0] = AT91C_BASE_TDES->TDES_ODATAxR[0];
    pOutput[1] = AT91C_BASE_TDES->TDES_ODATAxR[1];
}

//------------------------------------------------------------------------------
/// Sets the output buffer which will receive the encrypted/decrypted data when
/// using the PDC.
/// \param pOutput  Pointer to the output data.
/// \param size  Size of buffer in bytes.
//------------------------------------------------------------------------------
void TDES_SetOutputBuffer(unsigned int *pOutput, unsigned int size)
{
    TRACE_DEBUG("TDES_SetOutputBuffer()\n\r");
    SANITY_CHECK(pOutput);
    SANITY_CHECK((size > 0) && ((size % 8) == 0));

    AT91C_BASE_TDES->TDES_RPR = (unsigned int) pOutput;
    AT91C_BASE_TDES->TDES_RCR = size / 4;
}

//------------------------------------------------------------------------------
/// Sets the initialization vector to use when the TDES algorithm is configured
/// in a chained block mode (CBC, CFB or OFB).
/// \param pVector  Pointer to the 64-bits vector.
//------------------------------------------------------------------------------
void TDES_SetVector(const unsigned int *pVector)
{
    TRACE_DEBUG("TDES_SetVector()\n\r");
    SANITY_CHECK(pVector);

    AT91C_BASE_TDES->TDES_IVxR[0] = pVector[0];
    AT91C_BASE_TDES->TDES_IVxR[1] = pVector[1];
}

personal git repositories of Harald Welte. Your mileage may vary