AES 加密算法,sv实现

本文摘抄自:https://blog.csdn.net/gulang03/article/details/81175854 

代码是根据参考的博客中的算法自己手撸的。

  1 typedef bit[3:0][7:0] Matrix[4]; 
  2 class aes_encrypt;
  3    bit[15:0][7:0] SBLOCK[16] ='{'{8'h76,
  4    local Matrix key;
  5    local Matrix big_key[11];
  6    function new();
  7      key='{default:0};
  8      big_key='{default:0};
  9    endfunction
 10    extern local function Matrix key_xor(input Matrix tkey,input Matrix data);
 11    extern local function Matrix byte_transfer(input Matrix data);
 12    extern local function Matrix row_shift(input Matrix data);
 13    extern local function Matrix col_mix(Matrix data);
 14    extern local function bit[7:0] field_multi(input bit[7:0] val,input bit[7:0] m);
 15    extern local function bit[7:0] field_multi2(input bit[7:0] val);
 16    extern local function void extend_key();
 17    extern local function bit[3:0][7:0] T(input bit[3:0][7:0] W, input bit[7:0] t);
 18    extern local function Matrix get_ciphertext(input Matrix text);
 19    //extern function bit[127:0] get_ciphertext_array(input bit[127:0] text);
 20    extern function void get_key(bit[127:0] val);
 21    //extern function void print(Matrix val,string str);
 22 endclass
 23 function Matrix aes_encrypt::byte_transfer(input Matrix data);
 24   Matrix ret;
 25   for(int i=0; i<4;i++) begin
 26     for(int j=0;j<4;j++) begin
 27       ret[i][j]=SBLOCK[data[i][j][7:4]][data[i][j][3:0]];
 28     end
 29   end
 30   return ret;
 31 endfunction
 32 
 33 function Matrix aes_encrypt::row_shift(input Matrix data);
 34   Matrix ret;
 35   ret[0]=data[0];
 36   ret[1]={data[1][0],data[1][3],data[1][2],data[1][1]};
 37   ret[2]={data[2][1],data[2][0],data[2][3],data[2][2]};
 38   ret[3]={data[3][2],data[3][1],data[3][0],data[3][3]};
 39   return ret;
 40 endfunction
 41 function Matrix aes_encrypt::col_mix(Matrix data);
 42   Matrix ret;
 43   Matrix const_array='{'{8'h01,8'h01,8'h03,8'h02},
 44                '{8'h01,8'h03,8'h02,8'h01},
 45                '{8'h03,8'h02,8'h01,8'h01),
 46                '{8'h02,8'h01,8'h01,8'h03}};
 47   for(int i=0;i<4;i++)begin
 48     for(int j=0;j<4;j++) begin
 49       for(int m=0;m<4;m++) begin
 50         ret[i][j]=ret[i][j]^field_multi(data[m][j],const_array[i][m]);
 51       end
 52     end
 53   end
 54   return ret;
 55 enfunction
 56 
 57 function bit[7:0] aes_encrypt::field_multi(input bit[7:0] val, input bit[7:0] m);
 58   bit[7:0] result=8'h0;
 59   result=m[0]?val:8'd0;
 60   for(int i=1;i<8;i++) begin
 61     if(m[i]==1) begin
 62       for(int t=0;t<i;t++) begin
 63         result=result^field_multi2(val);
 64       end
 65     end
 66   end
 67   return result;
 68 endfunction
 69 
 70 function bit[7:0] aes_encrypt::field_multi2(input bit[7:0] val);
 71   return (val[7]!=1)?(val<<1):((val<<1)^8'h1b);
 72 endfunction
 73 
 74 function aes_encrypt::extend_key();
 75   bit[3:0][7:0] ww;
 76   big_key[0]=key;
 77   for(int m=1;m<11;m++) begin
 78     for(int j=0;j<4;j++) begin
 79       for(int i=0;i<4;i++) begin
 80         if(j==0) begin
 81           ww={big_key[m-1][0][3],big_key[m-1][1][3],big_key[m-1][2][3],big_key[m-1][3][3]};
 82           ww=T(ww,m);
 83           big_key[m][i][j]=big_key[m-1][i][j]^ww[3-i];
 84         end else begin
 85           big_key[m][i][j]=big_key[m-1][i][j]^big_key[m][i][j-1];
 86         end
 87       end
 88     end
 89   end
 90 endfunction
 91 function bit[3:0][7:0] aes_encrypt::T(input bit[3:0][7:0] W, input bit[7:0] t);
 92   bit[3:0][7:0] ret;
 93   ret={W[2],W[1],W[0],W[3]};
 94   for(int i=0;i<4;i++) begin
 95     ret[i]=SBLOCK[ret[i][7:4]][ret[i][3:0]];
 96   end
 97   case(t)
 98     8'd1: ret=ret^32'h01000000;
 99     8'd2: ret=ret^32'h02000000;
100     8'd3: ret=ret^32'h04000000;
101     8'd4: ret=ret^32'h08000000;
102     8'd5: ret=ret^32'h10000000;
103     8'd6: ret=ret^32'h20000000;
104     8'd7: ret=ret^32'h40000000;
105     8'd8: ret=ret^32'h80000000;
106     8'd9: ret=ret^32'h1b000000;
107     8'd10: ret=ret^32'h36000000;
108   endcase
109   return ret;
110 endfunction
111 function Matrix aes_encrypt::get_ciphertext(input Matrix text);
112   Matrix ret;
113   extend_key();
114   ret=key_xor(key,text);
115   for(int i=1;i<11;i++) begin
116     ret=byte_transfer(ret);
117     ret=row_shift(ret);
118     if(i!=10)begin
119       ret=col_mix(ret);
120     end
121     ret=key_xor(big_key[i],ret);
122   end
123   return ret;
124 endfunction
125 function aes_encrypt:;get_key(bit[127:0] val)
126   //这个只能根据需要自己写了
127 endfunction

 

posted @ 2022-02-25 15:11  学号党  阅读(102)  评论(0编辑  收藏  举报