Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:09

0001 // private header for Serpent and Sosemanuk

0002 
0003 #ifndef CRYPTOPP_SERPENTP_H
0004 #define CRYPTOPP_SERPENTP_H
0005 
0006 NAMESPACE_BEGIN(CryptoPP)
0007 
0008 // linear transformation

0009 #define LT(i,a,b,c,d,e) {\
0010     a = rotlConstant<13>(a);    \
0011     c = rotlConstant<3>(c);     \
0012     d = rotlConstant<7>(d ^ c ^ (a << 3));  \
0013     b = rotlConstant<1>(b ^ a ^ c);     \
0014     a = rotlConstant<5>(a ^ b ^ d);         \
0015     c = rotlConstant<22>(c ^ d ^ (b << 7));}
0016 
0017 // inverse linear transformation

0018 #define ILT(i,a,b,c,d,e)    {\
0019     c = rotrConstant<22>(c);    \
0020     a = rotrConstant<5>(a);     \
0021     c ^= d ^ (b << 7);  \
0022     a ^= b ^ d;         \
0023     b = rotrConstant<1>(b);     \
0024     d = rotrConstant<7>(d) ^ c ^ (a << 3);  \
0025     b ^= a ^ c;         \
0026     c = rotrConstant<3>(c);     \
0027     a = rotrConstant<13>(a);}
0028 
0029 // order of output from S-box functions

0030 #define beforeS0(f) f(0,a,b,c,d,e)
0031 #define afterS0(f) f(1,b,e,c,a,d)
0032 #define afterS1(f) f(2,c,b,a,e,d)
0033 #define afterS2(f) f(3,a,e,b,d,c)
0034 #define afterS3(f) f(4,e,b,d,c,a)
0035 #define afterS4(f) f(5,b,a,e,c,d)
0036 #define afterS5(f) f(6,a,c,b,e,d)
0037 #define afterS6(f) f(7,a,c,d,b,e)
0038 #define afterS7(f) f(8,d,e,b,a,c)
0039 
0040 // order of output from inverse S-box functions

0041 #define beforeI7(f) f(8,a,b,c,d,e)
0042 #define afterI7(f) f(7,d,a,b,e,c)
0043 #define afterI6(f) f(6,a,b,c,e,d)
0044 #define afterI5(f) f(5,b,d,e,c,a)
0045 #define afterI4(f) f(4,b,c,e,a,d)
0046 #define afterI3(f) f(3,a,b,e,c,d)
0047 #define afterI2(f) f(2,b,d,e,c,a)
0048 #define afterI1(f) f(1,a,b,c,e,d)
0049 #define afterI0(f) f(0,a,d,b,e,c)
0050 
0051 // The instruction sequences for the S-box functions

0052 // come from Dag Arne Osvik's paper "Speeding up Serpent".

