View Source OpenPGP.Util (OpenPGP v0.6.2)
Provides a set of utility functions to work with data.
Summary
Functions
Convert compression algorithm ID to a tuple with ID and name binary.
Concatenates packet body given a Packet or a list of BodyChunks.
Decode Multiprecision integer (MPI) given input binary. Return MPI value and remaining binary.
Inverse of .decode_mpi/1
. Takes an MPI value, and encode it as MPI
binary.
Convert public-key algorithm ID to a tuple with ID and name binary.
Detects cipher block size (bits) given symmetric encryption algorithm ID or a tuple.
Detects cipher key size (bits) given symmetric encryption algorithm ID or a tuple.
Convert symmetric encryption algorithm ID to a tuple with ID and name binary.
Types
Functions
@spec compression_algo_tuple(byte()) :: compression_algo_tuple()
Convert compression algorithm ID to a tuple with ID and name binary.
RFC4880 (https://www.ietf.org/rfc/rfc4880.txt)
9.3. Compression Algorithms
ID Algorithm
-- ---------
0 - Uncompressed
1 - ZIP [RFC1951]
2 - ZLIB [RFC1950]
3 - BZip2 [BZ2]
100 to 110 - Private/Experimental algorithm
@spec concat_body([OpenPGP.Packet.BodyChunk.t()] | OpenPGP.Packet.t()) :: bitstring()
Concatenates packet body given a Packet or a list of BodyChunks.
Decode Multiprecision integer (MPI) given input binary. Return MPI value and remaining binary.
RFC4880
3.2. Multiprecision Integers
Multiprecision integers (also called MPIs) are unsigned integers used to hold large integers such as the ones used in cryptographic calculations.
An MPI consists of two pieces: a two-octet scalar that is the length of the MPI in bits followed by a string of octets that contain the actual integer.
These octets form a big-endian number; a big-endian number can be made into an MPI by prefixing it with the appropriate length.
Examples:
(all numbers are in hexadecimal)
The string of octets [00 01 01] forms an MPI with the value 1. The string [00 09 01 FF] forms an MPI with the value of 511.
Additional rules:
The size of an MPI is ((MPI.length + 7) / 8) + 2 octets.
The length field of an MPI describes the length starting from its most significant non-zero bit. Thus, the MPI [00 02 01] is not formed correctly. It should be [00 01 01].
Unused bits of an MPI MUST be zero.
Also note that when an MPI is encrypted, the length refers to the plaintext MPI. It may be ill-formed in its ciphertext.
@spec encode_mpi(big_endian :: binary()) :: mpi :: <<_::16, _::_*8>>
Inverse of .decode_mpi/1
. Takes an MPI value, and encode it as MPI
binary.
Example:
iex> OpenPGP.Util.encode_mpi(<<0x1>>)
<<0, 0x1, 0x1>>
iex> OpenPGP.Util.encode_mpi(<<0x1, 0xFF>>)
<<0x0, 0x9, 0x1, 0xFF>>
iex> :crypto.strong_rand_bytes(65536) |> OpenPGP.Util.encode_mpi()
** (RuntimeError) big-endian is too long
@spec public_key_algo_tuple(1..255) :: public_key_algo_tuple()
Convert public-key algorithm ID to a tuple with ID and name binary.
RFC4880 (https://www.ietf.org/rfc/rfc4880.txt)
9.1. Public-Key Algorithms +-----------+----------------------------------------------------+ | ID | Algorithm | +-----------+----------------------------------------------------+ | 1 | RSA (Encrypt or Sign) [HAC] | | 2 | RSA Encrypt-Only [HAC] | | 3 | RSA Sign-Only [HAC] | | 16 | Elgamal (Encrypt-Only) [ELGAMAL] [HAC] | | 17 | DSA (Digital Signature Algorithm) [FIPS186] [HAC] | | 18 | ECDH public key algorithm | | 19 | ECDSA public key algorithm [FIPS186] | | 20 | Reserved (formerly Elgamal Encrypt or Sign) | | 21 | Reserved for Diffie-Hellman | | | (X9.42, as defined for IETF-S/MIME) | | 22 | EdDSA [RFC8032] | | 23 | Reserved for AEDH | | 24 | Reserved for AEDSA | | 100--110 | Private/Experimental algorithm | +-----------+----------------------------------------------------+
@spec sym_algo_cipher_block_size(byte() | sym_algo_tuple()) :: non_neg_integer()
Detects cipher block size (bits) given symmetric encryption algorithm ID or a tuple.
@spec sym_algo_key_size(byte() | sym_algo_tuple()) :: non_neg_integer()
Detects cipher key size (bits) given symmetric encryption algorithm ID or a tuple.
@spec sym_algo_to_crypto_cipher(sym_algo_tuple() | byte()) :: :aes_128_cfb128 | :aes_192_cfb128 | :aes_256_cfb128
@spec sym_algo_tuple(byte()) :: sym_algo_tuple()
Convert symmetric encryption algorithm ID to a tuple with ID and name binary.
RFC4880 (https://www.ietf.org/rfc/rfc4880.txt)
9.3. Symmetric-Key Algorithms +-----------+-----------------------------------------------+ | ID | Algorithm | +-----------+-----------------------------------------------+ | 0 | Plaintext or unencrypted data | | 1 | IDEA [IDEA] | | 2 | TripleDES (DES-EDE, [SCHNEIER] [HAC] | | | - 168 bit key derived from 192) | | 3 | CAST5 (128 bit key, as per [RFC2144]) | | 4 | Blowfish (128 bit key, 16 rounds) [BLOWFISH] | | 5 | Reserved | | 6 | Reserved | | 7 | AES with 128-bit key [AES] | | 8 | AES with 192-bit key | | 9 | AES with 256-bit key | | 10 | Twofish with 256-bit key [TWOFISH] | | 11 | Camellia with 128-bit key [RFC3713] | | 12 | Camellia with 192-bit key | | 13 | Camellia with 256-bit key | | 100--110 | Private/Experimental algorithm | +-----------+-----------------------------------------------+