Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
CAASymCipher Class Reference

#include <CAASymCipher.hpp>

List of all members.

Public Member Functions

 CAASymCipher ()
 ~CAASymCipher ()
SINT32 destroy ()
SINT32 decrypt (const UINT8 *from, UINT8 *to)
 Decrypts exactly one block which is stored in from.
SINT32 decryptOAEP (const UINT8 *from, UINT8 *to, UINT32 *len)
 Decrypts one OAEP encoded block which is stored in from.
SINT32 encrypt (const UINT8 *from, UINT8 *to)
 Encrypts exactly one 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 encryptPKCS1 (const UINT8 *from, UINT32 fromlen, UINT8 *to, UINT32 *len)
 Encrypts one block of plain text using PKCS1 padding.
SINT32 generateKeyPair (UINT32 size)
 Generates a new random key-pair of size bits.
SINT32 getPublicKeyAsXML (UINT8 *buff, UINT32 *len)
 Stores the public key in buff as XML.
SINT32 getPublicKeyAsDOMElement (DOMElement *&elemRoot, XERCES_CPP_NAMESPACE::DOMDocument *docOwner)
SINT32 setPublicKey (const CACertificate *pCert)
 Sets the public key which is used for encryption to the contained in the provided certificate.
SINT32 setPublicKeyAsXML (const UINT8 *buff, UINT32 len)
 Sets the public key to the values stored in key.
SINT32 setPublicKeyAsDOMNode (DOMNode *node)
SINT32 setPublicKey (const UINT8 *modulus, UINT32 moduluslen, const UINT8 *exponent, UINT32 exponentlen)

Static Public Member Functions

static SINT32 testSpeed ()

Private Member Functions

SINT32 addKeyPart (DOMElement *elemRoot, XERCES_CPP_NAMESPACE::DOMDocument *docOwner, const char *partName, BIGNUM *part)
 Stores the public key in buff.
SINT32 getKeyPart (BIGNUM **part, DOMNode *node)

Private Attributes

RSA * m_pRSA

Detailed Description

Definition at line 34 of file CAASymCipher.hpp.


Constructor & Destructor Documentation

Definition at line 34 of file CAASymCipher.cpp.

References m_pRSA.

Referenced by testSpeed().

  {
    m_pRSA=NULL;
  }

Definition at line 39 of file CAASymCipher.cpp.

References destroy().

  {
    destroy();
  }

Here is the call graph for this function:


Member Function Documentation

SINT32 CAASymCipher::addKeyPart ( DOMElement *  elemRoot,
XERCES_CPP_NAMESPACE::DOMDocument *  docOwner,
const char *  partName,
BIGNUM *  part 
) [private]

Stores the public key in buff.

The format is as follows:

  • SIZE-N [2 bytes] - number of bytes which are needed for the modulus n (in network byte order..)
  • N [SIZE-N bytes] - the modulus n as integer (in network byte order)
  • SIZE-E [2 bytes] - number of bytes which are needed for the exponent e (in network byte order..)
  • E [SIZE-E bytes] - the exponent e as integer (in network byte order)
Parameters:
buffbyte array in which the public key should be stored
lenon input holds the size of buff, on return it contains the number of bytes needed to store the public key
Return values:
E_UNKNOWNin case of an error
E_SUCCESSotherwise
See also:
getPublicKeySize()
setPublicKey() Returns the number of bytes needed to store we public key. This is the number of bytes needed for a call of getPublicKey().
Returns:
E_UNKOWN in case of an error number of bytes otherwise
See also:
getPublicKey Sets the public key to the vaules stored in key. The format must match the format described for getPublicKey().
Parameters:
keybyte array which holds the new public key
lenon input,size of key byte array, on successful return number of bytes 'consumed'
Return values:
E_UNKNOWNin case of an error, the cipher is the uninitialized (no key is set)
E_SUCCESSotherwise
See also:
getPublicKey

Definition at line 267 of file CAASymCipher.cpp.

References createDOMElement(), createDOMText(), E_SUCCESS, and CABase64::encode().

