Skip to main content
  • Home
  • login
  • Browse the archive

    swh mirror partner logo
swh logo
SoftwareHeritage
Software
Heritage
Mirror
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

swh:1:snp:dc2a5002442a00b1c0eda7c65d04ea7455e166cd
  • Code
  • Branches (204)
  • Releases (207)
    • Branches
    • Releases
    • HEAD
    • refs/heads/OpenSSL-engine-0_9_6-stable
    • refs/heads/OpenSSL-fips-0_9_7-stable
    • refs/heads/OpenSSL-fips-0_9_8-stable
    • refs/heads/OpenSSL-fips-1_2-stable
    • refs/heads/OpenSSL-fips-2_0-dev
    • refs/heads/OpenSSL-fips-2_0-stable
    • refs/heads/OpenSSL-fips2-0_9_7-stable
    • refs/heads/OpenSSL_0_9_6-stable
    • refs/heads/OpenSSL_0_9_7-stable
    • refs/heads/OpenSSL_0_9_8-stable
    • refs/heads/OpenSSL_0_9_8fg-stable
    • refs/heads/OpenSSL_1_0_0-stable
    • refs/heads/OpenSSL_1_0_1-stable
    • refs/heads/OpenSSL_1_0_2-stable
    • refs/heads/OpenSSL_1_1_0-stable
    • refs/heads/OpenSSL_1_1_1-stable
    • refs/heads/SSLeay
    • refs/heads/feature/dtls-1.3
    • refs/heads/feature/ech
    • refs/heads/feature/quic-server
    • refs/heads/master
    • refs/heads/openssl-3.0
    • refs/heads/openssl-3.1
    • refs/heads/openssl-3.2
    • refs/heads/openssl-3.3
    • refs/heads/openssl-3.4
    • refs/heads/tls1.3-draft-18
    • refs/heads/tls1.3-draft-19
    • refs/tags/AFTER_COMPAQ_PATCH
    • refs/tags/BEFORE_COMPAQ_PATCH
    • refs/tags/BEFORE_engine
    • refs/tags/BEN_FIPS_TEST_1
    • refs/tags/BEN_FIPS_TEST_2
    • refs/tags/BEN_FIPS_TEST_3
    • refs/tags/BEN_FIPS_TEST_4
    • refs/tags/BEN_FIPS_TEST_5
    • refs/tags/BEN_FIPS_TEST_6
    • refs/tags/BEN_FIPS_TEST_7
    • refs/tags/BEN_FIPS_TEST_8
    • refs/tags/FIPS_098_TEST_1
    • refs/tags/FIPS_098_TEST_2
    • refs/tags/FIPS_098_TEST_3
    • refs/tags/FIPS_098_TEST_4
    • refs/tags/FIPS_098_TEST_5
    • refs/tags/FIPS_098_TEST_6
    • refs/tags/FIPS_098_TEST_7
    • refs/tags/FIPS_098_TEST_8
    • refs/tags/FIPS_TEST_10
    • refs/tags/FIPS_TEST_9
    • refs/tags/LEVITTE_after_const
    • refs/tags/LEVITTE_before_const
    • refs/tags/OpenSSL-engine-0_9_6
    • refs/tags/OpenSSL-engine-0_9_6-beta1
    • refs/tags/OpenSSL-engine-0_9_6-beta2
    • refs/tags/OpenSSL-engine-0_9_6-beta3
    • refs/tags/OpenSSL-engine-0_9_6a
    • refs/tags/OpenSSL-engine-0_9_6a-beta1
    • refs/tags/OpenSSL-engine-0_9_6a-beta2
    • refs/tags/OpenSSL-engine-0_9_6a-beta3
    • refs/tags/OpenSSL-engine-0_9_6b
    • refs/tags/OpenSSL-engine-0_9_6c
    • refs/tags/OpenSSL-engine-0_9_6d
    • refs/tags/OpenSSL-engine-0_9_6d-beta1
    • refs/tags/OpenSSL-engine-0_9_6e
    • refs/tags/OpenSSL-engine-0_9_6f
    • refs/tags/OpenSSL-engine-0_9_6g
    • refs/tags/OpenSSL-engine-0_9_6h
    • refs/tags/OpenSSL-engine-0_9_6i
    • refs/tags/OpenSSL-engine-0_9_6j
    • refs/tags/OpenSSL-engine-0_9_6k
    • refs/tags/OpenSSL-engine-0_9_6l
    • refs/tags/OpenSSL-engine-0_9_6m
    • refs/tags/OpenSSL-fips-1_2_0
    • refs/tags/OpenSSL-fips-1_2_1
    • refs/tags/OpenSSL-fips-1_2_2
    • refs/tags/OpenSSL-fips-1_2_3
    • refs/tags/OpenSSL-fips-2_0
    • refs/tags/OpenSSL-fips-2_0-pl1
    • refs/tags/OpenSSL-fips-2_0-rc1
    • refs/tags/OpenSSL-fips-2_0-rc2
    • refs/tags/OpenSSL-fips-2_0-rc3
    • refs/tags/OpenSSL-fips-2_0-rc4
    • refs/tags/OpenSSL-fips-2_0-rc5
    • refs/tags/OpenSSL-fips-2_0-rc6
    • refs/tags/OpenSSL-fips-2_0-rc7
    • refs/tags/OpenSSL-fips-2_0-rc8
    • refs/tags/OpenSSL-fips-2_0-rc9
    • refs/tags/OpenSSL-fips-2_0_1
    • refs/tags/OpenSSL_0_9_1c
    • refs/tags/OpenSSL_0_9_2b
    • refs/tags/OpenSSL_0_9_3
    • refs/tags/OpenSSL_0_9_3a
    • refs/tags/OpenSSL_0_9_3beta1
    • refs/tags/OpenSSL_0_9_3beta2
    • refs/tags/OpenSSL_0_9_4
    • refs/tags/OpenSSL_0_9_5
    • refs/tags/OpenSSL_0_9_5a
    • refs/tags/OpenSSL_0_9_5a-beta1
    • refs/tags/OpenSSL_0_9_5a-beta2
    • refs/tags/OpenSSL_0_9_5beta1
    • refs/tags/OpenSSL_0_9_5beta2
    • refs/tags/OpenSSL_0_9_6
    • refs/tags/OpenSSL_0_9_6-beta1
    • refs/tags/OpenSSL_0_9_6-beta2
    • refs/tags/OpenSSL_0_9_6-beta3
    • refs/tags/OpenSSL_0_9_6a
    • refs/tags/OpenSSL_0_9_6a-beta1
    • refs/tags/OpenSSL_0_9_6a-beta2
    • refs/tags/OpenSSL_0_9_6a-beta3
    • refs/tags/OpenSSL_0_9_6b
    • refs/tags/OpenSSL_0_9_6c
    • refs/tags/OpenSSL_0_9_6d
    • refs/tags/OpenSSL_0_9_6d-beta1
    • refs/tags/OpenSSL_0_9_6e
    • refs/tags/OpenSSL_0_9_6f
    • refs/tags/OpenSSL_0_9_6g
    • refs/tags/OpenSSL_0_9_6h
    • refs/tags/OpenSSL_0_9_6i
    • refs/tags/OpenSSL_0_9_6j
    • refs/tags/OpenSSL_0_9_6k
    • refs/tags/OpenSSL_0_9_6l
    • refs/tags/OpenSSL_0_9_6m
    • refs/tags/OpenSSL_0_9_7
    • refs/tags/OpenSSL_0_9_7-beta1
    • refs/tags/OpenSSL_0_9_7-beta2
    • refs/tags/OpenSSL_0_9_7-beta3
    • refs/tags/OpenSSL_0_9_7-beta4
    • refs/tags/OpenSSL_0_9_7-beta5
    • refs/tags/OpenSSL_0_9_7-beta6
    • refs/tags/OpenSSL_0_9_7a
    • refs/tags/OpenSSL_0_9_7b
    • refs/tags/OpenSSL_0_9_7c
    • refs/tags/OpenSSL_0_9_7d
    • refs/tags/OpenSSL_0_9_7e
    • refs/tags/OpenSSL_0_9_7f
    • refs/tags/OpenSSL_0_9_7g
    • refs/tags/OpenSSL_0_9_7h
    • refs/tags/OpenSSL_0_9_7i
    • refs/tags/OpenSSL_0_9_7j
    • refs/tags/OpenSSL_0_9_7k
    • refs/tags/OpenSSL_0_9_7l
    • refs/tags/OpenSSL_0_9_7m
    • refs/tags/OpenSSL_0_9_8
    • refs/tags/OpenSSL_0_9_8-beta1
    • refs/tags/OpenSSL_0_9_8-beta2
    • refs/tags/OpenSSL_0_9_8-beta3
    • refs/tags/OpenSSL_0_9_8-beta4
    • refs/tags/OpenSSL_0_9_8-beta5
    • refs/tags/OpenSSL_0_9_8-beta6
    • refs/tags/OpenSSL_0_9_8a
    • refs/tags/OpenSSL_0_9_8b
    • refs/tags/OpenSSL_0_9_8c
    • refs/tags/OpenSSL_0_9_8d
    • refs/tags/OpenSSL_0_9_8e
    • refs/tags/OpenSSL_0_9_8f
    • refs/tags/OpenSSL_0_9_8g
    • refs/tags/OpenSSL_0_9_8h
    • refs/tags/OpenSSL_0_9_8i
    • refs/tags/OpenSSL_0_9_8j
    • refs/tags/OpenSSL_0_9_8k
    • refs/tags/OpenSSL_0_9_8l
    • refs/tags/OpenSSL_0_9_8m
    • refs/tags/OpenSSL_0_9_8m-beta1
    • refs/tags/OpenSSL_0_9_8n
    • refs/tags/OpenSSL_0_9_8o
    • refs/tags/OpenSSL_0_9_8p
    • refs/tags/OpenSSL_0_9_8q
    • refs/tags/OpenSSL_0_9_8r
    • refs/tags/OpenSSL_0_9_8s
    • refs/tags/OpenSSL_0_9_8t
    • refs/tags/OpenSSL_0_9_8u
    • refs/tags/OpenSSL_0_9_8v
    • refs/tags/OpenSSL_0_9_8w
    • refs/tags/OpenSSL_0_9_8x
    • refs/tags/OpenSSL_1_0_0
    • refs/tags/OpenSSL_1_0_0-beta1
    • refs/tags/OpenSSL_1_0_0-beta2
    • refs/tags/OpenSSL_1_0_0-beta3
    • refs/tags/OpenSSL_1_0_0-beta4
    • refs/tags/OpenSSL_1_0_0-beta5
    • refs/tags/OpenSSL_1_0_0a
    • refs/tags/OpenSSL_1_0_0b
    • refs/tags/OpenSSL_1_0_0c
    • refs/tags/OpenSSL_1_0_0d
    • refs/tags/OpenSSL_1_0_0e
    • refs/tags/OpenSSL_1_0_0f
    • refs/tags/OpenSSL_1_0_0g
    • refs/tags/OpenSSL_1_0_0h
    • refs/tags/OpenSSL_1_0_0i
    • refs/tags/OpenSSL_1_0_0j
    • refs/tags/OpenSSL_1_0_1
    • refs/tags/OpenSSL_1_0_1-beta1
    • refs/tags/OpenSSL_1_0_1-beta2
    • refs/tags/OpenSSL_1_0_1-beta3
    • refs/tags/OpenSSL_1_0_1a
    • refs/tags/OpenSSL_1_0_1b
    • refs/tags/OpenSSL_1_0_1c
    • refs/tags/OpenSSL_FIPS_1_0
    • refs/tags/SSLeay_0_8_1b
    • refs/tags/SSLeay_0_9_0b
    • refs/tags/SSLeay_0_9_1b
    • refs/tags/STATE_after_zlib
    • refs/tags/STATE_before_zlib
    • refs/tags/rsaref
    • openssl-3.4.0-alpha1
    • openssl-3.3.2
    • openssl-3.3.1
    • openssl-3.3.0-beta1
    • openssl-3.3.0-alpha1
    • openssl-3.3.0
    • openssl-3.2.3
    • openssl-3.2.2
    • openssl-3.2.1
    • openssl-3.2.0-beta1
    • openssl-3.2.0-alpha2
    • openssl-3.2.0-alpha1
    • openssl-3.2.0
    • openssl-3.1.7
    • openssl-3.1.6
    • openssl-3.1.5
    • openssl-3.1.4
    • openssl-3.1.3
    • openssl-3.1.2
    • openssl-3.1.1
    • openssl-3.1.0-beta1
    • openssl-3.1.0-alpha1
    • openssl-3.1.0
    • openssl-3.0.9
    • openssl-3.0.8
    • openssl-3.0.7
    • openssl-3.0.6
    • openssl-3.0.5
    • openssl-3.0.4
    • openssl-3.0.3
    • openssl-3.0.2
    • openssl-3.0.15
    • openssl-3.0.14
    • openssl-3.0.13
    • openssl-3.0.12
    • openssl-3.0.11
    • openssl-3.0.10
    • openssl-3.0.1
    • openssl-3.0.0-beta2
    • openssl-3.0.0-beta1
    • openssl-3.0.0-alpha9
    • openssl-3.0.0-alpha8
    • openssl-3.0.0-alpha7
    • openssl-3.0.0-alpha6
    • openssl-3.0.0-alpha5
    • openssl-3.0.0-alpha4
    • openssl-3.0.0-alpha3
    • openssl-3.0.0-alpha2
    • openssl-3.0.0-alpha17
    • openssl-3.0.0-alpha16
    • openssl-3.0.0-alpha15
    • openssl-3.0.0-alpha14
    • openssl-3.0.0-alpha13
    • openssl-3.0.0-alpha12
    • openssl-3.0.0-alpha11
    • openssl-3.0.0-alpha10
    • openssl-3.0.0-alpha1
    • openssl-3.0.0
    • master-pre-reformat
    • master-pre-auto-reformat
    • master-post-reformat
    • master-post-auto-reformat
    • OpenSSL_1_1_1w
    • OpenSSL_1_1_1v
    • OpenSSL_1_1_1u
    • OpenSSL_1_1_1t
    • OpenSSL_1_1_1s
    • OpenSSL_1_1_1r
    • OpenSSL_1_1_1q
    • OpenSSL_1_1_1p
    • OpenSSL_1_1_1o
    • OpenSSL_1_1_1n
    • OpenSSL_1_1_1m
    • OpenSSL_1_1_1l
    • OpenSSL_1_1_1k
    • OpenSSL_1_1_1j
    • OpenSSL_1_1_1i
    • OpenSSL_1_1_1h
    • OpenSSL_1_1_1g
    • OpenSSL_1_1_1f
    • OpenSSL_1_1_1e
    • OpenSSL_1_1_1d
    • OpenSSL_1_1_1c
    • OpenSSL_1_1_1b
    • OpenSSL_1_1_1a
    • OpenSSL_1_1_1-pre9
    • OpenSSL_1_1_1-pre8
    • OpenSSL_1_1_1-pre7
    • OpenSSL_1_1_1-pre6
    • OpenSSL_1_1_1-pre5
    • OpenSSL_1_1_1-pre4
    • OpenSSL_1_1_1-pre3
    • OpenSSL_1_1_1-pre2
    • OpenSSL_1_1_1-pre1
    • OpenSSL_1_1_1
    • OpenSSL_1_1_0l
    • OpenSSL_1_1_0k
    • OpenSSL_1_1_0j
    • OpenSSL_1_1_0i
    • OpenSSL_1_1_0h
    • OpenSSL_1_1_0g
    • OpenSSL_1_1_0f
    • OpenSSL_1_1_0e
    • OpenSSL_1_1_0d
    • OpenSSL_1_1_0c
    • OpenSSL_1_1_0b
    • OpenSSL_1_1_0a
    • OpenSSL_1_1_0-pre6
    • OpenSSL_1_1_0-pre5
    • OpenSSL_1_1_0-pre4
    • OpenSSL_1_1_0-pre3
    • OpenSSL_1_1_0-pre2
    • OpenSSL_1_1_0-pre1
    • OpenSSL_1_1_0
    • OpenSSL_1_0_2u
    • OpenSSL_1_0_2t
    • OpenSSL_1_0_2s
    • OpenSSL_1_0_2r
    • OpenSSL_1_0_2q
    • OpenSSL_1_0_2p
    • OpenSSL_1_0_2o
    • OpenSSL_1_0_2n
    • OpenSSL_1_0_2m
    • OpenSSL_1_0_2l
    • OpenSSL_1_0_2k
    • OpenSSL_1_0_2j
    • OpenSSL_1_0_2i
    • OpenSSL_1_0_2h
    • OpenSSL_1_0_2g
    • OpenSSL_1_0_2f
    • OpenSSL_1_0_2e
    • OpenSSL_1_0_2d
    • OpenSSL_1_0_2c
    • OpenSSL_1_0_2b
    • OpenSSL_1_0_2a
    • OpenSSL_1_0_2-pre-reformat
    • OpenSSL_1_0_2-pre-auto-reformat
    • OpenSSL_1_0_2-post-reformat
    • OpenSSL_1_0_2-post-auto-reformat
    • OpenSSL_1_0_2-beta3
    • OpenSSL_1_0_2-beta2
    • OpenSSL_1_0_2-beta1
    • OpenSSL_1_0_2
    • OpenSSL_1_0_1u
    • OpenSSL_1_0_1t
    • OpenSSL_1_0_1s
    • OpenSSL_1_0_1r
    • OpenSSL_1_0_1q
    • OpenSSL_1_0_1p
    • OpenSSL_1_0_1o
    • OpenSSL_1_0_1n
    • OpenSSL_1_0_1m
    • OpenSSL_1_0_1l
    • OpenSSL_1_0_1k
    • OpenSSL_1_0_1j
    • OpenSSL_1_0_1i
    • OpenSSL_1_0_1h
    • OpenSSL_1_0_1g
    • OpenSSL_1_0_1f
    • OpenSSL_1_0_1e
    • OpenSSL_1_0_1d
    • OpenSSL_1_0_1-pre-reformat
    • OpenSSL_1_0_1-pre-auto-reformat
    • OpenSSL_1_0_1-post-reformat
    • OpenSSL_1_0_1-post-auto-reformat
    • OpenSSL_1_0_0t
    • OpenSSL_1_0_0s
    • OpenSSL_1_0_0r
    • OpenSSL_1_0_0q
    • OpenSSL_1_0_0p
    • OpenSSL_1_0_0o
    • OpenSSL_1_0_0n
    • OpenSSL_1_0_0m
    • OpenSSL_1_0_0l
    • OpenSSL_1_0_0k
    • OpenSSL_1_0_0-pre-reformat
    • OpenSSL_1_0_0-pre-auto-reformat
    • OpenSSL_1_0_0-post-reformat
    • OpenSSL_1_0_0-post-auto-reformat
    • OpenSSL_0_9_8zh
    • OpenSSL_0_9_8zg
    • OpenSSL_0_9_8zf
    • OpenSSL_0_9_8ze
    • OpenSSL_0_9_8zd
    • OpenSSL_0_9_8zc
    • OpenSSL_0_9_8zb
    • OpenSSL_0_9_8za
    • OpenSSL_0_9_8y
    • OpenSSL_0_9_8-pre-reformat
    • OpenSSL_0_9_8-pre-auto-reformat
    • OpenSSL_0_9_8-post-reformat
    • OpenSSL_0_9_8-post-auto-reformat
    • OpenSSL-fips-2_0_9
    • OpenSSL-fips-2_0_8
    • OpenSSL-fips-2_0_7
    • OpenSSL-fips-2_0_6
    • OpenSSL-fips-2_0_5
    • OpenSSL-fips-2_0_4
    • OpenSSL-fips-2_0_3
    • OpenSSL-fips-2_0_2
    • OpenSSL-fips-2_0_16
    • OpenSSL-fips-2_0_15
    • OpenSSL-fips-2_0_14
    • OpenSSL-fips-2_0_13
    • OpenSSL-fips-2_0_12
    • OpenSSL-fips-2_0_11
    • OpenSSL-fips-2_0_10
  • 59e02e5
  • /
  • fuzz
  • /
  • provider.c
