Commit f1047898 authored by voussoir's avatar voussoir

Deconstruct & comment this private_key MPI parser.

parent 2ba447eb
......@@ -104,6 +104,11 @@ def str_to_a32(b):
def mpi_to_int(s):
'''
A Multi-precision integer is encoded as a series of bytes in big-endian
order. The first two bytes are a header which tell the number of bits in
the integer. The rest of the bytes are the integer.
'''
return int(binascii.hexlify(s[2:]), 16)
def extended_gcd(a, b):
......
import math
import re
import json
import logging
......@@ -119,13 +120,17 @@ class Mega:
)
private_key = a32_to_str(rsa_private_key)
# The private_key contains 4 MPI integers concatenated together.
rsa_private_key = [0, 0, 0, 0]
for i in range(4):
l = int(
((private_key[0]) * 256 + (private_key[1]) + 7) / 8
) + 2
rsa_private_key[i] = mpi_to_int(private_key[:l])
private_key = private_key[l:]
# An MPI integer has a 2-byte header which describes the number
# of bits in the integer.
bitlength = (private_key[0] * 256) + private_key[1]
bytelength = math.ceil(bitlength / 8)
# Add 2 bytes to accommodate the MPI header
bytelength += 2
rsa_private_key[i] = mpi_to_int(private_key[:bytelength])
private_key = private_key[bytelength:]
encrypted_sid = mpi_to_int(base64_url_decode(resp['csid']))
rsa_decrypter = RSA.construct(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment