pk_wrap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Public Key abstraction layer: wrapper functions
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PK_C)
  27. #include "mbedtls/pk_internal.h"
  28. /* Even if RSA not activated, for the sake of RSA-alt */
  29. #include "mbedtls/rsa.h"
  30. #include "mbedtls/bignum.h"
  31. #include <string.h>
  32. #if defined(MBEDTLS_ECP_C)
  33. #include "mbedtls/ecp.h"
  34. #endif
  35. #if defined(MBEDTLS_ECDSA_C)
  36. #include "mbedtls/ecdsa.h"
  37. #endif
  38. #if defined(MBEDTLS_PLATFORM_C)
  39. #include "mbedtls/platform.h"
  40. #else
  41. #include <stdlib.h>
  42. #define mbedtls_calloc calloc
  43. #define mbedtls_free free
  44. #endif
  45. #include <limits.h>
  46. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  47. /* Implementation that should never be optimized out by the compiler */
  48. static void mbedtls_zeroize( void *v, size_t n ) {
  49. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  50. }
  51. #endif
  52. #if defined(MBEDTLS_RSA_C)
  53. static int rsa_can_do( mbedtls_pk_type_t type )
  54. {
  55. return( type == MBEDTLS_PK_RSA ||
  56. type == MBEDTLS_PK_RSASSA_PSS );
  57. }
  58. static size_t rsa_get_bitlen( const void *ctx )
  59. {
  60. return( 8 * ((const mbedtls_rsa_context *) ctx)->len );
  61. }
  62. static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  63. const unsigned char *hash, size_t hash_len,
  64. const unsigned char *sig, size_t sig_len )
  65. {
  66. int ret;
  67. #if defined(MBEDTLS_HAVE_INT64)
  68. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  69. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  70. #endif /* MBEDTLS_HAVE_INT64 */
  71. if( sig_len < ((mbedtls_rsa_context *) ctx)->len )
  72. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  73. if( ( ret = mbedtls_rsa_pkcs1_verify( (mbedtls_rsa_context *) ctx, NULL, NULL,
  74. MBEDTLS_RSA_PUBLIC, md_alg,
  75. (unsigned int) hash_len, hash, sig ) ) != 0 )
  76. return( ret );
  77. if( sig_len > ((mbedtls_rsa_context *) ctx)->len )
  78. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  79. return( 0 );
  80. }
  81. static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  82. const unsigned char *hash, size_t hash_len,
  83. unsigned char *sig, size_t *sig_len,
  84. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  85. {
  86. #if defined(MBEDTLS_HAVE_INT64)
  87. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  88. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  89. #endif /* MBEDTLS_HAVE_INT64 */
  90. *sig_len = ((mbedtls_rsa_context *) ctx)->len;
  91. return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
  92. md_alg, (unsigned int) hash_len, hash, sig ) );
  93. }
  94. static int rsa_decrypt_wrap( void *ctx,
  95. const unsigned char *input, size_t ilen,
  96. unsigned char *output, size_t *olen, size_t osize,
  97. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  98. {
  99. if( ilen != ((mbedtls_rsa_context *) ctx)->len )
  100. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  101. return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx, f_rng, p_rng,
  102. MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
  103. }
  104. static int rsa_encrypt_wrap( void *ctx,
  105. const unsigned char *input, size_t ilen,
  106. unsigned char *output, size_t *olen, size_t osize,
  107. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  108. {
  109. *olen = ((mbedtls_rsa_context *) ctx)->len;
  110. if( *olen > osize )
  111. return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
  112. return( mbedtls_rsa_pkcs1_encrypt( (mbedtls_rsa_context *) ctx,
  113. f_rng, p_rng, MBEDTLS_RSA_PUBLIC, ilen, input, output ) );
  114. }
  115. static int rsa_check_pair_wrap( const void *pub, const void *prv )
  116. {
  117. return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
  118. (const mbedtls_rsa_context *) prv ) );
  119. }
  120. static void *rsa_alloc_wrap( void )
  121. {
  122. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
  123. if( ctx != NULL )
  124. mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
  125. return( ctx );
  126. }
  127. static void rsa_free_wrap( void *ctx )
  128. {
  129. mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
  130. mbedtls_free( ctx );
  131. }
  132. static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
  133. {
  134. items->type = MBEDTLS_PK_DEBUG_MPI;
  135. items->name = "rsa.N";
  136. items->value = &( ((mbedtls_rsa_context *) ctx)->N );
  137. items++;
  138. items->type = MBEDTLS_PK_DEBUG_MPI;
  139. items->name = "rsa.E";
  140. items->value = &( ((mbedtls_rsa_context *) ctx)->E );
  141. }
  142. const mbedtls_pk_info_t mbedtls_rsa_info = {
  143. MBEDTLS_PK_RSA,
  144. "RSA",
  145. rsa_get_bitlen,
  146. rsa_can_do,
  147. rsa_verify_wrap,
  148. rsa_sign_wrap,
  149. rsa_decrypt_wrap,
  150. rsa_encrypt_wrap,
  151. rsa_check_pair_wrap,
  152. rsa_alloc_wrap,
  153. rsa_free_wrap,
  154. rsa_debug,
  155. };
  156. #endif /* MBEDTLS_RSA_C */
  157. #if defined(MBEDTLS_ECP_C)
  158. /*
  159. * Generic EC key
  160. */
  161. static int eckey_can_do( mbedtls_pk_type_t type )
  162. {
  163. return( type == MBEDTLS_PK_ECKEY ||
  164. type == MBEDTLS_PK_ECKEY_DH ||
  165. type == MBEDTLS_PK_ECDSA );
  166. }
  167. static size_t eckey_get_bitlen( const void *ctx )
  168. {
  169. return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
  170. }
  171. #if defined(MBEDTLS_ECDSA_C)
  172. /* Forward declarations */
  173. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  174. const unsigned char *hash, size_t hash_len,
  175. const unsigned char *sig, size_t sig_len );
  176. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  177. const unsigned char *hash, size_t hash_len,
  178. unsigned char *sig, size_t *sig_len,
  179. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  180. static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  181. const unsigned char *hash, size_t hash_len,
  182. const unsigned char *sig, size_t sig_len )
  183. {
  184. int ret;
  185. mbedtls_ecdsa_context ecdsa;
  186. mbedtls_ecdsa_init( &ecdsa );
  187. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  188. ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
  189. mbedtls_ecdsa_free( &ecdsa );
  190. return( ret );
  191. }
  192. static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  193. const unsigned char *hash, size_t hash_len,
  194. unsigned char *sig, size_t *sig_len,
  195. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  196. {
  197. int ret;
  198. mbedtls_ecdsa_context ecdsa;
  199. mbedtls_ecdsa_init( &ecdsa );
  200. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  201. ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
  202. f_rng, p_rng );
  203. mbedtls_ecdsa_free( &ecdsa );
  204. return( ret );
  205. }
  206. #endif /* MBEDTLS_ECDSA_C */
  207. static int eckey_check_pair( const void *pub, const void *prv )
  208. {
  209. return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
  210. (const mbedtls_ecp_keypair *) prv ) );
  211. }
  212. static void *eckey_alloc_wrap( void )
  213. {
  214. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
  215. if( ctx != NULL )
  216. mbedtls_ecp_keypair_init( ctx );
  217. return( ctx );
  218. }
  219. static void eckey_free_wrap( void *ctx )
  220. {
  221. mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
  222. mbedtls_free( ctx );
  223. }
  224. static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
  225. {
  226. items->type = MBEDTLS_PK_DEBUG_ECP;
  227. items->name = "eckey.Q";
  228. items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
  229. }
  230. const mbedtls_pk_info_t mbedtls_eckey_info = {
  231. MBEDTLS_PK_ECKEY,
  232. "EC",
  233. eckey_get_bitlen,
  234. eckey_can_do,
  235. #if defined(MBEDTLS_ECDSA_C)
  236. eckey_verify_wrap,
  237. eckey_sign_wrap,
  238. #else
  239. NULL,
  240. NULL,
  241. #endif
  242. NULL,
  243. NULL,
  244. eckey_check_pair,
  245. eckey_alloc_wrap,
  246. eckey_free_wrap,
  247. eckey_debug,
  248. };
  249. /*
  250. * EC key restricted to ECDH
  251. */
  252. static int eckeydh_can_do( mbedtls_pk_type_t type )
  253. {
  254. return( type == MBEDTLS_PK_ECKEY ||
  255. type == MBEDTLS_PK_ECKEY_DH );
  256. }
  257. const mbedtls_pk_info_t mbedtls_eckeydh_info = {
  258. MBEDTLS_PK_ECKEY_DH,
  259. "EC_DH",
  260. eckey_get_bitlen, /* Same underlying key structure */
  261. eckeydh_can_do,
  262. NULL,
  263. NULL,
  264. NULL,
  265. NULL,
  266. eckey_check_pair,
  267. eckey_alloc_wrap, /* Same underlying key structure */
  268. eckey_free_wrap, /* Same underlying key structure */
  269. eckey_debug, /* Same underlying key structure */
  270. };
  271. #endif /* MBEDTLS_ECP_C */
  272. #if defined(MBEDTLS_ECDSA_C)
  273. static int ecdsa_can_do( mbedtls_pk_type_t type )
  274. {
  275. return( type == MBEDTLS_PK_ECDSA );
  276. }
  277. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  278. const unsigned char *hash, size_t hash_len,
  279. const unsigned char *sig, size_t sig_len )
  280. {
  281. int ret;
  282. ((void) md_alg);
  283. ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
  284. hash, hash_len, sig, sig_len );
  285. if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
  286. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  287. return( ret );
  288. }
  289. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  290. const unsigned char *hash, size_t hash_len,
  291. unsigned char *sig, size_t *sig_len,
  292. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  293. {
  294. return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
  295. md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
  296. }
  297. static void *ecdsa_alloc_wrap( void )
  298. {
  299. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
  300. if( ctx != NULL )
  301. mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
  302. return( ctx );
  303. }
  304. static void ecdsa_free_wrap( void *ctx )
  305. {
  306. mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
  307. mbedtls_free( ctx );
  308. }
  309. const mbedtls_pk_info_t mbedtls_ecdsa_info = {
  310. MBEDTLS_PK_ECDSA,
  311. "ECDSA",
  312. eckey_get_bitlen, /* Compatible key structures */
  313. ecdsa_can_do,
  314. ecdsa_verify_wrap,
  315. ecdsa_sign_wrap,
  316. NULL,
  317. NULL,
  318. eckey_check_pair, /* Compatible key structures */
  319. ecdsa_alloc_wrap,
  320. ecdsa_free_wrap,
  321. eckey_debug, /* Compatible key structures */
  322. };
  323. #endif /* MBEDTLS_ECDSA_C */
  324. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  325. /*
  326. * Support for alternative RSA-private implementations
  327. */
  328. static int rsa_alt_can_do( mbedtls_pk_type_t type )
  329. {
  330. return( type == MBEDTLS_PK_RSA );
  331. }
  332. static size_t rsa_alt_get_bitlen( const void *ctx )
  333. {
  334. const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
  335. return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
  336. }
  337. static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  338. const unsigned char *hash, size_t hash_len,
  339. unsigned char *sig, size_t *sig_len,
  340. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  341. {
  342. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  343. #if defined(MBEDTLS_HAVE_INT64)
  344. if( UINT_MAX < hash_len )
  345. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  346. #endif /* MBEDTLS_HAVE_INT64 */
  347. *sig_len = rsa_alt->key_len_func( rsa_alt->key );
  348. return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
  349. md_alg, (unsigned int) hash_len, hash, sig ) );
  350. }
  351. static int rsa_alt_decrypt_wrap( void *ctx,
  352. const unsigned char *input, size_t ilen,
  353. unsigned char *output, size_t *olen, size_t osize,
  354. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  355. {
  356. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  357. ((void) f_rng);
  358. ((void) p_rng);
  359. if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
  360. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  361. return( rsa_alt->decrypt_func( rsa_alt->key,
  362. MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
  363. }
  364. #if defined(MBEDTLS_RSA_C)
  365. static int rsa_alt_check_pair( const void *pub, const void *prv )
  366. {
  367. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  368. unsigned char hash[32];
  369. size_t sig_len = 0;
  370. int ret;
  371. if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
  372. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  373. memset( hash, 0x2a, sizeof( hash ) );
  374. if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
  375. hash, sizeof( hash ),
  376. sig, &sig_len, NULL, NULL ) ) != 0 )
  377. {
  378. return( ret );
  379. }
  380. if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
  381. hash, sizeof( hash ), sig, sig_len ) != 0 )
  382. {
  383. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  384. }
  385. return( 0 );
  386. }
  387. #endif /* MBEDTLS_RSA_C */
  388. static void *rsa_alt_alloc_wrap( void )
  389. {
  390. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
  391. if( ctx != NULL )
  392. memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
  393. return( ctx );
  394. }
  395. static void rsa_alt_free_wrap( void *ctx )
  396. {
  397. mbedtls_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
  398. mbedtls_free( ctx );
  399. }
  400. const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
  401. MBEDTLS_PK_RSA_ALT,
  402. "RSA-alt",
  403. rsa_alt_get_bitlen,
  404. rsa_alt_can_do,
  405. NULL,
  406. rsa_alt_sign_wrap,
  407. rsa_alt_decrypt_wrap,
  408. NULL,
  409. #if defined(MBEDTLS_RSA_C)
  410. rsa_alt_check_pair,
  411. #else
  412. NULL,
  413. #endif
  414. rsa_alt_alloc_wrap,
  415. rsa_alt_free_wrap,
  416. NULL,
  417. };
  418. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  419. #endif /* MBEDTLS_PK_C */