├── src ├── endian.test.c ├── include │ ├── crypto │ │ └── secp256k1.h │ ├── node │ │ ├── boundwitnessoption.h │ │ ├── nodelistener.h │ │ └── nodebase.h │ └── state │ │ └── originchain.h ├── ByteArray.h ├── XYObjects │ ├── XYObjectTypes.h │ ├── XYObjectHeader.h │ ├── Array │ │ ├── Iterator.h │ │ ├── WeakArray │ │ │ ├── WeakArray.h │ │ │ ├── WeakArray.c │ │ │ └── WeakArray.test.c │ │ └── Iterator.c │ ├── XYObject.test.c │ ├── XYObject.c │ └── XYObject.h ├── XYResult.test.c ├── XYResult.h ├── WolfSSL │ ├── trng.h │ ├── wc_md4.h │ ├── wc_wolfmath.h │ ├── wc_visibility.h │ ├── wc_mpi_superclass.h │ ├── wc_md5.h │ ├── wc_sha.h │ ├── wc_hmac.h │ ├── wc_hash.h │ ├── wc_random.h │ ├── wc_sha256.h │ ├── wc_memory.h │ ├── wc_rsa.h │ ├── wc_integer.h │ └── wc_port.h ├── hash.h ├── nodebase.c ├── state.h ├── relaynode.h ├── endian.h ├── network.h ├── endian.c ├── crypto.h ├── state.c ├── repository.c ├── hash.c ├── repository.h ├── defines.h ├── relaynode.c └── crypto.c ├── .clang_complete ├── testbuild ├── .vscode ├── extensions.json ├── settings.json ├── c_cpp_properties.json └── tasks.json ├── .github ├── ISSUE_TEMPLATE │ ├── config.yml │ ├── documentation_needed.md │ ├── feature_request.md │ └── bug_report.md └── workflows │ └── build.yml ├── .gitignore ├── CMakeLists.txt ├── README.md └── LICENSE /src/endian.test.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/include/crypto/secp256k1.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.clang_complete: -------------------------------------------------------------------------------- 1 | -Iinclude 2 | -Isrc 3 | -Isrc/Objects 4 | -------------------------------------------------------------------------------- /testbuild: -------------------------------------------------------------------------------- 1 | mkdir output 2 | cd output 3 | clang -c ../src/*.c 4 | cd .. -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "hookyqr.beautify", 4 | "mitaki28.vscode-clang", 5 | "ms-vscode.cpptools", 6 | "cschlosser.doxdocgen" 7 | ] 8 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "*.c": "c", 4 | "*.h": "c" 5 | }, 6 | "doxdocgen.file.copyrightTag": [ 7 | "@copyright Copyright (c) {year} XY - The Persistant COmpany" 8 | ], 9 | "doxdocgen.generic.authorEmail": "developer@xy.company" 10 | } -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: XYO Developer Portal 4 | url: https://developers.xyo.network/ 5 | about: XYO Foundation Developer Portal 6 | - name: XYO Foundation Site 7 | url: https://xyo.network/ 8 | about: Check out the fundamentals of our Foundation here -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | 6 | jobs: 7 | build: 8 | 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - uses: actions/checkout@v2 13 | - name: cmake build 14 | run: | 15 | mkdir output 16 | cd output 17 | clang -c ../src/*.c 18 | cd .. -------------------------------------------------------------------------------- /src/ByteArray.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file ByteArray.h 3 | * @author Arie Trouw (developer@xy.company) 4 | * @brief 5 | * @version 0.1 6 | * @date 2018-11-15 7 | * 8 | * @copyright Copyright (c) 2018 XY - The Persistant COmpany 9 | * 10 | */ 11 | 12 | #ifndef BYTEARRAY_H 13 | #define BYTEARRAY_H 14 | 15 | #include 16 | 17 | typedef struct ByteArray{ 18 | uint32_t size; 19 | char reserved[2]; 20 | char* payload; 21 | } ByteArray_t; 22 | #endif 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### C ### 2 | # Prerequisites 3 | *.d 4 | 5 | # Object files 6 | *.o 7 | *.ko 8 | *.obj 9 | *.elf 10 | 11 | # Linker output 12 | *.ilk 13 | *.map 14 | *.exp 15 | 16 | # Precompiled Headers 17 | *.gch 18 | *.pch 19 | 20 | # Libraries 21 | *.lib 22 | *.a 23 | *.la 24 | *.lo 25 | 26 | # Shared objects (inc. Windows DLLs) 27 | *.dll 28 | *.so 29 | *.so.* 30 | *.dylib 31 | 32 | # Executables 33 | *.exe 34 | *.out 35 | *.app 36 | *.i*86 37 | *.x86_64 38 | *.hex 39 | 40 | # Debug files 41 | *.dSYM/ 42 | *.su 43 | *.idb 44 | *.pdb 45 | 46 | build 47 | output 48 | .DS_Store 49 | -------------------------------------------------------------------------------- /src/XYObjects/XYObjectTypes.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file major.h 3 | * @author Arie Trouw (developer@xy.company) 4 | * @brief 5 | * @version 0.1 6 | * @date 2018-11-16 7 | * 8 | * @copyright Copyright (c) 2018 XY - The Persistant COmpany 9 | * 10 | */ 11 | 12 | #define XY_TYPE_NULL (0x00) 13 | #define MAJOR_ARRAY (0x01) 14 | 15 | #define MAJOR_2BYTE_ARRAY (MAJOR_ARRAY | MAJOR_LENGTH_2BYTE) 16 | #define MAJOR_4BYTE_ARRAY (MAJOR_ARRAY | MAJOR_LENGTH_4BYTE) 17 | 18 | #define TYPE_NULL (0x00) 19 | 20 | #define TYPE_ARRAY (0x01) 21 | //#define TYPE_ARRAY_STRONG (0x02) -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Mac", 5 | "includePath": [ 6 | "src/include", 7 | "src/", 8 | "XYObjects/" 9 | ], 10 | "defines": [], 11 | "macFrameworkPath": [ 12 | "/System/Library/Frameworks", 13 | "/Library/Frameworks" 14 | ], 15 | "compilerPath": "/usr/bin/clang", 16 | "cppStandard": "c++17", 17 | "cStandard": "c99" 18 | } 19 | ], 20 | "version": 4 21 | } -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/documentation_needed.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Documentation needed 3 | about: Suggest documentation that is needed 4 | title: "[DOCUMENTATION]:" 5 | labels: documentation 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your documentation request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the documentation and format you would like** 14 | A clear and concise description of what documentation you would like to see and what type for format. 15 | Ex. Step-by-step, Paragraph explainer, screenshots, etc. 16 | 17 | **Additional context** 18 | Add any other context or screenshots about the document request here. -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "tasks": [ 6 | { 7 | "label": "Build", 8 | "type": "shell", 9 | "command": "if [ -d build ]; then rm -Rf build; fi&& mkdir build && cd build && cmake .. && make", 10 | "group": { 11 | "kind": "build", 12 | "isDefault": true 13 | } 14 | }, 15 | { 16 | "label": "Install Library ", 17 | "type": "shell", 18 | "command": "if [ -d build ]; then rm -Rf build; fi&& mkdir build && cd build && cmake .. && sudo make install" 19 | } 20 | ] 21 | } -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for XYO SDK Android 4 | title: "[FEATURE]" 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. This could include specific devices, android versions, etc.s -------------------------------------------------------------------------------- /src/XYObjects/XYObjectHeader.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //bit fields for the flags in the first byte of the header 4 | typedef struct XYHeaderFlags { 5 | uint8_t reserved: 4; /*----xxxx*/ 6 | uint8_t typed: 1; /*---x----*/ 7 | uint8_t iteratable: 1; /*--x-----*/ 8 | uint8_t lengthType: 2; /*xx------*/ 9 | } XYHeaderFlags_t; 10 | 11 | // the type of the object 12 | typedef struct XYObjectHeader { 13 | XYHeaderFlags_t flags; 14 | uint8_t type; 15 | } XYObjectHeader_t; 16 | 17 | #define XY_HEADER_OFFSET (0) 18 | #define XY_HEADER_LENGTH (sizeof(XYObjectHeader_t)) 19 | 20 | // Masks for deternining the method of length discovery for a given major 21 | #define XY_LENGTH_1BYTE (0x00) 22 | #define XY_LENGTH_2BYTE (0x01) 23 | #define XY_LENGTH_4BYTE (0x02) 24 | #define XY_LENGTH_8BYTE (0x03) 25 | 26 | #define XY_LENGTH_OFFSET (XY_HEADER_LENGTH) -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve the XYO Android SDK 4 | title: "[BUG]" 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Observed behavior** 11 | A clear and concise description of what exactly happened. 12 | 13 | **Expected behavior** 14 | A clear and concise description of what you expected to happen. 15 | 16 | **To Reproduce** 17 | Steps to reproduce the behavior: 18 | 1. Go to '...' 19 | 2. Click on '....' 20 | 3. Scroll down to '....' 21 | 4. See error 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Smartphone (please complete the following information):** 27 | - Device: [e.g. Samsung Galaxy, Google Pixel] 28 | - OS: [e.g. Android 10] 29 | - Browser [e.g. stock browser, chrome] 30 | 31 | **Additional context** 32 | Add any other context about the problem here. 33 | -------------------------------------------------------------------------------- /src/XYObjects/Array/Iterator.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "WeakArray/WeakArray.h" 3 | #include "../XYObject.h" 4 | #include "../XYObjectTypes.h" 5 | #include "../../ByteArray.h" 6 | 7 | typedef struct arrayItr 8 | { 9 | XYObjectHeader_t* header; 10 | char* base; 11 | XYObjectHeader_t* innerHeader; 12 | void *indexPtr; 13 | } XYArrayItr_t; 14 | 15 | XYArrayItr_t WeakArrayIterator(XYObjectHeader_t* header, char* buffer); 16 | XYObject_t IteratorNext(XYArrayItr_t* itr); 17 | XYObject_t IteratorGet(XYArrayItr_t* itr); 18 | 19 | #define XYCHECK_ITRERATOR(_ITR_) if(( (XYArrayItr_t)_ITR_.header != NULL && (XYArrayItr_t)_ITR_.payload != NULL){ result.status = ERR_CRITICAL} 20 | #define Iterator_gap(_self, _element, _bytesAfter, _offset) Iterator_bookmark(_self, _element, _bytesAfter, _offset, NULL) 21 | 22 | XYResult_t Iterator_bookmark(XYObject_t *self, uint32_t element, uint32_t bytesAfter, uint32_t offset, XYArrayItr_t* optional); 23 | XYResult_t Iterator_insert(XYObject_t* self, uint32_t element, uint32_t offset, uint32_t totalBytes, char* bytes); -------------------------------------------------------------------------------- /src/XYResult.test.c: -------------------------------------------------------------------------------- 1 | #ifndef XYRESULT_H 2 | #define XYRESULT_H 3 | 4 | /* 5 | * STRUCTURES 6 | **************************************************************************************** 7 | */ 8 | typedef struct XYResult{ 9 | int status; 10 | union { 11 | void* ptr; 12 | int i; 13 | char c; 14 | short s; 15 | unsigned int ui; 16 | unsigned char uc; 17 | unsigned short us; 18 | unsigned char bytes[2]; 19 | } value; 20 | } XYResult_t; 21 | 22 | #define XYERROR(_STATUS_) { XYResult_t result; result.status = _STATUS_; result.value.i = 0; return result; }; 23 | #define CHECK_NULL(_VALUE_) if (!_VALUE_) { XYERROR(XY_STATUS_ERROR); } 24 | #define CHECK_RESULT(_RESULT_) if (_RESULT_.status != XY_STATUS_OK) { return _RESULT_; }; 25 | 26 | #define XY_STATUS_OK (0) 27 | #define XY_STATUS_INDEXOUTOFRANGE (1) 28 | #define XY_STATUS_ERROR (255) 29 | 30 | // create a new result and initialize it to all zeros 31 | // this also sets the initial status to OK 32 | #define DECLARE_RESULT() XYResult_t result; memset(&result, 0, sizeof(XYResult_t)); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/XYResult.h: -------------------------------------------------------------------------------- 1 | #ifndef XYRESULT_H 2 | #define XYRESULT_H 3 | 4 | /* 5 | * STRUCTURES 6 | **************************************************************************************** 7 | */ 8 | typedef struct XYResult{ 9 | int status; 10 | union { 11 | void* ptr; 12 | int i; 13 | char c; 14 | short s; 15 | unsigned int ui; 16 | unsigned char uc; 17 | unsigned short us; 18 | unsigned char bytes[2]; 19 | } value; 20 | } XYResult_t; 21 | 22 | #define XYERROR(_STATUS_) { XYResult_t result; result.status = _STATUS_; result.value.i = 0; return result; }; 23 | #define XYSTATUS(_STATUS_) { result.status = _STATUS_; result.value.i = 0; return result; }; 24 | #define CHECK_NULL(_VALUE_) if (!_VALUE_) { XYERROR(XY_STATUS_ERROR); } 25 | #define CHECK_RESULT(_RESULT_) if (_RESULT_.status != XY_STATUS_OK) { return _RESULT_; }; 26 | 27 | #define XY_STATUS_OK (0) 28 | #define XY_STATUS_INDEXOUTOFRANGE (1) 29 | #define XY_STATUS_ERROR (255) 30 | 31 | // create a new result and initialize it to all zeros 32 | // this also sets the initial status to OK 33 | #define DECLARE_RESULT() XYResult_t result; memset(&result, 0, sizeof(XYResult_t)); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /src/WolfSSL/trng.h: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file trng.h 5 | * 6 | * @brief True Random Number Generator header file. 7 | * 8 | * Copyright (C) 2014 Dialog Semiconductor. 9 | * This computer program includes Confidential, Proprietary Information 10 | * of Dialog Semiconductor. All Rights Reserved. 11 | * 12 | * and contributors. 13 | * 14 | **************************************************************************************** 15 | */ 16 | 17 | #ifndef _TRNG_H_ 18 | #define _TRNG_H_ 19 | 20 | #include 21 | 22 | /* 23 | * FUNCTION DECLARATIONS 24 | **************************************************************************************** 25 | */ 26 | 27 | /** 28 | **************************************************************************************** 29 | * @brief Acquires 128 bits, saves them in trng_bits[] and restores the modified regs. 30 | * @param[in] trng_bits_ptr Pointer to number 31 | * @return void 32 | **************************************************************************************** 33 | */ 34 | void trng_acquire(uint8_t *trng_bits_ptr); 35 | 36 | #endif // _TRNG_H_ 37 | -------------------------------------------------------------------------------- /src/include/node/boundwitnessoption.h: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file boundwitnessoption.h 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary crypto routines for the XYO Core. 9 | * 10 | * Copyright (C) 2018 XY - The Findables Company 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | 20 | #ifndef BOUNDWITNESSOPTION_H 21 | #define BOUNDWITNESSOPTION_H 22 | //#include "XYObjects/XYObject.h" 23 | 24 | typedef struct BoundWitnessOption BoundWitnessOption_t; 25 | 26 | /** 27 | * A base class for bound witness options. For example a sentinel would have an option for 28 | * bridging. 29 | */ 30 | struct BoundWitnessOption { 31 | /** 32 | * The flag of the option. 33 | */ 34 | uint8_t flag; 35 | 36 | /* 37 | * Gets the signed data to include in the bound witness. 38 | */ 39 | XYObject_t* (*getSignedPayload)(void*); 40 | 41 | /* 42 | * Gets the unsigned payload to include in the bound witness. 43 | */ 44 | XYObject_t* (*getUnsignedPayload)(void*); 45 | }; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /src/include/node/nodelistener.h: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file nodelistener.h 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary crypto routines for the XYO Core. 9 | * 10 | * Copyright (C) 2018 XY - The Findables Company 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | #ifndef NODELISTENER_H 20 | #define NODELISTENER_H 21 | //#include "XYObjects/XYObject.h" 22 | //#include "BoundWitness.h" 23 | 24 | typedef struct NodeListener NodeListener; 25 | 26 | typedef struct BoundWitness BoundWitness_t; 27 | 28 | /** 29 | * A base class for bound witness options. For example a sentinel would have an option for 30 | * bridging. 31 | */ 32 | struct NodeListener { 33 | /** 34 | * This function will be called on every bound witness start. 35 | */ 36 | void (*onBoundWitnessStart)(void); 37 | 38 | /** 39 | * This function will be called on evey time a bound witness ended successfully. 40 | */ 41 | void (*onBoundWitnessDiscovered)(BoundWitness_t* boundWitness); 42 | 43 | /** 44 | * This function will be called on evey time a bound witness did not end successfully. 45 | */ 46 | void (*onBoundWitnessEndFailure)(void); 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /src/XYObjects/XYObject.test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "XYObject.h" 4 | #include "../defines.h" 5 | 6 | #define XY_DEBUGMODE 7 | #ifdef XY_DEBUGMODE 8 | #define XY_ASSERT_EQUAL(A, B) XY_ASSERT_EQUAL_ex(A, B, __FILE__, __LINE__) 9 | #define XY_ASSERT_EQUAL_ex(A, B, file, line) if(A != B){ printf(RED "%s@%d Assert Failed\n" RESET, file, line); score = score + 1; } 10 | #elif 11 | #define XY_ASSERT_EQUAL(A, B) if(A != B){ score = score + 1; } 12 | #endif 13 | 14 | int main(){ 15 | int score = 0; 16 | 17 | XYHeaderFlags_t signatureFlags; 18 | signatureFlags.typed = 0; 19 | signatureFlags.iteratable = 1; 20 | signatureFlags.lengthType = 1; 21 | 22 | XYObjectHeader_t signatureHeader; 23 | signatureHeader.flags = signatureFlags; 24 | signatureHeader.type = 0x12; 25 | 26 | XYObject_t signatureObject; 27 | signatureObject.header = &signatureHeader; 28 | signatureObject.payload = malloc(sizeof(char)*4); 29 | *(char*)&signatureObject.payload[0] = 1; 30 | *(char*)&signatureObject.payload[1] = 2; 31 | *(char*)&signatureObject.payload[2] = 3; 32 | *(char*)&signatureObject.payload[3] = 4; 33 | XYObject_t* self = &signatureObject; 34 | 35 | uint32_t size32 = XYOBJ_READ_UINT32(); 36 | uint32_t size16 = XYOBJ_READ_UINT16(); 37 | uint32_t size8 = XYOBJ_READ_UINT8(); 38 | 39 | XY_ASSERT_EQUAL(size32, 16909060); 40 | XY_ASSERT_EQUAL(size16, 258); 41 | XY_ASSERT_EQUAL(size8, 1); 42 | return score; 43 | 44 | 45 | 46 | 47 | } -------------------------------------------------------------------------------- /src/hash.h: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file hash.h 5 | * 6 | * @XY4 project source code. 7 | * 8 | * @brief primary hash routines for the XY4 firmware. 9 | * 10 | * Copyright (C) 2017 XY - The Findables Company. All Rights Reserved. 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | #ifndef HASH_H 16 | #define HASH_H 17 | 18 | /* 19 | * INCLUDES 20 | **************************************************************************************** 21 | */ 22 | #include "XYResult.h" 23 | #include "ByteArray.h" 24 | #include "XYObjects/XYObject.h" 25 | #include "WolfSSL/wc_sha256.h" // wc_ = wolf crypto library routine (SHA-256) 26 | 27 | extern XYResult_t preallocated_return_result; 28 | 29 | /* 30 | * DEFINES 31 | **************************************************************************************** 32 | */ 33 | 34 | /* 35 | * TYPE DEFINITIONS 36 | **************************************************************************************** 37 | */ 38 | typedef unsigned char byte; 39 | 40 | /* 41 | * FUNCTION DECLARATIONS 42 | **************************************************************************************** 43 | */ 44 | 45 | XYResult_t newHashProvider(void); 46 | XYResult_t hashGetId( void ); 47 | XYResult_t createHash(ByteArray_t* dataToHash); 48 | XYResult_t verifyHash(ByteArray_t* dataHashed, XYObject_t* hash); 49 | 50 | #endif 51 | 52 | // end of file hash.h 53 | -------------------------------------------------------------------------------- /src/nodebase.c: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file NodeBase.c 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary crypto routines for the XYO Core. 9 | * 10 | * Copyright (C) 2018 XY - The Findables Company 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | #include "state.h" 20 | #include "XYObjects/XYObject.h" 21 | #include "ByteArray.h" 22 | #include "network.h" 23 | #include "defines.h" 24 | #include "include/node/nodebase.h" 25 | 26 | 27 | /** 28 | **************************************************************************************** 29 | * NAME 30 | * initNode 31 | * 32 | * DESCRIPTION 33 | * Initializes the Node Base class with a repository and hashing provider, and sets up 34 | * the state and bound witness session. 35 | * 36 | * PARAMETERS 37 | * object [in] CryptoCreator* 38 | * 39 | * RETURNS 40 | * id [out] char* 41 | * 42 | * NOTES 43 | * 44 | **************************************************************************************** 45 | */ 46 | 47 | XYResult_t initNode(NodeBase_t* self){ 48 | INIT_SELF_UNKNOWN(); 49 | 50 | self->originChainState.index = 0; 51 | self->choice = NODE_MODE; 52 | memset(self->originChainState.latestHash, 0, 32); 53 | 54 | return result; 55 | } -------------------------------------------------------------------------------- /src/state.h: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file state.h 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary crypto routines for the XYO Core. 9 | * 10 | * Copyright (C) 2017 XY - The Findables Company. All Rights Reserved. 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | 20 | #ifndef STATE_H 21 | #include 22 | #include "XYObjects/XYObject.h" 23 | #include "crypto.h" 24 | //#include "ZigZagBoundWitnessSession.h" 25 | //#include "include/state/originchain.h" 26 | //#include "include/state/repository.h" 27 | 28 | /* 29 | * TYPE DEFINITIONS 30 | **************************************************************************************** 31 | */ 32 | 33 | typedef struct OriginChainState { 34 | Signer_t* currentSigner; 35 | char latestHash[32]; 36 | uint32_t index; 37 | } OriginChainState_t; 38 | 39 | 40 | /* 41 | * FUNCTION DECLARATIONS 42 | **************************************************************************************** 43 | */ 44 | 45 | XYResult_t newOriginBlock(OriginChainState_t* self_OriginChainState, ByteArray_t* originBlockHash); 46 | XYResult_t addSigner(OriginChainState_t* self_OriginChainState, Signer_t* user_Signer); 47 | XYResult_t getSigners(OriginChainState_t* self_OriginChainState); 48 | 49 | #define STATE_H 50 | #endif 51 | -------------------------------------------------------------------------------- /src/relaynode.h: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file nodebase.h 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary crypto routines for the XYO Core. 9 | * 10 | * Copyright (C) 2018 XY - The Findables Company 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | 16 | /* 17 | * INCLUDES 18 | **************************************************************************************** 19 | */ 20 | 21 | #ifndef RELAYNODE_H 22 | #include 23 | #include "XYResult.h" 24 | #include "defines.h" 25 | #include "XYObjects/XYObject.h" 26 | #include "state.h" 27 | #include "network.h" 28 | #include "include/node/nodebase.h" 29 | #include "XYObjects/Array/Iterator.h" 30 | #include "XYObjects/XYObject.h" 31 | 32 | typedef struct RelayNode RelayNode_t; 33 | 34 | struct RelayNode { 35 | NodeBase_t node; 36 | NodeListener listener; 37 | NetworkPipe_t networkPipe; 38 | }; 39 | 40 | 41 | uint8_t Relay_getChoice(uint8_t* theirCatalog); 42 | XYResult_t doConnection(RelayNode_t* self); 43 | XYResult_t insertPublicKey(RelayNode_t* relay); 44 | XYResult_t insertPayloads(RelayNode_t* relay); 45 | XYResult_t insertSignature(RelayNode_t* relay); 46 | extern XYResult_t socket_send(NetworkPipe_t* self, char* data, uint32_t count, uint8_t debug); 47 | extern XYResult_t socket_recv(NetworkPipe_t* self, char* data, uint32_t recvBytes); 48 | //extern NetworkPipe_t* findSomeoneToTalkTo( void ); 49 | 50 | //TODO: DEBUG extern 51 | extern char globalBuffer[1024]; 52 | #define NODEBASE_H 53 | #endif 54 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 2.6) 2 | project (sdk-core) 3 | # set the version number. 4 | set (SDK_VERSION_MAJOR 0) 5 | set (SDK_VERSION_MINOR 1) 6 | 7 | # bring in the headers 8 | include_directories(src/include) 9 | include_directories(src/WolfSSL) 10 | 11 | #find_package(PkgConfig REQUIRED) 12 | #pkg_search_module(OPENSSL REQUIRED openssl) 13 | find_package (Threads REQUIRED) 14 | 15 | file(GLOB_RECURSE SOURCES "src/*.c") 16 | 17 | 18 | # add the binary tree to the search path for include files 19 | # so that we will find TutorialConfig.h 20 | 21 | include_directories("${PROJECT_BINARY_DIR}") 22 | 23 | set(CMAKE_BUILD_TYPE Debug) 24 | #set(CMAKE_BUILD_TYPE Release) 25 | 26 | #find_program(iwyu_path NAMES iwyu-tool iwyu-fix-includes) 27 | #if(NOT iwyu_path) 28 | # message(FATAL_ERROR "Could not find the program include-what-you-use") 29 | #endif() 30 | 31 | # add options for warnings 32 | if(UNIX AND NOT APPLE) 33 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") 34 | if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 35 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Weverything -fsanitize=address -fsanitize=leak -Wno-padded -Wno-zero-length-array -Wno-unused-parameter -ferror-limit=50") 36 | endif() 37 | endif() 38 | # add the executable 39 | # add_executable(sdk-xyo ${SOURCES}) 40 | 41 | 42 | ADD_LIBRARY(sdk-xyo STATIC ${SOURCES}) 43 | target_link_libraries(sdk-xyo ${OPENSSL_LIBRARIES}) 44 | target_link_libraries (sdk-xyo ${CMAKE_THREAD_LIBS_INIT}) 45 | target_link_libraries (sdk-xyo wolfssl) 46 | 47 | set_property(TARGET sdk-xyo PROPERTY C_INCLUDE_WHAT_YOU_USE ${iwyu_path}) 48 | 49 | 50 | INSTALL(TARGETS sdk-xyo 51 | DESTINATION "lib" 52 | ) 53 | INSTALL ( 54 | DIRECTORY ${CMAKE_SOURCE_DIR}/src/ 55 | DESTINATION "include/xyo" 56 | FILES_MATCHING PATTERN "*.h*") 57 | -------------------------------------------------------------------------------- /src/XYObjects/Array/WeakArray/WeakArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../../XYResult.h" 4 | #include "../../XYObject.h" 5 | #include "../../XYObjectTypes.h" 6 | #include "../../../defines.h" 7 | //TODO: Debug 8 | #include 9 | 10 | /** 11 | * @brief Add Item from a Weak Array 12 | * 13 | * @param self 14 | * @param newItemHeader 15 | * @param newItemLength 16 | * @return XYResult_t 17 | */ 18 | XYResult_t WeakArray_add(XYObject_t *self, 19 | XYObjectHeader_t newItemHeader, 20 | int newItemLength); 21 | 22 | /** 23 | * @brief Get Item from a Weak Array 24 | * 25 | * @param self 26 | * @param index 27 | * @return XYResult_t 28 | */ 29 | XYResult_t WeakArray_get(XYObject_t *self, 30 | int index); 31 | 32 | 33 | #define XYOBJ_INCREMENT(_VALUE_) \ 34 | switch (((XYObject_t *)self)->header->flags.lengthType) \ 35 | { \ 36 | case XY_LENGTH_1BYTE: \ 37 | memset(((XYObject_t *)self)->payload, _VALUE_, 1); \ 38 | break; \ 39 | case XY_LENGTH_2BYTE: \ 40 | XYOBJ_INCREMENT_UINT16(_VALUE_); \ 41 | break; \ 42 | case XY_LENGTH_4BYTE: \ 43 | XYOBJ_INCREMENT_UINT32(_VALUE_); \ 44 | break; \ 45 | case XY_LENGTH_8BYTE: \ 46 | XYOBJ_INCREMENT_UINT64(_VALUE_); \ 47 | break; \ 48 | } 49 | 50 | uint8_t lengthTypeToLength(int _VALUE_); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [logo]: https://cdn.xy.company/img/brand/XYO_full_colored.png 2 | 3 | [![logo]](https://xyo.network) 4 | 5 | # sdk-core-c 6 | 7 | ![](https://github.com/XYOracleNetwork/sdk-core-c/workflows/Build/badge.svg) 8 | 9 | > The XYO Foundation provides this source code available in our efforts to advance the understanding of the XYO Procotol and its possible uses. We continue to maintain this software in the interest of developer education. Usage of this source code is not intended for production. 10 | 11 | ## Table of Contents 12 | 13 | - [Title](#sdk-core-c) 14 | - [Description](#description) 15 | - [Building](#building) 16 | - [Prerequisites](#prerequisites) 17 | - [Maintainers](#maintainers) 18 | - [License](#license) 19 | - [Credits](#credits) 20 | 21 | ## Description 22 | 23 | A library to perform all basic XYO Network functions. 24 | This includes creating an origin chain, maintaining an origin chain, negotiations for talking to other nodes, and other basic functionality. 25 | The library has heavily abstracted modules so that all operations will work with any crypto, storage, networking, etc. 26 | 27 | ## Building 28 | 29 | You can add sdk-core-c to your existing app by cloning the project, building the library, and linking it into your project. 30 | 31 | ``` 32 | git clone git@github.com:XYOracleNetwork/sdk-core-c.git 33 | cd sdk-core-c 34 | mkdir build && cd build 35 | cmake .. 36 | make 37 | 38 | This should compile a XYO library in the `build` folder. 39 | ``` 40 | 41 | To install the project run `sudo make install`. 42 | 43 | ## Prerequisites 44 | 45 | * C compiler 46 | * Cmake 47 | 48 | ## Maintainers 49 | 50 | - Arie Trouw 51 | 52 | ## License 53 | 54 | See the [LICENSE](LICENSE) file for license details. 55 | 56 | ## Credits 57 | 58 | Made with 🔥and ❄️ by [XYO](https://www.xyo.network) 59 | -------------------------------------------------------------------------------- /src/include/state/originchain.h: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file originchain.h 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary crypto routines for the XYO Core. 9 | * 10 | * Copyright (C) 2018 XY - The Findables Company 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include "repository.h" 24 | 25 | #ifndef ORIGINCHAIN_H 26 | #define ORIGINCHAIN_H 27 | 28 | /* 29 | * TYPE DEFINITIONS 30 | **************************************************************************************** 31 | */ 32 | 33 | typedef struct OriginChainNavigator OriginChainNavigator; 34 | 35 | typedef struct OriginChainNavigatorT { 36 | XYResult_t* (*addBoundWitness)(struct OriginChainNavigatorT* self, BoundWitness_t*); 37 | XYResult_t* (*containsOriginBlock)(struct OriginChainNavigatorT* self_OriginChainNavigator, BoundWitness_t* user_BoundWitness); 38 | OriginChainProvider_t* originChainRepository; 39 | HashProvider_t* Hash; 40 | ByteArray_t* *bridgeQueue; 41 | uint32_t queueLen; 42 | char lastHash[32]; 43 | 44 | } OriginChainNavigator_t; 45 | #endif 46 | 47 | 48 | //OriginChainNavigator* initOriginChainProvider( OriginChainNavigator** self, char* bits); 49 | XYResult_t* getMostRecentOriginBlock(OriginChainNavigator_t* self_OriginChainNavigator); 50 | XYResult_t* containsOriginBlock(OriginChainNavigator_t* self_OriginChainNavigator, BoundWitness_t* user_BoundWitness); 51 | XYResult_t* addBoundWitness(OriginChainNavigator_t* self_OriginChainNavigator, BoundWitness_t* user_BoundWitness); 52 | -------------------------------------------------------------------------------- /src/endian.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file endian.h 3 | * @author Arie Trouw (developer@xy.company) 4 | * @brief Convert 16 and 32 but numebrs between endians 5 | * @version 0.1 6 | * @date 2018-11-16 7 | * 8 | * @copyright Copyright (c) 2018 XY - The Persistant COmpany 9 | * 10 | */ 11 | 12 | #ifndef ENDIAN_H 13 | #define ENDIAN_H 14 | 15 | #include 16 | 17 | /* 18 | * FUNCTIONS 19 | **************************************************************************************** 20 | */ 21 | 22 | /** 23 | * @brief Convert 64-bit Value from Big Endian to Platform Endian 24 | * 25 | * @param char 26 | * @return uint32_t 27 | */ 28 | uint64_t to_uint64(unsigned char* data); 29 | 30 | /** 31 | * @brief Convert 32-bit Value from Big Endian to Platform Endian 32 | * 33 | * @param char 34 | * @return uint32_t 35 | */ 36 | uint32_t to_uint32(unsigned char* data); 37 | 38 | /** 39 | * @brief Convert 16-bit Value from Big Endian to Platform Endian 40 | * 41 | * @param char 42 | * @return uint16_t 43 | */ 44 | uint16_t to_uint16(unsigned char* data); 45 | 46 | /** 47 | * @brief Write 64-Bit Big Endian value to a buffer from a Platform Endian source 48 | * 49 | * @param char 50 | * @param value 51 | */ 52 | void to_uint64_be(unsigned char* dest, uint64_t value); 53 | 54 | /** 55 | * @brief Write 32-Bit Big Endian value to a buffer from a Platform Endian source 56 | * 57 | * @param char 58 | * @param value 59 | */ 60 | void to_uint32_be(unsigned char* dest, uint32_t value); 61 | 62 | /** 63 | * @brief Write 16-Bit Big Endian value to a buffer from a Platform Endian source 64 | * 65 | * @param char 66 | * @param value 67 | */ 68 | void to_uint16_be(unsigned char* dest, uint16_t value); 69 | 70 | /** 71 | * @brief Detect if the machine running is littleEndian. 72 | * 73 | * @param char 74 | * @param value 75 | */ 76 | int littleEndian(void); 77 | 78 | #endif -------------------------------------------------------------------------------- /src/WolfSSL/wc_md4.h: -------------------------------------------------------------------------------- 1 | /* md4.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | /*! 23 | \file wolfssl/wolfcrypt/md4.h 24 | */ 25 | 26 | #ifndef WOLF_CRYPT_MD4_H 27 | #define WOLF_CRYPT_MD4_H 28 | 29 | //#include 30 | #include "wc_types.h" 31 | 32 | #ifndef NO_MD4 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | /* in bytes */ 39 | enum { 40 | MD4 = WC_HASH_TYPE_MD4, 41 | MD4_BLOCK_SIZE = 64, 42 | MD4_DIGEST_SIZE = 16, 43 | MD4_PAD_SIZE = 56 44 | }; 45 | 46 | 47 | /* MD4 digest */ 48 | typedef struct Md4 { 49 | word32 buffLen; /* in bytes */ 50 | word32 loLen; /* length in bytes */ 51 | word32 hiLen; /* length in bytes */ 52 | word32 digest[MD4_DIGEST_SIZE / sizeof(word32)]; 53 | word32 buffer[MD4_BLOCK_SIZE / sizeof(word32)]; 54 | } Md4; 55 | 56 | 57 | WOLFSSL_API void wc_InitMd4(Md4*); 58 | WOLFSSL_API void wc_Md4Update(Md4*, const byte*, word32); 59 | WOLFSSL_API void wc_Md4Final(Md4*, byte*); 60 | 61 | 62 | #ifdef __cplusplus 63 | } /* extern "C" */ 64 | #endif 65 | 66 | #endif /* NO_MD4 */ 67 | #endif /* WOLF_CRYPT_MD4_H */ 68 | 69 | -------------------------------------------------------------------------------- /src/network.h: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file network.h 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary crypto routines for the XYO Core. 9 | * 10 | * Copyright (C) 2017 XY - The Findables Company. All Rights Reserved. 11 | * 12 | **************************************************************************************** 13 | * INCLUDES 14 | **************************************************************************************** 15 | */ 16 | 17 | #ifndef NETWORK_H 18 | #define NETWORK_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include "defines.h" 31 | #include "ByteArray.h" 32 | #include "XYResult.h" 33 | 34 | typedef struct ProcedureCatalogue ProcedureCatalogue_t; 35 | typedef struct NetworkPipe NetworkPipe_t; 36 | typedef struct NetworkPeer NetworkPeer_t; 37 | 38 | struct NetworkPeer{ 39 | /* 40 | * Sentinel-known implementation specific private variables 41 | * They can be erased in later networking impelmentations, 42 | * the only file that uses these variables at the time 43 | * of writing is network.c for TCP. 44 | */ 45 | int socket; 46 | struct sockaddr_in address; 47 | uint16_t port; 48 | }; 49 | 50 | struct NetworkPipe{ 51 | NetworkPeer_t peer; 52 | uint8_t role; 53 | ByteArray_t scratchBuffer; 54 | char theirCatalog[CATALOG_BUFFER_SIZE+4]; 55 | }; 56 | 57 | 58 | struct client_arguments{ 59 | uint8_t flags; 60 | pthread_t* server; 61 | struct sockaddr_in* *peers; 62 | uint8_t peerCount; 63 | }; 64 | 65 | struct server_arguments{ 66 | uint8_t flags; 67 | pthread_t* client; 68 | int port; 69 | }; 70 | 71 | void* serverThread(void* flag); 72 | void* clientThread(void* args); 73 | extern XYResult_t findPeer(NetworkPipe_t* pipe, char* buffer, uint32_t size, uint8_t flags); 74 | extern uint8_t canDo(ByteArray_t* catalog); 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /src/XYObjects/XYObject.c: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file xyobject.c 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary xy object routines for the XYO Core. 9 | * 10 | * Copyright (C) 2018 XY - The Findables Company 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | 20 | #include "XYObject.h" 21 | 22 | /** 23 | * ==== Private Functions ==== 24 | */ 25 | 26 | //assuming validation by caller 27 | int getLength(XYObject_t* self) { 28 | switch(self->header->flags.lengthType) { 29 | case XY_LENGTH_1BYTE: 30 | return XYOBJ_READ_UINT8(); 31 | case XY_LENGTH_2BYTE: 32 | return XYOBJ_READ_UINT16(); 33 | case XY_LENGTH_4BYTE: 34 | return XYOBJ_READ_UINT32(); 35 | case XY_LENGTH_8BYTE: 36 | return XYOBJ_READ_UINT64(); 37 | } 38 | 39 | return 0; 40 | } 41 | 42 | //assuming validation by caller 43 | int getLengthFieldSize(XYObject_t* self) { 44 | switch(self->header->flags.lengthType) { 45 | case XY_LENGTH_1BYTE: 46 | return 1; 47 | case XY_LENGTH_2BYTE: 48 | return 2; 49 | case XY_LENGTH_4BYTE: 50 | return 4; 51 | case XY_LENGTH_8BYTE: 52 | return 8; 53 | } 54 | 55 | return 0; 56 | } 57 | 58 | /** 59 | * ==== Public Functions ==== 60 | */ 61 | 62 | XYResult_t XYObject_getValue(XYObject_t* self) { 63 | INIT_SELF_UNKNOWN(); 64 | result.value.ptr = ((uint8_t*)self) + XY_HEADER_LENGTH + getLengthFieldSize(self); 65 | return result; 66 | } 67 | 68 | XYResult_t XYObject_getLength(XYObject_t* self) { 69 | INIT_SELF_UNKNOWN(); 70 | result.value.i = getLength(self); 71 | return result; 72 | } 73 | 74 | XYResult_t XYObject_getFullLength(XYObject_t* self) { 75 | INIT_SELF_UNKNOWN(); 76 | result.value.i = getLength(self) + XY_HEADER_LENGTH; 77 | return result; 78 | } 79 | 80 | uint8_t matchType(XYObject_t* obj, uint8_t type){ 81 | switch(obj->header->type){ 82 | case 1: 83 | return obj->header->type == type; 84 | break; 85 | case 2: 86 | if(type == MINOR_ARRAY){ 87 | return TRUE; 88 | } else { 89 | return FALSE; 90 | } 91 | break; 92 | default: 93 | return obj->header->type == type; 94 | break; 95 | } 96 | } -------------------------------------------------------------------------------- /src/endian.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file endian.c 3 | * @author Arie Trouw (developer@xy.company) 4 | * @brief Convert 16 and 32 but numebrs between endians 5 | * @version 0.1 6 | * @date 2018-11-16 7 | * 8 | * @copyright Copyright (c) 2018 XY - The Persistant COmpany 9 | * 10 | */ 11 | #include "endian.h" 12 | 13 | uint64_t to_uint64(unsigned char *data) 14 | { 15 | return ((uint64_t)data[0] << 56) + ((uint64_t)data[1] << 48) + ((uint64_t)data[2] << 40) + ((uint64_t)data[3] << 32) + ((uint64_t)data[4] << 24) + ((uint64_t)data[5] << 16) + ((uint64_t)data[6] << 8) + (uint64_t)data[7]; 16 | } 17 | 18 | uint32_t to_uint32(unsigned char *data) 19 | { 20 | return (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]; 21 | } 22 | 23 | uint16_t to_uint16(unsigned char *data) 24 | { 25 | return (data[0] << 8) + data[1]; 26 | } 27 | 28 | void to_uint64_be(unsigned char *dest, uint64_t value) 29 | { 30 | dest[0] = (unsigned char)((value & 0xff00000000000000) >> 56); 31 | dest[1] = (unsigned char)((value & 0xff000000000000) >> 48); 32 | dest[2] = (unsigned char)((value & 0xff0000000000) >> 40); 33 | dest[3] = (unsigned char)((value & 0xff00000000) >> 32); 34 | dest[4] = (unsigned char)((value & 0xff000000) >> 24); 35 | dest[5] = (unsigned char)((value & 0xff0000) >> 16); 36 | dest[6] = (unsigned char)((value & 0xff00) >> 8); 37 | dest[7] = (unsigned char)(value & 0xff); 38 | } 39 | 40 | void to_uint32_be(unsigned char *dest, uint32_t value) 41 | { 42 | dest[0] = (unsigned char)((value & 0xff000000) >> 24); 43 | dest[1] = (unsigned char)((value & 0xff0000) >> 16); 44 | dest[2] = (unsigned char)((value & 0xff00) >> 8); 45 | dest[3] = (unsigned char)(value & 0xff); 46 | } 47 | 48 | void to_uint16_be(unsigned char *dest, uint16_t value) 49 | { 50 | dest[0] = (unsigned char)((value & 0xff00) >> 8); 51 | dest[1] = (unsigned char)(value & 0xff); 52 | } 53 | 54 | /*----------------------------------------------------------------------------* 55 | * NAME 56 | * littleEndian 57 | * 58 | * DESCRIPTION 59 | * Determines the endian of the device we are running on. 60 | * 61 | * PARAMETERS 62 | * none 63 | * 64 | * RETURNS 65 | * result [out] bool returns TRUE if Little Endian endian, FALSE 66 | * if Big Endian. 67 | * NOTES 68 | * 69 | *---------------------------------------------------------------------------- 70 | */ 71 | int littleEndian(void){ 72 | 73 | volatile uint32_t i=0x01234567; 74 | // return 0 for big endian, 1 for little endian. 75 | return (*((uint8_t*)(&i))) == 0x67; 76 | } -------------------------------------------------------------------------------- /src/WolfSSL/wc_wolfmath.h: -------------------------------------------------------------------------------- 1 | /* wolfmath.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | #if defined(HAVE_WOLF_BIGINT) && !defined(WOLF_BIGINT_DEFINED) 23 | /* raw big integer */ 24 | typedef struct WC_BIGINT { 25 | byte* buf; 26 | word32 len; 27 | void* heap; 28 | } WC_BIGINT; 29 | 30 | #define WOLF_BIGINT_DEFINED 31 | #endif 32 | 33 | 34 | /* only define functions if mp_int has been declared */ 35 | //#define MP_INT_DEFINED 36 | #ifdef MP_INT_DEFINED 37 | 38 | #ifndef __WOLFMATH_H__ 39 | #define __WOLFMATH_H__ 40 | 41 | /* timing resistance array */ 42 | #if !defined(WC_NO_CACHE_RESISTANT) && \ 43 | ((defined(HAVE_ECC) && defined(ECC_TIMING_RESISTANT)) || \ 44 | (defined(USE_FAST_MATH) && defined(TFM_TIMING_RESISTANT))) 45 | 46 | extern const wolfssl_word wc_off_on_addr[2]; 47 | #endif 48 | 49 | /* common math functions */ 50 | int get_digit_count(mp_int* a); 51 | mp_digit get_digit(mp_int* a, int n); 52 | int get_rand_digit(WC_RNG* rng, mp_digit* d); 53 | int mp_rand(mp_int* a, int digits, WC_RNG* rng); 54 | 55 | 56 | #ifdef HAVE_WOLF_BIGINT 57 | void wc_bigint_init(WC_BIGINT* a); 58 | int wc_bigint_alloc(WC_BIGINT* a, word32 sz); 59 | int wc_bigint_from_unsigned_bin(WC_BIGINT* a, const byte* in, word32 inlen); 60 | int wc_bigint_to_unsigned_bin(WC_BIGINT* a, byte* out, word32* outlen); 61 | void wc_bigint_zero(WC_BIGINT* a); 62 | void wc_bigint_free(WC_BIGINT* a); 63 | 64 | int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst); 65 | int wc_mp_to_bigint_sz(mp_int* src, WC_BIGINT* dst, word32 sz); 66 | int wc_bigint_to_mp(WC_BIGINT* src, mp_int* dst); 67 | #endif /* HAVE_WOLF_BIGINT */ 68 | 69 | #endif /* __WOLFMATH_H__ */ 70 | 71 | #endif /* MP_INT_DEFINED */ 72 | -------------------------------------------------------------------------------- /src/crypto.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file crypto.h 3 | * @author Arie Trouw (developer@xy.company) 4 | * @brief Primary crypto routines for the XY4 firmware. 5 | * @version 0.1 6 | * @date 2018-11-15 7 | * 8 | * @copyright Copyright (c) 2018 XY - The Persistant COmpany 9 | * 10 | */ 11 | 12 | #ifndef XYCRYPTO 13 | #define XYCRYPTO 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | 20 | #include "hash.h" // includes "xyobject.h" 21 | #include "WolfSSL/wc_types.h" 22 | #include "WolfSSL/wc_rsa.h" 23 | #include "WolfSSL/trng.h" 24 | #include "WolfSSL/wc_ecc.h" 25 | #include "WolfSSL/wc_random.h" 26 | 27 | /* 28 | * DEFINES 29 | **************************************************************************************** 30 | */ 31 | 32 | #define PRIVATE_KEY_LENGTH_2048 2048 33 | #define PRIVATE_KEY_LENGTH_1024 1024 34 | 35 | /* 36 | * TYPE DEFINITIONS 37 | **************************************************************************************** 38 | */ 39 | 40 | typedef struct Signer Signer_t; 41 | typedef struct CryptoCreator CryptoCreator_t; 42 | 43 | typedef struct{ 44 | char* publicKey; 45 | char* privateKey; 46 | } keyPairStruct; 47 | 48 | extern XYResult_t preallocated_return_result; 49 | 50 | /* 51 | * STRUCTURES 52 | **************************************************************************************** 53 | */ 54 | 55 | struct Signer{ 56 | 57 | ByteArray_t publicKey; // Cryptographic Public Key 58 | ByteArray_t privateKey; // Cryptographic Private Key 59 | }; 60 | 61 | /* 62 | * FUNCTION DECLARATIONS 63 | **************************************************************************************** 64 | */ 65 | 66 | XYResult_t* newCryptoSignerInstance(ByteArray_t* privateKey); 67 | XYResult_t* newCryptoCreator(void); 68 | XYResult_t* getPublicKeyId(Signer_t* signer); 69 | XYResult_t* getSignatureId(Signer_t* signer); 70 | XYResult_t* XyoCryptoSigner (XYObject_t* privateKey); 71 | XYResult_t* getPublicKey(Signer_t* signer); 72 | XYResult_t* sign(Signer_t* signer, ByteArray_t* dataToSign); 73 | XYResult_t* verify(Signer_t* signer, ByteArray_t* signedData, XYObject_t* signature, XYObject_t* publicKey); 74 | XYResult_t* xyencrypt(Signer_t* signer, ByteArray_t* unEncrypedData); 75 | XYResult_t* xydecrypt(Signer_t* signer, ByteArray_t* encrypedData); 76 | XYResult_t* getPrivateKey(Signer_t* signer); 77 | XYResult_t* newPrivateKey(void); 78 | 79 | XYResult_t* cryptoGetId(CryptoCreator_t* object); 80 | XYResult_t* generateNewKeyPair(void); 81 | XYResult_t* newKeyPair(void); 82 | XYResult_t* newPublicKey(Signer_t* signer); 83 | 84 | #endif 85 | 86 | // end of file crypto.h 87 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_visibility.h: -------------------------------------------------------------------------------- 1 | /* visibility.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | /* Visibility control macros */ 24 | 25 | #ifndef WOLF_CRYPT_VISIBILITY_H 26 | #define WOLF_CRYPT_VISIBILITY_H 27 | 28 | 29 | /* for compatibility and so that fips is using same name of macro @wc_fips */ 30 | #ifdef HAVE_FIPS 31 | #include 32 | #define WOLFSSL_API CYASSL_API 33 | #define WOLFSSL_LOCAL CYASSL_LOCAL 34 | #else 35 | 36 | /* WOLFSSL_API is used for the public API symbols. 37 | It either imports or exports (or does nothing for static builds) 38 | 39 | WOLFSSL_LOCAL is used for non-API symbols (private). 40 | */ 41 | 42 | #if defined(BUILDING_WOLFSSL) 43 | #if defined(HAVE_VISIBILITY) && HAVE_VISIBILITY 44 | #define WOLFSSL_API __attribute__ ((visibility("default"))) 45 | #define WOLFSSL_LOCAL __attribute__ ((visibility("hidden"))) 46 | #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550) 47 | #define WOLFSSL_API __global 48 | #define WOLFSSL_LOCAL __hidden 49 | #elif defined(_MSC_VER) || defined(__MINGW32__) 50 | #if defined(WOLFSSL_DLL) 51 | #define WOLFSSL_API __declspec(dllexport) 52 | #else 53 | #define WOLFSSL_API 54 | #endif 55 | #define WOLFSSL_LOCAL 56 | #else 57 | #define WOLFSSL_API 58 | #define WOLFSSL_LOCAL 59 | #endif /* HAVE_VISIBILITY */ 60 | #else /* BUILDING_WOLFSSL */ 61 | #if defined(_MSC_VER) || defined(__MINGW32__) 62 | #if defined(WOLFSSL_DLL) 63 | #define WOLFSSL_API __declspec(dllimport) 64 | #else 65 | #define WOLFSSL_API 66 | #endif 67 | #define WOLFSSL_LOCAL 68 | #else 69 | #define WOLFSSL_API 70 | #define WOLFSSL_LOCAL 71 | #endif 72 | #endif /* BUILDING_WOLFSSL */ 73 | 74 | #endif /* HAVE_FIPS */ 75 | #endif /* WOLF_CRYPT_VISIBILITY_H */ 76 | 77 | -------------------------------------------------------------------------------- /src/state.c: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file state.c 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary state routines for the XYO Core. 9 | * 10 | * Copyright (C) 2017 XY - The Findables Company. All Rights Reserved. 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | 20 | #include 21 | #include "state.h" 22 | 23 | /*----------------------------------------------------------------------------* 24 | * NAME 25 | * newOriginBlock 26 | * 27 | * DESCRIPTION 28 | * Append block hash to xyo network state object and update index/signer. 29 | * 30 | * PARAMETERS 31 | * *OriginChainNavigator [in] self_OriginChainNavigator* 32 | * *ByteArray [in] originBlockHash* 33 | * 34 | * RETURNS 35 | * XYResult_t* [out] bool Returns OK if success 36 | *----------------------------------------------------------------------------*/ 37 | XYResult_t newOriginBlock(OriginChainState_t* self_OriginChainState, 38 | ByteArray_t* originBlockHash) { 39 | 40 | XYResult_t result; 41 | return result; 42 | } 43 | 44 | /*----------------------------------------------------------------------------* 45 | * NAME 46 | * addSigner 47 | * 48 | * DESCRIPTION 49 | * Add a signer object to the state. 50 | * 51 | * PARAMETERS 52 | * *OriginChainNavigator [in] self_OriginChainNavigator* 53 | * *ByteArray [in] user_Signer* 54 | * 55 | * RETURNS 56 | * XYResult_t* [out] bool Returns OK if success 57 | *----------------------------------------------------------------------------*/ 58 | XYResult_t addSigner(OriginChainState_t* self_OriginChainState, Signer_t* user_Signer) { 59 | XYResult_t result; 60 | return result; 61 | } 62 | 63 | /** 64 | **************************************************************************************** 65 | * NAME 66 | * getSigners 67 | * 68 | * DESCRIPTION 69 | * Gets the current Signer object in use. 70 | * 71 | * PARAMETERS 72 | * self [in] OriginChainState_t* 73 | * 74 | * RETURNS 75 | * Signer [out] XYResult 76 | * 77 | * NOTES 78 | * 79 | **************************************************************************************** 80 | */ 81 | XYResult_t getSigners(OriginChainState_t* self_OriginChainState){ 82 | XYResult_t result; 83 | return result; 84 | } 85 | // end of file state.c 86 | -------------------------------------------------------------------------------- /src/XYObjects/Array/WeakArray/WeakArray.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file WeakArray.c 3 | * @author Arie Trouw (developer@xy.company) 4 | * @brief 5 | * @version 0.1 6 | * @date 2018-11-15 7 | * 8 | * @copyright Copyright (c) 2018 XY - The Persistant COmpany 9 | * 10 | */ 11 | 12 | #include "WeakArray.h" 13 | #include 14 | 15 | uint8_t lengthTypeToLength(int _VALUE_){ 16 | switch (_VALUE_) 17 | { 18 | case XY_LENGTH_1BYTE: 19 | return 1; 20 | break; 21 | case XY_LENGTH_2BYTE: 22 | return 2; 23 | break; 24 | case XY_LENGTH_4BYTE: 25 | return 4; 26 | break; 27 | case XY_LENGTH_8BYTE: 28 | return 8; 29 | break; 30 | } 31 | return 255; 32 | } 33 | 34 | XYResult_t WeakArray_add(XYObject_t *self, 35 | XYObjectHeader_t newItemHeader, 36 | int newItemLength) 37 | { 38 | INIT_SELF(TYPE_ARRAY); 39 | 40 | XYResult_t currentLength = XYObject_getLength(self); 41 | CHECK_RESULT(currentLength); 42 | 43 | XYResult_t fullLength = XYObject_getFullLength(self); 44 | CHECK_RESULT(fullLength); 45 | 46 | //put the new object at the end of the current object 47 | uint8_t *newObject = (((XYObject_t *)self)->payload + currentLength.value.i); 48 | 49 | //set the type of the newly added item 50 | if(!self->header->flags.typed || currentLength.value.ui == lengthTypeToLength(self->header->flags.lengthType)){ 51 | memcpy(newObject, &newItemHeader, sizeof(newItemHeader)); 52 | } 53 | 54 | //set the new length of the array object (old length + new object length) 55 | XYOBJ_INCREMENT(newItemLength); 56 | 57 | return result; 58 | } 59 | 60 | XYResult_t WeakArray_get(XYObject_t *self, int index) 61 | { 62 | INIT_SELF(TYPE_ARRAY); 63 | 64 | XYResult_t fullLength = XYObject_getFullLength(self); 65 | CHECK_RESULT(fullLength); 66 | 67 | //the nextPtr points to the first byte after the end of this object 68 | //we use this to prevent over flow and to check for end of list 69 | uint8_t *outOfBoundsPtr = ((XYObject_t *)self)->payload + fullLength.value.i; 70 | 71 | XYResult_t currentValue = XYObject_getValue(self); 72 | CHECK_RESULT(currentValue); 73 | 74 | //we use the ptr to iterate over the items in the array 75 | uint8_t *ptr = currentValue.value.ptr; 76 | XYResult_t lengthOfSubObject; 77 | 78 | for (int i = 0; i < index; i++) 79 | { 80 | if (ptr >= outOfBoundsPtr) 81 | { 82 | XYERROR(XY_STATUS_INDEXOUTOFRANGE) 83 | } 84 | lengthOfSubObject = XYObject_getFullLength((XYObject_t *)ptr); 85 | CHECK_RESULT(lengthOfSubObject); 86 | ptr += lengthOfSubObject.value.i; 87 | } 88 | 89 | //we iterated past all the other items, so we should be pointing to the item they need now 90 | result.value.ptr = ptr; 91 | 92 | return result; 93 | } 94 | 95 | // end of file WeakArray.c 96 | -------------------------------------------------------------------------------- /src/repository.c: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file repository.c 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary crypto routines for the XYO Core. 9 | * 10 | * Copyright (C) 2018 XY - The Findables Company 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | #include "repository.h" 20 | 21 | /*----------------------------------------------------------------------------* 22 | * NAME 23 | * initOriginChainProvider 24 | * 25 | * DESCRIPTION 26 | * Initializes an origin chain provider as a Int Strong Array 27 | * 28 | * PARAMETERS 29 | * *OriginChainNavigator [in] self_OriginChainNavigator* 30 | * *ByteArray [in] originBlockHash* 31 | * 32 | * RETURNS 33 | * XYResult_t* [out] bool Returns OK if success 34 | *----------------------------------------------------------------------------*/ 35 | OriginChainProvider_t* initOriginChainProvider(){ 36 | return NULL; 37 | 38 | } 39 | 40 | /*----------------------------------------------------------------------------* 41 | * NAME 42 | * append 43 | * 44 | * DESCRIPTION 45 | * Append block to the Repository Provider, in this case a ram disk. 46 | * 47 | * PARAMETERS 48 | * *OriginChainNavigator [in] self_OriginChainNavigator* 49 | * *ByteArray [in] originBlockHash* 50 | * 51 | * RETURNS 52 | * XYResult_t* [out] bool Returns OK if success 53 | *----------------------------------------------------------------------------*/ 54 | XYResult_t* append(OriginChainProvider_t* self, ByteArray_t* value, uint16_t timeout){ 55 | return NULL; 56 | } 57 | 58 | /*----------------------------------------------------------------------------* 59 | * NAME 60 | * getChain 61 | * 62 | * DESCRIPTION 63 | * Get the location of the origin chain on disk and return the array object. 64 | * 65 | * PARAMETERS 66 | * *OriginChainNavigator [in] self* 67 | * 68 | * RETURNS 69 | * XYResult_t* [out] XYObject_t* IntStrongArray 70 | *----------------------------------------------------------------------------*/ 71 | XYResult_t* getChain(OriginChainProvider_t* self){ 72 | return NULL; 73 | } 74 | 75 | /*----------------------------------------------------------------------------* 76 | * NAME 77 | * deleteChain 78 | * 79 | * DESCRIPTION 80 | * Nullify and clear the origin chain. 81 | * 82 | * PARAMETERS 83 | * *OriginChainNavigator [in] self* 84 | * 85 | * RETURNS 86 | * XYResult_t* [out] XYObject_t* IntStrongArray 87 | *----------------------------------------------------------------------------*/ 88 | XYResult_t* deleteChain(OriginChainProvider_t* self){ 89 | return NULL; 90 | } 91 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_mpi_superclass.h: -------------------------------------------------------------------------------- 1 | /* mpi_superclass.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | 24 | /* super class file for PK algos */ 25 | 26 | /* default ... include all MPI */ 27 | #define LTM_ALL 28 | 29 | /* RSA only (does not support DH/DSA/ECC) */ 30 | /* #define SC_RSA_1 */ 31 | 32 | /* For reference.... On an Athlon64 optimizing for speed... 33 | 34 | LTM's mpi.o with all functions [striped] is 142KiB in size. 35 | 36 | */ 37 | 38 | /* Works for RSA only, mpi.o is 68KiB */ 39 | #ifdef SC_RSA_1 40 | #define BN_MP_SHRINK_C 41 | #define BN_MP_LCM_C 42 | #define BN_MP_PRIME_RANDOM_EX_C 43 | #define BN_MP_INVMOD_C 44 | #define BN_MP_GCD_C 45 | #define BN_MP_MOD_C 46 | #define BN_MP_MULMOD_C 47 | #define BN_MP_ADDMOD_C 48 | #define BN_MP_EXPTMOD_C 49 | #define BN_MP_SET_INT_C 50 | #define BN_MP_INIT_MULTI_C 51 | #define BN_MP_CLEAR_MULTI_C 52 | #define BN_MP_UNSIGNED_BIN_SIZE_C 53 | #define BN_MP_TO_UNSIGNED_BIN_C 54 | #define BN_MP_MOD_D_C 55 | #define BN_MP_PRIME_RABIN_MILLER_TRIALS_C 56 | #define BN_REVERSE_C 57 | #define BN_PRIME_TAB_C 58 | 59 | /* other modifiers */ 60 | #define BN_MP_DIV_SMALL /* Slower division, not critical */ 61 | 62 | /* here we are on the last pass so we turn things off. The functions classes are still there 63 | * but we remove them specifically from the build. This also invokes tweaks in functions 64 | * like removing support for even moduli, etc... 65 | */ 66 | #ifdef LTM_LAST 67 | #undef BN_MP_TOOM_MUL_C 68 | #undef BN_MP_TOOM_SQR_C 69 | #undef BN_MP_KARATSUBA_MUL_C 70 | #undef BN_MP_KARATSUBA_SQR_C 71 | #undef BN_MP_REDUCE_C 72 | #undef BN_MP_REDUCE_SETUP_C 73 | #undef BN_MP_DR_IS_MODULUS_C 74 | #undef BN_MP_DR_SETUP_C 75 | #undef BN_MP_DR_REDUCE_C 76 | #undef BN_MP_REDUCE_IS_2K_C 77 | #undef BN_MP_REDUCE_2K_SETUP_C 78 | #undef BN_MP_REDUCE_2K_C 79 | #undef BN_S_MP_EXPTMOD_C 80 | #undef BN_MP_DIV_3_C 81 | #undef BN_S_MP_MUL_HIGH_DIGS_C 82 | #undef BN_FAST_S_MP_MUL_HIGH_DIGS_C 83 | #undef BN_FAST_MP_INVMOD_C 84 | 85 | /* To safely undefine these you have to make sure your RSA key won't exceed the Comba threshold 86 | * which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines] 87 | * which means roughly speaking you can handle up to 2536-bit RSA keys with these defined without 88 | * trouble. 89 | */ 90 | #undef BN_S_MP_MUL_DIGS_C 91 | #undef BN_S_MP_SQR_C 92 | #undef BN_MP_MONTGOMERY_REDUCE_C 93 | #endif 94 | 95 | #endif 96 | 97 | -------------------------------------------------------------------------------- /src/include/node/nodebase.h: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file nodebase.h 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary node base routines for the XYO Core. 9 | * 10 | * Copyright (C) 2017 XY - The Findables Company. All Rights Reserved. 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | 20 | #ifndef NODEBASE_H 21 | 22 | //#include "repository.h" 23 | //#include "hash.h" 24 | //#include "originchain.h" 25 | //#include "ZigZagBoundWitnessSession.h" 26 | //#include "defines.h" 27 | //#include "XYObjects/XYObject.h" 28 | //#include "boundwitnessoption.h" 29 | #include 30 | #include "../../network.h" 31 | #include "boundwitnessoption.h" 32 | #include "nodelistener.h" 33 | 34 | /* 35 | * TYPE DEFINITIONS 36 | **************************************************************************************** 37 | */ 38 | 39 | typedef struct NodeBase NodeBase_t; 40 | 41 | /* 42 | * FORWARD DEFINITIONS 43 | **************************************************************************************** 44 | */ 45 | /* 46 | typedef struct RepositoryProvider RepositoryProvider_t; 47 | typedef struct HashProvider HashProvider_t; 48 | typedef struct OriginChainNavigator OriginChainNavigator_t; 49 | typedef struct OriginChainState OriginChainState_t; 50 | typedef struct ZigZagBoundWitnessSession ZigZagBoundWitnessSession_t; 51 | typedef struct XYObject XYObject_t; 52 | typedef struct XYResult XYResult_t; 53 | typedef struct BoundWitnessOption BoundWitnessOption; 54 | //typedef struct ZigZagBoundWitnessSession ZigZagBoundWitnessSession_t; 55 | */ 56 | 57 | /* 58 | * STRUCTURES 59 | **************************************************************************************** 60 | */ 61 | 62 | typedef struct NodeBase { 63 | OriginChainState_t originChainState; 64 | //NetworkPipe_t* NetworkPipe; 65 | uint8_t choice; 66 | } NodeBase_t; 67 | 68 | XYResult_t initNode(NodeBase_t* self); 69 | uint8_t addHeuristic(NodeBase_t* self, uint32_t key, XYObject_t* heuristic); 70 | uint8_t removeHeuristic(NodeBase_t* self, uint32_t key); 71 | uint8_t selfSignOriginChain(NodeBase_t* self); 72 | XYObject_t* getUnSignedPayloads(NodeBase_t* self); 73 | XYObject_t* getSignedPayloads(NodeBase_t* self); 74 | void notifyListeners(NodeBase_t* self, BoundWitness_t* boundWitness); 75 | XYResult_t* getBridgedBlocks(NodeBase_t* self); 76 | //XYResult_t doBoundWitness(NodeBase_t* self, ByteArray_t* startingData, struct NetworkPipe_t* pipe); 77 | XYResult_t completeBoundWitness(NodeBase_t* self, 78 | ByteArray_t* boundWitnessData, NetworkPipe_t* pipe); 79 | uint8_t updateOriginState(NodeBase_t* self, BoundWitness_t* boundWitness); 80 | void onBoundWitnessEndSuccess(NodeBase_t* self, BoundWitness_t* boundWitness); 81 | void onBoundWitnessEndFailure( void ); 82 | void onBoundWitnessStart( void ); 83 | XYResult_t* makePayload(NodeBase_t* self); 84 | 85 | //extern uint8_t (*getChoice)(uint8_t catalog); 86 | XYObject_t* getSignedHash(void*); 87 | XYObject_t* getUnSignedHash(void*); 88 | XYObject_t* getSignedIndex(void*); 89 | XYObject_t* getUnSignedIndex(void*); 90 | XYObject_t* getSignedBridge(void*); 91 | XYObject_t* getUnSignedBridge(void*); 92 | 93 | 94 | /* Globals */ 95 | extern uint8_t gsignedHeuristicCount; 96 | extern uint8_t gunsignedHeuristicCount; 97 | //extern OriginChainState_t* gState; 98 | 99 | #define NODEBASE_H 100 | #endif 101 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_md5.h: -------------------------------------------------------------------------------- 1 | /* md5.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | /*! 23 | \file wolfssl/wolfcrypt/md5.h 24 | */ 25 | 26 | 27 | #ifndef WOLF_CRYPT_MD5_H 28 | #define WOLF_CRYPT_MD5_H 29 | 30 | //#include 31 | #include "wc_types.h" 32 | 33 | #ifndef NO_MD5 34 | 35 | #ifdef HAVE_FIPS 36 | #define wc_InitMd5 InitMd5 37 | #define wc_Md5Update Md5Update 38 | #define wc_Md5Final Md5Final 39 | #define wc_Md5Hash Md5Hash 40 | #endif 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | #ifndef NO_OLD_WC_NAMES 47 | #define Md5 wc_Md5 48 | #define MD5 WC_MD5 49 | #define MD5_BLOCK_SIZE WC_MD5_BLOCK_SIZE 50 | #define MD5_DIGEST_SIZE WC_MD5_DIGEST_SIZE 51 | #define WC_MD5_PAD_SIZE WC_MD5_PAD_SIZE 52 | #endif 53 | 54 | /* in bytes */ 55 | enum { 56 | WC_MD5 = WC_HASH_TYPE_MD5, 57 | WC_MD5_BLOCK_SIZE = 64, 58 | WC_MD5_DIGEST_SIZE = 16, 59 | WC_MD5_PAD_SIZE = 56 60 | }; 61 | 62 | 63 | #ifdef WOLFSSL_MICROCHIP_PIC32MZ 64 | #include 65 | #endif 66 | #ifdef STM32_HASH 67 | #include 68 | #endif 69 | #ifdef WOLFSSL_ASYNC_CRYPT 70 | #include 71 | #endif 72 | 73 | #ifdef WOLFSSL_TI_HASH 74 | #include "wolfssl/wolfcrypt/port/ti/ti-hash.h" 75 | #elif defined(WOLFSSL_IMX6_CAAM) 76 | #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h" 77 | #else 78 | 79 | /* MD5 digest */ 80 | typedef struct wc_Md5 { 81 | #ifdef STM32_HASH 82 | STM32_HASH_Context stmCtx; 83 | #else 84 | word32 buffLen; /* in bytes */ 85 | word32 loLen; /* length in bytes */ 86 | word32 hiLen; /* length in bytes */ 87 | word32 buffer[WC_MD5_BLOCK_SIZE / sizeof(word32)]; 88 | #ifdef WOLFSSL_PIC32MZ_HASH 89 | word32 digest[PIC32_DIGEST_SIZE / sizeof(word32)]; 90 | #else 91 | word32 digest[WC_MD5_DIGEST_SIZE / sizeof(word32)]; 92 | #endif 93 | void* heap; 94 | #ifdef WOLFSSL_PIC32MZ_HASH 95 | hashUpdCache cache; /* cache for updates */ 96 | #endif 97 | #endif /* STM32_HASH */ 98 | #ifdef WOLFSSL_ASYNC_CRYPT 99 | WC_ASYNC_DEV asyncDev; 100 | #endif /* WOLFSSL_ASYNC_CRYPT */ 101 | } wc_Md5; 102 | 103 | #endif /* WOLFSSL_TI_HASH */ 104 | 105 | WOLFSSL_API int wc_InitMd5(wc_Md5*); 106 | WOLFSSL_API int wc_InitMd5_ex(wc_Md5*, void*, int); 107 | WOLFSSL_API int wc_Md5Update(wc_Md5*, const byte*, word32); 108 | WOLFSSL_API int wc_Md5Final(wc_Md5*, byte*); 109 | WOLFSSL_API void wc_Md5Free(wc_Md5*); 110 | 111 | WOLFSSL_API int wc_Md5GetHash(wc_Md5*, byte*); 112 | WOLFSSL_API int wc_Md5Copy(wc_Md5*, wc_Md5*); 113 | 114 | #ifdef WOLFSSL_PIC32MZ_HASH 115 | WOLFSSL_API void wc_Md5SizeSet(wc_Md5* md5, word32 len); 116 | #endif 117 | 118 | #ifdef __cplusplus 119 | } /* extern "C" */ 120 | #endif 121 | 122 | #endif /* NO_MD5 */ 123 | #endif /* WOLF_CRYPT_MD5_H */ 124 | -------------------------------------------------------------------------------- /src/XYObjects/XYObject.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "../XYResult.h" 6 | #include "XYObjectHeader.h" 7 | #include "../endian.h" 8 | #include "../defines.h" 9 | 10 | /* 11 | * TYPES 12 | **************************************************************************************** 13 | */ 14 | 15 | typedef struct XYObject 16 | { 17 | XYObjectHeader_t* header; 18 | void* payload; 19 | } XYObject_t; 20 | 21 | /* 22 | * FUNCTIONS 23 | **************************************************************************************** 24 | */ 25 | 26 | //the value is all the data in the object other than the header (header/length length) 27 | XYResult_t XYObject_getValue(XYObject_t *self); 28 | 29 | //this is the length of the object, including the length field (if one exists), 30 | //but not including the header 31 | XYResult_t XYObject_getLength(XYObject_t *self); 32 | 33 | //this is the length of the object, including the length field (if one exists), 34 | //and also including the header 35 | XYResult_t XYObject_getFullLength(XYObject_t *self); 36 | 37 | static const char TypedArray_id[2] = { 0x30, 0x01 }; 38 | static const char UnTypedArray_id[2] = { 0x20, 0x01 }; 39 | 40 | static const char BoundWitness_id[2] = { 0x02, 0x01 }; 41 | static const char KeySet_id[2] = { 0x02, 0x02 }; 42 | static const char SignatureSet_id[2] = { 0x02, 0x03 }; 43 | static const char Payload_id[2] = { 0x02, 0x04 }; 44 | static const char Index_id[2] = { 0x02, 0x05 }; 45 | static const char PreviousHash_id[2] = { 0x02, 0x06 }; 46 | static const char NextPublicKey_id[2] = { 0x02, 0x07 }; 47 | static const char BridgeHashSet_id[2] = { 0x02, 0x08 }; 48 | static const char BridgeBlockSet_id[2] = { 0x02, 0x09 }; 49 | static const char BoundWitnessTransfer_id[2] = { 0x02, 0x0a }; 50 | 51 | static const char Sha256_id[2] = { 0x03, 0x05 }; 52 | static const char ECDSASecp256k1_id[2] = { 0x04, 0x01 }; 53 | static const char ECDSASecp256k1Sig_id[2] = { 0x05, 0x01 }; 54 | static const char Rssi_id[2] = { 0x08, 0x01 }; 55 | 56 | /* 57 | * MACROS 58 | **************************************************************************************** 59 | */ 60 | 61 | //should be called at the top of any XYObject function using self 62 | 63 | #define INIT_SELF_UNKNOWN() \ 64 | DECLARE_RESULT(); \ 65 | CHECK_NULL(self); \ 66 | 67 | #define INIT_SELF(_TYPE_) \ 68 | INIT_SELF_UNKNOWN(); \ 69 | CHECK_IS_XYOBJECT( _TYPE_ ); 70 | 71 | //check if the passed in buffer is the expected object type 72 | #define IS_XYOBJECT(_TYPE_, _PTR_) matchType(_PTR_, _TYPE_) 73 | //(((XYObject_t*)_PTR_)->header->type == _TYPE_) 74 | 75 | //check the object type and return a validation error if it fails 76 | #define CHECK_IS_XYOBJECT(_TYPE_) \ 77 | if( !IS_XYOBJECT(_TYPE_, self) ) \ 78 | { \ 79 | XYERROR(XY_STATUS_ERROR); \ 80 | } 81 | 82 | //helpers to read bytes from the object buffer 83 | #define XYOBJ_READ_UINT8() (*(uint8_t*)(self->payload)) 84 | #define XYOBJ_READ_UINT16() to_uint16((unsigned char*)((XYObject_t *)self->payload)) 85 | #define XYOBJ_READ_UINT32() to_uint32((unsigned char*)((XYObject_t *)self->payload)) 86 | #define XYOBJ_READ_UINT64() to_uint64((unsigned char*)((XYObject_t *)self->payload)) 87 | 88 | #define XYOBJ_READ_UINT8_ARRAY(_DEST_, _LEN_) memcpy(_DEST_, (XYObject_t *)self->payload, _LEN_) 89 | #define XYOBJ_WRITE_UINT8_ARRAY(_SRC_, _LEN_) memcpy((XYObject_t *)self->payload, _SRC_, _LEN_) 90 | 91 | #define XYOBJ_INCREMENT_UINT16(_VALUE_) to_uint16_be((unsigned char*)((XYObject_t *)self)->payload, to_uint16(self->payload) + _VALUE_) 92 | #define XYOBJ_INCREMENT_UINT32(_VALUE_) to_uint32_be((unsigned char*)((XYObject_t *)self)->payload, to_uint32(self->payload) + _VALUE_) 93 | #define XYOBJ_INCREMENT_UINT64(_VALUE_) to_uint64_be((unsigned char *)((XYObject_t *)self)->payload, to_uint64(self->payload) + _VALUE_) 94 | 95 | uint8_t matchType(XYObject_t* self, uint8_t type); -------------------------------------------------------------------------------- /src/XYObjects/Array/Iterator.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file WeakArray.c 3 | * @author Arie Trouw (developer@xy.company) 4 | * @brief 5 | * @version 0.1 6 | * @date 2018-11-15 7 | * 8 | * @copyright Copyright (c) 2018 XY - The Persistant COmpany 9 | * 10 | */ 11 | 12 | #include "Iterator.h" 13 | #include 14 | 15 | XYArrayItr_t WeakArrayIterator(XYObjectHeader_t* header, char* buffer){ 16 | if(!header || !buffer){ 17 | return (XYArrayItr_t){0, 0, 0, 0}; 18 | } else { 19 | uint8_t lenLen = lengthTypeToLength(header->flags.lengthType); 20 | return (XYArrayItr_t){header, buffer, (XYObjectHeader_t*)(buffer+lenLen), (XYObjectHeader_t*)(buffer+lenLen+2)}; 21 | } 22 | } 23 | 24 | XYObject_t IteratorNext(XYArrayItr_t* itr){ 25 | XYObject_t returnObject; 26 | returnObject.header = NULL; 27 | returnObject.payload = NULL; 28 | 29 | if((itr->header->flags.iteratable != 1)){ 30 | return returnObject; 31 | } 32 | 33 | if(!itr->header->flags.typed){ 34 | returnObject.header = itr->innerHeader; 35 | returnObject.payload = itr->indexPtr; 36 | uint32_t jump = XYObject_getLength(&returnObject).value.ui; 37 | itr->innerHeader = itr->indexPtr + jump; 38 | itr->indexPtr = (char*)itr->innerHeader + sizeof(XYObjectHeader_t); 39 | returnObject.header = itr->innerHeader; 40 | returnObject.payload = itr->indexPtr; 41 | } else { 42 | returnObject.header = itr->innerHeader; 43 | returnObject.payload = itr->indexPtr; 44 | uint32_t jump = XYObject_getLength(&returnObject).value.ui; 45 | itr->indexPtr = itr->indexPtr + jump; 46 | returnObject.payload = itr->indexPtr; 47 | } 48 | 49 | return returnObject; 50 | } 51 | 52 | XYObject_t IteratorGet(XYArrayItr_t* itr){ 53 | XYObject_t returnObject; 54 | returnObject.header = NULL; 55 | returnObject.payload = NULL; 56 | 57 | if((itr->header->type != TYPE_ARRAY)){ 58 | return returnObject; 59 | } 60 | 61 | if(itr->header->flags.typed){ 62 | returnObject.header = itr->indexPtr; 63 | returnObject.payload = itr->indexPtr + sizeof(XYObjectHeader_t); 64 | 65 | } else { 66 | returnObject.header = itr->header; 67 | returnObject.payload = itr->indexPtr; 68 | } 69 | return returnObject; 70 | } 71 | 72 | XYResult_t Iterator_bookmark(XYObject_t* self, uint32_t element, uint32_t bytesAfter, uint32_t offset, XYArrayItr_t* optionalItr){ 73 | DECLARE_RESULT() 74 | XYArrayItr_t* objectItr; 75 | 76 | if(optionalItr != NULL){ 77 | objectItr = optionalItr; 78 | } else { 79 | XYArrayItr_t itr = WeakArrayIterator(self->header, self->payload); 80 | objectItr = &itr; 81 | } 82 | XYObject_t currentObject = IteratorGet(objectItr); 83 | for(int i = 0; i!=element+1; i++){ 84 | if(currentObject.header == NULL && (i!=-1)){ 85 | result.status = XY_STATUS_ERROR; 86 | return result; 87 | } 88 | XYObject_t currentObject = IteratorNext(objectItr); 89 | } 90 | 91 | char* current = objectItr->indexPtr; 92 | 93 | memmove(current+offset, current, bytesAfter); 94 | 95 | XYOBJ_INCREMENT(offset); 96 | 97 | result.status = XY_STATUS_OK; 98 | return result; 99 | } 100 | 101 | XYResult_t Iterator_insert(XYObject_t* self, uint32_t element, uint32_t offset, uint32_t totalBytes, char* bytes){ 102 | 103 | DECLARE_RESULT(); 104 | XYArrayItr_t objectItr = WeakArrayIterator(self->header, self->payload); 105 | char* charCounter = objectItr.indexPtr; 106 | XYObject_t currentObject = IteratorGet(&objectItr); 107 | for(int i = 0; i!=element+1; i++){ 108 | if(currentObject.header == NULL && (i!=-1)){ 109 | result.status = XY_STATUS_ERROR; 110 | return result; 111 | } 112 | currentObject = IteratorNext(&objectItr); 113 | } 114 | 115 | char* current = objectItr.indexPtr; 116 | 117 | uint32_t bytesAfter = current-charCounter; 118 | 119 | memmove(current+offset, current, bytesAfter); 120 | memcpy(current, bytes, offset); 121 | 122 | 123 | XYOBJ_INCREMENT(offset); 124 | result.status = XY_STATUS_OK; 125 | return result; 126 | } 127 | 128 | // end of file Iterator.c 129 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_sha.h: -------------------------------------------------------------------------------- 1 | /* sha.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | /*! 23 | \file wolfssl/wolfcrypt/sha.h 24 | */ 25 | 26 | 27 | #ifndef WOLF_CRYPT_SHA_H 28 | #define WOLF_CRYPT_SHA_H 29 | 30 | //#include 31 | #include "wc_types.h" 32 | 33 | #ifndef NO_SHA 34 | 35 | #ifdef HAVE_FIPS 36 | #define wc_Sha Sha 37 | #define WC_SHA SHA 38 | #define WC_SHA_BLOCK_SIZE SHA_BLOCK_SIZE 39 | #define WC_SHA_DIGEST_SIZE SHA_DIGEST_SIZE 40 | #define WC_SHA_PAD_SIZE SHA_PAD_SIZE 41 | 42 | /* for fips @wc_fips */ 43 | #include 44 | #endif 45 | 46 | #ifdef FREESCALE_LTC_SHA 47 | #include "fsl_ltc.h" 48 | #endif 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | #ifndef HAVE_FIPS /* avoid redefining structs */ 55 | 56 | #ifdef WOLFSSL_MICROCHIP_PIC32MZ 57 | #include 58 | #endif 59 | #ifdef STM32_HASH 60 | #include 61 | #endif 62 | #ifdef WOLFSSL_ASYNC_CRYPT 63 | #include 64 | #endif 65 | 66 | #if !defined(NO_OLD_SHA_NAMES) 67 | #define SHA WC_SHA 68 | #endif 69 | 70 | #ifndef NO_OLD_WC_NAMES 71 | #define Sha wc_Sha 72 | #define SHA_BLOCK_SIZE WC_SHA_BLOCK_SIZE 73 | #define SHA_DIGEST_SIZE WC_SHA_DIGEST_SIZE 74 | #define SHA_PAD_SIZE WC_SHA_PAD_SIZE 75 | #endif 76 | 77 | /* in bytes */ 78 | enum { 79 | WC_SHA = WC_HASH_TYPE_SHA, 80 | WC_SHA_BLOCK_SIZE = 64, 81 | WC_SHA_DIGEST_SIZE = 20, 82 | WC_SHA_PAD_SIZE = 56 83 | }; 84 | 85 | 86 | #if defined(WOLFSSL_TI_HASH) 87 | #include "wolfssl/wolfcrypt/port/ti/ti-hash.h" 88 | 89 | #elif defined(WOLFSSL_IMX6_CAAM) 90 | #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h" 91 | 92 | #else 93 | /* Sha digest */ 94 | typedef struct wc_Sha { 95 | #ifdef FREESCALE_LTC_SHA 96 | ltc_hash_ctx_t ctx; 97 | #elif defined(STM32_HASH) 98 | STM32_HASH_Context stmCtx; 99 | #else 100 | word32 buffLen; /* in bytes */ 101 | word32 loLen; /* length in bytes */ 102 | word32 hiLen; /* length in bytes */ 103 | word32 buffer[WC_SHA_BLOCK_SIZE / sizeof(word32)]; 104 | #ifdef WOLFSSL_PIC32MZ_HASH 105 | word32 digest[PIC32_DIGEST_SIZE / sizeof(word32)]; 106 | #else 107 | word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)]; 108 | #endif 109 | void* heap; 110 | #ifdef WOLFSSL_PIC32MZ_HASH 111 | hashUpdCache cache; /* cache for updates */ 112 | #endif 113 | #ifdef WOLFSSL_ASYNC_CRYPT 114 | WC_ASYNC_DEV asyncDev; 115 | #endif /* WOLFSSL_ASYNC_CRYPT */ 116 | #endif 117 | } wc_Sha; 118 | 119 | #endif /* WOLFSSL_TI_HASH */ 120 | 121 | 122 | #endif /* HAVE_FIPS */ 123 | 124 | WOLFSSL_API int wc_InitSha(wc_Sha*); 125 | WOLFSSL_API int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId); 126 | WOLFSSL_API int wc_ShaUpdate(wc_Sha*, const byte*, word32); 127 | WOLFSSL_API int wc_ShaFinalRaw(wc_Sha*, byte*); 128 | WOLFSSL_API int wc_ShaFinal(wc_Sha*, byte*); 129 | WOLFSSL_API void wc_ShaFree(wc_Sha*); 130 | 131 | WOLFSSL_API int wc_ShaGetHash(wc_Sha*, byte*); 132 | WOLFSSL_API int wc_ShaCopy(wc_Sha*, wc_Sha*); 133 | 134 | #ifdef WOLFSSL_PIC32MZ_HASH 135 | WOLFSSL_API void wc_ShaSizeSet(wc_Sha* sha, word32 len); 136 | #endif 137 | 138 | #ifdef __cplusplus 139 | } /* extern "C" */ 140 | #endif 141 | 142 | #endif /* NO_SHA */ 143 | #endif /* WOLF_CRYPT_SHA_H */ 144 | 145 | -------------------------------------------------------------------------------- /src/hash.c: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file hash.c 5 | * 6 | * @XY4 project source code. 7 | * 8 | * @brief primary hashing routines for the XYO Core. 9 | * 10 | * Copyright (C) 2017 XY - The Findables Company. All Rights Reserved. 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | 20 | #include 21 | #include "hash.h" 22 | #include "XYObjects/XYObject.h" 23 | 24 | /* 25 | * FUNCTIONS & METHODS 26 | **************************************************************************************** 27 | */ 28 | 29 | void trng_acquire(){return;} 30 | 31 | /** 32 | **************************************************************************************** 33 | * NAME 34 | * getHashId 35 | * 36 | * DESCRIPTION 37 | * this routine returns the id of the supplied HashProvider object 38 | * 39 | * PARAMETERS 40 | * hashProviderObject [in] HashProvider_t* 41 | * 42 | * RETURNS 43 | * preallocated_return_result_ptr [out] XYResult_t* 44 | * -------------------- 45 | * preallocated_return_result_ptr->error (error code) 46 | * preallocated_return_result_ptr->result (hashProviderObject->id) 47 | * NOTES 48 | * the wiki on HackMD is out of date / incorrect 49 | **************************************************************************************** 50 | */ 51 | XYResult_t getHashId( void ){} 52 | 53 | /** 54 | **************************************************************************************** 55 | * NAME 56 | * createHash 57 | * 58 | * DESCRIPTION 59 | * this routine creates a hash of the data supplied 60 | * 61 | * PARAMETERS 62 | * dataToHash [in] ByteArray_t* 63 | * 64 | * RETURNS 65 | * preallocated_return_result_ptr [out] XYResult_t* (contains an error code and any hash created) 66 | * -------------------- 67 | * preallocated_return_result_ptr->error (error code) 68 | * preallocated_return_result_ptr->result (&sha256OutputBuffer) 69 | * 70 | * NOTES 71 | * SHA-256 is currently the only hash type implemented (to save runtime memory). 72 | * 73 | * wc_ = wolf crypto library routine or data type 74 | **************************************************************************************** 75 | */ 76 | XYResult_t createHash(ByteArray_t* dataToHash){} 77 | 78 | /** 79 | **************************************************************************************** 80 | * NAME 81 | * verifyHash 82 | * 83 | * DESCRIPTION 84 | * this routine compares the supplied hash to a new hash created from the supplied 85 | * 'unhashed' data 86 | * 87 | * PARAMETERS 88 | * dataToBeHashed [in] ByteArray_t* 89 | * hashForComparison [in] XYObject_t* 90 | * 91 | * RETURNS 92 | * preallocated_return_result_ptr [out] XYResult_t* preallocated_return_result_ptr->result 93 | * contains the 94 | * bool result of this call. 95 | * -------------------- 96 | * TRUE = hashes match 97 | * FALSE = hashes don't match 98 | * NOTES 99 | * wc_ = wolf crypto library routine or data type 100 | **************************************************************************************** 101 | */ 102 | 103 | XYResult_t verifyHash(ByteArray_t* dataToBeHashed, XYObject_t* hashForComparison){} 104 | 105 | /** 106 | **************************************************************************************** 107 | * NAME 108 | * newHashProvider 109 | * 110 | * DESCRIPTION 111 | * this routine creates a new instance of a hash provider 112 | * 113 | * PARAMETERS 114 | * none 115 | * 116 | * RETURNS 117 | * preallocated_return_result_ptr [out] XYResult_t* 118 | * -------------------- 119 | * preallocated_return_result_ptr->error (error code) 120 | * preallocated_return_result_ptr->result ((HashProvider_t*)newHasher) 121 | * NOTES 122 | * will return a malloc error if malloc fails 123 | **************************************************************************************** 124 | */ 125 | XYResult_t newHashProvider(){} 126 | 127 | // end of file hash.c 128 | -------------------------------------------------------------------------------- /src/repository.h: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file repository.c 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary crypto routines for the XYO Core. 9 | * 10 | * Copyright (C) 2018 XY - The Findables Company 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | #include 20 | #include "state.h" 21 | 22 | #ifndef REPOSITORY_H 23 | #define REPOSITORY_H 24 | /* 25 | * TYPE DEFINITIONS 26 | **************************************************************************************** 27 | */ 28 | 29 | /* 30 | * STRUCTURES 31 | **************************************************************************************** 32 | */ 33 | 34 | typedef struct OriginChainProviderT { 35 | void* repository; 36 | char optionalBits[ORIGINCHAIN_EXTRA_BITS]; 37 | uint16_t logicalEnd; 38 | } OriginChainProvider_t; 39 | 40 | typedef struct RepositoryProviderT { 41 | uint32_t repository; 42 | uint32_t logicalEnd; 43 | 44 | } RepositoryProvider_t; 45 | 46 | /*----------------------------------------------------------------------------* 47 | * NAME 48 | * initOriginChainProvider 49 | * 50 | * DESCRIPTION 51 | * Initializes an origin chain provider as a Int Strong Array 52 | * 53 | * PARAMETERS 54 | * *OriginChainNavigator [in] self_OriginChainNavigator* 55 | * *ByteArray [in] originBlockHash* 56 | * 57 | * RETURNS 58 | * XYResult_t* [out] bool Returns OK if success 59 | *----------------------------------------------------------------------------*/ 60 | OriginChainProvider_t* initOriginChainProvider( void ); 61 | 62 | /*----------------------------------------------------------------------------* 63 | * NAME 64 | * write 65 | * 66 | * DESCRIPTION 67 | * Write block to the Repository Provider, in this case a ram disk. 68 | * 69 | * PARAMETERS 70 | * *OriginChainNavigator [in] self_OriginChainNavigator* 71 | * *ByteArray [in] originBlockHash* 72 | * 73 | * RETURNS 74 | * XYResult_t* [out] bool Returns OK if success 75 | *----------------------------------------------------------------------------*/ 76 | /* 77 | XYResult_t* write(OriginChainProvider_t* self, ByteArray_t* value, uint16_t offset, uint16_t timeout); 78 | */ 79 | 80 | /*----------------------------------------------------------------------------* 81 | * NAME 82 | * read 83 | * 84 | * DESCRIPTION 85 | * Append block to the Repository Provider, in this case a ram disk. 86 | * 87 | * PARAMETERS 88 | * *OriginChainNavigator [in] self_OriginChainNavigator* 89 | * *ByteArray [in] originBlockHash* 90 | * 91 | * RETURNS 92 | * XYResult_t* [out] bool Returns OK if success 93 | *----------------------------------------------------------------------------*/ 94 | /* 95 | XYResult_t* read(RepositoryProvider_t* self, uint16_t offset, uint16_t timeout); 96 | */ 97 | 98 | /*----------------------------------------------------------------------------* 99 | * NAME 100 | * append 101 | * 102 | * DESCRIPTION 103 | * Append block to the Repository Provider, in this case a ram disk. 104 | * 105 | * PARAMETERS 106 | * *OriginChainNavigator [in] self_OriginChainNavigator* 107 | * *ByteArray [in] originBlockHash* 108 | * 109 | * RETURNS 110 | * XYResult_t* [out] bool Returns OK if success 111 | *----------------------------------------------------------------------------*/ 112 | XYResult_t* append(OriginChainProvider_t* self, ByteArray_t* value, uint16_t timeout); 113 | 114 | /*----------------------------------------------------------------------------* 115 | * NAME 116 | * getChain 117 | * 118 | * DESCRIPTION 119 | * Get the location of the origin chain on disk and return the array object. 120 | * 121 | * PARAMETERS 122 | * *OriginChainNavigator [in] self* 123 | * 124 | * RETURNS 125 | * XYResult_t* [out] XYObject_t* IntStrongArray 126 | *----------------------------------------------------------------------------*/ 127 | XYResult_t* getChain(OriginChainProvider_t* self); 128 | 129 | /*----------------------------------------------------------------------------* 130 | * NAME 131 | * deleteChain 132 | * 133 | * DESCRIPTION 134 | * Nullify and clear the origin chain. 135 | * 136 | * PARAMETERS 137 | * *OriginChainNavigator [in] self* 138 | * 139 | * RETURNS 140 | * XYResult_t* [out] XYObject_t* IntStrongArray 141 | *----------------------------------------------------------------------------*/ 142 | XYResult_t* deleteChain(OriginChainProvider_t* self); 143 | 144 | 145 | 146 | #define REPOSITORY_H 147 | #endif 148 | 149 | // end of file repository.h 150 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_hmac.h: -------------------------------------------------------------------------------- 1 | /* hmac.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | /*! 23 | \file wolfssl/wolfcrypt/hmac.h 24 | */ 25 | 26 | #ifndef NO_HMAC 27 | 28 | #ifndef WOLF_CRYPT_HMAC_H 29 | #define WOLF_CRYPT_HMAC_H 30 | 31 | //#include // wal 32 | #include "wc_hash.h" 33 | 34 | #ifdef HAVE_FIPS 35 | /* for fips */ 36 | #include 37 | #define WC_HMAC_BLOCK_SIZE HMAC_BLOCK_SIZE 38 | #endif 39 | 40 | 41 | #ifdef __cplusplus 42 | extern "C" { 43 | #endif 44 | #ifndef HAVE_FIPS 45 | 46 | #ifdef WOLFSSL_ASYNC_CRYPT 47 | #include 48 | #endif 49 | 50 | #ifndef NO_OLD_WC_NAMES 51 | #define HMAC_BLOCK_SIZE WC_HMAC_BLOCK_SIZE 52 | #endif 53 | 54 | enum { 55 | HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */ 56 | 57 | IPAD = 0x36, 58 | OPAD = 0x5C, 59 | 60 | /* If any hash is not enabled, add the ID here. */ 61 | #ifdef NO_MD5 62 | WC_MD5 = WC_HASH_TYPE_MD5, 63 | #endif 64 | #ifdef NO_SHA 65 | WC_SHA = WC_HASH_TYPE_SHA, 66 | #endif 67 | #ifdef NO_SHA256 68 | WC_SHA256 = WC_HASH_TYPE_SHA256, 69 | #endif 70 | #ifndef WOLFSSL_SHA512 71 | WC_SHA512 = WC_HASH_TYPE_SHA512, 72 | #endif 73 | #ifndef WOLFSSL_SHA384 74 | WC_SHA384 = WC_HASH_TYPE_SHA384, 75 | #endif 76 | #ifndef HAVE_BLAKE2 77 | BLAKE2B_ID = WC_HASH_TYPE_BLAKE2B, 78 | #endif 79 | #ifndef WOLFSSL_SHA224 80 | WC_SHA224 = WC_HASH_TYPE_SHA224, 81 | #endif 82 | #ifndef WOLFSSL_SHA3 83 | WC_SHA3_224 = WC_HASH_TYPE_SHA3_224, 84 | WC_SHA3_256 = WC_HASH_TYPE_SHA3_256, 85 | WC_SHA3_384 = WC_HASH_TYPE_SHA3_384, 86 | WC_SHA3_512 = WC_HASH_TYPE_SHA3_512, 87 | #endif 88 | }; 89 | 90 | /* Select the largest available hash for the buffer size. */ 91 | #define WC_HMAC_BLOCK_SIZE WC_MAX_BLOCK_SIZE 92 | 93 | #if !defined(WOLFSSL_SHA3) && !defined(WOLFSSL_SHA512) && !defined(HAVE_BLAKE2) && \ 94 | !defined(WOLFSSL_SHA384) && defined(NO_SHA256) && defined(WOLFSSL_SHA224) && \ 95 | defined(NO_SHA) && defined(NO_MD5) 96 | #error "You have to have some kind of hash if you want to use HMAC." 97 | #endif 98 | 99 | 100 | /* hash union */ 101 | typedef union { 102 | #ifndef NO_MD5 103 | wc_Md5 md5; 104 | #endif 105 | #ifndef NO_SHA 106 | wc_Sha sha; 107 | #endif 108 | #ifdef WOLFSSL_SHA224 109 | wc_Sha224 sha224; 110 | #endif 111 | #ifndef NO_SHA256 112 | wc_Sha256 sha256; 113 | #endif 114 | #ifdef WOLFSSL_SHA512 115 | #ifdef WOLFSSL_SHA384 116 | wc_Sha384 sha384; 117 | #endif 118 | wc_Sha512 sha512; 119 | #endif 120 | #ifdef HAVE_BLAKE2 121 | Blake2b blake2b; 122 | #endif 123 | #ifdef WOLFSSL_SHA3 124 | wc_Sha3 sha3; 125 | #endif 126 | } Hash; 127 | 128 | /* Hmac digest */ 129 | typedef struct Hmac { 130 | Hash hash; 131 | word32 ipad[WC_HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/ 132 | word32 opad[WC_HMAC_BLOCK_SIZE / sizeof(word32)]; 133 | word32 innerHash[WC_MAX_DIGEST_SIZE / sizeof(word32)]; 134 | void* heap; /* heap hint */ 135 | byte macType; /* md5 sha or sha256 */ 136 | byte innerHashKeyed; /* keyed flag */ 137 | 138 | #ifdef WOLFSSL_ASYNC_CRYPT 139 | WC_ASYNC_DEV asyncDev; 140 | word16 keyLen; /* hmac key length (key in ipad) */ 141 | #endif /* WOLFSSL_ASYNC_CRYPT */ 142 | } Hmac; 143 | 144 | #endif /* HAVE_FIPS */ 145 | 146 | /* does init */ 147 | WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz); 148 | WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32); 149 | WOLFSSL_API int wc_HmacFinal(Hmac*, byte*); 150 | WOLFSSL_API int wc_HmacSizeByType(int type); 151 | 152 | WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId); 153 | WOLFSSL_API void wc_HmacFree(Hmac*); 154 | 155 | WOLFSSL_API int wolfSSL_GetHmacMaxSize(void); 156 | 157 | WOLFSSL_LOCAL int _InitHmac(Hmac* hmac, int type, void* heap); 158 | 159 | #define HAVE_HKDF // wal 160 | #ifdef HAVE_HKDF 161 | 162 | WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz, 163 | const byte* inKey, word32 inKeySz, byte* out); 164 | WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz, 165 | const byte* info, word32 infoSz, 166 | byte* out, word32 outSz); 167 | 168 | WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz, 169 | const byte* salt, word32 saltSz, 170 | const byte* info, word32 infoSz, 171 | byte* out, word32 outSz); 172 | 173 | #endif /* HAVE_HKDF */ 174 | 175 | #ifdef __cplusplus 176 | } /* extern "C" */ 177 | #endif 178 | 179 | #endif /* WOLF_CRYPT_HMAC_H */ 180 | 181 | #endif /* NO_HMAC */ 182 | 183 | -------------------------------------------------------------------------------- /src/defines.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file defines.h 3 | * @author Arie Trouw (developer@xy.company) 4 | * @brief All standard definitions in XYO Library 5 | * @version 0.1 6 | * @date 2018-11-15 7 | * 8 | * @copyright Copyright (c) 2018 XY - The Persistant COmpany 9 | * 10 | */ 11 | 12 | /* 13 | * Defines 14 | **************************************************************************************** 15 | */ 16 | 17 | #define MAJOR_ARRAY (0x01) 18 | #define MAJOR_CORE (0x02) 19 | #define MAJOR_HASH (0x03) 20 | #define MAJOR_KEYS (0x04) 21 | #define MAJOR_SIGNATURES (0x05) 22 | #define MAJOR_HEURISTICS (0x08) 23 | #define MAJOR_CUSTOM (0x10) 24 | 25 | #define MINOR_ARRAY (0x01) 26 | /* 27 | * MAJOR_ARRAY group Minor values 28 | */ 29 | #define MINOR_BYTE_SINGLE_TYPE (0x01) 30 | #define MINOR_SHORT_SINGLE_TYPE (0x02) 31 | #define MINOR_INT_SINGLE_TYPE (0x03) 32 | #define MINOR_BYTE_MULTI_TYPE (0x04) 33 | #define MINOR_SHORT_MULTI_TYPE (0x05) 34 | #define MINOR_INT_MULTI_TYPE (0x06) 35 | 36 | /* 37 | * MAJOR_CORE group Minor values 38 | */ 39 | #define MINOR_BOUND_WITNESS (0x01) 40 | #define MINOR_KEY_SET (0x02) 41 | #define MINOR_SIGNATURE_SET (0x03) 42 | #define MINOR_PAYLOAD (0x04) 43 | #define MINOR_INDEX (0x05) 44 | #define MINOR_PREVIOUS_HASH (0x06) 45 | #define MINOR_NEXT_PUBLIC_KEY (0x07) 46 | #define MINOR_OC_HASH_SET (0x08) 47 | #define MINOR_OC_SET (0x09) 48 | #define MINOR_BOUND_WITNESS_TRANSFER (0x0a) 49 | 50 | #define BOUND_WITNESS_STAGE_1 (0x01) 51 | #define BOUND_WITNESS_STAGE_2 (0x02) 52 | #define BOUND_WITNESS_STAGE_3 (0x03) 53 | 54 | /* 55 | * MAJOR_HASH group Minor values 56 | */ 57 | #define MINOR_MD2 (0x01) 58 | #define MINOR_MD5 (0x02) 59 | #define MINOR_SHA1 (0x03) 60 | #define MINOR_SHA224 (0x04) 61 | #define MINOR_SHA256 (0x05) 62 | #define MINOR_SHA512 (0x06) 63 | 64 | /* 65 | * MAJOR_KEYS group Minor values 66 | */ 67 | #define MINOR_ECDSA_SECP256K1_UNCOMPRESSED (0x01) 68 | #define MINOR_ECDSA_SECP256K1_COMPRESSED (0x02) 69 | #define MINOR_RSA_STANDARD_EXPONENT (0x03) 70 | 71 | /* 72 | * MAJOR_SIGNATURES group Minor values 73 | */ 74 | #define MINOR_ECDSA_SECP256K1_SHA256_SIGNATURE (0x01) /*TODO: Flip these two back*/ 75 | #define MINOR_ECDSA_SECP256K1_SHA1_SIGNATURE (0x02) 76 | #define MINOR_ECDSA_SECP256K1_SHA3_SIGNATURE (0x03) 77 | #define MINOR_RSA_SHA256 (0x08) 78 | 79 | /* 80 | * MAJOR_HEURISTICS group Minor values 81 | */ 82 | #define MINOR_RSSI (0x01) 83 | #define MINOR_TEXT (0x01) 84 | 85 | /* 86 | * Type Table defines 87 | */ 88 | #define TYPE_TABLE_MAJOR_MAX (0x20) 89 | #define TYPE_TABLE_MINOR_MAX (0x10) 90 | 91 | /* 92 | * originchain.c defines 93 | */ 94 | #define MIN_QUEUE (0x05) 95 | #define MAX_QUEUE (0x20) 96 | #define DEFAULT_TIMEOUT (0xFFFF) 97 | #define ORIGINCHAIN_EXTRA_BITS (256) 98 | 99 | /* 100 | * nodebase.c defines 101 | */ 102 | #define MAX_ALLOCATED_OPTIONS (0x03) 103 | #define MAX_ALLOCATED_HEURISTICS (0x03) 104 | #define MAX_ALLOCATED_LISTENERS (0x00) 105 | #define BOUNDWITNESS_OPTIONS (0x03) 106 | 107 | /* 108 | * repository.c defines 109 | */ 110 | #define RAM_DISK_BYTES (7500) 111 | #define MAX_BOUNDWITNESS_IN_CHAIN (1000) 112 | 113 | /* 114 | * relaynode.c defines 115 | */ 116 | #define BOUND_WITNESS_OPTION (1) 117 | #define TAKE_ORIGIN_CHAIN_OPTION (2) 118 | #define GIVE_ORIGIN_CHAIN_OPTION (4) 119 | 120 | #ifndef TRUE 121 | #define TRUE 1 122 | #endif 123 | #ifndef FALSE 124 | #define FALSE 0 125 | #endif 126 | 127 | /* 128 | * Network.c 129 | */ 130 | #define PARENT_RECV_BUFF_SIZE 2048 131 | #define CHILD_RECV_BUFF_SIZE 512 132 | #define XYO_NETWORK_PORT 2421 133 | #define ERROR_SOCKET_FAILED -20 134 | #define ERROR_SETSOCKOPT_FAILED -21 135 | #define ERROR_BIND_FAILED -22 136 | #define ERROR_LISTEN_FAILED -23 137 | #define ERROR_ACCEPT_FAILED -24 138 | #define ERROR_READ_FAILED -25 139 | #define ERROR_ALLOC_FAILED -26 140 | #define ERROR_SEND_FAILED -27 141 | #define MAX_PEERS 10 142 | #define CATALOG_BUFFER_SIZE 5 143 | #define CATALOG_SIZE 4 144 | #define DEFAULT_NUM_PEERS 1 145 | 146 | #define UNTYPED_ITERABLE 160 147 | #define TYPED_ITERABLE 176 148 | #define NONITERABLE_TWOBYTE 111 149 | 150 | /* 151 | * Configs 152 | */ 153 | #define NODE_MODE BOUND_WITNESS_OPTION+GIVE_ORIGIN_CHAIN_OPTION 154 | 155 | #define RED "\x1B[31m" 156 | #define GRN "\x1B[32m" 157 | #define YEL "\x1B[33m" 158 | #define BLU "\x1B[34m" 159 | #define MAG "\x1B[35m" 160 | #define CYN "\x1B[36m" 161 | #define WHT "\x1B[37m" 162 | #define RESET "\x1B[0m" 163 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_hash.h: -------------------------------------------------------------------------------- 1 | /* hash.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | /*! 23 | \file wolfssl/wolfcrypt/hash.h 24 | */ 25 | 26 | #ifndef WOLF_CRYPT_HASH_H 27 | #define WOLF_CRYPT_HASH_H 28 | 29 | #//include 30 | #include "wc_types.h" 31 | 32 | #ifndef NO_MD5 33 | //#include 34 | //#include "wc_md5.h" //TODO: wal, restore 35 | #endif 36 | #ifndef NO_SHA 37 | //#include 38 | //#include "wc_sha.h" 39 | #endif 40 | #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256) 41 | //#include 42 | #include "wc_sha256.h" 43 | #endif 44 | #if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512) 45 | #include 46 | #endif 47 | #ifdef HAVE_BLAKE2 48 | #include 49 | #endif 50 | #ifdef WOLFSSL_SHA3 51 | #include 52 | #endif 53 | #ifndef NO_MD4 54 | //#include 55 | //#include "wc_md4.h" //TODO: wal, restore 56 | #endif 57 | #ifdef WOLFSSL_MD2 58 | #include 59 | #endif 60 | 61 | 62 | #ifdef __cplusplus 63 | extern "C" { 64 | #endif 65 | 66 | #if !defined(HAVE_FIPS) && !defined(NO_OLD_WC_NAMES) 67 | #define MAX_DIGEST_SIZE WC_MAX_DIGEST_SIZE 68 | #endif 69 | 70 | 71 | typedef union { 72 | #ifndef NO_MD5 73 | //wc_Md5 md5; //TODO: wal, restore 74 | #endif 75 | #ifndef NO_SHA 76 | //wc_Sha sha; //TODO: wal, restore 77 | #endif 78 | #ifdef WOLFSSL_SHA224 79 | wc_Sha224 sha224; 80 | #endif 81 | #ifndef NO_SHA256 82 | wc_Sha256 sha256; 83 | #endif 84 | #ifdef WOLFSSL_SHA384 85 | wc_Sha384 sha384; 86 | #endif 87 | #ifdef WOLFSSL_SHA512 88 | wc_Sha512 sha512; 89 | #endif 90 | } wc_HashAlg; 91 | 92 | /* Find largest possible digest size 93 | Note if this gets up to the size of 80 or over check smallstack build */ 94 | #if defined(WOLFSSL_SHA3) 95 | #define WC_MAX_DIGEST_SIZE WC_SHA3_512_DIGEST_SIZE 96 | #define WC_MAX_BLOCK_SIZE WC_SHA3_224_BLOCK_SIZE /* 224 is the largest block size */ 97 | #elif defined(WOLFSSL_SHA512) 98 | #define WC_MAX_DIGEST_SIZE WC_SHA512_DIGEST_SIZE 99 | #define WC_MAX_BLOCK_SIZE WC_SHA512_BLOCK_SIZE 100 | #elif defined(HAVE_BLAKE2) 101 | #define WC_MAX_DIGEST_SIZE BLAKE2B_OUTBYTES 102 | #define WC_MAX_BLOCK_SIZE BLAKE2B_BLOCKBYTES 103 | #elif defined(WOLFSSL_SHA384) 104 | #define WC_MAX_DIGEST_SIZE WC_SHA384_DIGEST_SIZE 105 | #define WC_MAX_BLOCK_SIZE WC_SHA384_BLOCK_SIZE 106 | #elif !defined(NO_SHA256) 107 | #define WC_MAX_DIGEST_SIZE WC_SHA256_DIGEST_SIZE 108 | #define WC_MAX_BLOCK_SIZE WC_SHA256_BLOCK_SIZE 109 | #elif defined(WOLFSSL_SHA224) 110 | #define WC_MAX_DIGEST_SIZE WC_SHA224_DIGEST_SIZE 111 | #define WC_MAX_BLOCK_SIZE WC_SHA224_BLOCK_SIZE 112 | #elif !defined(NO_SHA) 113 | #define WC_MAX_DIGEST_SIZE WC_SHA_DIGEST_SIZE 114 | #define WC_MAX_BLOCK_SIZE WC_SHA_BLOCK_SIZE 115 | #elif !defined(NO_MD5) 116 | #define WC_MAX_DIGEST_SIZE WC_MD5_DIGEST_SIZE 117 | #define WC_MAX_BLOCK_SIZE WC_MD5_BLOCK_SIZE 118 | #else 119 | #define WC_MAX_DIGEST_SIZE 64 /* default to max size of 64 */ 120 | #define WC_MAX_BLOCK_SIZE 128 121 | #endif 122 | 123 | #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) 124 | WOLFSSL_API int wc_HashGetOID(enum wc_HashType hash_type); 125 | WOLFSSL_API enum wc_HashType wc_OidGetHash(int oid); 126 | #endif 127 | 128 | WOLFSSL_API enum wc_HashType wc_HashTypeConvert(int hashType); 129 | 130 | WOLFSSL_API int wc_HashGetDigestSize(enum wc_HashType hash_type); 131 | WOLFSSL_API int wc_HashGetBlockSize(enum wc_HashType hash_type); 132 | WOLFSSL_API int wc_Hash(enum wc_HashType hash_type, 133 | const byte* data, word32 data_len, 134 | byte* hash, word32 hash_len); 135 | 136 | /* generic hash operation wrappers */ 137 | WOLFSSL_API int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type); 138 | WOLFSSL_API int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, 139 | const byte* data, word32 dataSz); 140 | WOLFSSL_API int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, 141 | byte* out); 142 | 143 | 144 | #ifndef NO_MD5 145 | //#include 146 | #include "wc_md5.h" 147 | WOLFSSL_API int wc_Md5Hash(const byte* data, word32 len, byte* hash); 148 | #endif 149 | 150 | #ifndef NO_SHA 151 | //#include 152 | #include "wc_sha.h" 153 | WOLFSSL_API int wc_ShaHash(const byte*, word32, byte*); 154 | #endif 155 | 156 | #ifndef NO_SHA256 157 | //#include 158 | #include "wc_sha256.h" 159 | WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*); 160 | 161 | #if defined(WOLFSSL_SHA224) 162 | WOLFSSL_API int wc_Sha224Hash(const byte*, word32, byte*); 163 | #endif /* defined(WOLFSSL_SHA224) */ 164 | #endif 165 | 166 | #ifdef WOLFSSL_SHA512 167 | #include 168 | WOLFSSL_API int wc_Sha512Hash(const byte*, word32, byte*); 169 | 170 | #if defined(WOLFSSL_SHA384) 171 | WOLFSSL_API int wc_Sha384Hash(const byte*, word32, byte*); 172 | #endif /* defined(WOLFSSL_SHA384) */ 173 | #endif /* WOLFSSL_SHA512 */ 174 | 175 | #ifdef __cplusplus 176 | } /* extern "C" */ 177 | #endif 178 | 179 | #endif /* WOLF_CRYPT_HASH_H */ 180 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_random.h: -------------------------------------------------------------------------------- 1 | /* random.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | /*! 23 | \file wolfssl/wolfcrypt/random.h 24 | */ 25 | 26 | 27 | 28 | #ifndef WOLF_CRYPT_RANDOM_H 29 | #define WOLF_CRYPT_RANDOM_H 30 | 31 | //#include 32 | #include "wc_types.h" 33 | 34 | #ifdef HAVE_FIPS 35 | /* for fips @wc_fips */ 36 | #include 37 | #endif 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /* Maximum generate block length */ 44 | #ifndef RNG_MAX_BLOCK_LEN 45 | #ifdef HAVE_INTEL_QA 46 | #define RNG_MAX_BLOCK_LEN (0xFFFF) 47 | #else 48 | #define RNG_MAX_BLOCK_LEN (0x10000) 49 | #endif 50 | #endif 51 | 52 | /* Size of the BRBG seed */ 53 | #ifndef DRBG_SEED_LEN 54 | #define DRBG_SEED_LEN (440/8) 55 | #endif 56 | 57 | 58 | #if !defined(CUSTOM_RAND_TYPE) 59 | /* To maintain compatibility the default is byte */ 60 | #define CUSTOM_RAND_TYPE byte 61 | #endif 62 | 63 | /* make sure Hash DRBG is enabled, unless WC_NO_HASHDRBG is defined 64 | or CUSTOM_RAND_GENERATE_BLOCK is defined*/ 65 | #define WC_NO_HASHDRBG 66 | #define CUSTOM_RAND_GENERATE_BLOCK 67 | #if !defined(WC_NO_HASHDRBG) || !defined(CUSTOM_RAND_GENERATE_BLOCK) 68 | #undef HAVE_HASHDRBG 69 | #define HAVE_HASHDRBG 70 | #ifndef WC_RESEED_INTERVAL 71 | #define WC_RESEED_INTERVAL (1000000) 72 | #endif 73 | #endif 74 | 75 | 76 | #ifndef HAVE_FIPS /* avoid redefining structs and macros */ 77 | 78 | /* RNG supports the following sources (in order): 79 | * 1. CUSTOM_RAND_GENERATE_BLOCK: Defines name of function as RNG source and 80 | * bypasses the options below. 81 | * 2. HAVE_INTEL_RDRAND: Uses the Intel RDRAND if supported by CPU. 82 | * 3. HAVE_HASHDRBG (requires SHA256 enabled): Uses SHA256 based P-RNG 83 | * seeded via wc_GenerateSeed. This is the default source. 84 | */ 85 | 86 | /* Seed source can be overriden by defining one of these: 87 | CUSTOM_RAND_GENERATE_SEED 88 | CUSTOM_RAND_GENERATE_SEED_OS 89 | CUSTOM_RAND_GENERATE */ 90 | 91 | 92 | #if defined(CUSTOM_RAND_GENERATE_BLOCK) 93 | /* To use define the following: 94 | * #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc 95 | * extern int myRngFunc(byte* output, word32 sz); 96 | */ 97 | #elif defined(HAVE_HASHDRBG) 98 | #ifdef NO_SHA256 99 | #error "Hash DRBG requires SHA-256." 100 | #endif /* NO_SHA256 */ 101 | //#include 102 | #include "wc_sha256.h" 103 | //#elif defined(HAVE_WNR) 104 | /* allow whitewood as direct RNG source using wc_GenerateSeed directly */ 105 | #else 106 | #error No RNG source defined! 107 | #endif 108 | 109 | #ifdef HAVE_WNR 110 | #include 111 | #endif 112 | 113 | #ifdef WOLFSSL_ASYNC_CRYPT 114 | #include 115 | #endif 116 | 117 | 118 | #if defined(USE_WINDOWS_API) 119 | #if defined(_WIN64) 120 | typedef unsigned __int64 ProviderHandle; 121 | /* type HCRYPTPROV, avoid #include */ 122 | #else 123 | typedef unsigned long ProviderHandle; 124 | #endif 125 | #endif 126 | 127 | 128 | /* OS specific seeder */ 129 | typedef struct OS_Seed { 130 | #if defined(USE_WINDOWS_API) 131 | ProviderHandle handle; 132 | #else 133 | int fd; 134 | #endif 135 | } OS_Seed; 136 | 137 | 138 | #ifndef WC_RNG_TYPE_DEFINED /* guard on redeclaration */ 139 | typedef struct WC_RNG WC_RNG; 140 | #define WC_RNG_TYPE_DEFINED 141 | #endif 142 | 143 | /* RNG context */ 144 | struct WC_RNG { 145 | OS_Seed seed; 146 | void* heap; 147 | #ifdef HAVE_HASHDRBG 148 | /* Hash-based Deterministic Random Bit Generator */ 149 | struct DRBG* drbg; 150 | byte status; 151 | #endif 152 | #ifdef WOLFSSL_ASYNC_CRYPT 153 | WC_ASYNC_DEV asyncDev; 154 | int devId; 155 | #endif 156 | }; 157 | 158 | #endif /* HAVE_FIPS */ 159 | 160 | /* NO_OLD_RNGNAME removes RNG struct name to prevent possible type conflicts, 161 | * can't be used with CTaoCrypt FIPS */ 162 | #if !defined(NO_OLD_RNGNAME) && !defined(HAVE_FIPS) 163 | #define RNG WC_RNG 164 | #endif 165 | 166 | 167 | //WOLFSSL_LOCAL 168 | int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz); 169 | 170 | 171 | #ifdef HAVE_WNR 172 | /* Whitewood netRandom client library */ 173 | WOLFSSL_API int wc_InitNetRandom(const char*, wnr_hmac_key, int); 174 | WOLFSSL_API int wc_FreeNetRandom(void); 175 | #endif /* HAVE_WNR */ 176 | 177 | 178 | WOLFSSL_API int wc_InitRng(WC_RNG*); 179 | WOLFSSL_API int wc_InitRng_ex(WC_RNG* rng, void* heap, int devId); 180 | WOLFSSL_API int wc_RNG_GenerateBlock(WC_RNG*, byte*, word32 sz); 181 | WOLFSSL_API int wc_RNG_GenerateByte(WC_RNG*, byte*); 182 | WOLFSSL_API int wc_FreeRng(WC_RNG*); 183 | 184 | 185 | #ifdef HAVE_HASHDRBG 186 | WOLFSSL_LOCAL int wc_RNG_DRBG_Reseed(WC_RNG* rng, const byte* entropy, 187 | word32 entropySz); 188 | WOLFSSL_API int wc_RNG_HealthTest(int reseed, 189 | const byte* entropyA, word32 entropyASz, 190 | const byte* entropyB, word32 entropyBSz, 191 | byte* output, word32 outputSz); 192 | #endif /* HAVE_HASHDRBG */ 193 | 194 | #ifdef __cplusplus 195 | } /* extern "C" */ 196 | #endif 197 | 198 | #endif /* WOLF_CRYPT_RANDOM_H */ 199 | 200 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_sha256.h: -------------------------------------------------------------------------------- 1 | /* sha256.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | /*! 23 | \file wolfssl/wolfcrypt/sha256.h 24 | */ 25 | 26 | 27 | /* code submitted by raphael.huck@efixo.com */ 28 | 29 | #ifndef WOLF_CRYPT_SHA256_H 30 | #define WOLF_CRYPT_SHA256_H 31 | 32 | /* 33 | * INCLUDES 34 | **************************************************************************************** 35 | */ 36 | 37 | #include "wc_types.h" 38 | 39 | /* 40 | * DEFINES 41 | **************************************************************************************** 42 | */ 43 | 44 | #ifndef NO_SHA256 45 | 46 | #ifdef HAVE_FIPS 47 | #define wc_Sha256 Sha256 48 | #define WC_SHA256 SHA256 49 | #define WC_SHA256_BLOCK_SIZE SHA256_BLOCK_SIZE 50 | #define WC_SHA256_DIGEST_SIZE SHA256_DIGEST_SIZE 51 | #define WC_SHA256_PAD_SIZE SHA256_PAD_SIZE 52 | 53 | #ifdef WOLFSSL_SHA224 54 | #define wc_Sha224 Sha224 55 | #define WC_SHA224 SHA224 56 | #define WC_SHA224_BLOCK_SIZE SHA224_BLOCK_SIZE 57 | #define WC_SHA224_DIGEST_SIZE SHA224_DIGEST_SIZE 58 | #define WC_SHA224_PAD_SIZE SHA224_PAD_SIZE 59 | #endif 60 | 61 | /* for fips @wc_fips */ 62 | #include 63 | #endif 64 | 65 | #ifdef FREESCALE_LTC_SHA 66 | #include "fsl_ltc.h" 67 | #endif 68 | 69 | 70 | #ifdef __cplusplus 71 | extern "C" { 72 | #endif 73 | 74 | #ifndef HAVE_FIPS /* avoid redefinition of structs */ 75 | 76 | #ifdef WOLFSSL_MICROCHIP_PIC32MZ 77 | #include 78 | #endif 79 | #ifdef STM32_HASH 80 | #include 81 | #endif 82 | #ifdef WOLFSSL_ASYNC_CRYPT 83 | #include 84 | #endif 85 | 86 | #if defined(_MSC_VER) 87 | #define SHA256_NOINLINE __declspec(noinline) 88 | #elif defined(__GNUC__) 89 | #define SHA256_NOINLINE __attribute__((noinline)) 90 | #else 91 | #define SHA256_NOINLINE 92 | #endif 93 | 94 | #if !defined(NO_OLD_SHA_NAMES) 95 | #define SHA256 WC_SHA256 96 | #endif 97 | 98 | #ifndef NO_OLD_WC_NAMES 99 | #define Sha256 wc_Sha256 100 | #define SHA256_BLOCK_SIZE WC_SHA256_BLOCK_SIZE 101 | #define SHA256_DIGEST_SIZE WC_SHA256_DIGEST_SIZE 102 | #define SHA256_PAD_SIZE WC_SHA256_PAD_SIZE 103 | #endif 104 | 105 | /* in bytes */ 106 | enum { 107 | WC_SHA256 = WC_HASH_TYPE_SHA256, 108 | WC_SHA256_BLOCK_SIZE = 64, 109 | WC_SHA256_DIGEST_SIZE = 32, 110 | WC_SHA256_PAD_SIZE = 56 111 | }; 112 | 113 | #ifdef WOLFSSL_TI_HASH 114 | #include "wolfssl/wolfcrypt/port/ti/ti-hash.h" 115 | #elif defined(WOLFSSL_IMX6_CAAM) 116 | #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h" 117 | #else 118 | /* wc_Sha256 digest */ 119 | typedef struct wc_Sha256 { 120 | #ifdef FREESCALE_LTC_SHA 121 | ltc_hash_ctx_t ctx; 122 | #elif defined(STM32_HASH) 123 | STM32_HASH_Context stmCtx; 124 | #else 125 | /* alignment on digest and buffer speeds up ARMv8 crypto operations */ 126 | ALIGN16 word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)]; 127 | ALIGN16 word32 buffer[WC_SHA256_BLOCK_SIZE / sizeof(word32)]; 128 | word32 buffLen; /* in bytes */ 129 | word32 loLen; /* length in bytes */ 130 | word32 hiLen; /* length in bytes */ 131 | void* heap; 132 | #ifdef USE_INTEL_SPEEDUP 133 | const byte* data; 134 | #endif 135 | #ifdef WOLFSSL_PIC32MZ_HASH 136 | hashUpdCache cache; /* cache for updates */ 137 | #endif 138 | #ifdef WOLFSSL_ASYNC_CRYPT 139 | WC_ASYNC_DEV asyncDev; 140 | #endif /* WOLFSSL_ASYNC_CRYPT */ 141 | #endif 142 | } wc_Sha256; 143 | 144 | #endif 145 | 146 | #endif /* HAVE_FIPS */ 147 | 148 | WOLFSSL_API int wc_InitSha256(wc_Sha256*); 149 | WOLFSSL_API int wc_InitSha256_ex(wc_Sha256*, void*, int); 150 | WOLFSSL_API int wc_Sha256Update(wc_Sha256*, const byte*, word32); 151 | WOLFSSL_API int wc_Sha256FinalRaw(wc_Sha256*, byte*); 152 | WOLFSSL_API int wc_Sha256Final(wc_Sha256*, byte*); 153 | WOLFSSL_API void wc_Sha256Free(wc_Sha256*); 154 | 155 | WOLFSSL_API int wc_Sha256GetHash(wc_Sha256*, byte*); 156 | WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst); 157 | 158 | #ifdef WOLFSSL_PIC32MZ_HASH 159 | WOLFSSL_API void wc_Sha256SizeSet(wc_Sha256*, word32); 160 | #endif 161 | 162 | #ifdef WOLFSSL_SHA224 163 | #ifndef HAVE_FIPS /* avoid redefinition of structs */ 164 | 165 | #ifndef NO_OLD_WC_NAMES 166 | #define Sha224 wc_Sha224 167 | #define SHA224 WC_SHA224 168 | #define SHA224_BLOCK_SIZE WC_SHA224_BLOCK_SIZE 169 | #define SHA224_DIGEST_SIZE WC_SHA224_DIGEST_SIZE 170 | #define SHA224_PAD_SIZE WC_SHA224_PAD_SIZE 171 | #endif 172 | 173 | /* in bytes */ 174 | enum { 175 | WC_SHA224 = WC_HASH_TYPE_SHA224, 176 | WC_SHA224_BLOCK_SIZE = WC_SHA256_BLOCK_SIZE, 177 | WC_SHA224_DIGEST_SIZE = 28, 178 | WC_SHA224_PAD_SIZE = WC_SHA256_PAD_SIZE 179 | }; 180 | 181 | 182 | typedef wc_Sha256 wc_Sha224; 183 | #endif /* HAVE_FIPS */ 184 | 185 | WOLFSSL_API int wc_InitSha224(wc_Sha224*); 186 | WOLFSSL_API int wc_InitSha224_ex(wc_Sha224*, void*, int); 187 | WOLFSSL_API int wc_Sha224Update(wc_Sha224*, const byte*, word32); 188 | WOLFSSL_API int wc_Sha224Final(wc_Sha224*, byte*); 189 | WOLFSSL_API void wc_Sha224Free(wc_Sha224*); 190 | 191 | WOLFSSL_API int wc_Sha224GetHash(wc_Sha224*, byte*); 192 | WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst); 193 | 194 | #endif /* WOLFSSL_SHA224 */ 195 | 196 | #ifdef __cplusplus 197 | } /* extern "C" */ 198 | #endif 199 | 200 | #endif /* NO_SHA256 */ 201 | #endif /* WOLF_CRYPT_SHA256_H */ 202 | 203 | -------------------------------------------------------------------------------- /src/relaynode.c: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file NodeBase.c 5 | * 6 | * @XYO Core library source code. 7 | * 8 | * @brief primary crypto routines for the XYO Core. 9 | * 10 | * Copyright (C) 2018 XY - The Findables Company 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | #include "relaynode.h" 20 | 21 | /** 22 | **************************************************************************************** 23 | * NAME 24 | * Relay_getChoice 25 | * 26 | * DESCRIPTION 27 | * Add a XYObject to the struct-hack 28 | * 29 | * PARAMETERS 30 | * object [in] CryptoCreator* 31 | * 32 | * RETURNS 33 | * id [out] char* 34 | * 35 | * NOTES 36 | * 37 | **************************************************************************************** 38 | */ 39 | uint8_t Relay_getChoice(uint8_t* theirCatalog){ 40 | if(*theirCatalog - BOUND_WITNESS_OPTION != 0){ 41 | return GIVE_ORIGIN_CHAIN_OPTION + BOUND_WITNESS_OPTION; 42 | } else { 43 | if(*theirCatalog & BOUND_WITNESS_OPTION){ 44 | return BOUND_WITNESS_OPTION; 45 | } else { 46 | return 0; 47 | } 48 | } 49 | return 0; 50 | } 51 | 52 | #define typedFlags 176 53 | #define untypedFlags 78 54 | #define untypedFlagsNoIteration 190 55 | 56 | char payloadBuffer[30] = { TYPED_ITERABLE, 0x01, 0x00, 0x00, 0x00, sizeof(payloadBuffer)-2, 176, 0x08, 57 | 0x00, 0x00, 0x00, sizeof(payloadBuffer)-8, 78, 0x01, 0x00, sizeof(payloadBuffer)-10, 190, 0x03, 58 | 0x00, sizeof(payloadBuffer)-12, 0x00, 0x00, 0x00, 0x01, UNTYPED_ITERABLE, 0x01, 59 | 0x00, 0x00, 0x00, 0x04}; 60 | char paydbg[] = { TYPED_ITERABLE, 0x01, 0x00, 0x00, 0x00, sizeof(payloadBuffer)-2, 176, 0x08, 61 | 0x00, 0x00, 0x00, sizeof(payloadBuffer)-8, 78, 0x01, 0x00, sizeof(payloadBuffer)-10, 190, 0x03, 62 | 0x00, sizeof(payloadBuffer)-12, 0x00, 0x00, 0x00, 0x01, UNTYPED_ITERABLE, 0x01, 63 | 0x00, 0x00, 0x00, 0x04}; 64 | 65 | char signatureBuffer[6+12+66] = { TYPED_ITERABLE, 0x01, 0x00, 0x00, 0x00, sizeof(signatureBuffer)-2, typedFlags, 0x08, 0x00, sizeof(signatureBuffer)-8, untypedFlags, 0x01, 0x00, 70, NONITERABLE_TWOBYTE, 0x04, 0x00, 68 }; 66 | char sigdbg[] = { TYPED_ITERABLE, 0x01, 0x00, 0x00, 0x00, sizeof(signatureBuffer)-2, typedFlags, 0x08, 0x00, sizeof(signatureBuffer)-8, untypedFlags, 0x01, 0x00, 70, NONITERABLE_TWOBYTE, 0x04, 0x00, 68 }; 67 | 68 | char pubkeyBuffer[6+8+66] = { TYPED_ITERABLE, 2, 0, 0, 0, sizeof(pubkeyBuffer)+sizeof(payloadBuffer)+sizeof(signatureBuffer), typedFlags, 0x01, 0x00, 68+4, untypedFlags, 0x0d, 0x00, 68 }; 69 | char pubdbg[] = { TYPED_ITERABLE, 2, 0, 0, 0, sizeof(pubkeyBuffer)+sizeof(payloadBuffer)+sizeof(signatureBuffer), typedFlags, 0x01, 0x00, 68+4, untypedFlags, 0x0d, 0x00, 68 }; 70 | 71 | 72 | /** 73 | **************************************************************************************** 74 | * NAME 75 | * doConnection 76 | * 77 | * DESCRIPTION 78 | * Add a XYObject to the struct-hack 79 | * 80 | * PARAMETERS 81 | * self [in] RelayNode_t* 82 | * 83 | * RETURNS 84 | * id [out] char* 85 | * 86 | * NOTES 87 | * 88 | **************************************************************************************** 89 | */ 90 | XYResult_t doConnection(RelayNode_t* self){ 91 | 92 | DECLARE_RESULT(); 93 | result = insertPublicKey(self); 94 | 95 | result = insertPayloads(self); 96 | 97 | result = insertSignature(self); 98 | to_uint32_be(globalBuffer, sizeof(pubkeyBuffer)+sizeof(payloadBuffer)+sizeof(signatureBuffer)+6); 99 | breakpoint(); 100 | socket_send(&self->networkPipe, globalBuffer, sizeof(pubkeyBuffer)+sizeof(payloadBuffer)+sizeof(signatureBuffer)+6, 0); 101 | char funBuffer[500]; 102 | int ret_code = socket_recv(&self->networkPipe, funBuffer, 65000).value.i; 103 | for(int i = 0; i < ret_code; i++){ 104 | printf("%d", ret_code); 105 | } 106 | printf("\n"); 107 | return result; 108 | } 109 | 110 | XYResult_t insertPublicKey(RelayNode_t* relay){ 111 | DECLARE_RESULT(); 112 | 113 | XYObject_t arrayObject; 114 | arrayObject.header = relay->networkPipe.scratchBuffer.payload; 115 | arrayObject.payload = relay->networkPipe.scratchBuffer.payload+2; 116 | 117 | void* endPtr; 118 | 119 | XYArrayItr_t itr = WeakArrayIterator(relay->networkPipe.scratchBuffer.payload, relay->networkPipe.scratchBuffer.payload+2); 120 | 121 | XYObject_t innerArrayObject; 122 | innerArrayObject.header = itr.header; 123 | innerArrayObject.payload = itr.indexPtr; 124 | //innerArrayObject = WeakArray_get(&itr, 0); 125 | 126 | 127 | XYArrayItr_t innerItr = WeakArrayIterator(relay->networkPipe.scratchBuffer.payload, itr.indexPtr); 128 | 129 | memset(pubkeyBuffer + 8 + 6, 1, 66); 130 | 131 | uint32_t oldLength = XYObject_getFullLength(&arrayObject).value.ui; 132 | 133 | Iterator_insert(&innerArrayObject, 0, 67+5, oldLength, pubkeyBuffer+8); 134 | XYObject_t* self = &arrayObject; 135 | XYOBJ_INCREMENT(67+5); 136 | socket_send(&relay->networkPipe, pubkeyBuffer, sizeof(pubkeyBuffer), 1); 137 | return result; 138 | } 139 | 140 | XYResult_t insertPayloads(RelayNode_t* relay){ 141 | DECLARE_RESULT(); 142 | 143 | XYObject_t arrayObject; 144 | arrayObject.header = relay->networkPipe.scratchBuffer.payload; 145 | arrayObject.payload = relay->networkPipe.scratchBuffer.payload+2; 146 | 147 | void* endPtr; 148 | 149 | XYArrayItr_t itr = WeakArrayIterator(relay->networkPipe.scratchBuffer.payload, relay->networkPipe.scratchBuffer.payload+2); 150 | 151 | XYObject_t innerArrayObject = IteratorNext(&itr); 152 | 153 | XYArrayItr_t innerItr = WeakArrayIterator(relay->networkPipe.scratchBuffer.payload, itr.indexPtr); 154 | 155 | Iterator_insert(&innerArrayObject, 0, (sizeof(payloadBuffer)-6), XYObject_getFullLength(&arrayObject).value.ui, payloadBuffer+6); 156 | socket_send(&relay->networkPipe, &payloadBuffer, sizeof(payloadBuffer), 1); 157 | return result; 158 | } 159 | 160 | 161 | XYResult_t insertSignature(RelayNode_t* relay){ 162 | DECLARE_RESULT(); 163 | 164 | XYObject_t arrayObject; 165 | arrayObject.header = (XYObjectHeader_t*)relay->networkPipe.scratchBuffer.payload; 166 | arrayObject.payload = relay->networkPipe.scratchBuffer.payload+2; 167 | 168 | void* endPtr; 169 | 170 | XYArrayItr_t itr = WeakArrayIterator((XYObjectHeader_t*)relay->networkPipe.scratchBuffer.payload, relay->networkPipe.scratchBuffer.payload+2); 171 | IteratorNext(&itr); 172 | 173 | XYObject_t innerArrayObject = IteratorNext(&itr); 174 | //XYObjectHeader_t innerArrayHeader; 175 | //innerArrayHeader.flags = typedFlags; 176 | //innerArrayHeader.type = TYPE_ARRAY; 177 | //innerArrayObject.header = &innerArrayHeader; 178 | //innerArrayObject.payload = itr.indexPtr; 179 | 180 | 181 | XYArrayItr_t innerItr = WeakArrayIterator((XYObjectHeader_t*)relay->networkPipe.scratchBuffer.payload, itr.indexPtr); 182 | 183 | //char writeBuffer[6+10+66] = { TYPED_ITERABLE, 0x01, 0x00, 0x00, 0x00, 80, *(uint8_t*)&typedFlags, 0x08, 0x00, 74, *(uint8_t*)&untypedFlags, 0x01, 0x00, 70, NONITERABLE_TWOBYTE, 0x04 }; 184 | memset(signatureBuffer + 12 + 6, 3, 66); 185 | Iterator_insert(&innerArrayObject, 0, 67+5, XYObject_getFullLength(&arrayObject).value.ui, signatureBuffer+6); 186 | socket_send(&relay->networkPipe, &signatureBuffer, sizeof(signatureBuffer), 1); 187 | 188 | return result; 189 | } 190 | -------------------------------------------------------------------------------- /src/XYObjects/Array/WeakArray/WeakArray.test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "WeakArray.h" 4 | #include "../Iterator.h" 5 | //#include "../../XYObjectHeader.h" 6 | #define XY_DEBUGMODE 7 | #ifdef XY_DEBUGMODE 8 | #define XY_ASSERT_EQUAL(A, B) XY_ASSERT_EQUAL_ex(A, B, __FILE__, __LINE__) 9 | #define XY_ASSERT_EQUAL_ex(A, B, file, line) if(A != B){ printf(RED "%s@%d Assert Failed\n" RESET, file, line); score = score + 1; } else { printf(GRN "%d==%d Assert Success\n", A, B); } 10 | #elif 11 | #define XY_ASSERT_EQUAL(A, B) if(A != B){ score = score + 1; } 12 | #endif 13 | int testAdd(); 14 | int testGap(); 15 | 16 | int main2(){ 17 | int score = 0; 18 | score += testAdd(); 19 | score += testGap(); 20 | score += testTypedGap(); 21 | return score; 22 | } 23 | 24 | int testAdd(){ 25 | int score = 0; 26 | 27 | XYHeaderFlags_t signatureFlags; 28 | signatureFlags.typed = 0; 29 | signatureFlags.iteratable = 1; 30 | signatureFlags.lengthType = 1; 31 | 32 | XYObjectHeader_t signatureHeader; 33 | signatureHeader.flags = signatureFlags; 34 | signatureHeader.type = 0x12; 35 | 36 | XYObject_t signatureObject; 37 | signatureObject.header = &signatureHeader; 38 | 39 | XYHeaderFlags_t untypedFlags; 40 | untypedFlags.typed = 0; 41 | untypedFlags.iteratable = 1; 42 | untypedFlags.lengthType = (uint8_t)2; 43 | 44 | XYObjectHeader_t arrayHeader; 45 | arrayHeader.flags = untypedFlags; 46 | arrayHeader.type = TYPE_ARRAY; 47 | 48 | XYObject_t arrayObject; 49 | arrayObject.header = &arrayHeader; 50 | 51 | arrayObject.payload = malloc(sizeof(char)*(67+67+4)); 52 | *(char*)&arrayObject.payload[3] = 4; // Set Size of array 53 | 54 | char* endPtr; 55 | XYObject_t* self = &arrayObject; 56 | 57 | endPtr = arrayObject.payload + XYObject_getLength(self).value.ui; 58 | XY_ASSERT_EQUAL(XYObject_getLength(self).value.ui, 4); 59 | 60 | WeakArray_add(self, signatureHeader, 67); 61 | XY_ASSERT_EQUAL(XYObject_getLength(self).value.ui, 4+67); 62 | XY_ASSERT_EQUAL(*(uint8_t*)(((&arrayObject)->payload)+4), *(uint8_t*)&signatureFlags ); 63 | 64 | WeakArray_add(self, signatureHeader, 67); 65 | XY_ASSERT_EQUAL(XYObject_getLength(self).value.ui, 4+67+67); 66 | XY_ASSERT_EQUAL(*(uint8_t*)(((&arrayObject)->payload)+4+67), *(uint8_t*)&signatureFlags ); 67 | 68 | endPtr = arrayObject.payload + XYObject_getLength(self).value.ui; 69 | XY_ASSERT_EQUAL(*(endPtr-(sizeof(char)*67)), *(uint8_t*)&signatureFlags ); 70 | 71 | return score; 72 | } 73 | void breakpoint(); 74 | void breakpoint(){} 75 | 76 | int testGap(){ 77 | int score = 0; 78 | 79 | XYHeaderFlags_t signatureFlags; 80 | signatureFlags.typed = 0; 81 | signatureFlags.iteratable = 1; 82 | signatureFlags.lengthType = 1; 83 | 84 | XYObjectHeader_t signatureHeader; 85 | signatureHeader.flags = signatureFlags; 86 | signatureHeader.type = 0x12; 87 | 88 | XYObject_t signatureObject; 89 | signatureObject.header = &signatureHeader; 90 | 91 | XYHeaderFlags_t untypedFlags; 92 | untypedFlags.typed = 0; 93 | untypedFlags.iteratable = 1; 94 | untypedFlags.lengthType = (uint8_t)2; 95 | 96 | XYObjectHeader_t arrayHeader; 97 | arrayHeader.flags = untypedFlags; 98 | arrayHeader.type = TYPE_ARRAY; 99 | 100 | XYObject_t arrayObject; 101 | char hackedHeader[6] = { *(uint8_t*)&untypedFlags, TYPE_ARRAY, 0, 0, 0, 4}; 102 | arrayObject.header = (XYObjectHeader_t*)hackedHeader; 103 | 104 | const uint8_t totalObjects = 5; 105 | const uint8_t overallocation = 10; 106 | 107 | arrayObject.payload = malloc(sizeof(char)*((((67)*totalObjects)+4)+overallocation)); 108 | *(char*)&arrayObject.payload[3] = 4; 109 | 110 | char* endPtr; 111 | XYObject_t* self = &arrayObject; 112 | 113 | WeakArray_add(self, signatureHeader, 6); 114 | 115 | XYArrayItr_t itr = WeakArrayIterator(arrayObject.header, arrayObject.payload); 116 | 117 | memset(((char*)itr.indexPtr), 3, 4); 118 | *(char*)&itr.indexPtr[0] = 0; 119 | *(char*)&itr.indexPtr[1] = 4; 120 | for(int i=0; ipayload)+(4+(6*(i+1)))), *(uint8_t*)&signatureFlags ); 130 | } 131 | 132 | endPtr = arrayObject.payload + XYObject_getLength(self).value.ui; 133 | XY_ASSERT_EQUAL(*(endPtr-(sizeof(char)*6)), *(uint8_t*)&signatureFlags ); 134 | 135 | Iterator_gap(&arrayObject, 3, 12, 2); 136 | 137 | char* newEndPtr = arrayObject.payload + XYObject_getLength(self).value.ui; 138 | //XY_ASSERT_EQUAL(*(newEndPtr-(sizeof(char)*6*2)), *(uint8_t*)&signatureFlags ); 139 | XY_ASSERT_EQUAL(*(newEndPtr-2), *endPtr ); 140 | 141 | Iterator_gap(&arrayObject, 2, 18, 5); 142 | newEndPtr = arrayObject.payload + XYObject_getLength(self).value.ui; 143 | 144 | //XY_ASSERT_EQUAL(*(newEndPtr-(sizeof(char)*6*2)), *(uint8_t*)&signatureFlags ); 145 | XY_ASSERT_EQUAL(*(newEndPtr-2-5), *endPtr ); 146 | 147 | return score; 148 | } 149 | 150 | int testTypedGap(){ 151 | int score = 0; 152 | 153 | XYHeaderFlags_t signatureFlags; 154 | signatureFlags.typed = 0; 155 | signatureFlags.iteratable = 1; 156 | signatureFlags.lengthType = 1; 157 | 158 | XYObjectHeader_t signatureHeader; 159 | signatureHeader.flags = signatureFlags; 160 | signatureHeader.type = 0x12; 161 | 162 | XYObject_t signatureObject; 163 | signatureObject.header = &signatureHeader; 164 | 165 | XYHeaderFlags_t typedFlags; 166 | typedFlags.typed = 1; 167 | typedFlags.iteratable = 1; 168 | typedFlags.lengthType = (uint8_t)2; 169 | 170 | XYObjectHeader_t arrayHeader; 171 | arrayHeader.flags = typedFlags; 172 | arrayHeader.type = TYPE_ARRAY; 173 | 174 | XYObject_t arrayObject; 175 | char hackedHeader[6] = { *(uint8_t*)&typedFlags, TYPE_ARRAY, 0, 0, 0, 4}; 176 | arrayObject.header = (XYObjectHeader_t*)hackedHeader; 177 | 178 | const uint8_t totalObjects = 5; 179 | const uint8_t overallocation = 10; 180 | 181 | arrayObject.payload = malloc(sizeof(char)*((((67)*totalObjects)+4)+overallocation)); 182 | *(char*)&arrayObject.payload[3] = 4; 183 | 184 | char* endPtr; 185 | XYObject_t* self = &arrayObject; 186 | WeakArray_add(self, signatureHeader, 6); 187 | 188 | XYArrayItr_t itr = WeakArrayIterator(arrayObject.header, arrayObject.payload); 189 | 190 | memset(((char*)itr.indexPtr), 3, 4); 191 | *(char*)&itr.indexPtr[0] = 0; 192 | *(char*)&itr.indexPtr[1] = 4; 193 | for(int i=0; ipayload)+(6+(4*(i+1))))), 4 ); 203 | } 204 | 205 | endPtr = arrayObject.payload + XYObject_getLength(self).value.ui; 206 | XY_ASSERT_EQUAL(to_uint16((unsigned char*)(endPtr-(sizeof(char)*4))), 4 ); 207 | 208 | Iterator_gap(&arrayObject, 3, 12, 2); 209 | 210 | char* newEndPtr = arrayObject.payload + XYObject_getLength(self).value.ui; 211 | //XY_ASSERT_EQUAL(*(newEndPtr-(sizeof(char)*6*2)), *(uint8_t*)&signatureFlags ); 212 | XY_ASSERT_EQUAL(*(newEndPtr-2), *endPtr ); 213 | 214 | Iterator_gap(&arrayObject, 2, 18, 5); 215 | newEndPtr = arrayObject.payload + XYObject_getLength(self).value.ui; 216 | 217 | //XY_ASSERT_EQUAL(*(newEndPtr-(sizeof(char)*6*2)), *(uint8_t*)&signatureFlags ); 218 | XY_ASSERT_EQUAL(*(newEndPtr-2-5), *endPtr ); 219 | 220 | breakpoint(); 221 | return score; 222 | } 223 | 224 | int testAddArray(){ 225 | int score = 0; 226 | return score; 227 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | GNU LESSER GENERAL PUBLIC LICENSE 3 | Version 3, 29 June 2007 4 | 5 | Copyright (C) 2007 Free Software Foundation, Inc. 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | 10 | This version of the GNU Lesser General Public License incorporates 11 | the terms and conditions of version 3 of the GNU General Public 12 | License, supplemented by the additional permissions listed below. 13 | 14 | 0. Additional Definitions. 15 | 16 | As used herein, "this License" refers to version 3 of the GNU Lesser 17 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 18 | General Public License. 19 | 20 | "The Library" refers to a covered work governed by this License, 21 | other than an Application or a Combined Work as defined below. 22 | 23 | An "Application" is any work that makes use of an interface provided 24 | by the Library, but which is not otherwise based on the Library. 25 | Defining a subclass of a class defined by the Library is deemed a mode 26 | of using an interface provided by the Library. 27 | 28 | A "Combined Work" is a work produced by combining or linking an 29 | Application with the Library. The particular version of the Library 30 | with which the Combined Work was made is also called the "Linked 31 | Version". 32 | 33 | The "Minimal Corresponding Source" for a Combined Work means the 34 | Corresponding Source for the Combined Work, excluding any source code 35 | for portions of the Combined Work that, considered in isolation, are 36 | based on the Application, and not on the Linked Version. 37 | 38 | The "Corresponding Application Code" for a Combined Work means the 39 | object code and/or source code for the Application, including any data 40 | and utility programs needed for reproducing the Combined Work from the 41 | Application, but excluding the System Libraries of the Combined Work. 42 | 43 | 1. Exception to Section 3 of the GNU GPL. 44 | 45 | You may convey a covered work under sections 3 and 4 of this License 46 | without being bound by section 3 of the GNU GPL. 47 | 48 | 2. Conveying Modified Versions. 49 | 50 | If you modify a copy of the Library, and, in your modifications, a 51 | facility refers to a function or data to be supplied by an Application 52 | that uses the facility (other than as an argument passed when the 53 | facility is invoked), then you may convey a copy of the modified 54 | version: 55 | 56 | a) under this License, provided that you make a good faith effort to 57 | ensure that, in the event an Application does not supply the 58 | function or data, the facility still operates, and performs 59 | whatever part of its purpose remains meaningful, or 60 | 61 | b) under the GNU GPL, with none of the additional permissions of 62 | this License applicable to that copy. 63 | 64 | 3. Object Code Incorporating Material from Library Header Files. 65 | 66 | The object code form of an Application may incorporate material from 67 | a header file that is part of the Library. You may convey such object 68 | code under terms of your choice, provided that, if the incorporated 69 | material is not limited to numerical parameters, data structure 70 | layouts and accessors, or small macros, inline functions and templates 71 | (ten or fewer lines in length), you do both of the following: 72 | 73 | a) Give prominent notice with each copy of the object code that the 74 | Library is used in it and that the Library and its use are 75 | covered by this License. 76 | 77 | b) Accompany the object code with a copy of the GNU GPL and this license 78 | document. 79 | 80 | 4. Combined Works. 81 | 82 | You may convey a Combined Work under terms of your choice that, 83 | taken together, effectively do not restrict modification of the 84 | portions of the Library contained in the Combined Work and reverse 85 | engineering for debugging such modifications, if you also do each of 86 | the following: 87 | 88 | a) Give prominent notice with each copy of the Combined Work that 89 | the Library is used in it and that the Library and its use are 90 | covered by this License. 91 | 92 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 93 | document. 94 | 95 | c) For a Combined Work that displays copyright notices during 96 | execution, include the copyright notice for the Library among 97 | these notices, as well as a reference directing the user to the 98 | copies of the GNU GPL and this license document. 99 | 100 | d) Do one of the following: 101 | 102 | 0) Convey the Minimal Corresponding Source under the terms of this 103 | License, and the Corresponding Application Code in a form 104 | suitable for, and under terms that permit, the user to 105 | recombine or relink the Application with a modified version of 106 | the Linked Version to produce a modified Combined Work, in the 107 | manner specified by section 6 of the GNU GPL for conveying 108 | Corresponding Source. 109 | 110 | 1) Use a suitable shared library mechanism for linking with the 111 | Library. A suitable mechanism is one that (a) uses at run time 112 | a copy of the Library already present on the user's computer 113 | system, and (b) will operate properly with a modified version 114 | of the Library that is interface-compatible with the Linked 115 | Version. 116 | 117 | e) Provide Installation Information, but only if you would otherwise 118 | be required to provide such information under section 6 of the 119 | GNU GPL, and only to the extent that such information is 120 | necessary to install and execute a modified version of the 121 | Combined Work produced by recombining or relinking the 122 | Application with a modified version of the Linked Version. (If 123 | you use option 4d0, the Installation Information must accompany 124 | the Minimal Corresponding Source and Corresponding Application 125 | Code. If you use option 4d1, you must provide the Installation 126 | Information in the manner specified by section 6 of the GNU GPL 127 | for conveying Corresponding Source.) 128 | 129 | 5. Combined Libraries. 130 | 131 | You may place library facilities that are a work based on the 132 | Library side by side in a single library together with other library 133 | facilities that are not Applications and are not covered by this 134 | License, and convey such a combined library under terms of your 135 | choice, if you do both of the following: 136 | 137 | a) Accompany the combined library with a copy of the same work based 138 | on the Library, uncombined with any other library facilities, 139 | conveyed under the terms of this License. 140 | 141 | b) Give prominent notice with the combined library that part of it 142 | is a work based on the Library, and explaining where to find the 143 | accompanying uncombined form of the same work. 144 | 145 | 6. Revised Versions of the GNU Lesser General Public License. 146 | 147 | The Free Software Foundation may publish revised and/or new versions 148 | of the GNU Lesser General Public License from time to time. Such new 149 | versions will be similar in spirit to the present version, but may 150 | differ in detail to address new problems or concerns. 151 | 152 | Each version is given a distinguishing version number. If the 153 | Library as you received it specifies that a certain numbered version 154 | of the GNU Lesser General Public License "or any later version" 155 | applies to it, you have the option of following the terms and 156 | conditions either of that published version or of any later version 157 | published by the Free Software Foundation. If the Library as you 158 | received it does not specify a version number of the GNU Lesser 159 | General Public License, you may choose any version of the GNU Lesser 160 | General Public License ever published by the Free Software Foundation. 161 | 162 | If the Library as you received it specifies that a proxy can decide 163 | whether future versions of the GNU Lesser General Public License shall 164 | apply, that proxy's public statement of acceptance of any version is 165 | permanent authorization for you to choose that version for the Library. 166 | 167 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_memory.h: -------------------------------------------------------------------------------- 1 | /* memory.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | /* submitted by eof */ 24 | 25 | /*! 26 | \file wolfssl/wolfcrypt/memory.h 27 | */ 28 | 29 | #ifndef WOLFSSL_MEMORY_H 30 | #define WOLFSSL_MEMORY_H 31 | 32 | #include 33 | //#include 34 | #include "wc_types.h" 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | #ifdef WOLFSSL_STATIC_MEMORY 41 | #ifdef WOLFSSL_DEBUG_MEMORY 42 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type, const char* func, unsigned int line); 43 | typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type, const char* func, unsigned int line); 44 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line); 45 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line); 46 | WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line); 47 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line); 48 | #else 49 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type); 50 | typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type); 51 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type); 52 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type); 53 | WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type); 54 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type); 55 | #endif /* WOLFSSL_DEBUG_MEMORY */ 56 | #else 57 | #ifdef WOLFSSL_DEBUG_MEMORY 58 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line); 59 | typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line); 60 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line); 61 | 62 | /* Public in case user app wants to use XMALLOC/XFREE */ 63 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line); 64 | WOLFSSL_API void wolfSSL_Free(void *ptr, const char* func, unsigned int line); 65 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line); 66 | #else 67 | typedef void *(*wolfSSL_Malloc_cb)(size_t size); 68 | typedef void (*wolfSSL_Free_cb)(void *ptr); 69 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size); 70 | /* Public in case user app wants to use XMALLOC/XFREE */ 71 | WOLFSSL_API void* wolfSSL_Malloc(size_t size); 72 | WOLFSSL_API void wolfSSL_Free(void *ptr); 73 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size); 74 | #endif /* WOLFSSL_DEBUG_MEMORY */ 75 | #endif /* WOLFSSL_STATIC_MEMORY */ 76 | 77 | /* Public get/set functions */ 78 | WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb, 79 | wolfSSL_Free_cb, 80 | wolfSSL_Realloc_cb); 81 | 82 | WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb*, 83 | wolfSSL_Free_cb*, 84 | wolfSSL_Realloc_cb*); 85 | 86 | #ifdef WOLFSSL_STATIC_MEMORY 87 | #define WOLFSSL_STATIC_TIMEOUT 1 88 | #ifndef WOLFSSL_STATIC_ALIGN 89 | #define WOLFSSL_STATIC_ALIGN 16 90 | #endif 91 | #ifndef WOLFMEM_MAX_BUCKETS 92 | #define WOLFMEM_MAX_BUCKETS 9 93 | #endif 94 | #define WOLFMEM_DEF_BUCKETS 9 /* number of default memory blocks */ 95 | #ifndef WOLFMEM_IO_SZ 96 | #define WOLFMEM_IO_SZ 16992 /* 16 byte aligned */ 97 | #endif 98 | #ifndef WOLFMEM_BUCKETS 99 | /* default size of chunks of memory to seperate into 100 | * having session certs enabled makes a 21k SSL struct */ 101 | #ifndef SESSION_CERTS 102 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,16128 103 | #else 104 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,21920 105 | #endif 106 | #endif 107 | #ifndef WOLFMEM_DIST 108 | #define WOLFMEM_DIST 8,4,4,12,4,5,8,1,1 109 | #endif 110 | 111 | /* flags for loading static memory (one hot bit) */ 112 | #define WOLFMEM_GENERAL 0x01 113 | #define WOLFMEM_IO_POOL 0x02 114 | #define WOLFMEM_IO_POOL_FIXED 0x04 115 | #define WOLFMEM_TRACK_STATS 0x08 116 | 117 | #ifndef WOLFSSL_MEM_GUARD 118 | #define WOLFSSL_MEM_GUARD 119 | typedef struct WOLFSSL_MEM_STATS WOLFSSL_MEM_STATS; 120 | typedef struct WOLFSSL_MEM_CONN_STATS WOLFSSL_MEM_CONN_STATS; 121 | #endif 122 | 123 | struct WOLFSSL_MEM_CONN_STATS { 124 | word32 peakMem; /* peak memory usage */ 125 | word32 curMem; /* current memory usage */ 126 | word32 peakAlloc; /* peak memory allocations */ 127 | word32 curAlloc; /* current memory allocations */ 128 | word32 totalAlloc;/* total memory allocations for lifetime */ 129 | word32 totalFr; /* total frees for lifetime */ 130 | }; 131 | 132 | struct WOLFSSL_MEM_STATS { 133 | word32 curAlloc; /* current memory allocations */ 134 | word32 totalAlloc;/* total memory allocations for lifetime */ 135 | word32 totalFr; /* total frees for lifetime */ 136 | word32 totalUse; /* total amount of memory used in blocks */ 137 | word32 avaIO; /* available IO specific pools */ 138 | word32 maxHa; /* max number of concurent handshakes allowed */ 139 | word32 maxIO; /* max number of concurent IO connections allowed */ 140 | word32 blockSz[WOLFMEM_MAX_BUCKETS]; /* block sizes in stacks */ 141 | word32 avaBlock[WOLFMEM_MAX_BUCKETS];/* ava block sizes */ 142 | word32 usedBlock[WOLFMEM_MAX_BUCKETS]; 143 | int flag; /* flag used */ 144 | }; 145 | 146 | typedef struct wc_Memory wc_Memory; /* internal structure for mem bucket */ 147 | typedef struct WOLFSSL_HEAP { 148 | wc_Memory* ava[WOLFMEM_MAX_BUCKETS]; 149 | wc_Memory* io; /* list of buffers to use for IO */ 150 | word32 maxHa; /* max concurent handshakes */ 151 | word32 curHa; 152 | word32 maxIO; /* max concurrent IO connections */ 153 | word32 curIO; 154 | word32 sizeList[WOLFMEM_MAX_BUCKETS];/* memory sizes in ava list */ 155 | word32 distList[WOLFMEM_MAX_BUCKETS];/* general distribution */ 156 | word32 inUse; /* amount of memory currently in use */ 157 | word32 ioUse; 158 | word32 alloc; /* total number of allocs */ 159 | word32 frAlc; /* total number of frees */ 160 | int flag; 161 | wolfSSL_Mutex memory_mutex; 162 | } WOLFSSL_HEAP; 163 | 164 | /* structure passed into XMALLOC as heap hint 165 | * having this abstraction allows tracking statistics of individual ssl's 166 | */ 167 | typedef struct WOLFSSL_HEAP_HINT { 168 | WOLFSSL_HEAP* memory; 169 | WOLFSSL_MEM_CONN_STATS* stats; /* hold individual connection stats */ 170 | wc_Memory* outBuf; /* set if using fixed io buffers */ 171 | wc_Memory* inBuf; 172 | byte haFlag; /* flag used for checking handshake count */ 173 | } WOLFSSL_HEAP_HINT; 174 | 175 | WOLFSSL_API int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint, 176 | unsigned char* buf, unsigned int sz, int flag, int max); 177 | 178 | WOLFSSL_LOCAL int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap); 179 | WOLFSSL_LOCAL int wolfSSL_load_static_memory(byte* buffer, word32 sz, 180 | int flag, WOLFSSL_HEAP* heap); 181 | WOLFSSL_LOCAL int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, 182 | WOLFSSL_MEM_STATS* stats); 183 | WOLFSSL_LOCAL int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io); 184 | WOLFSSL_LOCAL int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io); 185 | 186 | WOLFSSL_API int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag); 187 | WOLFSSL_API int wolfSSL_MemoryPaddingSz(void); 188 | #endif /* WOLFSSL_STATIC_MEMORY */ 189 | 190 | #ifdef __cplusplus 191 | } /* extern "C" */ 192 | #endif 193 | 194 | #endif /* WOLFSSL_MEMORY_H */ 195 | 196 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_rsa.h: -------------------------------------------------------------------------------- 1 | /* rsa.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | /*! 23 | \file wolfssl/wolfcrypt/rsa.h 24 | */ 25 | 26 | 27 | #ifndef WOLF_CRYPT_RSA_H 28 | #define WOLF_CRYPT_RSA_H 29 | 30 | //#include 31 | #include "wc_types.h" // wc_ = wolf crypto library routine (rsa) 32 | 33 | #ifndef NO_RSA 34 | 35 | /* RSA default exponent */ 36 | #ifndef WC_RSA_EXPONENT 37 | #define WC_RSA_EXPONENT 65537L 38 | #endif 39 | 40 | /* allow for user to plug in own crypto */ 41 | #if !defined(HAVE_FIPS) && (defined(HAVE_USER_RSA) || defined(HAVE_FAST_RSA)) 42 | #include "user_rsa.h" 43 | #else 44 | 45 | #if defined(HAVE_FIPS) && \ 46 | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)) 47 | /* for fips @wc_fips */ 48 | #include 49 | #if defined(CYASSL_KEY_GEN) && !defined(WOLFSSL_KEY_GEN) 50 | #define WOLFSSL_KEY_GEN 51 | #endif 52 | #else 53 | #include "wc_integer.h" 54 | #include "wc_random.h" 55 | #endif /* HAVE_FIPS && HAVE_FIPS_VERION 1 */ 56 | #if defined(HAVE_FIPS) && \ 57 | defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) 58 | #include 59 | #endif 60 | 61 | /* header file needed for OAEP padding */ 62 | //#include 63 | #include "wc_hash.h" 64 | 65 | #ifdef WOLFSSL_XILINX_CRYPT 66 | #include "xsecure_rsa.h" 67 | #endif 68 | 69 | #ifdef __cplusplus 70 | extern "C" { 71 | #endif 72 | 73 | /* avoid redefinition of structs */ 74 | #if !defined(HAVE_FIPS) || \ 75 | (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) 76 | 77 | #ifdef WOLFSSL_ASYNC_CRYPT 78 | #include 79 | #ifdef WOLFSSL_CERT_GEN 80 | #include 81 | #endif 82 | #endif 83 | 84 | enum { 85 | RSA_PUBLIC = 0, 86 | RSA_PRIVATE = 1, 87 | 88 | RSA_TYPE_UNKNOWN = -1, 89 | RSA_PUBLIC_ENCRYPT = 0, 90 | RSA_PUBLIC_DECRYPT = 1, 91 | RSA_PRIVATE_ENCRYPT = 2, 92 | RSA_PRIVATE_DECRYPT = 3, 93 | 94 | RSA_BLOCK_TYPE_1 = 1, 95 | RSA_BLOCK_TYPE_2 = 2, 96 | 97 | RSA_MIN_SIZE = 512, 98 | RSA_MAX_SIZE = 4096, 99 | 100 | RSA_MIN_PAD_SZ = 11, /* separator + 0 + pad value + 8 pads */ 101 | 102 | RSA_PSS_PAD_SZ = 8, 103 | 104 | #ifdef OPENSSL_EXTRA 105 | RSA_PKCS1_PADDING_SIZE = 11, 106 | RSA_PKCS1_OAEP_PADDING_SIZE = 42, /* (2 * hashlen(SHA-1)) + 2 */ 107 | #endif 108 | #ifdef WC_RSA_PSS 109 | RSA_PSS_PAD_TERM = 0xBC, 110 | #endif 111 | }; 112 | 113 | /* RSA */ 114 | struct RsaKey { 115 | mp_int n, e, d, p, q, dP, dQ, u; 116 | void* heap; /* for user memory overrides */ 117 | byte* data; /* temp buffer for async RSA */ 118 | int type; /* public or private */ 119 | int state; 120 | word32 dataLen; 121 | #ifdef WC_RSA_BLINDING 122 | WC_RNG* rng; /* for PrivateDecrypt blinding */ 123 | #endif 124 | #ifdef WOLF_CRYPTO_DEV 125 | int devId; 126 | #endif 127 | #ifdef WOLFSSL_ASYNC_CRYPT 128 | WC_ASYNC_DEV asyncDev; 129 | #ifdef WOLFSSL_CERT_GEN 130 | CertSignCtx certSignCtx; /* context info for cert sign (MakeSignature) */ 131 | #endif 132 | #endif /* WOLFSSL_ASYNC_CRYPT */ 133 | #ifdef WOLFSSL_XILINX_CRYPT 134 | word32 pubExp; /* to keep values in scope they are here in struct */ 135 | byte* mod; 136 | XSecure_Rsa xRsa; 137 | #endif 138 | byte dataIsAlloc; 139 | }; 140 | 141 | #ifndef WC_RSAKEY_TYPE_DEFINED 142 | typedef struct RsaKey RsaKey; 143 | #define WC_RSAKEY_TYPE_DEFINED 144 | #endif 145 | 146 | #endif /*HAVE_FIPS */ 147 | 148 | WOLFSSL_API int wc_InitRsaKey(RsaKey* key, void* heap); 149 | WOLFSSL_API int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId); 150 | WOLFSSL_API int wc_FreeRsaKey(RsaKey* key); 151 | #ifdef WOLFSSL_XILINX_CRYPT 152 | WOLFSSL_LOCAL int wc_InitRsaHw(RsaKey* key); 153 | #endif /* WOLFSSL_XILINX_CRYPT */ 154 | 155 | WOLFSSL_API int wc_RsaFunction(const byte* in, word32 inLen, byte* out, 156 | word32* outLen, int type, RsaKey* key, WC_RNG* rng); 157 | 158 | WOLFSSL_API int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out, 159 | word32 outLen, RsaKey* key, WC_RNG* rng); 160 | WOLFSSL_API int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out, 161 | RsaKey* key); 162 | WOLFSSL_API int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out, 163 | word32 outLen, RsaKey* key); 164 | WOLFSSL_API int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out, 165 | word32 outLen, RsaKey* key, WC_RNG* rng); 166 | WOLFSSL_API int wc_RsaPSS_Sign(const byte* in, word32 inLen, byte* out, 167 | word32 outLen, enum wc_HashType hash, int mgf, 168 | RsaKey* key, WC_RNG* rng); 169 | WOLFSSL_API int wc_RsaPSS_Sign_ex(const byte* in, word32 inLen, byte* out, 170 | word32 outLen, enum wc_HashType hash, 171 | int mgf, int saltLen, RsaKey* key, 172 | WC_RNG* rng); 173 | WOLFSSL_API int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out, 174 | RsaKey* key); 175 | WOLFSSL_API int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out, 176 | word32 outLen, RsaKey* key); 177 | WOLFSSL_API int wc_RsaPSS_VerifyInline(byte* in, word32 inLen, byte** out, 178 | enum wc_HashType hash, int mgf, 179 | RsaKey* key); 180 | WOLFSSL_API int wc_RsaPSS_VerifyInline_ex(byte* in, word32 inLen, byte** out, 181 | enum wc_HashType hash, int mgf, 182 | int saltLen, RsaKey* key); 183 | WOLFSSL_API int wc_RsaPSS_Verify(byte* in, word32 inLen, byte* out, 184 | word32 outLen, enum wc_HashType hash, int mgf, 185 | RsaKey* key); 186 | WOLFSSL_API int wc_RsaPSS_Verify_ex(byte* in, word32 inLen, byte* out, 187 | word32 outLen, enum wc_HashType hash, 188 | int mgf, int saltLen, RsaKey* key); 189 | WOLFSSL_API int wc_RsaPSS_CheckPadding(const byte* in, word32 inLen, byte* sig, 190 | word32 sigSz, 191 | enum wc_HashType hashType); 192 | WOLFSSL_API int wc_RsaPSS_CheckPadding_ex(const byte* in, word32 inLen, 193 | byte* sig, word32 sigSz, 194 | enum wc_HashType hashType, 195 | int saltLen); 196 | 197 | WOLFSSL_API int wc_RsaEncryptSize(RsaKey* key); 198 | 199 | #if !defined(HAVE_FIPS) || \ 200 | (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) 201 | /* to avoid asn duplicate symbols @wc_fips */ 202 | WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx, 203 | RsaKey*, word32); 204 | WOLFSSL_API int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx, 205 | RsaKey*, word32); 206 | WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz, 207 | const byte* e, word32 eSz, RsaKey* key); 208 | #ifdef WOLFSSL_KEY_GEN 209 | WOLFSSL_API int wc_RsaKeyToDer(RsaKey*, byte* output, word32 inLen); 210 | #endif 211 | 212 | WOLFSSL_API int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng); 213 | 214 | /* 215 | choice of padding added after fips, so not available when using fips RSA 216 | */ 217 | 218 | /* Mask Generation Function Identifiers */ 219 | #define WC_MGF1NONE 0 220 | #define WC_MGF1SHA1 26 221 | #define WC_MGF1SHA224 4 222 | #define WC_MGF1SHA256 1 223 | #define WC_MGF1SHA384 2 224 | #define WC_MGF1SHA512 3 225 | 226 | /* Padding types */ 227 | #define WC_RSA_PKCSV15_PAD 0 228 | #define WC_RSA_OAEP_PAD 1 229 | #define WC_RSA_PSS_PAD 2 230 | #define WC_RSA_NO_PAD 3 231 | 232 | WOLFSSL_API int wc_RsaPublicEncrypt_ex(const byte* in, word32 inLen, byte* out, 233 | word32 outLen, RsaKey* key, WC_RNG* rng, int type, 234 | enum wc_HashType hash, int mgf, byte* label, word32 lableSz); 235 | WOLFSSL_API int wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen, 236 | byte* out, word32 outLen, RsaKey* key, int type, 237 | enum wc_HashType hash, int mgf, byte* label, word32 lableSz); 238 | WOLFSSL_API int wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen, 239 | byte** out, RsaKey* key, int type, enum wc_HashType hash, 240 | int mgf, byte* label, word32 lableSz); 241 | #if defined(WC_RSA_DIRECT) || defined(WC_RSA_NO_PADDING) 242 | WOLFSSL_API int wc_RsaDirect(byte* in, word32 inLen, byte* out, word32* outSz, 243 | RsaKey* key, int type, WC_RNG* rng); 244 | #endif 245 | 246 | #endif /* HAVE_FIPS*/ 247 | 248 | WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey*, byte*, word32*, byte*, word32*); 249 | WOLFSSL_API int wc_RsaExportKey(RsaKey* key, 250 | byte* e, word32* eSz, 251 | byte* n, word32* nSz, 252 | byte* d, word32* dSz, 253 | byte* p, word32* pSz, 254 | byte* q, word32* qSz); 255 | 256 | WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey*, byte* output, word32 inLen); 257 | 258 | //#define WOLFSSL_KEY_GEN 259 | #ifdef WOLFSSL_KEY_GEN 260 | WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, 261 | int size, 262 | long e, 263 | WC_RNG* rng); 264 | WOLFSSL_API int wc_CheckProbablePrime(const byte* p, word32 pSz, 265 | const byte* q, word32 qSz, 266 | const byte* e, word32 eSz, 267 | int nlen, int* isPrime); 268 | #endif 269 | 270 | #endif /* HAVE_USER_RSA */ 271 | 272 | #ifdef __cplusplus 273 | } /* extern "C" */ 274 | #endif 275 | 276 | #endif /* NO_RSA */ 277 | #endif /* WOLF_CRYPT_RSA_H */ 278 | 279 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_integer.h: -------------------------------------------------------------------------------- 1 | /* integer.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | /* 24 | * Based on public domain LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca, 25 | * http://math.libtomcrypt.com 26 | */ 27 | 28 | 29 | #ifndef WOLF_CRYPT_INTEGER_H 30 | #define WOLF_CRYPT_INTEGER_H 31 | 32 | /* may optionally use fast math instead, not yet supported on all platforms and 33 | may not be faster on all 34 | */ 35 | //#include /* will set MP_xxBIT if not default */ 36 | #include "wc_types.h" /* will set MP_xxBIT if not default */ 37 | 38 | #ifdef WOLFSSL_SP_MATH 39 | #include 40 | #elif defined(USE_FAST_MATH) 41 | #include 42 | #else 43 | 44 | //#include 45 | #include "wc_random.h" 46 | 47 | #ifndef CHAR_BIT 48 | #include 49 | #endif 50 | 51 | //#include 52 | #include "wc_mpi_class.h" 53 | 54 | /* wolf big int and common functions */ 55 | //#include 56 | #include "wc_wolfmath.h" 57 | 58 | 59 | #ifdef WOLFSSL_PUBLIC_MP 60 | #define MP_API WOLFSSL_API 61 | #else 62 | #define MP_API 63 | #endif 64 | 65 | #ifndef MIN 66 | #define MIN(x,y) ((x)<(y)?(x):(y)) 67 | #endif 68 | 69 | #ifndef MAX 70 | #define MAX(x,y) ((x)>(y)?(x):(y)) 71 | #endif 72 | 73 | #ifdef __cplusplus 74 | extern "C" { 75 | 76 | /* C++ compilers don't like assigning void * to mp_digit * */ 77 | #define OPT_CAST(x) (x *) 78 | 79 | #else 80 | 81 | /* C on the other hand doesn't care */ 82 | #define OPT_CAST(x) 83 | 84 | #endif /* __cplusplus */ 85 | 86 | 87 | /* detect 64-bit mode if possible */ 88 | #if defined(__x86_64__) 89 | #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT)) 90 | #define MP_64BIT 91 | #endif 92 | #endif 93 | /* if intel compiler doesn't provide 128 bit type don't turn on 64bit */ 94 | #if defined(MP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T) 95 | #undef MP_64BIT 96 | #endif 97 | 98 | 99 | /* allow user to define on mp_digit, mp_word, DIGIT_BIT types */ 100 | #ifndef WOLFSSL_BIGINT_TYPES 101 | 102 | /* some default configurations. 103 | * 104 | * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits 105 | * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits 106 | * 107 | * At the very least a mp_digit must be able to hold 7 bits 108 | * [any size beyond that is ok provided it doesn't overflow the data type] 109 | */ 110 | #ifdef MP_8BIT 111 | typedef unsigned char mp_digit; 112 | typedef unsigned short mp_word; 113 | #elif defined(MP_16BIT) || defined(NO_64BIT) 114 | typedef unsigned short mp_digit; 115 | typedef unsigned int mp_word; 116 | #define DIGIT_BIT 12 117 | #elif defined(MP_64BIT) 118 | /* for GCC only on supported platforms */ 119 | typedef unsigned long long mp_digit; /* 64 bit type, 128 uses mode(TI) */ 120 | typedef unsigned long mp_word __attribute__ ((mode(TI))); 121 | 122 | #define DIGIT_BIT 60 123 | #else 124 | /* this is the default case, 28-bit digits */ 125 | 126 | #if defined(_MSC_VER) || defined(__BORLANDC__) 127 | typedef unsigned __int64 ulong64; 128 | #else 129 | typedef unsigned long long ulong64; 130 | #endif 131 | 132 | typedef unsigned int mp_digit; /* long could be 64 now, changed TAO */ 133 | typedef ulong64 mp_word; 134 | 135 | #ifdef MP_31BIT 136 | /* this is an extension that uses 31-bit digits */ 137 | #define DIGIT_BIT 31 138 | #else 139 | /* default case is 28-bit digits, defines MP_28BIT as a handy test macro */ 140 | #define DIGIT_BIT 28 141 | #define MP_28BIT 142 | #endif 143 | #endif 144 | 145 | #endif /* WOLFSSL_BIGINT_TYPES */ 146 | 147 | /* otherwise the bits per digit is calculated automatically from the size of 148 | a mp_digit */ 149 | #ifndef DIGIT_BIT 150 | #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) 151 | /* bits per digit */ 152 | #endif 153 | 154 | #define MP_DIGIT_BIT DIGIT_BIT 155 | #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1)) 156 | #define MP_DIGIT_MAX MP_MASK 157 | 158 | /* equalities */ 159 | #define MP_LT -1 /* less than */ 160 | #define MP_EQ 0 /* equal to */ 161 | #define MP_GT 1 /* greater than */ 162 | 163 | #define MP_ZPOS 0 /* positive integer */ 164 | #define MP_NEG 1 /* negative */ 165 | 166 | #define MP_OKAY 0 /* ok result */ 167 | #define MP_MEM -2 /* out of mem */ 168 | #define MP_VAL -3 /* invalid input */ 169 | #define MP_NOT_INF -4 /* point not at infinity */ 170 | #define MP_RANGE MP_NOT_INF 171 | 172 | #define MP_YES 1 /* yes response */ 173 | #define MP_NO 0 /* no response */ 174 | 175 | /* Primality generation flags */ 176 | #define LTM_PRIME_BBS 0x0001 /* BBS style prime */ 177 | #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */ 178 | #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */ 179 | 180 | typedef int mp_err; 181 | 182 | /* define this to use lower memory usage routines (exptmods mostly) */ 183 | #define MP_LOW_MEM 184 | 185 | /* default precision */ 186 | #ifndef MP_PREC 187 | #ifndef MP_LOW_MEM 188 | #define MP_PREC 32 /* default digits of precision */ 189 | #else 190 | #define MP_PREC 1 /* default digits of precision */ 191 | #endif 192 | #endif 193 | 194 | /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - 195 | BITS_PER_DIGIT*2) */ 196 | #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1)) 197 | 198 | #ifdef HAVE_WOLF_BIGINT 199 | struct WC_BIGINT; 200 | #endif 201 | 202 | /* the mp_int structure */ 203 | typedef struct mp_int { 204 | int used, alloc, sign; 205 | mp_digit *dp; 206 | 207 | #ifdef HAVE_WOLF_BIGINT 208 | struct WC_BIGINT raw; /* unsigned binary (big endian) */ 209 | #endif 210 | } mp_int; 211 | #define MP_INT_DEFINED 212 | 213 | /* callback for mp_prime_random, should fill dst with random bytes and return 214 | how many read [up to len] */ 215 | typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); 216 | 217 | 218 | #define USED(m) ((m)->used) 219 | #define DIGIT(m,k) ((m)->dp[(k)]) 220 | #define SIGN(m) ((m)->sign) 221 | 222 | 223 | /* ---> Basic Manipulations <--- */ 224 | #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) 225 | #define mp_isone(a) \ 226 | (((((a)->used == 1)) && ((a)->dp[0] == 1u)) ? MP_YES : MP_NO) 227 | #define mp_iseven(a) \ 228 | (((a)->used > 0 && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO) 229 | #define mp_isodd(a) \ 230 | (((a)->used > 0 && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO) 231 | #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO) 232 | 233 | /* number of primes */ 234 | #ifdef MP_8BIT 235 | #define PRIME_SIZE 31 236 | #else 237 | #define PRIME_SIZE 256 238 | #endif 239 | 240 | #ifndef MAX_INVMOD_SZ 241 | #if defined(WOLFSSL_MYSQL_COMPATIBLE) 242 | #define MAX_INVMOD_SZ 8192 243 | #else 244 | #define MAX_INVMOD_SZ 4096 245 | #endif 246 | #endif 247 | 248 | #define mp_prime_random(a, t, size, bbs, cb, dat) \ 249 | mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat) 250 | 251 | #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len)) 252 | #define mp_raw_size(mp) mp_signed_bin_size(mp) 253 | #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str)) 254 | #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len)) 255 | #define mp_mag_size(mp) mp_unsigned_bin_size(mp) 256 | #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str)) 257 | 258 | #define MP_RADIX_BIN 2 259 | #define MP_RADIX_OCT 8 260 | #define MP_RADIX_DEC 10 261 | #define MP_RADIX_HEX 16 262 | #define MP_RADIX_MAX 64 263 | 264 | #define mp_tobinary(M, S) mp_toradix((M), (S), MP_RADIX_BIN) 265 | #define mp_tooctal(M, S) mp_toradix((M), (S), MP_RADIX_OCT) 266 | #define mp_todecimal(M, S) mp_toradix((M), (S), MP_RADIX_DEC) 267 | #define mp_tohex(M, S) mp_toradix((M), (S), MP_RADIX_HEX) 268 | 269 | #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1) 270 | 271 | #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \ 272 | defined(WOLFSSL_DEBUG_MATH) || defined(DEBUG_WOLFSSL) 273 | extern const char *mp_s_rmap; 274 | #endif 275 | 276 | /* 6 functions needed by Rsa */ 277 | MP_API int mp_init (mp_int * a); 278 | MP_API void mp_clear (mp_int * a); 279 | MP_API void mp_free (mp_int * a); 280 | MP_API void mp_forcezero(mp_int * a); 281 | MP_API int mp_unsigned_bin_size(mp_int * a); 282 | MP_API int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c); 283 | MP_API int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b); 284 | MP_API int mp_to_unsigned_bin (mp_int * a, unsigned char *b); 285 | MP_API int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y); 286 | /* end functions needed by Rsa */ 287 | 288 | /* functions added to support above needed, removed TOOM and KARATSUBA */ 289 | MP_API int mp_count_bits (mp_int * a); 290 | MP_API int mp_leading_bit (mp_int * a); 291 | MP_API int mp_init_copy (mp_int * a, mp_int * b); 292 | MP_API int mp_copy (mp_int * a, mp_int * b); 293 | MP_API int mp_grow (mp_int * a, int size); 294 | MP_API int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d); 295 | MP_API void mp_zero (mp_int * a); 296 | MP_API void mp_clamp (mp_int * a); 297 | MP_API void mp_exch (mp_int * a, mp_int * b); 298 | MP_API void mp_rshd (mp_int * a, int b); 299 | MP_API void mp_rshb (mp_int * a, int b); 300 | MP_API int mp_mod_2d (mp_int * a, int b, mp_int * c); 301 | MP_API int mp_mul_2d (mp_int * a, int b, mp_int * c); 302 | MP_API int mp_lshd (mp_int * a, int b); 303 | MP_API int mp_abs (mp_int * a, mp_int * b); 304 | MP_API int mp_invmod (mp_int * a, mp_int * b, mp_int * c); 305 | int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c); 306 | MP_API int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c); 307 | MP_API int mp_cmp_mag (mp_int * a, mp_int * b); 308 | MP_API int mp_cmp (mp_int * a, mp_int * b); 309 | MP_API int mp_cmp_d(mp_int * a, mp_digit b); 310 | MP_API int mp_set (mp_int * a, mp_digit b); 311 | MP_API int mp_is_bit_set (mp_int * a, mp_digit b); 312 | MP_API int mp_mod (mp_int * a, mp_int * b, mp_int * c); 313 | MP_API int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d); 314 | MP_API int mp_div_2(mp_int * a, mp_int * b); 315 | MP_API int mp_add (mp_int * a, mp_int * b, mp_int * c); 316 | int s_mp_add (mp_int * a, mp_int * b, mp_int * c); 317 | int s_mp_sub (mp_int * a, mp_int * b, mp_int * c); 318 | MP_API int mp_sub (mp_int * a, mp_int * b, mp_int * c); 319 | MP_API int mp_reduce_is_2k_l(mp_int *a); 320 | MP_API int mp_reduce_is_2k(mp_int *a); 321 | MP_API int mp_dr_is_modulus(mp_int *a); 322 | MP_API int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, 323 | int); 324 | MP_API int mp_montgomery_setup (mp_int * n, mp_digit * rho); 325 | int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho); 326 | MP_API int mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho); 327 | MP_API void mp_dr_setup(mp_int *a, mp_digit *d); 328 | MP_API int mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k); 329 | MP_API int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d); 330 | int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs); 331 | int s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs); 332 | MP_API int mp_reduce_2k_setup_l(mp_int *a, mp_int *d); 333 | MP_API int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d); 334 | MP_API int mp_reduce (mp_int * x, mp_int * m, mp_int * mu); 335 | MP_API int mp_reduce_setup (mp_int * a, mp_int * b); 336 | int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode); 337 | MP_API int mp_montgomery_calc_normalization (mp_int * a, mp_int * b); 338 | int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs); 339 | int s_mp_sqr (mp_int * a, mp_int * b); 340 | int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs); 341 | int fast_s_mp_sqr (mp_int * a, mp_int * b); 342 | MP_API int mp_init_size (mp_int * a, int size); 343 | MP_API int mp_div_3 (mp_int * a, mp_int *c, mp_digit * d); 344 | MP_API int mp_mul_2(mp_int * a, mp_int * b); 345 | MP_API int mp_mul (mp_int * a, mp_int * b, mp_int * c); 346 | MP_API int mp_sqr (mp_int * a, mp_int * b); 347 | MP_API int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d); 348 | MP_API int mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d); 349 | MP_API int mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d); 350 | MP_API int mp_mul_d (mp_int * a, mp_digit b, mp_int * c); 351 | MP_API int mp_2expt (mp_int * a, int b); 352 | MP_API int mp_set_bit (mp_int * a, int b); 353 | MP_API int mp_reduce_2k_setup(mp_int *a, mp_digit *d); 354 | MP_API int mp_add_d (mp_int* a, mp_digit b, mp_int* c); 355 | MP_API int mp_set_int (mp_int * a, unsigned long b); 356 | MP_API int mp_sub_d (mp_int * a, mp_digit b, mp_int * c); 357 | /* end support added functions */ 358 | 359 | /* added */ 360 | MP_API int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, 361 | mp_int* f); 362 | MP_API int mp_toradix (mp_int *a, char *str, int radix); 363 | MP_API int mp_radix_size (mp_int * a, int radix, int *size); 364 | 365 | #ifdef WOLFSSL_DEBUG_MATH 366 | MP_API void mp_dump(const char* desc, mp_int* a, byte verbose); 367 | #else 368 | #define mp_dump(desc, a, verbose) 369 | #endif 370 | 371 | #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) 372 | MP_API int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c); 373 | #endif 374 | #if !defined(NO_DSA) || defined(HAVE_ECC) 375 | MP_API int mp_read_radix(mp_int* a, const char* str, int radix); 376 | #endif 377 | 378 | #define WOLFSSL_KEY_GEN 379 | #ifdef WOLFSSL_KEY_GEN 380 | MP_API int mp_prime_is_prime (mp_int * a, int t, int *result); 381 | MP_API int mp_gcd (mp_int * a, mp_int * b, mp_int * c); 382 | MP_API int mp_lcm (mp_int * a, mp_int * b, mp_int * c); 383 | MP_API int mp_rand_prime(mp_int* N, int len, WC_RNG* rng, void* heap); 384 | #endif 385 | 386 | MP_API int mp_cnt_lsb(mp_int *a); 387 | MP_API int mp_mod_d(mp_int* a, mp_digit b, mp_digit* c); 388 | 389 | 390 | /* wolf big int and common functions */ 391 | //#include 392 | #include "wc_wolfmath.h" 393 | 394 | 395 | #ifdef __cplusplus 396 | } 397 | #endif 398 | 399 | 400 | #endif /* USE_FAST_MATH */ 401 | 402 | #endif /* WOLF_CRYPT_INTEGER_H */ 403 | 404 | -------------------------------------------------------------------------------- /src/crypto.c: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************************** 3 | * 4 | * @file crypto.c 5 | * 6 | * @XY4 project source code. 7 | * 8 | * @brief primary crypto routines for the XY4 firmware. 9 | * 10 | * Copyright (C) 2017 XY - The Findables Company. All Rights Reserved. 11 | * 12 | **************************************************************************************** 13 | */ 14 | 15 | /* 16 | * INCLUDES 17 | **************************************************************************************** 18 | */ 19 | 20 | #include 21 | #include "crypto.h" // includes "xyobject.h", "hash.h" 22 | 23 | 24 | /* 25 | * FUNCTIONS & METHODS 26 | **************************************************************************************** 27 | */ 28 | 29 | /** 30 | **************************************************************************************** 31 | * NAME 32 | * newCryptoSignerInstance 33 | * 34 | * DESCRIPTION 35 | * Provides a new instance of an XyoCryptoSigner. 36 | * 37 | * PARAMETERS 38 | * privateKey [in] ByteArray_t* // A private key to initialize the 39 | * // XyoCryptoSigner with a public 40 | * // private keypair. To obtain a 41 | * // private key, one must call 42 | * // getPrivateKey() on a XyoCryptoSigner. 43 | * // If this value is null, the XyoCryptoSigner 44 | * // will automatically create a public 45 | * // and private keypair. 46 | * RETURNS 47 | * preallocated_return_result_ptr [out] XYResult_t* 48 | * // A XyoCryptoSigner that is compatible 49 | * // with getPublicKeyId() type and the 50 | * // getSignatureId() type. 51 | * NOTES 52 | * will return a malloc error if malloc fails. 53 | **************************************************************************************** 54 | */ 55 | XYResult_t* newCryptoSignerInstance(ByteArray_t* privateKey) {} 56 | 57 | /** 58 | **************************************************************************************** 59 | * NAME 60 | * newCryptoCreator 61 | * 62 | * DESCRIPTION 63 | * this routine returns a new CryptoCreator 64 | * 65 | * PARAMETERS 66 | * none 67 | * 68 | * RETURNS 69 | * preallocated_return_result_ptr [out] XYResult_t* 70 | * 71 | * NOTES 72 | * will return a malloc error if malloc fails. 73 | **************************************************************************************** 74 | */ 75 | XYResult_t* newCryptoCreator(){} 76 | 77 | /** 78 | **************************************************************************************** 79 | * NAME 80 | * cryptoGetId 81 | * 82 | * DESCRIPTION 83 | * this routine returns the id of the object supplied 84 | * 85 | * PARAMETERS 86 | * object [in] CryptoCreator_t* 87 | * 88 | * RETURNS 89 | * preallocated_return_result_ptr [out] XYResult_t* 90 | * 91 | * NOTES 92 | * this routine is a 'getter' only. it does't generate a new ID. 93 | **************************************************************************************** 94 | */ 95 | XYResult_t* cryptoGetId(CryptoCreator_t* object){} 96 | 97 | /** 98 | **************************************************************************************** 99 | * NAME 100 | * getSignatureId 101 | * 102 | * DESCRIPTION 103 | * this routine returns the public key from the supplied signer 104 | * 105 | * PARAMETERS 106 | * signer [in] Signer* 107 | * 108 | * RETURNS 109 | * preallocated_return_result_ptr [out] XYResult_t* 110 | * // The id (in form of a major in minor) 111 | * // of a signature key type that the 112 | * // XyoCryptoSigner (generated from newInstance()) 113 | * // will return when calling sign(data). 114 | * // This is used when verifying signature types. 115 | * NOTES 116 | * this routine is a 'getter' only. it does't generate a new ID. 117 | **************************************************************************************** 118 | */ 119 | XYResult_t* getSignatureId(Signer_t* signer){} 120 | 121 | /** 122 | **************************************************************************************** 123 | * NAME 124 | * getPublicKey 125 | * 126 | * DESCRIPTION 127 | * this routine returns the public key from the supplied signer 128 | * 129 | * PARAMETERS 130 | * signer [in] Signer* 131 | * 132 | * RETURNS 133 | * preallocated_return_result_ptr [out] XYResult_t* 134 | * 135 | * NOTES 136 | * this routine is a 'getter' only. it does't generate a new key. 137 | **************************************************************************************** 138 | */ 139 | XYResult_t* getPublicKey(Signer_t* signer){} 140 | 141 | /** 142 | **************************************************************************************** 143 | * NAME 144 | * getPublicKeyId 145 | * 146 | * DESCRIPTION 147 | * The id (in the form of a major in minor) of a public key type that the XyoCryptoSigner 148 | * (generated from newInstance()) will return when calling getPublicKey(). This is 149 | * used when verifying signature types. 150 | * 151 | * PARAMETERS 152 | * signer [in] Signer_t* 153 | * 154 | * RETURNS 155 | * preallocated_return_result_ptr [out] XYResult_t* // this holds an error code and the 156 | * // Public Key Id. 157 | * NOTES 158 | * this routine is a 'getter' only. it does't generate a new key. 159 | **************************************************************************************** 160 | */ 161 | XYResult_t* getPublicKeyId(Signer_t* signer){} 162 | 163 | /** 164 | **************************************************************************************** 165 | * NAME 166 | * newPublicKey 167 | * 168 | * DESCRIPTION 169 | * this routine returns a new public key 170 | * 171 | * PARAMETERS 172 | * signer [in] Signer_t* 173 | * 174 | * RETURNS 175 | * preallocated_return_result_ptr [out] XYResult_t* 176 | * 177 | * NOTES 178 | * //TODO: convert this routine from a simple 'getter' to a creator. 179 | **************************************************************************************** 180 | */ 181 | XYResult_t* newPublicKey(Signer_t* signer){} 182 | 183 | /** 184 | **************************************************************************************** 185 | * NAME 186 | * getPrivateKey 187 | * 188 | * DESCRIPTION 189 | * this routine returns the private key from the supplied signer object 190 | * 191 | * PARAMETERS 192 | * signer [in] Signer* 193 | * 194 | * RETURNS 195 | * preallocated_return_result_ptr [out] XYResult_t* 196 | * // the private key of the supplied 197 | * // XyoCryptoSigner. 198 | * // This private key can be used to restore 199 | * // the same keypair when creating a 200 | * // XyoCryptoSigner with XyoCryptoProvider. 201 | * ------------------------------ 202 | * preallocated_return_result_ptr.error = error code; 203 | * preallocated_return_result_ptr.result = (ByteArray_t*)&signer->privateKey; 204 | * 205 | * NOTES 206 | * keep in mind that this is just a fetch call (getter), not a generate new key call. 207 | **************************************************************************************** 208 | */ 209 | XYResult_t* getPrivateKey(Signer_t* signer){} 210 | 211 | /** 212 | **************************************************************************************** 213 | * NAME 214 | * newPrivateKey 215 | * 216 | * DESCRIPTION 217 | * this routine creates and returns a new private key 218 | * 219 | * PARAMETERS 220 | * none 221 | * 222 | * RETURNS 223 | * preallocated_return_result_ptr [out] XYResult_t* holds the error code 224 | * and a new private key ByteArray_t* 225 | * ------------------------ 226 | * preallocated_return_result_ptr.error = error code 227 | * preallocated_return_result_ptr.result = aNewPrivateKey; 228 | * NOTES 229 | * will return a malloc error if malloc fails. 230 | * wc_ = wolf crypto library routine or data type 231 | * support SECP256k1 232 | **************************************************************************************** 233 | */ 234 | /* 235 | int error = OK; 236 | uint8_t trng_bits[16]; 237 | WC_RNG dialogRng; 238 | int keysize; 239 | */ 240 | XYResult_t* newPrivateKey() {} 241 | 242 | /** 243 | **************************************************************************************** 244 | * NAME 245 | * newKeyPair 246 | * 247 | * DESCRIPTION 248 | * this routine returns a NEW pair of public and private keys 249 | * 250 | * PARAMETERS 251 | * none 252 | * 253 | * RETURNS 254 | * preallocated_return_result_ptr [out] XYResult_t* 255 | * 256 | * NOTES 257 | * will return a malloc error if malloc fails. 258 | * this routine is not a simple getter. it actually creates a new key pair. 259 | **************************************************************************************** 260 | */ 261 | XYResult_t* newKeyPair(){} 262 | 263 | /** 264 | **************************************************************************************** 265 | * NAME 266 | * sign 267 | * 268 | * DESCRIPTION 269 | * this routine performs a cryptographic signature of the data supplied, that was 270 | * created with the private key. 271 | * 272 | * PARAMETERS 273 | * dataToSign [in] ByteArray_t* // The data to cryptographically sign 274 | * // using the private key of the 275 | * // XyoCryptoSigner. 276 | * RETURNS 277 | * preallocated_return_result_ptr [out] XYResult_t* 278 | * 279 | * NOTES 280 | * 281 | **************************************************************************************** 282 | */ 283 | XYResult_t* sign(Signer_t* signer, ByteArray_t* dataToSign) {} 284 | 285 | /** 286 | **************************************************************************************** 287 | * NAME 288 | * verify 289 | * 290 | * DESCRIPTION 291 | * Cryptographically verify a signature given data, a signature, 292 | * and a public key that the XyoCryptoSigner supports. If the signature is valid, 293 | * this routine will return true, if it is invalid it will return false. 294 | * 295 | * PARAMETERS 296 | * Signer_t* [in] signer // The signer to use 297 | * signedData [in] ByteArray_t* // The data that was signed using the 298 | * // cryptographic function that the 299 | * // XyoCryptoSigner supports. 300 | * signature [in] XYObject_t* // The signature that was created using 301 | * // the cryptographic function that the 302 | * // XyoCryptoSigner supports. 303 | * publicKey [in] XYObject_t* // The public key of the party that signed 304 | * // the data with the the cryptographic 305 | * // function that the XyoCryptoSigner supports. 306 | * RETURNS 307 | * preallocated_return_result_ptr [out] XYResult_t* // the result is in 308 | * 309 | * NOTES 310 | * will return a malloc error if malloc fails. otherwise, a bool result (of the verify) 311 | * will be returned in preallocated_return_result_ptr->error 312 | **************************************************************************************** 313 | */ 314 | XYResult_t* verify(Signer_t* signer, 315 | ByteArray_t* signedData, 316 | XYObject_t* signature, 317 | XYObject_t* publicKey) {} 318 | 319 | /** 320 | **************************************************************************************** 321 | * NAME 322 | * encrypt 323 | * 324 | * DESCRIPTION 325 | * encrypts the supplied data. 326 | * 327 | * PARAMETERS 328 | * Signer_t* [in] signer // The signer to use 329 | * unEncrypedData [in] ByteArray_t* // The data that the XyoCryptoSigner will 330 | * // encrypt using its private key. 331 | * RETURNS 332 | * preallocated_return_result_ptr [out] XYResult_t* 333 | * // A promise with an XyoObject that contains 334 | * // the data encrypted. This data can be 335 | * // decrypted with the a XyoCryptoSigner 336 | * // that supports the same cryptographic function. 337 | * NOTES 338 | * will return a malloc error if malloc fails. 339 | **************************************************************************************** 340 | */ 341 | XYResult_t* xyencrypt(Signer_t* signer, ByteArray_t* unEncrypedData) {} 342 | 343 | /** 344 | **************************************************************************************** 345 | * NAME 346 | * decrypt 347 | * 348 | * DESCRIPTION 349 | * A ByteArray that is the decrypted data. 350 | * 351 | * PARAMETERS 352 | * Signer_t* [in] signer // The signer to use 353 | * encrypedData [in] ByteArray_t* // The data that was encrypted using a 354 | * // cryptographic function that the 355 | * // XyoCryptoSigner supports. 356 | * RETURNS 357 | * preallocated_return_result_ptr [out] XYResult_t* 358 | * // A promise with an XyoObject that contains 359 | * // the data encrypted. This data can be 360 | * // decrypted with the a XyoCryptoSigner 361 | * // that supports the same cryptographic function. 362 | * NOTES 363 | * will return a malloc error if malloc fails. 364 | **************************************************************************************** 365 | */ 366 | XYResult_t* xydecrypt(Signer_t* signer, ByteArray_t* encrypedData) {} 367 | 368 | /* 369 | ///////////////////// 370 | // type references // 371 | ///////////////////// 372 | 373 | struct ByteArray{ 374 | uint32_t size; 375 | char reserved[2]; 376 | char* payload; 377 | } 378 | 379 | struct XYResult{ 380 | int error; 381 | void* result; 382 | }; 383 | 384 | struct XYObject{ 385 | char id[2]; 386 | void* payload; 387 | char* (*GetXyobjectId)(XYObject_t*); // Fetch the above id object and return it. 388 | void* (*GetPayload)(XYObject_t*); // Fetch the above payload pointer object and return it. 389 | } ; 390 | 391 | struct RsaKey { 392 | mp_int n, e, d, p, q, dP, dQ, u; 393 | void* heap; // for user memory overrides 394 | byte* data; // temp buffer for async RSA 395 | int type; // public or private 396 | int state; 397 | word32 dataLen; 398 | 399 | struct Signer{ 400 | 401 | ByteArray_t publicKey; // Cryptographic Public Key 402 | ByteArray_t privateKey; // Cryptographic Private Key 403 | 404 | XYResult_t* (*getPublicKey)(Signer_t* signer); // Returns public key 405 | XYResult_t* (*sign)(Signer_t*, ByteArray_t*); // Returns a signed byte array 406 | 407 | / * 408 | * The method will take data and a cryptographic signature and a cryptographic public key 409 | * and determine if data was signed by the given public key correctly or if the signature 410 | * is malformed / invalid. Boolean return value. 411 | * / 412 | 413 | XYResult_t* (*verify)(Signer_t* signer, 414 | ByteArray_t* data, 415 | XYObject_t* signature, 416 | XYObject_t* publicKey); 417 | 418 | ByteArray_t* (*encrypt)(Signer_t*, ByteArray_t*); // Encrypt the data to the key of 419 | // this Signer object 420 | ByteArray_t* (*decrypt)(Signer_t*, ByteArray_t*); // Decrypt the data with priv key 421 | // of this Signer object. 422 | HashProvider_t* hashingProvider; 423 | }; 424 | 425 | struct CryptoCreator{ 426 | 427 | char id[2]; 428 | char* (*getId)(struct CryptoCreator*); // Fetch the above id 429 | // object and return it. 430 | Signer_t* (*newCryptoSignerInstance)(ByteArray_t* user_PrivateKey); // Generate a new Signer 431 | // object which includes 432 | // generating a new 433 | // keypair. 434 | }; 435 | struct XYResult{ 436 | int error; 437 | void* result; 438 | }; 439 | 440 | */ 441 | 442 | // end of file crypto.c 443 | -------------------------------------------------------------------------------- /src/WolfSSL/wc_port.h: -------------------------------------------------------------------------------- 1 | /* wc_port.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | /*! 23 | \file wolfssl/wolfcrypt/wc_port.h 24 | */ 25 | 26 | #ifndef WOLF_CRYPT_PORT_H 27 | #define WOLF_CRYPT_PORT_H 28 | 29 | /* 30 | * INCLUDES 31 | **************************************************************************************** 32 | */ 33 | 34 | #include "wc_settings.h" 35 | //#include "wc_visibility.h" 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /* Detect if compiler supports C99. "NO_WOLF_C99" can be defined in 42 | * user_settings.h to disable checking for C99 support. */ 43 | #if !defined(WOLF_C99) && defined(__STDC_VERSION__) && \ 44 | !defined(WOLFSSL_ARDUINO) && !defined(NO_WOLF_C99) 45 | #if __STDC_VERSION__ >= 199901L 46 | #define WOLF_C99 47 | #endif 48 | #endif 49 | 50 | #ifdef USE_WINDOWS_API 51 | #ifdef WOLFSSL_GAME_BUILD 52 | #include "system/xtl.h" 53 | #else 54 | #ifndef WIN32_LEAN_AND_MEAN 55 | #define WIN32_LEAN_AND_MEAN 56 | #endif 57 | #ifndef WOLFSSL_SGX 58 | #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN) 59 | /* On WinCE winsock2.h must be included before windows.h */ 60 | #include 61 | #endif 62 | #include 63 | #endif /* WOLFSSL_SGX */ 64 | #endif 65 | #elif defined(THREADX) 66 | #ifndef SINGLE_THREADED 67 | #ifdef NEED_THREADX_TYPES 68 | #include 69 | #endif 70 | #include 71 | #endif 72 | #elif defined(MICRIUM) 73 | /* do nothing, just don't pick Unix */ 74 | #elif defined(FREERTOS) || defined(FREERTOS_TCP) || defined(WOLFSSL_SAFERTOS) 75 | /* do nothing */ 76 | #elif defined(EBSNET) 77 | /* do nothing */ 78 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) 79 | /* do nothing */ 80 | #elif defined(FREESCALE_FREE_RTOS) 81 | #include "fsl_os_abstraction.h" 82 | #elif defined(WOLFSSL_uITRON4) 83 | #include "stddef.h" 84 | #include "kernel.h" 85 | #elif defined(WOLFSSL_uTKERNEL2) 86 | #include "tk/tkernel.h" 87 | #elif defined(WOLFSSL_CMSIS_RTOS) 88 | #include "cmsis_os.h" 89 | #elif defined(WOLFSSL_MDK_ARM) 90 | #if defined(WOLFSSL_MDK5) 91 | #include "cmsis_os.h" 92 | #else 93 | //#include 94 | #endif 95 | #elif defined(WOLFSSL_CMSIS_RTOS) 96 | #include "cmsis_os.h" 97 | #elif defined(WOLFSSL_TIRTOS) 98 | #include 99 | #include 100 | #elif defined(WOLFSSL_FROSTED) 101 | #include 102 | #elif defined(INTIME_RTOS) 103 | #include 104 | #include 105 | #else 106 | #ifndef SINGLE_THREADED 107 | //#define WOLFSSL_PTHREADS 108 | //#include 109 | #define SINGLE_THREADED 110 | #endif 111 | #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS) 112 | #include /* for close of BIO */ 113 | #endif 114 | #endif 115 | 116 | /* For FIPS keep the function names the same */ 117 | #ifdef HAVE_FIPS 118 | #define wc_InitMutex InitMutex 119 | #define wc_FreeMutex FreeMutex 120 | #define wc_LockMutex LockMutex 121 | #define wc_UnLockMutex UnLockMutex 122 | #endif /* HAVE_FIPS */ 123 | 124 | #ifdef SINGLE_THREADED 125 | typedef int wolfSSL_Mutex; 126 | #else /* MULTI_THREADED */ 127 | /* FREERTOS comes first to enable use of FreeRTOS Windows simulator only */ 128 | #if defined(FREERTOS) 129 | typedef xSemaphoreHandle wolfSSL_Mutex; 130 | #elif defined(FREERTOS_TCP) 131 | #include "FreeRTOS.h" 132 | #include "semphr.h" 133 | typedef SemaphoreHandle_t wolfSSL_Mutex; 134 | #elif defined(WOLFSSL_SAFERTOS) 135 | typedef struct wolfSSL_Mutex { 136 | signed char mutexBuffer[portQUEUE_OVERHEAD_BYTES]; 137 | xSemaphoreHandle mutex; 138 | } wolfSSL_Mutex; 139 | #elif defined(USE_WINDOWS_API) 140 | typedef CRITICAL_SECTION wolfSSL_Mutex; 141 | #elif defined(WOLFSSL_PTHREADS) 142 | typedef pthread_mutex_t wolfSSL_Mutex; 143 | #elif defined(THREADX) 144 | typedef TX_MUTEX wolfSSL_Mutex; 145 | #elif defined(MICRIUM) 146 | typedef OS_MUTEX wolfSSL_Mutex; 147 | #elif defined(EBSNET) 148 | typedef RTP_MUTEX wolfSSL_Mutex; 149 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) 150 | typedef MUTEX_STRUCT wolfSSL_Mutex; 151 | #elif defined(FREESCALE_FREE_RTOS) 152 | typedef mutex_t wolfSSL_Mutex; 153 | #elif defined(WOLFSSL_uITRON4) 154 | typedef struct wolfSSL_Mutex { 155 | T_CSEM sem ; 156 | ID id ; 157 | } wolfSSL_Mutex; 158 | #elif defined(WOLFSSL_uTKERNEL2) 159 | typedef struct wolfSSL_Mutex { 160 | T_CSEM sem ; 161 | ID id ; 162 | } wolfSSL_Mutex; 163 | #elif defined(WOLFSSL_MDK_ARM) 164 | #if defined(WOLFSSL_CMSIS_RTOS) 165 | typedef osMutexId wolfSSL_Mutex; 166 | #else 167 | typedef OS_MUT wolfSSL_Mutex; 168 | #endif 169 | #elif defined(WOLFSSL_CMSIS_RTOS) 170 | typedef osMutexId wolfSSL_Mutex; 171 | #elif defined(WOLFSSL_TIRTOS) 172 | typedef ti_sysbios_knl_Semaphore_Handle wolfSSL_Mutex; 173 | #elif defined(WOLFSSL_FROSTED) 174 | typedef mutex_t * wolfSSL_Mutex; 175 | #elif defined(INTIME_RTOS) 176 | typedef RTHANDLE wolfSSL_Mutex; 177 | #else 178 | #error Need a mutex type in multithreaded mode 179 | #endif /* USE_WINDOWS_API */ 180 | #endif /* SINGLE_THREADED */ 181 | 182 | /* Enable crypt HW mutex for Freescale MMCAU or PIC32MZ */ 183 | #if defined(FREESCALE_MMCAU) || defined(WOLFSSL_MICROCHIP_PIC32MZ) 184 | #ifndef WOLFSSL_CRYPT_HW_MUTEX 185 | #define WOLFSSL_CRYPT_HW_MUTEX 1 186 | #endif 187 | #endif /* FREESCALE_MMCAU */ 188 | 189 | #ifndef WOLFSSL_CRYPT_HW_MUTEX 190 | #define WOLFSSL_CRYPT_HW_MUTEX 0 191 | #endif 192 | 193 | #if WOLFSSL_CRYPT_HW_MUTEX 194 | /* wolfSSL_CryptHwMutexInit is called on first wolfSSL_CryptHwMutexLock, 195 | however it's recommended to call this directly on Hw init to avoid possible 196 | race condition where two calls to wolfSSL_CryptHwMutexLock are made at 197 | the same time. */ 198 | int wolfSSL_CryptHwMutexInit(void); 199 | int wolfSSL_CryptHwMutexLock(void); 200 | int wolfSSL_CryptHwMutexUnLock(void); 201 | #else 202 | /* Define stubs, since HW mutex is disabled */ 203 | #define wolfSSL_CryptHwMutexInit() 0 /* Success */ 204 | #define wolfSSL_CryptHwMutexLock() 0 /* Success */ 205 | #define wolfSSL_CryptHwMutexUnLock() (void)0 /* Success */ 206 | #endif /* WOLFSSL_CRYPT_HW_MUTEX */ 207 | 208 | /* Mutex functions */ 209 | WOLFSSL_API int wc_InitMutex(wolfSSL_Mutex*); 210 | WOLFSSL_API wolfSSL_Mutex* wc_InitAndAllocMutex(void); 211 | WOLFSSL_API int wc_FreeMutex(wolfSSL_Mutex*); 212 | WOLFSSL_API int wc_LockMutex(wolfSSL_Mutex*); 213 | WOLFSSL_API int wc_UnLockMutex(wolfSSL_Mutex*); 214 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) 215 | /* dynamiclly set which mutex to use. unlock / lock is controlled by flag */ 216 | typedef void (mutex_cb)(int flag, int type, const char* file, int line); 217 | 218 | WOLFSSL_API int wc_LockMutex_ex(int flag, int type, const char* file, int line); 219 | WOLFSSL_API int wc_SetMutexCb(mutex_cb* cb); 220 | #endif 221 | 222 | /* main crypto initialization function */ 223 | WOLFSSL_API int wolfCrypt_Init(void); 224 | WOLFSSL_API int wolfCrypt_Cleanup(void); 225 | 226 | /* filesystem abstraction layer, used by ssl.c */ 227 | #ifndef NO_FILESYSTEM 228 | 229 | #if defined(EBSNET) 230 | #include "vfapi.h" 231 | #include "vfile.h" 232 | 233 | #define XFILE int 234 | #define XFOPEN(NAME, MODE) vf_open((const char *)NAME, VO_RDONLY, 0); 235 | #define XFSEEK vf_lseek 236 | #define XFTELL vf_tell 237 | #define XREWIND vf_rewind 238 | #define XFREAD(BUF, SZ, AMT, FD) vf_read(FD, BUF, SZ*AMT) 239 | #define XFWRITE(BUF, SZ, AMT, FD) vf_write(FD, BUF, SZ*AMT) 240 | #define XFCLOSE vf_close 241 | #define XSEEK_END VSEEK_END 242 | #define XBADFILE -1 243 | #define XFGETS(b,s,f) -2 /* Not ported yet */ 244 | #elif defined(LSR_FS) 245 | #include 246 | #define XFILE struct fs_file* 247 | #define XFOPEN(NAME, MODE) fs_open((char*)NAME); 248 | #define XFSEEK(F, O, W) (void)F 249 | #define XFTELL(F) (F)->len 250 | #define XREWIND(F) (void)F 251 | #define XFREAD(BUF, SZ, AMT, F) fs_read(F, (char*)BUF, SZ*AMT) 252 | #define XFWRITE(BUF, SZ, AMT, F) fs_write(F, (char*)BUF, SZ*AMT) 253 | #define XFCLOSE fs_close 254 | #define XSEEK_END 0 255 | #define XBADFILE NULL 256 | #define XFGETS(b,s,f) -2 /* Not ported yet */ 257 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) 258 | #define XFILE MQX_FILE_PTR 259 | #define XFOPEN fopen 260 | #define XFSEEK fseek 261 | #define XFTELL ftell 262 | #define XREWIND(F) fseek(F, 0, IO_SEEK_SET) 263 | #define XFREAD fread 264 | #define XFWRITE fwrite 265 | #define XFCLOSE fclose 266 | #define XSEEK_END IO_SEEK_END 267 | #define XBADFILE NULL 268 | #define XFGETS fgets 269 | #elif defined(MICRIUM) 270 | #include 271 | #define XFILE FS_FILE* 272 | #define XFOPEN fs_fopen 273 | #define XFSEEK fs_fseek 274 | #define XFTELL fs_ftell 275 | #define XREWIND fs_rewind 276 | #define XFREAD fs_fread 277 | #define XFWRITE fs_fwrite 278 | #define XFCLOSE fs_fclose 279 | #define XSEEK_END FS_SEEK_END 280 | #define XBADFILE NULL 281 | #define XFGETS(b,s,f) -2 /* Not ported yet */ 282 | #else 283 | /* stdio, default case */ 284 | #include 285 | #define XFILE FILE* 286 | #if defined(WOLFSSL_MDK_ARM) 287 | extern FILE * wolfSSL_fopen(const char *name, const char *mode) ; 288 | #define XFOPEN wolfSSL_fopen 289 | #else 290 | #define XFOPEN fopen 291 | #endif 292 | #define XFSEEK fseek 293 | #define XFTELL ftell 294 | #define XREWIND rewind 295 | #define XFREAD fread 296 | #define XFWRITE fwrite 297 | #define XFCLOSE fclose 298 | #define XSEEK_END SEEK_END 299 | #define XBADFILE NULL 300 | #define XFGETS fgets 301 | 302 | #if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR)\ 303 | && !defined(WOLFSSL_NUCLEUS) 304 | //#include 305 | //#include 306 | //#include 307 | #endif 308 | #endif 309 | 310 | #ifndef MAX_FILENAME_SZ 311 | #define MAX_FILENAME_SZ 256 /* max file name length */ 312 | #endif 313 | #ifndef MAX_PATH 314 | #define MAX_PATH 256 315 | #endif 316 | 317 | #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_NUCLEUS) 318 | typedef struct ReadDirCtx { 319 | #ifdef USE_WINDOWS_API 320 | WIN32_FIND_DATAA FindFileData; 321 | HANDLE hFind; 322 | #else 323 | struct dirent* entry; 324 | //DIR* dir; 325 | //struct stat s; 326 | #endif 327 | char name[MAX_FILENAME_SZ]; 328 | } ReadDirCtx; 329 | 330 | WOLFSSL_API int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name); 331 | WOLFSSL_API int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name); 332 | WOLFSSL_API void wc_ReadDirClose(ReadDirCtx* ctx); 333 | #endif /* !NO_WOLFSSL_DIR */ 334 | 335 | #endif /* !NO_FILESYSTEM */ 336 | 337 | /* Windows API defines its own min() macro. */ 338 | #if defined(USE_WINDOWS_API) 339 | #if defined(min) || defined(WOLFSSL_MYSQL_COMPATIBLE) 340 | #define WOLFSSL_HAVE_MIN 341 | #endif /* min */ 342 | #if defined(max) || defined(WOLFSSL_MYSQL_COMPATIBLE) 343 | #define WOLFSSL_HAVE_MAX 344 | #endif /* max */ 345 | #endif /* USE_WINDOWS_API */ 346 | 347 | /* Time functions */ 348 | #ifndef NO_ASN_TIME 349 | #if defined(USER_TIME) 350 | /* Use our gmtime and time_t/struct tm types. 351 | Only needs seconds since EPOCH using XTIME function. 352 | time_t XTIME(time_t * timer) {} 353 | */ 354 | #define WOLFSSL_GMTIME 355 | #define USE_WOLF_TM 356 | #define USE_WOLF_TIME_T 357 | 358 | #elif defined(TIME_OVERRIDES) 359 | /* Override XTIME() and XGMTIME() functionality. 360 | Requires user to provide these functions: 361 | time_t XTIME(time_t * timer) {} 362 | struct tm* XGMTIME(const time_t* timer, struct tm* tmp) {} 363 | */ 364 | #ifndef HAVE_TIME_T_TYPE 365 | #define USE_WOLF_TIME_T 366 | #endif 367 | #ifndef HAVE_TM_TYPE 368 | #define USE_WOLF_TM 369 | #endif 370 | #define NEED_TMP_TIME 371 | 372 | #elif defined(HAVE_RTP_SYS) 373 | #include "os.h" /* dc_rtc_api needs */ 374 | #include "dc_rtc_api.h" /* to get current time */ 375 | 376 | /* uses parital structures */ 377 | #define XTIME(tl) (0) 378 | #define XGMTIME(c, t) rtpsys_gmtime((c)) 379 | 380 | #elif defined(MICRIUM) 381 | #include 382 | #include 383 | #define XTIME(t1) micrium_time((t1)) 384 | #define WOLFSSL_GMTIME 385 | 386 | #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP) 387 | #include 388 | #define XTIME(t1) pic32_time((t1)) 389 | #define XGMTIME(c, t) gmtime((c)) 390 | 391 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) 392 | #define XTIME(t1) mqx_time((t1)) 393 | #define HAVE_GMTIME_R 394 | 395 | #elif defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) 396 | #include 397 | #ifndef XTIME 398 | /*extern time_t ksdk_time(time_t* timer);*/ 399 | #define XTIME(t1) ksdk_time((t1)) 400 | #endif 401 | #define XGMTIME(c, t) gmtime((c)) 402 | 403 | #elif defined(WOLFSSL_ATMEL) 404 | #define XTIME(t1) atmel_get_curr_time_and_date((t1)) 405 | #define WOLFSSL_GMTIME 406 | #define USE_WOLF_TM 407 | #define USE_WOLF_TIME_T 408 | 409 | #elif defined(IDIRECT_DEV_TIME) 410 | /*Gets the timestamp from cloak software owned by VT iDirect 411 | in place of time() from */ 412 | #include 413 | #define XTIME(t1) idirect_time((t1)) 414 | #define XGMTIME(c, t) gmtime((c)) 415 | 416 | #elif defined(_WIN32_WCE) 417 | #include 418 | #define XTIME(t1) windows_time((t1)) 419 | #define WOLFSSL_GMTIME 420 | 421 | #else 422 | /* default */ 423 | /* uses complete facility */ 424 | #include 425 | #if defined(HAVE_SYS_TIME_H) || defined(WOLF_C99) 426 | //#include 427 | #endif 428 | 429 | /* PowerPC time_t is int */ 430 | #ifdef __PPC__ 431 | #define TIME_T_NOT_LONG 432 | #endif 433 | #endif 434 | 435 | 436 | /* Map default time functions */ 437 | #if !defined(XTIME) && !defined(TIME_OVERRIDES) && !defined(USER_TIME) 438 | #define XTIME(tl) time((tl)) 439 | #endif 440 | #if !defined(XGMTIME) && !defined(TIME_OVERRIDES) 441 | #if defined(WOLFSSL_GMTIME) || !defined(HAVE_GMTIME_R) || defined(WOLF_C99) 442 | #define XGMTIME(c, t) gmtime((c)) 443 | #else 444 | #define XGMTIME(c, t) gmtime_r((c), (t)) 445 | #define NEED_TMP_TIME 446 | #endif 447 | #endif 448 | #if !defined(XVALIDATE_DATE) && !defined(HAVE_VALIDATE_DATE) 449 | #define USE_WOLF_VALIDDATE 450 | #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) 451 | #endif 452 | 453 | /* wolf struct tm and time_t */ 454 | #if defined(USE_WOLF_TM) 455 | struct tm { 456 | int tm_sec; /* seconds after the minute [0-60] */ 457 | int tm_min; /* minutes after the hour [0-59] */ 458 | int tm_hour; /* hours since midnight [0-23] */ 459 | int tm_mday; /* day of the month [1-31] */ 460 | int tm_mon; /* months since January [0-11] */ 461 | int tm_year; /* years since 1900 */ 462 | int tm_wday; /* days since Sunday [0-6] */ 463 | int tm_yday; /* days since January 1 [0-365] */ 464 | int tm_isdst; /* Daylight Savings Time flag */ 465 | long tm_gmtoff; /* offset from CUT in seconds */ 466 | char *tm_zone; /* timezone abbreviation */ 467 | }; 468 | #endif /* USE_WOLF_TM */ 469 | #if defined(USE_WOLF_TIME_T) 470 | typedef long time_t; 471 | #endif 472 | #if defined(USE_WOLF_SUSECONDS_T) 473 | typedef long suseconds_t; 474 | #endif 475 | #if defined(USE_WOLF_TIMEVAL_T) 476 | struct timeval 477 | { 478 | time_t tv_sec; 479 | suseconds_t tv_usec; 480 | }; 481 | #endif 482 | 483 | /* forward declarations */ 484 | #if defined(USER_TIME) 485 | struct tm* gmtime(const time_t* timer); 486 | extern time_t XTIME(time_t * timer); 487 | 488 | #ifdef STACK_TRAP 489 | /* for stack trap tracking, don't call os gmtime on OS X/linux, 490 | uses a lot of stack spce */ 491 | extern time_t time(time_t * timer); 492 | #define XTIME(tl) time((tl)) 493 | #endif /* STACK_TRAP */ 494 | 495 | #elif defined(TIME_OVERRIDES) 496 | extern time_t XTIME(time_t * timer); 497 | extern struct tm* XGMTIME(const time_t* timer, struct tm* tmp); 498 | #elif defined(WOLFSSL_GMTIME) 499 | struct tm* gmtime(const time_t* timer); 500 | #endif 501 | #endif /* NO_ASN_TIME */ 502 | 503 | #ifndef WOLFSSL_LEANPSK 504 | char* mystrnstr(const char* s1, const char* s2, unsigned int n); 505 | #endif 506 | 507 | #ifndef FILE_BUFFER_SIZE 508 | #define FILE_BUFFER_SIZE 1024 /* default static file buffer size for input, 509 | will use dynamic buffer if not big enough */ 510 | #endif 511 | 512 | 513 | #ifdef __cplusplus 514 | } /* extern "C" */ 515 | #endif 516 | 517 | #endif /* WOLF_CRYPT_PORT_H */ 518 | 519 | --------------------------------------------------------------------------------