Raw File
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
content badge Iframe embedding
swh:1:cnt:492d249c7db339bf3a106c17b1efda1d86d905d1
directory badge Iframe embedding
swh:1:dir:1b64aeb03cfc40397aa13221ce77b45f8d1518bd
revision badge
swh:1:rev:0fdf965bf0b1f87d4a5d52c71994ffdda5235718
snapshot badge
swh:1:snp:dc2a5002442a00b1c0eda7c65d04ea7455e166cd
Tip revision: 0fdf965bf0b1f87d4a5d52c71994ffdda5235718 authored by Neil Horman on 11 September 2024, 13:53:49 UTC
review fixups for quic-hq-interop
Tip revision: 0fdf965
provider.c
/*
 * Copyright 2023-2024 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.openssl.org/source/license.html
 * or in the file LICENSE in the source distribution.
 */
#include <string.h>
#include <openssl/types.h>
#include <openssl/crypto.h>
#include <openssl/core_names.h>
#include <openssl/kdf.h>
#include <openssl/evp.h>
#include <openssl/provider.h>
#include "fuzzer.h"

#define DEFINE_ALGORITHMS(name, evp) DEFINE_STACK_OF(evp) \
    static int cmp_##evp(const evp *const *a, const evp *const *b); \
    static void collect_##evp(evp *obj, void *stack); \
    static void init_##name(OSSL_LIB_CTX *libctx); \
    static void cleanup_##name(void); \
    static STACK_OF(evp) *name##_collection; \
    static int cmp_##evp(const evp *const *a, const evp *const *b) \
    { \
        return strcmp(OSSL_PROVIDER_get0_name(evp##_get0_provider(*a)), \
                      OSSL_PROVIDER_get0_name(evp##_get0_provider(*b))); \
    } \
    static void collect_##evp(evp *obj, void *stack) \
    { \
        STACK_OF(evp) *obj_stack = stack;  \
        \
        if (sk_##evp##_push(obj_stack, obj) > 0) \
            evp##_up_ref(obj); \
    } \
    static void init_##name(OSSL_LIB_CTX *libctx) \
    { \
        name##_collection = sk_##evp##_new(cmp_##evp); \
        evp##_do_all_provided(libctx, collect_##evp, name##_collection); \
    } \
    static void cleanup_##name(void) \
    { \
        sk_##evp##_pop_free(name##_collection, evp##_free); \
    }

DEFINE_ALGORITHMS(digests, EVP_MD)

DEFINE_ALGORITHMS(kdf, EVP_KDF)

DEFINE_ALGORITHMS(cipher, EVP_CIPHER)

DEFINE_ALGORITHMS(kem, EVP_KEM)

DEFINE_ALGORITHMS(keyexch, EVP_KEYEXCH)

DEFINE_ALGORITHMS(rand, EVP_RAND)

DEFINE_ALGORITHMS(mac, EVP_MAC)

DEFINE_ALGORITHMS(keymgmt, EVP_KEYMGMT)

DEFINE_ALGORITHMS(signature, EVP_SIGNATURE)

DEFINE_ALGORITHMS(asym_ciphers, EVP_ASYM_CIPHER)

static OSSL_LIB_CTX *libctx = NULL;

int FuzzerInitialize(int *argc, char ***argv)
{
    libctx = OSSL_LIB_CTX_new();
    if (libctx == NULL)
        return 0;

    init_digests(libctx);
    init_kdf(libctx);
    init_cipher(libctx);
    init_kem(libctx);
    init_keyexch(libctx);
    init_rand(libctx);
    init_mac(libctx);
    init_keymgmt(libctx);
    init_signature(libctx);
    init_asym_ciphers(libctx);
    return 1;
}

void FuzzerCleanup(void)
{
    cleanup_digests();
    cleanup_kdf();
    cleanup_cipher();
    cleanup_kem();
    cleanup_keyexch();
    cleanup_rand();
    cleanup_mac();
    cleanup_keymgmt();
    cleanup_signature();
    cleanup_asym_ciphers();

    OSSL_LIB_CTX_free(libctx);
}

static int read_uint(const uint8_t **buf, size_t *len, uint64_t **res)
{
    int r = 1;

    if (*len < sizeof(uint64_t)) {
        r = 0;
        goto end;
    }

    *res = OPENSSL_malloc(sizeof(uint64_t));
    **res = (uint64_t) **buf;

    *buf += sizeof(uint64_t);
    *len -= sizeof(uint64_t);
end:
    return r;
}

static int read_int(const uint8_t **buf, size_t *len, int64_t **res)
{
    int r = 1;

    if (*len < sizeof(int64_t)) {
        r = 0;
        goto end;
    }

    *res = OPENSSL_malloc(sizeof(int64_t));
    **res = (int64_t) **buf;

    *buf += sizeof(int64_t);
    *len -= sizeof(int64_t);
end:
    return r;
}

static int read_double(const uint8_t **buf, size_t *len, double **res)
{
    int r = 1;

    if (*len < sizeof(double)) {
        r = 0;
        goto end;
    }

    *res = OPENSSL_malloc(sizeof(double));
    **res = (double) **buf;

    *buf += sizeof(double);
    *len -= sizeof(double);
end:
    return r;
}

static int read_utf8_string(const uint8_t **buf, size_t *len, char **res)
{
    size_t found_len;
    int r;

    found_len = OPENSSL_strnlen((const char *) *buf, *len);

    if (found_len == *len) {
        r = -1;
        goto end;
    }

    found_len++; /* skip over the \0 byte */

    r = (int) found_len;

    *res = (char *) *buf;
    *len -= found_len;
    *buf = *buf + found_len; /* continue after the \0 byte */
end:
    return r;
}

static int read_utf8_ptr(const uint8_t **buf, size_t *len, char **res)
{
    if (*len > 0 && **buf == 0xFF) {
        /* represent NULL somehow */
        *res = NULL;
        *buf += 1;
        *len -= 1;
        return 0;
    }
    return read_utf8_string(buf, len, res);
}

static int read_octet_string(const uint8_t **buf, size_t *len, char **res)
{
    int r;
    size_t i;
    const uint8_t *ptr = *buf;
    int found = 0;

    for (i = 0; i < *len; ++i) {
        if (*ptr == 0xFF &&
            (i + 1 < *len && *(ptr + 1) == 0xFF)) {
            ptr++;
            found = 1;
            break;
        }
        ptr++;
    }

    if (!found) {
        r = -1;
        goto end;
    }

    *res = (char *) *buf;

    r = ptr - *buf;
    *len -= r;
    *buf = ptr;

end:
    return r;
}

static int read_octet_ptr(const uint8_t **buf, size_t *len, char **res)
{
    /* TODO: This representation could need an improvement potentially. */
    if (*len > 1 && **buf == 0xFF && *(*buf + 1) == 0xFF) {
        /* represent NULL somehow */
        *res = NULL;
        *buf += 2;
        *len -= 2;
        return 0;
    }
    return read_octet_string(buf, len, res);
}

static char *DFLT_STR = "";
static char *DFLT_UTF8_PTR = NULL;
static char *DFLT_OCTET_STRING = "";
static char *DFLT_OCTET_PTR = NULL;

static int64_t ITERS = 1;
static uint64_t UITERS = 1;
static int64_t BLOCKSIZE = 8;
static uint64_t UBLOCKSIZE = 8;


static void free_params(OSSL_PARAM *param)
{
    for (; param != NULL && param->key != NULL; param++) {
        switch (param->data_type) {
            case OSSL_PARAM_INTEGER:
            case OSSL_PARAM_UNSIGNED_INTEGER:
            case OSSL_PARAM_REAL:
                if (param->data != NULL) {
                    OPENSSL_free(param->data);
                }
                break;
        }
    }
}

static OSSL_PARAM *fuzz_params(OSSL_PARAM *param, const uint8_t **buf, size_t *len)
{
    OSSL_PARAM *p;
    OSSL_PARAM *fuzzed_parameters;
    int p_num = 0;

    for (p = param; p != NULL && p->key != NULL; p++)
        p_num++;

    fuzzed_parameters = OPENSSL_zalloc(sizeof(OSSL_PARAM) *(p_num + 1));
    p = fuzzed_parameters;

    for (; param != NULL && param->key != NULL; param++) {
        int64_t *use_param = NULL;
        int64_t *p_value_int = NULL;
        uint64_t *p_value_uint = NULL;
        double *p_value_double = NULL;
        char *p_value_utf8_str = DFLT_STR;
        char *p_value_octet_str = DFLT_OCTET_STRING;
        char *p_value_utf8_ptr = DFLT_UTF8_PTR;
        char *p_value_octet_ptr = DFLT_OCTET_PTR;

        int data_len = 0;

        if (!read_int(buf, len, &use_param)) {
            use_param = OPENSSL_malloc(sizeof(uint64_t));
            *use_param = 0;
        }

        switch (param->data_type) {
        case OSSL_PARAM_INTEGER:
            if (strcmp(param->key, OSSL_KDF_PARAM_ITER) == 0) {
                p_value_int = OPENSSL_malloc(sizeof(ITERS));
                *p_value_int = ITERS;
            } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_N) == 0) {
                p_value_int = OPENSSL_malloc(sizeof(ITERS));
                *p_value_int = ITERS;
            } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_R) == 0) {
                p_value_int = OPENSSL_malloc(sizeof(BLOCKSIZE));
                *p_value_int = BLOCKSIZE;
            } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_P) == 0) {
                p_value_int = OPENSSL_malloc(sizeof(BLOCKSIZE));
                *p_value_int = BLOCKSIZE;
            } else if (!*use_param || !read_int(buf, len, &p_value_int)) {
                p_value_int = OPENSSL_malloc(sizeof(int64_t));
                *p_value_int = 0;
            }

            *p = *param;
            p->data = p_value_int;
            p++;
            break;
        case OSSL_PARAM_UNSIGNED_INTEGER:
            if (strcmp(param->key, OSSL_KDF_PARAM_ITER) == 0) {
                p_value_uint = OPENSSL_malloc(sizeof(UITERS));
                *p_value_uint = UITERS;
            } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_N) == 0) {
                p_value_uint = OPENSSL_malloc(sizeof(UITERS));
                *p_value_uint = UITERS;
            } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_R) == 0) {
                p_value_uint = OPENSSL_malloc(sizeof(UBLOCKSIZE));
                *p_value_uint = UBLOCKSIZE;
            } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_P) == 0) {
                p_value_uint = OPENSSL_malloc(sizeof(UBLOCKSIZE));
                *p_value_uint = UBLOCKSIZE;
            } else if (!*use_param || !read_uint(buf, len, &p_value_uint)) {
                p_value_uint = OPENSSL_malloc(sizeof(uint64_t));
                *p_value_uint = 0;
            }

            *p = *param;
            p->data = p_value_uint;
            p++;
            break;
        case OSSL_PARAM_REAL:
            if (!*use_param || !read_double(buf, len, &p_value_double)) {
                p_value_double = OPENSSL_malloc(sizeof(double));
                *p_value_double = 0;
            }

            *p = *param;
            p->data = p_value_double;
            p++;
            break;
        case OSSL_PARAM_UTF8_STRING:
            if (*use_param && (data_len = read_utf8_string(buf, len, &p_value_utf8_str)) < 0)
                data_len = 0;
            *p = *param;
            p->data = p_value_utf8_str;
            p->data_size = data_len;
            p++;
            break;
        case OSSL_PARAM_OCTET_STRING:
            if (*use_param && (data_len = read_octet_string(buf, len, &p_value_octet_str)) < 0)
                data_len = 0;
            *p = *param;
            p->data = p_value_octet_str;
            p->data_size = data_len;
            p++;
            break;
        case OSSL_PARAM_UTF8_PTR:
            if (*use_param && (data_len = read_utf8_ptr(buf, len, &p_value_utf8_ptr)) < 0)
                data_len = 0;
            *p = *param;
            p->data = p_value_utf8_ptr;
            p->data_size = data_len;
            p++;
            break;
        case OSSL_PARAM_OCTET_PTR:
            if (*use_param && (data_len = read_octet_ptr(buf, len, &p_value_octet_ptr)) < 0)
                data_len = 0;
            *p = *param;
            p->data = p_value_octet_ptr;
            p->data_size = data_len;
            p++;
            break;
        default:
            break;
        }

        OPENSSL_free(use_param);
    }

    return fuzzed_parameters;
}

