Commit b78cc14b authored by filatov's avatar filatov
Browse files

security external functions based on openssl

parent 8eed389f
Loading
Loading
Loading
Loading
+86 −6
Original line number Diff line number Diff line
#include "LibItsSecurity_Functions.hh"

#include <openssl/sha.h>
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/objects.h>

#define FIELD_SIZE_256 (256/8)
#define SIGNATURE_SIZE_256 (2+FIELD_SIZE_256*2)
#define FIELD_SIZE_384 (384/8)
#define SIGNATURE_SIZE_384 (2+FIELD_SIZE_284*2)

namespace LibItsSecurity__Functions 
{
//        group signing
@@ -11,9 +21,10 @@ namespace LibItsSecurity__Functions
OCTETSTRING fx__hashWithSha256(
  const OCTETSTRING& p__toBeHashedData
) {
//RGY when implementing, look at TCC_Useful_functions/TCCSecurity_Functions
	OCTETSTRING ret_val = int2oct(0,1);
	return ret_val;

  unsigned char sha256[SHA_DIGEST_LENGTH];
  SHA256((const unsigned char*)p__toBeHashedData,p__toBeHashedData.lengthof(),sha256);
  return OCTETSTRING(SHA_DIGEST_LENGTH,sha256);
}

/*          * @desc    Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signaturee
@@ -26,8 +37,38 @@ OCTETSTRING fx__signWithEcdsaNistp256WithSha256(
  const OCTETSTRING& p__toBeSignedSecuredMessage,
  const OCTETSTRING& p__privateKey
) {
	OCTETSTRING ret_val = int2oct(0,1);
	return ret_val;

  unsigned char sha256[SHA_DIGEST_LENGTH];
  unsigned char signature[SIGNATURE_SIZE_256];
  unsigned char *r = &signature[2], *s = &signature[2+FIELD_SIZE_256];
  memset(signature, 0, sizeof(signature));
  // signature[0] = 0; // ecdsa_nistp256_with_sha256
  // signature[1] = 0; // uncompressed

  SHA256((const unsigned char*)p__toBeSignedSecuredMessage,p__toBeSignedSecuredMessage.lengthof(),sha256);

  EC_KEY * k = EC_KEY_new();

  EC_GROUP * group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  EC_KEY_set_group(k, group);
  EC_GROUP_free(group);

  BIGNUM * p = BN_new();
  BN_bin2bn((const unsigned char*)p__privateKey, p__privateKey.lengthof(), p);
  EC_KEY_set_private_key(k, p);
  BN_clear_free(p);
  
  if (0 == EC_KEY_check_key(k)){
    
    ECDSA_SIG *sig;
    sig = ECDSA_do_sign(sha256, SHA_DIGEST_LENGTH, k);
    BN_bn2bin(sig->r, r);
    BN_bn2bin(sig->s, s);
    ECDSA_SIG_free(sig);
  }
  EC_KEY_free(k);

  return OCTETSTRING(sizeof(signature), signature);
}

/*          * @desc    Verify the signature of the specified data
@@ -44,7 +85,46 @@ BOOLEAN fx__verifyWithEcdsaNistp256WithSha256(
  const OCTETSTRING& p__ecdsaNistp256PublicKeyX,
  const OCTETSTRING& p__ecdsaNistp256PublicKeyY
) {
   return TRUE;
  unsigned char sha256[SHA_DIGEST_LENGTH];
  
  SHA256((const unsigned char*)p__toBeVerifiedData,p__toBeVerifiedData.lengthof(),sha256);
  const unsigned char * r = (const unsigned char*)p__signature;
  const unsigned char * x = (const unsigned char*)p__ecdsaNistp256PublicKeyX;
  const unsigned char * y = (const unsigned char*)p__ecdsaNistp256PublicKeyY;
  int alg = 0, type = 0;
  if(p__signature.lengthof() >= SIGNATURE_SIZE_256)
    alg = *(r++);
  if(p__signature.lengthof() >= (SIGNATURE_SIZE_256-1))
    type = *(r++);

  ECDSA_SIG * sig = ECDSA_SIG_new();
  BN_bin2bn(r,    32, sig->s);
  BN_bin2bn(r+32, 32, sig->r);

  EC_KEY * k;
  EC_GROUP * group;
  EC_POINT * pnt;
  BIGNUM *bnx, *bny;

  k = EC_KEY_new();

  group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  EC_KEY_set_group(k, group);
  EC_GROUP_free(group); 

  pnt = EC_POINT_new(group);
  bnx = BN_new(); BN_bin2bn(x, p__ecdsaNistp256PublicKeyX.lengthof(), bnx);
  bny = BN_new(); BN_bin2bn(y, p__ecdsaNistp256PublicKeyY.lengthof(), bny);
  EC_POINT_set_affine_coordinates_GFp(group, pnt, bnx, bny, NULL);
  BN_clear_free(bnx); BN_clear_free(bny);
  EC_KEY_set_public_key(k, pnt);
  EC_POINT_free(pnt);

  int rc = ECDSA_do_verify(sha256, SHA_DIGEST_LENGTH, sig, k);
  EC_KEY_free(k);
  ECDSA_SIG_free(sig);

  return rc==0 ? TRUE : FALSE;
}

/*          * @desc    Produce a new public/private key pair based on Elliptic Curve Digital Signature Algorithm (ECDSA) algorithm.