xref: /freebsd/crypto/openssl/doc/man3/EVP_EncodeInit.pod (revision a7148ab39c03abd4d1a84997c70bf96f15dd2a09)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk KimEVP_ENCODE_CTX_new, EVP_ENCODE_CTX_free, EVP_ENCODE_CTX_copy,
6e71b7053SJung-uk KimEVP_ENCODE_CTX_num, EVP_EncodeInit, EVP_EncodeUpdate, EVP_EncodeFinal,
7e71b7053SJung-uk KimEVP_EncodeBlock, EVP_DecodeInit, EVP_DecodeUpdate, EVP_DecodeFinal,
8e71b7053SJung-uk KimEVP_DecodeBlock - EVP base 64 encode/decode routines
9e71b7053SJung-uk Kim
10e71b7053SJung-uk Kim=head1 SYNOPSIS
11e71b7053SJung-uk Kim
12e71b7053SJung-uk Kim #include <openssl/evp.h>
13e71b7053SJung-uk Kim
14e71b7053SJung-uk Kim EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void);
15e71b7053SJung-uk Kim void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx);
16e71b7053SJung-uk Kim int EVP_ENCODE_CTX_copy(EVP_ENCODE_CTX *dctx, EVP_ENCODE_CTX *sctx);
17e71b7053SJung-uk Kim int EVP_ENCODE_CTX_num(EVP_ENCODE_CTX *ctx);
18e71b7053SJung-uk Kim void EVP_EncodeInit(EVP_ENCODE_CTX *ctx);
19e71b7053SJung-uk Kim int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
20e71b7053SJung-uk Kim                      const unsigned char *in, int inl);
21e71b7053SJung-uk Kim void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl);
22e71b7053SJung-uk Kim int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int n);
23e71b7053SJung-uk Kim
24e71b7053SJung-uk Kim void EVP_DecodeInit(EVP_ENCODE_CTX *ctx);
25e71b7053SJung-uk Kim int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
26e71b7053SJung-uk Kim                      const unsigned char *in, int inl);
27e71b7053SJung-uk Kim int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl);
28e71b7053SJung-uk Kim int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n);
29e71b7053SJung-uk Kim
30e71b7053SJung-uk Kim=head1 DESCRIPTION
31e71b7053SJung-uk Kim
3258f35182SJung-uk KimThe EVP encode routines provide a high-level interface to base 64 encoding and
33e71b7053SJung-uk Kimdecoding. Base 64 encoding converts binary data into a printable form that uses
34e71b7053SJung-uk Kimthe characters A-Z, a-z, 0-9, "+" and "/" to represent the data. For every 3
35e71b7053SJung-uk Kimbytes of binary data provided 4 bytes of base 64 encoded data will be produced
36e71b7053SJung-uk Kimplus some occasional newlines (see below). If the input data length is not a
37e71b7053SJung-uk Kimmultiple of 3 then the output data will be padded at the end using the "="
38e71b7053SJung-uk Kimcharacter.
39e71b7053SJung-uk Kim
40e71b7053SJung-uk KimEVP_ENCODE_CTX_new() allocates, initializes and returns a context to be used for
41e71b7053SJung-uk Kimthe encode/decode functions.
42e71b7053SJung-uk Kim
43e71b7053SJung-uk KimEVP_ENCODE_CTX_free() cleans up an encode/decode context B<ctx> and frees up the
44*a7148ab3SEnji Cooperspace allocated to it. If the argument is NULL, nothing is done.
45e71b7053SJung-uk Kim
46e71b7053SJung-uk KimEncoding of binary data is performed in blocks of 48 input bytes (or less for
47e71b7053SJung-uk Kimthe final block). For each 48 byte input block encoded 64 bytes of base 64 data
48e71b7053SJung-uk Kimis output plus an additional newline character (i.e. 65 bytes in total). The
49e71b7053SJung-uk Kimfinal block (which may be less than 48 bytes) will output 4 bytes for every 3
50e71b7053SJung-uk Kimbytes of input. If the data length is not divisible by 3 then a full 4 bytes is
51e71b7053SJung-uk Kimstill output for the final 1 or 2 bytes of input. Similarly a newline character
52e71b7053SJung-uk Kimwill also be output.
53e71b7053SJung-uk Kim
54e71b7053SJung-uk KimEVP_EncodeInit() initialises B<ctx> for the start of a new encoding operation.
55e71b7053SJung-uk Kim
56e71b7053SJung-uk KimEVP_EncodeUpdate() encode B<inl> bytes of data found in the buffer pointed to by
57e71b7053SJung-uk KimB<in>. The output is stored in the buffer B<out> and the number of bytes output
58e71b7053SJung-uk Kimis stored in B<*outl>. It is the caller's responsibility to ensure that the
59e71b7053SJung-uk Kimbuffer at B<out> is sufficiently large to accommodate the output data. Only full
60e71b7053SJung-uk Kimblocks of data (48 bytes) will be immediately processed and output by this
61e71b7053SJung-uk Kimfunction. Any remainder is held in the B<ctx> object and will be processed by a
62e71b7053SJung-uk Kimsubsequent call to EVP_EncodeUpdate() or EVP_EncodeFinal(). To calculate the
63e71b7053SJung-uk Kimrequired size of the output buffer add together the value of B<inl> with the
64e71b7053SJung-uk Kimamount of unprocessed data held in B<ctx> and divide the result by 48 (ignore
65e71b7053SJung-uk Kimany remainder). This gives the number of blocks of data that will be processed.
66e71b7053SJung-uk KimEnsure the output buffer contains 65 bytes of storage for each block, plus an
67e71b7053SJung-uk Kimadditional byte for a NUL terminator. EVP_EncodeUpdate() may be called
68e71b7053SJung-uk Kimrepeatedly to process large amounts of input data. In the event of an error
69e71b7053SJung-uk KimEVP_EncodeUpdate() will set B<*outl> to 0 and return 0. On success 1 will be
70e71b7053SJung-uk Kimreturned.
71e71b7053SJung-uk Kim
72e71b7053SJung-uk KimEVP_EncodeFinal() must be called at the end of an encoding operation. It will
73e71b7053SJung-uk Kimprocess any partial block of data remaining in the B<ctx> object. The output
74e71b7053SJung-uk Kimdata will be stored in B<out> and the length of the data written will be stored
75e71b7053SJung-uk Kimin B<*outl>. It is the caller's responsibility to ensure that B<out> is
76e71b7053SJung-uk Kimsufficiently large to accommodate the output data which will never be more than
77e71b7053SJung-uk Kim65 bytes plus an additional NUL terminator (i.e. 66 bytes in total).
78e71b7053SJung-uk Kim
79e71b7053SJung-uk KimEVP_ENCODE_CTX_copy() can be used to copy a context B<sctx> to a context
80e71b7053SJung-uk KimB<dctx>. B<dctx> must be initialized before calling this function.
81e71b7053SJung-uk Kim
82e71b7053SJung-uk KimEVP_ENCODE_CTX_num() will return the number of as yet unprocessed bytes still to
83e71b7053SJung-uk Kimbe encoded or decoded that are pending in the B<ctx> object.
84e71b7053SJung-uk Kim
85e71b7053SJung-uk KimEVP_EncodeBlock() encodes a full block of input data in B<f> and of length
8658f35182SJung-uk KimB<n> and stores it in B<t>. For every 3 bytes of input provided 4 bytes of
8758f35182SJung-uk Kimoutput data will be produced. If B<n> is not divisible by 3 then the block is
88e71b7053SJung-uk Kimencoded as a final block of data and the output is padded such that it is always
89e71b7053SJung-uk Kimdivisible by 4. Additionally a NUL terminator character will be added. For
90e71b7053SJung-uk Kimexample if 16 bytes of input data is provided then 24 bytes of encoded data is
91e71b7053SJung-uk Kimcreated plus 1 byte for a NUL terminator (i.e. 25 bytes in total). The length of
92e71b7053SJung-uk Kimthe data generated I<without> the NUL terminator is returned from the function.
93e71b7053SJung-uk Kim
94e71b7053SJung-uk KimEVP_DecodeInit() initialises B<ctx> for the start of a new decoding operation.
95e71b7053SJung-uk Kim
96e71b7053SJung-uk KimEVP_DecodeUpdate() decodes B<inl> characters of data found in the buffer pointed
97e71b7053SJung-uk Kimto by B<in>. The output is stored in the buffer B<out> and the number of bytes
98e71b7053SJung-uk Kimoutput is stored in B<*outl>. It is the caller's responsibility to ensure that
99e71b7053SJung-uk Kimthe buffer at B<out> is sufficiently large to accommodate the output data. This
100e71b7053SJung-uk Kimfunction will attempt to decode as much data as possible in 4 byte chunks. Any
101e71b7053SJung-uk Kimwhitespace, newline or carriage return characters are ignored. Any partial chunk
102e71b7053SJung-uk Kimof unprocessed data (1, 2 or 3 bytes) that remains at the end will be held in
103e71b7053SJung-uk Kimthe B<ctx> object and processed by a subsequent call to EVP_DecodeUpdate(). If
104e71b7053SJung-uk Kimany illegal base 64 characters are encountered or if the base 64 padding
105e71b7053SJung-uk Kimcharacter "=" is encountered in the middle of the data then the function returns
106e71b7053SJung-uk Kim-1 to indicate an error. A return value of 0 or 1 indicates successful
107e71b7053SJung-uk Kimprocessing of the data. A return value of 0 additionally indicates that the last
108e71b7053SJung-uk Kiminput data characters processed included the base 64 padding character "=" and
109e71b7053SJung-uk Kimtherefore no more non-padding character data is expected to be processed. For
110e71b7053SJung-uk Kimevery 4 valid base 64 bytes processed (ignoring whitespace, carriage returns and
111e71b7053SJung-uk Kimline feeds), 3 bytes of binary output data will be produced (or less at the end
112e71b7053SJung-uk Kimof the data where the padding character "=" has been used).
113e71b7053SJung-uk Kim
114e71b7053SJung-uk KimEVP_DecodeFinal() must be called at the end of a decoding operation. If there
115e71b7053SJung-uk Kimis any unprocessed data still in B<ctx> then the input data must not have been
116e71b7053SJung-uk Kima multiple of 4 and therefore an error has occurred. The function will return -1
117e71b7053SJung-uk Kimin this case. Otherwise the function returns 1 on success.
118e71b7053SJung-uk Kim
119e71b7053SJung-uk KimEVP_DecodeBlock() will decode the block of B<n> characters of base 64 data
120e71b7053SJung-uk Kimcontained in B<f> and store the result in B<t>. Any leading whitespace will be
121e71b7053SJung-uk Kimtrimmed as will any trailing whitespace, newlines, carriage returns or EOF
122e71b7053SJung-uk Kimcharacters. After such trimming the length of the data in B<f> must be divisible
123e71b7053SJung-uk Kimby 4. For every 4 input bytes exactly 3 output bytes will be produced. The
124e71b7053SJung-uk Kimoutput will be padded with 0 bits if necessary to ensure that the output is
125e71b7053SJung-uk Kimalways 3 bytes for every 4 input bytes. This function will return the length of
126e71b7053SJung-uk Kimthe data decoded or -1 on error.
127e71b7053SJung-uk Kim
128e71b7053SJung-uk Kim=head1 RETURN VALUES
129e71b7053SJung-uk Kim
130e71b7053SJung-uk KimEVP_ENCODE_CTX_new() returns a pointer to the newly allocated EVP_ENCODE_CTX
131e71b7053SJung-uk Kimobject or NULL on error.
132e71b7053SJung-uk Kim
133e71b7053SJung-uk KimEVP_ENCODE_CTX_num() returns the number of bytes pending encoding or decoding in
134e71b7053SJung-uk KimB<ctx>.
135e71b7053SJung-uk Kim
136e71b7053SJung-uk KimEVP_EncodeUpdate() returns 0 on error or 1 on success.
137e71b7053SJung-uk Kim
138e71b7053SJung-uk KimEVP_EncodeBlock() returns the number of bytes encoded excluding the NUL
139e71b7053SJung-uk Kimterminator.
140e71b7053SJung-uk Kim
141e71b7053SJung-uk KimEVP_DecodeUpdate() returns -1 on error and 0 or 1 on success. If 0 is returned
142e71b7053SJung-uk Kimthen no more non-padding base 64 characters are expected.
143e71b7053SJung-uk Kim
144e71b7053SJung-uk KimEVP_DecodeFinal() returns -1 on error or 1 on success.
145e71b7053SJung-uk Kim
146e71b7053SJung-uk KimEVP_DecodeBlock() returns the length of the data decoded or -1 on error.
147e71b7053SJung-uk Kim
148e71b7053SJung-uk Kim=head1 SEE ALSO
149e71b7053SJung-uk Kim
150e71b7053SJung-uk KimL<evp(7)>
151e71b7053SJung-uk Kim
152e71b7053SJung-uk Kim=head1 COPYRIGHT
153e71b7053SJung-uk Kim
154*a7148ab3SEnji CooperCopyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
155e71b7053SJung-uk Kim
156b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
157e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
158e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
159e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
160e71b7053SJung-uk Kim
161e71b7053SJung-uk Kim=cut
162