]> git.cryptolib.org Git - arm-crypto-lib.git/blob - bmw/autogen_f1.rb
including even/odd-trick for BMW
[arm-crypto-lib.git] / bmw / autogen_f1.rb
1 # autogen f1 function for BMW
2 =begin
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 =end
19
20 header = <<EOF
21 /* BEGIN of automatic generated code */
22
23 static inline
24 void bmw_small_f1(uint32_t* q, const void* m, const void* h){ 
25   uint32_t even, odd;
26 EOF
27
28 footer = <<EOF
29 }
30
31 /* END of automatic generated code */
32
33 EOF
34
35 =begin
36   uint32_t r;
37   /* r = 0x05555555*(j+16); */
38   r = (   ROTL32(((uint32_t*)m)[j],      ((j+0))+1  )
39          + ROTL32(((uint32_t*)m)[(j+3)],  ((j+3))+1  )
40          - ROTL32(((uint32_t*)m)[(j+10)], ((j+10))+1 )
41          + k_lut[j]
42        ) ^ ((uint32_t*)h)[(j+7)];
43   r += S32_1(q[j+ 0]) + S32_2(q[j+ 1]) + S32_3(q[j+ 2]) + S32_0(q[j+ 3]) +
44      S32_1(q[j+ 4]) + S32_2(q[j+ 5]) + S32_3(q[j+ 6]) + S32_0(q[j+ 7]) +
45      S32_1(q[j+ 8]) + S32_2(q[j+ 9]) + S32_3(q[j+10]) + S32_0(q[j+11]) +
46      S32_1(q[j+12]) + S32_2(q[j+13]) + S32_3(q[j+14]) + S32_0(q[j+15]);
47 =end
48
49 def expand_1(j)
50   s = sprintf("/* expand_1(%2d) */\n", j)
51   s += sprintf("\tq[%2d] = \n", j+16)
52   s += sprintf("\t\t((  ROTL32(((uint32_t*)m)[%2d], %d) \n", j%16,      (j%16)+1)
53   s += sprintf("\t\t  + ROTL32(((uint32_t*)m)[%2d], %d) \n", (j+ 3)%16, ((j+ 3)%16)+1)
54   s += sprintf("\t\t  - ROTL32(((uint32_t*)m)[%2d], %d) \n", (j+10)%16, ((j+10)%16)+1)
55   s += sprintf("\t\t  + 0x%08xUL \n", 0x0555_5555*(16+j))
56   s += sprintf("\t\t )^ ((uint32_t*)h)[%2d] \n", (j+7)%16)
57   s += sprintf("\t\t)");
58   (0..15).each do |x|
59     s += (x%4==0)?"\n\t\t":" "
60     s += sprintf("+ S32_%d(q[%2d])", (x+1)%4, x+j)
61   end
62   s += ';'
63   return s
64 end
65
66
67 def expand_2(j,start)
68   s = sprintf("/* expand_2(%2d) */\n", j)
69   s += sprintf("\tq[%2d] = \n", j+16)
70   s += sprintf("\t\t((  ROTL32(((uint32_t*)m)[%2d], %d) \n", j%16,      (j%16)+1)
71   s += sprintf("\t\t  + ROTL32(((uint32_t*)m)[%2d], %d) \n", (j+ 3)%16, ((j+ 3)%16)+1)
72   s += sprintf("\t\t  - ROTL32(((uint32_t*)m)[%2d], %d) \n", (j+10)%16, ((j+10)%16)+1)
73   s += sprintf("\t\t  + 0x%08xUL \n", 0x0555_5555*(16+j))
74   s += sprintf("\t\t )^ ((uint32_t*)h)[%2d] \n", (j+7)%16)
75   s += sprintf("\t\t)")
76   if(j-1<=start) 
77     if(j%2==0)
78       # even
79       s += sprintf("\n\t\t + ( even =  q[%2d] + q[%2d] + q[%2d]",j,j+2,j+4)
80       s += sprintf("\n\t\t           + q[%2d] + q[%2d] + q[%2d] + q[%2d] )",j+6,j+8,j+10,j+12)
81       s += sprintf("\n\t\t + R32_1(q[%2d]) + R32_2(q[%2d]) + R32_3(q[%2d])",j+1,j+3,j+5)
82       s += sprintf("\n\t\t + R32_4(q[%2d]) + R32_5(q[%2d]) + R32_6(q[%2d])",j+7,j+9,j+11)
83       s += sprintf("\n\t\t + R32_7(q[%2d]) + S32_4(q[%2d]) + S32_5(q[%2d])",j+13,j+14,j+15)
84     else
85       # odd
86       s += sprintf("\n\t\t + ( odd  =  q[%2d] + q[%2d] + q[%2d]",j,j+2,j+4)
87       s += sprintf("\n\t\t           + q[%2d] + q[%2d] + q[%2d] + q[%2d] )",j+6,j+8,j+10,j+12)
88       s += sprintf("\n\t\t + R32_1(q[%2d]) + R32_2(q[%2d]) + R32_3(q[%2d])",j+1,j+3,j+5)
89       s += sprintf("\n\t\t + R32_4(q[%2d]) + R32_5(q[%2d]) + R32_6(q[%2d])",j+7,j+9,j+11)
90       s += sprintf("\n\t\t + R32_7(q[%2d]) + S32_4(q[%2d]) + S32_5(q[%2d])",j+13,j+14,j+15)
91     end 
92   else 
93     s += sprintf("\n\t\t + ( %s  +=  q[%2d] - q[%2d] )",(j%2==0)?"even":"odd ",j+12,j-2)    
94     s += sprintf("\n\t\t + R32_1(q[%2d]) + R32_2(q[%2d]) + R32_3(q[%2d])",j+1,j+3,j+5)
95     s += sprintf("\n\t\t + R32_4(q[%2d]) + R32_5(q[%2d]) + R32_6(q[%2d])",j+7,j+9,j+11)
96     s += sprintf("\n\t\t + R32_7(q[%2d]) + S32_4(q[%2d]) + S32_5(q[%2d])",j+13,j+14,j+15)  
97   end 
98   s += ';'
99   return s
100 end
101
102
103 puts header
104 (0..1).each  {|x| puts expand_1(x)}
105 (2..15).each {|x| puts expand_2(x, 2)}
106 puts footer