Mixe for Privacy and Anonymity in the Internet
CAASymCipher.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright
11 notice,
12  this list of conditions and the following disclaimer in the
13 documentation and/or
14  other materials provided with the distribution.
15 
16  - Neither the name of the University of Technology Dresden, Germany nor
17 the names of its contributors
18  may be used to endorse or promote products derived from this software
19 without specific
20  prior written permission.
21 
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
24 AND ANY EXPRESS
25 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 MERCHANTABILITY
27 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 REGENTS OR CONTRIBUTORS
29 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES
31 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA,
33 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 LIABILITY, WHETHER
35 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY
37 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38 DAMAGE
39 */
40 #include "StdAfx.h"
41 
42 #include "CAASymCipher.hpp"
43 #include "CABase64.hpp"
44 #include "CAUtil.hpp"
45 #include "xml/DOM_Output.hpp"
47 {
48  m_pRSA = NULL;
49 }
50 
52 {
53  destroy();
54 }
55 
57 {
58  RSA_free(m_pRSA);
59  m_pRSA = NULL;
60  return E_SUCCESS;
61 }
62 
63 void setRSAFlags(RSA *pRSA)
64 {
65  if (pRSA == NULL)
66  return;
67  #if OPENSSL_VERSION_NUMBER > 0x100020cfL
68  RSA_set_flags(pRSA,RSA_FLAG_THREAD_SAFE | RSA_FLAG_NO_BLINDING );
69  #else
70  pRSA->flags |= RSA_FLAG_THREAD_SAFE;
71  pRSA->flags |= RSA_FLAG_SIGN_VER;
72  #ifdef RSA_FLAG_NO_BLINDING
73  pRSA->flags |= RSA_FLAG_NO_BLINDING;
74  #endif
75  #endif //OPENSSL_VERSION_NUMBER >= 0x1000204fL
76 #if OPENSSL_VERSION_NUMBER > 0x0090707fL
77 // pRSA->flags|=RSA_FLAG_NO_EXP_CONSTTIME;
78 #endif
79 }
80 
89 {
90  if (RSA_private_decrypt(RSA_SIZE, from, to, m_pRSA, RSA_NO_PADDING) == -1)
91  return E_UNKNOWN;
92  else
93  return E_SUCCESS;
94 }
95 
104  {
105  SINT32 ret =RSA_private_decrypt(RSA_SIZE, from, to, m_pRSA, RSA_PKCS1_OAEP_PADDING);
106  if (ret < 0)
107  return E_UNKNOWN;
108  *len = ret;
109  return E_SUCCESS;
110  }
111 
121  UINT32 *len)
122 {
123  SINT32 ret =
124  RSA_public_encrypt(fromlen, from, to, m_pRSA, RSA_PKCS1_OAEP_PADDING);
125  if (ret < 0)
126  return E_UNKNOWN;
127  *len = ret;
128  return E_SUCCESS;
129 }
130 
141  UINT32 *len)
142 {
143  SINT32 ret = RSA_public_encrypt(fromlen, from, to, m_pRSA, RSA_PKCS1_PADDING);
144  if (ret < 0)
145  return E_UNKNOWN;
146  *len = ret;
147  return E_SUCCESS;
148 }
149 
158 {
159  if (RSA_public_encrypt(RSA_SIZE, from, to, m_pRSA, RSA_NO_PADDING) == -1)
160  return E_UNKNOWN;
161  else
162  return E_SUCCESS;
163 }
164 
171 {
172  RSA_free(m_pRSA);
173  m_pRSA = NULL;
174 #if OPENSSL_VERSION_NUMBER >= 0x1000204fL
175  m_pRSA=::RSA_new();
176  BIGNUM* e = BN_new();
177  BN_set_word(e, 65537);
178  SINT32 ret=::RSA_generate_key_ex(m_pRSA,size, e, NULL);
179  if (ret != 1)
180  {
181  RSA_free(m_pRSA);
182  m_pRSA = NULL;
183  }
184 #else
185  m_pRSA = ::RSA_generate_key(size, 65537, NULL, NULL);
186 #endif
187  if (m_pRSA == NULL)
188  return E_UNKNOWN;
190  return E_SUCCESS;
191 }
192 
213 /*
214 SINT32 CAASymCipher::getPublicKey(UINT8* buff,UINT32 *len)
215 {
216  if(m_pRSA==NULL||buff==NULL)
217  return E_UNKNOWN;
218  SINT32 keySize=getPublicKeySize();
219  if(keySize<=0||(*len)<(UINT32)keySize)
220  return E_UNKNOWN;
221  UINT32 aktIndex=0;
222  UINT16 size=htons(BN_num_bytes(m_pRSA->n));
223  memcpy(buff,&size,sizeof(size));
224  aktIndex+=sizeof(size);
225  BN_bn2bin(m_pRSA->n,buff+aktIndex);
226  aktIndex+=BN_num_bytes(m_pRSA->n);
227  size=htons(BN_num_bytes(m_pRSA->e));
228  memcpy(buff+aktIndex,&size,sizeof(size));
229  aktIndex+=sizeof(size);
230  BN_bn2bin(m_pRSA->e,buff+aktIndex);
231  aktIndex+=BN_num_bytes(m_pRSA->e);
232  *len=aktIndex;
233  return E_SUCCESS;
234 }*/
235 
243 /*SINT32 CAASymCipher::getPublicKeySize()
244  {
245  if(m_pRSA==NULL||m_pRSA->n==NULL||m_pRSA->e==NULL)
246  return E_UNKNOWN;
247  return
248  (SINT32)BN_num_bytes(m_pRSA->n)+BN_num_bytes(m_pRSA->e)+4;
249  }*/
250 
261 /*SINT32 CAASymCipher::setPublicKey(UINT8* key,UINT32* len)
262  {
263  UINT32 aktIndex;
264  UINT32 availBytes;
265  UINT16 size;
266 
267  if(key==NULL||len==NULL||(*len)<6) //the need at least 6 bytes:
268 4 for the sizes and 1 for n and 1 for e
269  goto _ERROR;
270  availBytes=(*len);
271  RSA_free(m_pRSA);
272  m_pRSA=RSA_new();
273  if(m_pRSA==NULL)
274  goto _ERROR;
275  memcpy(&size,key,2);
276  size=ntohs(size);
277  availBytes-=2;
278  if(size>availBytes-3) //the need at least 3 bytes for the
279 exponent...
280  goto _ERROR;
281  aktIndex=2;
282  availBytes-=size;
283  m_pRSA->n=BN_new();
284  if(m_pRSA->n==NULL)
285  goto _ERROR;
286  BN_bin2bn(key+aktIndex,size,m_pRSA->n);
287  aktIndex+=size;
288  memcpy(&size,key+aktIndex,2);
289  size=ntohs(size);
290  availBytes-=2;
291  aktIndex+=2;
292  if(size>availBytes)
293  goto _ERROR;
294  m_pRSA->e=BN_new();
295  BN_bin2bn(key+aktIndex,size,m_pRSA->e);
296  aktIndex+=size;
297  (*len)=aktIndex;
298  m_pRSA->flags|=RSA_FLAG_THREAD_SAFE;
299  #ifdef RSA_FLAG_NO_BLINDING
300  m_pRSA->flags|=RSA_FLAG_NO_BLINDING;
301  #endif
302  return E_SUCCESS;
303 _ERROR:
304  RSA_free(m_pRSA);
305  m_pRSA=NULL;
306  return E_UNKNOWN;
307  }
308 */
309 
310 #ifndef ONLY_LOCAL_PROXY
311 
334 {
335  if (m_pRSA == NULL || buff == NULL)
336  return E_UNKNOWN;
337  XERCES_CPP_NAMESPACE::DOMDocument *pDoc = createDOMDocument();
338  DOMElement *elemRoot = NULL;
339  getPublicKeyAsDOMElement(elemRoot, pDoc);
340  DOM_Output::dumpToMem(elemRoot, buff, len);
341  if (pDoc != NULL)
342  {
343  pDoc->release();
344  pDoc = NULL;
345  }
346  return E_SUCCESS;
347 }
348 
349 
350 #ifdef EXPORT_ASYM_PRIVATE_KEY
391 SINT32 CAASymCipher::getPrivateKeyAsXML(UINT8 *buff, UINT32 *len)
392 {
393  if (m_pRSA == NULL || buff == NULL)
394  return E_UNKNOWN;
395  XERCES_CPP_NAMESPACE::DOMDocument *pDoc = createDOMDocument();
396  DOMElement *elemRoot = NULL;
397  getPrivateKeyAsDOMElement(elemRoot, pDoc);
398  DOM_Output::dumpToMem(elemRoot, buff, len);
399  if (pDoc != NULL)
400  {
401  pDoc->release();
402  pDoc = NULL;
403  }
404  return E_SUCCESS;
405 }
406 
407 SINT32 CAASymCipher::getPrivateKeyAsDOMElement(
408  DOMElement *&elemRoot, XERCES_CPP_NAMESPACE::DOMDocument *docOwner)
409 {
410  if (m_pRSA == NULL)
411  return E_UNKNOWN;
412 
413  elemRoot = createDOMElement(docOwner, "RSAKeyPair");
414 
415  const BIGNUM *n, *e, *p, *q, *dmp1, *dmq1, *iqmp, *d;
416  RSA_get0_key(m_pRSA, &n, &e, &d);
417  RSA_get0_factors(m_pRSA, &p, &q);
418  RSA_get0_crt_params(m_pRSA, &dmp1, &dmq1, &iqmp);
419 
420  addKeyPart(elemRoot, docOwner, "Modulus", n);
421  addKeyPart(elemRoot, docOwner, "Exponent", e);
422  addKeyPart(elemRoot, docOwner, "P", p);
423  addKeyPart(elemRoot, docOwner, "Q", q);
424  addKeyPart(elemRoot, docOwner, "DP", dmp1);
425  addKeyPart(elemRoot, docOwner, "DQ", dmq1);
426  addKeyPart(elemRoot, docOwner, "InverseQ", iqmp);
427  addKeyPart(elemRoot, docOwner, "D", d);
428 
429 
430 
431 /* addKeyPart(elemRoot, docOwner, "Modulus", m_pRSA->n);
432  addKeyPart(elemRoot, docOwner, "Exponent", m_pRSA->e);
433  addKeyPart(elemRoot, docOwner, "P", m_pRSA->p);
434  addKeyPart(elemRoot, docOwner, "Q", m_pRSA->q);
435  addKeyPart(elemRoot, docOwner, "DP", m_pRSA->dmp1);
436  addKeyPart(elemRoot, docOwner, "DQ", m_pRSA->dmq1);
437  addKeyPart(elemRoot, docOwner, "InverseQ", m_pRSA->iqmp);
438  addKeyPart(elemRoot, docOwner, "D", m_pRSA->d);
439 */
440  return E_SUCCESS;
441 }
453 SINT32 CAASymCipher::setPrivateKeyAsXML(const UINT8 *key, UINT32 len)
454 {
455  if (key == NULL)
456  return E_UNKNOWN;
457 
458  XERCES_CPP_NAMESPACE::DOMDocument *doc = parseDOMDocument(key, len);
459  if (doc == NULL)
460  {
461  return E_UNKNOWN;
462  }
463  DOMElement *root = doc->getDocumentElement();
464  if (root == NULL)
465  {
466  return E_UNKNOWN;
467  }
468  return setPrivateKeyAsDOMNode(root);
469 }
470 
471 /*
472 SINT32 CAASymCipher::setPrivateKeyAsDOMNode(DOMNode *node)
473 {
474  DOMNode *root = node;
475  while (root != NULL)
476  {
477  if (equals(root->getNodeName(), "RSAKeyPair"))
478  {
479  RSA *tmpRSA = RSA_new();
480  DOMNode *child = root->getFirstChild();
481  while (child != NULL)
482  {
483  if (equals(child->getNodeName(), "Modulus"))
484  {
485  getKeyPart(&tmpRSA->n, child);
486  }
487  else if (equals(child->getNodeName(), "Exponent"))
488  {
489  getKeyPart(&tmpRSA->e, child);
490  }
491  else if (equals(child->getNodeName(), "P"))
492  {
493  getKeyPart(&tmpRSA->p, child);
494  }
495  else if (equals(child->getNodeName(), "Q"))
496  {
497  getKeyPart(&tmpRSA->q, child);
498  }
499  else if (equals(child->getNodeName(), "DP"))
500  {
501  getKeyPart(&tmpRSA->dmp1, child);
502  }
503  else if (equals(child->getNodeName(), "DQ"))
504  {
505  getKeyPart(&tmpRSA->dmq1, child);
506  }
507  else if (equals(child->getNodeName(), "InverseQ"))
508  {
509  getKeyPart(&tmpRSA->iqmp, child);
510  }
511  else if (equals(child->getNodeName(), "D"))
512  {
513  getKeyPart(&tmpRSA->d, child);
514  }
515  child = child->getNextSibling();
516  }
517  if (tmpRSA->n != NULL && tmpRSA->e != NULL && tmpRSA->e != NULL &&
518  tmpRSA->p != NULL && tmpRSA->q != NULL && tmpRSA->d != NULL &&
519  tmpRSA->iqmp != NULL && tmpRSA->dmp1 != NULL &&
520  tmpRSA->dmq1 != NULL)
521  {
522  if (m_pRSA != NULL)
523  RSA_free(m_pRSA);
524  m_pRSA = tmpRSA;
525  ::setRSAFlags(m_pRSA);
526  return E_SUCCESS;
527  }
528  RSA_free(tmpRSA);
529  return E_UNKNOWN;
530  }
531  root = root->getNextSibling();
532  }
533  return E_UNKNOWN;
534 }
535 */
536 
537 SINT32 CAASymCipher::setPrivateKeyAsDOMNode(DOMNode *node)
538 {
539  DOMNode *root = node;
540  while (root != NULL)
541  {
542  if (equals(root->getNodeName(), "RSAKeyPair"))
543  {
544  RSA *tmpRSA = RSA_new();
545  BIGNUM *n, *e, *p, *q, *dmp1, *dmq1, *iqmp, *d;
546  n = e = p = q = dmp1 = dmq1 = iqmp = d = nullptr;
547  DOMNode *child = root->getFirstChild();
548  while (child != NULL)
549  {
550  if (equals(child->getNodeName(), "Modulus"))
551  {
552  getKeyPart(&n, child);
553  }
554  else if (equals(child->getNodeName(), "Exponent"))
555  {
556  getKeyPart(&e, child);
557  }
558  else if (equals(child->getNodeName(), "P"))
559  {
560  getKeyPart(&p, child);
561  }
562  else if (equals(child->getNodeName(), "Q"))
563  {
564  getKeyPart(&q, child);
565  }
566  else if (equals(child->getNodeName(), "DP"))
567  {
568  getKeyPart(&dmp1, child);
569  }
570  else if (equals(child->getNodeName(), "DQ"))
571  {
572  getKeyPart(&dmq1, child);
573  }
574  else if (equals(child->getNodeName(), "InverseQ"))
575  {
576  getKeyPart(&iqmp, child);
577  }
578  else if (equals(child->getNodeName(), "D"))
579  {
580  getKeyPart(&d, child);
581  }
582  child = child->getNextSibling();
583  }
584  if (n != NULL && e != NULL && e != NULL &&
585  p != NULL && q != NULL && d != NULL &&
586  iqmp != NULL && dmp1 != NULL &&
587  dmq1 != NULL)
588  {
589  if (m_pRSA != NULL)
590  RSA_free(m_pRSA);
591 
592  RSA_set0_key(tmpRSA, n, e, d);
593  RSA_set0_factors(tmpRSA, p, q);
594  RSA_set0_crt_params(tmpRSA, dmp1, dmq1, iqmp);
595 
596  m_pRSA = tmpRSA;
598  return E_SUCCESS;
599  }
600  RSA_free(tmpRSA);
601  return E_UNKNOWN;
602  }
603  root = root->getNextSibling();
604  }
605  return E_UNKNOWN;
606 }
607 
608 
609 #endif // EXPORT_ASYM_PRIVATE_KEY
610 
623 {
624  if (key == NULL)
625  return E_UNKNOWN;
626 
627  XERCES_CPP_NAMESPACE::DOMDocument *doc = parseDOMDocument(key, len);
628  if (doc == NULL)
629  {
630  return E_UNKNOWN;
631  }
632  DOMElement *root = doc->getDocumentElement();
633  if (root == NULL)
634  {
635  return E_UNKNOWN;
636  }
637  return setPublicKeyAsDOMNode(root);
638 }
639 
640 #endif // ONLY_LOCAL_PROXY
641 
642 SINT32 CAASymCipher::getKeyPart(BIGNUM **part, DOMNode *node)
643 {
644  if (*part != NULL)
645  BN_free(*part);
646  UINT8 *tmpStr = new UINT8[4096];
647  UINT32 tmpStrLen = 4096;
648  getDOMElementValue(node, tmpStr, &tmpStrLen);
649  UINT8 decBuff[4096];
650  UINT32 decLen = 4096;
651  CABase64::decode(tmpStr, tmpStrLen, decBuff, &decLen);
652  delete[] tmpStr;
653  tmpStr = NULL;
654  *part = BN_bin2bn(decBuff, decLen, NULL);
655  return E_SUCCESS;
656 }
657 
659 {
660  DOMNode *root = node;
661  while (root != NULL)
662  {
663  if (equals(root->getNodeName(), "RSAKeyValue"))
664  {
665  RSA *tmpRSA = RSA_new();
666  BIGNUM* n = NULL;
667  BIGNUM* e = NULL;
668  DOMNode *child = root->getFirstChild();
669  while (child != NULL)
670  {
671  if (equals(child->getNodeName(), "Modulus"))
672  {
673  getKeyPart(&n, child);
674  }
675  else if (equals(child->getNodeName(), "Exponent"))
676  {
677  getKeyPart(&e, child);
678  }
679  child = child->getNextSibling();
680  }
681  if (n != NULL && e != NULL)
682  {
683  if (m_pRSA != NULL)
684  RSA_free(m_pRSA);
685  m_pRSA = tmpRSA;
686  #if OPENSSL_VERSION_NUMBER > 0x100020cfL
687  RSA_set0_key(m_pRSA,n,e, NULL );
688  #else
689  m_pRSA->n=n;
690  m_pRSA->e=e;
691  #endif
693  return E_SUCCESS;
694  }
695  RSA_free(tmpRSA);
696  return E_UNKNOWN;
697  }
698  root = root->getNextSibling();
699  }
700  return E_UNKNOWN;
701 }
702 
703 #ifndef ONLY_LOCAL_PROXY
711 {
712  if (pCert == NULL)
713  return E_UNKNOWN;
714  EVP_PKEY *pubkey = X509_get_pubkey(pCert->m_pCert);
715  int keyType = 0;
716 #if OPENSSL_VERSION_NUMBER >= 0x1000204fL
717  keyType = EVP_PKEY_id(pubkey);
718 #else
719  keyType = pubkey->type;
720 #endif
721 
722  if (pubkey == NULL || (keyType != EVP_PKEY_RSA && keyType != EVP_PKEY_RSA2))
723  return E_UNKNOWN;
724  RSA *r = NULL;
725 #if OPENSSL_VERSION_NUMBER >= 0x1000204fL
726  r = EVP_PKEY_get1_RSA(pubkey);
727 #else
728  r = pubkey->pkey.rsa;
729 #endif
730 
731  if (RSA_size(r) != 128)
732  return E_UNKNOWN;
733  if (m_pRSA != NULL)
734  RSA_free(m_pRSA);
735  m_pRSA = r;
737  return E_SUCCESS;
738 }
739 #endif // ONLY_LOCAL_PROXY
740 
741 SINT32 CAASymCipher::setPublicKey(const UINT8 *m, UINT32 mlen, const UINT8 *e, UINT32 elen)
742 {
743  RSA *tmpRSA = RSA_new();
744  UINT32 decLen = 4096;
745  UINT8 decBuff[4096];
746  CABase64::decode(m, mlen, decBuff, &decLen);
747  BIGNUM* bnE = BN_bin2bn(decBuff, decLen, NULL);
748  decLen = 4096;
749  CABase64::decode(e, elen, decBuff, &decLen);
750  BIGNUM* bnN = BN_bin2bn(decBuff, decLen, NULL);
751  if (bnN != NULL && bnE != NULL)
752  {
753  if (m_pRSA != NULL)
754  RSA_free(m_pRSA);
755  m_pRSA = tmpRSA;
756  #if OPENSSL_VERSION_NUMBER > 0x100020cfL
757  RSA_set0_key(m_pRSA,bnN,bnE, NULL );
758  #else
759  m_pRSA->n=bnN;
760  m_pRSA->e=bnE;
761  #endif
763  return E_SUCCESS;
764  }
765  RSA_free(tmpRSA);
766  return E_UNKNOWN;
767 }
768 
769 #ifdef INTEL_IPP_CRYPTO
770 IppStatus __STDCALL myIppBitSupplier(Ipp32u *pData, int nBits,
771  void *pEbsParams)
772 {
773  getRandom((UINT8 *)pData, (nBits + 7) / 8);
774  return ippStsNoErr;
775 }
776 #endif
777 
779 {
780  const UINT32 runs = 10000;
781  UINT8 *inBuff = new UINT8[128];
782  UINT8 *outBuff = new UINT8[128];
783  getRandom(inBuff, 128);
784  inBuff[0] &= 0x7F;
785 #ifdef INTEL_IPP_CRYPTO
786  int size = 0;
787  ippsRSAGetSize(1024, 512, IppRSAprivate, &size);
788  IppsRSAState *pCtx = (IppsRSAState *)new UINT8[size];
789  IppStatus ret = ippsRSAInit(1024, 512, IppRSAprivate, pCtx);
790  if (ret != ippStsNoErr)
791  {
792  printf("Error in RSA init!\n");
793  return E_UNKNOWN;
794  }
795  ippsBigNumGetSize(1, &size);
796  IppsBigNumState *pE = (IppsBigNumState *)(new UINT8[size]);
797  ippsBigNumInit(1, pE);
798  UINT32 pEValue[] = {0x010001};
799  ret = ippsSet_BN(IppsBigNumPOS, 1, pEValue, pE);
800  if (ret != ippStsNoErr)
801  {
802  printf("Error in setBN(e)!\n");
803  return E_UNKNOWN;
804  }
805  ippsRSAGenerate(pE, 1024, 512, 1024, pCtx, myIppBitSupplier, NULL);
806  if (ret != ippStsNoErr)
807  {
808  printf("Error in RSA generate key!\n");
809  return E_UNKNOWN;
810  }
811 
812  ippsBigNumGetSize(32, &size);
813  IppsBigNumState *pY = (IppsBigNumState *)(new UINT8[size]);
814  ippsBigNumInit(32, pY);
815 
816  IppsBigNumState *pX = (IppsBigNumState *)(new UINT8[size]);
817  ippsBigNumInit(32, pX);
818  UINT32 *pXValue = new UINT32[32];
819  memcpy(pXValue, inBuff, 128);
820  pXValue[0] &= 0x7FFFFFFF;
821  ippsSet_BN(IppsBigNumPOS, 32, pXValue, pX);
822 
823 #else
824  CAASymCipher *pCipher = new CAASymCipher();
825  pCipher->generateKeyPair(1024);
826  UINT32 iLen = 128;
827  pCipher->encryptOAEP(inBuff, 86, inBuff, &iLen);
828 #endif
829  UINT64 start, end;
830  getcurrentTimeMillis(start);
831  for (UINT32 i = 0; i < runs; i++)
832  {
833 #ifdef INTEL_IPP_CRYPTO
834  IppStatus ret = ippsRSADecrypt(pX, pY, pCtx);
835  if (ret != ippStsNoErr)
836  {
837  printf("Error in RSADEcrypt %i!\n", ret);
838  return E_UNKNOWN;
839  }
840 #else
841  pCipher->decryptOAEP(inBuff, outBuff, &iLen);
842 // pCipher->decrypt(inBuff,outBuff);
843 #endif
844  }
846  UINT32 d = diff64(end, start);
847  printf("CAASymCiper::testSpeed() takes %u ms for %u decrypts!\n", d, runs);
848  return E_SUCCESS;
849 }
850 
851 #if !defined ONLY_LOCAL_PROXY || defined INCLUDE_MIDDLE_MIX
852 
853 SINT32 CAASymCipher::addKeyPart(DOMElement *elemRoot,
854  XERCES_CPP_NAMESPACE::DOMDocument *docOwner,
855  const char *partName, BIGNUM *part)
856 {
857  DOMElement *node = createDOMElement(docOwner, partName);
858  elemRoot->appendChild(node);
859  UINT8 tmpBuff[256];
860  UINT32 size = 256;
861  BN_bn2bin(part, tmpBuff);
862  CABase64::encode(tmpBuff, BN_num_bytes(part), tmpBuff, &size);
863  tmpBuff[size] = 0;
864  DOMText *tmpTextNode = createDOMText(docOwner, (const char *const)tmpBuff);
865  node->appendChild(tmpTextNode);
866  return E_SUCCESS;
867 }
868 
869 
870 SINT32 CAASymCipher::getPublicKeyAsDOMElement(DOMElement *&elemRoot, XERCES_CPP_NAMESPACE::DOMDocument *docOwner)
871 {
872  if (m_pRSA == NULL)
873  return E_UNKNOWN;
874  elemRoot = createDOMElement(docOwner, "RSAKeyValue");
875  BIGNUM* n = NULL;
876  BIGNUM* e = NULL;
877 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
878  RSA_get0_key(m_pRSA,(const BIGNUM**) &n,(const BIGNUM**) &e, NULL );
879 #else
880  n = m_pRSA->n;
881  e = m_pRSA->e;
882 #endif
883  addKeyPart(elemRoot, docOwner, "Modulus", n);
884  addKeyPart(elemRoot, docOwner, "Exponent", e);
885 
886  return E_SUCCESS;
887 }
888 
889 
890 #endif
891 
892 
void setRSAFlags(RSA *pRSA)
#define RSA_SIZE
SINT32 getcurrentTimeMillis(UINT64 &u64Time)
Gets the current Systemtime in milli seconds.
Definition: CAUtil.cpp:252
SINT32 getDOMElementValue(const DOMNode *const pElem, UINT8 *value, UINT32 *valuelen)
Returns the content of the text node(s) under elem as null-terminated C String.
Definition: CAUtil.cpp:746
bool equals(const XMLCh *const e1, const char *const e2)
Definition: CAUtil.cpp:645
XERCES_CPP_NAMESPACE::DOMDocument * parseDOMDocument(const UINT8 *const buff, UINT32 len)
Parses a buffer containing an XML document and returns this document.
Definition: CAUtil.cpp:663
SINT32 getRandom(UINT32 *val)
Gets 32 random bits.
Definition: CAUtil.cpp:346
DOMText * createDOMText(XERCES_CPP_NAMESPACE::DOMDocument *pOwnerDoc, const char *const text)
Creates a new DOMText with the given value which belongs to the DOMDocument owernDoc.
Definition: CAUtil.cpp:822
XERCES_CPP_NAMESPACE::DOMDocument * createDOMDocument()
Parses a timestamp in JDBC timestamp escape format (as it comes from the BI) and outputs the value in...
Definition: CAUtil.cpp:1568
DOMElement * createDOMElement(XERCES_CPP_NAMESPACE::DOMDocument *pOwnerDoc, const char *const name)
Creates a new DOMElement with the given name which belongs to the DOMDocument owernDoc.
Definition: CAUtil.cpp:814
UINT32 diff64(const UINT64 &bigop, const UINT64 &smallop)
Definition: CAUtil.hpp:398
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
static SINT32 testSpeed()
SINT32 getPublicKeyAsXML(UINT8 *buff, UINT32 *len)
Stores the public key in buff.
SINT32 addKeyPart(DOMElement *elemRoot, XERCES_CPP_NAMESPACE::DOMDocument *docOwner, const char *partName, BIGNUM *part)
SINT32 encryptPKCS1(const UINT8 *from, UINT32 fromlen, UINT8 *to, UINT32 *len)
Encrypts one block of plain text using PKCS1 padding.
SINT32 getPublicKeyAsDOMElement(DOMElement *&elemRoot, XERCES_CPP_NAMESPACE::DOMDocument *docOwner)
SINT32 setPublicKeyAsDOMNode(DOMNode *node)
SINT32 setPublicKeyAsXML(const UINT8 *buff, UINT32 len)
Sets the public key to the values stored in key.
SINT32 generateKeyPair(UINT32 size)
Generates a new random key-pair of size bits.
SINT32 decryptOAEP(const UINT8 *from, UINT8 *to, UINT32 *len)
Decrypts one OAEP encoded block which is stored in from.
SINT32 encryptOAEP(const UINT8 *from, UINT32 fromlen, UINT8 *to, UINT32 *len)
Encrypts one block of plain text using OAEP padding.
SINT32 decrypt(const UINT8 *from, UINT8 *to)
Decrypts exactly one block which is stored in from.
SINT32 destroy()
SINT32 encrypt(const UINT8 *from, UINT8 *to)
Encrypts exactly one block which is stored in from.
SINT32 setPublicKey(const CACertificate *pCert)
Sets the public key which is used for encryption to the contained in the provided certificate.
SINT32 getKeyPart(BIGNUM **part, DOMNode *node)
static SINT32 encode(const UINT8 *in, UINT32 len, UINT8 *out, UINT32 *outlen)
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
Definition: CABase64.cpp:102
static SINT32 decode(const UINT8 *in, UINT32 len, UINT8 *out, UINT32 *outlen)
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
Definition: CABase64.cpp:41
static SINT32 dumpToMem(const DOMNode *node, UINT8 *buff, UINT32 *size)
Dumps the node and all childs into buff.
Definition: DOM_Output.hpp:161
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
UINT16 len
Definition: typedefs.hpp:0