コード例 #1
0
ファイル: _ecdsa.py プロジェクト: sethmlarson/oscrypto
def ec_generate_pair(curve):
    """
    Generates a EC public/private key pair

    :param curve:
        A unicode string. Valid values include "secp256r1", "secp384r1" and
        "secp521r1".

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type

    :return:
        A 2-element tuple of (asn1crypto.keys.PublicKeyInfo,
        asn1crypto.keys.PrivateKeyInfo)
    """

    if curve not in set(['secp256r1', 'secp384r1', 'secp521r1']):
        raise ValueError(
            pretty_message(
                '''
            curve must be one of "secp256r1", "secp384r1", "secp521r1", not %s
            ''', repr(curve)))

    curve_num_bytes = CURVE_BYTES[curve]
    curve_base_point = {
        'secp256r1': SECP256R1_BASE_POINT,
        'secp384r1': SECP384R1_BASE_POINT,
        'secp521r1': SECP521R1_BASE_POINT,
    }[curve]

    while True:
        private_key_bytes = rand_bytes(curve_num_bytes)
        private_key_int = int_from_bytes(private_key_bytes, signed=False)

        if private_key_int > 0 and private_key_int < curve_base_point.order:
            break

    private_key_info = keys.PrivateKeyInfo({
        'version':
        0,
        'private_key_algorithm':
        keys.PrivateKeyAlgorithm({
            'algorithm':
            'ec',
            'parameters':
            keys.ECDomainParameters(name='named', value=curve)
        }),
        'private_key':
        keys.ECPrivateKey({
            'version': 'ecPrivkeyVer1',
            'private_key': private_key_int
        }),
    })

    ec_point = ec_compute_public_key_point(private_key_info)
    private_key_info['private_key'].parsed['public_key'] = ec_point.copy()

    return (ec_public_key_info(ec_point, curve), private_key_info)
コード例 #2
0
    def test_ec_private_key_width_dotted(self):
        k = keys.ECPrivateKey({
            'version': 1,
            'private_key': 1,
            'parameters': keys.ECDomainParameters(('named', '1.3.132.0.10')),
        })

        self.assertEqual('ecPrivkeyVer1', k['version'].native)
        self.assertEqual(1, k['private_key'].native)
        self.assertEqual('secp256k1', k['parameters'].native)
        self.assertEqual(
            b'\x04\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01',
            k['private_key'].dump()
        )
コード例 #3
0
    def test_named_curve_register(self):
        keys.NamedCurve.register('customcurve', '1.2.3.4.5.6.7.8', 16)

        k = keys.NamedCurve('customcurve')
        self.assertEqual('customcurve', k.native)
        self.assertEqual('1.2.3.4.5.6.7.8', k.dotted)

        k = keys.ECPrivateKey({
            'version': 1,
            'private_key': 1,
            'parameters': keys.ECDomainParameters(('named', 'customcurve')),
        })

        self.assertEqual('ecPrivkeyVer1', k['version'].native)
        self.assertEqual(1, k['private_key'].native)
        self.assertEqual('customcurve', k['parameters'].native)
        self.assertEqual(
            b'\x04\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01',
            k['private_key'].dump()
        )