|
PostgreSQL Source Code git master
|
Functions | |
| dict | generate_rsa_keypair (int key_size=2048) |
| None | validate_rsa_key (dict rsa) |
| bytes | mpi_encode (int x) |
| bytes | new_packet (int tag, bytes payload) |
| bytes | build_key_data (dict rsa) |
| pgp_cfb_encrypt_resync (key, plaintext) | |
| bytes | build_literal_data_packet (bytes data) |
| bytes | build_symenc_data_packet (bytes sess_key, int cipher_algo, bytes payload) |
| bytes | build_tag1_packet (dict rsa, bytes sess_key) |
| bytes | build_message_data (dict rsa) |
| main () | |
Variables | |
| str | AES_KEY = b'\x01' * 16 |
| bytes pgp_session_data.build_key_data | ( | dict | rsa | ) |
Build the key data, containing an RSA private key. The RSA contents should have been generated previously. Format (see RFC 4880, Section 5.5.3): - 1 byte: version (4) - 4 bytes: creation time (current Unix timestamp) - 1 byte: public key algorithm (2 = RSA encrypt) - MPI: RSA public modulus n - MPI: RSA public exponent e - 1 byte: string-to-key usage (0 = no encryption) - MPI: RSA private exponent d - MPI: RSA prime p - MPI: RSA prime q - MPI: RSA coefficient u = p^-1 mod q - 2 bytes: checksum of private key material This function takes a set of RSA key components in input (n, e, d, p, q, u) and returns a secret key packet.
Definition at line 184 of file pgp_session_data.py.
References fb(), mpi_encode(), and new_packet().
Referenced by main().
| bytes pgp_session_data.build_literal_data_packet | ( | bytes | data | ) |
Build a literal data packet containing a message.
Format (RFC 4880, Section 5.9):
- 1 byte: data format ('b' = binary, 't' = text, 'u' = UTF-8 text)
- 1 byte: filename length (0 = no filename)
- N bytes: filename (empty in this case)
- 4 bytes: date (current Unix timestamp)
- M bytes: literal data
The data used to build the packet is given in input, with the generated
result returned.
Definition at line 287 of file pgp_session_data.py.
References fb(), and new_packet().
Referenced by build_symenc_data_packet().
| bytes pgp_session_data.build_message_data | ( | dict | rsa | ) |
This function creates a crafted message, with a long session key length. This takes in input the RSA key components generated previously, returning a concatenated set of PGP packets crafted for the purpose of this test.
Definition at line 417 of file pgp_session_data.py.
References build_symenc_data_packet(), build_tag1_packet(), and fb().
Referenced by main().
| bytes pgp_session_data.build_symenc_data_packet | ( | bytes | sess_key, |
| int | cipher_algo, | ||
| bytes | payload | ||
| ) |
Build a symmetrically-encrypted data packet using AES-128-CFB. This packet contains encrypted data using the session key. The format includes a random prefix, for security (see RFC 4880, Section 5.7). Packet structure: - Random prefix (block_size bytes) - Prefix repeat (last 2 bytes of prefix repeated) - Encrypted literal data packet This function uses the following set of arguments: - sess_key: Session key for encryption - cipher_algo: Cipher algorithm identifier (7 = AES-128) - payload: Data to encrypt (wrapped in literal data packet)
Definition at line 308 of file pgp_session_data.py.
References build_literal_data_packet(), fb(), new_packet(), and pgp_cfb_encrypt_resync().
Referenced by build_message_data().
| bytes pgp_session_data.build_tag1_packet | ( | dict | rsa, |
| bytes | sess_key | ||
| ) |
Build a public-key encrypted key. This is a very important function, as it is able to create the packet triggering the overflow check. This function can also be used to create "legit" packet data. Format (RFC 4880, Section 5.1): - 1 byte: version (3) - 8 bytes: key ID (0 = any key accepted) - 1 byte: public key algorithm (2 = RSA encrypt) - MPI: RSA-encrypted session key This uses in arguments the generated RSA key pair, and the session key to encrypt. The latter is manipulated to trigger the overflow. This function returns a complete packet encrypted by a session key.
Definition at line 344 of file pgp_session_data.py.
References fb(), len, mpi_encode(), and new_packet().
Referenced by build_message_data().
Generate a fresh RSA key pair. The generated key includes all components needed for PGP operations: - n: public modulus (p * q) - e: public exponent (typically 65537) - d: private exponent (e^-1 mod phi(n)) - p, q: prime factors of n - u: coefficient (p^-1 mod q) for CRT optimization The caller can pass the wanted key size in input, for a default of 2048 bytes. This function returns the RSA key components, after performing some validation on them.
Definition at line 35 of file pgp_session_data.py.
References fb(), and validate_rsa_key().
Referenced by main().
| pgp_session_data.main | ( | void | ) |
Definition at line 453 of file pgp_session_data.py.
References build_key_data(), build_message_data(), fb(), generate_rsa_keypair(), main(), and print().
Referenced by main().
| bytes pgp_session_data.mpi_encode | ( | int | x | ) |
Encode an integer as an OpenPGP Multi-Precision Integer (MPI).
Format (RFC 4880, Section 3.2):
- 2 bytes: bit length of the integer (big-endian)
- N bytes: the integer in big-endian format
This is used to encode RSA key components (n, e, d, p, q, u) in PGP
packets.
The integer to encode is given in input, returning an MPI-encoded
integer.
For example:
mpi_encode(65537) -> b'\x00\x11\x01\x00\x01'
(17 bits, value 0x010001)
Definition at line 115 of file pgp_session_data.py.
References fb().
Referenced by build_key_data(), and build_tag1_packet().
| bytes pgp_session_data.new_packet | ( | int | tag, |
| bytes | payload | ||
| ) |
Create a new OpenPGP packet with a proper header.
OpenPGP packet format (RFC 4880, Section 4.2):
- New packet format: 0xC0 | tag
- Length encoding depends on payload size:
* 0-191: single byte
* 192-8383: two bytes (192 + ((length - 192) >> 8), (length - 192) & 0xFF)
* 8384+: five bytes (0xFF + 4-byte big-endian length)
The packet is built from a "tag" (1-63) and some "payload" data. The
result generated is a complete OpenPGP packet.
For example:
new_packet(1, b'data') -> b'\xC1\x04data'
(Tag 1, length 4, payload 'data')
Definition at line 148 of file pgp_session_data.py.
Referenced by build_key_data(), build_literal_data_packet(), build_symenc_data_packet(), and build_tag1_packet().
| pgp_session_data.pgp_cfb_encrypt_resync | ( | key, | |
| plaintext | |||
| ) |
Implement OpenPGP CFB mode with resync. OpenPGP CFB mode is a variant of standard CFB with a resync operation after the first two blocks. Algorithm (RFC 4880, Section 13.9): 1. Block 1: FR=zeros, encrypt full block_size bytes 2. Block 2: FR=block1, encrypt only 2 bytes 3. Resync: FR = block1[2:] + block2 4. Remaining blocks: standard CFB mode This function uses the following arguments: - key: AES encryption key (16 bytes for AES-128) - plaintext: Data to encrypt
Definition at line 231 of file pgp_session_data.py.
Referenced by build_symenc_data_packet().
Validate a generated RSA key. This function performs basic validation to ensure the RSA key is properly constructed and all components are consistent, at least mathematically. Validations performed: 1. n = p * q (modulus is product of primes) 2. gcd(e, phi(n)) = 1 (public exponent is coprime to phi(n)) 3. (d * e) mod(phi(n)) = 1 (private exponent is multiplicative inverse) 4. (u * p) (mod q) = 1 (coefficient is correct for CRT)
Definition at line 71 of file pgp_session_data.py.
Referenced by generate_rsa_keypair().
Definition at line 33 of file pgp_session_data.py.