Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.6 KiB

  1. // wake.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "wake.h"
  4. NAMESPACE_BEGIN(CryptoPP)
  5. void WAKE_TestInstantiations()
  6. {
  7. WAKE_OFB<>::Encryption x2;
  8. WAKE_OFB<>::Decryption x4;
  9. }
  10. inline word32 WAKE_Base::M(word32 x, word32 y)
  11. {
  12. word32 w = x+y;
  13. return (w>>8) ^ t[w & 0xff];
  14. }
  15. void WAKE_Base::GenKey(word32 k0, word32 k1, word32 k2, word32 k3)
  16. {
  17. // this code is mostly copied from David Wheeler's paper "A Bulk Data Encryption Algorithm"
  18. signed int x, z, p;
  19. // x and z were declared as "long" in Wheeler's paper, which is a signed type. I don't know if that was intentional, but it's too late to change it now. -- Wei 7/4/2010
  20. CRYPTOPP_COMPILE_ASSERT(sizeof(x) == 4);
  21. static int tt[10]= {
  22. 0x726a8f3b, // table
  23. 0xe69a3b5c,
  24. 0xd3c71fe5,
  25. 0xab3c73d2,
  26. 0x4d3a8eb3,
  27. 0x0396d6e8,
  28. 0x3d4c2f7a,
  29. 0x9ee27cf3, } ;
  30. t[0] = k0;
  31. t[1] = k1;
  32. t[2] = k2;
  33. t[3] = k3;
  34. for (p=4 ; p<256 ; p++)
  35. {
  36. x=t[p-4]+t[p-1] ; // fill t
  37. t[p]= (x>>3) ^ tt[x&7] ;
  38. }
  39. for (p=0 ; p<23 ; p++)
  40. t[p]+=t[p+89] ; // mix first entries
  41. x=t[33] ; z=t[59] | 0x01000001 ;
  42. z=z&0xff7fffff ;
  43. for (p=0 ; p<256 ; p++) { //change top byte to
  44. x=(x&0xff7fffff)+z ; // a permutation etc
  45. t[p]=(t[p] & 0x00ffffff) ^ x ; }
  46. t[256]=t[0] ;
  47. byte y=byte(x);
  48. for (p=0 ; p<256 ; p++) { // further change perm.
  49. t[p]=t[y=byte(t[p^y]^y)] ; // and other digits
  50. t[y]=t[p+1] ; }
  51. }
  52. template <class B>
  53. void WAKE_Policy<B>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
  54. {
  55. word32 k0, k1, k2, k3;
  56. BlockGetAndPut<word32, BigEndian>::Get(key)(r3)(r4)(r5)(r6)(k0)(k1)(k2)(k3);
  57. GenKey(k0, k1, k2, k3);
  58. }
  59. // OFB
  60. template <class B>
  61. void WAKE_Policy<B>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
  62. {
  63. #define WAKE_OUTPUT(x)\
  64. while (iterationCount--)\
  65. {\
  66. CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 0, r6);\
  67. r3 = M(r3, r6);\
  68. r4 = M(r4, r3);\
  69. r5 = M(r5, r4);\
  70. r6 = M(r6, r5);\
  71. output += 4;\
  72. if (!(x & INPUT_NULL))\
  73. input += 4;\
  74. }
  75. typedef word32 WordType;
  76. CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(WAKE_OUTPUT, 0);
  77. }
  78. /*
  79. template <class B>
  80. void WAKE_ROFB_Policy<B>::Iterate(KeystreamOperation operation, byte *output, const byte *input, unsigned int iterationCount)
  81. {
  82. KeystreamOutput<B> keystreamOperation(operation, output, input);
  83. while (iterationCount--)
  84. {
  85. keystreamOperation(r6);
  86. r3 = M(r3, r6);
  87. r4 = M(r4, r3);
  88. r5 = M(r5, r4);
  89. r6 = M(r6, r5);
  90. }
  91. }
  92. */
  93. template class WAKE_Policy<BigEndian>;
  94. template class WAKE_Policy<LittleEndian>;
  95. //template class WAKE_ROFB_Policy<BigEndian>;
  96. //template class WAKE_ROFB_Policy<LittleEndian>;
  97. NAMESPACE_END