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 |
21 |
ruschi |
#include <stdint.h>
|
42 |
5 |
ruschi |
|
43 |
21 |
ruschi |
/** memory offset for key DON'T CHANGE! */
|
44 |
|
|
const uint32_t KEY_ADDR = AES_BASEADDR;
|
45 |
|
|
/** memory offset of payload */
|
46 |
|
|
const uint32_t DATA_ADDR = AES_BASEADDR+0x08;
|
47 |
|
|
/** memory offset of result */
|
48 |
|
|
const uint32_t RESULT_ADDR = AES_BASEADDR+0x10;
|
49 |
|
|
/** memory offset for control word */
|
50 |
|
|
const uint32_t AESCTRLWD = AES_BASEADDR+0x18;
|
51 |
|
|
|
52 |
|
|
|
53 |
5 |
ruschi |
void avs_aes_init(avs_aes_handle* context){
|
54 |
|
|
context->key = (unsigned int*) KEY_ADDR;
|
55 |
|
|
context->payload= (unsigned int*) DATA_ADDR;
|
56 |
|
|
context->result = (unsigned int*) RESULT_ADDR;
|
57 |
|
|
context->control = (unsigned int*) AESCTRLWD;
|
58 |
|
|
*(context->control) = 0x00000000;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
11 |
ruschi |
|
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 |
21 |
ruschi |
/* Invalidate old key; */
|
66 |
|
|
*(context->control) &= (~KEY_VALID);
|
67 |
|
|
asm __volatile("sync" :::);
|
68 |
5 |
ruschi |
for(i=0; i<KEYWORDS; i++){
|
69 |
|
|
*(target_ptr++) = *(key++);
|
70 |
|
|
}
|
71 |
21 |
ruschi |
asm __volatile("sync" :::);
|
72 |
|
|
/* validate key */
|
73 |
|
|
*(context->control) |= KEY_VALID;
|
74 |
5 |
ruschi |
}
|
75 |
|
|
|
76 |
11 |
ruschi |
|
77 |
5 |
ruschi |
void avs_aes_setPayload(avs_aes_handle* context, unsigned int* payload){
|
78 |
|
|
int i=0;
|
79 |
|
|
unsigned int* target_ptr = (unsigned int* )context->payload;
|
80 |
|
|
for(i=0; i<4; i++){
|
81 |
|
|
*(target_ptr++) = *(payload++);
|
82 |
|
|
}
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
void avs_aes_setKeyvalid(avs_aes_handle* context){
|
87 |
21 |
ruschi |
*(context->control) |= KEY_VALID;
|
88 |
|
|
asm __volatile("sync" :::);
|
89 |
5 |
ruschi |
}
|
90 |
|
|
|
91 |
11 |
ruschi |
|
92 |
5 |
ruschi |
void avs_aes_encrypt(avs_aes_handle* context){
|
93 |
21 |
ruschi |
*(context->control) |= ENCRYPT;
|
94 |
|
|
asm __volatile("sync" :::);
|
95 |
5 |
ruschi |
}
|
96 |
|
|
|
97 |
21 |
ruschi |
|
98 |
|
|
void avs_aes_decrypt(avs_aes_handle* context) {
|
99 |
|
|
*(context->control) |= DECRYPT;
|
100 |
|
|
asm __volatile("sync" :::);
|
101 |
5 |
ruschi |
}
|
102 |
|
|
|
103 |
21 |
ruschi |
|
104 |
|
|
int avs_aes_isBusy(avs_aes_handle* context) {
|
105 |
5 |
ruschi |
unsigned int mycontrol = *(context->control);
|
106 |
21 |
ruschi |
return mycontrol & (DECRYPT | ENCRYPT);
|
107 |
5 |
ruschi |
}
|