static int do_evp_cipher(const EVP_CIPHER *evp_cipher, const OSSL_PARAM param[])
{
    unsigned char outbuf[1024];
    int outlen, tmplen;
    unsigned char key[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    unsigned char iv[] = {1, 2, 3, 4, 5, 6, 7, 8};
    const char intext[] = "text";
    EVP_CIPHER_CTX *ctx;

    ctx = EVP_CIPHER_CTX_new();

    if (!EVP_CIPHER_CTX_set_params(ctx, param)) {
        EVP_CIPHER_CTX_free(ctx);
        return 0;
    }

    if (!EVP_EncryptInit_ex2(ctx, evp_cipher, key, iv, NULL)) {
        /* Error */
        EVP_CIPHER_CTX_free(ctx);
        return 0;
    }

    if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, (const unsigned char *) intext, strlen(intext))) {
        /* Error */
        EVP_CIPHER_CTX_free(ctx);
        return 0;
    }
    /*
     * Buffer passed to EVP_EncryptFinal() must be after data just
     * encrypted to avoid overwriting it.
     */
    if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
        /* Error */
        EVP_CIPHER_CTX_free(ctx);
        return 0;
    }
    outlen += tmplen;
    EVP_CIPHER_CTX_free(ctx);
    return 1;
}

static int do_evp_kdf(EVP_KDF *evp_kdf, const OSSL_PARAM params[])
{
    int r = 1;
    EVP_KDF_CTX *kctx = NULL;
    unsigned char derived[32];

    kctx = EVP_KDF_CTX_new(evp_kdf);

    if (kctx == NULL) {
        r = 0;
        goto end;
    }

    if (EVP_KDF_CTX_set_params(kctx, params) <= 0) {
        r = 0;
        goto end;
    }

    if (EVP_KDF_derive(kctx, derived, sizeof(derived), NULL) <= 0) {
        r = 0;
        goto end;
    }

end:
    EVP_KDF_CTX_free(kctx);
    return r;
}