Referenced by getPublicKeyAsDOMElement().

  {
    DOMElement* node=createDOMElement(docOwner,partName);
    elemRoot->appendChild(node);
    UINT8 tmpBuff[256];
    UINT32 size=256;
    BN_bn2bin(part,tmpBuff);
    CABase64::encode(tmpBuff,BN_num_bytes(part),tmpBuff,&size);
    tmpBuff[size]=0;
    DOMText* tmpTextNode=createDOMText(docOwner,(const char* const)tmpBuff);
    node->appendChild(tmpTextNode);
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 CAASymCipher::decrypt ( const UINT8 from,
UINT8 to 
)

Decrypts exactly one block which is stored in from.

The result of the decryption is stored in to.

Parameters:
fromone block of cipher text
tothe decrypted plain text
Return values:
E_UNKNOWNin case of an error
E_SUCCESSotherwise

Definition at line 71 of file CAASymCipher.cpp.

References E_SUCCESS, E_UNKNOWN, m_pRSA, and RSA_SIZE.

Referenced by decodeXMLEncryptedKey(), and CALastMixB::loop().

  {
    if(RSA_private_decrypt(RSA_SIZE,from,to,m_pRSA,RSA_NO_PADDING)==-1)
      return E_UNKNOWN;
    else
      return E_SUCCESS;
  }
SINT32 CAASymCipher::decryptOAEP ( const UINT8 from,
UINT8 to,
UINT32 len 
)

Decrypts one OAEP encoded block which is stored in from.

Parameters:
fromone OAEP encoded block of cipher text
tothe plain text
lenon return contains the size of the plaintext
Return values:
E_UNKNOWNin case of an error
E_SUCCESSotherwise

Definition at line 86 of file CAASymCipher.cpp.

References E_SUCCESS, E_UNKNOWN, m_pRSA, and RSA_SIZE.

Referenced by decryptXMLElement(), CALastMixA::loop(), mm_loopReadFromMixBefore(), and testSpeed().

  {
    SINT32 ret=RSA_private_decrypt(RSA_SIZE,from,to,m_pRSA,RSA_PKCS1_OAEP_PADDING);
    if(ret<0)
      return E_UNKNOWN;
    *len=ret;
    return E_SUCCESS;
  }

Definition at line 44 of file CAASymCipher.cpp.

References E_SUCCESS, and m_pRSA.

Referenced by ~CAASymCipher().

  {
    RSA_free(m_pRSA);
    m_pRSA=NULL;
    return E_SUCCESS;
  }
SINT32 CAASymCipher::encrypt ( const UINT8 from,
UINT8 to 
)

Encrypts exactly one block which is stored in from.

The result of the encrpytion is stored in to.

Parameters:
fromone block of plain text
tothe encrypted cipher text
Return values:
E_UNKNOWNin case of an error
E_SUCCESSotherwise

Definition at line 137 of file CAASymCipher.cpp.

References E_SUCCESS, E_UNKNOWN, m_pRSA, and RSA_SIZE.

Referenced by __encryptKey(), CALocalProxy::loop(), CAMsg::openEncryptedLog(), and CALocalProxy::processKeyExchange().

  {
    if(RSA_public_encrypt(RSA_SIZE,from,to,m_pRSA,RSA_NO_PADDING)==-1)
      return E_UNKNOWN;
    else
      return E_SUCCESS;
  }
SINT32 CAASymCipher::encryptOAEP ( const UINT8 from,
UINT32  fromlen,
UINT8 to,
UINT32 len 
)

Encrypts one block of plain text using OAEP padding.

Parameters:
frompointer to one block of plain text
fromlensize of the plain text
tothe OAEP encoded cipher text
lenon return contains the size of the ciphertext
Return values:
E_UNKNOWNin case of an error
E_SUCCESSotherwise

Definition at line 103 of file CAASymCipher.cpp.

References E_SUCCESS, E_UNKNOWN, and m_pRSA.

Referenced by encryptXMLElement(), CALocalProxy::loop(), and testSpeed().

  {
    SINT32 ret=RSA_public_encrypt(fromlen,from,to,m_pRSA,RSA_PKCS1_OAEP_PADDING);
    if(ret<0)
      return E_UNKNOWN;
    *len=ret;
    return E_SUCCESS;
  }
SINT32 CAASymCipher::encryptPKCS1 ( const UINT8 from,
UINT32  fromlen,
UINT8 to,
UINT32 len 
)

Encrypts one block of plain text using PKCS1 padding.

Parameters:
frompointer to one block of plain text
fromlensize of the plain text
tothe OAEP encoded cipher text
lenon return contains the size of the ciphertext
Return values:
E_UNKNOWNin case of an error
E_SUCCESSotherwise Temporarly will be removed soon.

Definition at line 121 of file CAASymCipher.cpp.

References E_SUCCESS, E_UNKNOWN, and m_pRSA.

  {
    SINT32 ret=RSA_public_encrypt(fromlen,from,to,m_pRSA,RSA_PKCS1_PADDING);
    if(ret<0)
      return E_UNKNOWN;
    *len=ret;
    return E_SUCCESS;
  }

Generates a new random key-pair of size bits.

Parameters:
sizekeysize of the new keypair
Return values:
E_UNKNOWNin case of an error
E_SUCCESSotherwise

Definition at line 150 of file CAASymCipher.cpp.

References E_SUCCESS, E_UNKNOWN, m_pRSA, and setRSAFlags().

Referenced by CAMiddleMix::init(), CALastMix::init(), CAFirstMix::processKeyExchange(), and testSpeed().

  {
    RSA_free(m_pRSA);
    m_pRSA=RSA_generate_key(size,65537,NULL,NULL);
    setRSAFlags(m_pRSA);
    if(m_pRSA==NULL)
      return E_UNKNOWN;
    else
      return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 CAASymCipher::getKeyPart ( BIGNUM **  part,
DOMNode *  node 
) [private]

Definition at line 520 of file CAASymCipher.cpp.

References CABase64::decode(), E_SUCCESS, and getDOMElementValue().

Referenced by setPublicKeyAsDOMNode().

  {
    if(*part!=NULL)
      BN_free(*part);
    UINT8* tmpStr=new UINT8[4096];
    UINT32 tmpStrLen=4096;
    getDOMElementValue(node,tmpStr,&tmpStrLen);
    UINT8 decBuff[4096];
    UINT32 decLen=4096;
    CABase64::decode(tmpStr,tmpStrLen,decBuff,&decLen);
    delete []tmpStr;
    tmpStr = NULL;
    *part=BN_bin2bn(decBuff,decLen,NULL);
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 CAASymCipher::getPublicKeyAsDOMElement ( DOMElement *&  elemRoot,
XERCES_CPP_NAMESPACE::DOMDocument *  docOwner 
)

Definition at line 317 of file CAASymCipher.cpp.

References addKeyPart(), createDOMElement(), E_SUCCESS, E_UNKNOWN, and m_pRSA.

Referenced by getPublicKeyAsXML(), CAMiddleMix::processKeyExchange(), CALastMix::processKeyExchange(), and CAFirstMix::processKeyExchange().

  {
    if(m_pRSA==NULL)
      return E_UNKNOWN;
    elemRoot=createDOMElement(docOwner,"RSAKeyValue");
    
    addKeyPart(elemRoot,docOwner,"Modulus",m_pRSA->n);
    addKeyPart(elemRoot,docOwner,"Exponent",m_pRSA->e);

    return E_SUCCESS;
  }

Here is the call graph for this function:

Stores the public key in buff as XML.

The format is as follows:

	<RSAKeyValue>
	  <Modulus>
	    the modulus of the Key as ds::CryptoBinary
	  </Modulus>
	  <Exponent>
	    the exponent of the key as ds::CryptoBinary
	  </Exponent>
	<RSAKeyValue>

There is NO \0 at the end.

Parameters:
buffbyte array in which the public key should be stored
lenon input holds the size of buff, on return it contains the number of bytes needed to store the public key
Return values:
E_UNKNOWNin case of an error
E_SUCCESSotherwise
See also:
setPublicKeyAsXML()

Definition at line 301 of file CAASymCipher.cpp.

References createDOMDocument(), DOM_Output::dumpToMem(), E_SUCCESS, E_UNKNOWN, getPublicKeyAsDOMElement(), and m_pRSA.

  {
    if(m_pRSA==NULL||buff==NULL)
      return E_UNKNOWN;
    XERCES_CPP_NAMESPACE::DOMDocument* pDoc=createDOMDocument();
    DOMElement* elemRoot=NULL;
    getPublicKeyAsDOMElement(elemRoot,pDoc);
    DOM_Output::dumpToMem(elemRoot,buff,len);
    if (pDoc != NULL)
    {
      pDoc->release();
      pDoc = NULL;
    }
    return E_SUCCESS;
  }

Here is the call graph for this function:

Sets the public key which is used for encryption to the contained in the provided certificate.

The key has to be a RSA public key.

Return values:
E_SUCCESSif successful
E_UNKNOWNotherwise (in this case the key leaves untouched)

Definition at line 579 of file CAASymCipher.cpp.

References E_SUCCESS, E_UNKNOWN, CACertificate::m_pCert, m_pRSA, and setRSAFlags().

Referenced by CAMsg::openEncryptedLog().

  {
    if(pCert==NULL)
      return E_UNKNOWN;
    EVP_PKEY* pubkey=X509_get_pubkey(pCert->m_pCert);
    if(pubkey==NULL||(pubkey->type!=EVP_PKEY_RSA&&pubkey->type!=EVP_PKEY_RSA2))
      return E_UNKNOWN;
    RSA* r=pubkey->pkey.rsa;
    if(RSA_size(r)!=128)
      return E_UNKNOWN;
    if(m_pRSA!=NULL)
      RSA_free(m_pRSA);
    m_pRSA=r;
    setRSAFlags(m_pRSA);
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 CAASymCipher::setPublicKey ( const UINT8 modulus,
UINT32  moduluslen,
const UINT8 exponent,
UINT32  exponentlen 
)

Definition at line 597 of file CAASymCipher.cpp.

References CABase64::decode(), E_SUCCESS, E_UNKNOWN, m_pRSA, and setRSAFlags().

  {
    RSA* tmpRSA=RSA_new();
    UINT32 decLen=4096;
    UINT8 decBuff[4096];
    CABase64::decode(m,mlen,decBuff,&decLen);
    tmpRSA->n=BN_bin2bn(decBuff,decLen,NULL);
    decLen=4096;
    CABase64::decode(e,elen,decBuff,&decLen);
    tmpRSA->e=BN_bin2bn(decBuff,decLen,NULL);
    if(tmpRSA->n!=NULL&&tmpRSA->e!=NULL)
      {
        if(m_pRSA!=NULL)
          RSA_free(m_pRSA);
        m_pRSA=tmpRSA;
        setRSAFlags(m_pRSA);
        return E_SUCCESS;
      }
    RSA_free(tmpRSA);
    return E_UNKNOWN;
  }

Here is the call graph for this function:

Definition at line 536 of file CAASymCipher.cpp.

References E_SUCCESS, E_UNKNOWN, equals(), getKeyPart(), m_pRSA, and setRSAFlags().

Referenced by CAMiddleMix::processKeyExchange(), CAFirstMix::processKeyExchange(), and setPublicKeyAsXML().

  { 
    DOMNode* root=node;
    while(root!=NULL)
      { 
        if(equals(root->getNodeName(),"RSAKeyValue"))
          {
            RSA* tmpRSA=RSA_new();
            DOMNode* child=root->getFirstChild();
            while(child!=NULL)
              {
                if(equals(child->getNodeName(),"Modulus"))
                  {
                    getKeyPart(&tmpRSA->n,child);
                  }
                else if(equals(child->getNodeName(),"Exponent"))
                  {
                    getKeyPart(&tmpRSA->e,child);
                  }
                child=child->getNextSibling();
              }
            if(tmpRSA->n!=NULL&&tmpRSA->e!=NULL)
              {
                if(m_pRSA!=NULL)
                  RSA_free(m_pRSA);
                m_pRSA=tmpRSA;
                setRSAFlags(m_pRSA);
                return E_SUCCESS;
              }
            RSA_free(tmpRSA);
            return E_UNKNOWN;
          }
        root=root->getNextSibling();    
      }
    return E_UNKNOWN;
  }

Here is the call graph for this function:

SINT32 CAASymCipher::setPublicKeyAsXML ( const UINT8 key,
UINT32  len 
)

Sets the public key to the values stored in key.

The format must match the format XML described for getPublicKeyAsXML().

Parameters:
keybyte array which holds the new public key
lenon input,size of key byte array, on successful return number of bytes 'consumed'
Return values:
E_UNKNOWNin case of an error, the cipher is the uninitialized (no key is set)
E_SUCCESSotherwise
See also:
getPublicKeyAsXML

Definition at line 499 of file CAASymCipher.cpp.

References E_UNKNOWN, parseDOMDocument(), and setPublicKeyAsDOMNode().

  {
    if(key==NULL)
      return E_UNKNOWN;

    XERCES_CPP_NAMESPACE::DOMDocument* doc=parseDOMDocument(key,len);
    if(doc == NULL)
    {
      return E_UNKNOWN;
    }
    DOMElement* root=doc->getDocumentElement();
    if(root == NULL)
    {
      return E_UNKNOWN;
    }
    return setPublicKeyAsDOMNode(root);
  }   

Here is the call graph for this function:

Definition at line 627 of file CAASymCipher.cpp.

References CAASymCipher(), decryptOAEP(), diff64(), E_SUCCESS, E_UNKNOWN, encryptOAEP(), generateKeyPair(), getcurrentTimeMillis(), and getRandom().

  {
    const UINT32 runs=10000;
    UINT8* inBuff=new UINT8[128];
    UINT8* outBuff=new UINT8[128];
    getRandom(inBuff,128);
    inBuff[0]&=0x7F;
#ifdef INTEL_IPP_CRYPTO
    int size=0;
    ippsRSAGetSize(1024, 512, IppRSAprivate, &size);
    IppsRSAState* pCtx=(IppsRSAState*)new UINT8[size];
    IppStatus ret=ippsRSAInit(1024, 512, IppRSAprivate,pCtx);
    if(ret!=ippStsNoErr)
      {
        printf("Error in RSA init!\n");
        return E_UNKNOWN;
      }
    ippsBigNumGetSize(1, &size);
    IppsBigNumState* pE = (IppsBigNumState*)( new UINT8 [size] );
    ippsBigNumInit(1, pE);
    UINT32 pEValue[]= {0x010001};
    ret=ippsSet_BN(IppsBigNumPOS, 1, pEValue, pE);
    if(ret!=ippStsNoErr)
      {
        printf("Error in setBN(e)!\n");
        return E_UNKNOWN;
      }
    ippsRSAGenerate(pE,1024,512,1024,pCtx, myIppBitSupplier, NULL);
    if(ret!=ippStsNoErr)
      {
        printf("Error in RSA generate key!\n");
        return E_UNKNOWN;
      }

    ippsBigNumGetSize(32, &size);
    IppsBigNumState* pY = (IppsBigNumState*)( new UINT8 [size] );
    ippsBigNumInit(32, pY);

    IppsBigNumState* pX = (IppsBigNumState*)( new UINT8 [size] );
    ippsBigNumInit(32, pX);
    UINT32* pXValue=new UINT32[32];
    memcpy(pXValue,inBuff,128);
    pXValue[0]&=0x7FFFFFFF;
    ippsSet_BN(IppsBigNumPOS, 32, pXValue, pX);

#else
    CAASymCipher* pCipher=new CAASymCipher();
    pCipher->generateKeyPair(1024);
    UINT32 iLen=128;
    pCipher->encryptOAEP(inBuff,86,inBuff,&iLen);
#endif
    UINT64 start,end;
    getcurrentTimeMillis(start);
    for(UINT32 i=0;i<runs;i++)
      {
#ifdef INTEL_IPP_CRYPTO
        IppStatus ret=ippsRSADecrypt(pX,pY,pCtx);
        if(ret!=ippStsNoErr)
        {
          printf("Error in RSADEcrypt %i!\n",ret);
          return E_UNKNOWN;
        }
#else
        pCipher->decryptOAEP(inBuff,outBuff,&iLen);
//        pCipher->decrypt(inBuff,outBuff);
#endif
      }
    getcurrentTimeMillis(end);
    UINT32 d=diff64(end,start);
    printf("CAASymCiper::testSpeed() takes %u ms for %u decrypts!\n",d,runs);
    return E_SUCCESS;
  }

Here is the call graph for this function:


Member Data Documentation

RSA* CAASymCipher::m_pRSA [private]

The documentation for this class was generated from the following files: