Skip to content

Commit

Permalink
Apply @Halastra suggestions from code review of google#144
Browse files Browse the repository at this point in the history
Co-Authored-By: Halastra <turchy@gmail.com>
  • Loading branch information
joeleong and Halastra committed Jul 24, 2019
1 parent 2a33c39 commit 1a9a9cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
35 changes: 30 additions & 5 deletions adb/android_pubkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
https://github.com/aosp-mirror/platform_system_core/blob/c55fab4a59cfa461857c6a61d8a0f1ae4591900c/libcrypto_utils/android_pubkey.c
typedef struct RSAPublicKey {
// Modulus length. This must be ANDROID_PUBKEY_MODULUS_SIZE.
// Modulus length. This must be ANDROID_PUBKEY_MODULUS_SIZE_WORDS.
uint32_t modulus_size_words;
// Precomputed montgomery parameter: -1 / n[0] mod 2^32
Expand All @@ -28,7 +28,6 @@
from __future__ import print_function

import os
import six
import base64
import socket
import struct
Expand All @@ -40,6 +39,17 @@
# Size of an RSA modulus such as an encrypted block or a signature.
ANDROID_PUBKEY_MODULUS_SIZE = (2048 // 8)

# Python representation of "struct RSAPublicKey":
ANDROID_PUBKEY_STRUCT = (
'<' # Little-endian
'L' # uint32_t modulus_size_words;
'L' # uint32_t n0inv;
'{modulus_size}s' # uint8_t modulus[ANDROID_PUBKEY_MODULUS_SIZE];
'{modulus_size}s' # uint8_t rr[ANDROID_PUBKEY_MODULUS_SIZE];
'L' # uint32_t exponent;
).format(modulus_size=ANDROID_PUBKEY_MODULUS_SIZE)


# Size of an encoded RSA key.
ANDROID_PUBKEY_ENCODED_SIZE = \
(3 * 4 + 2 * ANDROID_PUBKEY_MODULUS_SIZE)
Expand All @@ -52,7 +62,7 @@
def _to_bytes(n, length, endianess='big'):
"""partial python2 compatibility with int.to_bytes
https://stackoverflow.com/a/20793663"""
if six.PY2:
if not hasattr(n, 'to_bytes'):
h = '{:x}'.format(n)
s = ('0' * (len(h) % 2) + h).zfill(length * 2).decode('hex')
return s if endianess == 'big' else s[::-1]
Expand All @@ -71,7 +81,12 @@ def decode_pubkey(public_key):
modulus = reversed(key_struct[2: 2 + ANDROID_PUBKEY_MODULUS_SIZE])
rr = reversed(key_struct[2 + ANDROID_PUBKEY_MODULUS_SIZE:
2 + 2 * ANDROID_PUBKEY_MODULUS_SIZE])
exponent = key_struct[-1]

key_struct = struct.unpack(ANDROID_PUBKEY_STRUCT, binary_key_data)
modulus_size_words, n0inv, modulus_bytes, rr_bytes, exponent = key_struct
assert modulus_size_words == ANDROID_PUBKEY_MODULUS_SIZE_WORDS # Verify modulus length
modulus = reversed(modulus_bytes)
rr = reversed(rr_bytes)
print('modulus_size_words:', hex(modulus_size_words))
print('n0inv:', hex(n0inv))
print('modulus: ', end='')
Expand Down Expand Up @@ -113,7 +128,17 @@ def encode_pubkey(private_key_path):
key_buffer += _to_bytes(rr, ANDROID_PUBKEY_MODULUS_SIZE, 'little')

key_buffer += struct.pack('<L', key.e)
return key_buffer

n_bytes = _to_bytes(key.n, ANDROID_PUBKEY_MODULUS_SIZE, 'little')
rr_bytes = _to_bytes(rr, ANDROID_PUBKEY_MODULUS_SIZE, 'little')
return struct.pack(
ANDROID_PUBKEY_STRUCT,
ANDROID_PUBKEY_MODULUS_SIZE_WORDS,
n0inv,
n_bytes,
rr_bytes,
key.e
)


def get_user_info():
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
install_requires = [
'libusb1>=1.0.16',
'pycryptodome',
'six',
rsa_signer_library
],

Expand Down

0 comments on commit 1a9a9cc

Please sign in to comment.