0053 
0054 #define S0(i, r0, r1, r2, r3, r4) \
0055        {           \
0056     r3 ^= r0;   \
0057     r4 = r1;   \
0058     r1 &= r3;   \
0059     r4 ^= r2;   \
0060     r1 ^= r0;   \
0061     r0 |= r3;   \
0062     r0 ^= r4;   \
0063     r4 ^= r3;   \
0064     r3 ^= r2;   \
0065     r2 |= r1;   \
0066     r2 ^= r4;   \
0067     r4 = ~r4;      \
0068     r4 |= r1;   \
0069     r1 ^= r3;   \
0070     r1 ^= r4;   \
0071     r3 |= r0;   \
0072     r1 ^= r3;   \
0073     r4 ^= r3;   \
0074             }
0075 
0076 #define I0(i, r0, r1, r2, r3, r4) \
0077        {           \
0078     r2 = ~r2;      \
0079     r4 = r1;   \
0080     r1 |= r0;   \
0081     r4 = ~r4;      \
0082     r1 ^= r2;   \
0083     r2 |= r4;   \
0084     r1 ^= r3;   \
0085     r0 ^= r4;   \
0086     r2 ^= r0;   \
0087     r0 &= r3;   \
0088     r4 ^= r0;   \
0089     r0 |= r1;   \
0090     r0 ^= r2;   \
0091     r3 ^= r4;   \
0092     r2 ^= r1;   \
0093     r3 ^= r0;   \
0094     r3 ^= r1;   \
0095     r2 &= r3;   \
0096     r4 ^= r2;   \
0097             }
0098 
0099 #define S1(i, r0, r1, r2, r3, r4) \
0100        {           \
0101     r0 = ~r0;      \
0102     r2 = ~r2;      \
0103     r4 = r0;   \
0104     r0 &= r1;   \
0105     r2 ^= r0;   \
0106     r0 |= r3;   \
0107     r3 ^= r2;   \
0108     r1 ^= r0;   \
0109     r0 ^= r4;   \
0110     r4 |= r1;   \
0111     r1 ^= r3;   \
0112     r2 |= r0;   \
0113     r2 &= r4;   \
0114     r0 ^= r1;   \
0115     r1 &= r2;   \
0116     r1 ^= r0;   \
0117     r0 &= r2;   \
0118     r0 ^= r4;   \
0119             }
0120 
0121 #define I1(i, r0, r1, r2, r3, r4) \
0122        {           \
0123     r4 = r1;   \
0124     r1 ^= r3;   \
0125     r3 &= r1;   \
0126     r4 ^= r2;   \
0127     r3 ^= r0;   \
0128     r0 |= r1;   \
0129     r2 ^= r3;   \
0130     r0 ^= r4;   \
0131     r0 |= r2;   \
0132     r1 ^= r3;   \
0133     r0 ^= r1;   \
0134     r1 |= r3;   \
0135     r1 ^= r0;   \
0136     r4 = ~r4;      \
0137     r4 ^= r1;   \
0138     r1 |= r0;   \
0139     r1 ^= r0;   \
0140     r1 |= r4;   \
0141     r3 ^= r1;   \
0142             }
0143 
0144 #define S2(i, r0, r1, r2, r3, r4) \
0145        {           \
0146     r4 = r0;   \
0147     r0 &= r2;   \
0148     r0 ^= r3;   \
0149     r2 ^= r1;   \
0150     r2 ^= r0;   \
0151     r3 |= r4;   \
0152     r3 ^= r1;   \
0153     r4 ^= r2;   \
0154     r1 = r3;   \
0155     r3 |= r4;   \
0156     r3 ^= r0;   \
0157     r0 &= r1;   \
0158     r4 ^= r0;   \
0159     r1 ^= r3;   \
0160     r1 ^= r4;   \
0161     r4 = ~r4;      \
0162             }
0163 
0164 #define I2(i, r0, r1, r2, r3, r4) \
0165        {           \
0166     r2 ^= r3;   \
0167     r3 ^= r0;   \
0168     r4 = r3;   \
0169     r3 &= r2;   \
0170     r3 ^= r1;   \
0171     r1 |= r2;   \
0172     r1 ^= r4;   \
0173     r4 &= r3;   \
0174     r2 ^= r3;   \
0175     r4 &= r0;   \
0176     r4 ^= r2;   \
0177     r2 &= r1;   \
0178     r2 |= r0;   \
0179     r3 = ~r3;      \
0180     r2 ^= r3;   \
0181     r0 ^= r3;   \
0182     r0 &= r1;   \
0183     r3 ^= r4;   \
0184     r3 ^= r0;   \
0185             }
0186 
0187 #define S3(i, r0, r1, r2, r3, r4) \
0188        {           \
0189     r4 = r0;   \
0190     r0 |= r3;   \
0191     r3 ^= r1;   \
0192     r1 &= r4;   \
0193     r4 ^= r2;   \
0194     r2 ^= r3;   \
0195     r3 &= r0;   \
0196     r4 |= r1;   \
0197     r3 ^= r4;   \
0198     r0 ^= r1;   \
0199     r4 &= r0;   \
0200     r1 ^= r3;   \
0201     r4 ^= r2;   \
0202     r1 |= r0;   \
0203     r1 ^= r2;   \
0204     r0 ^= r3;   \
0205     r2 = r1;   \
0206     r1 |= r3;   \
0207     r1 ^= r0;   \
0208             }
0209 
0210 #define I3(i, r0, r1, r2, r3, r4) \
0211        {           \
0212     r4 = r2;   \
0213     r2 ^= r1;   \
0214     r1 &= r2;   \
0215     r1 ^= r0;   \
0216     r0 &= r4;   \
0217     r4 ^= r3;   \
0218     r3 |= r1;   \
0219     r3 ^= r2;   \
0220     r0 ^= r4;   \
0221     r2 ^= r0;   \
0222     r0 |= r3;   \
0223     r0 ^= r1;   \
0224     r4 ^= r2;   \
0225     r2 &= r3;   \
0226     r1 |= r3;   \
0227     r1 ^= r2;   \
0228     r4 ^= r0;   \
0229     r2 ^= r4;   \
0230             }
0231 
0232 #define S4(i, r0, r1, r2, r3, r4) \
0233        {           \
0234     r1 ^= r3;   \
0235     r3 = ~r3;      \
0236     r2 ^= r3;   \
0237     r3 ^= r0;   \
0238     r4 = r1;   \
0239     r1 &= r3;   \
0240     r1 ^= r2;   \
0241     r4 ^= r3;   \
0242     r0 ^= r4;   \
0243     r2 &= r4;   \
0244     r2 ^= r0;   \
0245     r0 &= r1;   \
0246     r3 ^= r0;   \
0247     r4 |= r1;   \
0248     r4 ^= r0;   \
0249     r0 |= r3;   \
0250     r0 ^= r2;   \
0251     r2 &= r3;   \
0252     r0 = ~r0;      \
0253     r4 ^= r2;   \
0254             }
0255 
0256 #define I4(i, r0, r1, r2, r3, r4) \
0257        {           \
0258     r4 = r2;   \
0259     r2 &= r3;   \
0260     r2 ^= r1;   \
0261     r1 |= r3;   \
0262     r1 &= r0;   \
0263     r4 ^= r2;   \
0264     r4 ^= r1;   \
0265     r1 &= r2;   \
0266     r0 = ~r0;      \
0267     r3 ^= r4;   \
0268     r1 ^= r3;   \
0269     r3 &= r0;   \
0270     r3 ^= r2;   \
0271     r0 ^= r1;   \
0272     r2 &= r0;   \
0273     r3 ^= r0;   \
0274     r2 ^= r4;   \
0275     r2 |= r3;   \
0276     r3 ^= r0;   \
0277     r2 ^= r1;   \
0278             }
0279 
0280 #define S5(i, r0, r1, r2, r3, r4) \
0281        {           \
0282     r0 ^= r1;   \
0283     r1 ^= r3;   \
0284     r3 = ~r3;      \
0285     r4 = r1;   \
0286     r1 &= r0;   \
0287     r2 ^= r3;   \
0288     r1 ^= r2;   \
0289     r2 |= r4;   \
0290     r4 ^= r3;   \
0291     r3 &= r1;   \
0292     r3 ^= r0;   \
0293     r4 ^= r1;   \
0294     r4 ^= r2;   \
0295     r2 ^= r0;   \
0296     r0 &= r3;   \
0297     r2 = ~r2;      \
0298     r0 ^= r4;   \
0299     r4 |= r3;   \
0300     r2 ^= r4;   \
0301             }
0302 
0303 #define I5(i, r0, r1, r2, r3, r4) \
0304        {           \
0305     r1 = ~r1;      \
0306     r4 = r3;   \
0307     r2 ^= r1;   \
0308     r3 |= r0;   \
0309     r3 ^= r2;   \
0310     r2 |= r1;   \
0311     r2 &= r0;   \
0312     r4 ^= r3;   \
0313     r2 ^= r4;   \
0314     r4 |= r0;   \
0315     r4 ^= r1;   \
0316     r1 &= r2;   \
0317     r1 ^= r3;   \
0318     r4 ^= r2;   \
0319     r3 &= r4;   \
0320     r4 ^= r1;   \
0321     r3 ^= r0;   \
0322     r3 ^= r4;   \
0323     r4 = ~r4;      \
0324             }
0325 
0326 #define S6(i, r0, r1, r2, r3, r4) \
0327        {           \
0328     r2 = ~r2;      \
0329     r4 = r3;   \
0330     r3 &= r0;   \
0331     r0 ^= r4;   \
0332     r3 ^= r2;   \
0333     r2 |= r4;   \
0334     r1 ^= r3;   \
0335     r2 ^= r0;   \
0336     r0 |= r1;   \
0337     r2 ^= r1;   \
0338     r4 ^= r0;   \
0339     r0 |= r3;   \
0340     r0 ^= r2;   \
0341     r4 ^= r3;   \
0342     r4 ^= r0;   \
0343     r3 = ~r3;      \
0344     r2 &= r4;   \
0345     r2 ^= r3;   \
0346             }
0347 
0348 #define I6(i, r0, r1, r2, r3, r4) \
0349        {           \
0350     r0 ^= r2;   \
0351     r4 = r2;   \
0352     r2 &= r0;   \
0353     r4 ^= r3;   \
0354     r2 = ~r2;      \
0355     r3 ^= r1;   \
0356     r2 ^= r3;   \
0357     r4 |= r0;   \
0358     r0 ^= r2;   \
0359     r3 ^= r4;   \
0360     r4 ^= r1;   \
0361     r1 &= r3;   \
0362     r1 ^= r0;   \
0363     r0 ^= r3;   \
0364     r0 |= r2;   \
0365     r3 ^= r1;   \
0366     r4 ^= r0;   \
0367             }
0368 
0369 #define S7(i, r0, r1, r2, r3, r4) \
0370        {           \
0371     r4 = r2;   \
0372     r2 &= r1;   \
0373     r2 ^= r3;   \
0374     r3 &= r1;   \
0375     r4 ^= r2;   \
0376     r2 ^= r1;   \
0377     r1 ^= r0;   \
0378     r0 |= r4;   \
0379     r0 ^= r2;   \
0380     r3 ^= r1;   \
0381     r2 ^= r3;   \
0382     r3 &= r0;   \
0383     r3 ^= r4;   \
0384     r4 ^= r2;   \
0385     r2 &= r0;   \
0386     r4 = ~r4;      \
0387     r2 ^= r4;   \
0388     r4 &= r0;   \
0389     r1 ^= r3;   \
0390     r4 ^= r1;   \
0391             }
0392 
0393 #define I7(i, r0, r1, r2, r3, r4) \
0394        {           \
0395     r4 = r2;   \
0396     r2 ^= r0;   \
0397     r0 &= r3;   \
0398     r2 = ~r2;      \
0399     r4 |= r3;   \
0400     r3 ^= r1;   \
0401     r1 |= r0;   \
0402     r0 ^= r2;   \
0403     r2 &= r4;   \
0404     r1 ^= r2;   \
0405     r2 ^= r0;   \
0406     r0 |= r2;   \
0407     r3 &= r4;   \
0408     r0 ^= r3;   \
0409     r4 ^= r1;   \
0410     r3 ^= r4;   \
0411     r4 |= r0;   \
0412     r3 ^= r2;   \
0413     r4 ^= r2;   \
0414             }
0415 
0416 // key xor

0417 #define KX(r, a, b, c, d, e)    {\
0418     a ^= k[4 * r + 0]; \
0419     b ^= k[4 * r + 1]; \
0420     c ^= k[4 * r + 2]; \
0421     d ^= k[4 * r + 3];}
0422 
0423 #define LK(r, a, b, c, d, e)    {\
0424     a = k[(8-r)*4 + 0];     \
0425     b = k[(8-r)*4 + 1];     \
0426     c = k[(8-r)*4 + 2];     \
0427     d = k[(8-r)*4 + 3];}
0428 
0429 #define SK(r, a, b, c, d, e)    {\
0430     k[(8-r)*4 + 4] = a;     \
0431     k[(8-r)*4 + 5] = b;     \
0432     k[(8-r)*4 + 6] = c;     \
0433     k[(8-r)*4 + 7] = d;}
0434 
0435 void Serpent_KeySchedule(word32 *k, unsigned int rounds, const byte *userKey, size_t keylen);
0436 
0437 NAMESPACE_END
0438 
0439 #endif  // CRYPTOPP_SERPENTP_H