def pointMult(secret): # ctx = OpenSSL.BN_CTX_new() #This value proved to cause Seg Faults on # Linux. It turns out that it really didn't speed up EC_POINT_mul anyway. k = OpenSSL.EC_KEY_new_by_curve_name(OpenSSL.get_curve('secp256k1')) priv_key = OpenSSL.BN_bin2bn(secret, 32, 0) group = OpenSSL.EC_KEY_get0_group(k) pub_key = OpenSSL.EC_POINT_new(group) OpenSSL.EC_POINT_mul(group, pub_key, priv_key, None, None, None) OpenSSL.EC_KEY_set_private_key(k, priv_key) OpenSSL.EC_KEY_set_public_key(k, pub_key) # print 'priv_key',priv_key # print 'pub_key',pub_key size = OpenSSL.i2o_ECPublicKey(k, 0) mb = ctypes.create_string_buffer(size) OpenSSL.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(mb))) # print 'mb.raw', mb.raw.encode('hex'), 'length:', len(mb.raw) # print 'mb.raw', mb.raw, 'length:', len(mb.raw) OpenSSL.EC_POINT_free(pub_key) # OpenSSL.BN_CTX_free(ctx) OpenSSL.BN_free(priv_key) OpenSSL.EC_KEY_free(k) return mb.raw
def __mul__(self, other): """ Multiply an EC point by a scalar value and returns the multiplication result """ if isinstance(other, int) or isinstance(other, long): try: o = ec_bignum.BigNum(decval=other) result = OpenSSL.EC_POINT_new(self.os_group) OpenSSL.EC_POINT_mul(self.os_group, result, 0, self.os_point, o.bn, 0) return Point(self.curve, openssl_point=result) finally: del o else: return NotImplemented
def pointMul(privKey): ecKey = OpenSSL.EC_KEY_new_by_curve_name(OpenSSL.get_curve('secp256k1')) priv_key = OpenSSL.BN_bin2bn(privKey, 32, 0) group = OpenSSL.EC_KEY_get0_group(ecKey) pub_key = OpenSSL.EC_POINT_new(group) OpenSSL.EC_POINT_mul(group, pub_key, priv_key, None, None, None) OpenSSL.EC_KEY_set_private_key(ecKey, priv_key) OpenSSL.EC_KEY_set_public_key(ecKey, pub_key) size = OpenSSL.i2o_ECPublicKey(ecKey, 0) mb = ctypes.create_string_buffer(size) OpenSSL.i2o_ECPublicKey(ecKey, ctypes.byref(ctypes.pointer(mb))) OpenSSL.EC_POINT_free(pub_key) OpenSSL.BN_free(priv_key) OpenSSL.EC_KEY_free(ecKey) return mb.raw
def ecc_priv_to_pub_key(priv_key, curve=DEFAULT_CURVE): """Does an EC point multiplication to get public key""" k = OpenSSL.EC_KEY_new_by_curve_name(OpenSSL.get_curve(curve)) priv_key = OpenSSL.BN_bin2bn(priv_key, 32, 0) group = OpenSSL.EC_KEY_get0_group(k) pub_key = OpenSSL.EC_POINT_new(group) OpenSSL.EC_POINT_mul(group, pub_key, priv_key, None, None, None) OpenSSL.EC_KEY_set_private_key(k, priv_key) OpenSSL.EC_KEY_set_public_key(k, pub_key) size = OpenSSL.i2o_ECPublicKey(k, 0) mb = ctypes.create_string_buffer(size) OpenSSL.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(mb))) OpenSSL.EC_POINT_free(pub_key) OpenSSL.BN_free(priv_key) OpenSSL.EC_KEY_free(k) return mb.raw
def ecc_ecdh_key(sec, pub): assert isinstance(sec, ecc.ECC) if isinstance(pub, ecc.ECC): pub = pub.get_pubkey() #return sec.get_ecdh_key(pub) pubkey_x, pubkey_y = ecc.ECC._decode_pubkey(pub, 'binary') other_key = other_pub_key_x = other_pub_key_y = other_pub_key = None own_priv_key = res = res_x = res_y = None try: other_key = OpenSSL.EC_KEY_new_by_curve_name(sec.curve) if other_key == 0: raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ... " + OpenSSL.get_error()) other_pub_key_x = OpenSSL.BN_bin2bn(pubkey_x, len(pubkey_x), 0) other_pub_key_y = OpenSSL.BN_bin2bn(pubkey_y, len(pubkey_y), 0) other_group = OpenSSL.EC_KEY_get0_group(other_key) other_pub_key = OpenSSL.EC_POINT_new(other_group) if (other_pub_key == None): raise Exception("[OpenSSl] EC_POINT_new FAIL ... " + OpenSSL.get_error()) if (OpenSSL.EC_POINT_set_affine_coordinates_GFp(other_group, other_pub_key, other_pub_key_x, other_pub_key_y, 0)) == 0: raise Exception( "[OpenSSL] EC_POINT_set_affine_coordinates_GFp FAIL ..." + OpenSSL.get_error()) own_priv_key = OpenSSL.BN_bin2bn(sec.privkey, len(sec.privkey), 0) res = OpenSSL.EC_POINT_new(other_group) if (OpenSSL.EC_POINT_mul(other_group, res, 0, other_pub_key, own_priv_key, 0)) == 0: raise Exception( "[OpenSSL] EC_POINT_mul FAIL ..." + OpenSSL.get_error()) res_x = OpenSSL.BN_new() res_y = OpenSSL.BN_new() if (OpenSSL.EC_POINT_get_affine_coordinates_GFp(other_group, res, res_x, res_y, 0 )) == 0: raise Exception( "[OpenSSL] EC_POINT_get_affine_coordinates_GFp FAIL ... " + OpenSSL.get_error()) resx = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(res_x)) resy = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(res_y)) OpenSSL.BN_bn2bin(res_x, resx) resx = resx.raw OpenSSL.BN_bn2bin(res_y, resy) resy = resy.raw return resx, resy finally: if other_key: OpenSSL.EC_KEY_free(other_key) if other_pub_key_x: OpenSSL.BN_free(other_pub_key_x) if other_pub_key_y: OpenSSL.BN_free(other_pub_key_y) if other_pub_key: OpenSSL.EC_POINT_free(other_pub_key) if own_priv_key: OpenSSL.BN_free(own_priv_key) if res: OpenSSL.EC_POINT_free(res) if res_x: OpenSSL.BN_free(res_x) if res_y: OpenSSL.BN_free(res_y)
group = OpenSSL.EC_KEY_get0_group(k) for ff in itertools.product(*[[1, f] for f in factors]): a = 1 for f in ff: a *= f if a >= 2**128: continue ahex = '%x' % a if len(ahex) % 2: ahex = '0' + ahex abin = binascii.unhexlify(ahex) priv_key = OpenSSL.BN_bin2bn(abin, len(abin), 0) pub_key = OpenSSL.EC_POINT_new(group) assert OpenSSL.EC_POINT_mul(group, pub_key, priv_key, None, None, None) == 1 pubhex = OpenSSL.EC_POINT_point2hex(group, pub_key, POINT_CONVERSION_COMPRESSED, 0) if ctypes.cast(pubhex, ctypes.c_char_p).value == cp: print 'found it!', ahex key = ahex break OpenSSL.CRYPTO_free(pubhex) OpenSSL.EC_POINT_free(pub_key) OpenSSL.BN_free(priv_key) tried += 1 if tried % 10000 == 0: sys.stdout.write('%d.. ' % tried)