[Hardware] RC5 on FPGA, updated results

Martin Klingensmith martin at nnytech.net
Tue Feb 27 11:48:34 EST 2007


John,
Do you mind explaining to me what the timing issue is with the pipeline? 
I don't understand what needs to occur for me to put in one key per 
clock cycle and get the correct value on the output. Here is my code - 
this is for 40 bit but IIRC would work for up to 64 bits.
--
Martin K

module pipe(A12,B12,L0_0,L1_0,clock,clr);
    output [31:0]A12,B12;
    input [31:0]L0_0,L1_0;
    input clock,clr;

    reg[31:0]
A, B, L0, L1, L2, S0, 
S1,f,g,L0_1,L0_2,L0_3,L0_4,L0_5,L0_6,L0_7,L0_8,L0_9,L0_10,L0_11,L0_12,L0_13,L0_14,L0_15,L0_16,L0_17,L0_18,L0_19,L0_20,L0_21,L0_22,L0_23,L0_24,L0_25,L0_26,L0_27,L0_28,L0_29,L0_30,L0_31,L0_32,L0_33,L0_34,L0_35,L0_36,L0_37,L0_38,L1_1,L1_2,L1_3,L1_4,L1_5,L1_6,L1_7,L1_8,L1_9,L1_10,L1_11,L1_12,L1_13,L1_14,L1_15,L1_16,L1_17,L1_18,L1_19,L1_20,L1_21,L1_22,L1_23,L1_24,L1_25,L1_26,L1_27,L1_28,L1_29,L1_30,L1_31,L1_32,L1_33,L1_34,L1_35,L1_36,L1_37,L1_38,L2_0,L2_1,L2_2,L2_3,L2_4,L2_5,L2_6,L2_7,L2_8,L2_9,L2_10,L2_11,L2_12,L2_13,L2_14,L2_15,L2_16,L2_17,L2_18,L2_19,L2_20,L2_21,L2_22,L2_23,L2_24,L2_25,L2_26,L2_27,L2_28,L2_29,L2_30,L2_31,L2_32,L2_33,L2_34,L2_35,L2_36,L2_37,L2_38,S0a,S0b,S0c
,S1a,S1b,S1c,S2a,S2b,S2c,S3a,S3b,S3c,S4a,S4b,S4c,S5a,S5b,S5c
,S6a,S6b,S6c,S7a,S7b,S7c,S8a,S8b,S8c,S9a,S9b,S9c,S10a,S10b,S10c
,S11a,S11b,S11c,S12a,S12b,S12c,S13a,S13b,S13c,S14a,S14b,S14c,S15a,S15b,S15c
,S16a,S16b,S16c,S17a,S17b,S17c,S18a,S18b,S18c,S19a,S19b,S19c,S20a,S20b,S20c
,S21a,S21b,S21c,S22a,S22b,S22c,S23a,S23b,S23c,S24a,S24b,S24c,S25a,S25b,S25c
, A0, B0, A1, B1, A2, B2, A3, B3, A4, B4, A5, B5, A6, B6, A7, B7, A8, 
B8, A9, B9, A10, B10, A11, B11, A12, B12, L0_39, L1_39;

function [31:0]ROTL;
input [31:0]x;
input [31:0]n;
begin
    ROTL = (((x) << (n[4:0])) | ((x) >> (32-(n[4:0]))));
    end
endfunction
function [31:0]ROTL3;
    input [31:0]x;
    ROTL3 = (((x) << 3) | ((x) >> (32-3)));
endfunction

always @(posedge clock)
begin
    if(clr==1)
    begin

