EVP_ENCODEINIT(3ossl)

πŸ’– EVP_ENCODEINIT πŸ’–

okay bestie, pull up a chair, because today we're talking about the EVP base64 encode/decode routines from the OpenSSL manual, section 3ossl. this is the EVP_ENCODEINIT(3ossl) page and it's honestly giving ✨utility✨. no cap.


✨ 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, aka your whole base64 glow-up 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 crew is basically a high-level interface to base64 encoding and decoding. and base64? it's the girl who takes your binary data and turns it into something printable, using the characters A-Z, a-z, 0-9, "+" and "/" to represent the data. for every 3 bytes of binary data you hand over, 4 bytes of base64 encoded data come back, plus the occasional newline (more on that in a sec). and if your input data length isn't a multiple of 3, the output gets padded at the end with the "=" character. cute, right? πŸ’…

EVP_ENCODE_CTX_new() allocates, initializes and returns a context, and that context is what all the encode/decode functions are gonna be using.

EVP_ENCODE_CTX_free() cleans up an encode/decode context ctx and frees up the space allocated to it. if the argument is NULL? nothing happens, no drama, she just moves on. πŸ™ˆ


🌈 encoding, but make it blocks 🌈

encoding of binary data happens in blocks of 48 input bytes (or less for the final block, bestie, she's flexible). for each 48 byte input block you get 64 bytes of base64 data output plus an additional newline character β€” that's 65 bytes in total, 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 isn't divisible by 3 then a full 4 bytes is still output for the final 1 or 2 bytes of input. and similarly, a newline character will also be output. she's consistent like that. ✨

EVP_EncodeInit() initialises ctx for the start of a new encoding operation. fresh start, clean slate, main character energy.

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 to EVP_EncodeUpdate() or EVP_EncodeFinal().

so how do you size that buffer, bestie? add together the value of inl with the amount of unprocessed data held in ctx and divide the result by 48 (ignore any remainder). that gives you the number of blocks of data that will be processed. then make sure your 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 β€” like, mandatory, don't ghost her. 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).

EVP_ENCODE_CTX_copy() can be used to copy a context sctx to a context dctx. dctx must be initialized before calling this function. no shortcuts, babe. πŸŽ€

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. she's keeping receipts. 🧾

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. got it? got it. πŸ’–


πŸŽ€ decoding, the sequel πŸŽ€

EVP_DecodeInit() initialises ctx for the start of a new decoding operation. new era, new decode.

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 to EVP_DecodeUpdate() or EVP_DecodeFinal(). if the final chunk length is a multiple of 4, it is decoded immediately and not buffered. she doesn't hold onto what she doesn't need. πŸ’…

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. no cap, 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. πŸ’€ (in a good way)

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. she said what she said. 🫢

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. for every 4 input bytes exactly 3 output bytes will be produced. padding bytes (=) (even if internal) are decoded to 6 zero bits, and 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 allocated EVP_ENCODE_CTX object or NULL on 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 her too, she's iconic.


⭐ 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 confirmed. ✨


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 whole page, bestie!! this was version 3.5.9, dated 2026-09-29, and it's the EVP_ENCODEINIT(3ossl) page from the OpenSSL manual. go forth and base64 responsibly πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

πŸ“¦ where this page comes from