1 |
11 |
ruschi |
/**
|
2 |
|
|
* \file avs_aes.c
|
3 |
|
|
* \brief AES Avalon IP Core software driver
|
4 |
|
|
*
|
5 |
|
|
* This file is part of the project avs_aes
|
6 |
|
|
* see: http://opencores.org/project,avs_aes
|
7 |
|
|
*
|
8 |
|
|
* \section AUTHORS
|
9 |
|
|
* Thomas Ruschival -- ruschi@opencores.org (www.ruschival.de)
|
10 |
|
|
*
|
11 |
|
|
* \section LICENSE
|
12 |
|
|
* Copyright (c) 2009, Authors and opencores.org
|
13 |
|
|
* All rights reserved.
|
14 |
|
|
*
|
15 |
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
16 |
|
|
* are permitted provided that the following conditions are met:
|
17 |
|
|
* * Redistributions of source code must retain the above copyright notice,
|
18 |
|
|
* this list of conditions and the following disclaimer.
|
19 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
20 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
21 |
|
|
* and/or other materials provided with the distribution.
|
22 |
|
|
* * Neither the name of the organization nor the names of its contributors
|
23 |
|
|
* may be used to endorse or promote products derived from this software without
|
24 |
|
|
* specific prior written permission.
|
25 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
26 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
27 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
28 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
29 |
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
30 |
|
|
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
31 |
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
32 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
33 |
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
34 |
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
35 |
|
|
* THE POSSIBILITY OF SUCH DAMAGE
|
36 |
|
|
*/
|
37 |
|
|
|
38 |
|
|
|
39 |
5 |
ruschi |
#include <avs_aes.h>
|
40 |
|
|
#include <string.h>
|
41 |
|
|
|
42 |
11 |
ruschi |
/**
|
43 |
|
|
* \brief setup the context to be used later.
|
44 |
|
|
* initializes the pointers to the correct memory locations
|
45 |
|
|
* \param context : struct grouping address information
|
46 |
|
|
*/
|
47 |
5 |
ruschi |
void avs_aes_init(avs_aes_handle* context){
|
48 |
|
|
context->key = (unsigned int*) KEY_ADDR;
|
49 |
|
|
context->payload= (unsigned int*) DATA_ADDR;
|
50 |
|
|
context->result = (unsigned int*) RESULT_ADDR;
|
51 |
|
|
context->control = (unsigned int*) AESCTRLWD;
|
52 |
|
|
*(context->control) = 0x00000000;
|
53 |
|
|
}
|
54 |
|
|
|
55 |
11 |
ruschi |
|
56 |
|
|
/**
|
57 |
|
|
* \brief setup the context to be used later.
|
58 |
|
|
* initializes the pointers to the correct memory locations
|
59 |
|
|
* \param context struct grouping address information
|
60 |
|
|
* \param key user key to load
|
61 |
|
|
*/
|
62 |
5 |
ruschi |
void avs_aes_setKey(avs_aes_handle* context, unsigned int* key){
|
63 |
|
|
int i=0;
|
64 |
|
|
unsigned int* target_ptr = (unsigned int* )context->key;
|
65 |
|
|
// Invalidate old key;
|
66 |
|
|
*(context->control) &= (~0x00000080);
|
67 |
|
|
for(i=0; i<KEYWORDS; i++){
|
68 |
|
|
*(target_ptr++) = *(key++);
|
69 |
|
|
}
|
70 |
|
|
// validate key;
|
71 |
|
|
*(context->control) |= 0x00000080;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
11 |
ruschi |
|
75 |
|
|
/**
|
76 |
|
|
* \brief loads payload for processing to the core
|
77 |
|
|
* basically memcopy...
|
78 |
|
|
* \param context struct grouping address information
|
79 |
|
|
* \param payload user data to be processed
|
80 |
|
|
*/
|
81 |
5 |
ruschi |
void avs_aes_setPayload(avs_aes_handle* context, unsigned int* payload){
|
82 |
|
|
int i=0;
|
83 |
|
|
unsigned int* target_ptr = (unsigned int* )context->payload;
|
84 |
|
|
for(i=0; i<4; i++){
|
85 |
|
|
*(target_ptr++) = *(payload++);
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
|
90 |
11 |
ruschi |
|
91 |
|
|
/**
|
92 |
|
|
* \brief set the KEY_VALID flag in the control word
|
93 |
|
|
* used to signal the completion of writing the key ( \ref avs_aes_setKey )
|
94 |
|
|
* \param context struct grouping address information
|
95 |
|
|
*/
|
96 |
5 |
ruschi |
void avs_aes_setKeyvalid(avs_aes_handle* context){
|
97 |
|
|
*(context->control) |= 0x00000080;
|
98 |
|
|
}
|
99 |
|
|
|
100 |
11 |
ruschi |
|
101 |
|
|
/**
|
102 |
|
|
* \brief set the ENCRYPT flag in the control word
|
103 |
|
|
* start encryption of (hopefully) previously loaded payload
|
104 |
|
|
* \param context struct grouping address information
|
105 |
|
|
*/
|
106 |
5 |
ruschi |
void avs_aes_encrypt(avs_aes_handle* context){
|
107 |
|
|
*(context->control) |= 0x00000001;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
11 |
ruschi |
/**
|
111 |
|
|
* \brief set the DECRYPT flag in the control word
|
112 |
|
|
* start encryption of (hopefully) previously loaded payload
|
113 |
|
|
* \param context struct grouping address information
|
114 |
|
|
*/
|
115 |
5 |
ruschi |
void avs_aes_decrypt(avs_aes_handle* context){
|
116 |
|
|
*(context->control) |= 0x00000002;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
11 |
ruschi |
/**
|
120 |
|
|
* \brief checks the COMPLETED flag
|
121 |
|
|
* can be used for ugly polling the slave if IRQs are not used
|
122 |
|
|
* \param context struct grouping address information
|
123 |
|
|
* \return 1 if still computing 0 if done.
|
124 |
|
|
*/
|
125 |
5 |
ruschi |
int avs_aes_isBusy(avs_aes_handle* context){
|
126 |
|
|
unsigned int mycontrol = *(context->control);
|
127 |
|
|
return mycontrol & 0x03;
|
128 |
|
|
}
|