Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 = 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)
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
The X coordinate, as an integer
:param y:
The Y coordinate, as an integer
:return:
An ECPoint object
"""
x_bytes = int(math.ceil(math.log(x, 2) / 8.0))
y_bytes = int(math.ceil(math.log(y, 2) / 8.0))
num_bytes = max(x_bytes, y_bytes)
byte_string = b'\x04'
byte_string += int_to_bytes(x, width=num_bytes)
byte_string += int_to_bytes(y, width=num_bytes)
return cls(byte_string)
original_hmac = hmac.new(password, None, algo)
int_pack = struct.Struct(b'>I').pack
output = b''
for block in range(1, blocks + 1):
prf = original_hmac.copy()
prf.update(salt + int_pack(block))
last = prf.digest()
u = int_from_bytes(last)
for _ in range(2, iterations + 1):
prf = original_hmac.copy()
prf.update(last)
last = prf.digest()
u ^= int_from_bytes(last)
t = int_to_bytes(u)
output += t
return output[0:key_length]
len(value)
))
value += '0' * (size - len(value))
else:
size = len(value)
size_mod = size % 8
extra_bits = 0
if size_mod != 0:
extra_bits = 8 - size_mod
value += '0' * extra_bits
size_in_bytes = int(math.ceil(size / 8))
if extra_bits:
extra_bits_byte = int_to_bytes(extra_bits)
else:
extra_bits_byte = b'\x00'
if value == '':
value_bytes = b''
else:
value_bytes = int_to_bytes(int(value, 2))
if len(value_bytes) != size_in_bytes:
value_bytes = (b'\x00' * (size_in_bytes - len(value_bytes))) + value_bytes
self.contents = extra_bits_byte + value_bytes
self._header = None
if self._trailer != b'':
self._trailer = b''
('public', 'secp384r1'): BcryptConst.BCRYPT_ECDSA_PUBLIC_P384_MAGIC,
('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, y = public_key.to_coords()
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(),
:raises:
ValueError - when an invalid value is passed
"""
if not isinstance(value, int_types):
raise TypeError(unwrap(
'''
%s value must be an integer, not %s
''',
type_name(self),
type_name(value)
))
self._native = value
# Set the unused bits to 0
self.contents = b'\x00' + int_to_bytes(value, signed=True)
self._header = None
if self._trailer != b'':
self._trailer = b''
('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, y = public_key.to_coords()
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,
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 = 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)
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
extra_bits = 0
if size_mod != 0:
extra_bits = 8 - size_mod
value += '0' * extra_bits
size_in_bytes = int(math.ceil(size / 8))
if extra_bits:
extra_bits_byte = int_to_bytes(extra_bits)
else:
extra_bits_byte = b'\x00'
if value == '':
value_bytes = b''
else:
value_bytes = int_to_bytes(int(value, 2))
if len(value_bytes) != size_in_bytes:
value_bytes = (b'\x00' * (size_in_bytes - len(value_bytes))) + value_bytes
self.contents = extra_bits_byte + value_bytes
self._header = None
if self._trailer != b'':
self._trailer = b''
def to_p1363(self):
"""
Dumps a signature to a byte string compatible with Microsoft's
BCryptVerifySignature() function.
:return:
A byte string compatible with BCryptVerifySignature()
"""
r_bytes = int_to_bytes(self['r'].native)
s_bytes = int_to_bytes(self['s'].native)
int_byte_length = max(len(r_bytes), len(s_bytes))
r_bytes = fill_width(r_bytes, int_byte_length)
s_bytes = fill_width(s_bytes, int_byte_length)
return r_bytes + s_bytes