When I write the code SHA_CTX shash to SHA1_Final(hash, &shash), I have an error of Segmentation Fault, or in another case I have return value of 255. I don't understand why happen. This is a simple example.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <openssl/sha.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/obj_mac.h>
int main() {
BIGNUM *x=BN_new();
BIGNUM *y=BN_new();
BIGNUM *n=BN_new();
BIGNUM *e=BN_new();
EC_POINT *P;
BN_CTX *ctx;//Buffer
const EC_POINT *G; //Generator Point
unsigned char hash[SHA_DIGEST_LENGTH];
SHA_CTX shash;
SHA1_Init(&shash);
SHA1_Update(&shash, "abc", 3);
SHA1_Final(hash, &shash);
const EC_GROUP *curve = EC_GROUP_new_by_curve_name(NID_secp224r1);
P=EC_POINT_new(curve);
G=EC_GROUP_get0_generator(curve);
EC_GROUP_get_order(curve,n,ctx);
BN_rand_range(e,n);
EC_POINT_mul(curve,P,e,NULL,NULL,ctx);
if (!EC_POINT_is_on_curve(curve,P,ctx)) return -1;
if (!EC_POINT_get_affine_coordinates_GFp(curve, P, x, y, ctx)) return -1;
for(int i=0;i<SHA_DIGEST_LENGTH;i++)
{
printf("%02x",hash[i]);
}
return 0;
}