当前位置:   article > 正文

AES-128的verilog实现_aes 128 verilog

aes 128 verilog

原理部分可以参考:AES加密算法原理的详细介绍与实现_TimeShatter的博客-CSDN博客​​​​​废话不多说直接上代码:​​​​​​​

2024.5.8:在评论区更新了最新的代码,和官方文档加解密结果一致

主要模块:aes_main模块

  1. module aes_main (
  2. input wire [127:0] plaintext, // 输入明文
  3. input wire [128*11-1:0] round_keys , // 输入密钥
  4. input [5:0]state,
  5. input clk,
  6. input rst,
  7. output wire [127:0] ciphertext,// 输出密文
  8. output wire [127:0] plaintext_1// 输出恢复明文
  9. );
  10. integer k;
  11. reg [127:0] round_key [0:10];
  12. always @(*)begin
  13. for(k=0;k<=10;k=k+1)begin
  14. round_key[k]=round_keys[128*k +:128];
  15. end
  16. end
  17. reg[127:0]mid_value;
  18. reg[127:0]mid_value_1;
  19. wire [127:0]data[0:9];
  20. wire [127:0]data_1[0:9];
  21. assign data[0] = mid_value;
  22. assign data_1[0]=mid_value_1;
  23. // 初始轮
  24. always @(posedge clk or negedge rst) begin
  25. if (!rst) begin
  26. mid_value<=128'b0;
  27. end
  28. else begin
  29. case (state)
  30. 12: mid_value<=plaintext^round_key[0];// 初始轮
  31. endcase
  32. end
  33. end
  34. // 轮函数
  35. genvar i;
  36. generate
  37. for (i = 1; i < 10; i = i + 1) begin : round
  38. aes_round round (
  39. .state(data[i-1]),
  40. .round_key(round_key[i]),
  41. .next_state(data[i]),
  42. .clk(clk),
  43. .rst(rst)
  44. );
  45. end
  46. endgenerate
  47. // 最后一轮
  48. final_round final (
  49. .state(data[9]),
  50. .round_key(round_key[10]),
  51. .ciphertext(ciphertext)
  52. );
  53. // 初始轮
  54. always @(posedge clk or negedge rst) begin
  55. if (!rst) begin
  56. mid_value_1<=128'b0;
  57. end
  58. else begin
  59. case (state)
  60. 22: mid_value_1<=ciphertext^round_key[10];// 初始轮
  61. endcase
  62. end
  63. end
  64. //逆轮变换
  65. genvar j;
  66. generate
  67. for (j = 1; j < 10; j = j + 1) begin : round_1
  68. inv_aes_round inv_round (
  69. .state(data_1[j-1]),
  70. .round_key(round_key[10-j]),
  71. .next_state(data_1[j]),
  72. .clk(clk),
  73. .rst(rst)
  74. );
  75. end
  76. endgenerate
  77. // 最后一轮
  78. inv_final_round inv_final (
  79. .state(data_1[9]),
  80. .round_key(round_key[0]),
  81. .ciphertext(plaintext_1)
  82. );
  83. endmodule
  1. 模块定义:

    • 模块名为 aes_main
    • 输入信号包括明文 plaintext,密钥 round_keys,状态 state,时钟信号 clk,复位信号 rst
    • 输出信号包括密文 ciphertext 和恢复的明文 plaintext_1
  2. 整数 kreg 数组 round_key 用于存储轮密钥。

  3. always 块用于将 round_keys 分解成各轮的密钥存储在 round_key 数组中。

  4. mid_valuemid_value_1 是用于存储中间数据的寄存器。

  5. datadata_1 数组用于存储每轮的数据。

  6. 初始轮(Initialization Round):

    • 使用 always 块,根据 state 的值来执行初始轮操作。在状态为 12 时,执行 plaintext 和第一轮密钥的异或操作,并将结果存储在 mid_value 中。
  7. 轮函数(Round Function):

    • 使用 generate 区块创建轮函数。它包括一个循环,从第二轮到第九轮。每轮都使用名为 aes_round 的子模块进行处理,传递前一轮的状态数据、当前轮的密钥、时钟信号和复位信号。
  8. 最后一轮(Final Round):

    • 使用名为 final_round 的子模块,将第十轮的状态数据和密钥传递给它,生成最终的密文 ciphertext
  9. 逆初始轮和逆轮变换:

    • 类似于初始轮,使用 always 块执行逆初始轮操作。在状态为 22 时,执行密文和第十轮密钥的异或操作,并将结果存储在 mid_value_1 中。
    • 使用 generate 区块创建逆轮变换。它包括一个循环,从第二轮到第九轮,与加密轮函数相反。每轮都使用名为 inv_aes_round 的子模块进行处理。
  10. 逆最后一轮:

    • 使用名为 inv_final_round 的子模块,将第一轮密钥和逆变换后的状态数据传递给它,生成恢复的明文 plaintext_1

这个模块实际上是一个高级的AES加密和解密模块,通过组合不同的轮函数来实现加密和解密过程。它依赖于子模块(aes_roundfinal_roundinv_aes_roundinv_final_round)来执行每个轮的操作,而这些子模块的实现需要在代码中提供。此外,模块还需要外部提供的密钥和状态信息来控制加密和解密过程。

2.aes_round是轮运行模块

  1. module aes_round (
  2. input clk,
  3. input rst,
  4. input wire [127:0] state, // 输入状态
  5. input wire [127:0] round_key, // 输入轮密钥
  6. output reg [127:0] next_state // 输出下一轮状态
  7. );
  8. // 定义中间信号
  9. wire [127:0] sub_bytes_out;
  10. wire [127:0] shift_rows_out;
  11. wire [127:0] mix_columns_out;
  12. wire [127:0] add_round_key_out;
  13. // 16个S盒子实例化
  14. genvar j;
  15. generate
  16. for (j = 0; j < 16; j = j + 1) begin
  17. s_box s_box_inst (
  18. .in_byte(state[8*j +: 8]),
  19. .out_byte(sub_bytes_out[8*j +: 8])
  20. );
  21. end
  22. endgenerate
  23. // 行移位子模块实例化
  24. shift_rows shift_rows_inst (
  25. .in_state(sub_bytes_out),
  26. .out_state(shift_rows_out)
  27. );
  28. // 列混淆子模块实例化
  29. genvar i;
  30. generate
  31. for (i = 0; i < 4; i = i + 1) begin
  32. mix_col mix_col_inst (
  33. .s0(shift_rows_out[32*i +:8]),
  34. .s1(shift_rows_out[32*i+8 +:8]),
  35. .s2(shift_rows_out[32*i+16 +:8]),
  36. .s3(shift_rows_out[32*i+24 +:8]),
  37. .mix_col_0(mix_columns_out[32*i +:8]),
  38. .mix_col_1(mix_columns_out[32*i+8 +:8]),
  39. .mix_col_2(mix_columns_out[32*i+16 +:8]),
  40. .mix_col_3(mix_columns_out[32*i+24 +:8])
  41. );
  42. end
  43. endgenerate
  44. // 轮密钥加
  45. assign add_round_key_out = mix_columns_out ^ round_key;
  46. // 输出下一轮状态
  47. always @(posedge clk or negedge rst) begin
  48. if (!rst) begin
  49. next_state<=128'b0;
  50. end
  51. else begin
  52. next_state <= add_round_key_out;
  53. end
  54. end
  55. endmodule

3.下面是顶层模块

  1. module aes_top (
  2. input wire clk, // 时钟输入
  3. input wire rst // 复位输入
  4. );
  5. localparam key=128'h0123456789ABCDEF0123456789ABCDEF;
  6. localparam plaintext=128'h5E6C6C152BBC01B8961A2DED00822B0E;
  7. wire [5:0] state;
  8. wire [128*11-1:0]round_keys;
  9. wire [127:0]ciphertext;
  10. fsm fsm_inst(
  11. .clk(clk),
  12. .rst(rst),
  13. .state(state)
  14. );
  15. key_exp key_exp_inst(
  16. .clk(clk),
  17. .state(state),
  18. .rst(rst),
  19. .key(key),
  20. .round_key(round_keys)
  21. );
  22. aes_main aes_main_inst(
  23. .clk(clk),
  24. .rst(rst),
  25. .state(state),
  26. .round_keys(round_keys),
  27. .plaintext(plaintext),
  28. .ciphertext(ciphertext)
  29. );
  30. endmodule

