OpenCores
URL https://opencores.org/ocsvn/tiny_aes/tiny_aes/trunk

Subversion Repositories tiny_aes

[/] [tiny_aes/] [trunk/] [rtl/] [aes_128.v] - Blame information for rev 5

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 homer.hsin
/*
2
 * Copyright 2012, Homer Hsing <homer.hsing@gmail.com>
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
module aes_128(clk, state, key, out);
18
    input          clk;
19
    input  [127:0] state, key;
20
    output [127:0] out;
21
    reg    [127:0] s0, k0;
22
    wire   [127:0] s1, k1, s2, k2, s3, k3, s4, k4, s5, k5,
23
                   s6, k6, s7, k7, s8, k8, s9, k9, s10;
24
 
25
    always @ (posedge clk)
26
      begin
27
        s0 <= state ^ key;
28
        k0 <= key;
29
      end
30
    assign out = s10;
31
    one_round_128
32
        r1 (clk, s0, k0, s1, k1, 8'h1),
33
        r2 (clk, s1, k1, s2, k2, 8'h2),
34
        r3 (clk, s2, k2, s3, k3, 8'h4),
35
        r4 (clk, s3, k3, s4, k4, 8'h8),
36
        r5 (clk, s4, k4, s5, k5, 8'h10),
37
        r6 (clk, s5, k5, s6, k6, 8'h20),
38
        r7 (clk, s6, k6, s7, k7, 8'h40),
39
        r8 (clk, s7, k7, s8, k8, 8'h80),
40
        r9 (clk, s8, k8, s9, k9, 8'h1b);
41
    final_round_128
42
        rf (clk, s9, k9, s10, 8'h36);
43
endmodule
44
 
45
module one_round_128(clk, state_in, key_in, state_out, key_out, rcon);
46
    input              clk;
47
    input      [127:0] state_in,  key_in;
48
    input      [7:0]   rcon;
49
    output reg [127:0] state_out, key_out;
50
    wire [31:0] s0,  s1,  s2,  s3,
51
                v0,  v1,  v2,  v3,
52
                z0,  z1,  z2,  z3,
53
                p00, p01, p02, p03,
54
                p10, p11, p12, p13,
55
                p20, p21, p22, p23,
56
                p30, p31, p32, p33,
57
                k0,  k1,  k2,  k3;
58
    reg  [31:0] k0a, k1a, k2a, k3a;
59
    wire [31:0] k0b, k1b, k2b, k3b, k4a;
60
 
61
    assign {k0, k1, k2, k3} = key_in;
62
    assign v0 = {k0[31:24] ^ rcon, k0[23:0]};
63
    assign v1 = v0 ^ k1;
64
    assign v2 = v1 ^ k2;
65
    assign v3 = v2 ^ k3;
66
    always @ (posedge clk)
67
        {k0a, k1a, k2a, k3a} <= {v0, v1, v2, v3};
68
    S4
69
        S4_0 (clk, {k3[23:0], k3[31:24]}, k4a);
70
    assign k0b = k0a ^ k4a;
71
    assign k1b = k1a ^ k4a;
72
    assign k2b = k2a ^ k4a;
73
    assign k3b = k3a ^ k4a;
74
    always @ (posedge clk)
75
        key_out <= {k0b, k1b, k2b, k3b};
76
 
77
    assign {s0, s1, s2, s3} = state_in;
78
    table_lookup
79
        t0 (clk, s0, p00, p01, p02, p03),
80
        t1 (clk, s1, p10, p11, p12, p13),
81
        t2 (clk, s2, p20, p21, p22, p23),
82
        t3 (clk, s3, p30, p31, p32, p33);
83
    assign z0 = p00 ^ p11 ^ p22 ^ p33 ^ k0b;
84
    assign z1 = p03 ^ p10 ^ p21 ^ p32 ^ k1b;
85
    assign z2 = p02 ^ p13 ^ p20 ^ p31 ^ k2b;
86
    assign z3 = p01 ^ p12 ^ p23 ^ p30 ^ k3b;
87
    always @ (posedge clk)
88
        state_out <= {z0, z1, z2, z3};
89
endmodule
90
 
91
module final_round_128(clk, state_in, key_in, state_out, rcon);
92
    input              clk;
93
    input      [127:0] state_in,  key_in;
94
    input      [7:0]   rcon;
95
    output reg [127:0] state_out;
96
    wire [31:0] s0,  s1,  s2,  s3,
97
                v0,  v1,  v2,  v3,
98
                z0,  z1,  z2,  z3,
99
                k0,  k1,  k2,  k3;
100
    reg  [31:0] k0a, k1a, k2a, k3a;
101
    wire [31:0] k0b, k1b, k2b, k3b, k4a;
102
    wire [7:0]  p00, p01, p02, p03,
103
                p10, p11, p12, p13,
104
                p20, p21, p22, p23,
105
                p30, p31, p32, p33;
106
 
107
    assign {k0, k1, k2, k3} = key_in;
108
    assign v0 = {k0[31:24] ^ rcon, k0[23:0]};
109
    assign v1 = v0 ^ k1;
110
    assign v2 = v1 ^ k2;
111
    assign v3 = v2 ^ k3;
112
    always @ (posedge clk)
113
        {k0a, k1a, k2a, k3a} <= {v0, v1, v2, v3};
114
    S4
115
        S4_0 (clk, {k3[23:0], k3[31:24]}, k4a);
116
    assign k0b = k0a ^ k4a;
117
    assign k1b = k1a ^ k4a;
118
    assign k2b = k2a ^ k4a;
119
    assign k3b = k3a ^ k4a;
120
 
121
    assign {s0, s1, s2, s3} = state_in;
122
    S4
123
        S4_1 (clk, s0, {p00, p01, p02, p03}),
124
        S4_2 (clk, s1, {p10, p11, p12, p13}),
125
        S4_3 (clk, s2, {p20, p21, p22, p23}),
126
        S4_4 (clk, s3, {p30, p31, p32, p33});
127
    assign z0 = {p00, p11, p22, p33} ^ k0b;
128
    assign z1 = {p10, p21, p32, p03} ^ k1b;
129
    assign z2 = {p20, p31, p02, p13} ^ k2b;
130
    assign z3 = {p30, p01, p12, p23} ^ k3b;
131
    always @ (posedge clk)
132
        state_out <= {z0, z1, z2, z3};
133
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.