<commit_before>/**
 * @file
 *
 * (c) CossackLabs
 */

#include <common/error.h>
#include <soter/soter.h>
#include <soter/soter_ec_key.h>
#include "soter_openssl.h"
#include "soter_ecdsa_common.h"
#include <openssl/evp.h>
#include <openssl/ecdsa.h>
#include <openssl/err.h>


soter_status_t soter_sign_init_ecdsa_none_pkcs8(soter_sign_ctx_t* ctx, const void* private_key, const size_t private_key_length, const void* public_key, const size_t public_key_length)
{
  /* pkey_ctx init */
  EVP_PKEY *pkey;
  pkey = EVP_PKEY_new();
  if (!pkey){
    return HERMES_NO_MEMORY;
  }
  if (!EVP_PKEY_set_type(pkey, EVP_PKEY_EC)){
    EVP_PKEY_free(pkey);
    return HERMES_FAIL;
  }
  ctx->pkey_ctx = EVP_PKEY_CTX_new(pkey, NULL);
  if (!(ctx->pkey_ctx)){
    EVP_PKEY_free(pkey);
    return HERMES_FAIL;
  }
  if (!EVP_PKEY_paramgen_init(ctx->pkey_ctx)){
    EVP_PKEY_free(pkey);
    return HERMES_FAIL;
  }
  if (!EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx->pkey_ctx, NID_X9_62_prime256v1)){
    EVP_PKEY_free(pkey);
    return HERMES_FAIL;
  }
  if (!EVP_PKEY_paramgen(ctx->pkey_ctx, &pkey)){
    EVP_PKEY_free(pkey);
    return HERMES_FAIL;
  }
  if((!private_key)&&(!public_key)){
    if(soter_ec_gen_key(ctx->pkey_ctx)!=HERMES_SUCCESS){
      EVP_PKEY_free(pkey);
      return HERMES_FAIL;
    }
  }else{
    if(private_key!=NULL){
      if(soter_ec_import_key(pkey, private_key, private_key_length)!=HERMES_SUCCESS){
	EVP_PKEY_free(pkey);
	return HERMES_FAIL;
      }
    }
    if(public_key!=NULL){
      if(soter_ec_import_key(pkey, public_key, public_key_length)!=HERMES_SUCCESS){
	EVP_PKEY_free(pkey);
	return HERMES_FAIL;
      }
    }
  }

  /*md_ctx init*/
  ctx->md_ctx = EVP_MD_CTX_create();
  if(!(ctx->md_ctx)){
    EVP_PKEY_CTX_free(ctx->pkey_ctx);
    return HERMES_NO_MEMORY;
  }
  if(EVP_DigestSignInit(ctx->md_ctx, &(ctx->pkey_ctx), EVP_sha256(), NULL, pkey)!=1){
    EVP_PKEY_CTX_free(ctx->pkey_ctx);
    return HERMES_FAIL;
  }
  return HERMES_SUCCESS;
}

soter_status_t soter_sign_export_key_ecdsa_none_pkcs8(soter_sign_ctx_t* ctx, void* key, size_t* key_length, bool isprivate)
{
    return soter_ec_export_key(ctx,key,key_length, isprivate);
}

soter_status_t soter_sign_update_ecdsa_none_pkcs8(soter_sign_ctx_t* ctx, const void* data, const size_t data_length)
{
  if(EVP_DigestSignUpdate(ctx->md_ctx, data, data_length)!=1){
    return HERMES_FAIL;
  }
  return HERMES_SUCCESS;
}

soter_status_t soter_sign_final_ecdsa_none_pkcs8(soter_sign_ctx_t* ctx, void* signature, size_t *signature_length)
{
  EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pkey_ctx);
  if (!pkey && EVP_PKEY_type(pkey->type)!=EVP_PKEY_EC){
    return HERMES_INVALID_PARAMETER;
  } /* TODO: need review */
  if(!signature || (*signature_length)<EVP_PKEY_size(pkey)){
    (*signature_length)=EVP_PKEY_size(pkey);
    return HERMES_BUFFER_TOO_SMALL;
  }

  int res=EVP_DigestSignFinal(ctx->md_ctx, signature, signature_length);
  if(res!=1){
    unsigned long ss=ERR_get_error();
    char error_str[1024];
    ERR_load_crypto_strings();
    ERR_error_string(ss, error_str);
    fprintf(stderr, "%s", error_str);
    return HERMES_FAIL;
  }
  return HERMES_SUCCESS;
}<commit_msg>remove some debug code<commit_after>/**
 * @file
 *
 * (c) CossackLabs
 */

