pk_wrap.c 14 KB

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