rsa.c

Go to the documentation of this file.
00001 /*
00002  *  Copyright(C) 2006 Cameron Rich
00003  *
00004  *  This library is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published by
00006  *  the Free Software Foundation; either version 2.1 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public License
00015  *  along with this library; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 /**
00020  * Implements the RSA public encryption algorithm. Uses the bigint library to
00021  * perform its calculations.
00022  */
00023 
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <time.h>
00027 #include <stdlib.h>
00028 #include "crypto.h"
00029 
00030 #ifdef CONFIG_BIGINT_CRT
00031 static bigint *bi_crt(const RSA_CTX *rsa, bigint *bi);
00032 #endif
00033 
00034 void RSA_priv_key_new(RSA_CTX **ctx, 
00035         const uint8_t *modulus, int mod_len,
00036         const uint8_t *pub_exp, int pub_len,
00037         const uint8_t *priv_exp, int priv_len
00038 #if CONFIG_BIGINT_CRT
00039       , const uint8_t *p, int p_len,
00040         const uint8_t *q, int q_len,
00041         const uint8_t *dP, int dP_len,
00042         const uint8_t *dQ, int dQ_len,
00043         const uint8_t *qInv, int qInv_len
00044 #endif
00045     )
00046 {
00047     RSA_CTX *rsa_ctx;
00048     BI_CTX *bi_ctx;
00049     RSA_pub_key_new(ctx, modulus, mod_len, pub_exp, pub_len);
00050     rsa_ctx = *ctx;
00051     bi_ctx = rsa_ctx->bi_ctx;
00052     rsa_ctx->d = bi_import(bi_ctx, priv_exp, priv_len);
00053     bi_permanent(rsa_ctx->d);
00054 
00055 #ifdef CONFIG_BIGINT_CRT
00056     rsa_ctx->p = bi_import(bi_ctx, p, p_len);
00057     rsa_ctx->q = bi_import(bi_ctx, q, q_len);
00058     rsa_ctx->dP = bi_import(bi_ctx, dP, dP_len);
00059     rsa_ctx->dQ = bi_import(bi_ctx, dQ, dQ_len);
00060     rsa_ctx->qInv = bi_import(bi_ctx, qInv, qInv_len);
00061     bi_permanent(rsa_ctx->dP);
00062     bi_permanent(rsa_ctx->dQ);
00063     bi_permanent(rsa_ctx->qInv);
00064     bi_set_mod(bi_ctx, rsa_ctx->p, BIGINT_P_OFFSET);
00065     bi_set_mod(bi_ctx, rsa_ctx->q, BIGINT_Q_OFFSET);
00066 #endif
00067 }
00068 
00069 void RSA_pub_key_new(RSA_CTX **ctx, 
00070         const uint8_t *modulus, int mod_len,
00071         const uint8_t *pub_exp, int pub_len)
00072 {
00073     RSA_CTX *rsa_ctx;
00074     BI_CTX *bi_ctx = bi_initialize();
00075     *ctx = (RSA_CTX *)calloc(1, sizeof(RSA_CTX));
00076     rsa_ctx = *ctx;
00077     rsa_ctx->bi_ctx = bi_ctx;
00078     rsa_ctx->num_octets = (mod_len & 0xFFF0);
00079     rsa_ctx->m = bi_import(bi_ctx, modulus, mod_len);
00080     bi_set_mod(bi_ctx, rsa_ctx->m, BIGINT_M_OFFSET);
00081     rsa_ctx->e = bi_import(bi_ctx, pub_exp, pub_len);
00082     bi_permanent(rsa_ctx->e);
00083 }
00084 
00085 /**
00086  * Free up any RSA context resources.
00087  */
00088 void RSA_free(RSA_CTX *rsa_ctx)
00089 {
00090     BI_CTX *bi_ctx;
00091     if (rsa_ctx == NULL)                /* deal with ptrs that are null */
00092         return;
00093 
00094     bi_ctx = rsa_ctx->bi_ctx;
00095 
00096     bi_depermanent(rsa_ctx->e);
00097     bi_free(bi_ctx, rsa_ctx->e);
00098     bi_free_mod(rsa_ctx->bi_ctx, BIGINT_M_OFFSET);
00099 
00100     if (rsa_ctx->d)
00101     {
00102         bi_depermanent(rsa_ctx->d);
00103         bi_free(bi_ctx, rsa_ctx->d);
00104 #ifdef CONFIG_BIGINT_CRT
00105         bi_depermanent(rsa_ctx->dP);
00106         bi_depermanent(rsa_ctx->dQ);
00107         bi_depermanent(rsa_ctx->qInv);
00108         bi_free(bi_ctx, rsa_ctx->dP);
00109         bi_free(bi_ctx, rsa_ctx->dQ);
00110         bi_free(bi_ctx, rsa_ctx->qInv);
00111         bi_free_mod(rsa_ctx->bi_ctx, BIGINT_P_OFFSET);
00112         bi_free_mod(rsa_ctx->bi_ctx, BIGINT_Q_OFFSET);
00113 #endif
00114     }
00115 
00116     bi_terminate(bi_ctx);
00117     free(rsa_ctx);
00118 }
00119 
00120 /**
00121  * @brief Use PKCS1.5 for decryption/verification.
00122  * @param ctx [in] The context
00123  * @param in_data [in] The data to encrypt (must be < modulus size-11)
00124  * @param out_data [out] The encrypted data.
00125  * @param is_decryption [in] Decryption or verify operation.
00126  * @return  The number of bytes that were originally encrypted. -1 on error.
00127  * @see http://www.rsasecurity.com/rsalabs/node.asp?id=2125
00128  */
00129 int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, 
00130                             uint8_t *out_data, int is_decryption)
00131 {
00132     int byte_size = ctx->num_octets;
00133     uint8_t *block;
00134     int i, size;
00135     bigint *decrypted_bi, *dat_bi;
00136 
00137     memset(out_data, 0, byte_size); /* initialise */
00138 
00139     /* decrypt */
00140     dat_bi = bi_import(ctx->bi_ctx, in_data, byte_size);
00141 #ifdef CONFIG_SSL_CERT_VERIFICATION
00142     decrypted_bi = is_decryption ?  /* decrypt or verify? */
00143             RSA_private(ctx, dat_bi) : RSA_public(ctx, dat_bi);
00144 #else   /* always a decryption */
00145     decrypted_bi = RSA_private(ctx, dat_bi);
00146 #endif
00147 
00148     /* convert to a normal block */
00149     block = (uint8_t *)malloc(byte_size);
00150     bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size);
00151 
00152     i = 10; /* start at the first possible non-padded byte */
00153 
00154 #ifdef CONFIG_SSL_CERT_VERIFICATION
00155     if (is_decryption == 0) /* PKCS1.5 signing pads with "0xff"s */
00156     {
00157         while (block[i++] == 0xff && i < byte_size);
00158 
00159         if (block[i-2] != 0xff)
00160             i = byte_size;     /*ensure size is 0 */   
00161     }
00162     else                    /* PKCS1.5 encryption padding is random */
00163 #endif
00164     {
00165         while (block[i++] && i < byte_size);
00166     }
00167     size = byte_size - i;
00168 
00169     /* get only the bit we want */
00170     if (size > 0)
00171         memcpy(out_data, &block[i], size);
00172     
00173     free(block);
00174     return size ? size : -1;
00175 }
00176 
00177 /**
00178  * Performs m = c^d mod n
00179  */
00180 bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg)
00181 {
00182 #ifdef CONFIG_BIGINT_CRT
00183     return bi_crt(c, bi_msg);
00184 #else
00185     BI_CTX *ctx = c->bi_ctx;
00186     ctx->mod_offset = BIGINT_M_OFFSET;
00187     return bi_mod_power(ctx, bi_msg, c->d);
00188 #endif
00189 }
00190 
00191 #ifdef CONFIG_BIGINT_CRT
00192 /**
00193  * Use the Chinese Remainder Theorem to quickly perform RSA decrypts.
00194  * This should really be in bigint.c (and was at one stage), but needs 
00195  * access to the RSA_CTX context...
00196  */
00197 static bigint *bi_crt(const RSA_CTX *rsa, bigint *bi)
00198 {
00199     BI_CTX *ctx = rsa->bi_ctx;
00200     bigint *m1, *m2, *h;
00201 
00202     /* Montgomery has a condition the 0 < x, y < m and these products violate
00203      * that condition. So disable Montgomery when using CRT */
00204 #if defined(CONFIG_BIGINT_MONTGOMERY)
00205     ctx->use_classical = 1;
00206 #endif
00207     ctx->mod_offset = BIGINT_P_OFFSET;
00208     m1 = bi_mod_power(ctx, bi_copy(bi), rsa->dP);
00209 
00210     ctx->mod_offset = BIGINT_Q_OFFSET;
00211     m2 = bi_mod_power(ctx, bi, rsa->dQ);
00212 
00213     h = bi_subtract(ctx, bi_add(ctx, m1, rsa->p), bi_copy(m2), NULL);
00214     h = bi_multiply(ctx, h, rsa->qInv);
00215     ctx->mod_offset = BIGINT_P_OFFSET;
00216     h = bi_residue(ctx, h);
00217 #if defined(CONFIG_BIGINT_MONTGOMERY)
00218     ctx->use_classical = 0;         /* reset for any further operation */
00219 #endif
00220     return bi_add(ctx, m2, bi_multiply(ctx, rsa->q, h));
00221 }
00222 #endif
00223 
00224 #ifdef CONFIG_SSL_FULL_MODE
00225 /**
00226  * Used for diagnostics.
00227  */
00228 void RSA_print(const RSA_CTX *rsa_ctx) 
00229 {
00230     if (rsa_ctx == NULL)
00231         return;
00232 
00233     printf("-----------------   RSA DEBUG   ----------------\n");
00234     printf("Size:\t%d\n", rsa_ctx->num_octets);
00235     bi_print("Modulus", rsa_ctx->m);
00236     bi_print("Public Key", rsa_ctx->e);
00237     bi_print("Private Key", rsa_ctx->d);
00238 }
00239 #endif
00240 
00241 #ifdef CONFIG_SSL_CERT_VERIFICATION
00242 /**
00243  * Performs c = m^e mod n
00244  */
00245 bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg)
00246 {
00247     c->bi_ctx->mod_offset = BIGINT_M_OFFSET;
00248     return bi_mod_power(c->bi_ctx, bi_msg, c->e);
00249 }
00250 
00251 /**
00252  * Use PKCS1.5 for encryption/signing.
00253  * see http://www.rsasecurity.com/rsalabs/node.asp?id=2125
00254  */
00255 int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len, 
00256         uint8_t *out_data, int is_signing)
00257 {
00258     int byte_size = ctx->num_octets;
00259     int num_pads_needed = byte_size-in_len-3;
00260     bigint *dat_bi, *encrypt_bi;
00261 
00262     /* note: in_len+11 must be > byte_size */
00263     out_data[0] = 0;     /* ensure encryption block is < modulus */
00264 
00265     if (is_signing)
00266     {
00267         out_data[1] = 1;        /* PKCS1.5 signing pads with "0xff"'s */
00268         memset(&out_data[2], 0xff, num_pads_needed);
00269     }
00270     else /* randomize the encryption padding with non-zero bytes */   
00271     {
00272         out_data[1] = 2;
00273         get_random_NZ(num_pads_needed, &out_data[2]);
00274     }
00275 
00276     out_data[2+num_pads_needed] = 0;
00277     memcpy(&out_data[3+num_pads_needed], in_data, in_len);
00278 
00279     /* now encrypt it */
00280     dat_bi = bi_import(ctx->bi_ctx, out_data, byte_size);
00281     encrypt_bi = is_signing ? RSA_private(ctx, dat_bi) : 
00282         RSA_public(ctx, dat_bi);
00283     bi_export(ctx->bi_ctx, encrypt_bi, out_data, byte_size);
00284     return byte_size;
00285 }
00286 
00287 #if 0
00288 /**
00289  * Take a signature and decrypt it.
00290  */
00291 bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
00292         bigint *modulus, bigint *pub_exp)
00293 {
00294     uint8_t *block;
00295     int i, size;
00296     bigint *decrypted_bi, *dat_bi;
00297     bigint *bir = NULL;
00298 
00299     block = (uint8_t *)malloc(sig_len);
00300 
00301     /* decrypt */
00302     dat_bi = bi_import(ctx, sig, sig_len);
00303     ctx->mod_offset = BIGINT_M_OFFSET;
00304 
00305     /* convert to a normal block */
00306     decrypted_bi = bi_mod_power2(ctx, dat_bi, modulus, pub_exp);
00307 
00308     bi_export(ctx, decrypted_bi, block, sig_len);
00309     ctx->mod_offset = BIGINT_M_OFFSET;
00310 
00311     i = 10; /* start at the first possible non-padded byte */
00312     while (block[i++] && i < sig_len);
00313     size = sig_len - i;
00314 
00315     /* get only the bit we want */
00316     if (size > 0)
00317     {
00318         int len;
00319         const uint8_t *sig_ptr = x509_get_signature(&block[i], &len);
00320 
00321         if (sig_ptr)
00322         {
00323             bir = bi_import(ctx, sig_ptr, len);
00324         }
00325     }
00326 
00327     free(block);
00328     return bir;
00329 }
00330 #endif
00331 
00332 #endif  /* CONFIG_SSL_CERT_VERIFICATION */

Generated on Tue Apr 6 20:00:52 2010 for gPXE by  doxygen 1.5.7.1