Base64/PEM

Module Functionality
Provides an encoder and decoder for Base64 (sextet) encoding. PEM is simply a Base64 encoding of DER-encoded data wrapped with banners indicating the type of object that is being encoded. PEM is another encoding format common to cryptography.

Sextet encoding means that single byte of data encodes six (6) bits of information. Conversion involves parsing an octet-encoded (a single byte of data encodes eight (8) bytes of information) stream of data 6 bits at a time, then mapping the resulting value to one of 64 (hence base64) printable characters or the padding character =. Reversing this is done by doing these steps in reverse.

You may recognize the following in some files or data dumps you may have seen before. This is PEM.

-----BEGIN RSA PUBLIC KEY-----
A couple lines of
Base64-encoded data
that contain the RSA
public key
-----END RSA PUBLIC KEY-----

Macros

cryptx_base64_get_encoded_len(len)

Defines a macro to return the expected base64-encoded data length, given octet-encoded len.

This should be len * 8 / 6.

cryptx_base64_get_decoded_len(len)

Defines a macro to return the expected octet-encoded data length, given base64-encoded len.

This should be len * 6 / 8.

Functions

size_t cryptx_base64_encode(void *dest, const void *src, size_t len)

Converts an octet-encoded byte stream into a sextet-encoded byte stream.

Parameters
  • dest – Pointer to output sextet-encoded data stream.

  • src – Pointer to input octet-encoded data stream.

  • len – Length of octet-encoded data stream.

Returns

Length of output sextet.

size_t cryptx_base64_decode(void *dest, const void *src, size_t len)

Converts a sextet-encoded byte stream into a octet-encoded byte stream.

Parameters
  • dest – Pointer to output octet-encoded data stream.

  • src – Pointer to input sextet-encoded data stream.

  • len – Length of sextet-encoded data stream.

Returns

Length of output octet.