static int do_evp_mac(EVP_MAC *evp_mac, const OSSL_PARAM params[])
{
    int r = 1;
    const char *key = "mac_key";
    char text[] = "Some Crypto Text";
    EVP_MAC_CTX *ctx = NULL;
    unsigned char buf[4096];
    size_t final_l;

    if ((ctx = EVP_MAC_CTX_new(evp_mac)) == NULL
        || !EVP_MAC_init(ctx, (const unsigned char *) key, strlen(key),
                         params)) {
        r = 0;
        goto end;
    }

    if (EVP_MAC_CTX_set_params(ctx, params) <= 0) {
        r = 0;
        goto end;
    }

    if (!EVP_MAC_update(ctx, (unsigned char *) text, sizeof(text))) {
        r = 0;
        goto end;
    }

    if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf))) {
        r = 0;
        goto end;
    }

end:
    EVP_MAC_CTX_free(ctx);
    return r;
}

static int do_evp_rand(EVP_RAND *evp_rand, const OSSL_PARAM params[])
{
    int r = 1;
    EVP_RAND_CTX *ctx = NULL;
    unsigned char buf[4096];

    if (!(ctx = EVP_RAND_CTX_new(evp_rand, NULL))) {
        r = 0;
        goto end;
    }

    if (EVP_RAND_CTX_set_params(ctx, params) <= 0) {
        r = 0;
        goto end;
    }

    if (!EVP_RAND_generate(ctx, buf, sizeof(buf), 0, 0, NULL, 0)) {
        r = 0;
        goto end;
    }

    if (!EVP_RAND_reseed(ctx, 0, 0, 0, NULL, 0)) {
        r = 0;
        goto end;
    }

end:
    EVP_RAND_CTX_free(ctx);
    return r;
}

