1e1051a39Sopenharmony_ci=pod
2e1051a39Sopenharmony_ci
3e1051a39Sopenharmony_ci=head1 NAME
4e1051a39Sopenharmony_ci
5e1051a39Sopenharmony_ciEVP_ENCODE_CTX_new, EVP_ENCODE_CTX_free, EVP_ENCODE_CTX_copy,
6e1051a39Sopenharmony_ciEVP_ENCODE_CTX_num, EVP_EncodeInit, EVP_EncodeUpdate, EVP_EncodeFinal,
7e1051a39Sopenharmony_ciEVP_EncodeBlock, EVP_DecodeInit, EVP_DecodeUpdate, EVP_DecodeFinal,
8e1051a39Sopenharmony_ciEVP_DecodeBlock - EVP base 64 encode/decode routines
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci=head1 SYNOPSIS
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ci #include <openssl/evp.h>
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ci EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void);
15e1051a39Sopenharmony_ci void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx);
16e1051a39Sopenharmony_ci int EVP_ENCODE_CTX_copy(EVP_ENCODE_CTX *dctx, EVP_ENCODE_CTX *sctx);
17e1051a39Sopenharmony_ci int EVP_ENCODE_CTX_num(EVP_ENCODE_CTX *ctx);
18e1051a39Sopenharmony_ci void EVP_EncodeInit(EVP_ENCODE_CTX *ctx);
19e1051a39Sopenharmony_ci int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
20e1051a39Sopenharmony_ci                      const unsigned char *in, int inl);
21e1051a39Sopenharmony_ci void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl);
22e1051a39Sopenharmony_ci int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int n);
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ci void EVP_DecodeInit(EVP_ENCODE_CTX *ctx);
25e1051a39Sopenharmony_ci int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
26e1051a39Sopenharmony_ci                      const unsigned char *in, int inl);
27e1051a39Sopenharmony_ci int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl);
28e1051a39Sopenharmony_ci int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n);
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ci=head1 DESCRIPTION
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ciThe EVP encode routines provide a high-level interface to base 64 encoding and
33e1051a39Sopenharmony_cidecoding. Base 64 encoding converts binary data into a printable form that uses
34e1051a39Sopenharmony_cithe characters A-Z, a-z, 0-9, "+" and "/" to represent the data. For every 3
35e1051a39Sopenharmony_cibytes of binary data provided 4 bytes of base 64 encoded data will be produced
36e1051a39Sopenharmony_ciplus some occasional newlines (see below). If the input data length is not a
37e1051a39Sopenharmony_cimultiple of 3 then the output data will be padded at the end using the "="
38e1051a39Sopenharmony_cicharacter.
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_ciEVP_ENCODE_CTX_new() allocates, initializes and returns a context to be used for
41e1051a39Sopenharmony_cithe encode/decode functions.
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ciEVP_ENCODE_CTX_free() cleans up an encode/decode context B<ctx> and frees up the
44e1051a39Sopenharmony_cispace allocated to it.
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ciEncoding of binary data is performed in blocks of 48 input bytes (or less for
47e1051a39Sopenharmony_cithe final block). For each 48 byte input block encoded 64 bytes of base 64 data
48e1051a39Sopenharmony_ciis output plus an additional newline character (i.e. 65 bytes in total). The
49e1051a39Sopenharmony_cifinal block (which may be less than 48 bytes) will output 4 bytes for every 3
50e1051a39Sopenharmony_cibytes of input. If the data length is not divisible by 3 then a full 4 bytes is
51e1051a39Sopenharmony_cistill output for the final 1 or 2 bytes of input. Similarly a newline character
52e1051a39Sopenharmony_ciwill also be output.
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_ciEVP_EncodeInit() initialises B<ctx> for the start of a new encoding operation.
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ciEVP_EncodeUpdate() encode B<inl> bytes of data found in the buffer pointed to by
57e1051a39Sopenharmony_ciB<in>. The output is stored in the buffer B<out> and the number of bytes output
58e1051a39Sopenharmony_ciis stored in B<*outl>. It is the caller's responsibility to ensure that the
59e1051a39Sopenharmony_cibuffer at B<out> is sufficiently large to accommodate the output data. Only full
60e1051a39Sopenharmony_ciblocks of data (48 bytes) will be immediately processed and output by this
61e1051a39Sopenharmony_cifunction. Any remainder is held in the B<ctx> object and will be processed by a
62e1051a39Sopenharmony_cisubsequent call to EVP_EncodeUpdate() or EVP_EncodeFinal(). To calculate the
63e1051a39Sopenharmony_cirequired size of the output buffer add together the value of B<inl> with the
64e1051a39Sopenharmony_ciamount of unprocessed data held in B<ctx> and divide the result by 48 (ignore
65e1051a39Sopenharmony_ciany remainder). This gives the number of blocks of data that will be processed.
66e1051a39Sopenharmony_ciEnsure the output buffer contains 65 bytes of storage for each block, plus an
67e1051a39Sopenharmony_ciadditional byte for a NUL terminator. EVP_EncodeUpdate() may be called
68e1051a39Sopenharmony_cirepeatedly to process large amounts of input data. In the event of an error
69e1051a39Sopenharmony_ciEVP_EncodeUpdate() will set B<*outl> to 0 and return 0. On success 1 will be
70e1051a39Sopenharmony_cireturned.
71e1051a39Sopenharmony_ci
72e1051a39Sopenharmony_ciEVP_EncodeFinal() must be called at the end of an encoding operation. It will
73e1051a39Sopenharmony_ciprocess any partial block of data remaining in the B<ctx> object. The output
74e1051a39Sopenharmony_cidata will be stored in B<out> and the length of the data written will be stored
75e1051a39Sopenharmony_ciin B<*outl>. It is the caller's responsibility to ensure that B<out> is
76e1051a39Sopenharmony_cisufficiently large to accommodate the output data which will never be more than
77e1051a39Sopenharmony_ci65 bytes plus an additional NUL terminator (i.e. 66 bytes in total).
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ciEVP_ENCODE_CTX_copy() can be used to copy a context B<sctx> to a context
80e1051a39Sopenharmony_ciB<dctx>. B<dctx> must be initialized before calling this function.
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ciEVP_ENCODE_CTX_num() will return the number of as yet unprocessed bytes still to
83e1051a39Sopenharmony_cibe encoded or decoded that are pending in the B<ctx> object.
84e1051a39Sopenharmony_ci
85e1051a39Sopenharmony_ciEVP_EncodeBlock() encodes a full block of input data in B<f> and of length
86e1051a39Sopenharmony_ciB<n> and stores it in B<t>. For every 3 bytes of input provided 4 bytes of
87e1051a39Sopenharmony_cioutput data will be produced. If B<n> is not divisible by 3 then the block is
88e1051a39Sopenharmony_ciencoded as a final block of data and the output is padded such that it is always
89e1051a39Sopenharmony_cidivisible by 4. Additionally a NUL terminator character will be added. For
90e1051a39Sopenharmony_ciexample if 16 bytes of input data is provided then 24 bytes of encoded data is
91e1051a39Sopenharmony_cicreated plus 1 byte for a NUL terminator (i.e. 25 bytes in total). The length of
92e1051a39Sopenharmony_cithe data generated I<without> the NUL terminator is returned from the function.
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ciEVP_DecodeInit() initialises B<ctx> for the start of a new decoding operation.
95e1051a39Sopenharmony_ci
96e1051a39Sopenharmony_ciEVP_DecodeUpdate() decodes B<inl> characters of data found in the buffer pointed
97e1051a39Sopenharmony_cito by B<in>. The output is stored in the buffer B<out> and the number of bytes
98e1051a39Sopenharmony_cioutput is stored in B<*outl>. It is the caller's responsibility to ensure that
99e1051a39Sopenharmony_cithe buffer at B<out> is sufficiently large to accommodate the output data. This
100e1051a39Sopenharmony_cifunction will attempt to decode as much data as possible in 4 byte chunks. Any
101e1051a39Sopenharmony_ciwhitespace, newline or carriage return characters are ignored. Any partial chunk
102e1051a39Sopenharmony_ciof unprocessed data (1, 2 or 3 bytes) that remains at the end will be held in
103e1051a39Sopenharmony_cithe B<ctx> object and processed by a subsequent call to EVP_DecodeUpdate(). If
104e1051a39Sopenharmony_ciany illegal base 64 characters are encountered or if the base 64 padding
105e1051a39Sopenharmony_cicharacter "=" is encountered in the middle of the data then the function returns
106e1051a39Sopenharmony_ci-1 to indicate an error. A return value of 0 or 1 indicates successful
107e1051a39Sopenharmony_ciprocessing of the data. A return value of 0 additionally indicates that the last
108e1051a39Sopenharmony_ciinput data characters processed included the base 64 padding character "=" and
109e1051a39Sopenharmony_citherefore no more non-padding character data is expected to be processed. For
110e1051a39Sopenharmony_cievery 4 valid base 64 bytes processed (ignoring whitespace, carriage returns and
111e1051a39Sopenharmony_ciline feeds), 3 bytes of binary output data will be produced (or less at the end
112e1051a39Sopenharmony_ciof the data where the padding character "=" has been used).
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_ciEVP_DecodeFinal() must be called at the end of a decoding operation. If there
115e1051a39Sopenharmony_ciis any unprocessed data still in B<ctx> then the input data must not have been
116e1051a39Sopenharmony_cia multiple of 4 and therefore an error has occurred. The function will return -1
117e1051a39Sopenharmony_ciin this case. Otherwise the function returns 1 on success.
118e1051a39Sopenharmony_ci
119e1051a39Sopenharmony_ciEVP_DecodeBlock() will decode the block of B<n> characters of base 64 data
120e1051a39Sopenharmony_cicontained in B<f> and store the result in B<t>. Any leading whitespace will be
121e1051a39Sopenharmony_citrimmed as will any trailing whitespace, newlines, carriage returns or EOF
122e1051a39Sopenharmony_cicharacters. After such trimming the length of the data in B<f> must be divisible
123e1051a39Sopenharmony_ciby 4. For every 4 input bytes exactly 3 output bytes will be produced. The
124e1051a39Sopenharmony_cioutput will be padded with 0 bits if necessary to ensure that the output is
125e1051a39Sopenharmony_cialways 3 bytes for every 4 input bytes. This function will return the length of
126e1051a39Sopenharmony_cithe data decoded or -1 on error.
127e1051a39Sopenharmony_ci
128e1051a39Sopenharmony_ci=head1 RETURN VALUES
129e1051a39Sopenharmony_ci
130e1051a39Sopenharmony_ciEVP_ENCODE_CTX_new() returns a pointer to the newly allocated EVP_ENCODE_CTX
131e1051a39Sopenharmony_ciobject or NULL on error.
132e1051a39Sopenharmony_ci
133e1051a39Sopenharmony_ciEVP_ENCODE_CTX_num() returns the number of bytes pending encoding or decoding in
134e1051a39Sopenharmony_ciB<ctx>.
135e1051a39Sopenharmony_ci
136e1051a39Sopenharmony_ciEVP_EncodeUpdate() returns 0 on error or 1 on success.
137e1051a39Sopenharmony_ci
138e1051a39Sopenharmony_ciEVP_EncodeBlock() returns the number of bytes encoded excluding the NUL
139e1051a39Sopenharmony_citerminator.
140e1051a39Sopenharmony_ci
141e1051a39Sopenharmony_ciEVP_DecodeUpdate() returns -1 on error and 0 or 1 on success. If 0 is returned
142e1051a39Sopenharmony_cithen no more non-padding base 64 characters are expected.
143e1051a39Sopenharmony_ci
144e1051a39Sopenharmony_ciEVP_DecodeFinal() returns -1 on error or 1 on success.
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ciEVP_DecodeBlock() returns the length of the data decoded or -1 on error.
147e1051a39Sopenharmony_ci
148e1051a39Sopenharmony_ci=head1 SEE ALSO
149e1051a39Sopenharmony_ci
150e1051a39Sopenharmony_ciL<evp(7)>
151e1051a39Sopenharmony_ci
152e1051a39Sopenharmony_ci=head1 COPYRIGHT
153e1051a39Sopenharmony_ci
154e1051a39Sopenharmony_ciCopyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
155e1051a39Sopenharmony_ci
156e1051a39Sopenharmony_ciLicensed under the Apache License 2.0 (the "License").  You may not use
157e1051a39Sopenharmony_cithis file except in compliance with the License.  You can obtain a copy
158e1051a39Sopenharmony_ciin the file LICENSE in the source distribution or at
159e1051a39Sopenharmony_ciL<https://www.openssl.org/source/license.html>.
160e1051a39Sopenharmony_ci
161e1051a39Sopenharmony_ci=cut
162