4.最后一轮

  1. module final_round (
  2. input wire [127:0] state, // 输入状态
  3. input wire [127:0] round_key, // 输入轮密钥
  4. output wire [127:0] ciphertext // 输出下一轮状态
  5. );
  6. // 定义中间信号
  7. wire [127:0] sub_bytes_out;
  8. wire [127:0] shift_rows_out;
  9. // 16个S盒子实例化
  10. genvar i;
  11. generate
  12. for (i = 0; i < 16; i = i + 1) begin
  13. s_box s_box_inst (
  14. .in_byte(state[8*i +: 8]),
  15. .out_byte(sub_bytes_out[8*i +: 8])
  16. );
  17. end
  18. endgenerate
  19. // 行移位子模块实例化
  20. shift_rows shift_rows_inst (
  21. .in_state(sub_bytes_out),
  22. .out_state(shift_rows_out)
  23. );
  24. // 轮密钥加
  25. assign ciphertext = shift_rows_out ^ round_key;
  26. endmodule

5.写了个状态机表示state状态

  1. module fsm(
  2. input clk,
  3. input rst,
  4. output reg [5:0]state
  5. );
  6. always @(posedge clk or negedge rst) begin
  7. if (!rst) begin
  8. state<=6'b0;
  9. end
  10. else begin
  11. state<=state+1'b1;
  12. end
  13. end
  14. endmodule

6.解密的轮模块

  1. module inv_aes_round (
  2. input clk,
  3. input rst,
  4. input wire [127:0] state, // 输入状态
  5. input wire [127:0] round_key, // 输入轮密钥
  6. output reg [127:0] next_state // 输出下一轮状态
  7. );
  8. // 定义中间信号
  9. wire [127:0] sub_bytes_out;
  10. wire [127:0] shift_rows_out;
  11. wire [127:0] mix_columns_out;
  12. wire [127:0] add_round_key_out;
  13. // 16个S盒子实例化
  14. genvar j;
  15. generate
  16. for (j = 0; j < 16; j = j + 1) begin
  17. inv_s_box inv_s_box_inst (
  18. .in_byte(shift_rows_out[8*j +: 8]),
  19. .out_byte(sub_bytes_out[8*j +: 8])
  20. );
  21. end
  22. endgenerate
  23. // 行移位子模块实例化
  24. inv_shift_rows inv_shift_rows_inst (
  25. .in_state(state),
  26. .out_state(shift_rows_out)
  27. );
  28. // 列混淆子模块实例化
  29. genvar i;
  30. generate
  31. for (i = 0; i < 4; i = i + 1) begin
  32. inv_mix_col inv_mix_col_inst (
  33. .s0(add_round_key_out[32*i +:8]),
  34. .s1(add_round_key_out[32*i+8 +:8]),
  35. .s2(add_round_key_out[32*i+16 +:8]),
  36. .s3(add_round_key_out[32*i+24 +:8]),
  37. .inv_mix_col_0(mix_columns_out[32*i +:8]),
  38. .inv_mix_col_1(mix_columns_out[32*i+8 +:8]),
  39. .inv_mix_col_2(mix_columns_out[32*i+16 +:8]),
  40. .inv_mix_col_3(mix_columns_out[32*i+24 +:8])
  41. );
  42. end
  43. endgenerate
  44. // 轮密钥加
  45. assign add_round_key_out = sub_bytes_out ^ round_key;
  46. // 输出下一轮状态
  47. always @(posedge clk or negedge rst) begin
  48. if (!rst) begin
  49. next_state<=128'b0;
  50. end
  51. else begin
  52. next_state <= mix_columns_out;
  53. end
  54. end
  55. endmodule

7.解密的最终轮模块

  1. module inv_final_round (
  2. input wire [127:0] state, // 输入状态
  3. input wire [127:0] round_key, // 输入轮密钥
  4. output wire [127:0] ciphertext // 输出下一轮状态
  5. );
  6. // 定义中间信号
  7. wire [127:0] sub_bytes_out;
  8. wire [127:0] shift_rows_out;
  9. // 16个S盒子实例化
  10. genvar i;
  11. generate
  12. for (i = 0; i < 16; i = i + 1) begin
  13. inv_s_box inv_s_box_inst (
  14. .in_byte(shift_rows_out[8*i +: 8]),
  15. .out_byte(sub_bytes_out[8*i +: 8])
  16. );
  17. end
  18. endgenerate
  19. // 行移位子模块实例化
  20. inv_shift_rows inv_shift_rows_inst (
  21. .in_state(state),
  22. .out_state(shift_rows_out)
  23. );
  24. // 轮密钥加
  25. assign ciphertext = sub_bytes_out ^ round_key;
  26. endmodule

8.逆列混合模块

  1. module inv_mix_col(
  2. input [7:0] s0, s1, s2, s3,
  3. output reg [7:0] inv_mix_col_0,
  4. output reg [7:0] inv_mix_col_1,
  5. output reg [7:0] inv_mix_col_2,
  6. output reg [7:0] inv_mix_col_3
  7. );
  8. //logic: decryption mixcolumns
  9. always @(s0,s1,s2,s3)begin
  10. inv_mix_col_0=pmul_e(s0)^pmul_b(s1)^pmul_d(s2)^pmul_9(s3);
  11. inv_mix_col_1=pmul_9(s0)^pmul_e(s1)^pmul_b(s2)^pmul_d(s3);
  12. inv_mix_col_2=pmul_d(s0)^pmul_9(s1)^pmul_e(s2)^pmul_b(s3);
  13. inv_mix_col_3=pmul_b(s0)^pmul_d(s1)^pmul_9(s2)^pmul_e(s3);
  14. end
  15. //function
  16. function [7:0] pmul_e;
  17. input [7:0] b;
  18. reg [7:0] two,four,eight;
  19. begin
  20. two=gf8_2(b);
  21. four=gf8_2(two);
  22. eight=gf8_2(four);
  23. pmul_e=eight^four^two;
  24. end
  25. endfunction
  26. function [7:0] pmul_9;
  27. input [7:0] b;
  28. reg [7:0] two,four,eight;
  29. begin
  30. two=gf8_2(b);
  31. four=gf8_2(two);
  32. eight=gf8_2(four);
  33. pmul_9=eight^b;
  34. end
  35. endfunction
  36. function [7:0] pmul_d;
  37. input [7:0] b;
  38. reg [7:0] two,four,eight;
  39. begin
  40. two=gf8_2(b);
  41. four=gf8_2(two);
  42. eight=gf8_2(four);
  43. pmul_d=eight^four^b;
  44. end
  45. endfunction
  46. function [7:0] pmul_b;
  47. input [7:0] b;
  48. reg [7:0] two,four,eight;
  49. begin
  50. two=gf8_2(b);
  51. four=gf8_2(two);
  52. eight=gf8_2(four);
  53. pmul_b=eight^two^b;
  54. end
  55. endfunction
  56. function [7:0] gf8_2;
  57. input [7:0] b;
  58. gf8_2={b[6:0],1'b0}^(8'h1b&{8{b[7]}});
  59. endfunction
  60. endmodule

