eccapi.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**
  2. Copyright (c) Certicom Corp. 1996-2008. All rights reserved.
  3. This software contains trade secrets, confidential information, and
  4. other intellectual property of Certicom Corp. and its licensors.
  5. This software cannot be used, reproduced, or distributed in whole or
  6. in part by any means without the explicit prior consent of Certicom
  7. Corp.
  8. Such consent must arise from a separate license agreement from
  9. Certicom or its licensees, as appropriate. This software implements
  10. Canadian, U.S., and other nations' patents, both registered and
  11. pending (see the readme file included in this distribution).
  12. Warning: U.S. Federal and state laws and international treaties
  13. provide severe civil and criminal penalties for the unauthorized
  14. reproduction in any form of these copyrighted materials.
  15. Criminal copyright infringement constitutes a felony and is
  16. punishable by a $250,000 fine and up to 5 years in prison.
  17. Certicom, Certicom Security Architecture, Certicom CodeSign, Security
  18. Builder, Security Builder BSP, Security Builder API, Security
  19. Builder Crypto, Security Builder SSL, Security Builder PKI, Security
  20. Builder NSE and Security Builder GSE are trademarks or registered
  21. trademarks of Certicom Corp.
  22. All other trademarks or registered trademarks listed herein are
  23. property of their respective owners.
  24. Certicom Corp. has intellectual property rights relating to
  25. technology embodied in this product.
  26. In particular, and without limitation, these intellectual property
  27. rights may include one or more of the U.S. and non-U.S. patents listed
  28. at www.certicom.com/patents
  29. and one or more additional patents or pending patent applications in
  30. the U.S. and in other countries.
  31. Information subject to change without notice.
  32. Certicom Corp. Technical Support
  33. Email: support@certicom.com
  34. Vox: 1-800-511-8011
  35. Fax: 1-800-474-3877
  36. */
  37. /*****************************************************************************
  38. *
  39. * eccapi.h
  40. *
  41. *****************************************************************************/
  42. #ifndef ECCAPI_H
  43. #define ECCAPI_H
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. #define ZSE_MAJOR 1
  48. #define ZSE_MINOR 0
  49. #define ZSE_PATCH 1
  50. #define ZSE_BUILD 6
  51. #define ZSE_VERSION ((unsigned long)(ZSE_MAJOR & 0x0F)<< 28 | (unsigned long)(ZSE_MINOR & 0x0F) << 24 | (unsigned long)(ZSE_PATCH & 0x0FFF) << 12 | (unsigned long)(ZSE_BUILD & 0x0FFF))
  52. #define SECT163K1_COMPRESSED_PUBLIC_KEY_SIZE (22)
  53. #define SECT163K1_UNCOMPRESSED_PUBLIC_KEY_SIZE (43)
  54. #define SECT163K1_PRIVATE_KEY_SIZE (21)
  55. #define SECT163K1_CERTIFICATE_SIZE (48)
  56. #define SECT163K1_SHARED_SECRET_SIZE (21)
  57. #define SECT163K1_POINT_ORDER_SIZE (21)
  58. #define AES_MMO_HASH_SIZE (16)
  59. /******************************************************************************
  60. * Return Codes
  61. *****************************************************************************/
  62. #define MCE_SUCCESS 0x00
  63. #define MCE_ERR_FAIL_VERIFY 0x01
  64. #define MCE_ERR_NULL_PRIVATE_KEY 0x02
  65. #define MCE_ERR_NULL_PUBLIC_KEY 0x03
  66. #define MCE_ERR_NULL_INPUT_BUF 0x04
  67. #define MCE_ERR_NULL_OUTPUT_BUF 0x05
  68. #define MCE_ERR_NULL_FUNC_PTR 0x06
  69. #define MCE_ERR_NULL_EPHEM_PRI_KEY 0x07
  70. #define MCE_ERR_NULL_EPHEM_PUB_KEY 0x08
  71. #define MCE_ERR_BAD_INPUT 0x09
  72. /** This is a prototype of a user-provided callback function that generates
  73. * random seeds of the specified length.
  74. This function should copy <tt>sz</tt> bytes of random data into
  75. <tt>buffer</tt>.
  76. @param[out] buffer This is an unsigned char array of size at least
  77. <tt>sz</tt> to hold the random data.
  78. @param[in] sz The number of bytes of random data to compute and store.
  79. @retval MCE_SUCCESS Indicates successful completion.
  80. */
  81. typedef int GetRandomDataFunc(unsigned char *buffer, unsigned long sz);
  82. /** This is a prototype of a user-provided callback function that computes an
  83. * AES MMO message digest of the <tt>data</tt> of given size, <tt>sz</tt>.
  84. *
  85. This function should compute the hash of the <tt>data</tt> parameter of size
  86. <tt>sz</tt>, and store the result in the <tt>digest</tt> buffer parameter.
  87. @param[out] digest This is an unsigned char buffer to hold the message digest.
  88. The length of the digest must be <tt>AES_MMO_HASH_SIZE</tt> bytes.
  89. @param[in] sz The size in bytes of the message to be hashed.
  90. @param[in] data This is an unsigned char buffer of data to be hashed.
  91. @retval MCE_SUCCESS Indicates successful completion.
  92. */
  93. typedef int HashFunc(unsigned char *digest, unsigned long sz, unsigned char *data);
  94. /** This is a prototype of a user-provided callback function to process information
  95. during a long computation inside the library.
  96. @retval - This user-provided function should return MCE_SUCCESS to indicate
  97. successful completion.
  98. */
  99. typedef int YieldFunc(void);
  100. /**
  101. This is a sample implementation of the <tt>HashFunc</tt> callback used by the
  102. Security Builder MCE toolkit. Please note that this is not an optimized
  103. implementation and is provided for testing purposes.
  104. This function computes the AES MMO digest of the <tt>data</tt> parameter of
  105. length <tt>sz</tt>, and stores the result in <tt>digest</tt>.
  106. @param[out] digest This is an unsigned char buffer to hold the message digest.
  107. The length of the digest must be <tt>AES_MMO_HASH_SIZE</tt> bytes.
  108. @param[in] sz The size in bytes of the message to be hashed.
  109. @param[in] data This is an unsigned char buffer of data to be hashed.
  110. @retval MCE_ERR_NULL_OUTPUT_BUF <tt>digest</tt> is <tt>NULL</tt>
  111. @retval MCE_ERR_NULL_INPUT_BUF <tt>data</tt> is <tt>NULL</tt>
  112. @retval MCE_SUCCESS indicates successful completion.
  113. */
  114. int aesMmoHash(unsigned char *digest, unsigned long sz, unsigned char *data);
  115. /**
  116. Creates an ECDSA signature of a message digest.
  117. The outputs are the r and s components of the signature.
  118. @param[in] privateKey The private key. This is an unsigned char buffer of size
  119. <tt>SECT163K1_PRIVATE_KEY_SIZE</tt>.
  120. @param[in] msgDigest The hash of the message to be signed. This is an unsigned
  121. char buffer of size <tt>AES_MMO_HASH_SIZE</tt>.
  122. @param[in] GetRandomData Pointer to a function to get random data for
  123. generating ephemeral keys.
  124. @param[in] yieldLevel The yield level determines how often the user defined yield
  125. function will be called. This is a number from <tt>0</tt> to <tt>10</tt>.
  126. <tt>0</tt> will never yield.
  127. <tt>1</tt> will yield the most often.
  128. <tt>10</tt> will yield the least often.
  129. @param[in] YieldFunc Pointer to a function to allow user defined yielding.
  130. @param[out] r The r component of the signature. This is an unsigned char buffer
  131. of size <tt>SECT163K1_POINT_ORDER_SIZE</tt>.
  132. @param[out] s The s component of the signature. This is an unsigned char buffer
  133. of size <tt>SECT163K1_POINT_ORDER_SIZE</tt>.
  134. @retval MCE_ERR_NULL_PRIVATE_KEY <tt>privateKey</tt> is <tt>NULL</tt>.
  135. @retval MCE_ERR_NULL_OUTPUT_BUF <tt>msgDigest</tt>, <tt>r</tt> or
  136. <tt>s</tt> are <tt>NULL</tt>.
  137. @retval MCE_ERR_NULL_FUNC_PTR <tt>GetRandomData</tt> is <tt>NULL</tt> or
  138. <tt>YieldFunc</tt> is <tt>NULL</tt> and
  139. <tt>YieldLevel</tt> is not <tt>0</tt>.
  140. @retval MCE_ERR_BAD_INPUT <tt>YieldLevel</tt> is greater than <tt>10</tt>.
  141. @retval MCE_SUCCESS Success.
  142. */
  143. int ZSE_ECDSASign(unsigned char *privateKey,
  144. unsigned char *msgDigest,
  145. GetRandomDataFunc *GetRandomData,
  146. unsigned char *r,
  147. unsigned char *s,
  148. YieldFunc *yield,
  149. unsigned long yieldLevel );
  150. /**
  151. Verifies an ECDSA signature created using a private signing key by using
  152. the associated public key, the digest and the signature components.
  153. @param[in] publicKey The public key. This is an unsigned char buffer of size
  154. <tt>SECT163K1_COMPRESSED_PUBLIC_KEY_SIZE</tt>.
  155. @param[in] msgDigest The hash of the message to be verified. This is an
  156. unsigned char buffer of size <tt>AES_MMO_HASH_SIZE</tt>.
  157. @param[in] r The r component of the signature. This is an unsigned char
  158. buffer of size <tt>SECT163K1_POINT_ORDER_SIZE</tt>.
  159. @param[in] s The s component of the signature. This is an unsigned char
  160. buffer of size <tt>SECT163K1_POINT_ORDER_SIZE</tt>.
  161. @param[in] yieldLevel The yield level determines how often the user defined yield
  162. function will be called. This is a number from <tt>0</tt> to <tt>10</tt>.
  163. <tt>0</tt> will never yield.
  164. <tt>1</tt> will yield the most often.
  165. <tt>10</tt> will yield the least often.
  166. @param[in] YieldFunc Pointer to a function to allow user defined yielding.
  167. <tt>YieldFunc</tt> may be <tt>NULL</tt> if <tt>yieldLevel</tt> is <tt>0</tt>.
  168. @retval MCE_ERR_FAIL_VERIFY The signature verification failed.
  169. @retval MCE_ERR_NULL_PUBLIC_KEY <tt>publicKey</tt> is <tt>NULL</tt>.
  170. @retval MCE_ERR_NULL_INPUT_BUF <tt>msgDigest</tt>, <tt>r</tt> or
  171. <tt>s</tt> are <tt>NULL</tt>.
  172. @retval MCE_ERR_NULL_FUNC_PTR <tt>YieldFunc</tt> is <tt>NULL</tt> and
  173. <tt>YieldLevel</tt> is not <tt>0</tt>.
  174. @retval MCE_ERR_BAD_INPUT <tt>YieldLevel</tt> is greater than <tt>10</tt>.
  175. @retval MCE_SUCCESS Success.
  176. */
  177. int ZSE_ECDSAVerify(unsigned char *publicKey,
  178. unsigned char *msgDigest,
  179. unsigned char *r,
  180. unsigned char *s,
  181. YieldFunc *yield,
  182. unsigned long yieldLevel);
  183. /**
  184. Generates an ephemeral key pair using the specified random data generation
  185. function. Normally, the public key is sent to the remote party as part of the
  186. key agreement protocol.
  187. @param[out] privateKey The generated private key. This is an unsigned char
  188. buffer of size <tt>SECT163K1_PRIVATE_KEY_SIZE</tt>.
  189. @param[out] publicKey The generated public key. This is an unsigned char
  190. buffer of size
  191. <tt>SECT163K1_COMPRESSED_PUBLIC_KEY_SIZE</tt>.
  192. @param[in] GetRandomData Pointer to a function to get random data for
  193. generating the ephemeral key pair.
  194. @param[in] yieldLevel The yield level determines how often the user defined yield
  195. function will be called. This is a number from <tt>0</tt> to <tt>10</tt>.
  196. <tt>0</tt> will never yield.
  197. <tt>1</tt> will yield the most often.
  198. <tt>10</tt> will yield the least often.
  199. @param[in] YieldFunc Pointer to a function to allow user defined yielding.
  200. <tt>YieldFunc</tt> may be <tt>NULL</tt> if <tt>yieldLevel</tt> is <tt>0</tt>.
  201. @retval MCE_ERR_NULL_OUTPUT_BUF <tt>privateKey</tt> or <tt>publicKey</tt>
  202. are <tt>NULL</tt>.
  203. @retval MCE_ERR_NULL_FUNC_PTR <tt>GetRandomData</tt> is <tt>NULL</tt> or
  204. <tt>YieldFunc</tt> is <tt>NULL</tt> and
  205. <tt>YieldLevel</tt> is not <tt>0</tt>.
  206. @retval MCE_ERR_BAD_INPUT <tt>YieldLevel</tt> is greater than <tt>10</tt>.
  207. @retval MCE_SUCCESS Success.
  208. */
  209. int ZSE_ECCGenerateKey(unsigned char *privateKey,
  210. unsigned char *publicKey,
  211. GetRandomDataFunc *GetRandomData,
  212. YieldFunc *yield,
  213. unsigned long yieldLevel);
  214. /**
  215. Derives a shared secret using the ECMQV algorithm. The public key of the
  216. remote party is reconstructed using its implicit certificate and the CA
  217. public key.
  218. @param[in] privateKey The static private key of the local entity. This is an
  219. unsigned char buffer of size
  220. <tt>SECT163K1_PRIVATE_KEY_SIZE</tt>.
  221. @param[in] ephemeralPrivateKey The ephemeral private key of the local entity.
  222. It should be generated using a previous call
  223. to the function <tt>ECCGenerateKey</tt>. An
  224. unsigned char buffer of size
  225. <tt>SECT163K1_PRIVATE_KEY_SIZE</tt>.
  226. @param[in] ephemeralPublicKey The ephemeral public key of the local entity.
  227. It should be generated using a previous call
  228. to the function <tt>ECCGenerateKey</tt>. An
  229. unsigned char buffer of size
  230. <tt>SECT163K1_COMPRESSED_PUBLIC_KEY_SIZE</tt>.
  231. @param[in] remoteCertificate Implicit certificate of the remote party.
  232. This is an unsigned char buffer of size
  233. <tt>SECT163K1_CERTIFICATE_SIZE</tt>. The
  234. static public key of the remote party is
  235. derived from the certificate using the CA's
  236. public key.
  237. @param[in] remoteEphemeralPublicKey Ephemeral public key received from the
  238. remote party. This is an unsigned char
  239. buffer of size
  240. <tt>SECT163K1_COMPRESSED_PUBLIC_KEY_SIZE</tt>.
  241. @param[in] caPublicKey Public key of the certificate authority. The static
  242. public key for the remote party is derived from the
  243. certificate using the CA's public key.
  244. @param[out] keyBits The derived shared secret. This is an unsigned char
  245. buffer of size <tt>SECT163K1_SHARED_SECRET_SIZE</tt>.
  246. @param[in] Hash Pointer to a function to hash the certificate data.
  247. @param[in] yieldLevel The yield level determines how often the user defined yield
  248. function will be called. This is a number from <tt>0</tt> to <tt>10</tt>.
  249. <tt>0</tt> will never yield.
  250. <tt>1</tt> will yield the most often.
  251. <tt>10</tt> will yield the least often.
  252. @param[in] YieldFunc Pointer to a function to allow user defined yielding.
  253. <tt>YieldFunc</tt> may be <tt>NULL</tt> if <tt>yieldLevel</tt> is <tt>0</tt>.
  254. @retval MCE_ERR_NULL_PRIVATE_KEY <tt>privateKey</tt> is <tt>NULL</tt>.
  255. @retval MCE_ERR_NULL_EPHEM_PRI_KEY <tt>ephemeralPrivateKey</tt> is
  256. <tt>NULL</tt>
  257. @retval MCE_ERR_NULL_EPHEM_PUB_KEY <tt>ephemeralPublicKey</tt> or
  258. <tt>remoteEphemeralPublicKey</tt> are
  259. <tt>NULL</tt>.
  260. @retval MCE_ERR_NULL_INPUT_BUF <tt>remoteCertificate</tt> is
  261. <tt>NULL</tt>.
  262. @retval MCE_ERR_NULL_PUBLIC_KEY <tt>caPublicKey</tt> is <tt>NULL</tt>.
  263. @retval MCE_ERR_NULL_OUTPUT_BUF <tt>keyBits</tt> is <tt>NULL</tt>.
  264. @retval MCE_ERR_NULL_FUNC_PTR <tt>Hash</tt> is <tt>NULL</tt> or
  265. <tt>YieldFunc</tt> is <tt>NULL</tt> and
  266. <tt>YieldLevel</tt> is not <tt>0</tt>.
  267. @retval MCE_ERR_BAD_INPUT <tt>YieldLevel</tt> is greater than <tt>10</tt>.
  268. @retval MCE_SUCCESS Success.
  269. */
  270. int ZSE_ECCKeyBitGenerate(unsigned char *privateKey,
  271. unsigned char *ephemeralPrivateKey,
  272. unsigned char *ephemeralPublicKey,
  273. unsigned char *remoteCertificate,
  274. unsigned char *remoteEphemeralPublicKey,
  275. unsigned char *caPublicKey,
  276. unsigned char *keyBits,
  277. HashFunc *Hash,
  278. YieldFunc *yield,
  279. unsigned long yieldLevel);
  280. /**
  281. Reconstructs the remote party's public key using its implicit certificate
  282. and the CA public key.
  283. @param[in] certificate Implicit certificate of the remote party. This is an unsigned
  284. char buffer of size
  285. <tt>SECT163K1_CERTIFICATE_SIZE</tt>. The static public
  286. key of the remote party is derived from the certificate
  287. using the CA's public key.
  288. @param[in] caPublicKey Public key of the certificate authority. The static
  289. public key of the remote party is derived from the
  290. certificate using the CA's public key.
  291. @param[out] publicKey The derived public key. This is an unsigned char buffer
  292. of size
  293. <tt>SECT163K1_COMPRESSED_PUBLIC_KEY_SIZE</tt>.
  294. @param[in] Hash Pointer to a function to hash the certificate data.
  295. @param[in] yieldLevel The yield level determines how often the user defined
  296. yield function will be called. This is a number from
  297. <tt>0</tt> to <tt>10</tt>.
  298. <tt>0</tt> will never yield.
  299. <tt>1</tt> will yield the most often.
  300. <tt>10</tt> will yield the least often.
  301. @param[in] YieldFunc Pointer to a function to allow user defined yielding.
  302. <tt>YieldFunc</tt> may be <tt>NULL</tt> if
  303. <tt>yieldLevel</tt> is <tt>0</tt>.
  304. @retval MCE_ERR_NULL_INPUT_BUF <tt>certificate</tt> is <tt>NULL</tt>.
  305. @retval MCE_ERR_NULL_PUBLIC_KEY <tt>caPublicKey</tt> is <tt>NULL</tt>.
  306. @retval MCE_ERR_NULL_OUTPUT_BUF <tt>publicKey</tt> is <tt>NULL</tt>.
  307. @retval MCE_ERR_NULL_FUNC_PTR <tt>Hash</tt> is <tt>NULL</tt> or
  308. <tt>YieldFunc</tt> is <tt>NULL</tt> and
  309. <tt>YieldLevel</tt> is not <tt>0</tt>.
  310. @retval MCE_ERR_BAD_INPUT <tt>YieldLevel</tt> is greater than <tt>10</tt>.
  311. */
  312. int ZSE_ECQVReconstructPublicKey(unsigned char* certificate,
  313. unsigned char* caPublicKey,
  314. unsigned char* publicKey,
  315. HashFunc *Hash,
  316. YieldFunc *yield,
  317. unsigned long yieldLevel);
  318. /**
  319. Returns the version number for the product.
  320. @retval ZSE_VERSION The product version number.
  321. */
  322. unsigned long ZSE_GetVersion(void);
  323. #ifdef __cplusplus
  324. }
  325. #endif
  326. #endif