A<=0; 
B<=0;L0_1<=0;L0_2<=0;L0_3<=0;L0_4<=0;L0_5<=0;L0_6<=0;L0_7<=0;L0_8<=0;L0_9<=0;L0_10<=0;L0_11<=0;L0_12<=0;L0_13<=0;L0_39<=0;L1_39<=0;L0_14<=0;L0_15<=0;L0_16<=0;L0_17<=0;L0_18<=0;L0_19<=0;L0_20<=0;L0_21<=0;L0_22<=0;L0_23<=0;L0_24<=0;L0_25<=0;L0_26<=0;L0_27<=0;L0_28<=0;L0_29<=0;L0_30<=0;L0_31<=0;L0_32<=0;L0_33<=0;L0_34<=0;L0_35<=0;L0_36<=0;L0_37<=0;L0_38<=0;L1_1<=0;L1_2<=0;L1_3<=0;L1_4<=0;L1_5<=0;L1_6<=0;L1_7<=0;L1_8<=0;L1_9<=0;L1_10<=0;L1_11<=0;L1_12<=0;L1_13<=0;L1_14<=0;L1_15<=0;L1_16<=0;L1_17<=0;L1_18<=0;L1_19<=0;L1_20<=0;L1_21<=0;L1_22<=0;L1_23<=0;L1_24<=0;L1_25<=0;L1_26<=0;L1_27<=0;L1_28<=0;L1_29<=0;L1_30<=0;L1_31<=0;L1_32<=0;L1_33<=0;L1_34<=0;L1_35<=0;L1_36<=0;L1_37<=0;L1_38<=0;S0a<=0;S0b<=0;S0c<=0
;S1a<=0;S1b<=0;S1c<=0;S2a<=0;S2b<=0;S2c<=0;S3a<=0;S3b<=0;S3c<=0;S4a<=0;S4b<=0;S4c<=0;S5a<=0;S5b<=0;S5c<=0
;S6a<=0;S6b<=0;S6c<=0;S7a<=0;S7b<=0;S7c<=0;S8a<=0;S8b<=0;S8c<=0;S9a<=0;S9b<=0;S9c<=0;S10a<=0;S10b<=0;S10c<=0
;S11a<=0;S11b<=0;S11c<=0;S12a<=0;S12b<=0;S12c<=0;S13a<=0;S13b<=0;S13c<=0;S14a<=0;S14b<=0;S14c<=0;S15a<=0;S15b<=0;S15c<=0
;S16a<=0;S16b<=0;S16c<=0;S17a<=0;S17b<=0;S17c<=0;S18a<=0;S18b<=0;S18c<=0;S19a<=0;S19b<=0;S19c<=0;S20a<=0;S20b<=0;S20c<=0
;S21a<=0;S21b<=0;S21c<=0;S22a<=0;S22b<=0;S22c<=0;S23a<=0;S23b<=0;S23c<=0;S24a<=0;S24b<=0;S24c<=0;S25a<=0;S25b<=0;S25c<=0
; A0<=0; B0<=0; A1<=0; B1<=0; A2<=0; B2<=0; A3<=0; B3<=0; A4<=0; B4<=0; 
A5<=0; B5<=0; A6<=0; B6<=0; A7<=0; B7<=0; A8<=0; B8<=0; A9<=0; B9<=0; 
A10<=0; B10<=0; A11<=0; B11<=0; A12<=0; B12<=0;

end
else
begin


// Key Setup + Encryption
//L1_0 <= 32'h43;
//L0_0 <= 32'hd26dd827;

B <= 32'h6e6b6e75 ^ 32'hd0eab759; //   IV     f6 75 17 1a 59 b7 ea d0
A <= 32'h20656854 ^ 32'h1a1775f6;