9.逆s盒模块

  1. module inv_s_box(
  2. input [7:0] in_byte,
  3. output reg [7:0] out_byte
  4. );
  5. /*
  6. // 16x16 Inverse S-box lookup table
  7. reg [7:0] inv_s_box [0:255] = {
  8. 8'h52, 8'h09, 8'h6a, 8'hd5, 8'h30, 8'h36, 8'ha5, 8'h38, 8'hbf, 8'h40, 8'ha3, 8'h9e, 8'h81, 8'hf3, 8'hd7, 8'hfb,
  9. 8'h7c, 8'hE3, 8'h39, 8'h82, 8'h9b, 8'h2f, 8'hff, 8'h87, 8'h34, 8'h8e, 8'h43, 8'h44, 8'hc4, 8'hde, 8'hE9, 8'hcb,
  10. 8'h54, 8'h7b, 8'h94, 8'h32, 8'hA6, 8'hC2, 8'h23, 8'h3d, 8'hEE, 8'h4C, 8'h95, 8'h0B, 8'h42, 8'hFA, 8'hC3, 8'h4E,
  11. 8'h08, 8'h2E, 8'hA1, 8'h66, 8'h28, 8'hD9, 8'h24, 8'hB2, 8'h76, 8'h5B, 8'hA2, 8'h49, 8'h6D, 8'h8B, 8'hD1, 8'h25,
  12. 8'h72, 8'hF8, 8'hF6, 8'h64, 8'h86, 8'h68, 8'h98, 8'h16, 8'hD4, 8'hA4, 8'h5C, 8'hCC, 8'h5D, 8'h65, 8'hB6, 8'h92,
  13. 8'h6C, 8'h70, 8'h48, 8'h50, 8'hFD, 8'hED, 8'hB9, 8'hDA, 8'h5E, 8'h15, 8'h46, 8'h57, 8'hA7, 8'h8D, 8'h9D, 8'h84,
  14. 8'h90, 8'hD8, 8'hAB, 8'h00, 8'h8C, 8'hBC, 8'hD3, 8'h0A, 8'hF7, 8'hE4, 8'h58, 8'h05, 8'hB8, 8'hB3, 8'h45, 8'h06,
  15. 8'hD0, 8'h2C, 8'h1E, 8'h8F, 8'hCA, 8'h3F, 8'h0F, 8'h02, 8'hC1, 8'hAF, 8'hBD, 8'h03, 8'h01, 8'h13, 8'h8A, 8'h6B,
  16. 8'h3A, 8'h91, 8'h11, 8'h41, 8'h4F, 8'h67, 8'hDC, 8'hEA, 8'h97, 8'hF2, 8'hCF, 8'hCE, 8'hF0, 8'hB4, 8'hE6, 8'h73,
  17. 8'h96, 8'hAC, 8'h74, 8'h22, 8'hE7, 8'hAD, 8'h35, 8'h85, 8'hE2, 8'hF9, 8'h37, 8'hE8, 8'h1C, 8'h75, 8'hDF, 8'h6E,
  18. 8'h47, 8'hF1, 8'h1A, 8'h71, 8'h1D, 8'h29, 8'hC5, 8'h89, 8'h6F, 8'hB7, 8'h62, 8'h0E, 8'hAA, 8'h18, 8'hBE, 8'h1B,
  19. 8'hFC, 8'h56, 8'h3E, 8'h4B, 8'hC6, 8'hD2, 8'h79, 8'h20, 8'h9A, 8'hDB, 8'hC0, 8'hFE, 8'h78, 8'hCD, 8'h5A, 8'hF4,
  20. 8'h1F, 8'hDD, 8'hA8, 8'h33, 8'h88, 8'h07, 8'hC7, 8'h31, 8'hB1, 8'h12, 8'h10, 8'h59, 8'h27, 8'h80, 8'hEC, 8'h5F,
  21. 8'h60, 8'h51, 8'h7F, 8'hA9, 8'h19, 8'hB5, 8'h4A, 8'h0D, 8'h2D, 8'hE5, 8'h7A, 8'h9F, 8'h93, 8'hC9, 8'h9C, 8'hEF,
  22. 8'hA0, 8'hE0, 8'h3B, 8'h4D, 8'hAE, 8'h2A, 8'hF5, 8'hB0, 8'hC8, 8'hEB, 8'hBB, 8'h3C, 8'h83, 8'h53, 8'h99, 8'h61,
  23. 8'h17, 8'h2B, 8'h04, 8'h7E, 8'hBA, 8'h77, 8'hD6, 8'h26, 8'hE1, 8'h69, 8'h14, 8'h63, 8'h55, 8'h21, 8'h0C, 8'h7D
  24. };*/
  25. always @(in_byte) begin
  26. case (in_byte)
  27. 8'h00: out_byte = 8'h52;
  28. 8'h01: out_byte = 8'h09;
  29. 8'h02: out_byte = 8'h6A;
  30. 8'h03: out_byte = 8'hD5;
  31. 8'h04: out_byte = 8'h30;
  32. 8'h05: out_byte = 8'h36;
  33. 8'h06: out_byte = 8'hA5;
  34. 8'h07: out_byte = 8'h38;
  35. 8'h08: out_byte = 8'hBF;
  36. 8'h09: out_byte = 8'h40;
  37. 8'h0A: out_byte = 8'hA3;
  38. 8'h0B: out_byte = 8'h9E;
  39. 8'h0C: out_byte = 8'h81;
  40. 8'h0D: out_byte = 8'hF3;
  41. 8'h0E: out_byte = 8'hD7;
  42. 8'h0F: out_byte = 8'hFB;
  43. 8'h10: out_byte = 8'h7C;
  44. 8'h11: out_byte = 8'hE3;
  45. 8'h12: out_byte = 8'h39;
  46. 8'h13: out_byte = 8'h82;
  47. 8'h14: out_byte = 8'h9B;
  48. 8'h15: out_byte = 8'h2F;
  49. 8'h16: out_byte = 8'hFF;
  50. 8'h17: out_byte = 8'h87;
  51. 8'h18: out_byte = 8'h34;
  52. 8'h19: out_byte = 8'h8E;
  53. 8'h1A: out_byte = 8'h43;
  54. 8'h1B: out_byte = 8'h44;
  55. 8'h1C: out_byte = 8'hC4;
  56. 8'h1D: out_byte = 8'hDE;
  57. 8'h1E: out_byte = 8'hE9;
  58. 8'h1F: out_byte = 8'hCB;
  59. 8'h20: out_byte = 8'h54;
  60. 8'h21: out_byte = 8'h7B;
  61. 8'h22: out_byte = 8'h94;
  62. 8'h23: out_byte = 8'h32;
  63. 8'h24: out_byte = 8'hA6;
  64. 8'h25: out_byte = 8'hC2;
  65. 8'h26: out_byte = 8'h23;
  66. 8'h27: out_byte = 8'h3D;
  67. 8'h28: out_byte = 8'hEE;
  68. 8'h29: out_byte = 8'h4C;
  69. 8'h2A: out_byte = 8'h95;
  70. 8'h2B: out_byte = 8'h0B;
  71. 8'h2C: out_byte = 8'h42;
  72. 8'h2D: out_byte = 8'hFA;
  73. 8'h2E: out_byte = 8'hC3;
  74. 8'h2F: out_byte = 8'h4E;
  75. 8'h30: out_byte = 8'h08;
  76. 8'h31: out_byte = 8'h2E;
  77. 8'h32: out_byte = 8'hA1;
  78. 8'h33: out_byte = 8'h66;
  79. 8'h34: out_byte = 8'h28;
  80. 8'h35: out_byte = 8'hD9;
  81. 8'h36: out_byte = 8'h24;
  82. 8'h37: out_byte = 8'hB2;
  83. 8'h38: out_byte = 8'h76;
  84. 8'h39: out_byte = 8'h5B;
  85. 8'h3A: out_byte = 8'hA2;
  86. 8'h3B: out_byte = 8'h49;
  87. 8'h3C: out_byte = 8'h6D;
  88. 8'h3D: out_byte = 8'h8B;
  89. 8'h3E: out_byte = 8'hD1;
  90. 8'h3F: out_byte = 8'h25;
  91. 8'h40: out_byte = 8'h72;
  92. 8'h41: out_byte = 8'hF8;
  93. 8'h42: out_byte = 8'hF6;
  94. 8'h43: out_byte = 8'h64;
  95. 8'h44: out_byte = 8'h86;
  96. 8'h45: out_byte = 8'h68;
  97. 8'h46: out_byte = 8'h98;
  98. 8'h47: out_byte = 8'h16;
  99. 8'h48: out_byte = 8'hD4;
  100. 8'h49: out_byte = 8'hA4;
  101. 8'h4A: out_byte = 8'h5C;
  102. 8'h4B: out_byte = 8'hCC;
  103. 8'h4C: out_byte = 8'h5D;
  104. 8'h4D: out_byte = 8'h65;
  105. 8'h4E: out_byte = 8'hB6;
  106. 8'h4F: out_byte = 8'h92;
  107. 8'h50: out_byte = 8'h6C;
  108. 8'h51: out_byte = 8'h70;
  109. 8'h52: out_byte = 8'h48;
  110. 8'h53: out_byte = 8'h50;
  111. 8'h54: out_byte = 8'hFD;
  112. 8'h55: out_byte = 8'hED;
  113. 8'h56: out_byte = 8'hB9;
  114. 8'h57: out_byte = 8'hDA;
  115. 8'h58: out_byte = 8'h5E;
  116. 8'h59: out_byte = 8'h15;
  117. 8'h5A: out_byte = 8'h46;
  118. 8'h5B: out_byte = 8'h57;
  119. 8'h5C: out_byte = 8'hA7;
  120. 8'h5D: out_byte = 8'h8D;
  121. 8'h5E: out_byte = 8'h9D;
  122. 8'h5F: out_byte = 8'h84;
  123. 8'h60: out_byte = 8'h90;
  124. 8'h61: out_byte = 8'hD8;
  125. 8'h62: out_byte = 8'hAB;
  126. 8'h63: out_byte = 8'h00;
  127. 8'h64: out_byte = 8'h8C;
  128. 8'h65: out_byte = 8'hBC;
  129. 8'h66: out_byte = 8'hD3;
  130. 8'h67: out_byte = 8'h0A;
  131. 8'h68: out_byte = 8'hF7;
  132. 8'h69: out_byte = 8'hE4;
  133. 8'h6A: out_byte = 8'h58;
  134. 8'h6B: out_byte = 8'h05;
  135. 8'h6C: out_byte = 8'hB8;
  136. 8'h6D: out_byte = 8'hB3;
  137. 8'h6E: out_byte = 8'h45;
  138. 8'h6F: out_byte = 8'h06;
  139. 8'h70: out_byte = 8'hD0;
  140. 8'h71: out_byte = 8'h2C;
  141. 8'h72: out_byte = 8'h1E;
  142. 8'h73: out_byte = 8'h8F;
  143. 8'h74: out_byte = 8'hCA;
  144. 8'h75: out_byte = 8'h3F;
  145. 8'h76: out_byte = 8'h0F;
  146. 8'h77: out_byte = 8'h02;
  147. 8'h78: out_byte = 8'hC1;
  148. 8'h79: out_byte = 8'hAF;
  149. 8'h7A: out_byte = 8'hBD;
  150. 8'h7B: out_byte = 8'h03;
  151. 8'h7C: out_byte = 8'h01;
  152. 8'h7D: out_byte = 8'h13;
  153. 8'h7E: out_byte = 8'h8A;
  154. 8'h7F: out_byte = 8'h6B;
  155. 8'h80: out_byte = 8'h3A;
  156. 8'h81: out_byte = 8'h91;
  157. 8'h82: out_byte = 8'h11;
  158. 8'h83: out_byte = 8'h41;
  159. 8'h84: out_byte = 8'h4F;
  160. 8'h85: out_byte = 8'h67;
  161. 8'h86: out_byte = 8'hDC;
  162. 8'h87: out_byte = 8'hEA;
  163. 8'h88: out_byte = 8'h97;
  164. 8'h89: out_byte = 8'hF2;
  165. 8'h8A: out_byte = 8'hCF;
  166. 8'h8B: out_byte = 8'hCE;
  167. 8'h8C: out_byte = 8'hF0;
  168. 8'h8D: out_byte = 8'hB4;
  169. 8'h8E: out_byte = 8'hE6;
  170. 8'h8F: out_byte = 8'h73;
  171. 8'h90: out_byte = 8'h96;
  172. 8'h91: out_byte = 8'hAC;
  173. 8'h92: out_byte = 8'h74;
  174. 8'h93: out_byte = 8'h22;
  175. 8'h94: out_byte = 8'hE7;
  176. 8'h95: out_byte = 8'hAD;
  177. 8'h96: out_byte = 8'h35;
  178. 8'h97: out_byte = 8'h85;
  179. 8'h98: out_byte = 8'hE2;
  180. 8'h99: out_byte = 8'hF9;
  181. 8'h9A: out_byte = 8'h37;
  182. 8'h9B: out_byte = 8'hE8;
  183. 8'h9C: out_byte = 8'h1C;
  184. 8'h9D: out_byte = 8'h75;
  185. 8'h9E: out_byte = 8'hDF;
  186. 8'h9F: out_byte = 8'h6E;
  187. 8'hA0: out_byte = 8'h47;
  188. 8'hA1: out_byte = 8'hF1;
  189. 8'hA2: out_byte = 8'h1A;
  190. 8'hA3: out_byte = 8'h71;
  191. 8'hA4: out_byte = 8'h1D;
  192. 8'hA5: out_byte = 8'h29;
  193. 8'hA6: out_byte = 8'hC5;
  194. 8'hA7: out_byte = 8'h89;
  195. 8'hA8: out_byte = 8'h6F;
  196. 8'hA9: out_byte = 8'hB7;
  197. 8'hAA: out_byte = 8'h62;
  198. 8'hAB: out_byte = 8'h0E;
  199. 8'hAC: out_byte = 8'hAA;
  200. 8'hAD: out_byte = 8'h18;
  201. 8'hAE: out_byte = 8'hBE;
  202. 8'hAF: out_byte = 8'h1B;
  203. 8'hB0: out_byte = 8'hFC;
  204. 8'hB1: out_byte = 8'h56;
  205. 8'hB2: out_byte = 8'h3E;
  206. 8'hB3: out_byte = 8'h4B;
  207. 8'hB4: out_byte = 8'hC6;
  208. 8'hB5: out_byte = 8'hD2;
  209. 8'hB6: out_byte = 8'h79;
  210. 8'hB7: out_byte = 8'h20;
  211. 8'hB8: out_byte = 8'h9A;
  212. 8'hB9: out_byte = 8'hDB;
  213. 8'hBA: out_byte = 8'hC0;
  214. 8'hBB: out_byte = 8'hFE;
  215. 8'hBC: out_byte = 8'h78;
  216. 8'hBD: out_byte = 8'hCD;
  217. 8'hBE: out_byte = 8'h5A;
  218. 8'hBF: out_byte = 8'hF4;
  219. 8'hC0: out_byte = 8'h1F;
  220. 8'hC1: out_byte = 8'hDD;
  221. 8'hC2: out_byte = 8'hA8;
  222. 8'hC3: out_byte = 8'h33;
  223. 8'hC4: out_byte = 8'h88;
  224. 8'hC5: out_byte = 8'h07;
  225. 8'hC6: out_byte = 8'hC7;
  226. 8'hC7: out_byte = 8'h31;
  227. 8'hC8: out_byte = 8'hB1;
  228. 8'hC9: out_byte = 8'h12;
  229. 8'hCA: out_byte = 8'h10;
  230. 8'hCB: out_byte = 8'h59;
  231. 8'hCC: out_byte = 8'h27;
  232. 8'hCD: out_byte = 8'h80;
  233. 8'hCE: out_byte = 8'hEC;
  234. 8'hCF: out_byte = 8'h5F;
  235. 8'hD0: out_byte = 8'h60;
  236. 8'hD1: out_byte = 8'h51;
  237. 8'hD2: out_byte = 8'h7F;
  238. 8'hD3: out_byte = 8'hA9;
  239. 8'hD4: out_byte = 8'h19;
  240. 8'hD5: out_byte = 8'hB5;
  241. 8'hD6: out_byte = 8'h4A;
  242. 8'hD7: out_byte = 8'h0D;
  243. 8'hD8: out_byte = 8'h2D;
  244. 8'hD9: out_byte = 8'hE5;
  245. 8'hDA: out_byte = 8'h7A;
  246. 8'hDB: out_byte = 8'h9F;
  247. 8'hDC: out_byte = 8'h93;
  248. 8'hDD: out_byte = 8'hC9;
  249. 8'hDE: out_byte = 8'h9C;
  250. 8'hDF: out_byte = 8'hEF;
  251. 8'hE0: out_byte = 8'hA0;
  252. 8'hE1: out_byte = 8'hE0;
  253. 8'hE2: out_byte = 8'h3B;
  254. 8'hE3: out_byte = 8'h4D;
  255. 8'hE4: out_byte = 8'hAE;
  256. 8'hE5: out_byte = 8'h2A;
  257. 8'hE6: out_byte = 8'hF5;
  258. 8'hE7: out_byte = 8'hB0;
  259. 8'hE8: out_byte = 8'hC8;
  260. 8'hE9: out_byte = 8'hEB;
  261. 8'hEA: out_byte = 8'hBB;
  262. 8'hEB: out_byte = 8'h3C;
  263. 8'hEC: out_byte = 8'h83;
  264. 8'hED: out_byte = 8'h53;
  265. 8'hEE: out_byte = 8'h99;
  266. 8'hEF: out_byte = 8'h61;
  267. 8'hF0: out_byte = 8'h17;
  268. 8'hF1: out_byte = 8'h2B;
  269. 8'hF2: out_byte = 8'h04;
  270. 8'hF3: out_byte = 8'h7E;
  271. 8'hF4: out_byte = 8'hBA;
  272. 8'hF5: out_byte = 8'h77;
  273. 8'hF6: out_byte = 8'hD6;
  274. 8'hF7: out_byte = 8'h26;
  275. 8'hF8: out_byte = 8'hE1;
  276. 8'hF9: out_byte = 8'h69;
  277. 8'hFA: out_byte = 8'h14;
  278. 8'hFB: out_byte = 8'h63;
  279. 8'hFC: out_byte = 8'h55;
  280. 8'hFD: out_byte = 8'h21;
  281. 8'hFE: out_byte = 8'h0C;
  282. 8'hFF: out_byte = 8'h7D;
  283. default: out_byte = 8'h00;
  284. endcase
  285. end
  286. endmodule

