mirror of
https://github.com/jedisct1/libsodium.git
synced 2026-08-25 08:37:13 +09:00
Call sodium_misuse on ridiculously high base64 input lengths
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
#ifndef sodium_utils_H
|
||||
#define sodium_utils_H
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "export.h"
|
||||
|
||||
@@ -74,11 +76,16 @@ int sodium_hex2bin(unsigned char *const bin, const size_t bin_maxlen, const char
|
||||
* Computes the required length to encode BIN_LEN bytes as a base64 string
|
||||
* using the given variant. The computed length includes a trailing \0.
|
||||
*/
|
||||
#define sodium_base64_ENCODED_LEN(BIN_LEN, VARIANT) \
|
||||
(((BIN_LEN) / 3U) * 4U + \
|
||||
((((BIN_LEN) - ((BIN_LEN) / 3U) * 3U) | (((BIN_LEN) - ((BIN_LEN) / 3U) * 3U) >> 1)) & 1U) * \
|
||||
(4U - ((0U - (((VARIANT) & 2U) >> 1)) & (3U - ((BIN_LEN) - ((BIN_LEN) / 3U) * 3U)))) + \
|
||||
1U)
|
||||
#define sodium_base64_ENCODED_LEN(BIN_LEN, VARIANT) \
|
||||
((BIN_LEN) / 3U > (SIZE_MAX - 5) / 4U \
|
||||
? (size_t)SIZE_MAX \
|
||||
: (((BIN_LEN) / 3U) * 4U + \
|
||||
((((BIN_LEN) - ((BIN_LEN) / 3U) * 3U) | \
|
||||
(((BIN_LEN) - ((BIN_LEN) / 3U) * 3U) >> 1)) & \
|
||||
1U) * \
|
||||
(4U - ((0U - (((VARIANT) & 2U) >> 1)) & \
|
||||
(3U - ((BIN_LEN) - ((BIN_LEN) / 3U) * 3U)))) + \
|
||||
1U))
|
||||
|
||||
SODIUM_EXPORT
|
||||
size_t sodium_base64_encoded_len(const size_t bin_len, const int variant);
|
||||
|
||||
@@ -175,6 +175,9 @@ sodium_base64_encoded_len(const size_t bin_len, const int variant)
|
||||
{
|
||||
sodium_base64_check_variant(variant);
|
||||
|
||||
if (bin_len / 3 > (SIZE_MAX - 5) / 4) {
|
||||
sodium_misuse(); /* LCOV_EXCL_LINE */
|
||||
}
|
||||
return sodium_base64_ENCODED_LEN(bin_len, variant);
|
||||
}
|
||||
|
||||
@@ -193,6 +196,9 @@ sodium_bin2base64(char * const b64, const size_t b64_maxlen,
|
||||
|
||||
sodium_base64_check_variant(variant);
|
||||
nibbles = bin_len / 3;
|
||||
if (nibbles > (SIZE_MAX - 5) / 4) {
|
||||
sodium_misuse(); /* LCOV_EXCL_LINE */
|
||||
}
|
||||
remainder = bin_len - 3 * nibbles;
|
||||
b64_len = nibbles * 4;
|
||||
if (remainder != 0) {
|
||||
|
||||
Reference in New Issue
Block a user