PK.H 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. ** Command & Conquer Generals(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. /***********************************************************************************************
  19. *** 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 ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /G/wwlib/PK.H $*
  25. * *
  26. * $Author:: Eric_c $*
  27. * *
  28. * $Modtime:: 4/02/99 12:00p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if _MSC_VER >= 1000
  36. #pragma once
  37. #endif // _MSC_VER >= 1000
  38. #ifndef PK_H
  39. #define PK_H
  40. #ifndef STRAW_H
  41. #include "straw.h"
  42. #endif
  43. #include "int.h"
  44. /*
  45. ** This class holds a public or private key used in Public Key Cryptography. It also serves
  46. ** as the conduit for encrypting/decrypting data using that key. Cryptography, using this
  47. ** method, has a couple of characteristics that affect how it is used. One, the process of
  48. ** encrypting/decrypting is very slow. This limits the effective quantity of data that can
  49. ** be processed. Two, the ciphertext is larger than the plaintext. This property generally
  50. ** limits its use to streaming data as opposed to random access data. The data is processed
  51. ** in blocks. The size of the ciphertext and plaintext blocks can be determined only from
  52. ** the key itself.
  53. **
  54. ** A reasonable use of this technology would be to encrypt only critical data such as the
  55. ** password for a fast general purpose cryptographic algorithm.
  56. */
  57. class PKey
  58. {
  59. public:
  60. PKey(void) : Modulus(0), Exponent(0), BitPrecision(0) {}
  61. PKey(void const * exponent, void const * modulus); // DER initialization.
  62. int Encrypt(void const * source, int slen, void * dest) const;
  63. int Decrypt(void const * source, int slen, void * dest) const;
  64. static void Generate(Straw & random, int bits, PKey & fastkey, PKey & slowkey);
  65. int Plain_Block_Size(void) const {return((BitPrecision-1)/8);}
  66. int Crypt_Block_Size(void) const {return(Plain_Block_Size()+1);}
  67. int Block_Count(int plaintext_length) const {return((((plaintext_length-1)/Plain_Block_Size())+1));}
  68. int Encode_Modulus(void * buffer) const;
  69. int Encode_Exponent(void * buffer) const;
  70. void Decode_Modulus(void * buffer);
  71. void Decode_Exponent(void * buffer);
  72. static long Fast_Exponent(void) {return(65537L);}
  73. // private:
  74. // p*q
  75. BigInt Modulus;
  76. // 65537 or
  77. // inverse of (p-1)(q-1).
  78. BigInt Exponent;
  79. // Maximum bits allowed for block.
  80. int BitPrecision;
  81. };
  82. #endif