pcpngrsassapkcsv15ca.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "pcpngrsa.h"
  34. #include "pcphash.h"
  35. #include "pcptool.h"
  36. static int EMSA_PKCSv15(const Ipp8u* msgDg, int lenMsgDg,
  37. const Ipp8u* fixPS, int lenFixPS,
  38. Ipp8u* pEM, int lenEM)
  39. {
  40. /*
  41. // encoded message format:
  42. // EM = 00 || 01 || PS=(FF..FF) || 00 || T
  43. // T = fixPS || msgDg
  44. // len(PS) >= 8
  45. */
  46. int tLen = lenFixPS + lenMsgDg;
  47. if(lenEM >= tLen+11) {
  48. int psLen = lenEM - 3 - tLen;
  49. PaddBlock(0xFF, pEM, lenEM);
  50. pEM[0] = 0x00;
  51. pEM[1] = 0x01;
  52. pEM[2+psLen] = 0x00;
  53. CopyBlock(fixPS, pEM+3+psLen, lenFixPS);
  54. CopyBlock(msgDg, pEM+3+psLen+lenFixPS, lenMsgDg);
  55. return 1;
  56. }
  57. else
  58. return 0; /* encoded message length too long */
  59. }
  60. /*
  61. // The DER encoding T of the DigestInfo value is equal to the following (see PKCS-1v2-2):
  62. */
  63. static const Ipp8u SHA1_fixPS[] = "\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14";
  64. static const Ipp8u SHA224_fixPS[] = "\x30\x2d\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04\x05\x00\x04\x1c";
  65. static const Ipp8u SHA256_fixPS[] = "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20";
  66. static const Ipp8u SHA384_fixPS[] = "\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02\x05\x00\x04\x30";
  67. static const Ipp8u SHA512_fixPS[] = "\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40";
  68. static const Ipp8u MD5_fixPS[] = "\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10";
  69. static const Ipp8u SHA512_224_fixPS[] = "\x30\x2d\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x05\x05\x00\x04\x1c";
  70. static const Ipp8u SHA512_256_fixPS[] = "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x06\x05\x00\x04\x20";
  71. typedef struct {
  72. const Ipp8u* pSalt;
  73. int saltLen;
  74. } SaltInfo;
  75. static SaltInfo pksc15_salt[] = {
  76. {NULL, 0},
  77. {SHA1_fixPS, sizeof(SHA1_fixPS)-1},
  78. {SHA256_fixPS, sizeof(SHA256_fixPS)-1},
  79. {SHA224_fixPS, sizeof(SHA224_fixPS)-1},
  80. {SHA512_fixPS, sizeof(SHA512_fixPS)-1},
  81. {SHA384_fixPS, sizeof(SHA384_fixPS)-1},
  82. { MD5_fixPS, sizeof(MD5_fixPS)-1},
  83. {NULL, 0},
  84. {SHA512_224_fixPS, sizeof(SHA512_224_fixPS)-1},
  85. {SHA512_256_fixPS, sizeof(SHA512_256_fixPS)-1},
  86. };
  87. /* //////////////////////////////////////////////////////////////////////////////////////////////// */
  88. static int GenerateSing(const Ipp8u* pMsg, int msgLen, /* message representation */
  89. const Ipp8u* pSalt, int saltLen, /* fied string */
  90. Ipp8u* pSign,
  91. const IppsRSAPrivateKeyState* pPrvKey,
  92. const IppsRSAPublicKeyState* pPubKey,
  93. Ipp8u* pBuffer)
  94. {
  95. /* size of RSA modulus in bytes and chunks */
  96. cpSize rsaBits = RSA_PRV_KEY_BITSIZE_N(pPrvKey);
  97. cpSize k = BITS2WORD8_SIZE(rsaBits);
  98. cpSize nsN = BITS_BNU_CHUNK(rsaBits);
  99. /* EMSA-PKCS-v1_5 encoding */
  100. int result = EMSA_PKCSv15(pMsg,msgLen, pSalt,saltLen, pSign, k);
  101. if(result) {
  102. /* align buffer */
  103. BNU_CHUNK_T* pScratchBuffer = (BNU_CHUNK_T*)(IPP_ALIGNED_PTR(pBuffer, (int)sizeof(BNU_CHUNK_T)) );
  104. /* temporary BNs */
  105. __ALIGN8 IppsBigNumState bnC;
  106. __ALIGN8 IppsBigNumState bnP;
  107. /* make BNs */
  108. BN_Make(pScratchBuffer, pScratchBuffer+nsN+1, nsN, &bnC);
  109. pScratchBuffer += (nsN+1)*2;
  110. BN_Make(pScratchBuffer, pScratchBuffer+nsN+1, nsN, &bnP);
  111. pScratchBuffer += (nsN+1)*2;
  112. /*
  113. // private-key operation
  114. */
  115. ippsSetOctString_BN(pSign, k, &bnC);
  116. if(RSA_PRV_KEY1_VALID_ID(pPrvKey))
  117. gsRSAprv_cipher(&bnP, &bnC, pPrvKey, pScratchBuffer);
  118. else
  119. gsRSAprv_cipher_crt(&bnP, &bnC, pPrvKey, pScratchBuffer);
  120. ippsGetOctString_BN(pSign, k, &bnP);
  121. /* check the result before send it out (fault attack mitigatioin) */
  122. if(pPubKey) {
  123. gsRSApub_cipher(&bnP, &bnP, pPubKey, pScratchBuffer);
  124. /* check signature before send it out (fault attack mitigatioin) */
  125. if(0!=cpBN_cmp(&bnP, &bnC)) {
  126. PaddBlock(0, pSign, k);
  127. result = 0;
  128. }
  129. }
  130. }
  131. return result;
  132. }
  133. IPPFUN(IppStatus, ippsRSASign_PKCS1v15,(const Ipp8u* pMsg, int msgLen,
  134. Ipp8u* pSign,
  135. const IppsRSAPrivateKeyState* pPrvKey,
  136. const IppsRSAPublicKeyState* pPubKey,
  137. IppHashAlgId hashAlg,
  138. Ipp8u* pBuffer))
  139. {
  140. /* test private key context */
  141. IPP_BAD_PTR2_RET(pPrvKey, pBuffer);
  142. pPrvKey = (IppsRSAPrivateKeyState*)( IPP_ALIGNED_PTR(pPrvKey, RSA_PRIVATE_KEY_ALIGNMENT) );
  143. IPP_BADARG_RET(!RSA_PRV_KEY_VALID_ID(pPrvKey), ippStsContextMatchErr);
  144. IPP_BADARG_RET(!RSA_PRV_KEY_IS_SET(pPrvKey), ippStsIncompleteContextErr);
  145. /* test hash algorith ID */
  146. hashAlg = cpValidHashAlg(hashAlg);
  147. IPP_BADARG_RET(ippHashAlg_Unknown==hashAlg, ippStsNotSupportedModeErr);
  148. /* use aligned public key context if defined */
  149. if(pPubKey) {
  150. pPubKey = (IppsRSAPublicKeyState*)( IPP_ALIGNED_PTR(pPubKey, RSA_PUBLIC_KEY_ALIGNMENT) );
  151. IPP_BADARG_RET(!RSA_PUB_KEY_VALID_ID(pPubKey), ippStsContextMatchErr);
  152. IPP_BADARG_RET(!RSA_PUB_KEY_IS_SET(pPubKey), ippStsIncompleteContextErr);
  153. }
  154. /* test data pointer */
  155. IPP_BAD_PTR2_RET(pMsg, pSign);
  156. /* test length */
  157. IPP_BADARG_RET(msgLen<0, ippStsLengthErr);
  158. {
  159. Ipp8u md[IPP_SHA512_DIGEST_BITSIZE/BYTESIZE];
  160. int mdLen = cpHashSize(hashAlg);
  161. ippsHashMessage(pMsg, msgLen, md, hashAlg);
  162. {
  163. const Ipp8u* pSalt = pksc15_salt[hashAlg].pSalt;
  164. int saltLen = pksc15_salt[hashAlg].saltLen;
  165. int sts = GenerateSing(md, mdLen,
  166. pSalt, saltLen,
  167. pSign,
  168. pPrvKey, pPubKey, pBuffer);
  169. return (1==sts)? ippStsNoErr : ippStsSizeErr;
  170. }
  171. }
  172. }
  173. ////////////////////////////////////////////////////////////////////////////////////////////////////
  174. static int VerifySing(const Ipp8u* pMsg, int msgLen, /* message representation */
  175. const Ipp8u* pSalt, int saltLen, /* fied string */
  176. const Ipp8u* pSign,
  177. int* pIsValid,
  178. const IppsRSAPublicKeyState* pKey,
  179. Ipp8u* pBuffer)
  180. {
  181. /* size of RSA modulus in bytes and chunks */
  182. cpSize rsaBits = RSA_PUB_KEY_BITSIZE_N(pKey);
  183. cpSize k = BITS2WORD8_SIZE(rsaBits);
  184. cpSize nsN = BITS_BNU_CHUNK(rsaBits);
  185. /* align buffer */
  186. BNU_CHUNK_T* pScratchBuffer = (BNU_CHUNK_T*)(IPP_ALIGNED_PTR(pBuffer, (int)sizeof(BNU_CHUNK_T)) );
  187. /* temporary BNs */
  188. __ALIGN8 IppsBigNumState bnC;
  189. __ALIGN8 IppsBigNumState bnP;
  190. /* make BNs */
  191. BN_Make(pScratchBuffer, pScratchBuffer+nsN+1, nsN, &bnC);
  192. pScratchBuffer += (nsN+1)*2;
  193. BN_Make(pScratchBuffer, pScratchBuffer+nsN+1, nsN, &bnP);
  194. pScratchBuffer += (nsN+1)*2;
  195. /*
  196. // public-key operation
  197. */
  198. ippsSetOctString_BN(pSign, k, &bnP);
  199. gsRSApub_cipher(&bnC, &bnP, pKey, pScratchBuffer);
  200. /* convert EM into the string */
  201. ippsGetOctString_BN((Ipp8u*)(BN_BUFFER(&bnC)), k, &bnC);
  202. /* EMSA-PKCS-v1_5 encoding */
  203. if( EMSA_PKCSv15(pMsg,msgLen, pSalt,saltLen, (Ipp8u*)(BN_NUMBER(&bnC)), k) ) {
  204. *pIsValid = 1==EquBlock((Ipp8u*)(BN_BUFFER(&bnC)), (Ipp8u*)(BN_NUMBER(&bnC)), k);
  205. return 1;
  206. }
  207. else
  208. return 0;
  209. }
  210. IPPFUN(IppStatus, ippsRSAVerify_PKCS1v15,(const Ipp8u* pMsg, int msgLen,
  211. const Ipp8u* pSign, int* pIsValid,
  212. const IppsRSAPublicKeyState* pKey,
  213. IppHashAlgId hashAlg,
  214. Ipp8u* pBuffer))
  215. {
  216. /* test public key context */
  217. IPP_BAD_PTR2_RET(pKey, pBuffer);
  218. pKey = (IppsRSAPublicKeyState*)( IPP_ALIGNED_PTR(pKey, RSA_PUBLIC_KEY_ALIGNMENT) );
  219. IPP_BADARG_RET(!RSA_PUB_KEY_VALID_ID(pKey), ippStsContextMatchErr);
  220. IPP_BADARG_RET(!RSA_PUB_KEY_IS_SET(pKey), ippStsIncompleteContextErr);
  221. /* test hash algorith ID */
  222. hashAlg = cpValidHashAlg(hashAlg);
  223. IPP_BADARG_RET(ippHashAlg_Unknown==hashAlg, ippStsNotSupportedModeErr);
  224. /* test data pointer */
  225. IPP_BAD_PTR3_RET(pMsg, pSign, pIsValid);
  226. /* test length */
  227. IPP_BADARG_RET(msgLen<0, ippStsLengthErr);
  228. *pIsValid = 0;
  229. {
  230. Ipp8u md[IPP_SHA512_DIGEST_BITSIZE/BYTESIZE];
  231. int mdLen = cpHashSize(hashAlg);
  232. ippsHashMessage(pMsg, msgLen, md, hashAlg);
  233. return VerifySing(md, mdLen,
  234. pksc15_salt[hashAlg].pSalt, pksc15_salt[hashAlg].saltLen,
  235. pSign, pIsValid,
  236. pKey, pBuffer)? ippStsNoErr : ippStsSizeErr;
  237. }
  238. }