static int do_evp_sig(EVP_SIGNATURE *evp_sig, const OSSL_PARAM params[])
{
    return 0;
}

static int do_evp_asym_cipher(EVP_ASYM_CIPHER *evp_asym_cipher, const OSSL_PARAM params[])
{
    return 0;
}

static int do_evp_kem(EVP_KEM *evp_kem, const OSSL_PARAM params[])
{
    return 0;
}

static int do_evp_key_exch(EVP_KEYEXCH *evp_kdf, const OSSL_PARAM params[])
{
    return 0;
}

static int do_evp_md(EVP_MD *evp_md, const OSSL_PARAM params[])
{
    int r = 1;
    unsigned char md_value[EVP_MAX_MD_SIZE];
    unsigned int md_len;
    EVP_MD_CTX *mdctx = NULL;

    if (!(mdctx = EVP_MD_CTX_new())) {
        r = 0;
        goto end;
    }

    if (!EVP_MD_CTX_set_params(mdctx, params)) {
        r = 0;
        goto end;
    }

    if (!EVP_DigestInit_ex2(mdctx, evp_md, NULL)) {
        r = 0;
        goto end;
    }
    if (!EVP_DigestUpdate(mdctx, "Test", strlen("Test"))) {
        r = 0;
        goto end;
    }
    if (!EVP_DigestFinal_ex(mdctx, md_value, &md_len)) {
        r = 0;
        goto end;
    }

end:
    EVP_MD_CTX_free(mdctx);
    return r;
}

