Coverage Report

Created: 2022-08-24 06:37

/src/cryptofuzz-sp-math-all/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
}
15
16
namespace cryptofuzz {
17
namespace module {
18
namespace wolfCrypt_bignum {
19
20
class Bignum {
21
    private:
22
        mp_int* mp = nullptr;
23
        Datasource& ds;
24
        bool noFree = false;
25
26
        typedef enum {
27
            READ_RADIX_OK,
28
            READ_RADIX_FAIL_MEMORY,
29
            READ_RADIX_FAIL_OTHER,
30
        } read_radix_error_t;
31
32
        static read_radix_error_t read_radix(mp_int* dest, const std::string& str, const size_t base);
33
        static read_radix_error_t read_radix(mp_int* dest, const char* str, const size_t base);
34
        void baseConversion(void) const;
35
        void binaryConversion(void) const;
36
    public:
37
38
        Bignum(Datasource& ds);
39
        Bignum(mp_int* mp, Datasource& ds);
40
        Bignum(const Bignum& other);
41
        Bignum(const Bignum&& other);
42
        ~Bignum();
43
44
        void SetNoFree(void);
45
        bool Set(const std::string s);
46
        bool Set(const component::Bignum i);
47
        mp_int* GetPtr(void) const;
48
        mp_int* GetPtrDirect(void) const;
49
        std::optional<uint64_t> AsUint64(void) const;
50
51
        template <class T>
52
3.16k
        std::optional<T> AsUnsigned(void) const {
53
3.16k
            std::optional<T> ret = std::nullopt;
54
3.16k
            T v2;
55
56
3.16k
            auto v = AsUint64();
57
3.16k
            CF_CHECK_NE(v, std::nullopt);
58
59
2.81k
            v2 = *v;
60
2.81k
            CF_CHECK_EQ(v2, *v);
61
62
2.81k
            ret = v2;
63
64
3.16k
end:
65
3.16k
            return ret;
66
2.81k
        }
std::__1::optional<unsigned long> cryptofuzz::module::wolfCrypt_bignum::Bignum::AsUnsigned<unsigned long>() const
Line
Count
Source
52
2.70k
        std::optional<T> AsUnsigned(void) const {
53
2.70k
            std::optional<T> ret = std::nullopt;
54
2.70k
            T v2;
55
56
2.70k
            auto v = AsUint64();
57
2.70k
            CF_CHECK_NE(v, std::nullopt);
58
59
2.38k
            v2 = *v;
60
2.38k
            CF_CHECK_EQ(v2, *v);
61
62
2.38k
            ret = v2;
63
64
2.70k
end:
65
2.70k
            return ret;
66
2.38k
        }
std::__1::optional<unsigned int> cryptofuzz::module::wolfCrypt_bignum::Bignum::AsUnsigned<unsigned int>() const
Line
Count
Source
52
453
        std::optional<T> AsUnsigned(void) const {
53
453
            std::optional<T> ret = std::nullopt;
54
453
            T v2;
55
56
453
            auto v = AsUint64();
57
453
            CF_CHECK_NE(v, std::nullopt);
58
59
433
            v2 = *v;
60
433
            CF_CHECK_EQ(v2, *v);
61
62
433
            ret = v2;
63
64
453
end:
65
453
            return ret;
66
433
        }
67
68
        std::optional<std::string> ToDecString(void);
69
        std::optional<component::Bignum> ToComponentBignum(void);
70
        bool ToBin(uint8_t* dest, const size_t size);
71
        static std::optional<std::vector<uint8_t>> ToBin(Datasource& ds, const component::Bignum b, std::optional<size_t> size = std::nullopt);
72
        static bool ToBin(Datasource& ds, const component::Bignum b, uint8_t* dest, const size_t size);
73
        static bool ToBin(Datasource& ds, const component::BignumPair b, uint8_t* dest, const size_t size);
74
        static std::optional<component::Bignum> BinToBignum(Datasource& ds, const uint8_t* src, const size_t size);
75
        static std::optional<component::BignumPair> BinToBignumPair(Datasource& ds, const uint8_t* src, const size_t size);
76
        void Randomize(void);
77
        bool operator==(const Bignum& rhs) const;
78
};
79
80
class BignumCluster {
81
    private:
82
        Datasource& ds;
83
        std::array<Bignum, 4> bn;
84
85
        struct {
86
            bool invalid = false;
87
            std::array<mp_int*, 4> bn = {0};
88
        } cache;
89
    public:
90
        BignumCluster(Datasource& ds, Bignum bn0, Bignum bn1, Bignum bn2, Bignum bn3);
91
        ~BignumCluster();
92
        Bignum& operator[](const size_t index);
93
94
        bool Set(const size_t index, const std::string s);
95
        mp_int* GetDestPtr(const size_t index);
96
97
        void Save(void);
98
        void InvalidateCache(void);
99
        bool EqualsCache(void) const;
100
};
101
102
} /* namespace wolfCrypt_bignum */
103
} /* namespace module */
104
} /* namespace cryptofuzz */