adb_shell.auth.keygen module

This file implements encoding and decoding logic for Android’s custom RSA public key binary format. Public keys are stored as a sequence of little-endian 32 bit words. Note that Android only supports little-endian processors, so we don’t do any byte order conversions when parsing the binary struct.

Structure from: 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_WORDS
    uint32_t modulus_size_words;

    // Precomputed montgomery parameter: -1 / n[0] mod 2^32
    uint32_t n0inv;

    // RSA modulus as a little-endian array
    uint8_t modulus[ANDROID_PUBKEY_MODULUS_SIZE];

    // Montgomery parameter R^2 as a little-endian array of little-endian words
    uint8_t rr[ANDROID_PUBKEY_MODULUS_SIZE];

    // RSA modulus: 3 or 65537
    uint32_t exponent;
} RSAPublicKey;

Contents

adb_shell.auth.keygen.ANDROID_PUBKEY_MODULUS_SIZE = 256

Size of an RSA modulus such as an encrypted block or a signature.

adb_shell.auth.keygen.ANDROID_PUBKEY_MODULUS_SIZE_WORDS = 64

Size of the RSA modulus in words.

adb_shell.auth.keygen.ANDROID_RSAPUBLICKEY_STRUCT = '<LL256s256sL'

Python representation of “struct RSAPublicKey”

adb_shell.auth.keygen._to_bytes(n, length, endianess='big')[source]

Partial python2 compatibility with int.to_bytes

https://stackoverflow.com/a/20793663

Parameters:
  • n (TODO) – TODO

  • length (TODO) – TODO

  • endianess (str, TODO) – TODO

Returns:

TODO

Return type:

TODO

adb_shell.auth.keygen.decode_pubkey(public_key)[source]

Decode a public RSA key stored in Android’s custom binary format.

Parameters:

public_key (TODO) – TODO

adb_shell.auth.keygen.decode_pubkey_file(public_key_path)[source]

TODO

Parameters:

public_key_path (str) – TODO

adb_shell.auth.keygen.encode_pubkey(private_key_path)[source]

Encodes a public RSA key into Android’s custom binary format.

Parameters:

private_key_path (str) – TODO

Returns:

TODO

Return type:

TODO

adb_shell.auth.keygen.get_user_info()[source]

TODO

Returns:

' <username>@<hostname>

Return type:

str

adb_shell.auth.keygen.keygen(filepath)[source]

Generate an ADB public/private key pair.

  • The private key is stored in filepath.

  • The public key is stored in filepath + '.pub'

(Existing files will be overwritten.)

Parameters:

filepath (str) – File path to write the private/public keypair

adb_shell.auth.keygen.write_public_keyfile(private_key_path, public_key_path)[source]

Write a public keyfile to public_key_path in Android’s custom RSA public key format given a path to a private keyfile.

Parameters:
  • private_key_path (TODO) – TODO

  • public_key_path (TODO) – TODO