Changeset 4645

Show
Ignore:
Timestamp:
02/17/07 14:54:46 (2 years ago)
Author:
morris
Message:

Make crypto library non-P7-specific

Move stuff from wi-p7-base.h to wi-base.h

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libwired/trunk/libwired/base/wi-base.c

    r4623 r4645  
    5757        wi_address_register(); 
    5858        wi_array_register(); 
     59         
     60#ifdef WI_CRYPTO 
     61        wi_crypto_register(); 
     62#endif 
     63         
    5964        wi_data_register(); 
    6065        wi_date_register(); 
     
    7075         
    7176#ifdef WI_P7 
    72         wi_p7_crypto_register(); 
    7377        wi_p7_message_register(); 
    7478        wi_p7_socket_register(); 
     
    108112 
    109113        wi_address_initialize(); 
     114 
     115#ifdef WI_CRYPTO 
     116        wi_crypto_initialize(); 
     117#endif 
     118         
    110119        wi_data_initialize(); 
    111120        wi_date_initialize(); 
     
    121130         
    122131#ifdef WI_P7 
    123         wi_p7_crypto_initialize(); 
    124132        wi_p7_message_initialize(); 
    125133        wi_p7_socket_initialize(); 
  • libwired/trunk/libwired/base/wi-base.h

    r4635 r4645  
    195195typedef struct _wi_string                       wi_string_t; 
    196196 
     197typedef struct _wi_p7_message           wi_p7_message_t; 
     198typedef struct _wi_p7_socket            wi_p7_socket_t; 
     199typedef struct _wi_p7_spec                      wi_p7_spec_t; 
     200 
    197201 
    198202WI_EXPORT void                                          wi_initialize(void); 
  • libwired/trunk/libwired/base/wi-private.h

    r4623 r4645  
    4545WI_EXPORT void                                  wi_address_register(void); 
    4646WI_EXPORT void                                  wi_array_register(void); 
     47WI_EXPORT void                                  wi_crypto_register(void); 
    4748WI_EXPORT void                                  wi_data_register(void); 
    4849WI_EXPORT void                                  wi_date_register(void); 
     
    5556WI_EXPORT void                                  wi_log_register(void); 
    5657WI_EXPORT void                                  wi_number_register(void); 
    57 WI_EXPORT void                                  wi_p7_crypto_register(void); 
    5858WI_EXPORT void                                  wi_p7_message_register(void); 
    5959WI_EXPORT void                                  wi_p7_socket_register(void); 
     
    7777WI_EXPORT void                                  wi_address_initialize(void); 
    7878WI_EXPORT void                                  wi_array_initialize(void); 
     79WI_EXPORT void                                  wi_crypto_initialize(void); 
    7980WI_EXPORT void                                  wi_data_initialize(void); 
    8081WI_EXPORT void                                  wi_date_initialize(void); 
     
    8788WI_EXPORT void                                  wi_log_initialize(void); 
    8889WI_EXPORT void                                  wi_number_initialize(void); 
    89 WI_EXPORT void                                  wi_p7_crypto_initialize(void); 
    9090WI_EXPORT void                                  wi_p7_message_initialize(void); 
    9191WI_EXPORT void                                  wi_p7_socket_initialize(void); 
  • libwired/trunk/libwired/misc/wi-crypto.c

    r4644 r4645  
    2929#include "config.h" 
    3030 
    31 #ifdef WI_P7 
     31#ifdef WI_CRYPTO 
    3232 
    3333#include <wired/wi-data.h> 
    34 #include <wired/wi-p7-crypto.h> 
     34#include <wired/wi-crypto.h> 
    3535#include <wired/wi-private.h> 
    3636#include <wired/wi-string.h> 
     
    4242#include <openssl/x509.h> 
    4343 
    44 struct _wi_p7_rsa { 
     44struct _wi_rsa { 
    4545        wi_runtime_base_t                                       base; 
    4646         
     
    4949}; 
    5050 
    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, 
     51static void                                                             _wi_rsa_dealloc(wi_runtime_instance_t *); 
     52static wi_string_t *                                    _wi_rsa_description(wi_runtime_instance_t *); 
     53 
     54static wi_data_t *                                              _wi_rsa_public_key(wi_rsa_t *); 
     55 
     56static wi_runtime_id_t                                  _wi_rsa_runtime_id = WI_RUNTIME_ID_NULL; 
     57static wi_runtime_class_t                               _wi_rsa_runtime_class = { 
     58        "wi_rsa_t", 
     59        _wi_rsa_dealloc, 
    6060        NULL, 
    6161        NULL, 
    62         _wi_p7_rsa_description, 
     62        _wi_rsa_description, 
    6363        NULL 
    6464}; 
     
    6666 
    6767 
    68 struct _wi_p7_cipher { 
     68struct _wi_cipher { 
    6969        wi_runtime_base_t                                       base; 
    7070         
    71         wi_p7_cipher_type_t                                   type; 
     71        wi_cipher_type_t                                      type; 
    7272        const EVP_CIPHER                                        *cipher; 
    7373        EVP_CIPHER_CTX                                          encrypt_ctx; 
     
    7777}; 
    7878 
    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, 
     79static void                                                             _wi_cipher_dealloc(wi_runtime_instance_t *); 
     80static wi_string_t *                                    _wi_cipher_description(wi_runtime_instance_t *); 
     81 
     82static const EVP_CIPHER *                               _wi_cipher_cipher(wi_cipher_t *); 
     83static void                                                             _wi_cipher_configure_cipher(wi_cipher_t *); 
     84 
     85static wi_runtime_id_t                                  _wi_cipher_runtime_id = WI_RUNTIME_ID_NULL; 
     86static wi_runtime_class_t                               _wi_cipher_runtime_class = { 
     87        "wi_cipher_t", 
     88        _wi_cipher_dealloc, 
    8989        NULL, 
    9090        NULL, 
    91         _wi_p7_cipher_description, 
     91        _wi_cipher_description, 
    9292        NULL 
    9393}; 
     
    9595 
    9696 
    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) { 
     97void 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 
     104void wi_crypto_initialize(void) { 
     105} 
     106 
     107 
     108 
     109#pragma mark - 
     110 
     111wi_runtime_id_t wi_rsa_runtime_id(void) { 
     112        return _wi_rsa_runtime_id; 
     113} 
     114 
     115 
     116 
     117#pragma mark - 
     118 
     119wi_rsa_t * wi_rsa_alloc(void) { 
     120        return wi_runtime_create_instance(_wi_rsa_runtime_id, sizeof(wi_rsa_t)); 
     121} 
     122 
     123 
     124 
     125wi_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 
     147wi_rsa_t * wi_rsa_init_with_pem_file(wi_rsa_t *rsa, wi_string_t *path) { 
    148148        FILE            *fp; 
    149149         
     
    153153                wi_error_set_errno(errno); 
    154154                 
    155                 wi_release(p7_rsa); 
    156                  
    157                 return NULL; 
    158         } 
    159          
    160         p7_rsa->rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL); 
     155                wi_release(rsa); 
     156                 
     157                return NULL; 
     158        } 
     159         
     160        rsa->rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL); 
    161161         
    162162        fclose(fp); 
    163163         
    164         if(!p7_rsa->rsa) { 
    165                 wi_error_set_openssl_error(); 
    166                  
    167                 wi_release(p7_rsa); 
    168                  
    169                 return NULL; 
    170         } 
    171          
    172         p7_rsa->public_key = wi_retain(_wi_p7_rsa_public_key(p7_rsa)); 
    173          
    174         if(!p7_rsa->public_key) { 
    175                 wi_release(p7_rsa); 
    176  
    177                 return NULL; 
    178         } 
    179          
    180         return p7_rsa; 
    181 } 
    182  
    183  
    184  
    185 wi_p7_rsa_t * wi_p7_rsa_init_with_public_key(wi_p7_rsa_t *p7_rsa, wi_data_t *data) { 
     164        if(!rsa->rsa) { 
     165                wi_error_set_openssl_error(); 
     166                 
     167                wi_release(rsa); 
     168                 
     169                return NULL; 
     170        } 
     171         
     172        rsa->public_key = wi_retain(_wi_rsa_public_key(rsa)); 
     173         
     174        if(!rsa->public_key) { 
     175                wi_release(rsa); 
     176 
     177                return NULL; 
     178        } 
     179         
     180        return rsa; 
     181} 
     182 
     183 
     184 
     185wi_rsa_t * wi_rsa_init_with_public_key(wi_rsa_t *rsa, wi_data_t *data) { 
    186186        unsigned char   *buffer; 
    187187        long                    length; 
     
    190190        length = wi_data_length(data); 
    191191         
    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 
     209static 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 
     218static wi_string_t * _wi_rsa_description(wi_runtime_instance_t *instance) { 
     219        wi_rsa_t               *rsa = instance; 
    220220         
    221221        return wi_string_with_format(WI_STR("<%@ %p>{key = %p, bits = %u}"), 
    222         wi_runtime_class_name(p7_rsa), 
    223                 p7_rsa, 
    224                 p7_rsa->rsa, 
    225                 RSA_size(p7_rsa->rsa) * 8); 
    226 } 
    227  
    228  
    229  
    230 #pragma mark - 
    231  
    232 static wi_data_t * _wi_p7_rsa_public_key(wi_p7_rsa_t *p7_rsa) { 
     222        wi_runtime_class_name(rsa), 
     223                rsa, 
     224                rsa->rsa, 
     225                RSA_size(rsa->rsa) * 8); 
     226} 
     227 
     228 
     229 
     230#pragma mark - 
     231 
     232static wi_data_t * _wi_rsa_public_key(wi_rsa_t *rsa) { 
    233233        wi_data_t               *data; 
    234234        unsigned char   *buffer; 
     
    236236 
    237237        buffer = NULL; 
    238         length = i2d_RSA_PUBKEY(p7_rsa->rsa, &buffer); 
     238        length = i2d_RSA_PUBKEY(rsa->rsa, &buffer); 
    239239         
    240240        if(length <= 0) { 
     
    255255#pragma mark - 
    256256 
    257 wi_data_t * wi_p7_rsa_public_key(wi_p7_rsa_t *p7_rsa) { 
    258         return p7_rsa->public_key; 
    259 } 
    260  
    261  
    262  
    263 #pragma mark - 
    264  
    265 wi_data_t * wi_p7_rsa_encrypt(wi_p7_rsa_t *p7_rsa, wi_data_t *decrypted_data) { 
     257wi_data_t * wi_rsa_public_key(wi_rsa_t *rsa) { 
     258        return rsa->public_key; 
     259} 
     260 
     261 
     262 
     263#pragma mark - 
     264 
     265wi_data_t * wi_rsa_encrypt(wi_rsa_t *rsa, wi_data_t *decrypted_data) { 
    266266        const void              *decrypted_buffer; 
    267267        void                    *encrypted_buffer; 
     
    271271        decrypted_length = wi_data_length(decrypted_data); 
    272272         
    273         if(!wi_p7_rsa_encrypt_bytes(p7_rsa, decrypted_buffer, decrypted_length, &encrypted_buffer, &encrypted_length)) 
     273        if(!wi_rsa_encrypt_bytes(rsa, decrypted_buffer, decrypted_length, &encrypted_buffer, &encrypted_length)) 
    274274                return NULL; 
    275275         
     
    279279 
    280280 
    281 wi_boolean_t wi_p7_rsa_encrypt_bytes(wi_p7_rsa_t *p7_rsa, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
     281wi_boolean_t wi_rsa_encrypt_bytes(wi_rsa_t *rsa, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
    282282        void            *encrypted_buffer; 
    283283        int32_t         encrypted_length; 
    284284 
    285         encrypted_buffer = wi_malloc(RSA_size(p7_rsa->rsa)); 
    286         encrypted_length = RSA_public_encrypt(decrypted_length, decrypted_buffer, encrypted_buffer, p7_rsa->rsa, RSA_PKCS1_PADDING); 
     285        encrypted_buffer = wi_malloc(RSA_size(rsa->rsa)); 
     286        encrypted_length = RSA_public_encrypt(decrypted_length, decrypted_buffer, encrypted_buffer, rsa->rsa, RSA_PKCS1_PADDING); 
    287287         
    288288        if(encrypted_length == -1) { 
     
    302302 
    303303 
    304 wi_data_t * wi_p7_rsa_decrypt(wi_p7_rsa_t *p7_rsa, wi_data_t *encrypted_data) { 
     304wi_data_t * wi_rsa_decrypt(wi_rsa_t *rsa, wi_data_t *encrypted_data) { 
    305305        const void              *encrypted_buffer; 
    306306        void                    *decrypted_buffer; 
     
    310310        encrypted_length = wi_data_length(encrypted_data); 
    311311         
    312         if(!wi_p7_rsa_decrypt_bytes(p7_rsa, encrypted_buffer, encrypted_length, &decrypted_buffer, &decrypted_length)) 
     312        if(!wi_rsa_decrypt_bytes(rsa, encrypted_buffer, encrypted_length, &decrypted_buffer, &decrypted_length)) 
    313313                return NULL; 
    314314         
     
    318318 
    319319 
    320 wi_boolean_t wi_p7_rsa_decrypt_bytes(wi_p7_rsa_t *p7_rsa, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
     320wi_boolean_t wi_rsa_decrypt_bytes(wi_rsa_t *rsa, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
    321321        void            *decrypted_buffer; 
    322322        int32_t         decrypted_length; 
    323323         
    324         decrypted_buffer = wi_malloc(RSA_size(p7_rsa->rsa)); 
    325         decrypted_length = RSA_private_decrypt(encrypted_length, encrypted_buffer, decrypted_buffer, p7_rsa->rsa, RSA_PKCS1_PADDING); 
     324        decrypted_buffer = wi_malloc(RSA_size(rsa->rsa)); 
     325        decrypted_length = RSA_private_decrypt(encrypted_length, encrypted_buffer, decrypted_buffer, rsa->rsa, RSA_PKCS1_PADDING); 
    326326 
    327327        if(decrypted_length == -1) { 
     
    343343#pragma mark - 
    344344 
    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) { 
     345wi_runtime_id_t wi_cipher_runtime_id(void) { 
     346        return _wi_cipher_runtime_id; 
     347} 
     348 
     349 
     350 
     351#pragma mark - 
     352 
     353wi_cipher_t * wi_cipher_alloc(void) { 
     354        return wi_runtime_create_instance(_wi_cipher_runtime_id, sizeof(wi_cipher_t)); 
     355} 
     356 
     357 
     358 
     359wi_cipher_t * wi_cipher_init_with_key(wi_cipher_t *cipher, wi_cipher_type_t type, wi_data_t *key, wi_data_t *iv) { 
    360360        unsigned char           *key_buffer, *iv_buffer; 
    361361         
    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 
     410wi_cipher_t * wi_cipher_init_with_random_key(wi_cipher_t *cipher, wi_cipher_type_t type) { 
    411411        unsigned char           *key_buffer, *iv_buffer; 
    412412        int                                     key_length, iv_length; 
    413413         
    414         p7_cipher->type               = type; 
    415         p7_cipher->cipher      = _wi_p7_cipher_cipher(p7_cipher); 
    416          
    417         key_length                     = EVP_MAX_KEY_LENGTH; 
    418         key_buffer                     = wi_malloc(key_length); 
    419         iv_length                      = EVP_CIPHER_iv_length(p7_cipher->cipher); 
    420         iv_buffer                      = (iv_length > 0) ? wi_malloc(iv_length) : NULL; 
     414        cipher->type  = type; 
     415        cipher->cipher = _wi_cipher_cipher(cipher); 
     416         
     417        key_length              = EVP_MAX_KEY_LENGTH; 
     418        key_buffer              = wi_malloc(key_length); 
     419        iv_length               = EVP_CIPHER_iv_length(cipher->cipher); 
     420        iv_buffer               = (iv_length > 0) ? wi_malloc(iv_length) : NULL; 
    421421         
    422422        if(RAND_bytes(key_buffer, key_length) <= 0) { 
    423423                wi_error_set_openssl_error(); 
    424424                 
    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 
     472static 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 
     484static wi_string_t * _wi_cipher_description(wi_runtime_instance_t *instance) { 
     485        wi_cipher_t            *cipher = instance; 
    486486         
    487487        return wi_string_with_format(WI_STR("<%@ %p>{name = %@, bits = %u, iv = %@, key = %@}"), 
    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 
     500static 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(); 
    504504        } 
    505505         
     
    509509 
    510510 
    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"); 
     511static 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 
     522wi_data_t * wi_cipher_key(wi_cipher_t *cipher) { 
     523        return cipher->key; 
     524} 
     525 
     526 
     527 
     528wi_data_t * wi_cipher_iv(wi_cipher_t *cipher) { 
     529        return cipher->iv; 
     530} 
     531 
     532 
     533 
     534#pragma mark - 
     535 
     536wi_cipher_type_t wi_cipher_type(wi_cipher_t *cipher) { 
     537        return cipher->type; 
     538} 
     539 
     540 
     541 
     542wi_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"); 
    546546        } 
    547547         
     
    551551 
    552552 
    553 wi_uinteger_t wi_p7_cipher_bits(wi_p7_cipher_t *p7_cipher) { 
    554         return EVP_CIPHER_key_length(&p7_cipher->encrypt_ctx) * 8; 
    555 } 
    556  
    557  
    558  
    559 #pragma mark - 
    560  
    561 wi_data_t * wi_p7_cipher_encrypt(wi_p7_cipher_t *p7_cipher, wi_data_t *decrypted_data) { 
     553wi_uinteger_t wi_cipher_bits(wi_cipher_t *cipher) { 
     554        return EVP_CIPHER_key_length(&cipher->encrypt_ctx) * 8; 
     555} 
     556 
     557 
     558 
     559#pragma mark - 
     560 
     561wi_data_t * wi_cipher_encrypt(wi_cipher_t *cipher, wi_data_t *decrypted_data) { 
    562562        const void              *decrypted_buffer; 
    563563        void                    *encrypted_buffer; 
     
    567567        decrypted_length = wi_data_length(decrypted_data); 
    568568         
    569         if(!wi_p7_cipher_encrypt_bytes(p7_cipher, decrypted_buffer, decrypted_length, &encrypted_buffer, &encrypted_length)) 
     569        if(!wi_cipher_encrypt_bytes(cipher, decrypted_buffer, decrypted_length, &encrypted_buffer, &encrypted_length)) 
    570570                return NULL; 
    571571         
     
    575575 
    576576 
    577 wi_boolean_t wi_p7_cipher_encrypt_bytes(wi_p7_cipher_t *p7_cipher, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
     577wi_boolean_t wi_cipher_encrypt_bytes(wi_cipher_t *cipher, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
    578578        void            *encrypted_buffer; 
    579579        int                     encrypted_length, padded_length; 
    580580         
    581         encrypted_buffer = wi_malloc(decrypted_length + EVP_CIPHER_block_size(p7_cipher->cipher)); 
    582  
    583         if(EVP_EncryptUpdate(&p7_cipher->encrypt_ctx, encrypted_buffer, &encrypted_length, decrypted_buffer, decrypted_length) != 1) { 
     581        encrypted_buffer = wi_malloc(decrypted_length + EVP_CIPHER_block_size(cipher->cipher)); 
     582 
     583        if(EVP_EncryptUpdate(&cipher->encrypt_ctx, encrypted_buffer, &encrypted_length, decrypted_buffer, decrypted_length) != 1) { 
    584584                wi_error_set_openssl_error(); 
    585585                 
     
    589589        } 
    590590         
    591         if(EVP_EncryptFinal(&p7_cipher->encrypt_ctx, encrypted_buffer + encrypted_length, &padded_length) != 1) { 
     591        if(EVP_EncryptFinal(&cipher->encrypt_ctx, encrypted_buffer + encrypted_length, &padded_length) != 1) { 
    592592                wi_error_set_openssl_error(); 
    593593                 
     
    605605 
    606606 
    607 wi_data_t * wi_p7_cipher_decrypt(wi_p7_cipher_t *p7_cipher, wi_data_t *encrypted_data) { 
     607wi_data_t * wi_cipher_decrypt(wi_cipher_t *cipher, wi_data_t *encrypted_data) { 
    608608        const void              *encrypted_buffer; 
    609609        void                    *decrypted_buffer; 
     
    613613        encrypted_length = wi_data_length(encrypted_data); 
    614614         
    615         if(!wi_p7_cipher_decrypt_bytes(p7_cipher, encrypted_buffer, enc