Coverage Report

Created: 2024-06-28 06:19

/src/cryptofuzz/modules/wolfcrypt/bn_helper.h
Line
Count
Source
1
#pragma once
2
3
#include <cryptofuzz/components.h>
4
#include <cryptofuzz/util.h>
5
#include <array>
6
7
extern "C" {
8
#include <wolfssl/options.h>
9
#include <wolfssl/wolfcrypt/integer.h>
10
#include <wolfssl/wolfcrypt/ecc.h>
11
#if defined(WOLFSSL_SP_MATH)
12
 #include <wolfssl/wolfcrypt/sp.h>
13
#endif
14
#include <wolfssl/version.h>
15
}
16
17
namespace cryptofuzz {
18
namespace module {
19
namespace wolfCrypt_bignum {
20
21
class Bignum {
22
    private:
23
        mp_int* mp = nullptr;
24
        Datasource& ds;
25
        bool noFree = false;
26
27
        typedef enum {
28
            READ_RADIX_OK,
29
            READ_RADIX_FAIL_MEMORY,
30
            READ_RADIX_FAIL_OTHER,
31
        } read_radix_error_t;
32
33
        static read_radix_error_t read_radix(mp_int* dest, const std::string& str, const size_t base);
34
        static read_radix_error_t read_radix(mp_int* dest, const char* str, const size_t base);
35
        void baseConversion(void) const;
36
        void binaryConversion(void) const;
37
107k
        static int init_mp_int(mp_int* mp, Datasource& ds, const bool mp_init_size_ok = true) {
38
#if defined(USE_FAST_MATH)
39
            (void)ds;
40
            (void)mp_init_size_ok;
41
            return mp_init(mp);
42
#else
43
107k
            if ( mp_init_size_ok == false ) {
44
782
                return mp_init(mp);
45
782
            }
46
47
107k
            uint8_t size = 127;
48
107k
            try {
49
107k
                size = ds.Get<uint8_t>();
50
107k
            } catch ( ... ) { }
51
52
107k
            const auto ret = mp_init_size(mp, size);
53
54
107k
            if ( ret != MP_OKAY ) {
55
23.3k
                return mp_init(mp);
56
23.3k
            }
57
58
83.8k
            return ret;
59
107k
#endif
60
107k
        }
61
        void invariants(void) const;
62
63
    public:
64
65
        Bignum(Datasource& ds, const bool mp_init_size_ok = true);
66
        Bignum(mp_int* mp, Datasource& ds);
67
        Bignum(const Bignum& other);
68
        Bignum(const Bignum&& other);
69
        ~Bignum();
70
71
        void SetNoFree(void);
72
        bool Set(const std::string s);
73
        bool Set(const component::Bignum i);
74
        mp_int* GetPtr(void) const;
75
        mp_int* GetPtrDirect(void) const;
76
        std::optional<uint64_t> AsUint64(void) const;
77
78
        template <class T>
79
247
        std::optional<T> AsUnsigned(void) const {
80
247
            std::optional<T> ret = std::nullopt;
81
247
            T v2;
82
83
247
            auto v = AsUint64();
84
247
            CF_CHECK_NE(v, std::nullopt);
85
86
197
            v2 = *v;
87
197
            CF_CHECK_EQ(v2, *v);
88
89
197
            ret = v2;
90
91
247
end:
92
247
            return ret;
93
197
        }
std::__1::optional<unsigned long> cryptofuzz::module::wolfCrypt_bignum::Bignum::AsUnsigned<unsigned long>() const
Line
Count
Source
79
247
        std::optional<T> AsUnsigned(void) const {
80
247
            std::optional<T> ret = std::nullopt;
81
247
            T v2;
82
83
247
            auto v = AsUint64();
84
247
            CF_CHECK_NE(v, std::nullopt);
85
86
197
            v2 = *v;
87
197
            CF_CHECK_EQ(v2, *v);
88
89
197
            ret = v2;
90
91
247
end:
92
247
            return ret;
93
197
        }
Unexecuted instantiation: std::__1::optional<unsigned int> cryptofuzz::module::wolfCrypt_bignum::Bignum::AsUnsigned<unsigned int>() const
94
95
        std::optional<std::string> ToDecString(void);
96
        std::optional<component::Bignum> ToComponentBignum(void);
97
        bool ToBin(uint8_t* dest, const size_t size);
98
        static std::optional<std::vector<uint8_t>> ToBin(Datasource& ds, const component::Bignum b, std::optional<size_t> size = std::nullopt);
99
        static bool ToBin(Datasource& ds, const component::Bignum b, uint8_t* dest, const size_t size, const bool mustSucceed = false);
100
        static bool ToBin(Datasource& ds, const component::BignumPair b, uint8_t* dest, const size_t size, const bool mustSucceed = false);
101
        static std::optional<component::Bignum> BinToBignum(Datasource& ds, const uint8_t* src, const size_t size);
102
        static std::optional<component::BignumPair> BinToBignumPair(Datasource& ds, const uint8_t* src, const size_t size);
103
        void Randomize(void);
104
        bool operator==(const Bignum& rhs) const;
105
};
106
107
class BignumCluster {
108
    private:
109
        Datasource& ds;
110
        std::array<Bignum, 4> bn;
111
        std::optional<size_t> res_index = std::nullopt;
112
113
        struct {
114
            bool invalid = false;
115
            std::array<mp_int*, 4> bn = {0};
116
        } cache;
117
    public:
118
        BignumCluster(Datasource& ds, Bignum bn0, Bignum bn1, Bignum bn2, Bignum bn3);
119
        ~BignumCluster();
120
        Bignum& operator[](const size_t index);
121
122
        bool Set(const size_t index, const std::string s);
123
        mp_int* GetDestPtr(const size_t index);
124
        mp_int* GetResPtr(void);
125
        bool CopyResult(Bignum& res) const;
126
        void Save(void);
127
        void InvalidateCache(void);
128
        bool EqualsCache(void) const;
129
};
130
131
} /* namespace wolfCrypt_bignum */
132
} /* namespace module */
133
} /* namespace cryptofuzz */