// loopn<=1, third<=a, oldn<=
S0a <= ROTL(32'hB7E15163, 3);
L0_1 <= ROTL(L0_0 + S0a, S0a);

// loopn<=2, third<=a, oldn<=
S1a <= ROTL(32'h5618CB1C + S0a + L0_1, 3);
L1_1 <= ROTL(L1_0 + S1a + L0_1, S1a+L0_1);

// loopn<=3, third<=a, oldn<=
S2a <= ROTL(32'hF45044D5 + S1a + L1_1, 3);
L0_2 <= ROTL(L0_1 + S2a + L1_1, S2a+L1_1);

// loopn<=4, third<=a, oldn<=
S3a <= ROTL(32'h9287BE8E + S2a + L0_2, 3);
L1_2 <= ROTL(L1_1 + S3a + L0_2, S3a+L0_2);

// loopn<=5, third<=a, oldn<=
S4a <= ROTL(32'h30BF3847 + S3a + L1_2, 3);
L0_3 <= ROTL(L0_2 + S4a + L1_2, S4a+L1_2);

// loopn<=6, third<=a, oldn<=
S5a <= ROTL(32'hCEF6B200 + S4a + L0_3, 3);
L1_3 <= ROTL(L1_2 + S5a + L0_3, S5a+L0_3);

// loopn<=7, third<=a, oldn<=
S6a <= ROTL(32'h6D2E2BB9 + S5a + L1_3, 3);
L0_4 <= ROTL(L0_3 + S6a + L1_3, S6a+L1_3);

// loopn<=8, third<=a, oldn<=
S7a <= ROTL(32'hB65A572 + S6a + L0_4, 3);
L1_4 <= ROTL(L1_3 + S7a + L0_4, S7a+L0_4);

// loopn<=9, third<=a, oldn<=
S8a <= ROTL(32'hA99D1F2B + S7a + L1_4, 3);
L0_5 <= ROTL(L0_4 + S8a + L1_4, S8a+L1_4);

// loopn<=10, third<=a, oldn<=
S9a <= ROTL(32'h47D498E4 + S8a + L0_5, 3);
L1_5 <= ROTL(L1_4 + S9a + L0_5, S9a+L0_5);

// loopn<=11, third<=a, oldn<=
S10a <= ROTL(32'hE60C129D + S9a + L1_5, 3);
L0_6 <= ROTL(L0_5 + S10a + L1_5, S10a+L1_5);

// loopn<=12, third<=a, oldn<=
S11a <= ROTL(32'h84438C56 + S10a + L0_6, 3);
L1_6 <= ROTL(L1_5 + S11a + L0_6, S11a+L0_6);

// loopn<=13, third<=a, oldn<=
S12a <= ROTL(32'h227B060F + S11a + L1_6, 3);
L0_7 <= ROTL(L0_6 + S12a + L1_6, S12a+L1_6);

// loopn<=14, third<=a, oldn<=
S13a <= ROTL(32'hC0B27FC8 + S12a + L0_7, 3);
L1_7 <= ROTL(L1_6 + S13a + L0_7, S13a+L0_7);

// loopn<=15, third<=a, oldn<=
S14a <= ROTL(32'h5EE9F981 + S13a + L1_7, 3);
L0_8 <= ROTL(L0_7 + S14a + L1_7, S14a+L1_7);

// loopn<=16, third<=a, oldn<=
S15a <= ROTL(32'hFD21733A + S14a + L0_8, 3);
L1_8 <= ROTL(L1_7 + S15a + L0_8, S15a+L0_8);

// loopn<=17, third<=a, oldn<=
S16a <= ROTL(32'h9B58ECF3 + S15a + L1_8, 3);
L0_9 <= ROTL(L0_8 + S16a + L1_8, S16a+L1_8);

// loopn<=18, third<=a, oldn<=
S17a <= ROTL(32'h399066AC + S16a + L0_9, 3);
L1_9 <= ROTL(L1_8 + S17a + L0_9, S17a+L0_9);

// loopn<=19, third<=a, oldn<=
S18a <= ROTL(32'hD7C7E065 + S17a + L1_9, 3);
L0_10 <= ROTL(L0_9 + S18a + L1_9, S18a+L1_9);

// loopn<=20, third<=a, oldn<=
S19a <= ROTL(32'h75FF5A1E + S18a + L0_10, 3);
L1_10 <= ROTL(L1_9 + S19a + L0_10, S19a+L0_10);

// loopn<=21, third<=a, oldn<=
S20a <= ROTL(32'h1436D3D7 + S19a + L1_10, 3);
L0_11 <= ROTL(L0_10 + S20a + L1_10, S20a+L1_10);

// loopn<=22, third<=a, oldn<=
S21a <= ROTL(32'hB26E4D90 + S20a + L0_11, 3);
L1_11 <= ROTL(L1_10 + S21a + L0_11, S21a+L0_11);

// loopn<=23, third<=a, oldn<=
S22a <= ROTL(32'h50A5C749 + S21a + L1_11, 3);
L0_12 <= ROTL(L0_11 + S22a + L1_11, S22a+L1_11);

// loopn<=24, third<=a, oldn<=
S23a <= ROTL(32'hEEDD4102 + S22a + L0_12, 3);
L1_12 <= ROTL(L1_11 + S23a + L0_12, S23a+L0_12);

// loopn<=25, third<=a, oldn<=
S24a <= ROTL(32'h8D14BABB + S23a + L1_12, 3);
L0_13 <= ROTL(L0_12 + S24a + L1_12, S24a+L1_12);

// loopn<=26, third<=a, oldn<=
S25a <= ROTL(32'h2B4C3474 + S24a + L0_13, 3);
L1_13 <= ROTL(L1_12 + S25a + L0_13, S25a+L0_13);

// loopn<=27, third<=b, oldn<=a *
S0b <= ROTL(S0a + S25a + L1_13, 3);
L0_14 <= ROTL(L0_13 + S0b + L1_13, S0b+L1_13);

// loopn<=28, third<=b, oldn<=a
S1b <= ROTL(S1a + S0b + L0_14, 3);
L1_14 <= ROTL(L1_13 + S1b + L0_14, S1b+L0_14);

// loopn<=29, third<=b, oldn<=a
S2b <= ROTL(S2a + S1b + L1_14, 3);
L0_15 <= ROTL(L0_14 + S2b + L1_14, S2b+L1_14);

// loopn<=30, third<=b, oldn<=a
S3b <= ROTL(S3a + S2b + L0_15, 3);
L1_15 <= ROTL(L1_14 + S3b + L0_15, S3b+L0_15);

// loopn<=31, third<=b, oldn<=a
S4b <= ROTL(S4a + S3b + L1_15, 3);
L0_16 <= ROTL(L0_15 + S4b + L1_15, S4b+L1_15);

// loopn<=32, third<=b, oldn<=a
S5b <= ROTL(S5a + S4b + L0_16, 3);
L1_16 <= ROTL(L1_15 + S5b + L0_16, S5b+L0_16);

// loopn<=33, third<=b, oldn<=a
S6b <= ROTL(S6a + S5b + L1_16, 3);
L0_17 <= ROTL(L0_16 + S6b + L1_16, S6b+L1_16);

// loopn<=34, third<=b, oldn<=a
S7b <= ROTL(S7a + S6b + L0_17, 3);
L1_17 <= ROTL(L1_16 + S7b + L0_17, S7b+L0_17);

// loopn<=35, third<=b, oldn<=a
S8b <= ROTL(S8a + S7b + L1_17, 3);
L0_18 <= ROTL(L0_17 + S8b + L1_17, S8b+L1_17);

// loopn<=36, third<=b, oldn<=a
S9b <= ROTL(S9a + S8b + L0_18, 3);
L1_18 <= ROTL(L1_17 + S9b + L0_18, S9b+L0_18);

// loopn<=37, third<=b, oldn<=a
S10b <= ROTL(S10a + S9b + L1_18, 3);
L0_19 <= ROTL(L0_18 + S10b + L1_18, S10b+L1_18);

// loopn<=38, third<=b, oldn<=a
S11b <= ROTL(S11a + S10b + L0_19, 3);
L1_19 <= ROTL(L1_18 + S11b + L0_19, S11b+L0_19);

// loopn<=39, third<=b, oldn<=a
S12b <= ROTL(S12a + S11b + L1_19, 3);
L0_20 <= ROTL(L0_19 + S12b + L1_19, S12b+L1_19);

// loopn<=40, third<=b, oldn<=a
S13b <= ROTL(S13a + S12b + L0_20, 3);
L1_20 <= ROTL(L1_19 + S13b + L0_20, S13b+L0_20);

// loopn<=41, third<=b, oldn<=a
S14b <= ROTL(S14a + S13b + L1_20, 3);
L0_21 <= ROTL(L0_20 + S14b + L1_20, S14b+L1_20);

// loopn<=42, third<=b, oldn<=a
S15b <= ROTL(S15a + S14b + L0_21, 3);
L1_21 <= ROTL(L1_20 + S15b + L0_21, S15b+L0_21);

// loopn<=43, third<=b, oldn<=a
S16b <= ROTL(S16a + S15b + L1_21, 3);
L0_22 <= ROTL(L0_21 + S16b + L1_21, S16b+L1_21);

// loopn<=44, third<=b, oldn<=a
S17b <= ROTL(S17a + S16b + L0_22, 3);
L1_22 <= ROTL(L1_21 + S17b + L0_22, S17b+L0_22);

// loopn<=45, third<=b, oldn<=a
S18b <= ROTL(S18a + S17b + L1_22, 3);
L0_23 <= ROTL(L0_22 + S18b + L1_22, S18b+L1_22);

// loopn<=46, third<=b, oldn<=a
S19b <= ROTL(S19a + S18b + L0_23, 3);
L1_23 <= ROTL(L1_22 + S19b + L0_23, S19b+L0_23);

// loopn<=47, third<=b, oldn<=a
S20b <= ROTL(S20a + S19b + L1_23, 3);
L0_24 <= ROTL(L0_23 + S20b + L1_23, S20b+L1_23);

// loopn<=48, third<=b, oldn<=a
S21b <= ROTL(S21a + S20b + L0_24, 3);
L1_24 <= ROTL(L1_23 + S21b + L0_24, S21b+L0_24);

// loopn<=49, third<=b, oldn<=a
S22b <= ROTL(S22a + S21b + L1_24, 3);
L0_25 <= ROTL(L0_24 + S22b + L1_24, S22b+L1_24);

// loopn<=50, third<=b, oldn<=a
S23b <= ROTL(S23a + S22b + L0_25, 3);
L1_25 <= ROTL(L1_24 + S23b + L0_25, S23b+L0_25);

// loopn<=51, third<=b, oldn<=a
S24b <= ROTL(S24a + S23b + L1_25, 3);
L0_26 <= ROTL(L0_25 + S24b + L1_25, S24b+L1_25);

// loopn<=52, third<=b, oldn<=a
S25b <= ROTL(S25a + S24b + L0_26, 3);
L1_26 <= ROTL(L1_25 + S25b + L0_26, S25b+L0_26);

// loopn<=53, third<=c, oldn<=b *
S0c <= ROTL(S0b + S25b + L1_26, 3);
L0_27 <= ROTL(L0_26 + S0c + L1_26, S0c+L1_26);
A0 <= A + S0c;

// loopn<=54, third<=c, oldn<=b
S1c <= ROTL(S1b + S0c + L0_27, 3);
L1_27 <= ROTL(L1_26 + S1c + L0_27, S1c+L0_27);
B0 <= B + S1c;

// loopn<=55, third<=c, oldn<=b
S2c <= ROTL(S2b + S1c + L1_27, 3);
L0_28 <= ROTL(L0_27 + S2c + L1_27, S2c+L1_27);
A1 <= ROTL(A0^B0, B0) + S2c;

// loopn<=56, third<=c, oldn<=b
S3c <= ROTL(S3b + S2c + L0_28, 3);
L1_28 <= ROTL(L1_27 + S3c + L0_28, S3c+L0_28);
B1 <= ROTL(B0^A1, A1) + S3c;

// loopn<=57, third<=c, oldn<=b
S4c <= ROTL(S4b + S3c + L1_28, 3);
L0_29 <= ROTL(L0_28 + S4c + L1_28, S4c+L1_28);
A2 <= ROTL(A1^B1, B1) + S4c;

// loopn<=58, third<=c, oldn<=b
S5c <= ROTL(S5b + S4c + L0_29, 3);
L1_29 <= ROTL(L1_28 + S5c + L0_29, S5c+L0_29);
B2 <= ROTL(B1^A2, A2) + S5c;

// loopn<=59, third<=c, oldn<=b
S6c <= ROTL(S6b + S5c + L1_29, 3);
L0_30 <= ROTL(L0_29 + S6c + L1_29, S6c+L1_29);
A3 <= ROTL(A2^B2, B2) + S6c;

// loopn<=60, third<=c, oldn<=b
S7c <= ROTL(S7b + S6c + L0_30, 3);
L1_30 <= ROTL(L1_29 + S7c + L0_30, S7c+L0_30);
B3 <= ROTL(B2^A3, A3) + S7c;

// loopn<=61, third<=c, oldn<=b
S8c <= ROTL(S8b + S7c + L1_30, 3);
L0_31 <= ROTL(L0_30 + S8c + L1_30, S8c+L1_30);
A4 <= ROTL(A3^B3, B3) + S8c;

// loopn<=62, third<=c, oldn<=b
S9c <= ROTL(S9b + S8c + L0_31, 3);
L1_31 <= ROTL(L1_30 + S9c + L0_31, S9c+L0_31);
B4 <= ROTL(B3^A4, A4) + S9c;

// loopn<=63, third<=c, oldn<=b
S10c <= ROTL(S10b + S9c + L1_31, 3);
L0_32 <= ROTL(L0_31 + S10c + L1_31, S10c+L1_31);
A5 <= ROTL(A4^B4, B4) + S10c;

// loopn<=64, third<=c, oldn<=b
S11c <= ROTL(S11b + S10c + L0_32, 3);
L1_32 <= ROTL(L1_31 + S11c + L0_32, S11c+L0_32);
B5 <= ROTL(B4^A5, A5) + S11c;

// loopn<=65, third<=c, oldn<=b
S12c <= ROTL(S12b + S11c + L1_32, 3);
L0_33 <= ROTL(L0_32 + S12c + L1_32, S12c+L1_32);
A6 <= ROTL(A5^B5, B5) + S12c;

// loopn<=66, third<=c, oldn<=b
S13c <= ROTL(S13b + S12c + L0_33, 3);
L1_33 <= ROTL(L1_32 + S13c + L0_33, S13c+L0_33);
B6 <= ROTL(B5^A6, A6) + S13c;

// loopn<=67, third<=c, oldn<=b
S14c <= ROTL(S14b + S13c + L1_33, 3);
L0_34 <= ROTL(L0_33 + S14c + L1_33, S14c+L1_33);
A7 <= ROTL(A6^B6, B6) + S14c;

// loopn<=68, third<=c, oldn<=b
S15c <= ROTL(S15b + S14c + L0_34, 3);
L1_34 <= ROTL(L1_33 + S15c + L0_34, S15c+L0_34);
B7 <= ROTL(B6^A7, A7) + S15c;

// loopn<=69, third<=c, oldn<=b
S16c <= ROTL(S16b + S15c + L1_34, 3);
L0_35 <= ROTL(L0_34 + S16c + L1_34, S16c+L1_34);
A8 <= ROTL(A7^B7, B7) + S16c;

// loopn<=70, third<=c, oldn<=b
S17c <= ROTL(S17b + S16c + L0_35, 3);
L1_35 <= ROTL(L1_34 + S17c + L0_35, S17c+L0_35);
B8 <= ROTL(B7^A8, A8) + S17c;

// loopn<=71, third<=c, oldn<=b
S18c <= ROTL(S18b + S17c + L1_35, 3);
L0_36 <= ROTL(L0_35 + S18c + L1_35, S18c+L1_35);
A9 <= ROTL(A8^B8, B8) + S18c;

// loopn<=72, third<=c, oldn<=b
S19c <= ROTL(S19b + S18c + L0_36, 3);
L1_36 <= ROTL(L1_35 + S19c + L0_36, S19c+L0_36);
B9 <= ROTL(B8^A9, A9) + S19c;

// loopn<=73, third<=c, oldn<=b
S20c <= ROTL(S20b + S19c + L1_36, 3);
L0_37 <= ROTL(L0_36 + S20c + L1_36, S20c+L1_36);
A10 <= ROTL(A9^B9, B9) + S20c;

// loopn<=74, third<=c, oldn<=b
S21c <= ROTL(S21b + S20c + L0_37, 3);
L1_37 <= ROTL(L1_36 + S21c + L0_37, S21c+L0_37);
B10 <= ROTL(B9^A10, A10) + S21c;

// loopn<=75, third<=c, oldn<=b
S22c <= ROTL(S22b + S21c + L1_37, 3);
L0_38 <= ROTL(L0_37 + S22c + L1_37, S22c+L1_37);
A11 <= ROTL(A10^B10, B10) + S22c;

// loopn<=76, third<=c, oldn<=b
S23c <= ROTL(S23b + S22c + L0_38, 3);
L1_38 <= ROTL(L1_37 + S23c + L0_38, S23c+L0_38);
B11 <= ROTL(B10^A11, A11) + S23c;

// loopn<=77, third<=c, oldn<=b
S24c <= ROTL(S24b + S23c + L1_38, 3);
L0_39 <= ROTL(L0_38 + S24c + L1_38, S24c+L1_38);
A12 <= ROTL(A11^B11, B11) + S24c;

// loopn<=78, third<=c, oldn<=b
S25c <= ROTL(S25b + S24c + L0_39, 3);
L1_39 <= ROTL(L1_38 + S25c + L0_39, S25c+L0_39);
B12 <= ROTL(B11^A12, A12) + S25c;

end
end
endmodule




More information about the Hardware mailing list