10.逆行移位模块

  1. module inv_shift_rows(
  2. input [127:0] in_state, // 输入状态矩阵,每行32位,总共4
  3. output reg [127:0] out_state // 输出状态矩阵
  4. );
  5. // 定义每一列的信号,从上而下,从左到右
  6. reg [31:0] col0, col1, col2, col3;
  7. // 从输入状态中提取每一列的每个字节
  8. always @(*) begin
  9. col0 = in_state[31:0];
  10. col1 = in_state[63:32];
  11. col2 = in_state[95:64];
  12. col3 = in_state[127:96];
  13. end
  14. // 行移位操作
  15. always @(*) begin
  16. out_state[7:0] = col0[7:0];
  17. out_state[15:8] = col3[15:8];
  18. out_state[23:16] = col2[23:16];
  19. out_state[31:24] = col1[31:24];
  20. out_state[39:32] = col1[7:0];
  21. out_state[47:40] = col0[15:8];
  22. out_state[55:48] = col3[23:16];
  23. out_state[63:56] = col2[31:24];
  24. out_state[71:64] = col2[7:0];
  25. out_state[79:72] = col1[15:8];
  26. out_state[87:80] = col0[23:16];
  27. out_state[95:88] = col3[31:24];
  28. out_state[103:96] = col3[7:0];
  29. out_state[111:104] = col2[15:8];
  30. out_state[119:112] = col1[23:16];
  31. out_state[127:120] = col0[31:24];
  32. end
  33. endmodule

