Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
('private', 'secp256r1'): BcryptConst.BCRYPT_ECDSA_PRIVATE_P256_MAGIC,
('private', 'secp384r1'): BcryptConst.BCRYPT_ECDSA_PRIVATE_P384_MAGIC,
('private', 'secp521r1'): BcryptConst.BCRYPT_ECDSA_PRIVATE_P521_MAGIC,
}[(key_type, curve_name)]
key_width = {
'secp256r1': 32,
'secp384r1': 48,
'secp521r1': 66
}[curve_name]
x_bytes = int_to_bytes(x)
y_bytes = int_to_bytes(y)
x_bytes = fill_width(x_bytes, key_width)
y_bytes = fill_width(y_bytes, key_width)
blob_struct.dwMagic = magic
blob_struct.cbKey = key_width
blob = struct_bytes(blob_struct_pointer) + x_bytes + y_bytes
if key_type == 'private':
blob += fill_width(private_bytes, key_width)
key_handle_pointer = new(bcrypt, 'BCRYPT_KEY_HANDLE *')
res = bcrypt.BCryptImportKeyPair(
alg_handle,
null(),
blob_type,
key_handle_pointer,
blob,
len(blob),
'secp384r1': 48,
'secp521r1': 66
}[curve_name]
x_bytes = int_to_bytes(x)
y_bytes = int_to_bytes(y)
x_bytes = fill_width(x_bytes, key_width)
y_bytes = fill_width(y_bytes, key_width)
blob_struct.dwMagic = magic
blob_struct.cbKey = key_width
blob = struct_bytes(blob_struct_pointer) + x_bytes + y_bytes
if key_type == 'private':
blob += fill_width(private_bytes, key_width)
key_handle_pointer = new(bcrypt, 'BCRYPT_KEY_HANDLE *')
res = bcrypt.BCryptImportKeyPair(
alg_handle,
null(),
blob_type,
key_handle_pointer,
blob,
len(blob),
BcryptConst.BCRYPT_NO_KEY_VALIDATION
)
handle_error(res)
key_handle = unwrap(key_handle_pointer)
return container(key_handle, key_object)
modulus = int_to_bytes(parsed_key['modulus'].native)
blob_struct_pointer = struct(bcrypt, 'BCRYPT_RSAKEY_BLOB')
blob_struct = unwrap(blob_struct_pointer)
blob_struct.Magic = magic
blob_struct.BitLength = key_info.bit_size
blob_struct.cbPublicExp = len(public_exponent)
blob_struct.cbModulus = len(modulus)
blob_struct.cbPrime1 = prime1_size
blob_struct.cbPrime2 = prime2_size
blob = struct_bytes(blob_struct_pointer) + public_exponent + modulus
if key_type == 'private':
blob += prime1 + prime2
blob += fill_width(exponent1, prime1_size)
blob += fill_width(exponent2, prime2_size)
blob += fill_width(coefficient, prime1_size)
blob += fill_width(private_exponent, len(modulus))
elif algo == 'dsa':
if key_type == 'public':
blob_type = BcryptConst.BCRYPT_DSA_PUBLIC_BLOB
public_key = key_info['public_key'].parsed.native
params = key_info['algorithm']['parameters']
else:
blob_type = BcryptConst.BCRYPT_DSA_PRIVATE_BLOB
public_key = _unwrap_private_key_info(key_info)['public_key'].native
private_bytes = int_to_bytes(key_info['private_key'].parsed.native)
params = key_info['private_key_algorithm']['parameters']
public_bytes = int_to_bytes(public_key)
p = int_to_bytes(params['p'].native)
public_bytes = int_to_bytes(public_key)
p = int_to_bytes(params['p'].native)
g = int_to_bytes(params['g'].native)
q = int_to_bytes(params['q'].native)
if key_info.bit_size > 1024:
q_len = len(q)
else:
q_len = 20
key_width = max(len(public_bytes), len(g), len(p))
public_bytes = fill_width(public_bytes, key_width)
p = fill_width(p, key_width)
g = fill_width(g, key_width)
q = fill_width(q, q_len)
# We don't know the count or seed, so we set them to the max value
# since setting them to 0 results in a parameter error
count = b'\xff' * 4
seed = b'\xff' * q_len
if key_info.bit_size > 1024:
if key_type == 'public':
magic = BcryptConst.BCRYPT_DSA_PUBLIC_MAGIC_V2
else:
magic = BcryptConst.BCRYPT_DSA_PRIVATE_MAGIC_V2
blob_struct_pointer = struct(bcrypt, 'BCRYPT_DSA_KEY_BLOB_V2')
blob_struct = unwrap(blob_struct_pointer)
blob_struct.dwMagic = magic
blob_struct.cbKey = key_width
# We don't know if SHA256 was used here, but the output is long
if key_type == 'public':
magic = BcryptConst.BCRYPT_DSA_PUBLIC_MAGIC
else:
magic = BcryptConst.BCRYPT_DSA_PRIVATE_MAGIC
blob_struct_pointer = struct(bcrypt, 'BCRYPT_DSA_KEY_BLOB')
blob_struct = unwrap(blob_struct_pointer)
blob_struct.dwMagic = magic
blob_struct.cbKey = key_width
blob_struct.Count = byte_array(count)
blob_struct.Seed = byte_array(seed)
blob_struct.q = byte_array(q)
blob = struct_bytes(blob_struct_pointer) + p + g + public_bytes
if key_type == 'private':
blob += fill_width(private_bytes, q_len)
elif algo == 'ec':
if key_type == 'public':
blob_type = BcryptConst.BCRYPT_ECCPUBLIC_BLOB
x, y = key_info['public_key'].to_coords()
else:
blob_type = BcryptConst.BCRYPT_ECCPRIVATE_BLOB
public_key = key_info['private_key'].parsed['public_key']
# We aren't guaranteed to get the public key coords with the
# key info structure, but BCrypt doesn't seem to have an issue
# importing the private key with 0 values, which can only be
# presumed that it is generating the x and y points from the
# private key value and base point
if public_key:
x, y = public_key.to_coords()
else:
blob_struct = unwrap(blob_struct_pointer)
blob_struct.dwMagic = magic
blob_struct.cbKey = key_width
# We don't know if SHA256 was used here, but the output is long
# enough for the generation of q for the supported 2048/224,
# 2048/256 and 3072/256 FIPS approved pairs
blob_struct.hashAlgorithm = BcryptConst.DSA_HASH_ALGORITHM_SHA256
blob_struct.standardVersion = BcryptConst.DSA_FIPS186_3
blob_struct.cbSeedLength = q_len
blob_struct.cbGroupSize = q_len
blob_struct.Count = byte_array(count)
blob = struct_bytes(blob_struct_pointer)
blob += seed + q + p + g + public_bytes
if key_type == 'private':
blob += fill_width(private_bytes, q_len)
else:
if key_type == 'public':
magic = BcryptConst.BCRYPT_DSA_PUBLIC_MAGIC
else:
magic = BcryptConst.BCRYPT_DSA_PRIVATE_MAGIC
blob_struct_pointer = struct(bcrypt, 'BCRYPT_DSA_KEY_BLOB')
blob_struct = unwrap(blob_struct_pointer)
blob_struct.dwMagic = magic
blob_struct.cbKey = key_width
blob_struct.Count = byte_array(count)
blob_struct.Seed = byte_array(seed)
blob_struct.q = byte_array(q)
blob = struct_bytes(blob_struct_pointer) + p + g + public_bytes
public_bytes = int_to_bytes(public_key)
p = int_to_bytes(params['p'].native)
g = int_to_bytes(params['g'].native)
q = int_to_bytes(params['q'].native)
if key_info.bit_size > 1024:
q_len = len(q)
else:
q_len = 20
key_width = max(len(public_bytes), len(g), len(p))
public_bytes = fill_width(public_bytes, key_width)
p = fill_width(p, key_width)
g = fill_width(g, key_width)
q = fill_width(q, q_len)
# We don't know the count or seed, so we set them to the max value
# since setting them to 0 results in a parameter error
count = b'\xff' * 4
seed = b'\xff' * q_len
if key_info.bit_size > 1024:
if key_type == 'public':
magic = BcryptConst.BCRYPT_DSA_PUBLIC_MAGIC_V2
else:
magic = BcryptConst.BCRYPT_DSA_PRIVATE_MAGIC_V2
blob_struct_pointer = struct(bcrypt, 'BCRYPT_DSA_KEY_BLOB_V2')
blob_struct = unwrap(blob_struct_pointer)
blob_struct.dwMagic = magic
blob_struct.cbKey = key_width
('public', 'secp521r1'): BcryptConst.BCRYPT_ECDSA_PUBLIC_P521_MAGIC,
('private', 'secp256r1'): BcryptConst.BCRYPT_ECDSA_PRIVATE_P256_MAGIC,
('private', 'secp384r1'): BcryptConst.BCRYPT_ECDSA_PRIVATE_P384_MAGIC,
('private', 'secp521r1'): BcryptConst.BCRYPT_ECDSA_PRIVATE_P521_MAGIC,
}[(key_type, curve_name)]
key_width = {
'secp256r1': 32,
'secp384r1': 48,
'secp521r1': 66
}[curve_name]
x_bytes = int_to_bytes(x)
y_bytes = int_to_bytes(y)
x_bytes = fill_width(x_bytes, key_width)
y_bytes = fill_width(y_bytes, key_width)
blob_struct.dwMagic = magic
blob_struct.cbKey = key_width
blob = struct_bytes(blob_struct_pointer) + x_bytes + y_bytes
if key_type == 'private':
blob += fill_width(private_bytes, key_width)
key_handle_pointer = new(bcrypt, 'BCRYPT_KEY_HANDLE *')
res = bcrypt.BCryptImportKeyPair(
alg_handle,
null(),
blob_type,
key_handle_pointer,
blob,