crypt2.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #define NROUNDS 32
  2. #define substLong(t) ( (uint)SubstTable20[(uint)t&255] | \
  3. ((uint)SubstTable20[(int)(t>> 8)&255]<< 8) | \
  4. ((uint)SubstTable20[(int)(t>>16)&255]<<16) | \
  5. ((uint)SubstTable20[(int)(t>>24)&255]<<24) )
  6. static byte InitSubstTable20[256]={
  7. 215, 19,149, 35, 73,197,192,205,249, 28, 16,119, 48,221, 2, 42,
  8. 232, 1,177,233, 14, 88,219, 25,223,195,244, 90, 87,239,153,137,
  9. 255,199,147, 70, 92, 66,246, 13,216, 40, 62, 29,217,230, 86, 6,
  10. 71, 24,171,196,101,113,218,123, 93, 91,163,178,202, 67, 44,235,
  11. 107,250, 75,234, 49,167,125,211, 83,114,157,144, 32,193,143, 36,
  12. 158,124,247,187, 89,214,141, 47,121,228, 61,130,213,194,174,251,
  13. 97,110, 54,229,115, 57,152, 94,105,243,212, 55,209,245, 63, 11,
  14. 164,200, 31,156, 81,176,227, 21, 76, 99,139,188,127, 17,248, 51,
  15. 207,120,189,210, 8,226, 41, 72,183,203,135,165,166, 60, 98, 7,
  16. 122, 38,155,170, 69,172,252,238, 39,134, 59,128,236, 27,240, 80,
  17. 131, 3, 85,206,145, 79,154,142,159,220,201,133, 74, 64, 20,129,
  18. 224,185,138,103,173,182, 43, 34,254, 82,198,151,231,180, 58, 10,
  19. 118, 26,102, 12, 50,132, 22,191,136,111,162,179, 45, 4,148,108,
  20. 161, 56, 78,126,242,222, 15,175,146, 23, 33,241,181,190, 77,225,
  21. 0, 46,169,186, 68, 95,237, 65, 53,208,253,168, 9, 18,100, 52,
  22. 116,184,160, 96,109, 37, 30,106,140,104,150, 5,204,117,112, 84
  23. };
  24. void CryptData::SetKey20(const char *Password)
  25. {
  26. InitCRC32(CRCTab);
  27. char Psw[MAXPASSWORD];
  28. strncpyz(Psw,Password,ASIZE(Psw)); // We'll need to modify it below.
  29. size_t PswLength=strlen(Psw);
  30. Key20[0]=0xD3A3B879L;
  31. Key20[1]=0x3F6D12F7L;
  32. Key20[2]=0x7515A235L;
  33. Key20[3]=0xA4E7F123L;
  34. memcpy(SubstTable20,InitSubstTable20,sizeof(SubstTable20));
  35. for (uint J=0;J<256;J++)
  36. for (size_t I=0;I<PswLength;I+=2)
  37. {
  38. uint N1=(byte)CRCTab [ (byte(Password[I]) - J) &0xff];
  39. uint N2=(byte)CRCTab [ (byte(Password[I+1]) + J) &0xff];
  40. for (int K=1;N1!=N2;N1=(N1+1)&0xff,K++)
  41. Swap20(&SubstTable20[N1],&SubstTable20[(N1+I+K)&0xff]);
  42. }
  43. // Incomplete last block of password must be zero padded.
  44. if ((PswLength & CRYPT_BLOCK_MASK)!=0)
  45. for (size_t I=PswLength;I<=(PswLength|CRYPT_BLOCK_MASK);I++)
  46. Psw[I]=0;
  47. for (size_t I=0;I<PswLength;I+=CRYPT_BLOCK_SIZE)
  48. EncryptBlock20((byte *)Psw+I);
  49. }
  50. void CryptData::EncryptBlock20(byte *Buf)
  51. {
  52. uint A,B,C,D,T,TA,TB;
  53. A=RawGet4(Buf+0)^Key20[0];
  54. B=RawGet4(Buf+4)^Key20[1];
  55. C=RawGet4(Buf+8)^Key20[2];
  56. D=RawGet4(Buf+12)^Key20[3];
  57. for(int I=0;I<NROUNDS;I++)
  58. {
  59. T=((C+rotls(D,11,32))^Key20[I&3]);
  60. TA=A^substLong(T);
  61. T=((D^rotls(C,17,32))+Key20[I&3]);
  62. TB=B^substLong(T);
  63. A=C;
  64. B=D;
  65. C=TA;
  66. D=TB;
  67. }
  68. RawPut4(C^Key20[0],Buf+0);
  69. RawPut4(D^Key20[1],Buf+4);
  70. RawPut4(A^Key20[2],Buf+8);
  71. RawPut4(B^Key20[3],Buf+12);
  72. UpdKeys20(Buf);
  73. }
  74. void CryptData::DecryptBlock20(byte *Buf)
  75. {
  76. byte InBuf[16];
  77. uint A,B,C,D,T,TA,TB;
  78. A=RawGet4(Buf+0)^Key20[0];
  79. B=RawGet4(Buf+4)^Key20[1];
  80. C=RawGet4(Buf+8)^Key20[2];
  81. D=RawGet4(Buf+12)^Key20[3];
  82. memcpy(InBuf,Buf,sizeof(InBuf));
  83. for(int I=NROUNDS-1;I>=0;I--)
  84. {
  85. T=((C+rotls(D,11,32))^Key20[I&3]);
  86. TA=A^substLong(T);
  87. T=((D^rotls(C,17,32))+Key20[I&3]);
  88. TB=B^substLong(T);
  89. A=C;
  90. B=D;
  91. C=TA;
  92. D=TB;
  93. }
  94. RawPut4(C^Key20[0],Buf+0);
  95. RawPut4(D^Key20[1],Buf+4);
  96. RawPut4(A^Key20[2],Buf+8);
  97. RawPut4(B^Key20[3],Buf+12);
  98. UpdKeys20(InBuf);
  99. }
  100. void CryptData::UpdKeys20(byte *Buf)
  101. {
  102. for (int I=0;I<16;I+=4)
  103. {
  104. Key20[0]^=CRCTab[Buf[I]];
  105. Key20[1]^=CRCTab[Buf[I+1]];
  106. Key20[2]^=CRCTab[Buf[I+2]];
  107. Key20[3]^=CRCTab[Buf[I+3]];
  108. }
  109. }
  110. void CryptData::Swap20(byte *Ch1,byte *Ch2)
  111. {
  112. byte Ch=*Ch1;
  113. *Ch1=*Ch2;
  114. *Ch2=Ch;
  115. }