PK.H 4.2 KB

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