Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  *  Helper functions for the RSA module
  3.  *
  4.  *  Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
  5.  *  SPDX-License-Identifier: GPL-2.0
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License along
  18.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  19.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  *  This file is part of mbed TLS (https://tls.mbed.org)
  22.  *
  23.  */
  24.  
  25. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "mbedtls/config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30.  
  31. #if defined(MBEDTLS_RSA_C)
  32.  
  33. #include "mbedtls/rsa.h"
  34. #include "mbedtls/bignum.h"
  35. #include "mbedtls/rsa_internal.h"
  36.  
  37. /*
  38.  * Compute RSA prime factors from public and private exponents
  39.  *
  40.  * Summary of algorithm:
  41.  * Setting F := lcm(P-1,Q-1), the idea is as follows:
  42.  *
  43.  * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
  44.  *     is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
  45.  *     square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
  46.  *     possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
  47.  *     or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
  48.  *     factors of N.
  49.  *
  50.  * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
  51.  *     construction still applies since (-)^K is the identity on the set of
  52.  *     roots of 1 in Z/NZ.
  53.  *
  54.  * The public and private key primitives (-)^E and (-)^D are mutually inverse
  55.  * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
  56.  * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
  57.  * Splitting L = 2^t * K with K odd, we have
  58.  *
  59.  *   DE - 1 = FL = (F/2) * (2^(t+1)) * K,
  60.  *
  61.  * so (F / 2) * K is among the numbers
  62.  *
  63.  *   (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
  64.  *
  65.  * where ord is the order of 2 in (DE - 1).
  66.  * We can therefore iterate through these numbers apply the construction
  67.  * of (a) and (b) above to attempt to factor N.
  68.  *
  69.  */
  70. int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N,
  71.                      mbedtls_mpi const *E, mbedtls_mpi const *D,
  72.                      mbedtls_mpi *P, mbedtls_mpi *Q )
  73. {
  74.     int ret = 0;
  75.  
  76.     uint16_t attempt;  /* Number of current attempt  */
  77.     uint16_t iter;     /* Number of squares computed in the current attempt */
  78.  
  79.     uint16_t order;    /* Order of 2 in DE - 1 */
  80.  
  81.     mbedtls_mpi T;  /* Holds largest odd divisor of DE - 1     */
  82.     mbedtls_mpi K;  /* Temporary holding the current candidate */
  83.  
  84.     const unsigned char primes[] = { 2,
  85.            3,    5,    7,   11,   13,   17,   19,   23,
  86.           29,   31,   37,   41,   43,   47,   53,   59,
  87.           61,   67,   71,   73,   79,   83,   89,   97,
  88.          101,  103,  107,  109,  113,  127,  131,  137,
  89.          139,  149,  151,  157,  163,  167,  173,  179,
  90.          181,  191,  193,  197,  199,  211,  223,  227,
  91.          229,  233,  239,  241,  251
  92.     };
  93.  
  94.     const size_t num_primes = sizeof( primes ) / sizeof( *primes );
  95.  
  96.     if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
  97.         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  98.  
  99.     if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
  100.         mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
  101.         mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
  102.         mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
  103.         mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
  104.     {
  105.         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  106.     }
  107.  
  108.     /*
  109.      * Initializations and temporary changes
  110.      */
  111.  
  112.     mbedtls_mpi_init( &K );
  113.     mbedtls_mpi_init( &T );
  114.  
  115.     /* T := DE - 1 */
  116.     MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, D,  E ) );
  117.     MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &T, &T, 1 ) );
  118.  
  119.     if( ( order = (uint16_t) mbedtls_mpi_lsb( &T ) ) == 0 )
  120.     {
  121.         ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  122.         goto cleanup;
  123.     }
  124.  
  125.     /* After this operation, T holds the largest odd divisor of DE - 1. */
  126.     MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &T, order ) );
  127.  
  128.     /*
  129.      * Actual work
  130.      */
  131.  
  132.     /* Skip trying 2 if N == 1 mod 8 */
  133.     attempt = 0;
  134.     if( N->p[0] % 8 == 1 )
  135.         attempt = 1;
  136.  
  137.     for( ; attempt < num_primes; ++attempt )
  138.     {
  139.         mbedtls_mpi_lset( &K, primes[attempt] );
  140.  
  141.         /* Check if gcd(K,N) = 1 */
  142.         MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
  143.         if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
  144.             continue;
  145.  
  146.         /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
  147.          * and check whether they have nontrivial GCD with N. */
  148.         MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, &T, N,
  149.                              Q /* temporarily use Q for storing Montgomery
  150.                                 * multiplication helper values */ ) );
  151.  
  152.         for( iter = 1; iter <= order; ++iter )
  153.         {
  154.             /* If we reach 1 prematurely, there's no point
  155.              * in continuing to square K */
  156.             if( mbedtls_mpi_cmp_int( &K, 1 ) == 0 )
  157.                 break;
  158.  
  159.             MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
  160.             MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
  161.  
  162.             if( mbedtls_mpi_cmp_int( P, 1 ) ==  1 &&
  163.                 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
  164.             {
  165.                 /*
  166.                  * Have found a nontrivial divisor P of N.
  167.                  * Set Q := N / P.
  168.                  */
  169.  
  170.                 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, NULL, N, P ) );
  171.                 goto cleanup;
  172.             }
  173.  
  174.             MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  175.             MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
  176.             MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
  177.         }
  178.  
  179.         /*
  180.          * If we get here, then either we prematurely aborted the loop because
  181.          * we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must
  182.          * be 1 if D,E,N were consistent.
  183.          * Check if that's the case and abort if not, to avoid very long,
  184.          * yet eventually failing, computations if N,D,E were not sane.
  185.          */
  186.         if( mbedtls_mpi_cmp_int( &K, 1 ) != 0 )
  187.         {
  188.             break;
  189.         }
  190.     }
  191.  
  192.     ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  193.  
  194. cleanup:
  195.  
  196.     mbedtls_mpi_free( &K );
  197.     mbedtls_mpi_free( &T );
  198.     return( ret );
  199. }
  200.  
  201. /*
  202.  * Given P, Q and the public exponent E, deduce D.
  203.  * This is essentially a modular inversion.
  204.  */
  205. int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
  206.                                          mbedtls_mpi const *Q,
  207.                                          mbedtls_mpi const *E,
  208.                                          mbedtls_mpi *D )
  209. {
  210.     int ret = 0;
  211.     mbedtls_mpi K, L;
  212.  
  213.     if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
  214.         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  215.  
  216.     if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
  217.         mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
  218.         mbedtls_mpi_cmp_int( E, 0 ) == 0 )
  219.     {
  220.         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  221.     }
  222.  
  223.     mbedtls_mpi_init( &K );
  224.     mbedtls_mpi_init( &L );
  225.  
  226.     /* Temporarily put K := P-1 and L := Q-1 */
  227.     MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
  228.     MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
  229.  
  230.     /* Temporarily put D := gcd(P-1, Q-1) */
  231.     MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, &K, &L ) );
  232.  
  233.     /* K := LCM(P-1, Q-1) */
  234.     MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &L ) );
  235.     MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
  236.  
  237.     /* Compute modular inverse of E in LCM(P-1, Q-1) */
  238.     MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
  239.  
  240. cleanup:
  241.  
  242.     mbedtls_mpi_free( &K );
  243.     mbedtls_mpi_free( &L );
  244.  
  245.     return( ret );
  246. }
  247.  
  248. /*
  249.  * Check that RSA CRT parameters are in accordance with core parameters.
  250.  */
  251. int mbedtls_rsa_validate_crt( const mbedtls_mpi *P,  const mbedtls_mpi *Q,
  252.                               const mbedtls_mpi *D,  const mbedtls_mpi *DP,
  253.                               const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
  254. {
  255.     int ret = 0;
  256.  
  257.     mbedtls_mpi K, L;
  258.     mbedtls_mpi_init( &K );
  259.     mbedtls_mpi_init( &L );
  260.  
  261.     /* Check that DP - D == 0 mod P - 1 */
  262.     if( DP != NULL )
  263.     {
  264.         if( P == NULL )
  265.         {
  266.             ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  267.             goto cleanup;
  268.         }
  269.  
  270.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
  271.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
  272.         MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
  273.  
  274.         if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
  275.         {
  276.             ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  277.             goto cleanup;
  278.         }
  279.     }
  280.  
  281.     /* Check that DQ - D == 0 mod Q - 1 */
  282.     if( DQ != NULL )
  283.     {
  284.         if( Q == NULL )
  285.         {
  286.             ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  287.             goto cleanup;
  288.         }
  289.  
  290.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
  291.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
  292.         MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
  293.  
  294.         if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
  295.         {
  296.             ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  297.             goto cleanup;
  298.         }
  299.     }
  300.  
  301.     /* Check that QP * Q - 1 == 0 mod P */
  302.     if( QP != NULL )
  303.     {
  304.         if( P == NULL || Q == NULL )
  305.         {
  306.             ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
  307.             goto cleanup;
  308.         }
  309.  
  310.         MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
  311.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  312.         MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
  313.         if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  314.         {
  315.             ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  316.             goto cleanup;
  317.         }
  318.     }
  319.  
  320. cleanup:
  321.  
  322.     /* Wrap MPI error codes by RSA check failure error code */
  323.     if( ret != 0 &&
  324.         ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
  325.         ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
  326.     {
  327.         ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  328.     }
  329.  
  330.     mbedtls_mpi_free( &K );
  331.     mbedtls_mpi_free( &L );
  332.  
  333.     return( ret );
  334. }
  335.  
  336. /*
  337.  * Check that core RSA parameters are sane.
  338.  */
  339. int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
  340.                                  const mbedtls_mpi *Q, const mbedtls_mpi *D,
  341.                                  const mbedtls_mpi *E,
  342.                                  int (*f_rng)(void *, unsigned char *, size_t),
  343.                                  void *p_rng )
  344. {
  345.     int ret = 0;
  346.     mbedtls_mpi K, L;
  347.  
  348.     mbedtls_mpi_init( &K );
  349.     mbedtls_mpi_init( &L );
  350.  
  351.     /*
  352.      * Step 1: If PRNG provided, check that P and Q are prime
  353.      */
  354.  
  355. #if defined(MBEDTLS_GENPRIME)
  356.     /*
  357.      * When generating keys, the strongest security we support aims for an error
  358.      * rate of at most 2^-100 and we are aiming for the same certainty here as
  359.      * well.
  360.      */
  361.     if( f_rng != NULL && P != NULL &&
  362.         ( ret = mbedtls_mpi_is_prime_ext( P, 50, f_rng, p_rng ) ) != 0 )
  363.     {
  364.         ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  365.         goto cleanup;
  366.     }
  367.  
  368.     if( f_rng != NULL && Q != NULL &&
  369.         ( ret = mbedtls_mpi_is_prime_ext( Q, 50, f_rng, p_rng ) ) != 0 )
  370.     {
  371.         ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  372.         goto cleanup;
  373.     }
  374. #else
  375.     ((void) f_rng);
  376.     ((void) p_rng);
  377. #endif /* MBEDTLS_GENPRIME */
  378.  
  379.     /*
  380.      * Step 2: Check that 1 < N = P * Q
  381.      */
  382.  
  383.     if( P != NULL && Q != NULL && N != NULL )
  384.     {
  385.         MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
  386.         if( mbedtls_mpi_cmp_int( N, 1 )  <= 0 ||
  387.             mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
  388.         {
  389.             ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  390.             goto cleanup;
  391.         }
  392.     }
  393.  
  394.     /*
  395.      * Step 3: Check and 1 < D, E < N if present.
  396.      */
  397.  
  398.     if( N != NULL && D != NULL && E != NULL )
  399.     {
  400.         if ( mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
  401.              mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
  402.              mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
  403.              mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
  404.         {
  405.             ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  406.             goto cleanup;
  407.         }
  408.     }
  409.  
  410.     /*
  411.      * Step 4: Check that D, E are inverse modulo P-1 and Q-1
  412.      */
  413.  
  414.     if( P != NULL && Q != NULL && D != NULL && E != NULL )
  415.     {
  416.         if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
  417.             mbedtls_mpi_cmp_int( Q, 1 ) <= 0 )
  418.         {
  419.             ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  420.             goto cleanup;
  421.         }
  422.  
  423.         /* Compute DE-1 mod P-1 */
  424.         MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
  425.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  426.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
  427.         MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
  428.         if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  429.         {
  430.             ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  431.             goto cleanup;
  432.         }
  433.  
  434.         /* Compute DE-1 mod Q-1 */
  435.         MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
  436.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
  437.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
  438.         MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
  439.         if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
  440.         {
  441.             ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  442.             goto cleanup;
  443.         }
  444.     }
  445.  
  446. cleanup:
  447.  
  448.     mbedtls_mpi_free( &K );
  449.     mbedtls_mpi_free( &L );
  450.  
  451.     /* Wrap MPI error codes by RSA check failure error code */
  452.     if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
  453.     {
  454.         ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
  455.     }
  456.  
  457.     return( ret );
  458. }
  459.  
  460. int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
  461.                             const mbedtls_mpi *D, mbedtls_mpi *DP,
  462.                             mbedtls_mpi *DQ, mbedtls_mpi *QP )
  463. {
  464.     int ret = 0;
  465.     mbedtls_mpi K;
  466.     mbedtls_mpi_init( &K );
  467.  
  468.     /* DP = D mod P-1 */
  469.     if( DP != NULL )
  470.     {
  471.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1  ) );
  472.         MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
  473.     }
  474.  
  475.     /* DQ = D mod Q-1 */
  476.     if( DQ != NULL )
  477.     {
  478.         MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1  ) );
  479.         MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
  480.     }
  481.  
  482.     /* QP = Q^{-1} mod P */
  483.     if( QP != NULL )
  484.     {
  485.         MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
  486.     }
  487.  
  488. cleanup:
  489.     mbedtls_mpi_free( &K );
  490.  
  491.     return( ret );
  492. }
  493.  
  494. #endif /* MBEDTLS_RSA_C */
  495.