PK.H 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/PK.H 1 3/03/97 10:25a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : PK.H *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 07/03/96 *
  26. * *
  27. * Last Update : July 3, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  32. #ifndef PK_H
  33. #define PK_H
  34. #include "int.h"
  35. /*
  36. ** This class holds a public or private key used in Public Key Cryptography. It also serves
  37. ** as the conduit for encrypting/decrypting data using that key. Cryptography, using this
  38. ** method, has a couple of characteristics that affect how it is used. One, the process of
  39. ** encrypting/decrypting is very slow. This limits the effective quantity of data that can
  40. ** be processed. Two, the ciphertext is larger than the plaintext. This property generally
  41. ** limits its use to streaming data as opposed to random access data. The data is processed
  42. ** in blocks. The size of the ciphertext and plaintext blocks can be determined only from
  43. ** the key itself.
  44. **
  45. ** A reasonable use of this technology would be to encrypt only critical data such as the
  46. ** password for a fast general purpose cryptographic algorithm.
  47. */
  48. class PKey
  49. {
  50. public:
  51. PKey(void) : Modulus(0), Exponent(0), BitPrecision(0) {}
  52. PKey(void const * exponent, void const * modulus); // DER initialization.
  53. int Encrypt(void const * source, int slen, void * dest) const;
  54. int Decrypt(void const * source, int slen, void * dest) const;
  55. static void Generate(Straw & random, int bits, PKey & fastkey, PKey & slowkey);
  56. int Plain_Block_Size(void) const {return((BitPrecision-1)/8);}
  57. int Crypt_Block_Size(void) const {return(Plain_Block_Size()+1);}
  58. int Block_Count(int plaintext_length) const {return((((plaintext_length-1)/Plain_Block_Size())+1));}
  59. int Encode_Modulus(void * buffer) const;
  60. int Encode_Exponent(void * buffer) const;
  61. void Decode_Modulus(void * buffer);
  62. void Decode_Exponent(void * buffer);
  63. static long Fast_Exponent(void) {return(65537L);}
  64. private:
  65. // p*q
  66. BigInt Modulus;
  67. // 65537 or
  68. // inverse of (p-1)(q-1).
  69. BigInt Exponent;
  70. // Maximum bits allowed for block.
  71. int BitPrecision;
  72. };
  73. #endif