11.密钥拓展模块

  1. module key_exp(
  2. input clk,
  3. input rst,
  4. input [5:0]state,
  5. input [127:0] key, // 输入的128位主密钥
  6. output [128*11-1:0] round_key // 输出的轮密钥数组,共11个轮密钥
  7. );
  8. // 定义局部参数
  9. localparam rcon0 = 32'h01000000;
  10. localparam rcon1 = 32'h02000000;
  11. localparam rcon2 = 32'h04000000;
  12. localparam rcon3 = 32'h08000000;
  13. localparam rcon4 = 32'h10000000;
  14. localparam rcon5 = 32'h20000000;
  15. localparam rcon6 = 32'h40000000;
  16. localparam rcon7 = 32'h80000000;
  17. localparam rcon8 = 32'h1b000000;
  18. localparam rcon9 = 32'h36000000;
  19. wire [31:0]subword;
  20. wire [31:0]tmp_w;
  21. reg [127:0] round_keys [0:10];// 输出的轮密钥数组,共11个轮密钥
  22. reg [31:0] w[0:3]; // 用于临时存储中间结果的寄存器数组
  23. // 初始化轮密钥数组的前4个轮密钥为主密钥的前4个字
  24. always @(posedge clk or negedge rst)begin
  25. if (!rst) begin
  26. w[0]=32'b0;
  27. end
  28. else begin
  29. case (state)
  30. 0: w[0] <= key[127:96]; // 根据 state 选择不同的 rcon 值
  31. 1: w[0] <= w[0] ^ subword ^ rcon0;
  32. 2: w[0] <= w[0] ^ subword ^ rcon1;
  33. 3: w[0] <= w[0] ^ subword ^ rcon2;
  34. 4: w[0] <= w[0] ^ subword ^ rcon3;
  35. 5: w[0] <= w[0] ^ subword ^ rcon4;
  36. 6: w[0] <= w[0] ^ subword ^ rcon5;
  37. 7: w[0] <= w[0] ^ subword ^ rcon6;
  38. 8: w[0] <= w[0] ^ subword ^ rcon7;
  39. 9: w[0] <= w[0] ^ subword ^ rcon8;
  40. 10: w[0] <= w[0] ^ subword ^ rcon9;
  41. default: w[0] <= 32'b0; // 默认情况下设置为 0
  42. endcase
  43. end
  44. end
  45. always @(posedge clk or negedge rst)begin
  46. if (!rst) begin
  47. w[1]=32'b0;
  48. end
  49. else begin
  50. case (state)
  51. 0: w[1] <= key[95:64]; // 根据 state 选择不同的 rcon 值
  52. 1: w[1] <= w[0]^w[1]^subword^rcon0;
  53. 2: w[1] <= w[0]^w[1]^subword^rcon1;
  54. 3: w[1] <= w[0]^w[1]^subword^rcon2;
  55. 4: w[1] <= w[0]^w[1]^subword^rcon3;
  56. 5: w[1] <= w[0]^w[1]^subword^rcon4;
  57. 6: w[1] <= w[0]^w[1]^subword^rcon5;
  58. 7: w[1] <= w[0]^w[1]^subword^rcon6;
  59. 8: w[1] <= w[0]^w[1]^subword^rcon7;
  60. 9: w[1] <= w[0]^w[1]^subword^rcon8;
  61. 10: w[1] <= w[0]^w[1]^subword^rcon9;
  62. default: w[1] <= 32'b0; // 默认情况下设置为 0
  63. endcase
  64. end
  65. end
  66. always @(posedge clk or negedge rst)begin
  67. if (!rst) begin
  68. w[2]=32'b0;
  69. end
  70. else begin
  71. case (state)
  72. 0: w[2] <= key[63:32]; // 根据 state 选择不同的 rcon 值
  73. 1: w[2] <= w[2]^w[0]^w[1]^subword^rcon0;
  74. 2: w[2] <= w[2]^w[0]^w[1]^subword^rcon1;
  75. 3: w[2] <= w[2]^w[0]^w[1]^subword^rcon2;
  76. 4: w[2] <= w[2]^w[0]^w[1]^subword^rcon3;
  77. 5: w[2] <= w[2]^w[0]^w[1]^subword^rcon4;
  78. 6: w[2] <= w[2]^w[0]^w[1]^subword^rcon5;
  79. 7: w[2] <= w[2]^w[0]^w[1]^subword^rcon6;
  80. 8: w[2] <= w[2]^w[0]^w[1]^subword^rcon7;
  81. 9: w[2] <= w[2]^w[0]^w[1]^subword^rcon8;
  82. 10: w[2] <= w[2]^w[0]^w[1]^subword^rcon9;
  83. default: w[2] <= 32'b0; // 默认情况下设置为 0
  84. endcase
  85. end
  86. end
  87. always @(posedge clk or negedge rst)begin
  88. if (!rst) begin
  89. w[3]=32'b0;
  90. end
  91. else begin
  92. case (state)
  93. 0: w[3] <= key[31:0]; // 根据 state 选择不同的 rcon 值
  94. 1: w[3] <= w[3]^w[2]^w[0]^w[1]^subword^rcon0;
  95. 2: w[3] <= w[3]^w[2]^w[0]^w[1]^subword^rcon1;
  96. 3: w[3] <= w[3]^w[2]^w[0]^w[1]^subword^rcon2;
  97. 4: w[3] <= w[3]^w[2]^w[0]^w[1]^subword^rcon3;
  98. 5: w[3] <= w[3]^w[2]^w[0]^w[1]^subword^rcon4;
  99. 6: w[3] <= w[3]^w[2]^w[0]^w[1]^subword^rcon5;
  100. 7: w[3] <= w[3]^w[2]^w[0]^w[1]^subword^rcon6;
  101. 8: w[3] <= w[3]^w[2]^w[0]^w[1]^subword^rcon7;
  102. 9: w[3] <= w[3]^w[2]^w[0]^w[1]^subword^rcon8;
  103. 10: w[3] <= w[3]^w[2]^w[0]^w[1]^subword^rcon9;
  104. default: w[3] <= 32'b0; // 默认情况下设置为 0
  105. endcase
  106. end
  107. end
  108. assign tmp_w = w[3];
  109. s_box u0(.in_byte(tmp_w[23:16]), .out_byte(subword[31:24]));
  110. s_box u1(.in_byte(tmp_w[15:08]), .out_byte(subword[23:16]));
  111. s_box u2(.in_byte(tmp_w[07:00]), .out_byte(subword[15:08]));
  112. s_box u3(.in_byte(tmp_w[31:24]), .out_byte(subword[07:00]));
  113. always @(posedge clk or negedge rst) begin
  114. if (!rst) begin
  115. round_keys[0]=128'b0;
  116. round_keys[1]=128'b0;
  117. round_keys[2]=128'b0;
  118. round_keys[3]=128'b0;
  119. round_keys[4]=128'b0;
  120. round_keys[5]=128'b0;
  121. round_keys[6]=128'b0;
  122. round_keys[7]=128'b0;
  123. round_keys[8]=128'b0;
  124. round_keys[9]=128'b0;
  125. round_keys[10]=128'b0;
  126. end
  127. else begin
  128. case (state)
  129. 1: round_keys[0] <= {w[0],w[1],w[2],w[3]};
  130. 2: round_keys[1] <= {w[0],w[1],w[2],w[3]};
  131. 3: round_keys[2] <= {w[0],w[1],w[2],w[3]};
  132. 4: round_keys[3] <= {w[0],w[1],w[2],w[3]};
  133. 5: round_keys[4] <= {w[0],w[1],w[2],w[3]};
  134. 6: round_keys[5] <= {w[0],w[1],w[2],w[3]};
  135. 7: round_keys[6] <= {w[0],w[1],w[2],w[3]};
  136. 8: round_keys[7] <= {w[0],w[1],w[2],w[3]};
  137. 9: round_keys[8] <= {w[0],w[1],w[2],w[3]};
  138. 10: round_keys[9] <= {w[0],w[1],w[2],w[3]};
  139. 11: round_keys[10] <= {w[0],w[1],w[2],w[3]};
  140. endcase
  141. end
  142. end
  143. assign round_key={round_keys[10],round_keys[9],round_keys[8],round_keys[7],round_keys[6],round_keys[5],round_keys[4],round_keys[3],round_keys[2],round_keys[1],round_keys[0]};
  144. endmodule

