| 51 | | static void _wi_p7_rsa_dealloc(wi_runtime_instance_t *); |
|---|
| 52 | | static wi_string_t * _wi_p7_rsa_description(wi_runtime_instance_t *); |
|---|
| 53 | | |
|---|
| 54 | | static wi_data_t * _wi_p7_rsa_public_key(wi_p7_rsa_t *); |
|---|
| 55 | | |
|---|
| 56 | | static wi_runtime_id_t _wi_p7_rsa_runtime_id = WI_RUNTIME_ID_NULL; |
|---|
| 57 | | static wi_runtime_class_t _wi_p7_rsa_runtime_class = { |
|---|
| 58 | | "wi_p7_rsa_t", |
|---|
| 59 | | _wi_p7_rsa_dealloc, |
|---|
| | 51 | static void _wi_rsa_dealloc(wi_runtime_instance_t *); |
|---|
| | 52 | static wi_string_t * _wi_rsa_description(wi_runtime_instance_t *); |
|---|
| | 53 | |
|---|
| | 54 | static wi_data_t * _wi_rsa_public_key(wi_rsa_t *); |
|---|
| | 55 | |
|---|
| | 56 | static wi_runtime_id_t _wi_rsa_runtime_id = WI_RUNTIME_ID_NULL; |
|---|
| | 57 | static wi_runtime_class_t _wi_rsa_runtime_class = { |
|---|
| | 58 | "wi_rsa_t", |
|---|
| | 59 | _wi_rsa_dealloc, |
|---|
| 79 | | static void _wi_p7_cipher_dealloc(wi_runtime_instance_t *); |
|---|
| 80 | | static wi_string_t * _wi_p7_cipher_description(wi_runtime_instance_t *); |
|---|
| 81 | | |
|---|
| 82 | | static const EVP_CIPHER * _wi_p7_cipher_cipher(wi_p7_cipher_t *); |
|---|
| 83 | | static void _wi_p7_cipher_configure_cipher(wi_p7_cipher_t *); |
|---|
| 84 | | |
|---|
| 85 | | static wi_runtime_id_t _wi_p7_cipher_runtime_id = WI_RUNTIME_ID_NULL; |
|---|
| 86 | | static wi_runtime_class_t _wi_p7_cipher_runtime_class = { |
|---|
| 87 | | "wi_p7_cipher_t", |
|---|
| 88 | | _wi_p7_cipher_dealloc, |
|---|
| | 79 | static void _wi_cipher_dealloc(wi_runtime_instance_t *); |
|---|
| | 80 | static wi_string_t * _wi_cipher_description(wi_runtime_instance_t *); |
|---|
| | 81 | |
|---|
| | 82 | static const EVP_CIPHER * _wi_cipher_cipher(wi_cipher_t *); |
|---|
| | 83 | static void _wi_cipher_configure_cipher(wi_cipher_t *); |
|---|
| | 84 | |
|---|
| | 85 | static wi_runtime_id_t _wi_cipher_runtime_id = WI_RUNTIME_ID_NULL; |
|---|
| | 86 | static wi_runtime_class_t _wi_cipher_runtime_class = { |
|---|
| | 87 | "wi_cipher_t", |
|---|
| | 88 | _wi_cipher_dealloc, |
|---|
| 97 | | void wi_p7_crypto_register(void) { |
|---|
| 98 | | _wi_p7_rsa_runtime_id = wi_runtime_register_class(&_wi_p7_rsa_runtime_class); |
|---|
| 99 | | _wi_p7_cipher_runtime_id = wi_runtime_register_class(&_wi_p7_cipher_runtime_class); |
|---|
| 100 | | } |
|---|
| 101 | | |
|---|
| 102 | | |
|---|
| 103 | | |
|---|
| 104 | | void wi_p7_crypto_initialize(void) { |
|---|
| 105 | | } |
|---|
| 106 | | |
|---|
| 107 | | |
|---|
| 108 | | |
|---|
| 109 | | #pragma mark - |
|---|
| 110 | | |
|---|
| 111 | | wi_runtime_id_t wi_p7_rsa_runtime_id(void) { |
|---|
| 112 | | return _wi_p7_rsa_runtime_id; |
|---|
| 113 | | } |
|---|
| 114 | | |
|---|
| 115 | | |
|---|
| 116 | | |
|---|
| 117 | | #pragma mark - |
|---|
| 118 | | |
|---|
| 119 | | wi_p7_rsa_t * wi_p7_rsa_alloc(void) { |
|---|
| 120 | | return wi_runtime_create_instance(_wi_p7_rsa_runtime_id, sizeof(wi_p7_rsa_t)); |
|---|
| 121 | | } |
|---|
| 122 | | |
|---|
| 123 | | |
|---|
| 124 | | |
|---|
| 125 | | wi_p7_rsa_t * wi_p7_rsa_init_with_bits(wi_p7_rsa_t *p7_rsa, wi_uinteger_t size) { |
|---|
| 126 | | p7_rsa->rsa = RSA_generate_key(size, RSA_F4, NULL, NULL); |
|---|
| 127 | | |
|---|
| 128 | | if(!p7_rsa->rsa) { |
|---|
| 129 | | wi_release(p7_rsa); |
|---|
| 130 | | |
|---|
| 131 | | return NULL; |
|---|
| 132 | | } |
|---|
| 133 | | |
|---|
| 134 | | p7_rsa->public_key = wi_retain(_wi_p7_rsa_public_key(p7_rsa)); |
|---|
| 135 | | |
|---|
| 136 | | if(!p7_rsa->public_key) { |
|---|
| 137 | | wi_release(p7_rsa); |
|---|
| 138 | | |
|---|
| 139 | | return NULL; |
|---|
| 140 | | } |
|---|
| 141 | | |
|---|
| 142 | | return p7_rsa; |
|---|
| 143 | | } |
|---|
| 144 | | |
|---|
| 145 | | |
|---|
| 146 | | |
|---|
| 147 | | wi_p7_rsa_t * wi_p7_rsa_init_with_pem_file(wi_p7_rsa_t *p7_rsa, wi_string_t *path) { |
|---|
| | 97 | void wi_crypto_register(void) { |
|---|
| | 98 | _wi_rsa_runtime_id = wi_runtime_register_class(&_wi_rsa_runtime_class); |
|---|
| | 99 | _wi_cipher_runtime_id = wi_runtime_register_class(&_wi_cipher_runtime_class); |
|---|
| | 100 | } |
|---|
| | 101 | |
|---|
| | 102 | |
|---|
| | 103 | |
|---|
| | 104 | void wi_crypto_initialize(void) { |
|---|
| | 105 | } |
|---|
| | 106 | |
|---|
| | 107 | |
|---|
| | 108 | |
|---|
| | 109 | #pragma mark - |
|---|
| | 110 | |
|---|
| | 111 | wi_runtime_id_t wi_rsa_runtime_id(void) { |
|---|
| | 112 | return _wi_rsa_runtime_id; |
|---|
| | 113 | } |
|---|
| | 114 | |
|---|
| | 115 | |
|---|
| | 116 | |
|---|
| | 117 | #pragma mark - |
|---|
| | 118 | |
|---|
| | 119 | wi_rsa_t * wi_rsa_alloc(void) { |
|---|
| | 120 | return wi_runtime_create_instance(_wi_rsa_runtime_id, sizeof(wi_rsa_t)); |
|---|
| | 121 | } |
|---|
| | 122 | |
|---|
| | 123 | |
|---|
| | 124 | |
|---|
| | 125 | wi_rsa_t * wi_rsa_init_with_bits(wi_rsa_t *rsa, wi_uinteger_t size) { |
|---|
| | 126 | rsa->rsa = RSA_generate_key(size, RSA_F4, NULL, NULL); |
|---|
| | 127 | |
|---|
| | 128 | if(!rsa->rsa) { |
|---|
| | 129 | wi_release(rsa); |
|---|
| | 130 | |
|---|
| | 131 | return NULL; |
|---|
| | 132 | } |
|---|
| | 133 | |
|---|
| | 134 | rsa->public_key = wi_retain(_wi_rsa_public_key(rsa)); |
|---|
| | 135 | |
|---|
| | 136 | if(!rsa->public_key) { |
|---|
| | 137 | wi_release(rsa); |
|---|
| | 138 | |
|---|
| | 139 | return NULL; |
|---|
| | 140 | } |
|---|
| | 141 | |
|---|
| | 142 | return rsa; |
|---|
| | 143 | } |
|---|
| | 144 | |
|---|
| | 145 | |
|---|
| | 146 | |
|---|
| | 147 | wi_rsa_t * wi_rsa_init_with_pem_file(wi_rsa_t *rsa, wi_string_t *path) { |
|---|
| 192 | | p7_rsa->rsa = d2i_RSA_PUBKEY(NULL, &buffer, length); |
|---|
| 193 | | |
|---|
| 194 | | if(!p7_rsa->rsa) { |
|---|
| 195 | | wi_error_set_openssl_error(); |
|---|
| 196 | | |
|---|
| 197 | | wi_release(p7_rsa); |
|---|
| 198 | | |
|---|
| 199 | | return NULL; |
|---|
| 200 | | } |
|---|
| 201 | | |
|---|
| 202 | | p7_rsa->public_key = wi_retain(data); |
|---|
| 203 | | |
|---|
| 204 | | return p7_rsa; |
|---|
| 205 | | } |
|---|
| 206 | | |
|---|
| 207 | | |
|---|
| 208 | | |
|---|
| 209 | | static void _wi_p7_rsa_dealloc(wi_runtime_instance_t *instance) { |
|---|
| 210 | | wi_p7_rsa_t *p7_rsa = instance; |
|---|
| 211 | | |
|---|
| 212 | | RSA_free(p7_rsa->rsa); |
|---|
| 213 | | wi_release(p7_rsa->public_key); |
|---|
| 214 | | } |
|---|
| 215 | | |
|---|
| 216 | | |
|---|
| 217 | | |
|---|
| 218 | | static wi_string_t * _wi_p7_rsa_description(wi_runtime_instance_t *instance) { |
|---|
| 219 | | wi_p7_rsa_t *p7_rsa = instance; |
|---|
| | 192 | rsa->rsa = d2i_RSA_PUBKEY(NULL, &buffer, length); |
|---|
| | 193 | |
|---|
| | 194 | if(!rsa->rsa) { |
|---|
| | 195 | wi_error_set_openssl_error(); |
|---|
| | 196 | |
|---|
| | 197 | wi_release(rsa); |
|---|
| | 198 | |
|---|
| | 199 | return NULL; |
|---|
| | 200 | } |
|---|
| | 201 | |
|---|
| | 202 | rsa->public_key = wi_retain(data); |
|---|
| | 203 | |
|---|
| | 204 | return rsa; |
|---|
| | 205 | } |
|---|
| | 206 | |
|---|
| | 207 | |
|---|
| | 208 | |
|---|
| | 209 | static void _wi_rsa_dealloc(wi_runtime_instance_t *instance) { |
|---|
| | 210 | wi_rsa_t *rsa = instance; |
|---|
| | 211 | |
|---|
| | 212 | RSA_free(rsa->rsa); |
|---|
| | 213 | wi_release(rsa->public_key); |
|---|
| | 214 | } |
|---|
| | 215 | |
|---|
| | 216 | |
|---|
| | 217 | |
|---|
| | 218 | static wi_string_t * _wi_rsa_description(wi_runtime_instance_t *instance) { |
|---|
| | 219 | wi_rsa_t *rsa = instance; |
|---|
| 345 | | wi_runtime_id_t wi_p7_cipher_runtime_id(void) { |
|---|
| 346 | | return _wi_p7_cipher_runtime_id; |
|---|
| 347 | | } |
|---|
| 348 | | |
|---|
| 349 | | |
|---|
| 350 | | |
|---|
| 351 | | #pragma mark - |
|---|
| 352 | | |
|---|
| 353 | | wi_p7_cipher_t * wi_p7_cipher_alloc(void) { |
|---|
| 354 | | return wi_runtime_create_instance(_wi_p7_cipher_runtime_id, sizeof(wi_p7_cipher_t)); |
|---|
| 355 | | } |
|---|
| 356 | | |
|---|
| 357 | | |
|---|
| 358 | | |
|---|
| 359 | | wi_p7_cipher_t * wi_p7_cipher_init_with_key(wi_p7_cipher_t *p7_cipher, wi_p7_cipher_type_t type, wi_data_t *key, wi_data_t *iv) { |
|---|
| | 345 | wi_runtime_id_t wi_cipher_runtime_id(void) { |
|---|
| | 346 | return _wi_cipher_runtime_id; |
|---|
| | 347 | } |
|---|
| | 348 | |
|---|
| | 349 | |
|---|
| | 350 | |
|---|
| | 351 | #pragma mark - |
|---|
| | 352 | |
|---|
| | 353 | wi_cipher_t * wi_cipher_alloc(void) { |
|---|
| | 354 | return wi_runtime_create_instance(_wi_cipher_runtime_id, sizeof(wi_cipher_t)); |
|---|
| | 355 | } |
|---|
| | 356 | |
|---|
| | 357 | |
|---|
| | 358 | |
|---|
| | 359 | wi_cipher_t * wi_cipher_init_with_key(wi_cipher_t *cipher, wi_cipher_type_t type, wi_data_t *key, wi_data_t *iv) { |
|---|
| 362 | | p7_cipher->type = type; |
|---|
| 363 | | p7_cipher->cipher = _wi_p7_cipher_cipher(p7_cipher); |
|---|
| 364 | | |
|---|
| 365 | | key_buffer = (unsigned char *) wi_data_bytes(key); |
|---|
| 366 | | iv_buffer = iv ? (unsigned char *) wi_data_bytes(iv) : NULL; |
|---|
| 367 | | |
|---|
| 368 | | if(EVP_EncryptInit(&p7_cipher->encrypt_ctx, p7_cipher->cipher, NULL, NULL) != 1) { |
|---|
| 369 | | wi_error_set_openssl_error(); |
|---|
| 370 | | |
|---|
| 371 | | wi_release(p7_cipher); |
|---|
| 372 | | |
|---|
| 373 | | return NULL; |
|---|
| 374 | | } |
|---|
| 375 | | |
|---|
| 376 | | if(EVP_DecryptInit(&p7_cipher->decrypt_ctx, p7_cipher->cipher, NULL, NULL) != 1) { |
|---|
| 377 | | wi_error_set_openssl_error(); |
|---|
| 378 | | |
|---|
| 379 | | wi_release(p7_cipher); |
|---|
| 380 | | |
|---|
| 381 | | return NULL; |
|---|
| 382 | | } |
|---|
| 383 | | |
|---|
| 384 | | _wi_p7_cipher_configure_cipher(p7_cipher); |
|---|
| 385 | | |
|---|
| 386 | | if(EVP_EncryptInit(&p7_cipher->encrypt_ctx, p7_cipher->cipher, key_buffer, iv_buffer) != 1) { |
|---|
| 387 | | wi_error_set_openssl_error(); |
|---|
| 388 | | |
|---|
| 389 | | wi_release(p7_cipher); |
|---|
| 390 | | |
|---|
| 391 | | return NULL; |
|---|
| 392 | | } |
|---|
| 393 | | |
|---|
| 394 | | if(EVP_DecryptInit(&p7_cipher->decrypt_ctx, p7_cipher->cipher, key_buffer, iv_buffer) != 1) { |
|---|
| 395 | | wi_error_set_openssl_error(); |
|---|
| 396 | | |
|---|
| 397 | | wi_release(p7_cipher); |
|---|
| 398 | | |
|---|
| 399 | | return NULL; |
|---|
| 400 | | } |
|---|
| 401 | | |
|---|
| 402 | | p7_cipher->key = wi_retain(key); |
|---|
| 403 | | p7_cipher->iv = wi_retain(iv); |
|---|
| 404 | | |
|---|
| 405 | | return p7_cipher; |
|---|
| 406 | | } |
|---|
| 407 | | |
|---|
| 408 | | |
|---|
| 409 | | |
|---|
| 410 | | wi_p7_cipher_t * wi_p7_cipher_init_with_random_key(wi_p7_cipher_t *p7_cipher, wi_p7_cipher_type_t type) { |
|---|
| | 362 | cipher->type = type; |
|---|
| | 363 | cipher->cipher = _wi_cipher_cipher(cipher); |
|---|
| | 364 | |
|---|
| | 365 | key_buffer = (unsigned char *) wi_data_bytes(key); |
|---|
| | 366 | iv_buffer = iv ? (unsigned char *) wi_data_bytes(iv) : NULL; |
|---|
| | 367 | |
|---|
| | 368 | if(EVP_EncryptInit(&cipher->encrypt_ctx, cipher->cipher, NULL, NULL) != 1) { |
|---|
| | 369 | wi_error_set_openssl_error(); |
|---|
| | 370 | |
|---|
| | 371 | wi_release(cipher); |
|---|
| | 372 | |
|---|
| | 373 | return NULL; |
|---|
| | 374 | } |
|---|
| | 375 | |
|---|
| | 376 | if(EVP_DecryptInit(&cipher->decrypt_ctx, cipher->cipher, NULL, NULL) != 1) { |
|---|
| | 377 | wi_error_set_openssl_error(); |
|---|
| | 378 | |
|---|
| | 379 | wi_release(cipher); |
|---|
| | 380 | |
|---|
| | 381 | return NULL; |
|---|
| | 382 | } |
|---|
| | 383 | |
|---|
| | 384 | _wi_cipher_configure_cipher(cipher); |
|---|
| | 385 | |
|---|
| | 386 | if(EVP_EncryptInit(&cipher->encrypt_ctx, cipher->cipher, key_buffer, iv_buffer) != 1) { |
|---|
| | 387 | wi_error_set_openssl_error(); |
|---|
| | 388 | |
|---|
| | 389 | wi_release(cipher); |
|---|
| | 390 | |
|---|
| | 391 | return NULL; |
|---|
| | 392 | } |
|---|
| | 393 | |
|---|
| | 394 | if(EVP_DecryptInit(&cipher->decrypt_ctx, cipher->cipher, key_buffer, iv_buffer) != 1) { |
|---|
| | 395 | wi_error_set_openssl_error(); |
|---|
| | 396 | |
|---|
| | 397 | wi_release(cipher); |
|---|
| | 398 | |
|---|
| | 399 | return NULL; |
|---|
| | 400 | } |
|---|
| | 401 | |
|---|
| | 402 | cipher->key = wi_retain(key); |
|---|
| | 403 | cipher->iv = wi_retain(iv); |
|---|
| | 404 | |
|---|
| | 405 | return cipher; |
|---|
| | 406 | } |
|---|
| | 407 | |
|---|
| | 408 | |
|---|
| | 409 | |
|---|
| | 410 | wi_cipher_t * wi_cipher_init_with_random_key(wi_cipher_t *cipher, wi_cipher_type_t type) { |
|---|
| 425 | | wi_release(p7_cipher); |
|---|
| 426 | | |
|---|
| 427 | | return NULL; |
|---|
| 428 | | } |
|---|
| 429 | | |
|---|
| 430 | | if(EVP_EncryptInit(&p7_cipher->encrypt_ctx, p7_cipher->cipher, NULL, NULL) != 1) { |
|---|
| 431 | | wi_error_set_openssl_error(); |
|---|
| 432 | | |
|---|
| 433 | | wi_release(p7_cipher); |
|---|
| 434 | | |
|---|
| 435 | | return NULL; |
|---|
| 436 | | } |
|---|
| 437 | | |
|---|
| 438 | | if(EVP_DecryptInit(&p7_cipher->decrypt_ctx, p7_cipher->cipher, NULL, NULL) != 1) { |
|---|
| 439 | | wi_error_set_openssl_error(); |
|---|
| 440 | | |
|---|
| 441 | | wi_release(p7_cipher); |
|---|
| 442 | | |
|---|
| 443 | | return NULL; |
|---|
| 444 | | } |
|---|
| 445 | | |
|---|
| 446 | | _wi_p7_cipher_configure_cipher(p7_cipher); |
|---|
| 447 | | |
|---|
| 448 | | if(EVP_EncryptInit(&p7_cipher->encrypt_ctx, p7_cipher->cipher, key_buffer, iv_buffer) != 1) { |
|---|
| 449 | | wi_error_set_openssl_error(); |
|---|
| 450 | | |
|---|
| 451 | | wi_release(p7_cipher); |
|---|
| 452 | | |
|---|
| 453 | | return NULL; |
|---|
| 454 | | } |
|---|
| 455 | | |
|---|
| 456 | | if(EVP_DecryptInit(&p7_cipher->decrypt_ctx, p7_cipher->cipher, key_buffer, iv_buffer) != 1) { |
|---|
| 457 | | wi_error_set_openssl_error(); |
|---|
| 458 | | |
|---|
| 459 | | wi_release(p7_cipher); |
|---|
| 460 | | |
|---|
| 461 | | return NULL; |
|---|
| 462 | | } |
|---|
| 463 | | |
|---|
| 464 | | p7_cipher->key = wi_data_init_with_bytes_no_copy(wi_data_alloc(), key_buffer, key_length, true); |
|---|
| 465 | | p7_cipher->iv = (iv_length > 0) ? wi_data_init_with_bytes_no_copy(wi_data_alloc(), iv_buffer, iv_length, true) : NULL; |
|---|
| 466 | | |
|---|
| 467 | | return p7_cipher; |
|---|
| 468 | | } |
|---|
| 469 | | |
|---|
| 470 | | |
|---|
| 471 | | |
|---|
| 472 | | static void _wi_p7_cipher_dealloc(wi_runtime_instance_t *instance) { |
|---|
| 473 | | wi_p7_cipher_t *p7_cipher = instance; |
|---|
| 474 | | |
|---|
| 475 | | EVP_CIPHER_CTX_cleanup(&p7_cipher->encrypt_ctx); |
|---|
| 476 | | EVP_CIPHER_CTX_cleanup(&p7_cipher->decrypt_ctx); |
|---|
| 477 | | |
|---|
| 478 | | wi_release(p7_cipher->key); |
|---|
| 479 | | wi_release(p7_cipher->iv); |
|---|
| 480 | | } |
|---|
| 481 | | |
|---|
| 482 | | |
|---|
| 483 | | |
|---|
| 484 | | static wi_string_t * _wi_p7_cipher_description(wi_runtime_instance_t *instance) { |
|---|
| 485 | | wi_p7_cipher_t *p7_cipher = instance; |
|---|
| | 425 | wi_release(cipher); |
|---|
| | 426 | |
|---|
| | 427 | return NULL; |
|---|
| | 428 | } |
|---|
| | 429 | |
|---|
| | 430 | if(EVP_EncryptInit(&cipher->encrypt_ctx, cipher->cipher, NULL, NULL) != 1) { |
|---|
| | 431 | wi_error_set_openssl_error(); |
|---|
| | 432 | |
|---|
| | 433 | wi_release(cipher); |
|---|
| | 434 | |
|---|
| | 435 | return NULL; |
|---|
| | 436 | } |
|---|
| | 437 | |
|---|
| | 438 | if(EVP_DecryptInit(&cipher->decrypt_ctx, cipher->cipher, NULL, NULL) != 1) { |
|---|
| | 439 | wi_error_set_openssl_error(); |
|---|
| | 440 | |
|---|
| | 441 | wi_release(cipher); |
|---|
| | 442 | |
|---|
| | 443 | return NULL; |
|---|
| | 444 | } |
|---|
| | 445 | |
|---|
| | 446 | _wi_cipher_configure_cipher(cipher); |
|---|
| | 447 | |
|---|
| | 448 | if(EVP_EncryptInit(&cipher->encrypt_ctx, cipher->cipher, key_buffer, iv_buffer) != 1) { |
|---|
| | 449 | wi_error_set_openssl_error(); |
|---|
| | 450 | |
|---|
| | 451 | wi_release(cipher); |
|---|
| | 452 | |
|---|
| | 453 | return NULL; |
|---|
| | 454 | } |
|---|
| | 455 | |
|---|
| | 456 | if(EVP_DecryptInit(&cipher->decrypt_ctx, cipher->cipher, key_buffer, iv_buffer) != 1) { |
|---|
| | 457 | wi_error_set_openssl_error(); |
|---|
| | 458 | |
|---|
| | 459 | wi_release(cipher); |
|---|
| | 460 | |
|---|
| | 461 | return NULL; |
|---|
| | 462 | } |
|---|
| | 463 | |
|---|
| | 464 | cipher->key = wi_data_init_with_bytes_no_copy(wi_data_alloc(), key_buffer, key_length, true); |
|---|
| | 465 | cipher->iv = (iv_length > 0) ? wi_data_init_with_bytes_no_copy(wi_data_alloc(), iv_buffer, iv_length, true) : NULL; |
|---|
| | 466 | |
|---|
| | 467 | return cipher; |
|---|
| | 468 | } |
|---|
| | 469 | |
|---|
| | 470 | |
|---|
| | 471 | |
|---|
| | 472 | static void _wi_cipher_dealloc(wi_runtime_instance_t *instance) { |
|---|
| | 473 | wi_cipher_t *cipher = instance; |
|---|
| | 474 | |
|---|
| | 475 | EVP_CIPHER_CTX_cleanup(&cipher->encrypt_ctx); |
|---|
| | 476 | EVP_CIPHER_CTX_cleanup(&cipher->decrypt_ctx); |
|---|
| | 477 | |
|---|
| | 478 | wi_release(cipher->key); |
|---|
| | 479 | wi_release(cipher->iv); |
|---|
| | 480 | } |
|---|
| | 481 | |
|---|
| | 482 | |
|---|
| | 483 | |
|---|
| | 484 | static wi_string_t * _wi_cipher_description(wi_runtime_instance_t *instance) { |
|---|
| | 485 | wi_cipher_t *cipher = instance; |
|---|
| 488 | | wi_runtime_class_name(p7_cipher), |
|---|
| 489 | | p7_cipher, |
|---|
| 490 | | wi_p7_cipher_name(p7_cipher), |
|---|
| 491 | | wi_p7_cipher_bits(p7_cipher), |
|---|
| 492 | | wi_p7_cipher_iv(p7_cipher), |
|---|
| 493 | | wi_p7_cipher_key(p7_cipher)); |
|---|
| 494 | | } |
|---|
| 495 | | |
|---|
| 496 | | |
|---|
| 497 | | |
|---|
| 498 | | #pragma mark - |
|---|
| 499 | | |
|---|
| 500 | | static const EVP_CIPHER * _wi_p7_cipher_cipher(wi_p7_cipher_t *p7_cipher) { |
|---|
| 501 | | switch(p7_cipher->type) { |
|---|
| 502 | | case WI_P7_CIPHER_AES_256: return EVP_aes_256_cbc(); |
|---|
| 503 | | case WI_P7_CIPHER_BF_128: return EVP_bf_cbc(); |
|---|
| | 488 | wi_runtime_class_name(cipher), |
|---|
| | 489 | cipher, |
|---|
| | 490 | wi_cipher_name(cipher), |
|---|
| | 491 | wi_cipher_bits(cipher), |
|---|
| | 492 | wi_cipher_iv(cipher), |
|---|
| | 493 | wi_cipher_key(cipher)); |
|---|
| | 494 | } |
|---|
| | 495 | |
|---|
| | 496 | |
|---|
| | 497 | |
|---|
| | 498 | #pragma mark - |
|---|
| | 499 | |
|---|
| | 500 | static const EVP_CIPHER * _wi_cipher_cipher(wi_cipher_t *cipher) { |
|---|
| | 501 | switch(cipher->type) { |
|---|
| | 502 | case WI_CIPHER_AES_256: return EVP_aes_256_cbc(); |
|---|
| | 503 | case WI_CIPHER_BF_128: return EVP_bf_cbc(); |
|---|
| 511 | | static void _wi_p7_cipher_configure_cipher(wi_p7_cipher_t *p7_cipher) { |
|---|
| 512 | | if(p7_cipher->type == WI_P7_CIPHER_BF_128) { |
|---|
| 513 | | EVP_CIPHER_CTX_set_key_length(&p7_cipher->encrypt_ctx, 16); |
|---|
| 514 | | EVP_CIPHER_CTX_set_key_length(&p7_cipher->decrypt_ctx, 16); |
|---|
| 515 | | } |
|---|
| 516 | | } |
|---|
| 517 | | |
|---|
| 518 | | |
|---|
| 519 | | |
|---|
| 520 | | #pragma mark - |
|---|
| 521 | | |
|---|
| 522 | | wi_data_t * wi_p7_cipher_key(wi_p7_cipher_t *p7_cipher) { |
|---|
| 523 | | return p7_cipher->key; |
|---|
| 524 | | } |
|---|
| 525 | | |
|---|
| 526 | | |
|---|
| 527 | | |
|---|
| 528 | | wi_data_t * wi_p7_cipher_iv(wi_p7_cipher_t *p7_cipher) { |
|---|
| 529 | | return p7_cipher->iv; |
|---|
| 530 | | } |
|---|
| 531 | | |
|---|
| 532 | | |
|---|
| 533 | | |
|---|
| 534 | | #pragma mark - |
|---|
| 535 | | |
|---|
| 536 | | wi_p7_cipher_type_t wi_p7_cipher_type(wi_p7_cipher_t *p7_cipher) { |
|---|
| 537 | | return p7_cipher->type; |
|---|
| 538 | | } |
|---|
| 539 | | |
|---|
| 540 | | |
|---|
| 541 | | |
|---|
| 542 | | wi_string_t * wi_p7_cipher_name(wi_p7_cipher_t *p7_cipher) { |
|---|
| 543 | | switch(p7_cipher->type) { |
|---|
| 544 | | case WI_P7_CIPHER_AES_256: return WI_STR("AES"); |
|---|
| 545 | | case WI_P7_CIPHER_BF_128: return WI_STR("Blowfish"); |
|---|
| | 511 | static void _wi_cipher_configure_cipher(wi_cipher_t *cipher) { |
|---|
| | 512 | if(cipher->type == WI_CIPHER_BF_128) { |
|---|
| | 513 | EVP_CIPHER_CTX_set_key_length(&cipher->encrypt_ctx, 16); |
|---|
| | 514 | EVP_CIPHER_CTX_set_key_length(&cipher->decrypt_ctx, 16); |
|---|
| | 515 | } |
|---|
| | 516 | } |
|---|
| | 517 | |
|---|
| | 518 | |
|---|
| | 519 | |
|---|
| | 520 | #pragma mark - |
|---|
| | 521 | |
|---|
| | 522 | wi_data_t * wi_cipher_key(wi_cipher_t *cipher) { |
|---|
| | 523 | return cipher->key; |
|---|
| | 524 | } |
|---|
| | 525 | |
|---|
| | 526 | |
|---|
| | 527 | |
|---|
| | 528 | wi_data_t * wi_cipher_iv(wi_cipher_t *cipher) { |
|---|
| | 529 | return cipher->iv; |
|---|
| | 530 | } |
|---|
| | 531 | |
|---|
| | 532 | |
|---|
| | 533 | |
|---|
| | 534 | #pragma mark - |
|---|
| | 535 | |
|---|
| | 536 | wi_cipher_type_t wi_cipher_type(wi_cipher_t *cipher) { |
|---|
| | 537 | return cipher->type; |
|---|
| | 538 | } |
|---|
| | 539 | |
|---|
| | 540 | |
|---|
| | 541 | |
|---|
| | 542 | wi_string_t * wi_cipher_name(wi_cipher_t *cipher) { |
|---|
| | 543 | switch(cipher->type) { |
|---|
| | 544 | case WI_CIPHER_AES_256: return WI_STR("AES"); |
|---|
| | 545 | case WI_CIPHER_BF_128: return WI_STR("Blowfish"); |
|---|