#include <common/error.h>
#include <soter/soter.h>
#include <soter/soter_ec_key.h>
#include "soter_openssl.h"
#include "soter_ecdsa_common.h"
#include <openssl/evp.h>
#include <openssl/ecdsa.h>
#include <openssl/err.h>


soter_status_t soter_sign_init_ecdsa_none_pkcs8(soter_sign_ctx_t* ctx, const void* private_key, const size_t private_key_length, const void* public_key, const size_t public_key_length)
{
  /* pkey_ctx init */
  EVP_PKEY *pkey;
  pkey = EVP_PKEY_new();
  if (!pkey){
    return HERMES_NO_MEMORY;
  }
  if (!EVP_PKEY_set_type(pkey, EVP_PKEY_EC)){
    EVP_PKEY_free(pkey);
    return HERMES_FAIL;
  }
  ctx->pkey_ctx = EVP_PKEY_CTX_new(pkey, NULL);
  if (!(ctx->pkey_ctx)){
    EVP_PKEY_free(pkey);
    return HERMES_FAIL;
  }
  if (!EVP_PKEY_paramgen_init(ctx->pkey_ctx)){
    EVP_PKEY_free(pkey);
    return HERMES_FAIL;
  }
  if (!EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx->pkey_ctx, NID_X9_62_prime256v1)){
    EVP_PKEY_free(pkey);
    return HERMES_FAIL;
  }
  if (!EVP_PKEY_paramgen(ctx->pkey_ctx, &pkey)){
    EVP_PKEY_free(pkey);
    return HERMES_FAIL;
  }
  if((!private_key)&&(!public_key)){
    if(soter_ec_gen_key(ctx->pkey_ctx)!=HERMES_SUCCESS){
      EVP_PKEY_free(pkey);
      return HERMES_FAIL;
    }
  }else{
    if(private_key!=NULL){
      if(soter_ec_import_key(pkey, private_key, private_key_length)!=HERMES_SUCCESS){
	EVP_PKEY_free(pkey);
	return HERMES_FAIL;
      }
    }
    if(public_key!=NULL){
      if(soter_ec_import_key(pkey, public_key, public_key_length)!=HERMES_SUCCESS){
	EVP_PKEY_free(pkey);
	return HERMES_FAIL;
      }
    }
  }

  /*md_ctx init*/
  ctx->md_ctx = EVP_MD_CTX_create();
  if(!(ctx->md_ctx)){
    EVP_PKEY_CTX_free(ctx->pkey_ctx);
    return HERMES_NO_MEMORY;
  }
  if(EVP_DigestSignInit(ctx->md_ctx, &(ctx->pkey_ctx), EVP_sha256(), NULL, pkey)!=1){
    EVP_PKEY_CTX_free(ctx->pkey_ctx);
    return HERMES_FAIL;
  }
  return HERMES_SUCCESS;
}

soter_status_t soter_sign_export_key_ecdsa_none_pkcs8(soter_sign_ctx_t* ctx, void* key, size_t* key_length, bool isprivate)
{
    return soter_ec_export_key(ctx,key,key_length, isprivate);
}

soter_status_t soter_sign_update_ecdsa_none_pkcs8(soter_sign_ctx_t* ctx, const void* data, const size_t data_length)
{
  if(EVP_DigestSignUpdate(ctx->md_ctx, data, data_length)!=1){
    return HERMES_FAIL;
  }
  return HERMES_SUCCESS;
}

soter_status_t soter_sign_final_ecdsa_none_pkcs8(soter_sign_ctx_t* ctx, void* signature, size_t *signature_length)
{
  EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pkey_ctx);
  if (!pkey && EVP_PKEY_type(pkey->type)!=EVP_PKEY_EC){
    return HERMES_INVALID_PARAMETER;
  } /* TODO: need review */
  if(!signature || (*signature_length)<EVP_PKEY_size(pkey)){
    (*signature_length)=EVP_PKEY_size(pkey);
    return HERMES_BUFFER_TOO_SMALL;
  }

  if(EVP_DigestSignFinal(ctx->md_ctx, signature, signature_length)!=1){
    return HERMES_FAIL;
  }
  return HERMES_SUCCESS;
}
{'fraction_non_alphanumeric': 0.1279220779220779, 'fraction_numerical': 0.006493506493506494, 'mean_word_length': 5.1303482587064675, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4806902', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2707, 'n_words': 360}