Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 1a9a9cc

Browse files
joeleongHalastra
andauthored
Apply @Halastra suggestions from code review of #144
Co-Authored-By: Halastra <[email protected]>
1 parent 2a33c39 commit 1a9a9cc

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

adb/android_pubkey.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
https://github.com/aosp-mirror/platform_system_core/blob/c55fab4a59cfa461857c6a61d8a0f1ae4591900c/libcrypto_utils/android_pubkey.c
99
1010
typedef struct RSAPublicKey {
11-
// Modulus length. This must be ANDROID_PUBKEY_MODULUS_SIZE.
11+
// Modulus length. This must be ANDROID_PUBKEY_MODULUS_SIZE_WORDS.
1212
uint32_t modulus_size_words;
1313
1414
// Precomputed montgomery parameter: -1 / n[0] mod 2^32
@@ -28,7 +28,6 @@
2828
from __future__ import print_function
2929

3030
import os
31-
import six
3231
import base64
3332
import socket
3433
import struct
@@ -40,6 +39,17 @@
4039
# Size of an RSA modulus such as an encrypted block or a signature.
4140
ANDROID_PUBKEY_MODULUS_SIZE = (2048 // 8)
4241

42+
# Python representation of "struct RSAPublicKey":
43+
ANDROID_PUBKEY_STRUCT = (
44+
'<' # Little-endian
45+
'L' # uint32_t modulus_size_words;
46+
'L' # uint32_t n0inv;
47+
'{modulus_size}s' # uint8_t modulus[ANDROID_PUBKEY_MODULUS_SIZE];
48+
'{modulus_size}s' # uint8_t rr[ANDROID_PUBKEY_MODULUS_SIZE];
49+
'L' # uint32_t exponent;
50+
).format(modulus_size=ANDROID_PUBKEY_MODULUS_SIZE)
51+
52+
4353
# Size of an encoded RSA key.
4454
ANDROID_PUBKEY_ENCODED_SIZE = \
4555
(3 * 4 + 2 * ANDROID_PUBKEY_MODULUS_SIZE)
@@ -52,7 +62,7 @@
5262
def _to_bytes(n, length, endianess='big'):
5363
"""partial python2 compatibility with int.to_bytes
5464
https://stackoverflow.com/a/20793663"""
55-
if six.PY2:
65+
if not hasattr(n, 'to_bytes'):
5666
h = '{:x}'.format(n)
5767
s = ('0' * (len(h) % 2) + h).zfill(length * 2).decode('hex')
5868
return s if endianess == 'big' else s[::-1]
@@ -71,7 +81,12 @@ def decode_pubkey(public_key):
7181
modulus = reversed(key_struct[2: 2 + ANDROID_PUBKEY_MODULUS_SIZE])
7282
rr = reversed(key_struct[2 + ANDROID_PUBKEY_MODULUS_SIZE:
7383
2 + 2 * ANDROID_PUBKEY_MODULUS_SIZE])
74-
exponent = key_struct[-1]
84+
85+
key_struct = struct.unpack(ANDROID_PUBKEY_STRUCT, binary_key_data)
86+
modulus_size_words, n0inv, modulus_bytes, rr_bytes, exponent = key_struct
87+
assert modulus_size_words == ANDROID_PUBKEY_MODULUS_SIZE_WORDS # Verify modulus length
88+
modulus = reversed(modulus_bytes)
89+
rr = reversed(rr_bytes)
7590
print('modulus_size_words:', hex(modulus_size_words))
7691
print('n0inv:', hex(n0inv))
7792
print('modulus: ', end='')
@@ -113,7 +128,17 @@ def encode_pubkey(private_key_path):
113128
key_buffer += _to_bytes(rr, ANDROID_PUBKEY_MODULUS_SIZE, 'little')
114129

115130
key_buffer += struct.pack('<L', key.e)
116-
return key_buffer
131+
132+
n_bytes = _to_bytes(key.n, ANDROID_PUBKEY_MODULUS_SIZE, 'little')
133+
rr_bytes = _to_bytes(rr, ANDROID_PUBKEY_MODULUS_SIZE, 'little')
134+
return struct.pack(
135+
ANDROID_PUBKEY_STRUCT,
136+
ANDROID_PUBKEY_MODULUS_SIZE_WORDS,
137+
n0inv,
138+
n_bytes,
139+
rr_bytes,
140+
key.e
141+
)
117142

118143

119144
def get_user_info():

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
install_requires = [
5757
'libusb1>=1.0.16',
5858
'pycryptodome',
59-
'six',
6059
rsa_signer_library
6160
],
6261

0 commit comments

Comments
 (0)