rsacrypt.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. ** Command & Conquer Renegade(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. #ifndef RSACRYPT_H
  19. #define RSACRYPT_H
  20. #include <wwlib/int.h>
  21. #include <wwlib/wwfile.h>
  22. //#define SIMPLE_AND_SLOW_RSA
  23. // Version identification string for OpenSSH identity files.
  24. #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n"
  25. //
  26. // RSACrypt - Implementation of RSA using the Int class from wwlib
  27. //
  28. // For key generation I suggest you use OpenSSH (http://www.openssh.com/)
  29. // There is a utility called "ssh-keygen", call:
  30. //
  31. // ssh-keygen [-b bits]
  32. //
  33. // If you generate the file on a UNIX system, be sure and FTP it in BINARY MODE!!!!
  34. //
  35. // You can then use 'Load_SSH_Keyset' on the identity file it produces.
  36. //
  37. // Note: Make sure you leave the keyphrase blank for the SSH keyset. I don't support
  38. // decrypting that file!
  39. //
  40. // Notation:
  41. // Public keys:
  42. // n = product of two primes, p & q (p & q must remain secret)
  43. // e = relatively prime to (p-1)(q-1)
  44. //
  45. // Private keys:
  46. // d = e^-1 mod ((p-1)(q-1)) // e^-1 = inverse of e
  47. //
  48. // Encrypting:
  49. // cyphertext = m^e mod n
  50. //
  51. // Decrypting:
  52. // message = cyphertext^d mod n
  53. //
  54. // Note: I use a trick involving the chinese remainder theorem to get a 3x speedup in decryption.
  55. // It's well documented on the web so I won't get into detail here.
  56. //
  57. template <int PRECISION>
  58. class RSACrypt
  59. {
  60. public:
  61. typedef Int<PRECISION> Integer;
  62. RSACrypt() {}
  63. ~RSACrypt() {}
  64. void Set_Public_Keys(const Integer &pub_n, const Integer &pub_e);
  65. void Set_Keys(const Integer &pub_n, const Integer &pub_e,
  66. const Integer &priv_d, const Integer &keygen_p, const Integer &keygen_q);
  67. void Get_Public_Keys(Integer &pub_n, Integer &pub_e) const;
  68. void Get_Private_Key(Integer &priv_d) const;
  69. void Get_Keygen_Keys(Integer &keygen_p, Integer &keygen_q) const;
  70. bool Load_SSH_Keyset(FileClass *file);
  71. void Encrypt(const Integer &plaintext, Integer &cyphertext) const;
  72. void Decrypt(const Integer &cyphertext, Integer &plaintext) const;
  73. private:
  74. bool Load_Bignum(FileClass *file, Integer &num);
  75. void Decryption_Setup(); // Do precomputation to speedup decryption
  76. Integer PublicN;
  77. Integer PublicE;
  78. Integer PrivateD;
  79. Integer KeygenP; // Primes P & Q generated as part of the keyset
  80. Integer KeygenQ;
  81. // Precomputed values that speed up encryption
  82. Integer DmodPm1; // d mod p-1
  83. Integer DmodQm1; // d mod q-1
  84. Integer RP; // RP = q^(p-1) mod n
  85. Integer RQ; // RQ = p^(q-1) mod n;
  86. };
  87. //
  88. // Set the two public keys: n & e
  89. //
  90. template <int PRECISION>
  91. void RSACrypt<PRECISION>::Set_Public_Keys(const Integer &pub_n, const Integer &pub_e)
  92. {
  93. PublicN=pub_n;
  94. PublicE=pub_e;
  95. }
  96. //
  97. // Set the public & private keys: n, e & d
  98. //
  99. template <int PRECISION>
  100. void RSACrypt<PRECISION>::Set_Keys(const Integer &pub_n, const Integer &pub_e,
  101. const Integer &priv_d, const Integer &keygen_p, const Integer &keygen_q)
  102. {
  103. PublicN=pub_n;
  104. PublicE=pub_e;
  105. PrivateD=priv_d;
  106. KeygenP=keygen_p;
  107. KeygenQ=keygen_q;
  108. Decrtyption_Setup();
  109. }
  110. //
  111. // Get the public keys
  112. //
  113. template <int PRECISION>
  114. void RSACrypt<PRECISION>::Get_Public_Keys(Integer &pub_n, Integer &pub_e) const
  115. {
  116. pub_n=PublicN;
  117. pub_e=PublicE;
  118. }
  119. //
  120. // Get the private key
  121. //
  122. template <int PRECISION>
  123. void RSACrypt<PRECISION>::Get_Private_Key(Integer &priv_d) const
  124. {
  125. priv_d=PrivateD;
  126. }
  127. //
  128. // Get the private numbers created during the keyset generation
  129. // Private as in revealing these will reveal the private key!
  130. //
  131. template <int PRECISION>
  132. void RSACrypt<PRECISION>::Get_Keygen_Keys(Integer &keygen_p, Integer &keygen_q) const
  133. {
  134. keygen_p=KeygenP;
  135. keygen_q=KeygenQ;
  136. }
  137. //
  138. // Load an RSA private keyset from an OpenSSH "identity" file.
  139. //
  140. template <int PRECISION>
  141. bool RSACrypt<PRECISION>::Load_SSH_Keyset(FileClass *file)
  142. {
  143. assert(file);
  144. if ( ! file)
  145. return(false);
  146. bool retval=true;
  147. unsigned char buffer[1024];
  148. if ( ! file->Open())
  149. return(false);
  150. file->Read(buffer, strlen(AUTHFILE_ID_STRING)+1);
  151. buffer[strlen(AUTHFILE_ID_STRING)]=0; // null term
  152. if (strcmp((char *)buffer, AUTHFILE_ID_STRING))
  153. return(false);
  154. unsigned char cypher_type; // keyfile encryption method
  155. file->Read(&cypher_type, 1);
  156. if (cypher_type != 0)
  157. return(false);
  158. file->Read(buffer, 4); // reserved data
  159. file->Read(buffer, 4); // ignored
  160. retval=retval && Load_Bignum(file, PublicN);
  161. retval=retval && Load_Bignum(file, PublicE);
  162. if (!retval)
  163. return(false);
  164. // comment string
  165. int comment_length;
  166. file->Read(&comment_length, 4);
  167. comment_length=ntohl(comment_length);
  168. file->Read(buffer, comment_length);
  169. // post-decrypt check chars
  170. file->Read(buffer, 4);
  171. if ((buffer[0] != buffer[2]) || (buffer[1] != buffer[3]))
  172. return(false);
  173. Integer q_inv_mod_p; // invserse of q mod p (we don't need this)
  174. retval=retval && Load_Bignum(file, PrivateD);
  175. retval=retval && Load_Bignum(file, q_inv_mod_p);
  176. retval=retval && Load_Bignum(file, KeygenP);
  177. retval=retval && Load_Bignum(file, KeygenQ);
  178. if (!retval)
  179. return(false);
  180. // Any remaining bytes are padding
  181. Decryption_Setup();
  182. return(true);
  183. }
  184. //
  185. // Precomputation for fast decryption
  186. //
  187. template <int PRECISION>
  188. void RSACrypt<PRECISION>::Decryption_Setup(void)
  189. {
  190. Integer temp;
  191. // If p < q, swap p & q
  192. if (KeygenP < KeygenQ)
  193. {
  194. temp=KeygenP;
  195. KeygenP=KeygenQ;
  196. KeygenQ=temp;
  197. }
  198. assert(KeygenP > KeygenQ);
  199. // pm1 = p - 1
  200. Integer pm1(KeygenP);
  201. --pm1;
  202. // pm1 = p - 1
  203. Integer qm1(KeygenQ);
  204. --qm1;
  205. // DmodPm1 = d mod (p-1)
  206. Integer::Unsigned_Divide(DmodPm1, temp, PrivateD, pm1);
  207. assert(DmodPm1 < pm1);
  208. // DmodQm1 = d mod (q-1)
  209. Integer::Unsigned_Divide(DmodQm1, temp, PrivateD, qm1);
  210. assert(DmodQm1 < qm1);
  211. RP = KeygenQ.exp_b_mod_c(pm1, PublicN);
  212. RQ = KeygenP.exp_b_mod_c(qm1, PublicN);
  213. }
  214. //
  215. // RSA Encryption c = m^e mod n
  216. //
  217. template <int PRECISION>
  218. void RSACrypt<PRECISION>::Encrypt(const Integer &plaintext, Integer &cyphertext) const
  219. {
  220. Integer m(plaintext);
  221. cyphertext=m.exp_b_mod_c(PublicE, PublicN);
  222. }
  223. //
  224. // RSA Decryption m = c^d mod n
  225. //
  226. template <int PRECISION>
  227. void RSACrypt<PRECISION>::Decrypt(const Integer &cyphertext, Integer &plaintext) const
  228. {
  229. #ifdef SIMPLE_AND_SLOW_RSA
  230. Integer c(cyphertext);
  231. plaintext=c.exp_b_mod_c(PrivateD, PublicN);
  232. #else
  233. Integer temp;
  234. // Get a version of the cyphertext mod q & p
  235. Integer cmp, cmq;
  236. Integer::Unsigned_Divide(cmp, temp, cyphertext, KeygenP);
  237. Integer::Unsigned_Divide(cmq, temp, cyphertext, KeygenQ);
  238. // mp = cmp ^ dmp mod p
  239. Integer mp;
  240. mp=cmp.exp_b_mod_c(DmodPm1, KeygenP);
  241. // mq = cmq ^ dmq mod q
  242. Integer mq;
  243. mq=cmq.exp_b_mod_c(DmodQm1, KeygenQ);
  244. Integer sp, sq;
  245. //sp=mp * RP mod n;
  246. //sq=mq * RQ mod n;
  247. XMP_Prepare_Modulus(&PublicN.reg[0], PRECISION);
  248. XMP_Mod_Mult(&sp.reg[0], &mp.reg[0], &RP.reg[0], PRECISION);
  249. XMP_Mod_Mult(&sq.reg[0], &mq.reg[0], &RQ.reg[0], PRECISION);
  250. XMP_Mod_Mult_Clear(PRECISION);
  251. plaintext = sp+sq;
  252. if (plaintext >= PublicN)
  253. plaintext-=PublicN;
  254. #endif
  255. }
  256. ////////////////////////////////// Private Methods Below ///////////////////////////////////
  257. //
  258. // Load a large number from the SSH keyset file
  259. //
  260. template <int PRECISION>
  261. bool RSACrypt<PRECISION>::Load_Bignum(FileClass *file, Integer &num)
  262. {
  263. int readlen;
  264. unsigned char buffer[1024];
  265. unsigned short int n_bits, n_bytes;
  266. readlen=file->Read(&n_bits, 2); // bits in network byte order
  267. if (readlen != 2)
  268. return(false);
  269. n_bits=ntohs(n_bits);
  270. n_bytes=(n_bits+7)/8;
  271. readlen=file->Read(buffer, n_bytes);
  272. if (readlen != n_bytes)
  273. return(false);
  274. num.Unsigned_Decode(buffer, n_bytes);
  275. return(true);
  276. }
  277. #endif