12.列混合模块

  1. module mix_col(
  2. input [7:0] s0, s1, s2, s3,
  3. output reg [7:0] mix_col_0,
  4. output reg [7:0] mix_col_1,
  5. output reg [7:0] mix_col_2,
  6. output reg [7:0] mix_col_3
  7. );
  8. //logic: decryption mixcolumns
  9. always @(s0,s1,s2,s3)begin
  10. mix_col_0=pmul_2(s0)^pmul_3(s1)^pmul_1(s2)^pmul_1(s3);
  11. mix_col_1=pmul_1(s0)^pmul_2(s1)^pmul_3(s2)^pmul_1(s3);
  12. mix_col_2=pmul_1(s0)^pmul_1(s1)^pmul_2(s2)^pmul_3(s3);
  13. mix_col_3=pmul_3(s0)^pmul_1(s1)^pmul_1(s2)^pmul_2(s3);
  14. end
  15. //function
  16. function [7:0] pmul_1;
  17. input [7:0] b;
  18. begin
  19. pmul_1=b;
  20. end
  21. endfunction
  22. function [7:0] pmul_3;
  23. input [7:0] b;
  24. reg [7:0] two;
  25. begin
  26. two=pmul_2(b);
  27. pmul_3=two^b;
  28. end
  29. endfunction
  30. function [7:0] pmul_2;
  31. input [7:0] b;
  32. pmul_2={b[6:0],1'b0}^(8'h1b&{8{b[7]}});
  33. endfunction
  34. endmodule

