/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  | 63.6k  |         static int init_mp_int(mp_int* mp, Datasource& ds) { | 
38  |  | #if defined(USE_FAST_MATH)  | 
39  |  |             (void)ds;  | 
40  |  |             return mp_init(mp);  | 
41  |  | #else  | 
42  | 63.6k  |             uint8_t size = 127;  | 
43  | 63.6k  |             try { | 
44  | 63.6k  |                 size = ds.Get<uint8_t>();  | 
45  | 63.6k  |             } catch ( ... ) { } | 
46  |  |  | 
47  | 63.6k  |             const auto ret = mp_init_size(mp, size);  | 
48  |  |  | 
49  | 63.6k  |             if ( ret != MP_OKAY ) { | 
50  | 12.8k  |                 return mp_init(mp);  | 
51  | 12.8k  |             }  | 
52  |  |  | 
53  | 50.8k  |             return ret;  | 
54  | 63.6k  | #endif  | 
55  | 63.6k  |         }  | 
56  |  |         void invariants(void) const;  | 
57  |  |  | 
58  |  |     public:  | 
59  |  |  | 
60  |  |         Bignum(Datasource& ds);  | 
61  |  |         Bignum(mp_int* mp, Datasource& ds);  | 
62  |  |         Bignum(const Bignum& other);  | 
63  |  |         Bignum(const Bignum&& other);  | 
64  |  |         ~Bignum();  | 
65  |  |  | 
66  |  |         void SetNoFree(void);  | 
67  |  |         bool Set(const std::string s);  | 
68  |  |         bool Set(const component::Bignum i);  | 
69  |  |         mp_int* GetPtr(void) const;  | 
70  |  |         mp_int* GetPtrDirect(void) const;  | 
71  |  |         std::optional<uint64_t> AsUint64(void) const;  | 
72  |  |  | 
73  |  |         template <class T>  | 
74  | 120  |         std::optional<T> AsUnsigned(void) const { | 
75  | 120  |             std::optional<T> ret = std::nullopt;  | 
76  | 120  |             T v2;  | 
77  |  |  | 
78  | 120  |             auto v = AsUint64();  | 
79  | 120  |             CF_CHECK_NE(v, std::nullopt);  | 
80  |  |  | 
81  | 101  |             v2 = *v;  | 
82  | 101  |             CF_CHECK_EQ(v2, *v);  | 
83  |  |  | 
84  | 101  |             ret = v2;  | 
85  |  |  | 
86  | 120  | end:  | 
87  | 120  |             return ret;  | 
88  | 101  |         } std::__1::optional<unsigned long> cryptofuzz::module::wolfCrypt_bignum::Bignum::AsUnsigned<unsigned long>() const Line  | Count  | Source  |  74  | 120  |         std::optional<T> AsUnsigned(void) const { |  75  | 120  |             std::optional<T> ret = std::nullopt;  |  76  | 120  |             T v2;  |  77  |  |  |  78  | 120  |             auto v = AsUint64();  |  79  | 120  |             CF_CHECK_NE(v, std::nullopt);  |  80  |  |  |  81  | 101  |             v2 = *v;  |  82  | 101  |             CF_CHECK_EQ(v2, *v);  |  83  |  |  |  84  | 101  |             ret = v2;  |  85  |  |  |  86  | 120  | end:  |  87  | 120  |             return ret;  |  88  | 101  |         }  |  
 Unexecuted instantiation: std::__1::optional<unsigned int> cryptofuzz::module::wolfCrypt_bignum::Bignum::AsUnsigned<unsigned int>() const  | 
89  |  |  | 
90  |  |         std::optional<std::string> ToDecString(void);  | 
91  |  |         std::optional<component::Bignum> ToComponentBignum(void);  | 
92  |  |         bool ToBin(uint8_t* dest, const size_t size);  | 
93  |  |         static std::optional<std::vector<uint8_t>> ToBin(Datasource& ds, const component::Bignum b, std::optional<size_t> size = std::nullopt);  | 
94  |  |         static bool ToBin(Datasource& ds, const component::Bignum b, uint8_t* dest, const size_t size, const bool mustSucceed = false);  | 
95  |  |         static bool ToBin(Datasource& ds, const component::BignumPair b, uint8_t* dest, const size_t size, const bool mustSucceed = false);  | 
96  |  |         static std::optional<component::Bignum> BinToBignum(Datasource& ds, const uint8_t* src, const size_t size);  | 
97  |  |         static std::optional<component::BignumPair> BinToBignumPair(Datasource& ds, const uint8_t* src, const size_t size);  | 
98  |  |         void Randomize(void);  | 
99  |  |         bool operator==(const Bignum& rhs) const;  | 
100  |  | };  | 
101  |  |  | 
102  |  | class BignumCluster { | 
103  |  |     private:  | 
104  |  |         Datasource& ds;  | 
105  |  |         std::array<Bignum, 4> bn;  | 
106  |  |  | 
107  |  |         struct { | 
108  |  |             bool invalid = false;  | 
109  |  |             std::array<mp_int*, 4> bn = {0}; | 
110  |  |         } cache;  | 
111  |  |     public:  | 
112  |  |         BignumCluster(Datasource& ds, Bignum bn0, Bignum bn1, Bignum bn2, Bignum bn3);  | 
113  |  |         ~BignumCluster();  | 
114  |  |         Bignum& operator[](const size_t index);  | 
115  |  |  | 
116  |  |         bool Set(const size_t index, const std::string s);  | 
117  |  |         mp_int* GetDestPtr(const size_t index);  | 
118  |  |  | 
119  |  |         void Save(void);  | 
120  |  |         void InvalidateCache(void);  | 
121  |  |         bool EqualsCache(void) const;  | 
122  |  | };  | 
123  |  |  | 
124  |  | } /* namespace wolfCrypt_bignum */  | 
125  |  | } /* namespace module */  | 
126  |  | } /* namespace cryptofuzz */  |