1EVP_ENCODEINIT(3) OpenSSL EVP_ENCODEINIT(3)
2
3
4
6 EVP_ENCODE_CTX_new, EVP_ENCODE_CTX_free, EVP_ENCODE_CTX_copy,
7 EVP_ENCODE_CTX_num, EVP_EncodeInit, EVP_EncodeUpdate, EVP_EncodeFinal,
8 EVP_EncodeBlock, EVP_DecodeInit, EVP_DecodeUpdate, EVP_DecodeFinal,
9 EVP_DecodeBlock - EVP base 64 encode/decode routines
10
12 #include <openssl/evp.h>
13
14 EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void);
15 void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx);
16 int EVP_ENCODE_CTX_copy(EVP_ENCODE_CTX *dctx, EVP_ENCODE_CTX *sctx);
17 int EVP_ENCODE_CTX_num(EVP_ENCODE_CTX *ctx);
18 void EVP_EncodeInit(EVP_ENCODE_CTX *ctx);
19 int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
20 const unsigned char *in, int inl);
21 void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl);
22 int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int n);
23
24 void EVP_DecodeInit(EVP_ENCODE_CTX *ctx);
25 int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
26 const unsigned char *in, int inl);
27 int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl);
28 int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n);
29
31 The EVP encode routines provide a high-level interface to base 64
32 encoding and decoding. Base 64 encoding converts binary data into a
33 printable form that uses the characters A-Z, a-z, 0-9, "+" and "/" to
34 represent the data. For every 3 bytes of binary data provided 4 bytes
35 of base 64 encoded data will be produced plus some occasional newlines
36 (see below). If the input data length is not a multiple of 3 then the
37 output data will be padded at the end using the "=" character.
38
39 EVP_ENCODE_CTX_new() allocates, initializes and returns a context to be
40 used for the encode/decode functions.
41
42 EVP_ENCODE_CTX_free() cleans up an encode/decode context ctx and frees
43 up the space allocated to it.
44
45 Encoding of binary data is performed in blocks of 48 input bytes (or
46 less for the final block). For each 48 byte input block encoded 64
47 bytes of base 64 data is output plus an additional newline character
48 (i.e. 65 bytes in total). The final block (which may be less than 48
49 bytes) will output 4 bytes for every 3 bytes of input. If the data
50 length is not divisible by 3 then a full 4 bytes is still output for
51 the final 1 or 2 bytes of input. Similarly a newline character will
52 also be output.
53
54 EVP_EncodeInit() initialises ctx for the start of a new encoding
55 operation.
56
57 EVP_EncodeUpdate() encode inl bytes of data found in the buffer pointed
58 to by in. The output is stored in the buffer out and the number of
59 bytes output is stored in *outl. It is the caller's responsibility to
60 ensure that the buffer at out is sufficiently large to accommodate the
61 output data. Only full blocks of data (48 bytes) will be immediately
62 processed and output by this function. Any remainder is held in the ctx
63 object and will be processed by a subsequent call to EVP_EncodeUpdate()
64 or EVP_EncodeFinal(). To calculate the required size of the output
65 buffer add together the value of inl with the amount of unprocessed
66 data held in ctx and divide the result by 48 (ignore any remainder).
67 This gives the number of blocks of data that will be processed. Ensure
68 the output buffer contains 65 bytes of storage for each block, plus an
69 additional byte for a NUL terminator. EVP_EncodeUpdate() may be called
70 repeatedly to process large amounts of input data. In the event of an
71 error EVP_EncodeUpdate() will set *outl to 0 and return 0. On success 1
72 will be returned.
73
74 EVP_EncodeFinal() must be called at the end of an encoding operation.
75 It will process any partial block of data remaining in the ctx object.
76 The output data will be stored in out and the length of the data
77 written will be stored in *outl. It is the caller's responsibility to
78 ensure that out is sufficiently large to accommodate the output data
79 which will never be more than 65 bytes plus an additional NUL
80 terminator (i.e. 66 bytes in total).
81
82 EVP_ENCODE_CTX_copy() can be used to copy a context sctx to a context
83 dctx. dctx must be initialized before calling this function.
84
85 EVP_ENCODE_CTX_num() will return the number of as yet unprocessed bytes
86 still to be encoded or decoded that are pending in the ctx object.
87
88 EVP_EncodeBlock() encodes a full block of input data in f and of length
89 n and stores it in t. For every 3 bytes of input provided 4 bytes of
90 output data will be produced. If n is not divisible by 3 then the block
91 is encoded as a final block of data and the output is padded such that
92 it is always divisible by 4. Additionally a NUL terminator character
93 will be added. For example if 16 bytes of input data is provided then
94 24 bytes of encoded data is created plus 1 byte for a NUL terminator
95 (i.e. 25 bytes in total). The length of the data generated without the
96 NUL terminator is returned from the function.
97
98 EVP_DecodeInit() initialises ctx for the start of a new decoding
99 operation.
100
101 EVP_DecodeUpdate() decodes inl characters of data found in the buffer
102 pointed to by in. The output is stored in the buffer out and the number
103 of bytes output is stored in *outl. It is the caller's responsibility
104 to ensure that the buffer at out is sufficiently large to accommodate
105 the output data. This function will attempt to decode as much data as
106 possible in 4 byte chunks. Any whitespace, newline or carriage return
107 characters are ignored. Any partial chunk of unprocessed data (1, 2 or
108 3 bytes) that remains at the end will be held in the ctx object and
109 processed by a subsequent call to EVP_DecodeUpdate(). If any illegal
110 base 64 characters are encountered or if the base 64 padding character
111 "=" is encountered in the middle of the data then the function returns
112 -1 to indicate an error. A return value of 0 or 1 indicates successful
113 processing of the data. A return value of 0 additionally indicates that
114 the last input data characters processed included the base 64 padding
115 character "=" and therefore no more non-padding character data is
116 expected to be processed. For every 4 valid base 64 bytes processed
117 (ignoring whitespace, carriage returns and line feeds), 3 bytes of
118 binary output data will be produced (or less at the end of the data
119 where the padding character "=" has been used).
120
121 EVP_DecodeFinal() must be called at the end of a decoding operation. If
122 there is any unprocessed data still in ctx then the input data must not
123 have been a multiple of 4 and therefore an error has occurred. The
124 function will return -1 in this case. Otherwise the function returns 1
125 on success.
126
127 EVP_DecodeBlock() will decode the block of n characters of base 64 data
128 contained in f and store the result in t. Any leading whitespace will
129 be trimmed as will any trailing whitespace, newlines, carriage returns
130 or EOF characters. After such trimming the length of the data in f must
131 be divisible by 4. For every 4 input bytes exactly 3 output bytes will
132 be produced. The output will be padded with 0 bits if necessary to
133 ensure that the output is always 3 bytes for every 4 input bytes. This
134 function will return the length of the data decoded or -1 on error.
135
137 EVP_ENCODE_CTX_new() returns a pointer to the newly allocated
138 EVP_ENCODE_CTX object or NULL on error.
139
140 EVP_ENCODE_CTX_num() returns the number of bytes pending encoding or
141 decoding in ctx.
142
143 EVP_EncodeUpdate() returns 0 on error or 1 on success.
144
145 EVP_EncodeBlock() returns the number of bytes encoded excluding the NUL
146 terminator.
147
148 EVP_DecodeUpdate() returns -1 on error and 0 or 1 on success. If 0 is
149 returned then no more non-padding base 64 characters are expected.
150
151 EVP_DecodeFinal() returns -1 on error or 1 on success.
152
153 EVP_DecodeBlock() returns the length of the data decoded or -1 on
154 error.
155
157 evp(7)
158
160 Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
161
162 Licensed under the OpenSSL license (the "License"). You may not use
163 this file except in compliance with the License. You can obtain a copy
164 in the file LICENSE in the source distribution or at
165 <https://www.openssl.org/source/license.html>.
166
167
168
1691.1.1q 2023-07-20 EVP_ENCODEINIT(3)