88https://github.com/aosp-mirror/platform_system_core/blob/c55fab4a59cfa461857c6a61d8a0f1ae4591900c/libcrypto_utils/android_pubkey.c
99
1010typedef 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
2828from __future__ import print_function
2929
3030import os
31- import six
3231import base64
3332import socket
3433import struct
4039# Size of an RSA modulus such as an encrypted block or a signature.
4140ANDROID_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.
4454ANDROID_PUBKEY_ENCODED_SIZE = \
4555 (3 * 4 + 2 * ANDROID_PUBKEY_MODULUS_SIZE )
5262def _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
119144def get_user_info ():
0 commit comments