blob: cc460471daf38cd7353ab8a197d68a239efa6043 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file base64.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief RFC 1521 base64 encoding/decoding
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_BASE64_H
11#define MBEDTLS_BASE64_H
Paul Bakker5121ce52009-01-03 21:22:43 +000012
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050013#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010014#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050015#else
16#include MBEDTLS_CONFIG_FILE
17#endif
18
Rich Evans00ab4702015-02-06 13:43:58 +000019#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000020
Gilles Peskinea3974432021-07-26 18:48:10 +020021/** Output buffer too small. */
22#define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A
23/** Invalid character in input. */
24#define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C
Paul Bakker5121ce52009-01-03 21:22:43 +000025
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/**
31 * \brief Encode a buffer into base64 format
32 *
33 * \param dst destination buffer
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010034 * \param dlen size of the destination buffer
35 * \param olen number of bytes written
Paul Bakker5121ce52009-01-03 21:22:43 +000036 * \param src source buffer
37 * \param slen amount of data to be encoded
38 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 * \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL.
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010040 * *olen is always updated to reflect the amount
Paul Bakker5121ce52009-01-03 21:22:43 +000041 * of data that has (or would have) been written.
Manuel Pégourié-Gonnard0aa45c22015-09-30 16:30:28 +020042 * If that length cannot be represented, then no data is
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010043 * written to the buffer and *olen is set to the maximum
44 * length representable as a size_t.
Paul Bakker5121ce52009-01-03 21:22:43 +000045 *
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010046 * \note Call this function with dlen = 0 to obtain the
47 * required buffer size in *olen
Paul Bakker5121ce52009-01-03 21:22:43 +000048 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
50 const unsigned char *src, size_t slen);
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52/**
53 * \brief Decode a base64-formatted buffer
54 *
Paul Bakkerf4a14272013-07-05 10:29:12 +020055 * \param dst destination buffer (can be NULL for checking size)
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010056 * \param dlen size of the destination buffer
57 * \param olen number of bytes written
Paul Bakker5121ce52009-01-03 21:22:43 +000058 * \param src source buffer
59 * \param slen amount of data to be decoded
60 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 * \return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or
62 * MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010063 * not correct. *olen is always updated to reflect the amount
Paul Bakker5121ce52009-01-03 21:22:43 +000064 * of data that has (or would have) been written.
65 *
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010066 * \note Call this function with *dst = NULL or dlen = 0 to obtain
67 * the required buffer size in *olen
Paul Bakker5121ce52009-01-03 21:22:43 +000068 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,
70 const unsigned char *src, size_t slen);
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +000073/**
74 * \brief Checkup routine
75 *
76 * \return 0 if successful, or 1 if the test failed
77 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078int mbedtls_base64_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080#endif /* MBEDTLS_SELF_TEST */
81
Paul Bakker5121ce52009-01-03 21:22:43 +000082#ifdef __cplusplus
83}
84#endif
85
86#endif /* base64.h */