EVP_ENCODEINIT(3ossl)
β¨ EVP_ENCODEINIT β¨
okay so bestie, you found it: this is the EVP_ENCODEINIT(3ossl) page straight out of the OpenSSL manual, and it's literally the whole base64 encode/decode family in one place. grab a snack, we're going through every single function π
πΈ NAME πΈ
EVP_ENCODE_CTX_new, EVP_ENCODE_CTX_free, EVP_ENCODE_CTX_copy, EVP_ENCODE_CTX_num, EVP_EncodeInit, EVP_EncodeUpdate, EVP_EncodeFinal, EVP_EncodeBlock, EVP_DecodeInit, EVP_DecodeUpdate, EVP_DecodeFinal, EVP_DecodeBlock β the EVP base64 encode/decode routines. that's the whole squad π«Ά
π¦ SYNOPSIS π¦
#include <openssl/evp.h>
EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void);
void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx);
int EVP_ENCODE_CTX_copy(EVP_ENCODE_CTX *dctx, EVP_ENCODE_CTX *sctx);
int EVP_ENCODE_CTX_num(EVP_ENCODE_CTX *ctx);
void EVP_EncodeInit(EVP_ENCODE_CTX *ctx);
int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
const unsigned char *in, int inl);
void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl);
int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int n);
void EVP_DecodeInit(EVP_ENCODE_CTX *ctx);
int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
const unsigned char *in, int inl);
int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl);
int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n);
π DESCRIPTION π
so the EVP encode routines are like, a high-level interface to base64 encoding and decoding β the glow-up wrapper so you don't have to do the bit math yourself. base64 encoding converts binary data into a printable form that uses the characters A-Z, a-z, 0-9, + and / to represent the data. for every 3 bytes of binary data provided, 4 bytes of base64 encoded data will be produced, plus some occasional newlines (see below, iykyk). if the input data length is not a multiple of 3 then the output data will be padded at the end using the = character. cute, iconic, standard π
π the setup functions
EVP_ENCODE_CTX_new()β allocates, initializes and returns a context to be used for the encode/decode functions. it's giving main character introduction.EVP_ENCODE_CTX_free()β cleans up an encode/decode context ctx and frees up the space allocated to it. if the argument isNULL, nothing is done. no drama, no crash, we love a graceful exit π«Ά
π encoding: how the bytes actually move
encoding of binary data is performed in blocks of 48 input bytes (or less for the final block). for each 48 byte input block encoded, 64 bytes of base64 data is output plus an additional newline character β that's 65 bytes in total, bestie, do the math β¨
the final block (which may be less than 48 bytes) will output 4 bytes for every 3 bytes of input. if the data length is not divisible by 3 then a full 4 bytes is still output for the final 1 or 2 bytes of input. similarly a newline character will also be output. no crumbs left behind.
EVP_EncodeInit()β initialises ctx for the start of a new encoding operation. fresh start, clean slate, new era πEVP_EncodeUpdate()β encodes inl bytes of data found in the buffer pointed to by in. the output is stored in the buffer out and the number of bytes output is stored in*outl. it is the caller's responsibility to ensure that the buffer at out is sufficiently large to accommodate the output data. only full blocks of data (48 bytes) will be immediately processed and output by this function. any remainder is held in the ctx object and will be processed by a subsequent call toEVP_EncodeUpdate()orEVP_EncodeFinal().
to calculate the required size of the output buffer, add together the value of inl with the amount of unprocessed data held in ctx and divide the result by 48 (ignore any remainder). this gives you the number of blocks of data that will be processed. make sure the output buffer contains 65 bytes of storage for each block, plus an additional byte for a NUL terminator. π
EVP_EncodeUpdate() may be called repeatedly to process large amounts of input data. in the event of an error EVP_EncodeUpdate() will set *outl to 0 and return 0. on success 1 will be returned. slay.
EVP_EncodeFinal()β must be called at the end of an encoding operation. it will process any partial block of data remaining in the ctx object. the output data will be stored in out and the length of the data written will be stored in*outl. it is the caller's responsibility to ensure that out is sufficiently large to accommodate the output data, which will never be more than 65 bytes plus an additional NUL terminator (i.e. 66 bytes in total). periodt.
π¦ context housekeeping
EVP_ENCODE_CTX_copy()β can be used to copy a context sctx to a context dctx. dctx must be initialized before calling this function. don't skip the init, that's the whole vibe check π¨EVP_ENCODE_CTX_num()β will return the number of as yet unprocessed bytes still to be encoded or decoded that are pending in the ctx object. it's like checking how many messages you left on read πEVP_EncodeBlock()β encodes a full block of input data in f and of length n and stores it in t. for every 3 bytes of input provided, 4 bytes of output data will be produced. if n is not divisible by 3 then the block is encoded as a final block of data and the output is padded such that it is always divisible by 4. additionally a NUL terminator character will be added.
for example, if 16 bytes of input data is provided then 24 bytes of encoded data is created plus 1 byte for a NUL terminator (i.e. 25 bytes in total). π
the length of the data generated without the NUL terminator is returned from the function.
π§ decoding: the reverse glow-up
EVP_DecodeInit()β initialises ctx for the start of a new decoding operation.EVP_DecodeUpdate()β decodes inl characters of data found in the buffer pointed to by in. the output is stored in the buffer out and the number of bytes output is stored in*outl. it is the caller's responsibility to ensure that the buffer at out is sufficiently large to accommodate the output data. this function will attempt to decode as much data as possible in chunks of up to 80 base64 characters at a time. residual input shorter than the internal chunk size will be buffered in ctx if its length is not a multiple of 4 (including any padding), to be processed in future calls toEVP_DecodeUpdate()orEVP_DecodeFinal(). if the final chunk length is a multiple of 4, it is decoded immediately and not buffered. no cap, that's just the rules π
any whitespace, newline or carriage return characters are ignored. for compatibility with PEM, the - (hyphen) character is treated as a soft end-of-input, subsequent bytes are not buffered, and the return value will be 0 to indicate that the end of the base64 input has been detected.
β οΈ the soft end-of-input, if present, MUST occur after a multiple of 4 valid base64 input bytes. the soft end-of-input condition is not remembered in ctx, it is up to the caller to avoid further calls to EVP_DecodeUpdate() after a 0 or negative (error) return. like, it will not remember for you, bestie, that's on you π
if any invalid base64 characters are encountered or if the base64 padding character (=) is encountered in the middle of the data then EVP_DecodeUpdate() returns -1 to indicate an error. a return value of 0 or 1 indicates successful processing of the data. a return value of 0 additionally indicates that the last 4 bytes processed ended with base64 padding (=), or that the next 4 byte group starts with the soft end-of-input (-) character, and therefore no more input data is expected to be processed. π
for every 4 valid base64 bytes processed (ignoring whitespace, carriage returns and line feeds), 3 bytes of binary output data will be produced (except at the end of data terminated with one or two padding characters). math is math.
EVP_DecodeFinal()β should be called at the end of a decoding operation, but it will never decode additional data. if there is no residual data it will return 1 to indicate success. if there is residual data, its length is not a multiple of 4, i.e. it was not properly padded, -1 is returned in that case to indicate an error. so basically: leftovers = red flag π©EVP_DecodeBlock()β will decode the block of n characters of base64 data contained in f and store the result in t. any leading whitespace will be trimmed, as will any trailing whitespace, newlines, carriage returns or EOF characters.
π¨ internal whitespace MUST NOT be present. after trimming, the data in f MUST consist entirely of valid base64 characters or padding (only at the tail of the input) and its length MUST be divisible by 4. those are hard requirements, not suggestions.
for every 4 input bytes exactly 3 output bytes will be produced. padding bytes (=) (even if internal) are decoded to 6 zero bits, so the caller is responsible for taking trailing padding into account, by ignoring as many bytes at the tail of the returned output. EVP_DecodeBlock() will return the length of the data decoded or -1 on error. π
β RETURN VALUES β
EVP_ENCODE_CTX_new()β returns a pointer to the newly allocatedEVP_ENCODE_CTXobject orNULLon error.EVP_ENCODE_CTX_num()β returns the number of bytes pending encoding or decoding in ctx.EVP_EncodeUpdate()β returns 0 on error or 1 on success.EVP_EncodeBlock()β returns the number of bytes encoded excluding the NUL terminator.EVP_DecodeUpdate()β returns -1 on error and 0 or 1 on success. if 0 is returned then no more non-padding base64 characters are expected.EVP_DecodeFinal()β returns -1 on error or 1 on success.EVP_DecodeBlock()β returns the length of the data decoded or -1 on error.
π SEE ALSO π
evp(7)β go read that one next, it's the lore π¦
π HISTORY π
the EVP_DecodeUpdate() function was fixed in OpenSSL 3.5, so now it produces the number of bytes specified in outl* and does not decode padding bytes (=) to 6 zero bits. glow-up complete, we stan a bugfix β¨
π COPYRIGHT π
Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
and that's the page!! brought to you by OpenSSL 3.5.0, dated 2026-09-25 β mwah, go encode something gorgeous π π
π¦ where this page comes from
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms