Transform() 함수에서 사용되는 FF() 매크로 함수에는 다양한 내부 매크로 함수들이 존재한다.
그래서 FF() 함수를 구성하는 함수들을 모두 모듈로 만들었다.
우선 c코드로 FF()함수와 이를 구성하는 함수들을 살펴보면 다음과 같다.
#define FF(a, b, c, d, e, f, g, h, j) { \
T1 = h + Sigma1(e) + Ch(e, f, g) + SHA256_K[j] + X[j]; \
d += T1; \
h = T1 + Sigma0(a) + Maj(a, b, c); \
}
#if defined(_MSC_VER)
#define ROTL_ULONG(x, n) _lrotl((x), (n)) // rotate left
#define ROTR_ULONG(x, n) _lrotr((x), (n)) // rotate right
#else
#define ROTL_ULONG(x, n) ((ULONG)((x) << (n)) | (ULONG)((x) >> (32 - (n))))
#define ROTR_ULONG(x, n) ((ULONG)((x) >> (n)) | (ULONG)((x) << (32 - (n))))
#endif
////////////////////////////////////////////////////////////////////////////////
#define RR(x, n) ROTR_ULONG(x, n)
#define SS(x, n) (x >> n)
#define Ch(x, y, z) ((x & y) ^ ((~x) & z))
#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
#define Sigma0(x) (RR(x, 2) ^ RR(x, 13) ^ RR(x, 22))
#define Sigma1(x) (RR(x, 6) ^ RR(x, 11) ^ RR(x, 25))
#define RHO0(x) (RR(x, 7) ^ RR(x, 18) ^ SS(x, 3))
#define RHO1(x) (RR(x, 17) ^ RR(x, 19) ^ SS(x, 10))
* RTL
// rotate right
module calc_RR(
input wire [31:0] i_x,
input wire [4:0] i_n,
output wire [31:0] o_calc_x
);
//assign calc_x = (i_x>>i_n) | (i_x<<(64-i_n)); // 64bit
assign o_calc_x = (i_x>>i_n) | (i_x<<(32-i_n));
endmodule
module calc_SS(
input wire [31:0] i_x,
input wire [4:0] i_n,
output wire [31:0] o_calc_x
);
assign o_calc_x = i_x>>i_n;
endmodule
/////////////////////////////////////////////////////////////////////
module ch(
input wire [31:0] i_x,
input wire [31:0] i_y,
input wire [31:0] i_z,
output wire [31:0] o_data
);
assign o_data = (i_x & i_y) ^ ((~i_x) & i_z);
endmodule
module maj(
input wire [31:0] i_x,
input wire [31:0] i_y,
input wire [31:0] i_z,
output wire [31:0] o_data
);
assign o_data = (i_x & i_y) ^ (i_x & i_z) ^ (i_y & i_z);
endmodule
/////////////////////////////////////////////////////////////////////
module sigma0(
input wire [31:0] i_x,
output wire [31:0] o_x
);
wire [4:0] n0;
wire [4:0] n1;
wire [4:0] n2;
wire [31:0] calc_x0;
wire [31:0] calc_x1;
wire [31:0] calc_x2;
assign n0 = 'd2;
assign n1 = 'd13;
assign n2 = 'd22;
calc_RR U0_RR (
.i_x ( i_x ),
.i_n ( n0 ),
.o_calc_x ( calc_x0 )
);
calc_RR U1_RR (
.i_x ( i_x ),
.i_n ( n1 ),
.o_calc_x ( calc_x1 )
);
calc_RR U2_RR (
.i_x ( i_x ),
.i_n ( n2 ),
.o_calc_x ( calc_x2 )
);
assign o_x = calc_x0^calc_x1^calc_x2; // XOR
endmodule
module sigma1(
input wire [31:0] i_x,
output wire [31:0] o_x
);
wire [4:0] n0;
wire [4:0] n1;
wire [4:0] n2;
wire [31:0] calc_x0;
wire [31:0] calc_x1;
wire [31:0] calc_x2;
assign n0 = 'd6;
assign n1 = 'd11;
assign n2 = 'd25;
calc_RR U0_RR (
.i_x ( i_x ),
.i_n ( n0 ),
.o_calc_x ( calc_x0 )
);
calc_RR U1_RR (
.i_x ( i_x ),
.i_n ( n1 ),
.o_calc_x ( calc_x1 )
);
calc_RR U2_RR (
.i_x ( i_x ),
.i_n ( n2 ),
.o_calc_x ( calc_x2 )
);
assign o_x = calc_x0^calc_x1^calc_x2; // XOR
endmodule
/////////////////////////////////////////////////////////////////////
module calc_RHO0(
input wire [31:0] i_x,
output wire [31:0] o_x
);
wire [4:0] n0;
wire [4:0] n1;
wire [4:0] n2;
wire [31:0] calc_x0;
wire [31:0] calc_x1;
wire [31:0] calc_x2;
assign n0 = 'd7;
assign n1 = 'd18;
assign n2 = 'd3;
calc_RR U0_RR (
.i_x ( i_x ),
.i_n ( n0 ),
.o_calc_x ( calc_x0 )
);
calc_RR U1_RR (
.i_x ( i_x ),
.i_n ( n1 ),
.o_calc_x ( calc_x1 )
);
calc_SS U0_SS (
.i_x ( x ),
.i_n ( n2 ),
.o_calc_x ( calc_x2 )
);
assign o_x = calc_x0^calc_x1^calc_x2; // XOR
endmodule
module calc_RHO1(
input wire [31:0] i_x,
output wire [31:0] o_x
);
wire [4:0] n0;
wire [4:0] n1;
wire [4:0] n2;
wire [31:0] calc_x0;
wire [31:0] calc_x1;
wire [31:0] calc_x2;
assign n0 = 'd17;
assign n1 = 'd19;
assign n2 = 'd10;
calc_RR U0_RR (
.i_x ( i_x ),
.i_n ( n0 ),
.o_calc_x ( calc_x0 )
);
calc_RR U1_RR (
.i_x ( i_x ),
.i_n ( n1 ),
.o_calc_x ( calc_x1 )
);
calc_SS U0_SS (
.i_x ( x ),
.i_n ( n2 ),
.o_calc_x ( calc_x2 )
);
assign o_x = calc_x0^calc_x1^calc_x2; // XOR
endmodule
- 참고 -
https://seed.kisa.or.kr/kisa/Board/21/detailView.do
'Hardware Security' 카테고리의 다른 글
[SHA256] FF 모듈 (0) | 2022.02.23 |
---|---|
[SHA256] FF() 구성 모듈 - (1): RR, Sigma0, Sigma1 (0) | 2022.02.04 |
RR(Rotate Right) (0) | 2022.02.04 |
[SHA256][Verilog HDL] 코드 만들기 (0) | 2021.11.18 |