13.s盒子模块

  1. module s_box(
  2. input [7:0] in_byte,
  3. output reg [7:0] out_byte
  4. );
  5. // 256x8 S-box lookup table
  6. /*reg [7:0] s_box [0:255] = {
  7. 8'h63, 8'h7c, 8'h77, 8'h7b, 8'hf2, 8'h6b, 8'h6f, 8'hc5,
  8. 8'h30, 8'h01, 8'h67, 8'h2b, 8'hfe, 8'hd7, 8'hab, 8'h76,
  9. 8'hca, 8'h82, 8'hc9, 8'h7d, 8'hfa, 8'h59, 8'h47, 8'hf0,
  10. 8'had, 8'hd4, 8'ha2, 8'haf, 8'h9c, 8'ha4, 8'h72, 8'hc0,
  11. 8'hb7, 8'hfd, 8'h93, 8'h26, 8'h36, 8'h3f, 8'hf7, 8'hcc,
  12. 8'h34, 8'ha5, 8'he5, 8'hf1, 8'h71, 8'hd8, 8'h31, 8'h15,
  13. 8'h04, 8'hc7, 8'h23, 8'hc3, 8'h18, 8'h96, 8'h05, 8'h9a,
  14. 8'h07, 8'h12, 8'h80, 8'he2, 8'heb, 8'h27, 8'hb2, 8'h75,
  15. 8'h09, 8'h83, 8'h2c, 8'h1a, 8'h1b, 8'h6e, 8'h5a, 8'ha0,
  16. 8'h52, 8'h3b, 8'hd6, 8'hb3, 8'h29, 8'he3, 8'h2f, 8'h84,
  17. 8'h53, 8'hd1, 8'h00, 8'hed, 8'h20, 8'hfc, 8'hb1, 8'h5b,
  18. 8'h6a, 8'hcb, 8'hbe, 8'h39, 8'h4a, 8'h4c, 8'h58, 8'hcf,
  19. 8'hd0, 8'hef, 8'haa, 8'hfb, 8'h43, 8'h4d, 8'h33, 8'h85,
  20. 8'h45, 8'hf9, 8'h02, 8'h7f, 8'h50, 8'h3c, 8'h9f, 8'ha8,
  21. 8'h51, 8'ha3, 8'h40, 8'h8f, 8'h92, 8'h9d, 8'h38, 8'hf5,
  22. 8'hbc, 8'hb6, 8'hda, 8'h21, 8'h10, 8'hff, 8'hf3, 8'hd2,
  23. 8'hcd, 8'h0c, 8'h13, 8'hec, 8'h5f, 8'h97, 8'h44, 8'h17,
  24. 8'hc4, 8'ha7, 8'h7e, 8'h3d, 8'h64, 8'h5d, 8'h19, 8'h73,
  25. 8'h60, 8'h81, 8'h4f, 8'hdc, 8'h22, 8'h2a, 8'h90, 8'h88,
  26. 8'h46, 8'hee, 8'hb8, 8'h14, 8'hde, 8'h5e, 8'h0b, 8'hdb,
  27. 8'he0, 8'h32, 8'h3a, 8'h0a, 8'h49, 8'h06, 8'h24, 8'h5c,
  28. 8'hc2, 8'hd3, 8'hac, 8'h62, 8'h91, 8'h95, 8'he4, 8'h79,
  29. 8'he7, 8'hc8, 8'h37, 8'h6d, 8'h8d, 8'hd5, 8'h4e, 8'ha9,
  30. 8'h6c, 8'h56, 8'hf4, 8'hea, 8'h65, 8'h7a, 8'hae, 8'h08,
  31. 8'hba, 8'h78, 8'h25, 8'h2e, 8'h1c, 8'ha6, 8'hb4, 8'hc6,
  32. 8'he8, 8'hdd, 8'h74, 8'h1f, 8'h4b, 8'hbd, 8'h8b, 8'h8a,
  33. 8'h70, 8'h3e, 8'hb5, 8'h66, 8'h48, 8'h03, 8'hf6, 8'h0e,
  34. 8'h61, 8'h35, 8'h57, 8'hb9, 8'h86, 8'hc1, 8'h1d, 8'h9e,
  35. 8'he1, 8'hf8, 8'h98, 8'h11, 8'h69, 8'hd9, 8'h8e, 8'h94,
  36. 8'h9b, 8'h1e, 8'h87, 8'he9, 8'hce, 8'h55, 8'h28, 8'hdf,
  37. 8'h8c, 8'ha1, 8'h89, 8'h0d, 8'hbf, 8'he6, 8'h42, 8'h68,
  38. 8'h41, 8'h99, 8'h2d, 8'h0f, 8'hb0, 8'h54, 8'hbb, 8'h16
  39. };*/
  40. always @(in_byte) begin
  41. case (in_byte)
  42. 8'h00: out_byte = 8'h63;
  43. 8'h01: out_byte = 8'h7C;
  44. 8'h02: out_byte = 8'h77;
  45. 8'h03: out_byte = 8'h7B;
  46. 8'h04: out_byte = 8'hF2;
  47. 8'h05: out_byte = 8'h6B;
  48. 8'h06: out_byte = 8'h6F;
  49. 8'h07: out_byte = 8'hC5;
  50. 8'h08: out_byte = 8'h30;
  51. 8'h09: out_byte = 8'h01;
  52. 8'h0A: out_byte = 8'h67;
  53. 8'h0B: out_byte = 8'h2B;
  54. 8'h0C: out_byte = 8'hFE;
  55. 8'h0D: out_byte = 8'hD7;
  56. 8'h0E: out_byte = 8'hAB;
  57. 8'h0F: out_byte = 8'h76;
  58. 8'h10: out_byte = 8'hCA;
  59. 8'h11: out_byte = 8'h82;
  60. 8'h12: out_byte = 8'hC9;
  61. 8'h13: out_byte = 8'h7D;
  62. 8'h14: out_byte = 8'hFA;
  63. 8'h15: out_byte = 8'h59;
  64. 8'h16: out_byte = 8'h47;
  65. 8'h17: out_byte = 8'hF0;
  66. 8'h18: out_byte = 8'hAD;
  67. 8'h19: out_byte = 8'hD4;
  68. 8'h1A: out_byte = 8'hA2;
  69. 8'h1B: out_byte = 8'hAF;
  70. 8'h1C: out_byte = 8'h9C;
  71. 8'h1D: out_byte = 8'hA4;
  72. 8'h1E: out_byte = 8'h72;
  73. 8'h1F: out_byte = 8'hC0;
  74. 8'h20: out_byte = 8'hB7;
  75. 8'h21: out_byte = 8'hFD;
  76. 8'h22: out_byte = 8'h93;
  77. 8'h23: out_byte = 8'h26;
  78. 8'h24: out_byte = 8'h36;
  79. 8'h25: out_byte = 8'h3F;
  80. 8'h26: out_byte = 8'hF7;
  81. 8'h27: out_byte = 8'hCC;
  82. 8'h28: out_byte = 8'h34;
  83. 8'h29: out_byte = 8'hA5;
  84. 8'h2A: out_byte = 8'hE5;
  85. 8'h2B: out_byte = 8'hF1;
  86. 8'h2C: out_byte = 8'h71;
  87. 8'h2D: out_byte = 8'hD8;
  88. 8'h2E: out_byte = 8'h31;
  89. 8'h2F: out_byte = 8'h15;
  90. 8'h30: out_byte = 8'h04;
  91. 8'h31: out_byte = 8'hC7;
  92. 8'h32: out_byte = 8'h23;
  93. 8'h33: out_byte = 8'hC3;
  94. 8'h34: out_byte = 8'h18;
  95. 8'h35: out_byte = 8'h96;
  96. 8'h36: out_byte = 8'h05;
  97. 8'h37: out_byte = 8'h9A;
  98. 8'h38: out_byte = 8'h07;
  99. 8'h39: out_byte = 8'h12;
  100. 8'h3A: out_byte = 8'h80;
  101. 8'h3B: out_byte = 8'hE2;
  102. 8'h3C: out_byte = 8'hEB;
  103. 8'h3D: out_byte = 8'h27;
  104. 8'h3E: out_byte = 8'hB2;
  105. 8'h3F: out_byte = 8'h75;
  106. 8'h40: out_byte = 8'h09;
  107. 8'h41: out_byte = 8'h83;
  108. 8'h42: out_byte = 8'h2C;
  109. 8'h43: out_byte = 8'h1A;
  110. 8'h44: out_byte = 8'h1B;
  111. 8'h45: out_byte = 8'h6E;
  112. 8'h46: out_byte = 8'h5A;
  113. 8'h47: out_byte = 8'hA0;
  114. 8'h48: out_byte = 8'h52;
  115. 8'h49: out_byte = 8'h3B;
  116. 8'h4A: out_byte = 8'hD6;
  117. 8'h4B: out_byte = 8'hB3;
  118. 8'h4C: out_byte = 8'h29;
  119. 8'h4D: out_byte = 8'hE3;
  120. 8'h4E: out_byte = 8'h2F;
  121. 8'h4F: out_byte = 8'h84;
  122. 8'h50: out_byte = 8'h53;
  123. 8'h51: out_byte = 8'hD1;
  124. 8'h52: out_byte = 8'h00;
  125. 8'h53: out_byte = 8'hED;
  126. 8'h54: out_byte = 8'h20;
  127. 8'h55: out_byte = 8'hFC;
  128. 8'h56: out_byte = 8'hB1;
  129. 8'h57: out_byte = 8'h5B;
  130. 8'h58: out_byte = 8'h6A;
  131. 8'h59: out_byte = 8'hCB;
  132. 8'h5A: out_byte = 8'hBE;
  133. 8'h5B: out_byte = 8'h39;
  134. 8'h5C: out_byte = 8'h4A;
  135. 8'h5D: out_byte = 8'h4C;
  136. 8'h5E: out_byte = 8'h58;
  137. 8'h5F: out_byte = 8'hCF;
  138. 8'h60: out_byte = 8'hD0;
  139. 8'h61: out_byte = 8'hEF;
  140. 8'h62: out_byte = 8'hAA;
  141. 8'h63: out_byte = 8'hFB;
  142. 8'h64: out_byte = 8'h43;
  143. 8'h65: out_byte = 8'h4D;
  144. 8'h66: out_byte = 8'h33;
  145. 8'h67: out_byte = 8'h85;
  146. 8'h68: out_byte = 8'h45;
  147. 8'h69: out_byte = 8'hF9;
  148. 8'h6A: out_byte = 8'h02;
  149. 8'h6B: out_byte = 8'h7F;
  150. 8'h6C: out_byte = 8'h50;
  151. 8'h6D: out_byte = 8'h3C;
  152. 8'h6E: out_byte = 8'h9F;
  153. 8'h6F: out_byte = 8'hA8;
  154. 8'h70: out_byte = 8'h51;
  155. 8'h71: out_byte = 8'hA3;
  156. 8'h72: out_byte = 8'h40;
  157. 8'h73: out_byte = 8'h8F;
  158. 8'h74: out_byte = 8'h92;
  159. 8'h75: out_byte = 8'h9D;
  160. 8'h76: out_byte = 8'h38;
  161. 8'h77: out_byte = 8'hF5;
  162. 8'h78: out_byte = 8'hBC;
  163. 8'h79: out_byte = 8'hB6;
  164. 8'h7A: out_byte = 8'hDA;
  165. 8'h7B: out_byte = 8'h21;
  166. 8'h7C: out_byte = 8'h10;
  167. 8'h7D: out_byte = 8'hFF;
  168. 8'h7E: out_byte = 8'hF3;
  169. 8'h7F: out_byte = 8'hD2;
  170. 8'h80: out_byte = 8'hCD;
  171. 8'h81: out_byte = 8'h0C;
  172. 8'h82: out_byte = 8'h13;
  173. 8'h83: out_byte = 8'hEC;
  174. 8'h84: out_byte = 8'h5F;
  175. 8'h85: out_byte = 8'h97;
  176. 8'h86: out_byte = 8'h44;
  177. 8'h87: out_byte = 8'h17;
  178. 8'h88: out_byte = 8'hC4;
  179. 8'h89: out_byte = 8'hA7;
  180. 8'h8A: out_byte = 8'h7E;
  181. 8'h8B: out_byte = 8'h3D;
  182. 8'h8C: out_byte = 8'h64;
  183. 8'h8D: out_byte = 8'h5D;
  184. 8'h8E: out_byte = 8'h19;
  185. 8'h8F: out_byte = 8'h73;
  186. 8'h90: out_byte = 8'h60;
  187. 8'h91: out_byte = 8'h81;
  188. 8'h92: out_byte = 8'h4F;
  189. 8'h93: out_byte = 8'hDC;
  190. 8'h94: out_byte = 8'h22;
  191. 8'h95: out_byte = 8'h2A;
  192. 8'h96: out_byte = 8'h90;
  193. 8'h97: out_byte = 8'h88;
  194. 8'h98: out_byte = 8'h46;
  195. 8'h99: out_byte = 8'hEE;
  196. 8'h9A: out_byte = 8'hB8;
  197. 8'h9B: out_byte = 8'h14;
  198. 8'h9C: out_byte = 8'hDE;
  199. 8'h9D: out_byte = 8'h5E;
  200. 8'h9E: out_byte = 8'h0B;
  201. 8'h9F: out_byte = 8'hDB;
  202. 8'hA0: out_byte = 8'hE0;
  203. 8'hA1: out_byte = 8'h32;
  204. 8'hA2: out_byte = 8'h3A;
  205. 8'hA3: out_byte = 8'h0A;
  206. 8'hA4: out_byte = 8'h49;
  207. 8'hA5: out_byte = 8'h06;
  208. 8'hA6: out_byte = 8'h24;
  209. 8'hA7: out_byte = 8'h5C;
  210. 8'hA8: out_byte = 8'hC2;
  211. 8'hA9: out_byte = 8'hD3;
  212. 8'hAA: out_byte = 8'hAC;
  213. 8'hAB: out_byte = 8'h62;
  214. 8'hAC: out_byte = 8'h91;
  215. 8'hAD: out_byte = 8'h95;
  216. 8'hAE: out_byte = 8'hE4;
  217. 8'hAF: out_byte = 8'h79;
  218. 8'hB0: out_byte = 8'hE7;
  219. 8'hB1: out_byte = 8'hC8;
  220. 8'hB2: out_byte = 8'h37;
  221. 8'hB3: out_byte = 8'h6D;
  222. 8'hB4: out_byte = 8'h8D;
  223. 8'hB5: out_byte = 8'hD5;
  224. 8'hB6: out_byte = 8'h4E;
  225. 8'hB7: out_byte = 8'hA9;
  226. 8'hB8: out_byte = 8'h6C;
  227. 8'hB9: out_byte = 8'h56;
  228. 8'hBA: out_byte = 8'hF4;
  229. 8'hBB: out_byte = 8'hEA;
  230. 8'hBC: out_byte = 8'h65;
  231. 8'hBD: out_byte = 8'h7A;
  232. 8'hBE: out_byte = 8'hAE;
  233. 8'hBF: out_byte = 8'h08;
  234. 8'hC0: out_byte = 8'hBA;
  235. 8'hC1: out_byte = 8'h78;
  236. 8'hC2: out_byte = 8'h25;
  237. 8'hC3: out_byte = 8'h2E;
  238. 8'hC4: out_byte = 8'h1C;
  239. 8'hC5: out_byte = 8'hA6;
  240. 8'hC6: out_byte = 8'hB4;
  241. 8'hC7: out_byte = 8'hC6;
  242. 8'hC8: out_byte = 8'hE8;
  243. 8'hC9: out_byte = 8'hDD;
  244. 8'hCA: out_byte = 8'h74;
  245. 8'hCB: out_byte = 8'h1F;
  246. 8'hCC: out_byte = 8'h4B;
  247. 8'hCD: out_byte = 8'hBD;
  248. 8'hCE: out_byte = 8'h8B;
  249. 8'hCF: out_byte = 8'h8A;
  250. 8'hD0: out_byte = 8'h70;
  251. 8'hD1: out_byte = 8'h3E;
  252. 8'hD2: out_byte = 8'hB5;
  253. 8'hD3: out_byte = 8'h66;
  254. 8'hD4: out_byte = 8'h48;
  255. 8'hD5: out_byte = 8'h03;
  256. 8'hD6: out_byte = 8'hF6;
  257. 8'hD7: out_byte = 8'h0E;
  258. 8'hD8: out_byte = 8'h61;
  259. 8'hD9: out_byte = 8'h35;
  260. 8'hDA: out_byte = 8'h57;
  261. 8'hDB: out_byte = 8'hB9;
  262. 8'hDC: out_byte = 8'h86;
  263. 8'hDD: out_byte = 8'hC1;
  264. 8'hDE: out_byte = 8'h1D;
  265. 8'hDF: out_byte = 8'h9E;
  266. 8'hE0: out_byte = 8'hE1;
  267. 8'hE1: out_byte = 8'hF8;
  268. 8'hE2: out_byte = 8'h98;
  269. 8'hE3: out_byte = 8'h11;
  270. 8'hE4: out_byte = 8'h69;
  271. 8'hE5: out_byte = 8'hD9;
  272. 8'hE6: out_byte = 8'h8E;
  273. 8'hE7: out_byte = 8'h94;
  274. 8'hE8: out_byte = 8'h9B;
  275. 8'hE9: out_byte = 8'h1E;
  276. 8'hEA: out_byte = 8'h87;
  277. 8'hEB: out_byte = 8'hE9;
  278. 8'hEC: out_byte = 8'hCE;
  279. 8'hED: out_byte = 8'h55;
  280. 8'hEE: out_byte = 8'h28;
  281. 8'hEF: out_byte = 8'hDF;
  282. 8'hF0: out_byte = 8'h8C;
  283. 8'hF1: out_byte = 8'hA1;
  284. 8'hF2: out_byte = 8'h89;
  285. 8'hF3: out_byte = 8'h0D;
  286. 8'hF4: out_byte = 8'hBF;
  287. 8'hF5: out_byte = 8'hE6;
  288. 8'hF6: out_byte = 8'h42;
  289. 8'hF7: out_byte = 8'h68;
  290. 8'hF8: out_byte = 8'h41;
  291. 8'hF9: out_byte = 8'h99;
  292. 8'hFA: out_byte = 8'h2D;
  293. 8'hFB: out_byte = 8'h0F;
  294. 8'hFC: out_byte = 8'hB0;
  295. 8'hFD: out_byte = 8'h54;
  296. 8'hFE: out_byte = 8'hBB;
  297. 8'hFF: out_byte = 8'h16;
  298. default: out_byte = 8'h00;
  299. endcase
  300. end
  301. endmodule

