Python3 fixes
This commit is contained in:
@@ -1,7 +1,13 @@
|
|||||||
2015-08-03 Niibe Yutaka <gniibe@fsij.org>
|
2015-08-03 Niibe Yutaka <gniibe@fsij.org>
|
||||||
|
|
||||||
|
* test/features/steps.py (set_msg): Python3 fix.
|
||||||
|
* test/generate_keys.py: Likewise.
|
||||||
|
* test/rsa_keys.py: Likewise.
|
||||||
|
|
||||||
* tool/gnuk_token.py (gnuk_token.download, gnuk_token.execute)
|
* tool/gnuk_token.py (gnuk_token.download, gnuk_token.execute)
|
||||||
(regnual.download): Python3 fix.
|
(regnual.download): Python3 fix.
|
||||||
|
(list_to_string): Remove.
|
||||||
|
|
||||||
* tool/upgrade_by_passwd.py (maian): Python3 fix.
|
* tool/upgrade_by_passwd.py (maian): Python3 fix.
|
||||||
* tool/usb_strings.py (main): Python3 fix.
|
* tool/usb_strings.py (main): Python3 fix.
|
||||||
|
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ def cmd_put_data_with_result(tag_str):
|
|||||||
|
|
||||||
@Given("a message (\".*\")")
|
@Given("a message (\".*\")")
|
||||||
def set_msg(content_str_repr):
|
def set_msg(content_str_repr):
|
||||||
msg = ast.literal_eval(content_str_repr)
|
msg = ast.literal_eval(content_str_repr).encode('UTF-8')
|
||||||
scc.digestinfo = rsa_keys.compute_digestinfo(msg)
|
scc.digestinfo = rsa_keys.compute_digestinfo(msg)
|
||||||
|
|
||||||
@Given("a public key from token for OPENPGP.(.*)")
|
@Given("a public key from token for OPENPGP.(.*)")
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ def print_key_in_hex(k):
|
|||||||
q_str = hexlify(q)
|
q_str = hexlify(q)
|
||||||
if int(p_str, 16)*int(q_str, 16) != int(n_str, 16):
|
if int(p_str, 16)*int(q_str, 16) != int(n_str, 16):
|
||||||
raise ValueError("wrong key", k)
|
raise ValueError("wrong key", k)
|
||||||
print n_str
|
print(n_str)
|
||||||
print e_str
|
print(e_str)
|
||||||
print p_str
|
print(p_str)
|
||||||
print q_str
|
print(q_str)
|
||||||
|
|
||||||
rng = Random.new().read
|
rng = Random.new().read
|
||||||
key = RSA.generate(2048, rng)
|
key = RSA.generate(2048, rng)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ def calc_fpr(n,e):
|
|||||||
timestamp = int(time())
|
timestamp = int(time())
|
||||||
timestamp_data = pack('>I', timestamp)
|
timestamp_data = pack('>I', timestamp)
|
||||||
m_len = 6 + 2 + 256 + 2 + 4
|
m_len = 6 + 2 + 256 + 2 + 4
|
||||||
m = '\x99' + pack('>H', m_len) + '\x04' + timestamp_data + '\x01' + \
|
m = b'\x99' + pack('>H', m_len) + b'\x04' + timestamp_data + b'\x01' + \
|
||||||
pack('>H', 2048) + n + pack('>H', 17) + e
|
pack('>H', 2048) + n + pack('>H', 17) + e
|
||||||
fpr = sha1(m).digest()
|
fpr = sha1(m).digest()
|
||||||
return (fpr, timestamp_data)
|
return (fpr, timestamp_data)
|
||||||
@@ -42,39 +42,39 @@ key[2] = read_key_from_file('rsa-aut.key')
|
|||||||
(fpr[2], timestamp[2]) = calc_fpr(key[2][0], key[2][1])
|
(fpr[2], timestamp[2]) = calc_fpr(key[2][0], key[2][1])
|
||||||
|
|
||||||
def build_privkey_template(openpgp_keyno, keyno):
|
def build_privkey_template(openpgp_keyno, keyno):
|
||||||
n_str = key[keyno][0]
|
n_bytes = key[keyno][0]
|
||||||
e_str = '\x00' + key[keyno][1]
|
e_bytes = b'\x00' + key[keyno][1]
|
||||||
p_str = key[keyno][2]
|
p_bytes = key[keyno][2]
|
||||||
q_str = key[keyno][3]
|
q_bytes = key[keyno][3]
|
||||||
|
|
||||||
if openpgp_keyno == 1:
|
if openpgp_keyno == 1:
|
||||||
keyspec = '\xb6'
|
keyspec = b'\xb6'
|
||||||
elif openpgp_keyno == 2:
|
elif openpgp_keyno == 2:
|
||||||
keyspec = '\xb8'
|
keyspec = b'\xb8'
|
||||||
else:
|
else:
|
||||||
keyspec = '\xa4'
|
keyspec = b'\xa4'
|
||||||
|
|
||||||
key_template = '\x91\x04'+ '\x92\x81\x80' + '\x93\x81\x80'
|
key_template = b'\x91\x04'+ b'\x92\x81\x80' + b'\x93\x81\x80'
|
||||||
|
|
||||||
exthdr = keyspec + '\x00' + '\x7f\x48' + '\x08' + key_template
|
exthdr = keyspec + b'\x00' + b'\x7f\x48' + b'\x08' + key_template
|
||||||
|
|
||||||
suffix = '\x5f\x48' + '\x82\x01\x04'
|
suffix = b'\x5f\x48' + b'\x82\x01\x04'
|
||||||
|
|
||||||
t = '\x4d' + '\x82\01\16' + exthdr + suffix + e_str + p_str + q_str
|
t = b'\x4d' + b'\x82\01\16' + exthdr + suffix + e_bytes + p_bytes + q_bytes
|
||||||
return t
|
return t
|
||||||
|
|
||||||
def build_privkey_template_for_remove(openpgp_keyno):
|
def build_privkey_template_for_remove(openpgp_keyno):
|
||||||
if openpgp_keyno == 1:
|
if openpgp_keyno == 1:
|
||||||
keyspec = '\xb6'
|
keyspec = b'\xb6'
|
||||||
elif openpgp_keyno == 2:
|
elif openpgp_keyno == 2:
|
||||||
keyspec = '\xb8'
|
keyspec = b'\xb8'
|
||||||
else:
|
else:
|
||||||
keyspec = '\xa4'
|
keyspec = b'\xa4'
|
||||||
return '\x4d\02' + keyspec + '\0x00'
|
return b'\x4d\02' + keyspec + b'\0x00'
|
||||||
|
|
||||||
def compute_digestinfo(msg):
|
def compute_digestinfo(msg):
|
||||||
digest = sha256(msg).digest()
|
digest = sha256(msg).digest()
|
||||||
prefix = '\x30\31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20'
|
prefix = b'\x30\31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20'
|
||||||
return prefix + digest
|
return prefix + digest
|
||||||
|
|
||||||
# egcd and modinv are from wikibooks
|
# egcd and modinv are from wikibooks
|
||||||
@@ -95,14 +95,14 @@ def modinv(a, m):
|
|||||||
return x % m
|
return x % m
|
||||||
|
|
||||||
def pkcs1_pad_for_sign(digestinfo):
|
def pkcs1_pad_for_sign(digestinfo):
|
||||||
byte_repr = '\x00' + '\x01' + string.ljust('', 256 - 19 - 32 - 3, '\xff') \
|
byte_repr = b'\x00' + b'\x01' + bytes.ljust(b'', 256 - 19 - 32 - 3, b'\xff') \
|
||||||
+ '\x00' + digestinfo
|
+ b'\x00' + digestinfo
|
||||||
return int(hexlify(byte_repr), 16)
|
return int(hexlify(byte_repr), 16)
|
||||||
|
|
||||||
def pkcs1_pad_for_crypt(msg):
|
def pkcs1_pad_for_crypt(msg):
|
||||||
padlen = 256 - 3 - len(msg)
|
padlen = 256 - 3 - len(msg)
|
||||||
byte_repr = '\x00' + '\x02' \
|
byte_repr = b'\x00' + b'\x02' \
|
||||||
+ string.replace(urandom(padlen),'\x00','\x01') + '\x00' + msg
|
+ bytes.replace(urandom(padlen), b'\x00', b'\x01') + b'\x00' + msg
|
||||||
return int(hexlify(byte_repr), 16)
|
return int(hexlify(byte_repr), 16)
|
||||||
|
|
||||||
def compute_signature(keyno, digestinfo):
|
def compute_signature(keyno, digestinfo):
|
||||||
@@ -136,13 +136,13 @@ def encrypt(keyno, plaintext):
|
|||||||
e = key[keyno][4]
|
e = key[keyno][4]
|
||||||
n = key[keyno][7]
|
n = key[keyno][7]
|
||||||
m = pkcs1_pad_for_crypt(plaintext)
|
m = pkcs1_pad_for_crypt(plaintext)
|
||||||
return '\x00' + integer_to_bytes_256(pow(m, e, n))
|
return b'\x00' + integer_to_bytes_256(pow(m, e, n))
|
||||||
|
|
||||||
def encrypt_with_pubkey(pubkey_info, plaintext):
|
def encrypt_with_pubkey(pubkey_info, plaintext):
|
||||||
n = int(hexlify(pubkey_info[0]), 16)
|
n = int(hexlify(pubkey_info[0]), 16)
|
||||||
e = int(hexlify(pubkey_info[1]), 16)
|
e = int(hexlify(pubkey_info[1]), 16)
|
||||||
m = pkcs1_pad_for_crypt(plaintext)
|
m = pkcs1_pad_for_crypt(plaintext)
|
||||||
return '\x00' + integer_to_bytes_256(pow(m, e, n))
|
return b'\x00' + integer_to_bytes_256(pow(m, e, n))
|
||||||
|
|
||||||
def verify_signature(pubkey_info, digestinfo, sig):
|
def verify_signature(pubkey_info, digestinfo, sig):
|
||||||
n = int(hexlify(pubkey_info[0]), 16)
|
n = int(hexlify(pubkey_info[0]), 16)
|
||||||
|
|||||||
Reference in New Issue
Block a user