ciel.xia的个人空间 https://blog.eetop.cn/1544698 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

sha加密算法

已有 323 次阅读| 2018-8-1 15:36 |个人分类:代码带回家|系统分类:芯片设计


//sha256 20180801
`timescale 1ns/1ns
module SHA2560801 #(parameter SIZE=16)(data,zhaiyao,rst_n,n,clk);
 input [SIZE-1:0] data;
 input   rst_n;
 input   clk;
 input [63:0] n;//输入数据的位数,用64位数据表示
 output reg [255:0]  zhaiyao;
//得到常数H初值和常数K 
reg [31:0] K [0:63];
initial begin
  $readmemh ("C:/bangong/lianxi/data.txt",K);end
  //6a09e65d 6a09e667
 parameter  H00 = 32'h6a09e667 ,
    H11 = 32'hbb67ae85 ,
    H22 = 32'h3c6ef372 ,
    H33 = 32'ha54ff53a ,
    H44 = 32'h510e527f ,
    H55 = 32'h9b05688c ,
    H66 = 32'h1f83d9ab ,
    H77 = 32'h5be0cd19 ;
//数据补位
wire [511:0] data_buwei;
assign data_buwei = {data,1'b1,{(447-SIZE){1'b0}},n[63:0]};
//assign data_buwei = {16'b00110000_00110001,1'b1,{(448-16-1){1'b0}},32'b00000000000000000000000000010000};

//用到的函数
function  [31:0] ROTR;
 input [31:0] x;
 input [4:0] shift;
 begin
  ROTR = (x>>shift) | (x<<(5'd32-shift));
 end
endfunction
function  [31:0] WAJ;
 input [31:0] x;
 input [31:0] y;
 input [31:0] z;
 begin
  WAJ = (x & y) ^ (x & z) ^ (y & z);
 end
endfunction
function [31:0] CH;
 input [31:0] x;
 input [31:0] y;
 input [31:0] z;
 begin
  CH = (x & y)^ ((~x) & z);
 end
endfunction

function [31:0] SSIG0;
 input [31:0] x;
 begin
  SSIG0 = ROTR(x,5'd7)^ROTR(x,5'd18)^(x>>3);
 end
endfunction
function [31:0] SSIG1;
 input [31:0] x;
 begin
  SSIG1 = ROTR(x,5'd17)^ROTR(x,5'd19)^(x>>10);
 end
endfunction
function [31:0] BSIG0;
 input [31:0] x;
 begin
  BSIG0 = ROTR(x,5'd2)^ROTR(x,5'd13)^ROTR(x,5'd22);
 end
endfunction
function [31:0] BSIG1;
 input [31:0] x;
 begin
  BSIG1 = ROTR(x,5'd6)^ROTR(x,5'd11)^ROTR(x,5'd25);
 end
endfunction
//得到常数M
reg [31:0] M [15:0];
always@(posedge clk ) begin
 if(!rst_n)begin
  M[0] <= 32'h0;
  M[1] <= 32'h0;
  M[2] <= 32'h0;
  M[3] <= 32'h0;
  M[4] <= 32'h0;
  M[5] <= 32'h0;
  M[6] <= 32'h0;
  M[7] <= 32'h0;
  M[8] <= 32'h0;
  M[9] <= 32'h0;
  M[10] <= 32'h0;
  M[11] <= 32'h0;
  M[12] <= 32'h0;
  M[13] <= 32'h0;
  M[14] <= 32'h0;
  M[15] <= 32'h0;
 end
 else begin  
    M[0] <= data_buwei [511:480];
    M[1] <= data_buwei[479:448];
    M[2] <= data_buwei [447:416];
    M[3] <= data_buwei[415:384];   
    M[4] <= data_buwei [383:352];
    M[5] <= data_buwei[351:320];
    M[6] <= data_buwei [319:288];
    M[7] <= data_buwei[287:256];
    M[8] <= data_buwei [255:224];
    M[9] <= data_buwei[223:192];
    M[10] <= data_buwei [191:160];
    M[11] <= data_buwei[159:128];
    M[12] <= data_buwei [127:96];
    M[13] <= data_buwei[95:64];
    M[14] <= data_buwei [63:32];
    M[15] <= data_buwei[31:0];
 end
end 
//得到W
//分组后的每组512位值W0--W15
reg [31:0] W[63:0];
reg  [31:0] T1,T2;
reg  [31:0]a,b,c,d,e,f,g,h;
reg [6:0] count,count1;//循环64次运算
reg [31:0] H0;
reg [31:0] H1;
reg [31:0] H2;
reg [31:0] H3;
reg [31:0] H4;
reg [31:0] H5;
reg [31:0] H6;
reg [31:0] H7;
always@(posedge clk )
  if(!rst_n)
    count1<= 0;
  else if(count1 == 'd64)
    count1 <= 0;
  else
    count1 <= count1+1'b1;
/*always@(posedge clk ) begin  
  if(!rst_n)
    count2<= 0;
  else
    count2 <= count1;
end*/
always@(posedge clk ) begin  
  if(!rst_n)
    count<= 0;
  else
    count <= count1;
end
//求过程值a,b,c,d,e,f,g,h
//reg [31:0] u,v;//测试
always@(count1 or count )
 if(!rst_n) begin
    H0 = H00;
    H1 = H11;
    H2 = H22;
    H3 = H33;
    H4 = H44;
    H5 = H55;
    H6 = H66;
    H7 = H77;
    a = H0;
   b = H1;
   c = H2;
   d = H3;
   e = H4;
   f = H5;
   g = H6;
   h = H7;
  end
  
  else if(count=='d64)begin
    H0 = a+H0;
    H1 = b+H1;
    H2 = c+H2;
    H3 = d+H3;
    H4 = e+H4;
    H5 = f+H5;
    H6 = g+H6;
    H7 = h+H7;
    zhaiyao = {H0,H1,H2,H3,H4,H5,H6,H7};
   //disable machine;
  end
  else if(count<='d15)begin
  
    W[count] = M[count];
    T1 = h+BSIG1(e) + CH(e,f,g) + W[count]+K[count];
    T2 = BSIG0(a) + WAJ(a,b,c);
    h = g;
    g = f;
    f = e;
    e = d + T1;
    d = c;
    c = b;
    b = a;
    a = T1+T2;
  end 
  else begin
    //u = SSIG1(W[count-2]);
    //v = SSIG0(W[count-15]);
    W[count] = SSIG1(W[count-2])+W[count-7]+SSIG0(W[count-15])+W[count-16];
    T1 = h+BSIG1(e) + CH(e,f,g) + W[count]+K[count];
    T2 = BSIG0(a) + WAJ(a,b,c);
    h = g;
    g = f;
    f = e;
    e = d + T1;
    d = c;
    c = b;
    b = a;
    a = T1+T2;  
  end 
endmodule


点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 1

    粉丝
  • 0

    好友
  • 0

    获赞
  • 1

    评论
  • 访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-5-12 05:52 , Processed in 0.023845 second(s), 19 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部