00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00087
00088 void RSA_free(RSA_CTX *rsa_ctx)
00089 {
00090 BI_CTX *bi_ctx;
00091 if (rsa_ctx == 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
00122
00123
00124
00125
00126
00127
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);
00138
00139
00140 dat_bi = bi_import(ctx->bi_ctx, in_data, byte_size);
00141 #ifdef CONFIG_SSL_CERT_VERIFICATION
00142 decrypted_bi = is_decryption ?
00143 RSA_private(ctx, dat_bi) : RSA_public(ctx, dat_bi);
00144 #else
00145 decrypted_bi = RSA_private(ctx, dat_bi);
00146 #endif
00147
00148
00149 block = (uint8_t *)malloc(byte_size);
00150 bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size);
00151
00152 i = 10;
00153
00154 #ifdef CONFIG_SSL_CERT_VERIFICATION
00155 if (is_decryption == 0)
00156 {
00157 while (block[i++] == 0xff && i < byte_size);
00158
00159 if (block[i-2] != 0xff)
00160 i = byte_size;
00161 }
00162 else
00163 #endif
00164 {
00165 while (block[i++] && i < byte_size);
00166 }
00167 size = byte_size - i;
00168
00169
00170 if (size > 0)
00171 memcpy(out_data, &block[i], size);
00172
00173 free(block);
00174 return size ? size : -1;
00175 }
00176
00177
00178
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
00194
00195
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
00203
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;
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
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
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
00253
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
00263 out_data[0] = 0;
00264
00265 if (is_signing)
00266 {
00267 out_data[1] = 1;
00268 memset(&out_data[2], 0xff, num_pads_needed);
00269 }
00270 else
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
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
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
00302 dat_bi = bi_import(ctx, sig, sig_len);
00303 ctx->mod_offset = BIGINT_M_OFFSET;
00304
00305
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;
00312 while (block[i++] && i < sig_len);
00313 size = sig_len - i;
00314
00315
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