14.行移位模块

  1. module shift_rows(
  2. input [127:0] in_state, // 输入状态矩阵,每行32位,总共4
  3. output reg [127:0] out_state // 输出状态矩阵
  4. );
  5. // 定义每一列的信号,从上而下,从左到右
  6. reg [31:0] col0, col1, col2, col3;
  7. // 从输入状态中提取每一列的每个字节
  8. always @(*) begin
  9. col0 = in_state[31:0];
  10. col1 = in_state[63:32];
  11. col2 = in_state[95:64];
  12. col3 = in_state[127:96];
  13. end
  14. // 行移位操作
  15. always @(*) begin
  16. out_state[7:0] = col0[7:0];
  17. out_state[15:8] = col1[15:8];
  18. out_state[23:16] = col2[23:16];
  19. out_state[31:24] = col3[31:24];
  20. out_state[39:32] = col1[7:0];
  21. out_state[47:40] = col2[15:8];
  22. out_state[55:48] = col3[23:16];
  23. out_state[63:56] = col0[31:24];
  24. out_state[71:64] = col2[7:0];
  25. out_state[79:72] = col3[15:8];
  26. out_state[87:80] = col0[23:16];
  27. out_state[95:88] = col1[31:24];
  28. out_state[103:96] = col3[7:0];
  29. out_state[111:104] = col0[15:8];
  30. out_state[119:112] = col1[23:16];
  31. out_state[127:120] = col2[31:24];
  32. end
  33. endmodule

15.testbench

  1. module testbench;
  2. reg clk; // 时钟信号
  3. reg rst; // 复位信号
  4. // 被测试的模块实例化
  5. aes_top aes_top_inst (
  6. .clk(clk),
  7. .rst(rst)
  8. );
  9. // 时钟生成
  10. always begin
  11. #5 clk = ~clk; // 周期为 10 个时间单位
  12. end
  13. // 模块输入数据生成
  14. initial begin
  15. clk = 0;
  16. rst = 1;
  17. // 等待一些时间来确保模块初始化完成
  18. #20;
  19. // 使能复位
  20. rst = 0;
  21. // 在时钟上升沿之前等待一些时间
  22. #20;
  23. // 停止复位
  24. rst = 1;
  25. end
  26. endmodule
结果:先加密,后解密,得到的plaintext_1 和原来的 plaintext是一样的,说明这个算法是有效的。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/856597
推荐阅读
相关标签
  

闽ICP备14008679号