| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <string.h> |
| 38 |
#include <time.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include "os_port.h" |
| 41 |
#include "crypto.h" |
| 42 |
|
| 43 |
void RSA_priv_key_new(RSA_CTX **ctx, |
| 44 |
const uint8_t *modulus, int mod_len, |
| 45 |
const uint8_t *pub_exp, int pub_len, |
| 46 |
const uint8_t *priv_exp, int priv_len |
| 47 |
#if CONFIG_BIGINT_CRT |
| 48 |
, const uint8_t *p, int p_len, |
| 49 |
const uint8_t *q, int q_len, |
| 50 |
const uint8_t *dP, int dP_len, |
| 51 |
const uint8_t *dQ, int dQ_len, |
| 52 |
const uint8_t *qInv, int qInv_len |
| 53 |
#endif |
| 54 |
) |
| 55 |
{ |
| 56 |
RSA_CTX *rsa_ctx; |
| 57 |
BI_CTX *bi_ctx; |
| 58 |
RSA_pub_key_new(ctx, modulus, mod_len, pub_exp, pub_len); |
| 59 |
rsa_ctx = *ctx; |
| 60 |
bi_ctx = rsa_ctx->bi_ctx; |
| 61 |
rsa_ctx->d = bi_import(bi_ctx, priv_exp, priv_len); |
| 62 |
bi_permanent(rsa_ctx->d); |
| 63 |
|
| 64 |
#ifdef CONFIG_BIGINT_CRT |
| 65 |
rsa_ctx->p = bi_import(bi_ctx, p, p_len); |
| 66 |
rsa_ctx->q = bi_import(bi_ctx, q, q_len); |
| 67 |
rsa_ctx->dP = bi_import(bi_ctx, dP, dP_len); |
| 68 |
rsa_ctx->dQ = bi_import(bi_ctx, dQ, dQ_len); |
| 69 |
rsa_ctx->qInv = bi_import(bi_ctx, qInv, qInv_len); |
| 70 |
bi_permanent(rsa_ctx->dP); |
| 71 |
bi_permanent(rsa_ctx->dQ); |
| 72 |
bi_permanent(rsa_ctx->qInv); |
| 73 |
bi_set_mod(bi_ctx, rsa_ctx->p, BIGINT_P_OFFSET); |
| 74 |
bi_set_mod(bi_ctx, rsa_ctx->q, BIGINT_Q_OFFSET); |
| 75 |
#endif |
| 76 |
} |
| 77 |
|
| 78 |
void RSA_pub_key_new(RSA_CTX **ctx, |
| 79 |
const uint8_t *modulus, int mod_len, |
| 80 |
const uint8_t *pub_exp, int pub_len) |
| 81 |
{ |
| 82 |
RSA_CTX *rsa_ctx; |
| 83 |
BI_CTX *bi_ctx; |
| 84 |
|
| 85 |
if (*ctx) |
| 86 |
RSA_free(*ctx); |
| 87 |
|
| 88 |
bi_ctx = bi_initialize(); |
| 89 |
*ctx = (RSA_CTX *)calloc(1, sizeof(RSA_CTX)); |
| 90 |
rsa_ctx = *ctx; |
| 91 |
rsa_ctx->bi_ctx = bi_ctx; |
| 92 |
rsa_ctx->num_octets = mod_len; |
| 93 |
rsa_ctx->m = bi_import(bi_ctx, modulus, mod_len); |
| 94 |
bi_set_mod(bi_ctx, rsa_ctx->m, BIGINT_M_OFFSET); |
| 95 |
rsa_ctx->e = bi_import(bi_ctx, pub_exp, pub_len); |
| 96 |
bi_permanent(rsa_ctx->e); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
void RSA_free(RSA_CTX *rsa_ctx) |
| 103 |
{ |
| 104 |
BI_CTX *bi_ctx; |
| 105 |
if (rsa_ctx == NULL) |
| 106 |
return; |
| 107 |
|
| 108 |
bi_ctx = rsa_ctx->bi_ctx; |
| 109 |
|
| 110 |
bi_depermanent(rsa_ctx->e); |
| 111 |
bi_free(bi_ctx, rsa_ctx->e); |
| 112 |
bi_free_mod(rsa_ctx->bi_ctx, BIGINT_M_OFFSET); |
| 113 |
|
| 114 |
if (rsa_ctx->d) |
| 115 |
{ |
| 116 |
bi_depermanent(rsa_ctx->d); |
| 117 |
bi_free(bi_ctx, rsa_ctx->d); |
| 118 |
#ifdef CONFIG_BIGINT_CRT |
| 119 |
bi_depermanent(rsa_ctx->dP); |
| 120 |
bi_depermanent(rsa_ctx->dQ); |
| 121 |
bi_depermanent(rsa_ctx->qInv); |
| 122 |
bi_free(bi_ctx, rsa_ctx->dP); |
| 123 |
bi_free(bi_ctx, rsa_ctx->dQ); |
| 124 |
bi_free(bi_ctx, rsa_ctx->qInv); |
| 125 |
bi_free_mod(rsa_ctx->bi_ctx, BIGINT_P_OFFSET); |
| 126 |
bi_free_mod(rsa_ctx->bi_ctx, BIGINT_Q_OFFSET); |
| 127 |
#endif |
| 128 |
} |
| 129 |
|
| 130 |
bi_terminate(bi_ctx); |
| 131 |
free(rsa_ctx); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, |
| 145 |
uint8_t *out_data, int out_len, int is_decryption) |
| 146 |
{ |
| 147 |
const int byte_size = ctx->num_octets; |
| 148 |
int i = 0, size; |
| 149 |
bigint *decrypted_bi, *dat_bi; |
| 150 |
uint8_t *block = (uint8_t *)malloc(byte_size); |
| 151 |
int pad_count = 0; |
| 152 |
|
| 153 |
if (out_len < byte_size) |
| 154 |
return -1; |
| 155 |
|
| 156 |
memset(out_data, 0, out_len); |
| 157 |
|
| 158 |
|
| 159 |
dat_bi = bi_import(ctx->bi_ctx, in_data, byte_size); |
| 160 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 161 |
decrypted_bi = is_decryption ? |
| 162 |
RSA_private(ctx, dat_bi) : RSA_public(ctx, dat_bi); |
| 163 |
#else |
| 164 |
decrypted_bi = RSA_private(ctx, dat_bi); |
| 165 |
#endif |
| 166 |
|
| 167 |
|
| 168 |
bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size); |
| 169 |
|
| 170 |
if (block[i++] != 0) { |
| 171 |
free(block); |
| 172 |
return -1; |
| 173 |
} |
| 174 |
|
| 175 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 176 |
if (is_decryption == 0) |
| 177 |
{ |
| 178 |
if (block[i++] != 0x01) { |
| 179 |
free(block); |
| 180 |
return -1; |
| 181 |
} |
| 182 |
|
| 183 |
while (block[i++] == 0xff && i < byte_size) |
| 184 |
pad_count++; |
| 185 |
} |
| 186 |
else |
| 187 |
#endif |
| 188 |
{ |
| 189 |
if (block[i++] != 0x02) { |
| 190 |
free(block); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
|
| 194 |
while (block[i++] && i < byte_size) |
| 195 |
pad_count++; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
if (i == byte_size || pad_count < 8) { |
| 200 |
free(block); |
| 201 |
return -1; |
| 202 |
} |
| 203 |
|
| 204 |
size = byte_size - i; |
| 205 |
|
| 206 |
|
| 207 |
memcpy(out_data, &block[i], size); |
| 208 |
free(block); |
| 209 |
return size; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
int RSA_decrypt2(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data) |
| 221 |
{ |
| 222 |
const int byte_size = ctx->num_octets; |
| 223 |
int i, size; |
| 224 |
bigint *decrypted_bi, *dat_bi; |
| 225 |
uint8_t *block = (uint8_t *)malloc(byte_size); |
| 226 |
|
| 227 |
|
| 228 |
dat_bi = bi_import(ctx->bi_ctx, in_data, byte_size); |
| 229 |
|
| 230 |
decrypted_bi = RSA_public(ctx, dat_bi); |
| 231 |
|
| 232 |
|
| 233 |
bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size); |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
i = 2; |
| 238 |
while (block[i++] == 0xff && i < byte_size) |
| 239 |
; |
| 240 |
|
| 241 |
|
| 242 |
if (i < byte_size && block[i] == 0x00) |
| 243 |
i++; |
| 244 |
|
| 245 |
size = byte_size - i; |
| 246 |
|
| 247 |
|
| 248 |
if (size > 0) { |
| 249 |
memcpy(out_data, &block[i], size); |
| 250 |
} |
| 251 |
|
| 252 |
free(block); |
| 253 |
|
| 254 |
return size ? size : -1; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg) |
| 261 |
{ |
| 262 |
#ifdef CONFIG_BIGINT_CRT |
| 263 |
return bi_crt(c->bi_ctx, bi_msg, c->dP, c->dQ, c->p, c->q, c->qInv); |
| 264 |
#else |
| 265 |
BI_CTX *ctx = c->bi_ctx; |
| 266 |
ctx->mod_offset = BIGINT_M_OFFSET; |
| 267 |
return bi_mod_power(ctx, bi_msg, c->d); |
| 268 |
#endif |
| 269 |
} |
| 270 |
|
| 271 |
#ifdef CONFIG_DEBUG |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
void RSA_print(const RSA_CTX *rsa_ctx) |
| 276 |
{ |
| 277 |
if (rsa_ctx == NULL) |
| 278 |
return; |
| 279 |
|
| 280 |
printf("----------------- RSA DEBUG ----------------\n"); |
| 281 |
printf("Size:\t%d\n", rsa_ctx->num_octets); |
| 282 |
bi_print("Modulus", rsa_ctx->m); |
| 283 |
bi_print("Public Key", rsa_ctx->e); |
| 284 |
bi_print("Private Key", rsa_ctx->d); |
| 285 |
} |
| 286 |
#endif |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg) |
| 293 |
{ |
| 294 |
c->bi_ctx->mod_offset = BIGINT_M_OFFSET; |
| 295 |
return bi_mod_power(c->bi_ctx, bi_msg, c->e); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len, |
| 303 |
uint8_t *out_data, int is_signing) |
| 304 |
{ |
| 305 |
int byte_size = ctx->num_octets; |
| 306 |
int num_pads_needed = byte_size-in_len-3; |
| 307 |
bigint *dat_bi, *encrypt_bi; |
| 308 |
|
| 309 |
|
| 310 |
out_data[0] = 0; |
| 311 |
|
| 312 |
if (is_signing) |
| 313 |
{ |
| 314 |
out_data[1] = 1; |
| 315 |
memset(&out_data[2], 0xff, num_pads_needed); |
| 316 |
} |
| 317 |
else |
| 318 |
{ |
| 319 |
out_data[1] = 2; |
| 320 |
if (get_random_NZ(num_pads_needed, &out_data[2]) < 0) |
| 321 |
return -1; |
| 322 |
} |
| 323 |
|
| 324 |
out_data[2+num_pads_needed] = 0; |
| 325 |
memcpy(&out_data[3+num_pads_needed], in_data, in_len); |
| 326 |
|
| 327 |
|
| 328 |
dat_bi = bi_import(ctx->bi_ctx, out_data, byte_size); |
| 329 |
encrypt_bi = is_signing ? RSA_private(ctx, dat_bi) : |
| 330 |
RSA_public(ctx, dat_bi); |
| 331 |
bi_export(ctx->bi_ctx, encrypt_bi, out_data, byte_size); |
| 332 |
|
| 333 |
|
| 334 |
bi_clear_cache(ctx->bi_ctx); |
| 335 |
return byte_size; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|