Esempio n. 1
0
soter_sym_ctx_t* soter_sym_ctx_init(const uint32_t alg,
				    const void* key,
				    const size_t key_length,
				    const void* salt,
				    const size_t salt_length,
				    const void* iv,
				    const size_t iv_length,
				    bool encrypt){
  SOTER_CHECK_PARAM_(key!=NULL);
  SOTER_CHECK_PARAM_(key_length!=0);
  soter_sym_ctx_t* ctx=NULL;
  ctx=malloc(sizeof(soter_sym_ctx_t));
  SOTER_CHECK_MALLOC_(ctx);
  ctx->alg=alg;
  uint8_t key_[SOTER_SYM_MAX_KEY_LENGTH];
  size_t key_length_=(alg&SOTER_SYM_KEY_LENGTH_MASK)/8;
  EVP_CIPHER_CTX_init(&(ctx->evp_sym_ctx));
  //  if(iv!=NULL && (iv_length<SOTER_SYM_BLOCK_LENGTH(alg))){ // как проверить длину iv??
  //  return NULL;
  //}
  SOTER_IF_FAIL_(soter_withkdf(alg,key, key_length, salt, salt_length, key_, &key_length_)==SOTER_SUCCESS, soter_sym_encrypt_destroy(ctx));
  if(encrypt){
    SOTER_IF_FAIL_(EVP_EncryptInit_ex(&(ctx->evp_sym_ctx), algid_to_evp(alg), NULL, key_, iv), soter_sym_encrypt_destroy(ctx));
  } else {
    SOTER_IF_FAIL_(EVP_DecryptInit_ex(&(ctx->evp_sym_ctx), algid_to_evp(alg), NULL, key_, iv), soter_sym_encrypt_destroy(ctx));
  }
  return ctx;
}
soter_rsa_key_pair_gen_t* soter_rsa_key_pair_gen_create(const unsigned key_length){
    SOTER_CHECK_PARAM_(rsa_key_length(key_length)>0);
    soter_rsa_key_pair_gen_t *ctx = malloc(sizeof(soter_rsa_key_pair_gen_t));
    SOTER_CHECK_MALLOC_(ctx);
    SOTER_IF_FAIL_(soter_rsa_key_pair_gen_init(ctx, key_length)==SOTER_SUCCESS, free(ctx));
    return ctx;
}