pcpngrsaesoaepca.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "owndefs.h"
  32. #include "owncp.h"
  33. #include "pcptool.h"
  34. #include "pcpngrsa.h"
  35. #include "pcphash.h"
  36. /*F*
  37. // Name: ippsRSAEncrypt_OAEP
  38. //
  39. // Purpose: Performs RSAES-OAEP encryprion scheme
  40. //
  41. // Returns: Reason:
  42. // ippStsNotSupportedModeErr unknown hashAlg
  43. //
  44. // ippStsNullPtrErr NULL == pKey
  45. // NULL == pSrc
  46. // NULL == pDst
  47. // NULL == pLabel
  48. // NULL == pSeed
  49. // NULL == pBuffer
  50. //
  51. // ippStsLengthErr srcLen <0
  52. // labLen <0
  53. // srcLen > RSAsize -2*hashLen -2
  54. // RSAsize < 2*hashLen +2
  55. //
  56. // ippStsContextMatchErr !RSA_PUB_KEY_VALID_ID()
  57. //
  58. // ippStsIncompleteContextErr public key is not set up
  59. //
  60. // ippStsNoErr no error
  61. //
  62. // Parameters:
  63. // pSrc pointer to the plaintext
  64. // srcLen plaintext length (bytes)
  65. // pLabel (optional) pointer to the label associated with plaintext
  66. // labLen label length (bytes)
  67. // pSeed seed string of hashLen size
  68. // pDst pointer to the ciphertext (length of pdst is not less then size of RSA modulus)
  69. // pKey pointer to the RSA public key context
  70. // hashAlg hash alg ID
  71. // pBuffer pointer to scratch buffer
  72. *F*/
  73. IPPFUN(IppStatus, ippsRSAEncrypt_OAEP,(const Ipp8u* pSrc, int srcLen,
  74. const Ipp8u* pLabel, int labLen,
  75. const Ipp8u* pSeed,
  76. Ipp8u* pDst,
  77. const IppsRSAPublicKeyState* pKey,
  78. IppHashAlgId hashAlg,
  79. Ipp8u* pBuffer))
  80. {
  81. int hashLen;
  82. /* test hash algorith ID */
  83. hashAlg = cpValidHashAlg(hashAlg);
  84. IPP_BADARG_RET(ippHashAlg_Unknown==hashAlg, ippStsNotSupportedModeErr);
  85. /* test data pointer */
  86. IPP_BAD_PTR3_RET(pSrc,pDst, pSeed);
  87. IPP_BADARG_RET(!pLabel && labLen, ippStsNullPtrErr);
  88. /* test public key context */
  89. IPP_BAD_PTR2_RET(pKey, pBuffer);
  90. pKey = (IppsRSAPublicKeyState*)( IPP_ALIGNED_PTR(pKey, RSA_PUBLIC_KEY_ALIGNMENT) );
  91. IPP_BADARG_RET(!RSA_PUB_KEY_VALID_ID(pKey), ippStsContextMatchErr);
  92. IPP_BADARG_RET(!RSA_PUB_KEY_IS_SET(pKey), ippStsIncompleteContextErr);
  93. /* test length */
  94. IPP_BADARG_RET(srcLen<0||labLen<0, ippStsLengthErr);
  95. hashLen = cpHashSize(hashAlg);
  96. /* test compatibility of RSA and hash length */
  97. IPP_BADARG_RET(BITS2WORD8_SIZE(RSA_PRV_KEY_BITSIZE_N(pKey)) < (2*hashLen +2), ippStsLengthErr);
  98. /* test compatibility of msg length and other (RSA and hash) lengths */
  99. IPP_BADARG_RET(BITS2WORD8_SIZE(RSA_PRV_KEY_BITSIZE_N(pKey))-(2*hashLen +2) < srcLen, ippStsLengthErr);
  100. {
  101. /* size of RSA modulus in bytes and chunks */
  102. int k = BITS2WORD8_SIZE(RSA_PUB_KEY_BITSIZE_N(pKey));
  103. cpSize nsN = BITS_BNU_CHUNK(RSA_PUB_KEY_BITSIZE_N(pKey));
  104. /*
  105. // EME-OAEP encoding
  106. */
  107. {
  108. Ipp8u seedMask[BITS2WORD8_SIZE(IPP_SHA512_DIGEST_BITSIZE)];
  109. Ipp8u* pMaskedSeed = pDst+1;
  110. Ipp8u* pMaskedDB = pDst +hashLen +1;
  111. pDst[0] = 0;
  112. /* maskedDB = MGF(seed, k-1-hashLen)*/
  113. ippsMGF(pSeed, hashLen, pMaskedDB, k-1-hashLen, hashAlg);
  114. /* seedMask = HASH(pLab) */
  115. ippsHashMessage(pLabel, labLen, seedMask, hashAlg);
  116. /* maskedDB ^= concat(HASH(pLab),PS,0x01,pSc) */
  117. XorBlock(pMaskedDB, seedMask, pMaskedDB, hashLen);
  118. pMaskedDB[k-srcLen-hashLen-2] ^= 0x01;
  119. XorBlock(pMaskedDB+k-srcLen-hashLen-2+1, pSrc, pMaskedDB+k-srcLen-hashLen-2+1, srcLen);
  120. /* seedMask = MGF(maskedDB, hashLen) */
  121. ippsMGF(pMaskedDB, k-1-hashLen, seedMask, hashLen, hashAlg);
  122. /* maskedSeed = seed ^ seedMask */
  123. XorBlock(pSeed, seedMask, pMaskedSeed, hashLen);
  124. }
  125. /* RSA encryption */
  126. {
  127. /* align buffer */
  128. BNU_CHUNK_T* pScratchBuffer = (BNU_CHUNK_T*)(IPP_ALIGNED_PTR(pBuffer, (int)sizeof(BNU_CHUNK_T)) );
  129. /* temporary BN */
  130. __ALIGN8 IppsBigNumState tmpBN;
  131. BN_Make(pScratchBuffer, pScratchBuffer+nsN+1, nsN, &tmpBN);
  132. /* updtae buffer pointer */
  133. pScratchBuffer += (nsN+1)*2;
  134. ippsSetOctString_BN(pDst, k, &tmpBN);
  135. gsRSApub_cipher(&tmpBN, &tmpBN, pKey, pScratchBuffer);
  136. ippsGetOctString_BN(pDst, k, &tmpBN);
  137. }
  138. return ippStsNoErr;
  139. }
  140. }
  141. IPPFUN(IppStatus, ippsRSA_OAEPEncrypt_SHA256,(const Ipp8u* pSrc, int srcLen,
  142. const Ipp8u* pLabel, int labLen,
  143. const Ipp8u* pSeed,
  144. Ipp8u* pDst,
  145. const IppsRSAPublicKeyState* pKey,
  146. Ipp8u* pBuffer))
  147. { return ippsRSAEncrypt_OAEP(pSrc,srcLen, pLabel,labLen, pSeed,
  148. pDst, pKey,
  149. IPP_ALG_HASH_SHA256,
  150. pBuffer); }