pk_wrap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 <string.h>
  31. #if defined(MBEDTLS_ECP_C)
  32. #include "mbedtls/ecp.h"
  33. #endif
  34. #if defined(MBEDTLS_ECDSA_C)
  35. #include "mbedtls/ecdsa.h"
  36. #endif
  37. #if defined(MBEDTLS_PLATFORM_C)
  38. #include "mbedtls/platform.h"
  39. #else
  40. #include <stdlib.h>
  41. #define mbedtls_calloc calloc
  42. #define mbedtls_free free
  43. #endif
  44. #include <limits.h>
  45. #include <stdint.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. const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
  61. return( 8 * mbedtls_rsa_get_len( rsa ) );
  62. }
  63. static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  64. const unsigned char *hash, size_t hash_len,
  65. const unsigned char *sig, size_t sig_len )
  66. {
  67. int ret;
  68. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  69. size_t rsa_len = mbedtls_rsa_get_len( rsa );
  70. #if SIZE_MAX > UINT_MAX
  71. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  72. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  73. #endif /* SIZE_MAX > UINT_MAX */
  74. if( sig_len < rsa_len )
  75. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  76. if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
  77. MBEDTLS_RSA_PUBLIC, md_alg,
  78. (unsigned int) hash_len, hash, sig ) ) != 0 )
  79. return( ret );
  80. if( sig_len > rsa_len )
  81. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  82. return( 0 );
  83. }
  84. static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  85. const unsigned char *hash, size_t hash_len,
  86. unsigned char *sig, size_t *sig_len,
  87. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  88. {
  89. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  90. #if SIZE_MAX > UINT_MAX
  91. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  92. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  93. #endif /* SIZE_MAX > UINT_MAX */
  94. *sig_len = mbedtls_rsa_get_len( rsa );
  95. return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
  96. md_alg, (unsigned int) hash_len, hash, sig ) );
  97. }
  98. static int rsa_decrypt_wrap( void *ctx,
  99. const unsigned char *input, size_t ilen,
  100. unsigned char *output, size_t *olen, size_t osize,
  101. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  102. {
  103. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  104. if( ilen != mbedtls_rsa_get_len( rsa ) )
  105. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  106. return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
  107. MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
  108. }
  109. static int rsa_encrypt_wrap( void *ctx,
  110. const unsigned char *input, size_t ilen,
  111. unsigned char *output, size_t *olen, size_t osize,
  112. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  113. {
  114. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  115. *olen = mbedtls_rsa_get_len( rsa );
  116. if( *olen > osize )
  117. return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
  118. return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
  119. ilen, input, output ) );
  120. }
  121. static int rsa_check_pair_wrap( const void *pub, const void *prv )
  122. {
  123. return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
  124. (const mbedtls_rsa_context *) prv ) );
  125. }
  126. static void *rsa_alloc_wrap( void )
  127. {
  128. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
  129. if( ctx != NULL )
  130. mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
  131. return( ctx );
  132. }
  133. static void rsa_free_wrap( void *ctx )
  134. {
  135. mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
  136. mbedtls_free( ctx );
  137. }
  138. static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
  139. {
  140. items->type = MBEDTLS_PK_DEBUG_MPI;
  141. items->name = "rsa.N";
  142. items->value = &( ((mbedtls_rsa_context *) ctx)->N );
  143. items++;
  144. items->type = MBEDTLS_PK_DEBUG_MPI;
  145. items->name = "rsa.E";
  146. items->value = &( ((mbedtls_rsa_context *) ctx)->E );
  147. }
  148. const mbedtls_pk_info_t mbedtls_rsa_info = {
  149. MBEDTLS_PK_RSA,
  150. "RSA",
  151. rsa_get_bitlen,
  152. rsa_can_do,
  153. rsa_verify_wrap,
  154. rsa_sign_wrap,
  155. rsa_decrypt_wrap,
  156. rsa_encrypt_wrap,
  157. rsa_check_pair_wrap,
  158. rsa_alloc_wrap,
  159. rsa_free_wrap,
  160. rsa_debug,
  161. };
  162. #endif /* MBEDTLS_RSA_C */
  163. #if defined(MBEDTLS_ECP_C)
  164. /*
  165. * Generic EC key
  166. */
  167. static int eckey_can_do( mbedtls_pk_type_t type )
  168. {
  169. return( type == MBEDTLS_PK_ECKEY ||
  170. type == MBEDTLS_PK_ECKEY_DH ||
  171. type == MBEDTLS_PK_ECDSA );
  172. }
  173. static size_t eckey_get_bitlen( const void *ctx )
  174. {
  175. return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
  176. }
  177. #if defined(MBEDTLS_ECDSA_C)
  178. /* Forward declarations */
  179. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  180. const unsigned char *hash, size_t hash_len,
  181. const unsigned char *sig, size_t sig_len );
  182. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  183. const unsigned char *hash, size_t hash_len,
  184. unsigned char *sig, size_t *sig_len,
  185. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  186. static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  187. const unsigned char *hash, size_t hash_len,
  188. const unsigned char *sig, size_t sig_len )
  189. {
  190. int ret;
  191. mbedtls_ecdsa_context ecdsa;
  192. mbedtls_ecdsa_init( &ecdsa );
  193. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  194. ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
  195. mbedtls_ecdsa_free( &ecdsa );
  196. return( ret );
  197. }
  198. static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  199. const unsigned char *hash, size_t hash_len,
  200. unsigned char *sig, size_t *sig_len,
  201. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  202. {
  203. int ret;
  204. mbedtls_ecdsa_context ecdsa;
  205. mbedtls_ecdsa_init( &ecdsa );
  206. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  207. ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
  208. f_rng, p_rng );
  209. mbedtls_ecdsa_free( &ecdsa );
  210. return( ret );
  211. }
  212. #endif /* MBEDTLS_ECDSA_C */
  213. static int eckey_check_pair( const void *pub, const void *prv )
  214. {
  215. return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
  216. (const mbedtls_ecp_keypair *) prv ) );
  217. }
  218. static void *eckey_alloc_wrap( void )
  219. {
  220. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
  221. if( ctx != NULL )
  222. mbedtls_ecp_keypair_init( ctx );
  223. return( ctx );
  224. }
  225. static void eckey_free_wrap( void *ctx )
  226. {
  227. mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
  228. mbedtls_free( ctx );
  229. }
  230. static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
  231. {
  232. items->type = MBEDTLS_PK_DEBUG_ECP;
  233. items->name = "eckey.Q";
  234. items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
  235. }
  236. const mbedtls_pk_info_t mbedtls_eckey_info = {
  237. MBEDTLS_PK_ECKEY,
  238. "EC",
  239. eckey_get_bitlen,
  240. eckey_can_do,
  241. #if defined(MBEDTLS_ECDSA_C)
  242. eckey_verify_wrap,
  243. eckey_sign_wrap,
  244. #else
  245. NULL,
  246. NULL,
  247. #endif
  248. NULL,
  249. NULL,
  250. eckey_check_pair,
  251. eckey_alloc_wrap,
  252. eckey_free_wrap,
  253. eckey_debug,
  254. };
  255. /*
  256. * EC key restricted to ECDH
  257. */
  258. static int eckeydh_can_do( mbedtls_pk_type_t type )
  259. {
  260. return( type == MBEDTLS_PK_ECKEY ||
  261. type == MBEDTLS_PK_ECKEY_DH );
  262. }
  263. const mbedtls_pk_info_t mbedtls_eckeydh_info = {
  264. MBEDTLS_PK_ECKEY_DH,
  265. "EC_DH",
  266. eckey_get_bitlen, /* Same underlying key structure */
  267. eckeydh_can_do,
  268. NULL,
  269. NULL,
  270. NULL,
  271. NULL,
  272. eckey_check_pair,
  273. eckey_alloc_wrap, /* Same underlying key structure */
  274. eckey_free_wrap, /* Same underlying key structure */
  275. eckey_debug, /* Same underlying key structure */
  276. };
  277. #endif /* MBEDTLS_ECP_C */
  278. #if defined(MBEDTLS_ECDSA_C)
  279. static int ecdsa_can_do( mbedtls_pk_type_t type )
  280. {
  281. return( type == MBEDTLS_PK_ECDSA );
  282. }
  283. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  284. const unsigned char *hash, size_t hash_len,
  285. const unsigned char *sig, size_t sig_len )
  286. {
  287. int ret;
  288. ((void) md_alg);
  289. ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
  290. hash, hash_len, sig, sig_len );
  291. if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
  292. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  293. return( ret );
  294. }
  295. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  296. const unsigned char *hash, size_t hash_len,
  297. unsigned char *sig, size_t *sig_len,
  298. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  299. {
  300. return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
  301. md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
  302. }
  303. static void *ecdsa_alloc_wrap( void )
  304. {
  305. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
  306. if( ctx != NULL )
  307. mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
  308. return( ctx );
  309. }
  310. static void ecdsa_free_wrap( void *ctx )
  311. {
  312. mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
  313. mbedtls_free( ctx );
  314. }
  315. const mbedtls_pk_info_t mbedtls_ecdsa_info = {
  316. MBEDTLS_PK_ECDSA,
  317. "ECDSA",
  318. eckey_get_bitlen, /* Compatible key structures */
  319. ecdsa_can_do,
  320. ecdsa_verify_wrap,
  321. ecdsa_sign_wrap,
  322. NULL,
  323. NULL,
  324. eckey_check_pair, /* Compatible key structures */
  325. ecdsa_alloc_wrap,
  326. ecdsa_free_wrap,
  327. eckey_debug, /* Compatible key structures */
  328. };
  329. #endif /* MBEDTLS_ECDSA_C */
  330. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  331. /*
  332. * Support for alternative RSA-private implementations
  333. */
  334. static int rsa_alt_can_do( mbedtls_pk_type_t type )
  335. {
  336. return( type == MBEDTLS_PK_RSA );
  337. }
  338. static size_t rsa_alt_get_bitlen( const void *ctx )
  339. {
  340. const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
  341. return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
  342. }
  343. static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  344. const unsigned char *hash, size_t hash_len,
  345. unsigned char *sig, size_t *sig_len,
  346. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  347. {
  348. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  349. #if SIZE_MAX > UINT_MAX
  350. if( UINT_MAX < hash_len )
  351. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  352. #endif /* SIZE_MAX > UINT_MAX */
  353. *sig_len = rsa_alt->key_len_func( rsa_alt->key );
  354. return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
  355. md_alg, (unsigned int) hash_len, hash, sig ) );
  356. }
  357. static int rsa_alt_decrypt_wrap( void *ctx,
  358. const unsigned char *input, size_t ilen,
  359. unsigned char *output, size_t *olen, size_t osize,
  360. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  361. {
  362. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  363. ((void) f_rng);
  364. ((void) p_rng);
  365. if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
  366. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  367. return( rsa_alt->decrypt_func( rsa_alt->key,
  368. MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
  369. }
  370. #if defined(MBEDTLS_RSA_C)
  371. static int rsa_alt_check_pair( const void *pub, const void *prv )
  372. {
  373. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  374. unsigned char hash[32];
  375. size_t sig_len = 0;
  376. int ret;
  377. if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
  378. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  379. memset( hash, 0x2a, sizeof( hash ) );
  380. if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
  381. hash, sizeof( hash ),
  382. sig, &sig_len, NULL, NULL ) ) != 0 )
  383. {
  384. return( ret );
  385. }
  386. if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
  387. hash, sizeof( hash ), sig, sig_len ) != 0 )
  388. {
  389. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  390. }
  391. return( 0 );
  392. }
  393. #endif /* MBEDTLS_RSA_C */
  394. static void *rsa_alt_alloc_wrap( void )
  395. {
  396. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
  397. if( ctx != NULL )
  398. memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
  399. return( ctx );
  400. }
  401. static void rsa_alt_free_wrap( void *ctx )
  402. {
  403. mbedtls_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
  404. mbedtls_free( ctx );
  405. }
  406. const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
  407. MBEDTLS_PK_RSA_ALT,
  408. "RSA-alt",
  409. rsa_alt_get_bitlen,
  410. rsa_alt_can_do,
  411. NULL,
  412. rsa_alt_sign_wrap,
  413. rsa_alt_decrypt_wrap,
  414. NULL,
  415. #if defined(MBEDTLS_RSA_C)
  416. rsa_alt_check_pair,
  417. #else
  418. NULL,
  419. #endif
  420. rsa_alt_alloc_wrap,
  421. rsa_alt_free_wrap,
  422. NULL,
  423. };
  424. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  425. #endif /* MBEDTLS_PK_C */