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.

119 lines
2.4 KiB

  1. // dsa.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "dsa.h"
  5. #include "nbtheory.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. size_t DSAConvertSignatureFormat(byte *buffer, size_t bufferSize, DSASignatureFormat toFormat, const byte *signature, size_t signatureLen, DSASignatureFormat fromFormat)
  8. {
  9. Integer r, s;
  10. StringStore store(signature, signatureLen);
  11. ArraySink sink(buffer, bufferSize);
  12. switch (fromFormat)
  13. {
  14. case DSA_P1363:
  15. r.Decode(store, signatureLen/2);
  16. s.Decode(store, signatureLen/2);
  17. break;
  18. case DSA_DER:
  19. {
  20. BERSequenceDecoder seq(store);
  21. r.BERDecode(seq);
  22. s.BERDecode(seq);
  23. seq.MessageEnd();
  24. break;
  25. }
  26. case DSA_OPENPGP:
  27. r.OpenPGPDecode(store);
  28. s.OpenPGPDecode(store);
  29. break;
  30. }
  31. switch (toFormat)
  32. {
  33. case DSA_P1363:
  34. r.Encode(sink, bufferSize/2);
  35. s.Encode(sink, bufferSize/2);
  36. break;
  37. case DSA_DER:
  38. {
  39. DERSequenceEncoder seq(sink);
  40. r.DEREncode(seq);
  41. s.DEREncode(seq);
  42. seq.MessageEnd();
  43. break;
  44. }
  45. case DSA_OPENPGP:
  46. r.OpenPGPEncode(sink);
  47. s.OpenPGPEncode(sink);
  48. break;
  49. }
  50. return (size_t)sink.TotalPutLength();
  51. }
  52. bool DSA::GeneratePrimes(const byte *seedIn, unsigned int g, int &counter,
  53. Integer &p, unsigned int L, Integer &q, bool useInputCounterValue)
  54. {
  55. assert(g%8 == 0);
  56. SHA sha;
  57. SecByteBlock seed(seedIn, g/8);
  58. SecByteBlock U(SHA::DIGESTSIZE);
  59. SecByteBlock temp(SHA::DIGESTSIZE);
  60. SecByteBlock W(((L-1)/160+1) * SHA::DIGESTSIZE);
  61. const int n = (L-1) / 160;
  62. const int b = (L-1) % 160;
  63. Integer X;
  64. sha.CalculateDigest(U, seed, g/8);
  65. for (int i=g/8-1, carry=true; i>=0 && carry; i--)
  66. carry=!++seed[i];
  67. sha.CalculateDigest(temp, seed, g/8);
  68. xorbuf(U, temp, SHA::DIGESTSIZE);
  69. U[0] |= 0x80;
  70. U[SHA::DIGESTSIZE-1] |= 1;
  71. q.Decode(U, SHA::DIGESTSIZE);
  72. if (!IsPrime(q))
  73. return false;
  74. int counterEnd = useInputCounterValue ? counter+1 : 4096;
  75. for (int c = 0; c < counterEnd; c++)
  76. {
  77. for (int k=0; k<=n; k++)
  78. {
  79. for (int i=g/8-1, carry=true; i>=0 && carry; i--)
  80. carry=!++seed[i];
  81. if (!useInputCounterValue || c == counter)
  82. sha.CalculateDigest(W+(n-k)*SHA::DIGESTSIZE, seed, g/8);
  83. }
  84. if (!useInputCounterValue || c == counter)
  85. {
  86. W[SHA::DIGESTSIZE - 1 - b/8] |= 0x80;
  87. X.Decode(W + SHA::DIGESTSIZE - 1 - b/8, L/8);
  88. p = X-((X % (2*q))-1);
  89. if (p.GetBit(L-1) && IsPrime(p))
  90. {
  91. counter = c;
  92. return true;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. NAMESPACE_END
  99. #endif