#define EVP_FUZZ(source, evp, f) \
    do { \
        evp *alg = sk_##evp##_value(source, *algorithm % sk_##evp##_num(source)); \
        OSSL_PARAM *fuzzed_params; \
        \
        if (alg == NULL) \
            break; \
        fuzzed_params = fuzz_params((OSSL_PARAM*) evp##_settable_ctx_params(alg), &buf, &len); \
        if (fuzzed_params != NULL) \
            f(alg, fuzzed_params); \
        free_params(fuzzed_params); \
        OSSL_PARAM_free(fuzzed_params); \
    } while (0);

int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
    int r = 1;
    uint64_t *operation = NULL;
    int64_t *algorithm = NULL;

    if (!read_uint(&buf, &len, &operation)) {
        r = 0;
        goto end;
    }

    if (!read_int(&buf, &len, &algorithm)) {
        r = 0;
        goto end;
    }

    switch (*operation % 10) {
    case 0:
        EVP_FUZZ(digests_collection, EVP_MD, do_evp_md);
        break;
    case 1:
        EVP_FUZZ(cipher_collection, EVP_CIPHER, do_evp_cipher);
        break;
    case 2:
        EVP_FUZZ(kdf_collection, EVP_KDF, do_evp_kdf);
        break;
    case 3:
        EVP_FUZZ(mac_collection, EVP_MAC, do_evp_mac);
        break;
    case 4:
        EVP_FUZZ(kem_collection, EVP_KEM, do_evp_kem);
        break;
    case 5:
        EVP_FUZZ(rand_collection, EVP_RAND, do_evp_rand);
        break;
    case 6:
        EVP_FUZZ(asym_ciphers_collection, EVP_ASYM_CIPHER, do_evp_asym_cipher);
        break;
    case 7:
        EVP_FUZZ(signature_collection, EVP_SIGNATURE, do_evp_sig);
        break;
    case 8:
        EVP_FUZZ(keyexch_collection, EVP_KEYEXCH, do_evp_key_exch);
        break;
    case 9:
        /*
        Implement and call:
        static int do_evp_keymgmt(EVP_KEYMGMT *evp_kdf, const OSSL_PARAM params[])
        {
            return 0;
        }
        */
        /* not yet implemented */
        break;
    default:
        r = 0;
        goto end;
    }

end:
    OPENSSL_free(operation);
    OPENSSL_free(algorithm);
    return r;
}

ENEA — Copyright (C), ENEA. License: GNU AGPLv3+.
Legal notes  ::  JavaScript license information ::  Web API

back to top