<commit_before>/*
* Copyright (c) 2015 Cossack Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <soter/error.h>
#include <soter/soter.h>
#include <soter/soter_ec_key.h>
#include "soter_engine.h"
#include "soter_ecdsa_common.h"
#include <openssl/evp.h>
#include <openssl/ecdsa.h>

soter_status_t soter_verify_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)
{
  if(ctx->md_ctx){
    EVP_MD_CTX_destroy(ctx->md_ctx);
    ctx->md_ctx=NULL;
  }
  if(ctx->pkey_ctx){
    EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pkey_ctx);
    EVP_PKEY_CTX_free(ctx->pkey_ctx);
    ctx->pkey_ctx=NULL;
    EVP_PKEY_free(pkey);
  }
  /* pkey_ctx init */
  EVP_PKEY *pkey;
  pkey = EVP_PKEY_new();
  if (!pkey){
    return SOTER_NO_MEMORY;
  }
  if (!EVP_PKEY_set_type(pkey, EVP_PKEY_EC)){
    EVP_PKEY_free(pkey);
    return SOTER_FAIL;
  }
  ctx->pkey_ctx = EVP_PKEY_CTX_new(pkey, NULL);
  if (!(ctx->pkey_ctx)){
    EVP_PKEY_free(pkey);
    return SOTER_FAIL;
  }

  /* TODO: Review needed */
  if ((private_key) && (private_key_length)) {
	  if(soter_ec_import_key(pkey, private_key, private_key_length)!=SOTER_SUCCESS){
		EVP_PKEY_free(pkey);
		return SOTER_FAIL;
	  }
  }

  if ((public_key) && (public_key_length)) {
	  if(soter_ec_import_key(pkey, public_key, public_key_length)!=SOTER_SUCCESS){
		EVP_PKEY_free(pkey);
		return SOTER_FAIL;
	  }
  }

  /*md_ctx init*/
//  if(!ctx->md_ctx){
    ctx->md_ctx = EVP_MD_CTX_create();
    if(!(ctx->md_ctx)){
	EVP_PKEY_CTX_free(ctx->pkey_ctx);
        return SOTER_NO_MEMORY;
    }
//  }else{
//    EVP_MD_CTX_cleanup(ctx->md_ctx);
//  }
  if(!EVP_DigestVerifyInit(ctx->md_ctx, NULL/*&(ctx->pkey_ctx)*/, EVP_sha256(), NULL, pkey)){
    EVP_PKEY_CTX_free(ctx->pkey_ctx);
    return SOTER_FAIL;
  }
  return SOTER_SUCCESS;
}<commit_msg>update memory management<commit_after>/*
* Copyright (c) 2015 Cossack Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <soter/error.h>
#include <soter/soter.h>
#include <soter/soter_ec_key.h>
#include "soter_engine.h"
#include "soter_ecdsa_common.h"
#include <openssl/evp.h>
#include <openssl/ecdsa.h>

soter_status_t soter_verify_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)
{
  if(ctx->md_ctx){
    EVP_MD_CTX_destroy(ctx->md_ctx);
    ctx->md_ctx=NULL;
  }
  if(ctx->pkey_ctx){
    EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pkey_ctx);
    EVP_PKEY_CTX_free(ctx->pkey_ctx);
    ctx->pkey_ctx=NULL;
    EVP_PKEY_free(pkey);
  }
  /* pkey_ctx init */
  EVP_PKEY *pkey;
  pkey = EVP_PKEY_new();
  if (!pkey){
    return SOTER_NO_MEMORY;
  }
  if (!EVP_PKEY_set_type(pkey, EVP_PKEY_EC)){
    EVP_PKEY_free(pkey);
    return SOTER_FAIL;
  }
  ctx->pkey_ctx = EVP_PKEY_CTX_new(pkey, NULL);
  if (!(ctx->pkey_ctx)){
    EVP_PKEY_free(pkey);
    return SOTER_FAIL;
  }

  /* TODO: Review needed */
  if ((private_key) && (private_key_length)) {
	  if(soter_ec_import_key(pkey, private_key, private_key_length)!=SOTER_SUCCESS){
		EVP_PKEY_CTX_free(ctx->pkey_ctx);
    		EVP_PKEY_free(pkey);
		return SOTER_FAIL;
	  }
  }

  if ((public_key) && (public_key_length)) {
	  if(soter_ec_import_key(pkey, public_key, public_key_length)!=SOTER_SUCCESS){
		EVP_PKEY_CTX_free(ctx->pkey_ctx);
		EVP_PKEY_free(pkey);
		return SOTER_FAIL;
	  }
  }

  /*md_ctx init*/
//  if(!ctx->md_ctx){
    ctx->md_ctx = EVP_MD_CTX_create();
    if(!(ctx->md_ctx)){
	EVP_PKEY_CTX_free(ctx->pkey_ctx);
	EVP_PKEY_free(pkey);
        return SOTER_NO_MEMORY;
    }
//  }else{
//    EVP_MD_CTX_cleanup(ctx->md_ctx);
//  }
  if(!EVP_DigestVerifyInit(ctx->md_ctx, NULL/*&(ctx->pkey_ctx)*/, EVP_sha256(), NULL, pkey)){
    EVP_PKEY_CTX_free(ctx->pkey_ctx);
    EVP_PKEY_free(pkey);
    return SOTER_FAIL;
  }
  return SOTER_SUCCESS;
}
{'fraction_non_alphanumeric': 0.12875710804224208, 'fraction_numerical': 0.005280259951259139, 'mean_word_length': 4.43598233995585, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6525676', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1997, 'n_words': 386}