bool bnConvertToString ( const IppsBigNumState *pBN, char* sBN ){
	// size of Big Number
	int size;
	Ipp32u res = ippsGetSize_BN(pBN, &size);
	if ( res != ippStsNoErr )
		std::cerr << res << std::endl;
	// extract Big Number value and convert it to the string presentation
	Ipp8u* bnValue = new Ipp8u [size*4+4];
	res = ippsGetOctString_BN(bnValue, size*4, pBN);
	if ( res != ippStsNoErr ){
		std::cerr << res << std::endl;
		return false;
	}
	// save representation
	for(int i=0; i<size*4; i++){
		static char xlat[16] = {
			'0', '1', '2', '3',
			'4', '5', '6', '7',
			'8', '9', 'A', 'B',
			'C', 'D', 'E', 'F'
		};

		sBN[2*i] = xlat[(bnValue[i]&0xF0) >> 4];
		sBN[2*i+1] = xlat[bnValue[i]&0x0F];
	}
	sBN[2*size*4] = '\0';
	delete[] bnValue;	
	return true;
}
示例#2
0
void PrintBigNum(BigNum const* big_num, char const* var_name) {
  IppStatus sts = ippStsNoErr;
  unsigned char* buf = NULL;
  int ipp_word_buf_size;
  if (!var_name) {
    var_name = "<no name>";
  }
  PRINT("%s (BigNum):\n", var_name);
  if (!big_num) {
    MAKE_INDENT();
    PRINT("<null>\n");
    return;
  }
  if (!big_num->ipp_bn) {
    MAKE_INDENT();
    PRINT("<invalid>\n");
    return;
  }
  sts = ippsGetSize_BN(big_num->ipp_bn, &ipp_word_buf_size);
  if (ippStsNoErr != sts) {
    MAKE_INDENT();
    PRINT("<invalid>\n");
    return;
  }
  do {
    buf = SAFE_ALLOC(ipp_word_buf_size * sizeof(Ipp32u));
    if (!buf) {
      MAKE_INDENT();
      PRINT("<invalid>\n");
      break;
    }
    sts = ippsGetOctString_BN((Ipp8u*)buf, ipp_word_buf_size * sizeof(Ipp32u),
                              big_num->ipp_bn);
    if (ippStsNoErr != sts) {
      MAKE_INDENT();
      PRINT("<invalid>\n");
      break;
    }
    if (0 != PrintBuf((const void*)buf, ipp_word_buf_size * sizeof(Ipp32u))) {
      MAKE_INDENT();
      PRINT("<invalid>\n");
      break;
    }
  } while (0);

  SAFE_FREE(buf);
}
std::ostream& operator<<(std::ostream& os, const IppsBigNumState* pBN){
	// size of Big Number
	int size;
	Ipp32u res = ippsGetSize_BN(pBN, &size);
	if ( res != ippStsNoErr )
		std::cerr << res << std::endl;
	// extract Big Number value and convert it to the string presentation
	Ipp8u* bnValue = new Ipp8u [size*4+4];
	res = ippsGetOctString_BN(bnValue, size*4, pBN);
	if ( res != ippStsNoErr )
		std::cerr << res << std::endl;
	// type value
	for(int i=0; i<size*4; i++)
		os<< std::setfill('0') << std::setw(2) << std::hex <<(int)bnValue[i];
	delete[] bnValue;	
	return os;
}
示例#4
0
/** Create an ECC public key based on a given ECC private key.
*
* Parameters:
*   Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
*   Input: p_att_priv_key - Input private key
*   Output: p_att_pub_key - Output public key - LITTLE ENDIAN
*
*/
sgx_status_t sgx_ecc256_calculate_pub_from_priv(const sgx_ec256_private_t *p_att_priv_key, sgx_ec256_public_t  *p_att_pub_key)
{
    if ((p_att_priv_key == NULL) || (p_att_pub_key == NULL)) {
        return SGX_ERROR_INVALID_PARAMETER;
    }

    IppsECCPState* p_ecc_state = NULL;
    sgx_status_t ret = SGX_ERROR_UNEXPECTED;
    int ctx_size = 0;
    int point_size = 0;
    IppsECCPPointState* public_key = NULL;
    IppsBigNumState*    bn_o = NULL;
    IppsBigNumState*    bn_x = NULL;
    IppsBigNumState*    bn_y = NULL;
    sgx_ec256_private_t att_priv_key_be;
    uint8_t* p_temp;
    int size = 0;
    IppsBigNumSGN sgn;

    do {
        //get the size of the IppsECCPState context
        //
        if (ippsECCPGetSize(ECC_FIELD_SIZE, &ctx_size) != ippStsNoErr) {
            break;
        }

        //allocate ecc ctx
        //
        p_ecc_state = (IppsECCPState*)(malloc(ctx_size));
        if (NULL == p_ecc_state) {
            ret = SGX_ERROR_OUT_OF_MEMORY;
            break;
        }

        //init ecc ctx
        //
        if (ippsECCPInit(ECC_FIELD_SIZE, p_ecc_state) != ippStsNoErr) {
            break;
        }

        //set up elliptic curve domain parameters over GF(p)
        //
        if (ippsECCPSetStd(IppECCPStd256r1, p_ecc_state) != ippStsNoErr) {
            break;
        }

        //get point (public key) size
        //
        if (ippsECCPPointGetSize(ECC_FIELD_SIZE, &point_size) != ippStsNoErr) {
            break;
        }

        //allocate point of point_size size
        //
        public_key = (IppsECCPPointState*)(malloc(point_size));
        if (NULL == public_key) {
            ret = SGX_ERROR_OUT_OF_MEMORY;
            break;
        }

        //init point
        //
        if (ippsECCPPointInit(ECC_FIELD_SIZE, public_key) != ippStsNoErr) {
            break;
        }

        //allocate bn_o, will be used for private key
        //
        if (sgx_ipp_newBN(NULL, sizeof(sgx_ec256_private_t), &bn_o) != ippStsNoErr) {
            break;
        }

        //convert private key into big endian
        //
        p_temp = (uint8_t*)p_att_priv_key;
        for (uint32_t i = 0; i<sizeof(att_priv_key_be); i++) {
            att_priv_key_be.r[i] = *(p_temp + sizeof(att_priv_key_be) - 1 - i);
        }

        //assign private key into bn_o
        //
        if (ippsSetOctString_BN(reinterpret_cast<Ipp8u *>(&att_priv_key_be), sizeof(sgx_ec256_private_t), bn_o) != ippStsNoErr) {
            break;
        }

        //compute public key from the given private key (bn_o) of the elliptic cryptosystem (p_ecc_state) over GF(p).
        //
        if (ippsECCPPublicKey(bn_o, public_key, p_ecc_state) != ippStsNoErr) {
            break;
        }

        //allocate BNs
        //
        if (sgx_ipp_newBN(NULL, sizeof(sgx_ec256_private_t), &bn_x) != ippStsNoErr) {
            break;
        }

        if (sgx_ipp_newBN(NULL, sizeof(sgx_ec256_private_t), &bn_y) != ippStsNoErr) {
            break;
        }
        //assign public key into BNs
        //
        if (ippsECCPGetPoint(bn_x, bn_y, public_key, p_ecc_state) != ippStsNoErr) {
            break;
        }

        //output key in little endian order
        //
        //gx value
        if (ippsGetSize_BN(bn_x, &size) != ippStsNoErr) {
            break;
        }
        if (ippsGet_BN(&sgn, &size, reinterpret_cast<Ipp32u *>(p_att_pub_key->gx), bn_x) != ippStsNoErr) {
            break;
        }
        //gy value
        //
        if (ippsGetSize_BN(bn_y, &size) != ippStsNoErr) {
            break;
        }
        if (ippsGet_BN(&sgn, &size, reinterpret_cast<Ipp32u *>(p_att_pub_key->gy), bn_y) != ippStsNoErr) {
            break;
        }

        ret = SGX_SUCCESS;
    } while (0);

    //in case of failure clear public key
    //
    if (ret != SGX_SUCCESS) {
        (void)memset_s(p_att_pub_key, sizeof(sgx_ec256_public_t), 0, sizeof(sgx_ec256_public_t));
    }

    CLEAR_FREE_MEM(p_ecc_state, ctx_size);
    CLEAR_FREE_MEM(public_key, point_size);
    sgx_ipp_secure_free_BN(bn_o, sizeof(sgx_ec256_private_t));
    sgx_ipp_secure_free_BN(bn_x, sizeof(sgx_ec256_private_t));
    sgx_ipp_secure_free_BN(bn_y, sizeof(sgx_ec256_private_t));

    return ret;
}