├── .gitignore ├── LICENSE ├── README.md ├── blockchain-ios.podspec ├── blockchain ├── blockchain.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── blockchain │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ ├── AccentColor.colorset │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ ├── SceneDelegate.h │ ├── SceneDelegate.m │ ├── ViewController.h │ ├── ViewController.m │ └── main.m └── sources ├── Ethereum ├── ETHBlockChain.h └── ETHBlockChain.m ├── bips ├── bip39.c ├── bip39.h ├── wordlist.c └── wordlist.h ├── core ├── BaseChain.h └── BaseChain.m └── crypto ├── aes.c ├── aes.h ├── base58.c ├── base58.h ├── rmd160.c ├── rmd160.h ├── sha256.c ├── sha256.h ├── uECC.c └── uECC.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 77。 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BlockChain 2 | base block chain 3 | -------------------------------------------------------------------------------- /blockchain-ios.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod lib lint blockchain-ios.podspec' to ensure this is a 3 | # valid spec before submitting. 4 | # 5 | # Any lines starting with a # are optional, but their use is encouraged 6 | # To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html 7 | # 8 | 9 | Pod::Spec.new do |s| 10 | s.name = 'blockchain-ios' 11 | s.version = '0.0.1' 12 | s.summary = 'A short description of blockchain-ios.' 13 | 14 | # This description is used to generate tags and improve search results. 15 | # * Think: What does it do? Why did you write it? What is the focus? 16 | # * Try to keep it short, snappy and to the point. 17 | # * Write the description between the DESC delimiters below. 18 | # * Finally, don't worry about the indent, CocoaPods strips it! 19 | 20 | s.homepage = 'https://github.com/yangKJ/MnemonicDemo' 21 | s.license = { :type => 'MIT', :file => 'LICENSE' } 22 | s.author = { 'yangkejun' => 'ykj310@126.com' } 23 | s.source = { :git => 'git@github.com:yangKJ/MnemonicDemo.git', :tag => s.version.to_s } 24 | s.platform = :ios, '10.0' 25 | s.module_name = 'blockchain' 26 | 27 | s.swift_version = '5.0' 28 | s.ios.deployment_target = '10.0' 29 | s.requires_arc = true 30 | s.static_framework = true 31 | 32 | s.source_files = 'Sources/**/*' 33 | 34 | end 35 | -------------------------------------------------------------------------------- /blockchain/blockchain.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 04B04DC627A652D600D1D416 /* aes.c in Sources */ = {isa = PBXBuildFile; fileRef = 04B04DB127A652D600D1D416 /* aes.c */; }; 11 | 04B04DC727A652D600D1D416 /* uECC.c in Sources */ = {isa = PBXBuildFile; fileRef = 04B04DB627A652D600D1D416 /* uECC.c */; }; 12 | 04B04DC827A652D600D1D416 /* sha256.c in Sources */ = {isa = PBXBuildFile; fileRef = 04B04DB827A652D600D1D416 /* sha256.c */; }; 13 | 04B04DC927A652D600D1D416 /* base58.c in Sources */ = {isa = PBXBuildFile; fileRef = 04B04DB927A652D600D1D416 /* base58.c */; }; 14 | 04B04DCA27A652D600D1D416 /* rmd160.c in Sources */ = {isa = PBXBuildFile; fileRef = 04B04DBA27A652D600D1D416 /* rmd160.c */; }; 15 | 04B04DCB27A652D600D1D416 /* BaseChain.m in Sources */ = {isa = PBXBuildFile; fileRef = 04B04DBD27A652D600D1D416 /* BaseChain.m */; }; 16 | 04B04DCC27A652D600D1D416 /* bip39.c in Sources */ = {isa = PBXBuildFile; fileRef = 04B04DBF27A652D600D1D416 /* bip39.c */; }; 17 | 04B04DCD27A652D600D1D416 /* wordlist.c in Sources */ = {isa = PBXBuildFile; fileRef = 04B04DC227A652D600D1D416 /* wordlist.c */; }; 18 | 04B04DCE27A652D600D1D416 /* ETHBlockChain.m in Sources */ = {isa = PBXBuildFile; fileRef = 04B04DC427A652D600D1D416 /* ETHBlockChain.m */; }; 19 | 68B4F9AE27062D52003F323F /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 68B4F9AD27062D52003F323F /* AppDelegate.m */; }; 20 | 68B4F9B127062D52003F323F /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 68B4F9B027062D52003F323F /* SceneDelegate.m */; }; 21 | 68B4F9B427062D52003F323F /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 68B4F9B327062D52003F323F /* ViewController.m */; }; 22 | 68B4F9B727062D52003F323F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 68B4F9B527062D52003F323F /* Main.storyboard */; }; 23 | 68B4F9B927062D53003F323F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 68B4F9B827062D53003F323F /* Assets.xcassets */; }; 24 | 68B4F9BC27062D53003F323F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 68B4F9BA27062D53003F323F /* LaunchScreen.storyboard */; }; 25 | 68B4F9BF27062D53003F323F /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 68B4F9BE27062D53003F323F /* main.m */; }; 26 | /* End PBXBuildFile section */ 27 | 28 | /* Begin PBXFileReference section */ 29 | 04B04DB127A652D600D1D416 /* aes.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = aes.c; sourceTree = ""; }; 30 | 04B04DB227A652D600D1D416 /* uECC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = uECC.h; sourceTree = ""; }; 31 | 04B04DB327A652D600D1D416 /* base58.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = base58.h; sourceTree = ""; }; 32 | 04B04DB427A652D600D1D416 /* sha256.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sha256.h; sourceTree = ""; }; 33 | 04B04DB527A652D600D1D416 /* rmd160.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rmd160.h; sourceTree = ""; }; 34 | 04B04DB627A652D600D1D416 /* uECC.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = uECC.c; sourceTree = ""; }; 35 | 04B04DB727A652D600D1D416 /* aes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes.h; sourceTree = ""; }; 36 | 04B04DB827A652D600D1D416 /* sha256.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha256.c; sourceTree = ""; }; 37 | 04B04DB927A652D600D1D416 /* base58.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = base58.c; sourceTree = ""; }; 38 | 04B04DBA27A652D600D1D416 /* rmd160.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = rmd160.c; sourceTree = ""; }; 39 | 04B04DBC27A652D600D1D416 /* BaseChain.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BaseChain.h; sourceTree = ""; }; 40 | 04B04DBD27A652D600D1D416 /* BaseChain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BaseChain.m; sourceTree = ""; }; 41 | 04B04DBF27A652D600D1D416 /* bip39.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bip39.c; sourceTree = ""; }; 42 | 04B04DC027A652D600D1D416 /* wordlist.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wordlist.h; sourceTree = ""; }; 43 | 04B04DC127A652D600D1D416 /* bip39.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bip39.h; sourceTree = ""; }; 44 | 04B04DC227A652D600D1D416 /* wordlist.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = wordlist.c; sourceTree = ""; }; 45 | 04B04DC427A652D600D1D416 /* ETHBlockChain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ETHBlockChain.m; sourceTree = ""; }; 46 | 04B04DC527A652D600D1D416 /* ETHBlockChain.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ETHBlockChain.h; sourceTree = ""; }; 47 | 68B4F9A927062D52003F323F /* blockchain.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = blockchain.app; sourceTree = BUILT_PRODUCTS_DIR; }; 48 | 68B4F9AC27062D52003F323F /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 49 | 68B4F9AD27062D52003F323F /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 50 | 68B4F9AF27062D52003F323F /* SceneDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SceneDelegate.h; sourceTree = ""; }; 51 | 68B4F9B027062D52003F323F /* SceneDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SceneDelegate.m; sourceTree = ""; }; 52 | 68B4F9B227062D52003F323F /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 53 | 68B4F9B327062D52003F323F /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 54 | 68B4F9B627062D52003F323F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 55 | 68B4F9B827062D53003F323F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 56 | 68B4F9BB27062D53003F323F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 57 | 68B4F9BD27062D53003F323F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 58 | 68B4F9BE27062D53003F323F /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 59 | /* End PBXFileReference section */ 60 | 61 | /* Begin PBXFrameworksBuildPhase section */ 62 | 68B4F9A627062D52003F323F /* Frameworks */ = { 63 | isa = PBXFrameworksBuildPhase; 64 | buildActionMask = 2147483647; 65 | files = ( 66 | ); 67 | runOnlyForDeploymentPostprocessing = 0; 68 | }; 69 | /* End PBXFrameworksBuildPhase section */ 70 | 71 | /* Begin PBXGroup section */ 72 | 04B04DAF27A652D600D1D416 /* sources */ = { 73 | isa = PBXGroup; 74 | children = ( 75 | 04B04DBE27A652D600D1D416 /* bips */, 76 | 04B04DBB27A652D600D1D416 /* core */, 77 | 04B04DB027A652D600D1D416 /* crypto */, 78 | 04B04DC327A652D600D1D416 /* Ethereum */, 79 | ); 80 | name = sources; 81 | path = ../../sources; 82 | sourceTree = ""; 83 | }; 84 | 04B04DB027A652D600D1D416 /* crypto */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | 04B04DB127A652D600D1D416 /* aes.c */, 88 | 04B04DB727A652D600D1D416 /* aes.h */, 89 | 04B04DB927A652D600D1D416 /* base58.c */, 90 | 04B04DB327A652D600D1D416 /* base58.h */, 91 | 04B04DBA27A652D600D1D416 /* rmd160.c */, 92 | 04B04DB527A652D600D1D416 /* rmd160.h */, 93 | 04B04DB827A652D600D1D416 /* sha256.c */, 94 | 04B04DB427A652D600D1D416 /* sha256.h */, 95 | 04B04DB627A652D600D1D416 /* uECC.c */, 96 | 04B04DB227A652D600D1D416 /* uECC.h */, 97 | ); 98 | path = crypto; 99 | sourceTree = ""; 100 | }; 101 | 04B04DBB27A652D600D1D416 /* core */ = { 102 | isa = PBXGroup; 103 | children = ( 104 | 04B04DBC27A652D600D1D416 /* BaseChain.h */, 105 | 04B04DBD27A652D600D1D416 /* BaseChain.m */, 106 | ); 107 | path = core; 108 | sourceTree = ""; 109 | }; 110 | 04B04DBE27A652D600D1D416 /* bips */ = { 111 | isa = PBXGroup; 112 | children = ( 113 | 04B04DBF27A652D600D1D416 /* bip39.c */, 114 | 04B04DC127A652D600D1D416 /* bip39.h */, 115 | 04B04DC227A652D600D1D416 /* wordlist.c */, 116 | 04B04DC027A652D600D1D416 /* wordlist.h */, 117 | ); 118 | path = bips; 119 | sourceTree = ""; 120 | }; 121 | 04B04DC327A652D600D1D416 /* Ethereum */ = { 122 | isa = PBXGroup; 123 | children = ( 124 | 04B04DC527A652D600D1D416 /* ETHBlockChain.h */, 125 | 04B04DC427A652D600D1D416 /* ETHBlockChain.m */, 126 | ); 127 | path = Ethereum; 128 | sourceTree = ""; 129 | }; 130 | 68B4F9A027062D51003F323F = { 131 | isa = PBXGroup; 132 | children = ( 133 | 68B4F9AB27062D52003F323F /* blockchain */, 134 | 68B4F9AA27062D52003F323F /* Products */, 135 | ); 136 | sourceTree = ""; 137 | }; 138 | 68B4F9AA27062D52003F323F /* Products */ = { 139 | isa = PBXGroup; 140 | children = ( 141 | 68B4F9A927062D52003F323F /* blockchain.app */, 142 | ); 143 | name = Products; 144 | sourceTree = ""; 145 | }; 146 | 68B4F9AB27062D52003F323F /* blockchain */ = { 147 | isa = PBXGroup; 148 | children = ( 149 | 04B04DAF27A652D600D1D416 /* sources */, 150 | 68B4F9AC27062D52003F323F /* AppDelegate.h */, 151 | 68B4F9AD27062D52003F323F /* AppDelegate.m */, 152 | 68B4F9AF27062D52003F323F /* SceneDelegate.h */, 153 | 68B4F9B027062D52003F323F /* SceneDelegate.m */, 154 | 68B4F9B227062D52003F323F /* ViewController.h */, 155 | 68B4F9B327062D52003F323F /* ViewController.m */, 156 | 68B4F9B527062D52003F323F /* Main.storyboard */, 157 | 68B4F9B827062D53003F323F /* Assets.xcassets */, 158 | 68B4F9BA27062D53003F323F /* LaunchScreen.storyboard */, 159 | 68B4F9BD27062D53003F323F /* Info.plist */, 160 | 68B4F9BE27062D53003F323F /* main.m */, 161 | ); 162 | path = blockchain; 163 | sourceTree = ""; 164 | }; 165 | /* End PBXGroup section */ 166 | 167 | /* Begin PBXNativeTarget section */ 168 | 68B4F9A827062D52003F323F /* blockchain */ = { 169 | isa = PBXNativeTarget; 170 | buildConfigurationList = 68B4F9C227062D53003F323F /* Build configuration list for PBXNativeTarget "blockchain" */; 171 | buildPhases = ( 172 | 68B4F9A527062D52003F323F /* Sources */, 173 | 68B4F9A627062D52003F323F /* Frameworks */, 174 | 68B4F9A727062D52003F323F /* Resources */, 175 | ); 176 | buildRules = ( 177 | ); 178 | dependencies = ( 179 | ); 180 | name = blockchain; 181 | productName = blockchain; 182 | productReference = 68B4F9A927062D52003F323F /* blockchain.app */; 183 | productType = "com.apple.product-type.application"; 184 | }; 185 | /* End PBXNativeTarget section */ 186 | 187 | /* Begin PBXProject section */ 188 | 68B4F9A127062D51003F323F /* Project object */ = { 189 | isa = PBXProject; 190 | attributes = { 191 | BuildIndependentTargetsInParallel = 1; 192 | LastUpgradeCheck = 1300; 193 | TargetAttributes = { 194 | 68B4F9A827062D52003F323F = { 195 | CreatedOnToolsVersion = 13.0; 196 | }; 197 | }; 198 | }; 199 | buildConfigurationList = 68B4F9A427062D51003F323F /* Build configuration list for PBXProject "blockchain" */; 200 | compatibilityVersion = "Xcode 13.0"; 201 | developmentRegion = en; 202 | hasScannedForEncodings = 0; 203 | knownRegions = ( 204 | en, 205 | Base, 206 | ); 207 | mainGroup = 68B4F9A027062D51003F323F; 208 | productRefGroup = 68B4F9AA27062D52003F323F /* Products */; 209 | projectDirPath = ""; 210 | projectRoot = ""; 211 | targets = ( 212 | 68B4F9A827062D52003F323F /* blockchain */, 213 | ); 214 | }; 215 | /* End PBXProject section */ 216 | 217 | /* Begin PBXResourcesBuildPhase section */ 218 | 68B4F9A727062D52003F323F /* Resources */ = { 219 | isa = PBXResourcesBuildPhase; 220 | buildActionMask = 2147483647; 221 | files = ( 222 | 68B4F9BC27062D53003F323F /* LaunchScreen.storyboard in Resources */, 223 | 68B4F9B927062D53003F323F /* Assets.xcassets in Resources */, 224 | 68B4F9B727062D52003F323F /* Main.storyboard in Resources */, 225 | ); 226 | runOnlyForDeploymentPostprocessing = 0; 227 | }; 228 | /* End PBXResourcesBuildPhase section */ 229 | 230 | /* Begin PBXSourcesBuildPhase section */ 231 | 68B4F9A527062D52003F323F /* Sources */ = { 232 | isa = PBXSourcesBuildPhase; 233 | buildActionMask = 2147483647; 234 | files = ( 235 | 04B04DCE27A652D600D1D416 /* ETHBlockChain.m in Sources */, 236 | 68B4F9B427062D52003F323F /* ViewController.m in Sources */, 237 | 04B04DC927A652D600D1D416 /* base58.c in Sources */, 238 | 04B04DC727A652D600D1D416 /* uECC.c in Sources */, 239 | 68B4F9AE27062D52003F323F /* AppDelegate.m in Sources */, 240 | 04B04DC827A652D600D1D416 /* sha256.c in Sources */, 241 | 68B4F9BF27062D53003F323F /* main.m in Sources */, 242 | 04B04DCB27A652D600D1D416 /* BaseChain.m in Sources */, 243 | 04B04DC627A652D600D1D416 /* aes.c in Sources */, 244 | 68B4F9B127062D52003F323F /* SceneDelegate.m in Sources */, 245 | 04B04DCA27A652D600D1D416 /* rmd160.c in Sources */, 246 | 04B04DCC27A652D600D1D416 /* bip39.c in Sources */, 247 | 04B04DCD27A652D600D1D416 /* wordlist.c in Sources */, 248 | ); 249 | runOnlyForDeploymentPostprocessing = 0; 250 | }; 251 | /* End PBXSourcesBuildPhase section */ 252 | 253 | /* Begin PBXVariantGroup section */ 254 | 68B4F9B527062D52003F323F /* Main.storyboard */ = { 255 | isa = PBXVariantGroup; 256 | children = ( 257 | 68B4F9B627062D52003F323F /* Base */, 258 | ); 259 | name = Main.storyboard; 260 | sourceTree = ""; 261 | }; 262 | 68B4F9BA27062D53003F323F /* LaunchScreen.storyboard */ = { 263 | isa = PBXVariantGroup; 264 | children = ( 265 | 68B4F9BB27062D53003F323F /* Base */, 266 | ); 267 | name = LaunchScreen.storyboard; 268 | sourceTree = ""; 269 | }; 270 | /* End PBXVariantGroup section */ 271 | 272 | /* Begin XCBuildConfiguration section */ 273 | 68B4F9C027062D53003F323F /* Debug */ = { 274 | isa = XCBuildConfiguration; 275 | buildSettings = { 276 | ALWAYS_SEARCH_USER_PATHS = NO; 277 | CLANG_ANALYZER_NONNULL = YES; 278 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 279 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 280 | CLANG_CXX_LIBRARY = "libc++"; 281 | CLANG_ENABLE_MODULES = YES; 282 | CLANG_ENABLE_OBJC_ARC = YES; 283 | CLANG_ENABLE_OBJC_WEAK = YES; 284 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 285 | CLANG_WARN_BOOL_CONVERSION = YES; 286 | CLANG_WARN_COMMA = YES; 287 | CLANG_WARN_CONSTANT_CONVERSION = YES; 288 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 289 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 290 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 291 | CLANG_WARN_EMPTY_BODY = YES; 292 | CLANG_WARN_ENUM_CONVERSION = YES; 293 | CLANG_WARN_INFINITE_RECURSION = YES; 294 | CLANG_WARN_INT_CONVERSION = YES; 295 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 296 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 297 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 298 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 299 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 300 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 301 | CLANG_WARN_STRICT_PROTOTYPES = YES; 302 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 303 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 304 | CLANG_WARN_UNREACHABLE_CODE = YES; 305 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 306 | COPY_PHASE_STRIP = NO; 307 | DEBUG_INFORMATION_FORMAT = dwarf; 308 | ENABLE_STRICT_OBJC_MSGSEND = YES; 309 | ENABLE_TESTABILITY = YES; 310 | GCC_C_LANGUAGE_STANDARD = gnu11; 311 | GCC_DYNAMIC_NO_PIC = NO; 312 | GCC_NO_COMMON_BLOCKS = YES; 313 | GCC_OPTIMIZATION_LEVEL = 0; 314 | GCC_PREPROCESSOR_DEFINITIONS = ( 315 | "DEBUG=1", 316 | "$(inherited)", 317 | ); 318 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 319 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 320 | GCC_WARN_UNDECLARED_SELECTOR = YES; 321 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 322 | GCC_WARN_UNUSED_FUNCTION = YES; 323 | GCC_WARN_UNUSED_VARIABLE = YES; 324 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 325 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 326 | MTL_FAST_MATH = YES; 327 | ONLY_ACTIVE_ARCH = YES; 328 | SDKROOT = iphoneos; 329 | }; 330 | name = Debug; 331 | }; 332 | 68B4F9C127062D53003F323F /* Release */ = { 333 | isa = XCBuildConfiguration; 334 | buildSettings = { 335 | ALWAYS_SEARCH_USER_PATHS = NO; 336 | CLANG_ANALYZER_NONNULL = YES; 337 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 338 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 339 | CLANG_CXX_LIBRARY = "libc++"; 340 | CLANG_ENABLE_MODULES = YES; 341 | CLANG_ENABLE_OBJC_ARC = YES; 342 | CLANG_ENABLE_OBJC_WEAK = YES; 343 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 344 | CLANG_WARN_BOOL_CONVERSION = YES; 345 | CLANG_WARN_COMMA = YES; 346 | CLANG_WARN_CONSTANT_CONVERSION = YES; 347 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 348 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 349 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 350 | CLANG_WARN_EMPTY_BODY = YES; 351 | CLANG_WARN_ENUM_CONVERSION = YES; 352 | CLANG_WARN_INFINITE_RECURSION = YES; 353 | CLANG_WARN_INT_CONVERSION = YES; 354 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 355 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 356 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 357 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 358 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 359 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 360 | CLANG_WARN_STRICT_PROTOTYPES = YES; 361 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 362 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 363 | CLANG_WARN_UNREACHABLE_CODE = YES; 364 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 365 | COPY_PHASE_STRIP = NO; 366 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 367 | ENABLE_NS_ASSERTIONS = NO; 368 | ENABLE_STRICT_OBJC_MSGSEND = YES; 369 | GCC_C_LANGUAGE_STANDARD = gnu11; 370 | GCC_NO_COMMON_BLOCKS = YES; 371 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 372 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 373 | GCC_WARN_UNDECLARED_SELECTOR = YES; 374 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 375 | GCC_WARN_UNUSED_FUNCTION = YES; 376 | GCC_WARN_UNUSED_VARIABLE = YES; 377 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 378 | MTL_ENABLE_DEBUG_INFO = NO; 379 | MTL_FAST_MATH = YES; 380 | SDKROOT = iphoneos; 381 | VALIDATE_PRODUCT = YES; 382 | }; 383 | name = Release; 384 | }; 385 | 68B4F9C327062D53003F323F /* Debug */ = { 386 | isa = XCBuildConfiguration; 387 | buildSettings = { 388 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 389 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 390 | CODE_SIGN_STYLE = Automatic; 391 | CURRENT_PROJECT_VERSION = 1; 392 | GENERATE_INFOPLIST_FILE = YES; 393 | INFOPLIST_FILE = blockchain/Info.plist; 394 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 395 | INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; 396 | INFOPLIST_KEY_UIMainStoryboardFile = Main; 397 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 398 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 399 | LD_RUNPATH_SEARCH_PATHS = ( 400 | "$(inherited)", 401 | "@executable_path/Frameworks", 402 | ); 403 | MARKETING_VERSION = 1.0; 404 | PRODUCT_BUNDLE_IDENTIFIER = com.ykj.blockchain; 405 | PRODUCT_NAME = "$(TARGET_NAME)"; 406 | SWIFT_EMIT_LOC_STRINGS = YES; 407 | TARGETED_DEVICE_FAMILY = "1,2"; 408 | }; 409 | name = Debug; 410 | }; 411 | 68B4F9C427062D53003F323F /* Release */ = { 412 | isa = XCBuildConfiguration; 413 | buildSettings = { 414 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 415 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 416 | CODE_SIGN_STYLE = Automatic; 417 | CURRENT_PROJECT_VERSION = 1; 418 | GENERATE_INFOPLIST_FILE = YES; 419 | INFOPLIST_FILE = blockchain/Info.plist; 420 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 421 | INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; 422 | INFOPLIST_KEY_UIMainStoryboardFile = Main; 423 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 424 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 425 | LD_RUNPATH_SEARCH_PATHS = ( 426 | "$(inherited)", 427 | "@executable_path/Frameworks", 428 | ); 429 | MARKETING_VERSION = 1.0; 430 | PRODUCT_BUNDLE_IDENTIFIER = com.ykj.blockchain; 431 | PRODUCT_NAME = "$(TARGET_NAME)"; 432 | SWIFT_EMIT_LOC_STRINGS = YES; 433 | TARGETED_DEVICE_FAMILY = "1,2"; 434 | }; 435 | name = Release; 436 | }; 437 | /* End XCBuildConfiguration section */ 438 | 439 | /* Begin XCConfigurationList section */ 440 | 68B4F9A427062D51003F323F /* Build configuration list for PBXProject "blockchain" */ = { 441 | isa = XCConfigurationList; 442 | buildConfigurations = ( 443 | 68B4F9C027062D53003F323F /* Debug */, 444 | 68B4F9C127062D53003F323F /* Release */, 445 | ); 446 | defaultConfigurationIsVisible = 0; 447 | defaultConfigurationName = Release; 448 | }; 449 | 68B4F9C227062D53003F323F /* Build configuration list for PBXNativeTarget "blockchain" */ = { 450 | isa = XCConfigurationList; 451 | buildConfigurations = ( 452 | 68B4F9C327062D53003F323F /* Debug */, 453 | 68B4F9C427062D53003F323F /* Release */, 454 | ); 455 | defaultConfigurationIsVisible = 0; 456 | defaultConfigurationName = Release; 457 | }; 458 | /* End XCConfigurationList section */ 459 | }; 460 | rootObject = 68B4F9A127062D51003F323F /* Project object */; 461 | } 462 | -------------------------------------------------------------------------------- /blockchain/blockchain.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /blockchain/blockchain.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /blockchain/blockchain/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #import 9 | 10 | @interface AppDelegate : UIResponder 11 | 12 | 13 | @end 14 | 15 | -------------------------------------------------------------------------------- /blockchain/blockchain/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #import "AppDelegate.h" 9 | 10 | @interface AppDelegate () 11 | 12 | @end 13 | 14 | @implementation AppDelegate 15 | 16 | 17 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 18 | // Override point for customization after application launch. 19 | return YES; 20 | } 21 | 22 | 23 | #pragma mark - UISceneSession lifecycle 24 | 25 | 26 | - (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options { 27 | // Called when a new scene session is being created. 28 | // Use this method to select a configuration to create the new scene with. 29 | return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role]; 30 | } 31 | 32 | 33 | - (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet *)sceneSessions { 34 | // Called when the user discards a scene session. 35 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 36 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 37 | } 38 | 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /blockchain/blockchain/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /blockchain/blockchain/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /blockchain/blockchain/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /blockchain/blockchain/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /blockchain/blockchain/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /blockchain/blockchain/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | UIApplicationSceneManifest 6 | 7 | UIApplicationSupportsMultipleScenes 8 | 9 | UISceneConfigurations 10 | 11 | UIWindowSceneSessionRoleApplication 12 | 13 | 14 | UISceneConfigurationName 15 | Default Configuration 16 | UISceneDelegateClassName 17 | SceneDelegate 18 | UISceneStoryboardFile 19 | Main 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /blockchain/blockchain/SceneDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // SceneDelegate.h 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #import 9 | 10 | @interface SceneDelegate : UIResponder 11 | 12 | @property (strong, nonatomic) UIWindow * window; 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /blockchain/blockchain/SceneDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // SceneDelegate.m 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #import "SceneDelegate.h" 9 | 10 | @interface SceneDelegate () 11 | 12 | @end 13 | 14 | @implementation SceneDelegate 15 | 16 | 17 | - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions { 18 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 19 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 20 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 21 | } 22 | 23 | 24 | - (void)sceneDidDisconnect:(UIScene *)scene { 25 | // Called as the scene is being released by the system. 26 | // This occurs shortly after the scene enters the background, or when its session is discarded. 27 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 28 | // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead). 29 | } 30 | 31 | 32 | - (void)sceneDidBecomeActive:(UIScene *)scene { 33 | // Called when the scene has moved from an inactive state to an active state. 34 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 35 | } 36 | 37 | 38 | - (void)sceneWillResignActive:(UIScene *)scene { 39 | // Called when the scene will move from an active state to an inactive state. 40 | // This may occur due to temporary interruptions (ex. an incoming phone call). 41 | } 42 | 43 | 44 | - (void)sceneWillEnterForeground:(UIScene *)scene { 45 | // Called as the scene transitions from the background to the foreground. 46 | // Use this method to undo the changes made on entering the background. 47 | } 48 | 49 | 50 | - (void)sceneDidEnterBackground:(UIScene *)scene { 51 | // Called as the scene transitions from the foreground to the background. 52 | // Use this method to save data, release shared resources, and store enough scene-specific state information 53 | // to restore the scene back to its current state. 54 | } 55 | 56 | 57 | @end 58 | -------------------------------------------------------------------------------- /blockchain/blockchain/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #import 9 | 10 | @interface ViewController : UIViewController 11 | 12 | 13 | @end 14 | 15 | -------------------------------------------------------------------------------- /blockchain/blockchain/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #import "ViewController.h" 9 | 10 | #import "ETHBlockChain.h" 11 | 12 | @interface ViewController () 13 | 14 | @end 15 | 16 | @implementation ViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | // Do any additional setup after loading the view. 21 | 22 | NSString * mnemonic = [ETHBlockChain chain_generateMnemonicBits:m12 language:english]; 23 | 24 | NSLog(@"--- \n%@", mnemonic); 25 | } 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /blockchain/blockchain/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #import 9 | #import "AppDelegate.h" 10 | 11 | int main(int argc, char * argv[]) { 12 | NSString * appDelegateClassName; 13 | @autoreleasepool { 14 | // Setup code that might create autoreleased objects goes here. 15 | appDelegateClassName = NSStringFromClass([AppDelegate class]); 16 | } 17 | return UIApplicationMain(argc, argv, nil, appDelegateClassName); 18 | } 19 | -------------------------------------------------------------------------------- /sources/Ethereum/ETHBlockChain.h: -------------------------------------------------------------------------------- 1 | // 2 | // ETHBlockChain.h 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #import "BaseChain.h" 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface ETHBlockChain : BaseChain 13 | 14 | @end 15 | 16 | NS_ASSUME_NONNULL_END 17 | -------------------------------------------------------------------------------- /sources/Ethereum/ETHBlockChain.m: -------------------------------------------------------------------------------- 1 | // 2 | // ETHBlockChain.m 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #import "ETHBlockChain.h" 9 | 10 | @implementation ETHBlockChain 11 | 12 | /// 生成助记词 13 | /// @param bits 助记词位数 14 | /// @param language 助记词语言 15 | /// @return 助记词字符串,以空格 ` ` 分割 16 | + (nullable NSString *)chain_generateMnemonicBits:(BIP39Bits)bits language:(BIP39Language)language{ 17 | return [super chain_generateMnemonicBits:bits language:language]; 18 | } 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /sources/bips/bip39.c: -------------------------------------------------------------------------------- 1 | // 2 | // bip39.c 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #include "bip39.h" 9 | 10 | #include 11 | #include 12 | #include 13 | #include "sha256.h" 14 | 15 | /// 获取助记词个数 16 | size_t bip39_mnemonic_bits(BIP39Bits bits){ 17 | switch (bits) { 18 | case m12:return 12; 19 | case m15:return 15; 20 | case m18:return 18; 21 | case m21:return 21; 22 | case m24:return 24; 23 | } 24 | return 0; 25 | } 26 | 27 | /// 生成随机数的回调函数 28 | /// @param random 随机数 29 | /// @param len 长度 30 | void bip39_random(unsigned char * random, int len){ 31 | int i, random_num; 32 | struct timeval tv; 33 | unsigned int seed_num; 34 | char seed_str[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 35 | unsigned long seed_str_len = strlen(seed_str); 36 | gettimeofday(&tv, NULL); 37 | seed_num = (unsigned int)(tv.tv_sec + tv.tv_usec); 38 | srand(seed_num); 39 | for (i = 0; i < len; i++) { 40 | random_num = rand() % seed_str_len; 41 | random[i] = seed_str[random_num]; 42 | } 43 | } 44 | 45 | /// 字符串转二进制,转十进制 46 | /// @param str 待转字符串 47 | /// @param cnt 二进制长度 48 | int bip39_char_to_binary_to_decimalism(unsigned char * str, size_t cnt){ 49 | char binary[cnt]; 50 | for (int i = 0; i < cnt; i++) { 51 | if ((str[i] >> (i-1)) & (0x01 == 1)) { 52 | binary[i] = '1'; 53 | } else { 54 | binary[i] = '0'; 55 | } 56 | } 57 | return strtol(binary, NULL, 2); 58 | } 59 | 60 | /// 生成助记词 61 | /// @param bits 助记词位数 62 | /// @param language 助记词语言 63 | void bip39_generate_mnemonic(unsigned char dst[][20], BIP39Bits bits, BIP39Language language){ 64 | // 读取文件内容,助记词单词 65 | const struct words read = word_lists(language); 66 | 67 | // 生成指定位长度随机数 68 | BYTE random[bits]; 69 | bip39_random(random, bits); 70 | 71 | // SHA256 72 | BYTE buf[SHA256_BLOCK_SIZE]; 73 | SHA256_CTX ctx; 74 | sha256_init(&ctx); 75 | sha256_update(&ctx, random, strlen(random)); 76 | sha256_final(&ctx, buf); 77 | 78 | // 拼接,取哈希之后的前4位 79 | BYTE checksum[4],append[bits + 4]; 80 | strncpy(checksum, buf, 4); 81 | strcpy(append, random); 82 | strcat(append, checksum); 83 | 84 | // 按每11位切分`split` 85 | size_t cnt = 11; 86 | BYTE dividing[cnt]; 87 | for (int i = 0; i < bits + 4; i+=cnt) { 88 | strncpy(dividing, append + i, cnt); 89 | int index = bip39_char_to_binary_to_decimalism(dividing, cnt); 90 | strcpy(dst[i/cnt], read.lists[index]); 91 | } 92 | 93 | word_lists_free(read); 94 | } 95 | -------------------------------------------------------------------------------- /sources/bips/bip39.h: -------------------------------------------------------------------------------- 1 | // 2 | // bip39.h 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 助记词相关 7 | 8 | #ifndef bip39_h 9 | #define bip39_h 10 | 11 | #include 12 | #include "wordlist.h" 13 | 14 | /// 助记词秘钥生成器网站,https://iancoleman.io/bip39/ 15 | /// 助记词生成过程: 16 | /// 举12位助记词例子, 17 | /// 生成一个128位随机数,再加上对随机数校验4位,得到132位的数, 18 | /// 然后按每11位做切分,这样就有12个二进制数, 19 | /// 然后用每个数去查BIP39定义的单词表,这样就得到12个助记单词 20 | 21 | /// 助记词位数 22 | enum BIP39Bits { 23 | m12 = 128, 24 | m15 = 160, 25 | m18 = 192, 26 | m21 = 224, 27 | m24 = 256, 28 | }; 29 | typedef enum BIP39Bits BIP39Bits; 30 | 31 | /// 获取助记词个数 32 | extern size_t bip39_mnemonic_bits(BIP39Bits bits); 33 | 34 | /// 生成随机数的回调函数 35 | /// @param random 随机数 36 | /// @param len 长度 37 | extern void bip39_random(unsigned char * random, int len); 38 | 39 | /// 生成助记词 40 | /// @param bits 助记词位数 41 | /// @param language 助记词语言 42 | extern void bip39_generate_mnemonic(unsigned char dst[][20], 43 | BIP39Bits bits, 44 | BIP39Language language); 45 | 46 | 47 | 48 | #endif /* bip39_h */ 49 | -------------------------------------------------------------------------------- /sources/bips/wordlist.c: -------------------------------------------------------------------------------- 1 | // 2 | // wordlist.c 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #include "wordlist.h" 9 | #include 10 | #include 11 | 12 | static char * word_english_list(void); 13 | static char ** word_explode(char separator, const char * string, int * size); 14 | static void word_freeExplode(char **arr, int size); 15 | /// 获取单词表 16 | /// @param language 单词语言 17 | struct words word_lists(BIP39Language language){ 18 | int size; 19 | char **ret = word_explode(' ', word_english_list(), &size); 20 | struct words word = {size, ret}; 21 | return word; 22 | } 23 | 24 | /// 释放资源 25 | void word_lists_free(struct words word){ 26 | word_freeExplode(word.lists, word.size); 27 | } 28 | 29 | static void word_freeExplode(char **arr, int size){ 30 | for (int i = 0; i < size; i++) { 31 | free(arr[i]); 32 | } 33 | free(arr); 34 | } 35 | 36 | // 返回一个 char *arr[], size为返回数组的长度 37 | /// @param separator 分割符 38 | /// @param string 待分割字符串 39 | /// @param size 返回数组的长度 40 | static char ** word_explode(char separator, const char * string, int * size){ 41 | int count = 0, i; 42 | for (i = 0; i < strlen(string); i++) { 43 | if (string[i] == separator) { 44 | count ++; 45 | } 46 | } 47 | char **ret = calloc(++count, sizeof(char *)); 48 | int lastindex = -1; 49 | int j = 0; 50 | for (i = 0; i < strlen(string); i++) { 51 | if (string[i] == separator) { 52 | ret[j] = calloc(i - lastindex, sizeof(char));//分配子串长度+1的内存空间 53 | memcpy(ret[j], string + lastindex + 1, i - lastindex - 1); 54 | j++; 55 | lastindex = i; 56 | } 57 | } 58 | if (lastindex <= strlen(string) - 1) { 59 | ret[j] = calloc(strlen(string) - lastindex, sizeof(char)); 60 | memcpy(ret[j], string + lastindex + 1, strlen(string) - 1 - lastindex); 61 | j++; 62 | } 63 | *size = j; 64 | return ret; 65 | } 66 | 67 | /// english单词表 68 | static char * word_english_list(void){ 69 | return "abandon ability able about above absent absorb abstract absurd abuse access accident account accuse achieve acid acoustic acquire across act action actor actress actual adapt add addict address adjust admit adult advance advice aerobic affair afford afraid again age agent agree ahead aim air airport aisle alarm album alcohol alert alien all alley allow almost alone alpha already also alter always amateur amazing among amount amused analyst anchor ancient anger angle angry animal ankle announce annual another answer antenna antique anxiety any apart apology appear apple approve april arch arctic area arena argue arm armed armor army around arrange arrest arrive arrow art artefact artist artwork ask aspect assault asset assist assume asthma athlete atom attack attend attitude attract auction audit august aunt author auto autumn average avocado avoid awake aware away awesome awful awkward axis baby bachelor bacon badge bag balance balcony ball bamboo banana banner bar barely bargain barrel base basic basket battle beach bean beauty because become beef before begin behave behind believe below belt bench benefit best betray better between beyond bicycle bid bike bind biology bird birth bitter black blade blame blanket blast bleak bless blind blood blossom blouse blue blur blush board boat body boil bomb bone bonus book boost border boring borrow boss bottom bounce box boy bracket brain brand brass brave bread breeze brick bridge brief bright bring brisk broccoli broken bronze broom brother brown brush bubble buddy budget buffalo build bulb bulk bullet bundle bunker burden burger burst bus business busy butter buyer buzz cabbage cabin cable cactus cage cake call calm camera camp can canal cancel candy cannon canoe canvas canyon capable capital captain car carbon card cargo carpet carry cart case cash casino castle casual cat catalog catch category cattle caught cause caution cave ceiling celery cement census century cereal certain chair chalk champion change chaos chapter charge chase chat cheap check cheese chef cherry chest chicken chief child chimney choice choose chronic chuckle chunk churn cigar cinnamon circle citizen city civil claim clap clarify claw clay clean clerk clever click client cliff climb clinic clip clock clog close cloth cloud clown club clump cluster clutch coach coast coconut code coffee coil coin collect color column combine come comfort comic common company concert conduct confirm congress connect consider control convince cook cool copper copy coral core corn correct cost cotton couch country couple course cousin cover coyote crack cradle craft cram crane crash crater crawl crazy cream credit creek crew cricket crime crisp critic crop cross crouch crowd crucial cruel cruise crumble crunch crush cry crystal cube culture cup cupboard curious current curtain curve cushion custom cute cycle dad damage damp dance danger daring dash daughter dawn day deal debate debris decade december decide decline decorate decrease deer defense define defy degree delay deliver demand demise denial dentist deny depart depend deposit depth deputy derive describe desert design desk despair destroy detail detect develop device devote diagram dial diamond diary dice diesel diet differ digital dignity dilemma dinner dinosaur direct dirt disagree discover disease dish dismiss disorder display distance divert divide divorce dizzy doctor document dog doll dolphin domain donate donkey donor door dose double dove draft dragon drama drastic draw dream dress drift drill drink drip drive drop drum dry duck dumb dune during dust dutch duty dwarf dynamic eager eagle early earn earth easily east easy echo ecology economy edge edit educate effort egg eight either elbow elder electric elegant element elephant elevator elite else embark embody embrace emerge emotion employ empower empty enable enact end endless endorse enemy energy enforce engage engine enhance enjoy enlist enough enrich enroll ensure enter entire entry envelope episode equal equip era erase erode erosion error erupt escape essay essence estate eternal ethics evidence evil evoke evolve exact example excess exchange excite exclude excuse execute exercise exhaust exhibit exile exist exit exotic expand expect expire explain expose express extend extra eye eyebrow fabric face faculty fade faint faith fall false fame family famous fan fancy fantasy farm fashion fat fatal father fatigue fault favorite feature february federal fee feed feel female fence festival fetch fever few fiber fiction field figure file film filter final find fine finger finish fire firm first fiscal fish fit fitness fix flag flame flash flat flavor flee flight flip float flock floor flower fluid flush fly foam focus fog foil fold follow food foot force forest forget fork fortune forum forward fossil foster found fox fragile frame frequent fresh friend fringe frog front frost frown frozen fruit fuel fun funny furnace fury future gadget gain galaxy gallery game gap garage garbage garden garlic garment gas gasp gate gather gauge gaze general genius genre gentle genuine gesture ghost giant gift giggle ginger giraffe girl give glad glance glare glass glide glimpse globe gloom glory glove glow glue goat goddess gold good goose gorilla gospel gossip govern gown grab grace grain grant grape grass gravity great green grid grief grit grocery group grow grunt guard guess guide guilt guitar gun gym habit hair half hammer hamster hand happy harbor hard harsh harvest hat have hawk hazard head health heart heavy hedgehog height hello helmet help hen hero hidden high hill hint hip hire history hobby hockey hold hole holiday hollow home honey hood hope horn horror horse hospital host hotel hour hover hub huge human humble humor hundred hungry hunt hurdle hurry hurt husband hybrid ice icon idea identify idle ignore ill illegal illness image imitate immense immune impact impose improve impulse inch include income increase index indicate indoor industry infant inflict inform inhale inherit initial inject injury inmate inner innocent input inquiry insane insect inside inspire install intact interest into invest invite involve iron island isolate issue item ivory jacket jaguar jar jazz jealous jeans jelly jewel job join joke journey joy judge juice jump jungle junior junk just kangaroo keen keep ketchup key kick kid kidney kind kingdom kiss kit kitchen kite kitten kiwi knee knife knock know lab label labor ladder lady lake lamp language laptop large later latin laugh laundry lava law lawn lawsuit layer lazy leader leaf learn leave lecture left leg legal legend leisure lemon lend length lens leopard lesson letter level liar liberty library license life lift light like limb limit link lion liquid list little live lizard load loan lobster local lock logic lonely long loop lottery loud lounge love loyal lucky luggage lumber lunar lunch luxury lyrics machine mad magic magnet maid mail main major make mammal man manage mandate mango mansion manual maple marble march margin marine market marriage mask mass master match material math matrix matter maximum maze meadow mean measure meat mechanic medal media melody melt member memory mention menu mercy merge merit merry mesh message metal method middle midnight milk million mimic mind minimum minor minute miracle mirror misery miss mistake mix mixed mixture mobile model modify mom moment monitor monkey monster month moon moral more morning mosquito mother motion motor mountain mouse move movie much muffin mule multiply muscle museum mushroom music must mutual myself mystery myth naive name napkin narrow nasty nation nature near neck need negative neglect neither nephew nerve nest net network neutral never news next nice night noble noise nominee noodle normal north nose notable note nothing notice novel now nuclear number nurse nut oak obey object oblige obscure observe obtain obvious occur ocean october odor off offer office often oil okay old olive olympic omit once one onion online only open opera opinion oppose option orange orbit orchard order ordinary organ orient original orphan ostrich other outdoor outer output outside oval oven over own owner oxygen oyster ozone pact paddle page pair palace palm panda panel panic panther paper parade parent park parrot party pass patch path patient patrol pattern pause pave payment peace peanut pear peasant pelican pen penalty pencil people pepper perfect permit person pet phone photo phrase physical piano picnic picture piece pig pigeon pill pilot pink pioneer pipe pistol pitch pizza place planet plastic plate play please pledge pluck plug plunge poem poet point polar pole police pond pony pool popular portion position possible post potato pottery poverty powder power practice praise predict prefer prepare present pretty prevent price pride primary print priority prison private prize problem process produce profit program project promote proof property prosper protect proud provide public pudding pull pulp pulse pumpkin punch pupil puppy purchase purity purpose purse push put puzzle pyramid quality quantum quarter question quick quit quiz quote rabbit raccoon race rack radar radio rail rain raise rally ramp ranch random range rapid rare rate rather raven raw razor ready real reason rebel rebuild recall receive recipe record recycle reduce reflect reform refuse region regret regular reject relax release relief rely remain remember remind remove render renew rent reopen repair repeat replace report require rescue resemble resist resource response result retire retreat return reunion reveal review reward rhythm rib ribbon rice rich ride ridge rifle right rigid ring riot ripple risk ritual rival river road roast robot robust rocket romance roof rookie room rose rotate rough round route royal rubber rude rug rule run runway rural sad saddle sadness safe sail salad salmon salon salt salute same sample sand satisfy satoshi sauce sausage save say scale scan scare scatter scene scheme school science scissors scorpion scout scrap screen script scrub sea search season seat second secret section security seed seek segment select sell seminar senior sense sentence series service session settle setup seven shadow shaft shallow share shed shell sheriff shield shift shine ship shiver shock shoe shoot shop short shoulder shove shrimp shrug shuffle shy sibling sick side siege sight sign silent silk silly silver similar simple since sing siren sister situate six size skate sketch ski skill skin skirt skull slab slam sleep slender slice slide slight slim slogan slot slow slush small smart smile smoke smooth snack snake snap sniff snow soap soccer social sock soda soft solar soldier solid solution solve someone song soon sorry sort soul sound soup source south space spare spatial spawn speak special speed spell spend sphere spice spider spike spin spirit split spoil sponsor spoon sport spot spray spread spring spy square squeeze squirrel stable stadium staff stage stairs stamp stand start state stay steak steel stem step stereo stick still sting stock stomach stone stool story stove strategy street strike strong struggle student stuff stumble style subject submit subway success such sudden suffer sugar suggest suit summer sun sunny sunset super supply supreme sure surface surge surprise surround survey suspect sustain swallow swamp swap swarm swear sweet swift swim swing switch sword symbol symptom syrup system table tackle tag tail talent talk tank tape target task taste tattoo taxi teach team tell ten tenant tennis tent term test text thank that theme then theory there they thing this thought three thrive throw thumb thunder ticket tide tiger tilt timber time tiny tip tired tissue title toast tobacco today toddler toe together toilet token tomato tomorrow tone tongue tonight tool tooth top topic topple torch tornado tortoise toss total tourist toward tower town toy track trade traffic tragic train transfer trap trash travel tray treat tree trend trial tribe trick trigger trim trip trophy trouble truck true truly trumpet trust truth try tube tuition tumble tuna tunnel turkey turn turtle twelve twenty twice twin twist two type typical ugly umbrella unable unaware uncle uncover under undo unfair unfold unhappy uniform unique unit universe unknown unlock until unusual unveil update upgrade uphold upon upper upset urban urge usage use used useful useless usual utility vacant vacuum vague valid valley valve van vanish vapor various vast vault vehicle velvet vendor venture venue verb verify version very vessel veteran viable vibrant vicious victory video view village vintage violin virtual virus visa visit visual vital vivid vocal voice void volcano volume vote voyage wage wagon wait walk wall walnut want warfare warm warrior wash wasp waste water wave way wealth weapon wear weasel weather web wedding weekend weird welcome west wet whale what wheat wheel when where whip whisper wide width wife wild will win window wine wing wink winner winter wire wisdom wise wish witness wolf woman wonder wood wool word work world worry worth wrap wreck wrestle wrist write wrong yard year yellow you young youth zebra zero zone zoo"; 70 | } 71 | -------------------------------------------------------------------------------- /sources/bips/wordlist.h: -------------------------------------------------------------------------------- 1 | // 2 | // wordlist.h 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #ifndef wordlist_h 9 | #define wordlist_h 10 | 11 | #include 12 | 13 | /// 14 | /// BIP39定义的单词表,https://github.com/bitcoin/bips/blob/master/bip-0039/bip-0039-wordlists.md 15 | 16 | /// 助记词语言 17 | typedef enum { 18 | english, 19 | } BIP39Language; 20 | struct words { 21 | size_t size; /// 单词数目 22 | const char ** lists; /// 单词列表 23 | }; 24 | /// 获取单词表 25 | /// @param language 单词语言 26 | extern struct words word_lists(BIP39Language language); 27 | 28 | /// 释放资源 29 | extern void word_lists_free(struct words word); 30 | 31 | #endif /* wordlist_h */ 32 | -------------------------------------------------------------------------------- /sources/core/BaseChain.h: -------------------------------------------------------------------------------- 1 | // 2 | // BaseChain.h 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 底层公共链 7 | 8 | #import 9 | 10 | #include "bip39.h" 11 | 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | @interface BaseChain : NSObject 15 | 16 | /// 生成助记词 17 | /// @param bits 助记词位数 18 | /// @param language 助记词语言 19 | /// @return 助记词字符串,以空格 ` ` 分割 20 | + (nullable NSString *)chain_generateMnemonicBits:(BIP39Bits)bits 21 | language:(BIP39Language)language NS_REQUIRES_SUPER; 22 | 23 | 24 | 25 | @end 26 | 27 | NS_ASSUME_NONNULL_END 28 | -------------------------------------------------------------------------------- /sources/core/BaseChain.m: -------------------------------------------------------------------------------- 1 | // 2 | // BaseChain.m 3 | // blockchain 4 | // 5 | // Created by 77。 on 2021/10/1. 6 | // 7 | 8 | #import "BaseChain.h" 9 | 10 | @implementation BaseChain 11 | 12 | /// 生成助记词 13 | /// @param bits 助记词位数 14 | /// @param language 助记词语言 15 | /// @return 助记词字符串,以空格 ` ` 分割 16 | + (nullable NSString *)chain_generateMnemonicBits:(BIP39Bits)bits language:(BIP39Language)language{ 17 | size_t digit = bip39_mnemonic_bits(bits); 18 | unsigned char dst[digit][20]; 19 | bip39_generate_mnemonic(dst, bits, language); 20 | NSMutableString * __autoreleasing string = [NSMutableString string]; 21 | for (int i = 0; i < digit; i++) { 22 | NSString * word = [NSString stringWithUTF8String:dst[i]]; 23 | [string appendFormat:@"%@ ", word]; 24 | } 25 | NSString * result = [string copy]; 26 | return [result length] ? result : nil; 27 | } 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /sources/crypto/aes.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This is an implementation of the AES algorithm, specifically ECB, CTR and CBC mode. 4 | Block size can be chosen in aes.h - available choices are AES128, AES192, AES256. 5 | 6 | The implementation is verified against the test vectors in: 7 | National Institute of Standards and Technology Special Publication 800-38A 2001 ED 8 | 9 | ECB-AES128 10 | ---------- 11 | 12 | plain-text: 13 | 6bc1bee22e409f96e93d7e117393172a 14 | ae2d8a571e03ac9c9eb76fac45af8e51 15 | 30c81c46a35ce411e5fbc1191a0a52ef 16 | f69f2445df4f9b17ad2b417be66c3710 17 | 18 | key: 19 | 2b7e151628aed2a6abf7158809cf4f3c 20 | 21 | resulting cipher 22 | 3ad77bb40d7a3660a89ecaf32466ef97 23 | f5d3d58503b9699de785895a96fdbaaf 24 | 43b1cd7f598ece23881b00e3ed030688 25 | 7b0c785e27e8ad3f8223207104725dd4 26 | 27 | 28 | NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) 29 | You should pad the end of the string with zeros if this is not the case. 30 | For AES192/256 the key size is proportionally larger. 31 | 32 | */ 33 | 34 | 35 | /*****************************************************************************/ 36 | /* Includes: */ 37 | /*****************************************************************************/ 38 | #include 39 | #include // CBC mode, for memset 40 | #include "aes.h" 41 | 42 | /*****************************************************************************/ 43 | /* Defines: */ 44 | /*****************************************************************************/ 45 | // The number of columns comprising a state in AES. This is a constant in AES. Value=4 46 | #define Nb 4 47 | 48 | #if defined(AES256) && (AES256 == 1) 49 | #define Nk 8 50 | #define Nr 14 51 | #elif defined(AES192) && (AES192 == 1) 52 | #define Nk 6 53 | #define Nr 12 54 | #else 55 | #define Nk 4 // The number of 32 bit words in a key. 56 | #define Nr 10 // The number of rounds in AES Cipher. 57 | #endif 58 | 59 | // jcallan@github points out that declaring Multiply as a function 60 | // reduces code size considerably with the Keil ARM compiler. 61 | // See this link for more information: https://github.com/kokke/tiny-AES-C/pull/3 62 | #ifndef MULTIPLY_AS_A_FUNCTION 63 | #define MULTIPLY_AS_A_FUNCTION 0 64 | #endif 65 | 66 | 67 | 68 | 69 | /*****************************************************************************/ 70 | /* Private variables: */ 71 | /*****************************************************************************/ 72 | // state - array holding the intermediate results during decryption. 73 | typedef uint8_t state_t[4][4]; 74 | 75 | 76 | 77 | // The lookup-tables are marked const so they can be placed in read-only storage instead of RAM 78 | // The numbers below can be computed dynamically trading ROM for RAM - 79 | // This can be useful in (embedded) bootloader applications, where ROM is often limited. 80 | static const uint8_t sbox[256] = { 81 | //0 1 2 3 4 5 6 7 8 9 A B C D E F 82 | 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 83 | 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 84 | 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 85 | 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 86 | 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 87 | 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 88 | 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 89 | 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 90 | 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 91 | 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 92 | 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 93 | 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 94 | 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 95 | 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 96 | 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 97 | 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; 98 | 99 | static const uint8_t rsbox[256] = { 100 | 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 101 | 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 102 | 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, 103 | 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, 104 | 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 105 | 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, 106 | 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, 107 | 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 108 | 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, 109 | 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, 110 | 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 111 | 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, 112 | 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, 113 | 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 114 | 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 115 | 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; 116 | 117 | // The round constant word array, Rcon[i], contains the values given by 118 | // x to the power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8) 119 | static const uint8_t Rcon[11] = { 120 | 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; 121 | 122 | /* 123 | * Jordan Goulder points out in PR #12 (https://github.com/kokke/tiny-AES-C/pull/12), 124 | * that you can remove most of the elements in the Rcon array, because they are unused. 125 | * 126 | * From Wikipedia's article on the Rijndael key schedule @ https://en.wikipedia.org/wiki/Rijndael_key_schedule#Rcon 127 | * 128 | * "Only the first some of these constants are actually used – up to rcon[10] for AES-128 (as 11 round keys are needed), 129 | * up to rcon[8] for AES-192, up to rcon[7] for AES-256. rcon[0] is not used in AES algorithm." 130 | */ 131 | 132 | 133 | /*****************************************************************************/ 134 | /* Private functions: */ 135 | /*****************************************************************************/ 136 | /* 137 | static uint8_t getSBoxValue(uint8_t num) 138 | { 139 | return sbox[num]; 140 | } 141 | */ 142 | #define getSBoxValue(num) (sbox[(num)]) 143 | /* 144 | static uint8_t getSBoxInvert(uint8_t num) 145 | { 146 | return rsbox[num]; 147 | } 148 | */ 149 | #define getSBoxInvert(num) (rsbox[(num)]) 150 | 151 | // This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. 152 | static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key) 153 | { 154 | unsigned i, j, k; 155 | uint8_t tempa[4]; // Used for the column/row operations 156 | 157 | // The first round key is the key itself. 158 | for (i = 0; i < Nk; ++i) 159 | { 160 | RoundKey[(i * 4) + 0] = Key[(i * 4) + 0]; 161 | RoundKey[(i * 4) + 1] = Key[(i * 4) + 1]; 162 | RoundKey[(i * 4) + 2] = Key[(i * 4) + 2]; 163 | RoundKey[(i * 4) + 3] = Key[(i * 4) + 3]; 164 | } 165 | 166 | // All other round keys are found from the previous round keys. 167 | for (i = Nk; i < Nb * (Nr + 1); ++i) 168 | { 169 | { 170 | k = (i - 1) * 4; 171 | tempa[0]=RoundKey[k + 0]; 172 | tempa[1]=RoundKey[k + 1]; 173 | tempa[2]=RoundKey[k + 2]; 174 | tempa[3]=RoundKey[k + 3]; 175 | 176 | } 177 | 178 | if (i % Nk == 0) 179 | { 180 | // This function shifts the 4 bytes in a word to the left once. 181 | // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] 182 | 183 | // Function RotWord() 184 | { 185 | k = tempa[0]; 186 | tempa[0] = tempa[1]; 187 | tempa[1] = tempa[2]; 188 | tempa[2] = tempa[3]; 189 | tempa[3] = k; 190 | } 191 | 192 | // SubWord() is a function that takes a four-byte input word and 193 | // applies the S-box to each of the four bytes to produce an output word. 194 | 195 | // Function Subword() 196 | { 197 | tempa[0] = getSBoxValue(tempa[0]); 198 | tempa[1] = getSBoxValue(tempa[1]); 199 | tempa[2] = getSBoxValue(tempa[2]); 200 | tempa[3] = getSBoxValue(tempa[3]); 201 | } 202 | 203 | tempa[0] = tempa[0] ^ Rcon[i/Nk]; 204 | } 205 | #if defined(AES256) && (AES256 == 1) 206 | if (i % Nk == 4) 207 | { 208 | // Function Subword() 209 | { 210 | tempa[0] = getSBoxValue(tempa[0]); 211 | tempa[1] = getSBoxValue(tempa[1]); 212 | tempa[2] = getSBoxValue(tempa[2]); 213 | tempa[3] = getSBoxValue(tempa[3]); 214 | } 215 | } 216 | #endif 217 | j = i * 4; k=(i - Nk) * 4; 218 | RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0]; 219 | RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1]; 220 | RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2]; 221 | RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3]; 222 | } 223 | } 224 | 225 | void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key) 226 | { 227 | KeyExpansion(ctx->RoundKey, key); 228 | } 229 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 230 | void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv) 231 | { 232 | KeyExpansion(ctx->RoundKey, key); 233 | memcpy (ctx->Iv, iv, AES_BLOCKLEN); 234 | } 235 | void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv) 236 | { 237 | memcpy (ctx->Iv, iv, AES_BLOCKLEN); 238 | } 239 | #endif 240 | 241 | // This function adds the round key to state. 242 | // The round key is added to the state by an XOR function. 243 | static void AddRoundKey(uint8_t round,state_t* state,uint8_t* RoundKey) 244 | { 245 | uint8_t i,j; 246 | for (i = 0; i < 4; ++i) 247 | { 248 | for (j = 0; j < 4; ++j) 249 | { 250 | (*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j]; 251 | } 252 | } 253 | } 254 | 255 | // The SubBytes Function Substitutes the values in the 256 | // state matrix with values in an S-box. 257 | static void SubBytes(state_t* state) 258 | { 259 | uint8_t i, j; 260 | for (i = 0; i < 4; ++i) 261 | { 262 | for (j = 0; j < 4; ++j) 263 | { 264 | (*state)[j][i] = getSBoxValue((*state)[j][i]); 265 | } 266 | } 267 | } 268 | 269 | // The ShiftRows() function shifts the rows in the state to the left. 270 | // Each row is shifted with different offset. 271 | // Offset = Row number. So the first row is not shifted. 272 | static void ShiftRows(state_t* state) 273 | { 274 | uint8_t temp; 275 | 276 | // Rotate first row 1 columns to left 277 | temp = (*state)[0][1]; 278 | (*state)[0][1] = (*state)[1][1]; 279 | (*state)[1][1] = (*state)[2][1]; 280 | (*state)[2][1] = (*state)[3][1]; 281 | (*state)[3][1] = temp; 282 | 283 | // Rotate second row 2 columns to left 284 | temp = (*state)[0][2]; 285 | (*state)[0][2] = (*state)[2][2]; 286 | (*state)[2][2] = temp; 287 | 288 | temp = (*state)[1][2]; 289 | (*state)[1][2] = (*state)[3][2]; 290 | (*state)[3][2] = temp; 291 | 292 | // Rotate third row 3 columns to left 293 | temp = (*state)[0][3]; 294 | (*state)[0][3] = (*state)[3][3]; 295 | (*state)[3][3] = (*state)[2][3]; 296 | (*state)[2][3] = (*state)[1][3]; 297 | (*state)[1][3] = temp; 298 | } 299 | 300 | static uint8_t xtime(uint8_t x) 301 | { 302 | return ((x<<1) ^ (((x>>7) & 1) * 0x1b)); 303 | } 304 | 305 | // MixColumns function mixes the columns of the state matrix 306 | static void MixColumns(state_t* state) 307 | { 308 | uint8_t i; 309 | uint8_t Tmp, Tm, t; 310 | for (i = 0; i < 4; ++i) 311 | { 312 | t = (*state)[i][0]; 313 | Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ; 314 | Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ; 315 | Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ; 316 | Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ; 317 | Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ; 318 | } 319 | } 320 | 321 | // Multiply is used to multiply numbers in the field GF(2^8) 322 | #if MULTIPLY_AS_A_FUNCTION 323 | static uint8_t Multiply(uint8_t x, uint8_t y) 324 | { 325 | return (((y & 1) * x) ^ 326 | ((y>>1 & 1) * xtime(x)) ^ 327 | ((y>>2 & 1) * xtime(xtime(x))) ^ 328 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ 329 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); 330 | } 331 | #else 332 | #define Multiply(x, y) \ 333 | ( ((y & 1) * x) ^ \ 334 | ((y>>1 & 1) * xtime(x)) ^ \ 335 | ((y>>2 & 1) * xtime(xtime(x))) ^ \ 336 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \ 337 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \ 338 | 339 | #endif 340 | 341 | // MixColumns function mixes the columns of the state matrix. 342 | // The method used to multiply may be difficult to understand for the inexperienced. 343 | // Please use the references to gain more information. 344 | static void InvMixColumns(state_t* state) 345 | { 346 | int i; 347 | uint8_t a, b, c, d; 348 | for (i = 0; i < 4; ++i) 349 | { 350 | a = (*state)[i][0]; 351 | b = (*state)[i][1]; 352 | c = (*state)[i][2]; 353 | d = (*state)[i][3]; 354 | 355 | (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09); 356 | (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d); 357 | (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b); 358 | (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e); 359 | } 360 | } 361 | 362 | 363 | // The SubBytes Function Substitutes the values in the 364 | // state matrix with values in an S-box. 365 | static void InvSubBytes(state_t* state) 366 | { 367 | uint8_t i, j; 368 | for (i = 0; i < 4; ++i) 369 | { 370 | for (j = 0; j < 4; ++j) 371 | { 372 | (*state)[j][i] = getSBoxInvert((*state)[j][i]); 373 | } 374 | } 375 | } 376 | 377 | static void InvShiftRows(state_t* state) 378 | { 379 | uint8_t temp; 380 | 381 | // Rotate first row 1 columns to right 382 | temp = (*state)[3][1]; 383 | (*state)[3][1] = (*state)[2][1]; 384 | (*state)[2][1] = (*state)[1][1]; 385 | (*state)[1][1] = (*state)[0][1]; 386 | (*state)[0][1] = temp; 387 | 388 | // Rotate second row 2 columns to right 389 | temp = (*state)[0][2]; 390 | (*state)[0][2] = (*state)[2][2]; 391 | (*state)[2][2] = temp; 392 | 393 | temp = (*state)[1][2]; 394 | (*state)[1][2] = (*state)[3][2]; 395 | (*state)[3][2] = temp; 396 | 397 | // Rotate third row 3 columns to right 398 | temp = (*state)[0][3]; 399 | (*state)[0][3] = (*state)[1][3]; 400 | (*state)[1][3] = (*state)[2][3]; 401 | (*state)[2][3] = (*state)[3][3]; 402 | (*state)[3][3] = temp; 403 | } 404 | 405 | 406 | // Cipher is the main function that encrypts the PlainText. 407 | static void Cipher(state_t* state, uint8_t* RoundKey) 408 | { 409 | uint8_t round = 0; 410 | 411 | // Add the First round key to the state before starting the rounds. 412 | AddRoundKey(0, state, RoundKey); 413 | 414 | // There will be Nr rounds. 415 | // The first Nr-1 rounds are identical. 416 | // These Nr-1 rounds are executed in the loop below. 417 | for (round = 1; round < Nr; ++round) 418 | { 419 | SubBytes(state); 420 | ShiftRows(state); 421 | MixColumns(state); 422 | AddRoundKey(round, state, RoundKey); 423 | } 424 | 425 | // The last round is given below. 426 | // The MixColumns function is not here in the last round. 427 | SubBytes(state); 428 | ShiftRows(state); 429 | AddRoundKey(Nr, state, RoundKey); 430 | } 431 | 432 | static void InvCipher(state_t* state,uint8_t* RoundKey) 433 | { 434 | uint8_t round = 0; 435 | 436 | // Add the First round key to the state before starting the rounds. 437 | AddRoundKey(Nr, state, RoundKey); 438 | 439 | // There will be Nr rounds. 440 | // The first Nr-1 rounds are identical. 441 | // These Nr-1 rounds are executed in the loop below. 442 | for (round = (Nr - 1); round > 0; --round) 443 | { 444 | InvShiftRows(state); 445 | InvSubBytes(state); 446 | AddRoundKey(round, state, RoundKey); 447 | InvMixColumns(state); 448 | } 449 | 450 | // The last round is given below. 451 | // The MixColumns function is not here in the last round. 452 | InvShiftRows(state); 453 | InvSubBytes(state); 454 | AddRoundKey(0, state, RoundKey); 455 | } 456 | 457 | 458 | /*****************************************************************************/ 459 | /* Public functions: */ 460 | /*****************************************************************************/ 461 | #if defined(ECB) && (ECB == 1) 462 | 463 | 464 | void AES_ECB_encrypt(struct AES_ctx *ctx,const uint8_t* buf) 465 | { 466 | // The next function call encrypts the PlainText with the Key using AES algorithm. 467 | Cipher((state_t*)buf, ctx->RoundKey); 468 | } 469 | 470 | void AES_ECB_decrypt(struct AES_ctx* ctx,const uint8_t* buf) 471 | { 472 | // The next function call decrypts the PlainText with the Key using AES algorithm. 473 | InvCipher((state_t*)buf, ctx->RoundKey); 474 | } 475 | 476 | 477 | #endif // #if defined(ECB) && (ECB == 1) 478 | 479 | 480 | 481 | 482 | 483 | #if defined(CBC) && (CBC == 1) 484 | 485 | 486 | static void XorWithIv(uint8_t* buf, uint8_t* Iv) 487 | { 488 | uint8_t i; 489 | for (i = 0; i < AES_BLOCKLEN; ++i) // The block in AES is always 128bit no matter the key size 490 | { 491 | buf[i] ^= Iv[i]; 492 | } 493 | } 494 | 495 | void AES_CBC_encrypt_buffer(struct AES_ctx *ctx,uint8_t* buf, uint32_t length) 496 | { 497 | uintptr_t i; 498 | uint8_t *Iv = ctx->Iv; 499 | for (i = 0; i < length; i += AES_BLOCKLEN) 500 | { 501 | XorWithIv(buf, Iv); 502 | Cipher((state_t*)buf, ctx->RoundKey); 503 | Iv = buf; 504 | buf += AES_BLOCKLEN; 505 | //printf("Step %d - %d", i/16, i); 506 | } 507 | /* store Iv in ctx for next call */ 508 | memcpy(ctx->Iv, Iv, AES_BLOCKLEN); 509 | } 510 | 511 | void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length) 512 | { 513 | uintptr_t i; 514 | uint8_t storeNextIv[AES_BLOCKLEN]; 515 | for (i = 0; i < length; i += AES_BLOCKLEN) 516 | { 517 | memcpy(storeNextIv, buf, AES_BLOCKLEN); 518 | InvCipher((state_t*)buf, ctx->RoundKey); 519 | XorWithIv(buf, ctx->Iv); 520 | memcpy(ctx->Iv, storeNextIv, AES_BLOCKLEN); 521 | buf += AES_BLOCKLEN; 522 | } 523 | 524 | } 525 | 526 | #endif // #if defined(CBC) && (CBC == 1) 527 | 528 | 529 | 530 | #if defined(CTR) && (CTR == 1) 531 | 532 | /* Symmetrical operation: same function for encrypting as for decrypting. Note any IV/nonce should never be reused with the same key */ 533 | void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length) 534 | { 535 | uint8_t buffer[AES_BLOCKLEN]; 536 | 537 | unsigned i; 538 | int bi; 539 | for (i = 0, bi = AES_BLOCKLEN; i < length; ++i, ++bi) 540 | { 541 | if (bi == AES_BLOCKLEN) /* we need to regen xor compliment in buffer */ 542 | { 543 | 544 | memcpy(buffer, ctx->Iv, AES_BLOCKLEN); 545 | Cipher((state_t*)buffer,ctx->RoundKey); 546 | 547 | /* Increment Iv and handle overflow */ 548 | for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi) 549 | { 550 | /* inc will owerflow */ 551 | if (ctx->Iv[bi] == 255) 552 | { 553 | ctx->Iv[bi] = 0; 554 | continue; 555 | } 556 | ctx->Iv[bi] += 1; 557 | break; 558 | } 559 | bi = 0; 560 | } 561 | 562 | buf[i] = (buf[i] ^ buffer[bi]); 563 | } 564 | } 565 | 566 | #endif // #if defined(CTR) && (CTR == 1) 567 | 568 | -------------------------------------------------------------------------------- /sources/crypto/aes.h: -------------------------------------------------------------------------------- 1 | #ifndef _AES_H_ 2 | #define _AES_H_ 3 | 4 | #include 5 | 6 | // #define the macros below to 1/0 to enable/disable the mode of operation. 7 | // 8 | // CBC enables AES encryption in CBC-mode of operation. 9 | // CTR enables encryption in counter-mode. 10 | // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously. 11 | 12 | // The #ifndef-guard allows it to be configured before #include'ing or at compile time. 13 | #ifndef CBC 14 | #define CBC 1 15 | #endif 16 | 17 | #ifndef ECB 18 | #define ECB 1 19 | #endif 20 | 21 | #ifndef CTR 22 | #define CTR 1 23 | #endif 24 | 25 | 26 | #define AES128 1 27 | //#define AES192 1 28 | //#define AES256 1 29 | 30 | #define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only 31 | 32 | #if defined(AES256) && (AES256 == 1) 33 | #define AES_KEYLEN 32 34 | #define AES_keyExpSize 240 35 | #elif defined(AES192) && (AES192 == 1) 36 | #define AES_KEYLEN 24 37 | #define AES_keyExpSize 208 38 | #else 39 | #define AES_KEYLEN 16 // Key length in bytes 40 | #define AES_keyExpSize 176 41 | #endif 42 | 43 | struct AES_ctx{ 44 | uint8_t RoundKey[AES_keyExpSize]; 45 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 46 | uint8_t Iv[AES_BLOCKLEN]; 47 | #endif 48 | }; 49 | 50 | void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key); 51 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 52 | void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv); 53 | void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv); 54 | #endif 55 | 56 | #if defined(ECB) && (ECB == 1) 57 | // buffer size is exactly AES_BLOCKLEN bytes; 58 | // you need only AES_init_ctx as IV is not used in ECB 59 | // NB: ECB is considered insecure for most uses 60 | void AES_ECB_encrypt(struct AES_ctx* ctx, const uint8_t* buf); 61 | void AES_ECB_decrypt(struct AES_ctx* ctx, const uint8_t* buf); 62 | 63 | #endif // #if defined(ECB) && (ECB == !) 64 | 65 | 66 | #if defined(CBC) && (CBC == 1) 67 | // buffer size MUST be mutile of AES_BLOCKLEN; 68 | // Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme 69 | // NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv() 70 | // no IV should ever be reused with the same key 71 | void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 72 | void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 73 | 74 | #endif // #if defined(CBC) && (CBC == 1) 75 | 76 | 77 | #if defined(CTR) && (CTR == 1) 78 | 79 | // Same function for encrypting as for decrypting. 80 | // IV is incremented for every block, and used after encryption as XOR-compliment for output 81 | // Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme 82 | // NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv() 83 | // no IV should ever be reused with the same key 84 | void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 85 | 86 | #endif // #if defined(CTR) && (CTR == 1) 87 | 88 | 89 | #endif //_AES_H_ 90 | -------------------------------------------------------------------------------- /sources/crypto/base58.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012-2014 Luke Dashjr 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the standard MIT license. See COPYING for more details. 6 | */ 7 | 8 | #ifndef WIN32 9 | #include 10 | #else 11 | #include 12 | #endif 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include "base58.h" 21 | 22 | bool (*b58_sha256_impl)(void *, const void *, size_t) = NULL; 23 | 24 | static const int8_t b58digits_map[] = { 25 | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, 26 | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, 27 | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, 28 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1, 29 | -1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1, 30 | 22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1, 31 | -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46, 32 | 47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1, 33 | }; 34 | 35 | bool b58tobin(void *bin, size_t *binszp, const char *b58, size_t b58sz){ 36 | size_t binsz = *binszp; 37 | const unsigned char *b58u = (void*)b58; 38 | unsigned char *binu = bin; 39 | size_t outisz = (binsz + 3) / 4; 40 | uint32_t outi[outisz]; 41 | uint64_t t; 42 | uint32_t c; 43 | size_t i, j; 44 | uint8_t bytesleft = binsz % 4; 45 | uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0; 46 | unsigned zerocount = 0; 47 | 48 | if (!b58sz) 49 | b58sz = strlen(b58); 50 | 51 | memset(outi, 0, outisz * sizeof(*outi)); 52 | 53 | // Leading zeros, just count 54 | for (i = 0; i < b58sz && b58u[i] == '1'; ++i) 55 | ++zerocount; 56 | 57 | for ( ; i < b58sz; ++i) { 58 | if (b58u[i] & 0x80){ 59 | // High-bit set on invalid digit 60 | //printf("High-bit set on invalid digit\n"); 61 | return false; 62 | } 63 | if (b58digits_map[b58u[i]] == -1){ 64 | // Invalid base58 digit 65 | //printf("Invalid base58 digit = %d\n",i); 66 | return false; 67 | } 68 | c = (unsigned)b58digits_map[b58u[i]]; 69 | for (j = outisz; j--; ) { 70 | t = ((uint64_t)outi[j]) * 58 + c; 71 | c = (t & 0x3f00000000) >> 32; 72 | outi[j] = t & 0xffffffff; 73 | } 74 | if (c){ 75 | // Output number too big (carry to the next int32) 76 | // printf("Output number too big (carry to the next int32)\n"); 77 | return false; 78 | } 79 | if (outi[0] & zeromask){ 80 | // Output number too big (last int32 filled too far) 81 | // printf("Output number too big (last int32 filled too far)\n"); 82 | return false; 83 | } 84 | } 85 | 86 | j = 0; 87 | switch (bytesleft) { 88 | case 3: 89 | *(binu++) = (outi[0] & 0xff0000) >> 16; 90 | case 2: 91 | *(binu++) = (outi[0] & 0xff00) >> 8; 92 | case 1: 93 | *(binu++) = (outi[0] & 0xff); 94 | ++j; 95 | default:break; 96 | } 97 | 98 | for (; j < outisz; ++j) { 99 | *(binu++) = (outi[j] >> 0x18) & 0xff; 100 | *(binu++) = (outi[j] >> 0x10) & 0xff; 101 | *(binu++) = (outi[j] >> 8) & 0xff; 102 | *(binu++) = (outi[j] >> 0) & 0xff; 103 | } 104 | 105 | // Count canonical base58 byte count 106 | binu = bin; 107 | for (i = 0; i < binsz; ++i) { 108 | if (binu[i]) 109 | break; 110 | --*binszp; 111 | } 112 | *binszp += zerocount; 113 | return true; 114 | } 115 | 116 | static 117 | bool my_dblsha256(void *hash, const void *data, size_t datasz){ 118 | uint8_t buf[0x20]; 119 | return b58_sha256_impl(buf, data, datasz) && b58_sha256_impl(hash, buf, sizeof(buf)); 120 | } 121 | 122 | int b58check(const void *bin, size_t binsz, const char *base58str, size_t b58sz){ 123 | unsigned char buf[32]; 124 | const uint8_t *binc = bin; 125 | unsigned i; 126 | if (binsz < 4) 127 | return -4; 128 | if (!my_dblsha256(buf, bin, binsz - 4)) 129 | return -2; 130 | if (memcmp(&binc[binsz - 4], buf, 4)) 131 | return -1; 132 | 133 | // Check number of zeros is correct AFTER verifying checksum (to avoid possibility of accessing base58str beyond the end) 134 | for (i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i) { 135 | // Just finding the end of zeros, nothing to do in loop 136 | } 137 | if (binc[i] == '\0' || base58str[i] == '1') 138 | return -3; 139 | 140 | return binc[0]; 141 | } 142 | 143 | static const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; 144 | 145 | bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz){ 146 | const uint8_t *bin = data; 147 | int carry; 148 | ssize_t i, j, high, zcount = 0; 149 | size_t size; 150 | 151 | while (zcount < binsz && !bin[zcount]) 152 | ++zcount; 153 | 154 | size = (binsz - zcount) * 138 / 100 + 1; 155 | uint8_t buf[size]; 156 | memset(buf, 0, size); 157 | 158 | for (i = zcount, high = size - 1; i < binsz; ++i, high = j) { 159 | for (carry = bin[i], j = size - 1; (j > high) || carry; --j) { 160 | carry += 256 * buf[j]; 161 | buf[j] = carry % 58; 162 | carry /= 58; 163 | } 164 | } 165 | 166 | for (j = 0; j < size && !buf[j]; ++j); 167 | 168 | if (*b58sz <= zcount + size - j) { 169 | *b58sz = zcount + size - j + 1; 170 | return false; 171 | } 172 | 173 | if (zcount) 174 | memset(b58, '1', zcount); 175 | for (i = zcount; j < size; ++i, ++j) 176 | b58[i] = b58digits_ordered[buf[j]]; 177 | b58[i] = '\0'; 178 | *b58sz = i + 1; 179 | 180 | return true; 181 | } 182 | 183 | bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz){ 184 | uint8_t buf[1 + datasz + 0x20]; 185 | uint8_t *hash = &buf[1 + datasz]; 186 | 187 | buf[0] = ver; 188 | memcpy(&buf[1], data, datasz); 189 | if (!my_dblsha256(hash, buf, datasz + 1)) 190 | { 191 | *b58c_sz = 0; 192 | return false; 193 | } 194 | 195 | return b58enc(b58c, b58c_sz, buf, 1 + datasz + 4); 196 | } 197 | -------------------------------------------------------------------------------- /sources/crypto/base58.h: -------------------------------------------------------------------------------- 1 | #ifndef BASE58_H 2 | #define BASE58_H 3 | 4 | #include 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | extern bool (*b58_sha256_impl)(void *, const void *, size_t); 12 | 13 | extern bool b58tobin(void *bin, size_t *binsz, const char *b58, size_t b58sz); 14 | extern int b58check(const void *bin, size_t binsz, const char *b58, size_t b58sz); 15 | 16 | extern bool b58enc(char *b58, size_t *b58sz, const void *bin, size_t binsz); 17 | extern bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz); 18 | 19 | #ifdef __cplusplus 20 | } 21 | #endif 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /sources/crypto/rmd160.c: -------------------------------------------------------------------------------- 1 | // 2 | // rmd160.c 3 | // wif_test 4 | // 5 | // Created by 宋赓 on 2018/3/9. 6 | // Copyright © 2018年 宋赓. All rights reserved. 7 | // 8 | 9 | #define RMDsize 160 10 | 11 | #include "rmd160.h" 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | void MDinit(dword *MDbuf){ 18 | MDbuf[0] = 0x67452301UL; 19 | MDbuf[1] = 0xefcdab89UL; 20 | MDbuf[2] = 0x98badcfeUL; 21 | MDbuf[3] = 0x10325476UL; 22 | MDbuf[4] = 0xc3d2e1f0UL; 23 | 24 | return; 25 | } 26 | 27 | void compress(dword *MDbuf, dword *X){ 28 | dword aa = MDbuf[0], bb = MDbuf[1], cc = MDbuf[2], 29 | dd = MDbuf[3], ee = MDbuf[4]; 30 | dword aaa = MDbuf[0], bbb = MDbuf[1], ccc = MDbuf[2], 31 | ddd = MDbuf[3], eee = MDbuf[4]; 32 | 33 | /* round 1 */ 34 | FF(aa, bb, cc, dd, ee, X[ 0], 11); 35 | FF(ee, aa, bb, cc, dd, X[ 1], 14); 36 | FF(dd, ee, aa, bb, cc, X[ 2], 15); 37 | FF(cc, dd, ee, aa, bb, X[ 3], 12); 38 | FF(bb, cc, dd, ee, aa, X[ 4], 5); 39 | FF(aa, bb, cc, dd, ee, X[ 5], 8); 40 | FF(ee, aa, bb, cc, dd, X[ 6], 7); 41 | FF(dd, ee, aa, bb, cc, X[ 7], 9); 42 | FF(cc, dd, ee, aa, bb, X[ 8], 11); 43 | FF(bb, cc, dd, ee, aa, X[ 9], 13); 44 | FF(aa, bb, cc, dd, ee, X[10], 14); 45 | FF(ee, aa, bb, cc, dd, X[11], 15); 46 | FF(dd, ee, aa, bb, cc, X[12], 6); 47 | FF(cc, dd, ee, aa, bb, X[13], 7); 48 | FF(bb, cc, dd, ee, aa, X[14], 9); 49 | FF(aa, bb, cc, dd, ee, X[15], 8); 50 | 51 | /* round 2 */ 52 | GG(ee, aa, bb, cc, dd, X[ 7], 7); 53 | GG(dd, ee, aa, bb, cc, X[ 4], 6); 54 | GG(cc, dd, ee, aa, bb, X[13], 8); 55 | GG(bb, cc, dd, ee, aa, X[ 1], 13); 56 | GG(aa, bb, cc, dd, ee, X[10], 11); 57 | GG(ee, aa, bb, cc, dd, X[ 6], 9); 58 | GG(dd, ee, aa, bb, cc, X[15], 7); 59 | GG(cc, dd, ee, aa, bb, X[ 3], 15); 60 | GG(bb, cc, dd, ee, aa, X[12], 7); 61 | GG(aa, bb, cc, dd, ee, X[ 0], 12); 62 | GG(ee, aa, bb, cc, dd, X[ 9], 15); 63 | GG(dd, ee, aa, bb, cc, X[ 5], 9); 64 | GG(cc, dd, ee, aa, bb, X[ 2], 11); 65 | GG(bb, cc, dd, ee, aa, X[14], 7); 66 | GG(aa, bb, cc, dd, ee, X[11], 13); 67 | GG(ee, aa, bb, cc, dd, X[ 8], 12); 68 | 69 | /* round 3 */ 70 | HH(dd, ee, aa, bb, cc, X[ 3], 11); 71 | HH(cc, dd, ee, aa, bb, X[10], 13); 72 | HH(bb, cc, dd, ee, aa, X[14], 6); 73 | HH(aa, bb, cc, dd, ee, X[ 4], 7); 74 | HH(ee, aa, bb, cc, dd, X[ 9], 14); 75 | HH(dd, ee, aa, bb, cc, X[15], 9); 76 | HH(cc, dd, ee, aa, bb, X[ 8], 13); 77 | HH(bb, cc, dd, ee, aa, X[ 1], 15); 78 | HH(aa, bb, cc, dd, ee, X[ 2], 14); 79 | HH(ee, aa, bb, cc, dd, X[ 7], 8); 80 | HH(dd, ee, aa, bb, cc, X[ 0], 13); 81 | HH(cc, dd, ee, aa, bb, X[ 6], 6); 82 | HH(bb, cc, dd, ee, aa, X[13], 5); 83 | HH(aa, bb, cc, dd, ee, X[11], 12); 84 | HH(ee, aa, bb, cc, dd, X[ 5], 7); 85 | HH(dd, ee, aa, bb, cc, X[12], 5); 86 | 87 | /* round 4 */ 88 | II(cc, dd, ee, aa, bb, X[ 1], 11); 89 | II(bb, cc, dd, ee, aa, X[ 9], 12); 90 | II(aa, bb, cc, dd, ee, X[11], 14); 91 | II(ee, aa, bb, cc, dd, X[10], 15); 92 | II(dd, ee, aa, bb, cc, X[ 0], 14); 93 | II(cc, dd, ee, aa, bb, X[ 8], 15); 94 | II(bb, cc, dd, ee, aa, X[12], 9); 95 | II(aa, bb, cc, dd, ee, X[ 4], 8); 96 | II(ee, aa, bb, cc, dd, X[13], 9); 97 | II(dd, ee, aa, bb, cc, X[ 3], 14); 98 | II(cc, dd, ee, aa, bb, X[ 7], 5); 99 | II(bb, cc, dd, ee, aa, X[15], 6); 100 | II(aa, bb, cc, dd, ee, X[14], 8); 101 | II(ee, aa, bb, cc, dd, X[ 5], 6); 102 | II(dd, ee, aa, bb, cc, X[ 6], 5); 103 | II(cc, dd, ee, aa, bb, X[ 2], 12); 104 | 105 | /* round 5 */ 106 | JJ(bb, cc, dd, ee, aa, X[ 4], 9); 107 | JJ(aa, bb, cc, dd, ee, X[ 0], 15); 108 | JJ(ee, aa, bb, cc, dd, X[ 5], 5); 109 | JJ(dd, ee, aa, bb, cc, X[ 9], 11); 110 | JJ(cc, dd, ee, aa, bb, X[ 7], 6); 111 | JJ(bb, cc, dd, ee, aa, X[12], 8); 112 | JJ(aa, bb, cc, dd, ee, X[ 2], 13); 113 | JJ(ee, aa, bb, cc, dd, X[10], 12); 114 | JJ(dd, ee, aa, bb, cc, X[14], 5); 115 | JJ(cc, dd, ee, aa, bb, X[ 1], 12); 116 | JJ(bb, cc, dd, ee, aa, X[ 3], 13); 117 | JJ(aa, bb, cc, dd, ee, X[ 8], 14); 118 | JJ(ee, aa, bb, cc, dd, X[11], 11); 119 | JJ(dd, ee, aa, bb, cc, X[ 6], 8); 120 | JJ(cc, dd, ee, aa, bb, X[15], 5); 121 | JJ(bb, cc, dd, ee, aa, X[13], 6); 122 | 123 | /* parallel round 1 */ 124 | JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8); 125 | JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9); 126 | JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9); 127 | JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11); 128 | JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13); 129 | JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15); 130 | JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15); 131 | JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5); 132 | JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7); 133 | JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7); 134 | JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8); 135 | JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11); 136 | JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14); 137 | JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14); 138 | JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12); 139 | JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6); 140 | 141 | /* parallel round 2 */ 142 | III(eee, aaa, bbb, ccc, ddd, X[ 6], 9); 143 | III(ddd, eee, aaa, bbb, ccc, X[11], 13); 144 | III(ccc, ddd, eee, aaa, bbb, X[ 3], 15); 145 | III(bbb, ccc, ddd, eee, aaa, X[ 7], 7); 146 | III(aaa, bbb, ccc, ddd, eee, X[ 0], 12); 147 | III(eee, aaa, bbb, ccc, ddd, X[13], 8); 148 | III(ddd, eee, aaa, bbb, ccc, X[ 5], 9); 149 | III(ccc, ddd, eee, aaa, bbb, X[10], 11); 150 | III(bbb, ccc, ddd, eee, aaa, X[14], 7); 151 | III(aaa, bbb, ccc, ddd, eee, X[15], 7); 152 | III(eee, aaa, bbb, ccc, ddd, X[ 8], 12); 153 | III(ddd, eee, aaa, bbb, ccc, X[12], 7); 154 | III(ccc, ddd, eee, aaa, bbb, X[ 4], 6); 155 | III(bbb, ccc, ddd, eee, aaa, X[ 9], 15); 156 | III(aaa, bbb, ccc, ddd, eee, X[ 1], 13); 157 | III(eee, aaa, bbb, ccc, ddd, X[ 2], 11); 158 | 159 | /* parallel round 3 */ 160 | HHH(ddd, eee, aaa, bbb, ccc, X[15], 9); 161 | HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7); 162 | HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15); 163 | HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11); 164 | HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8); 165 | HHH(ddd, eee, aaa, bbb, ccc, X[14], 6); 166 | HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6); 167 | HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14); 168 | HHH(aaa, bbb, ccc, ddd, eee, X[11], 12); 169 | HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13); 170 | HHH(ddd, eee, aaa, bbb, ccc, X[12], 5); 171 | HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14); 172 | HHH(bbb, ccc, ddd, eee, aaa, X[10], 13); 173 | HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13); 174 | HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7); 175 | HHH(ddd, eee, aaa, bbb, ccc, X[13], 5); 176 | 177 | /* parallel round 4 */ 178 | GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15); 179 | GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5); 180 | GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8); 181 | GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11); 182 | GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14); 183 | GGG(ccc, ddd, eee, aaa, bbb, X[11], 14); 184 | GGG(bbb, ccc, ddd, eee, aaa, X[15], 6); 185 | GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14); 186 | GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6); 187 | GGG(ddd, eee, aaa, bbb, ccc, X[12], 9); 188 | GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12); 189 | GGG(bbb, ccc, ddd, eee, aaa, X[13], 9); 190 | GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12); 191 | GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5); 192 | GGG(ddd, eee, aaa, bbb, ccc, X[10], 15); 193 | GGG(ccc, ddd, eee, aaa, bbb, X[14], 8); 194 | 195 | /* parallel round 5 */ 196 | FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8); 197 | FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5); 198 | FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12); 199 | FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9); 200 | FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12); 201 | FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5); 202 | FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14); 203 | FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6); 204 | FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8); 205 | FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13); 206 | FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6); 207 | FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5); 208 | FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15); 209 | FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13); 210 | FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11); 211 | FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11); 212 | 213 | /* combine results */ 214 | ddd += cc + MDbuf[1]; /* final result for MDbuf[0] */ 215 | MDbuf[1] = MDbuf[2] + dd + eee; 216 | MDbuf[2] = MDbuf[3] + ee + aaa; 217 | MDbuf[3] = MDbuf[4] + aa + bbb; 218 | MDbuf[4] = MDbuf[0] + bb + ccc; 219 | MDbuf[0] = ddd; 220 | 221 | return; 222 | } 223 | 224 | void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen){ 225 | unsigned int i; /* counter */ 226 | dword X[16]; /* message words */ 227 | 228 | memset(X, 0, 16*sizeof(dword)); 229 | 230 | /* put bytes from strptr into X */ 231 | for (i=0; i<(lswlen&63); i++) { 232 | /* byte i goes into word X[i div 4] at pos. 8*(i mod 4) */ 233 | X[i>>2] ^= (dword) *strptr++ << (8 * (i&3)); 234 | } 235 | 236 | /* append the bit m_n == 1 */ 237 | X[(lswlen>>2)&15] ^= (dword)1 << (8*(lswlen&3) + 7); 238 | 239 | if ((lswlen & 63) > 55) { 240 | /* length goes to next block */ 241 | compress(MDbuf, X); 242 | memset(X, 0, 16*sizeof(dword)); 243 | } 244 | 245 | /* append length in bits*/ 246 | X[14] = lswlen << 3; 247 | X[15] = (lswlen >> 29) | (mswlen << 3); 248 | compress(MDbuf, X); 249 | 250 | return; 251 | } 252 | 253 | 254 | byte *RMD(byte *message , int msglen) 255 | /* 256 | * returns RMD(message) 257 | * message should be a string terminated by '\0' 258 | */ 259 | { 260 | dword MDbuf[RMDsize/32]; /* contains (A, B, C, D(, E)) */ 261 | static byte hashcode[RMDsize/8]; /* for final hash-value */ 262 | dword X[16]; /* current 16-word chunk */ 263 | unsigned int i; /* counter */ 264 | dword length; /* length in bytes of message */ 265 | dword nbytes; /* # of bytes not yet processed */ 266 | 267 | /* initialize */ 268 | MDinit(MDbuf); 269 | length = msglen; 270 | //(dword)strlen((char *)message); 271 | //printf("len = %d\n",length); 272 | 273 | /* process message in 16-word chunks */ 274 | for (nbytes=length; nbytes > 63; nbytes-=64) { 275 | for (i=0; i<16; i++) { 276 | X[i] = BYTES_TO_DWORD(message); 277 | message += 4; 278 | } 279 | compress(MDbuf, X); 280 | } /* length mod 64 bytes left */ 281 | 282 | /* finish: */ 283 | MDfinish(MDbuf, message, length, 0); 284 | 285 | for (i=0; i>2]; /* implicit cast to byte */ 287 | hashcode[i+1] = (MDbuf[i>>2] >> 8); /* extracts the 8 least */ 288 | hashcode[i+2] = (MDbuf[i>>2] >> 16); /* significant bits. */ 289 | hashcode[i+3] = (MDbuf[i>>2] >> 24); 290 | } 291 | 292 | return (byte *)hashcode; 293 | } 294 | -------------------------------------------------------------------------------- /sources/crypto/rmd160.h: -------------------------------------------------------------------------------- 1 | // 2 | // rmd160.h 3 | // wif_test 4 | // 5 | // Created by 宋赓 on 2018/3/9. 6 | // Copyright © 2018年 宋赓. All rights reserved. 7 | // 8 | 9 | #ifndef rmd160_h 10 | #define rmd160_h 11 | 12 | #include 13 | 14 | #ifndef RMD160H /* make sure this file is read only once */ 15 | #define RMD160H 16 | 17 | /* typedef 8 and 32 bit types, resp. */ 18 | /* adapt these, if necessary, 19 | for your operating system and compiler */ 20 | typedef unsigned char byte; 21 | typedef unsigned int dword; 22 | 23 | /* if this line causes a compiler error, 24 | adapt the defintion of dword above */ 25 | typedef int the_correct_size_was_chosen [sizeof (dword) == 4? 1: -1]; 26 | 27 | /* macro definitions */ 28 | 29 | /* collect four bytes into one word: */ 30 | #define BYTES_TO_DWORD(strptr) \ 31 | (((dword) *((strptr)+3) << 24) | \ 32 | ((dword) *((strptr)+2) << 16) | \ 33 | ((dword) *((strptr)+1) << 8) | \ 34 | ((dword) *(strptr))) 35 | 36 | /* ROL(x, n) cyclically rotates x over n bits to the left */ 37 | /* x must be of an unsigned 32 bits type and 0 <= n < 32. */ 38 | #define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 39 | 40 | /* the five basic functions F(), G() and H() */ 41 | #define F(x, y, z) ((x) ^ (y) ^ (z)) 42 | #define G(x, y, z) (((x) & (y)) | (~(x) & (z))) 43 | #define H(x, y, z) (((x) | ~(y)) ^ (z)) 44 | #define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) 45 | #define J(x, y, z) ((x) ^ ((y) | ~(z))) 46 | 47 | /* the ten basic operations FF() through III() */ 48 | #define FF(a, b, c, d, e, x, s) {\ 49 | (a) += F((b), (c), (d)) + (x);\ 50 | (a) = ROL((a), (s)) + (e);\ 51 | (c) = ROL((c), 10);\ 52 | } 53 | #define GG(a, b, c, d, e, x, s) {\ 54 | (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\ 55 | (a) = ROL((a), (s)) + (e);\ 56 | (c) = ROL((c), 10);\ 57 | } 58 | #define HH(a, b, c, d, e, x, s) {\ 59 | (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\ 60 | (a) = ROL((a), (s)) + (e);\ 61 | (c) = ROL((c), 10);\ 62 | } 63 | #define II(a, b, c, d, e, x, s) {\ 64 | (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\ 65 | (a) = ROL((a), (s)) + (e);\ 66 | (c) = ROL((c), 10);\ 67 | } 68 | #define JJ(a, b, c, d, e, x, s) {\ 69 | (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\ 70 | (a) = ROL((a), (s)) + (e);\ 71 | (c) = ROL((c), 10);\ 72 | } 73 | #define FFF(a, b, c, d, e, x, s) {\ 74 | (a) += F((b), (c), (d)) + (x);\ 75 | (a) = ROL((a), (s)) + (e);\ 76 | (c) = ROL((c), 10);\ 77 | } 78 | #define GGG(a, b, c, d, e, x, s) {\ 79 | (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\ 80 | (a) = ROL((a), (s)) + (e);\ 81 | (c) = ROL((c), 10);\ 82 | } 83 | #define HHH(a, b, c, d, e, x, s) {\ 84 | (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\ 85 | (a) = ROL((a), (s)) + (e);\ 86 | (c) = ROL((c), 10);\ 87 | } 88 | #define III(a, b, c, d, e, x, s) {\ 89 | (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\ 90 | (a) = ROL((a), (s)) + (e);\ 91 | (c) = ROL((c), 10);\ 92 | } 93 | #define JJJ(a, b, c, d, e, x, s) {\ 94 | (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\ 95 | (a) = ROL((a), (s)) + (e);\ 96 | (c) = ROL((c), 10);\ 97 | } 98 | 99 | /* function prototypes */ 100 | void MDinit(dword *MDbuf); 101 | /* 102 | * initializes MDbuffer to "magic constants" 103 | */ 104 | void compress(dword *MDbuf, dword *X); 105 | /* 106 | * the compression function. 107 | * transforms MDbuf using message bytes X[0] through X[15] 108 | */ 109 | void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen); 110 | /* 111 | * puts bytes from strptr into X and pad out; appends length 112 | * and finally, compresses the last block(s) 113 | * note: length in bits == 8 * (lswlen + 2^32 mswlen). 114 | * note: there are (lswlen mod 64) bytes left in strptr. 115 | */ 116 | byte * RMD(byte *message , int msglen); 117 | 118 | #endif /* RMD160H */ 119 | 120 | #endif /* rmd160_h */ 121 | -------------------------------------------------------------------------------- /sources/crypto/sha256.c: -------------------------------------------------------------------------------- 1 | /* ******************************************************************** 2 | * Filename: sha256.c 3 | * Author: Brad Conte (brad AT bradconte.com) 4 | * Copyright: 5 | * Disclaimer: This code is presented "as is" without any guarantees. 6 | * Details: Implementation of the SHA-256 hashing algorithm. 7 | SHA-256 is one of the three algorithms in the SHA2 8 | specification. The others, SHA-384 and SHA-512, are not 9 | offered in this implementation. 10 | Algorithm specification can be found here: 11 | * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf 12 | This implementation uses little endian byte order. 13 | *********************************************************************/ 14 | 15 | /* ************************** HEADER FILES ***************************/ 16 | #include 17 | #include 18 | #include "sha256.h" 19 | 20 | /* ***************************** MACROS ******************************/ 21 | #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b)))) 22 | #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b)))) 23 | 24 | #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z))) 25 | #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 26 | #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22)) 27 | #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25)) 28 | #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3)) 29 | #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10)) 30 | 31 | /* *************************** VARIABLES *****************************/ 32 | static const WORD k[64] = { 33 | 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, 34 | 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, 35 | 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, 36 | 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, 37 | 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, 38 | 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, 39 | 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, 40 | 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 41 | }; 42 | 43 | /* ********************** FUNCTION DEFINITIONS ***********************/ 44 | void sha256_transform(SHA256_CTX *ctx, const BYTE data[]){ 45 | WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; 46 | 47 | for (i = 0, j = 0; i < 16; ++i, j += 4) 48 | m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]); 49 | for ( ; i < 64; ++i) 50 | m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; 51 | 52 | a = ctx->state[0]; 53 | b = ctx->state[1]; 54 | c = ctx->state[2]; 55 | d = ctx->state[3]; 56 | e = ctx->state[4]; 57 | f = ctx->state[5]; 58 | g = ctx->state[6]; 59 | h = ctx->state[7]; 60 | 61 | for (i = 0; i < 64; ++i) { 62 | t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i]; 63 | t2 = EP0(a) + MAJ(a,b,c); 64 | h = g; 65 | g = f; 66 | f = e; 67 | e = d + t1; 68 | d = c; 69 | c = b; 70 | b = a; 71 | a = t1 + t2; 72 | } 73 | 74 | ctx->state[0] += a; 75 | ctx->state[1] += b; 76 | ctx->state[2] += c; 77 | ctx->state[3] += d; 78 | ctx->state[4] += e; 79 | ctx->state[5] += f; 80 | ctx->state[6] += g; 81 | ctx->state[7] += h; 82 | } 83 | 84 | void sha256_init(SHA256_CTX *ctx){ 85 | ctx->datalen = 0; 86 | ctx->bitlen = 0; 87 | ctx->state[0] = 0x6a09e667; 88 | ctx->state[1] = 0xbb67ae85; 89 | ctx->state[2] = 0x3c6ef372; 90 | ctx->state[3] = 0xa54ff53a; 91 | ctx->state[4] = 0x510e527f; 92 | ctx->state[5] = 0x9b05688c; 93 | ctx->state[6] = 0x1f83d9ab; 94 | ctx->state[7] = 0x5be0cd19; 95 | } 96 | 97 | void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len){ 98 | WORD i; 99 | for (i = 0; i < len; ++i) { 100 | ctx->data[ctx->datalen] = data[i]; 101 | ctx->datalen++; 102 | if (ctx->datalen == 64) { 103 | sha256_transform(ctx, ctx->data); 104 | ctx->bitlen += 512; 105 | ctx->datalen = 0; 106 | } 107 | } 108 | } 109 | 110 | void sha256_final(SHA256_CTX *ctx, BYTE hash[]){ 111 | WORD i; 112 | i = ctx->datalen; 113 | // Pad whatever data is left in the buffer. 114 | if (ctx->datalen < 56) { 115 | ctx->data[i++] = 0x80; 116 | while (i < 56) 117 | ctx->data[i++] = 0x00; 118 | } else { 119 | ctx->data[i++] = 0x80; 120 | while (i < 64) 121 | ctx->data[i++] = 0x00; 122 | sha256_transform(ctx, ctx->data); 123 | memset(ctx->data, 0, 56); 124 | } 125 | 126 | // Append to the padding the total message's length in bits and transform. 127 | ctx->bitlen += ctx->datalen * 8; 128 | ctx->data[63] = ctx->bitlen; 129 | ctx->data[62] = ctx->bitlen >> 8; 130 | ctx->data[61] = ctx->bitlen >> 16; 131 | ctx->data[60] = ctx->bitlen >> 24; 132 | ctx->data[59] = ctx->bitlen >> 32; 133 | ctx->data[58] = ctx->bitlen >> 40; 134 | ctx->data[57] = ctx->bitlen >> 48; 135 | ctx->data[56] = ctx->bitlen >> 56; 136 | sha256_transform(ctx, ctx->data); 137 | 138 | // Since this implementation uses little endian byte ordering and SHA uses big endian, 139 | // reverse all the bytes when copying the final state to the output hash. 140 | for (i = 0; i < 4; ++i) { 141 | hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; 142 | hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; 143 | hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; 144 | hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; 145 | hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; 146 | hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff; 147 | hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff; 148 | hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /sources/crypto/sha256.h: -------------------------------------------------------------------------------- 1 | /* ******************************************************************** 2 | * Filename: sha256.h 3 | * Author: Brad Conte (brad AT bradconte.com) 4 | * Copyright: 5 | * Disclaimer: This code is presented "as is" without any guarantees. 6 | * Details: Defines the API for the corresponding SHA1 implementation. 7 | *********************************************************************/ 8 | 9 | #ifndef SHA256_H 10 | #define SHA256_H 11 | 12 | /* ************************** HEADER FILES ***************************/ 13 | #include 14 | 15 | /* ***************************** MACROS ******************************/ 16 | #define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest 17 | 18 | /* *************************** DATA TYPES ****************************/ 19 | typedef unsigned char BYTE; // 8-bit byte 20 | typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines 21 | 22 | typedef struct { 23 | BYTE data[64]; 24 | WORD datalen; 25 | unsigned long long bitlen; 26 | WORD state[8]; 27 | } SHA256_CTX; 28 | 29 | /* ********************** FUNCTION DECLARATIONS **********************/ 30 | void sha256_init(SHA256_CTX *ctx); 31 | void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len); 32 | void sha256_final(SHA256_CTX *ctx, BYTE hash[]); 33 | 34 | #endif // SHA256_H 35 | -------------------------------------------------------------------------------- /sources/crypto/uECC.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */ 2 | 3 | #ifndef _MICRO_ECC_H_ 4 | #define _MICRO_ECC_H_ 5 | 6 | #include 7 | 8 | /* Platform selection options. 9 | If uECC_PLATFORM is not defined, the code will try to guess it based on compiler macros. 10 | Possible values for uECC_PLATFORM are defined below: */ 11 | #define uECC_arch_other 0 12 | #define uECC_x86 1 13 | #define uECC_x86_64 2 14 | #define uECC_arm 3 15 | #define uECC_arm_thumb 4 16 | #define uECC_avr 5 17 | #define uECC_arm_thumb2 6 18 | 19 | /* If desired, you can define uECC_WORD_SIZE as appropriate for your platform (1, 4, or 8 bytes). 20 | If uECC_WORD_SIZE is not explicitly defined then it will be automatically set based on your 21 | platform. */ 22 | 23 | /* Inline assembly options. 24 | uECC_asm_none - Use standard C99 only. 25 | uECC_asm_small - Use GCC inline assembly for the target platform (if available), optimized for 26 | minimum size. 27 | uECC_asm_fast - Use GCC inline assembly optimized for maximum speed. */ 28 | #define uECC_asm_none 0 29 | #define uECC_asm_small 1 30 | #define uECC_asm_fast 2 31 | #ifndef uECC_ASM 32 | #define uECC_ASM uECC_asm_fast 33 | #endif 34 | 35 | /* Curve selection options. */ 36 | #define uECC_secp160r1 1 37 | #define uECC_secp192r1 2 38 | #define uECC_secp256r1 3 39 | #define uECC_secp256k1 4 40 | #define uECC_secp224r1 5 41 | #ifndef uECC_CURVE 42 | #define uECC_CURVE uECC_secp256k1 43 | #endif 44 | 45 | /* uECC_SQUARE_FUNC - If enabled (defined as nonzero), this will cause a specific function to be 46 | used for (scalar) squaring instead of the generic multiplication function. This will make things 47 | faster by about 8% but increases the code size. */ 48 | #ifndef uECC_SQUARE_FUNC 49 | #define uECC_SQUARE_FUNC 1 50 | #endif 51 | 52 | #define uECC_CONCAT1(a, b) a##b 53 | #define uECC_CONCAT(a, b) uECC_CONCAT1(a, b) 54 | 55 | #define uECC_size_1 20 /* secp160r1 */ 56 | #define uECC_size_2 24 /* secp192r1 */ 57 | #define uECC_size_3 32 /* secp256r1 */ 58 | #define uECC_size_4 32 /* secp256k1 */ 59 | #define uECC_size_5 28 /* secp224r1 */ 60 | 61 | #define uECC_BYTES uECC_CONCAT(uECC_size_, uECC_CURVE) 62 | 63 | #ifdef __cplusplus 64 | extern "C" 65 | { 66 | #endif 67 | 68 | /* uECC_RNG_Function type 69 | The RNG function should fill 'size' random bytes into 'dest'. It should return 1 if 70 | 'dest' was filled with random data, or 0 if the random data could not be generated. 71 | The filled-in values should be either truly random, or from a cryptographically-secure PRNG. 72 | 73 | A correctly functioning RNG function must be set (using uECC_set_rng()) before calling 74 | uECC_make_key() or uECC_sign(). 75 | 76 | Setting a correctly functioning RNG function improves the resistance to side-channel attacks 77 | for uECC_shared_secret() and uECC_sign_deterministic(). 78 | 79 | A correct RNG function is set by default when building for Windows, Linux, or OS X. 80 | If you are building on another POSIX-compliant system that supports /dev/random or /dev/urandom, 81 | you can define uECC_POSIX to use the predefined RNG. For embedded platforms there is no predefined 82 | RNG function; you must provide your own. 83 | */ 84 | typedef int (*uECC_RNG_Function)(uint8_t *dest, unsigned size); 85 | 86 | /* uECC_set_rng() function. 87 | Set the function that will be used to generate random bytes. The RNG function should 88 | return 1 if the random data was generated, or 0 if the random data could not be generated. 89 | 90 | On platforms where there is no predefined RNG function (eg embedded platforms), this must 91 | be called before uECC_make_key() or uECC_sign() are used. 92 | 93 | Inputs: 94 | rng_function - The function that will be used to generate random bytes. 95 | */ 96 | void uECC_set_rng(uECC_RNG_Function rng_function); 97 | 98 | /* uECC_make_key() function. 99 | Create a public/private key pair. 100 | 101 | Outputs: 102 | public_key - Will be filled in with the public key. 103 | private_key - Will be filled in with the private key. 104 | 105 | Returns 1 if the key pair was generated successfully, 0 if an error occurred. 106 | */ 107 | int uECC_make_key(uint8_t public_key[uECC_BYTES*2], uint8_t private_key[uECC_BYTES]); 108 | 109 | /* uECC_shared_secret() function. 110 | Compute a shared secret given your secret key and someone else's public key. 111 | Note: It is recommended that you hash the result of uECC_shared_secret() before using it for 112 | symmetric encryption or HMAC. 113 | 114 | Inputs: 115 | public_key - The public key of the remote party. 116 | private_key - Your private key. 117 | 118 | Outputs: 119 | secret - Will be filled in with the shared secret value. 120 | 121 | Returns 1 if the shared secret was generated successfully, 0 if an error occurred. 122 | */ 123 | int uECC_shared_secret(const uint8_t public_key[uECC_BYTES*2], 124 | const uint8_t private_key[uECC_BYTES], 125 | uint8_t secret[uECC_BYTES]); 126 | 127 | /* uECC_sign() function. 128 | Generate an ECDSA signature for a given hash value. 129 | 130 | Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and pass it in to 131 | this function along with your private key. 132 | 133 | Inputs: 134 | private_key - Your private key. 135 | message_hash - The hash of the message to sign. 136 | 137 | Outputs: 138 | signature - Will be filled in with the signature value. 139 | 140 | Returns 1 if the signature generated successfully, 0 if an error occurred. 141 | */ 142 | int uECC_sign(const uint8_t private_key[uECC_BYTES], 143 | const uint8_t message_hash[uECC_BYTES], 144 | uint8_t signature[uECC_BYTES*2]); 145 | 146 | //return the recid 147 | int uECC_sign_forbc(const uint8_t private_key[uECC_BYTES], 148 | const uint8_t message_hash[uECC_BYTES], 149 | uint8_t signature[uECC_BYTES*2]); 150 | /* uECC_HashContext structure. 151 | This is used to pass in an arbitrary hash function to uECC_sign_deterministic(). 152 | The structure will be used for multiple hash computations; each time a new hash 153 | is computed, init_hash() will be called, followed by one or more calls to 154 | update_hash(), and finally a call to finish_hash() to prudoce the resulting hash. 155 | 156 | The intention is that you will create a structure that includes uECC_HashContext 157 | followed by any hash-specific data. For example: 158 | 159 | typedef struct SHA256_HashContext { 160 | uECC_HashContext uECC; 161 | SHA256_CTX ctx; 162 | } SHA256_HashContext; 163 | 164 | void init_SHA256(uECC_HashContext *base) { 165 | SHA256_HashContext *context = (SHA256_HashContext *)base; 166 | SHA256_Init(&context->ctx); 167 | } 168 | 169 | void update_SHA256(uECC_HashContext *base, 170 | const uint8_t *message, 171 | unsigned message_size) { 172 | SHA256_HashContext *context = (SHA256_HashContext *)base; 173 | SHA256_Update(&context->ctx, message, message_size); 174 | } 175 | 176 | void finish_SHA256(uECC_HashContext *base, uint8_t *hash_result) { 177 | SHA256_HashContext *context = (SHA256_HashContext *)base; 178 | SHA256_Final(hash_result, &context->ctx); 179 | } 180 | 181 | ... when signing ... 182 | { 183 | uint8_t tmp[32 + 32 + 64]; 184 | SHA256_HashContext ctx = {{&init_SHA256, &update_SHA256, &finish_SHA256, 64, 32, tmp}}; 185 | uECC_sign_deterministic(key, message_hash, &ctx.uECC, signature); 186 | } 187 | */ 188 | typedef struct uECC_HashContext { 189 | void (*init_hash)(struct uECC_HashContext *context); 190 | void (*update_hash)(struct uECC_HashContext *context, 191 | const uint8_t *message, 192 | unsigned message_size); 193 | void (*finish_hash)(struct uECC_HashContext *context, uint8_t *hash_result); 194 | unsigned block_size; /* Hash function block size in bytes, eg 64 for SHA-256. */ 195 | unsigned result_size; /* Hash function result size in bytes, eg 32 for SHA-256. */ 196 | uint8_t *tmp; /* Must point to a buffer of at least (2 * result_size + block_size) bytes. */ 197 | } uECC_HashContext; 198 | 199 | /* uECC_sign_deterministic() function. 200 | Generate an ECDSA signature for a given hash value, using a deterministic algorithm 201 | (see RFC 6979). You do not need to set the RNG using uECC_set_rng() before calling 202 | this function; however, if the RNG is defined it will improve resistance to side-channel 203 | attacks. 204 | 205 | Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and pass it in to 206 | this function along with your private key and a hash context. 207 | 208 | Inputs: 209 | private_key - Your private key. 210 | message_hash - The hash of the message to sign. 211 | hash_context - A hash context to use. 212 | 213 | Outputs: 214 | signature - Will be filled in with the signature value. 215 | 216 | Returns 1 if the signature generated successfully, 0 if an error occurred. 217 | */ 218 | int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES], 219 | const uint8_t message_hash[uECC_BYTES], 220 | uECC_HashContext *hash_context, 221 | uint8_t signature[uECC_BYTES*2]); 222 | 223 | /* uECC_verify() function. 224 | Verify an ECDSA signature. 225 | 226 | Usage: Compute the hash of the signed data using the same hash as the signer and 227 | pass it to this function along with the signer's public key and the signature values (r and s). 228 | 229 | Inputs: 230 | public_key - The signer's public key 231 | hash - The hash of the signed data. 232 | signature - The signature value. 233 | 234 | Returns 1 if the signature is valid, 0 if it is invalid. 235 | */ 236 | int uECC_verify(const uint8_t public_key[uECC_BYTES*2], 237 | const uint8_t hash[uECC_BYTES], 238 | const uint8_t signature[uECC_BYTES*2]); 239 | 240 | /* uECC_compress() function. 241 | Compress a public key. 242 | 243 | Inputs: 244 | public_key - The public key to compress. 245 | 246 | Outputs: 247 | compressed - Will be filled in with the compressed public key. 248 | */ 249 | void uECC_compress(const uint8_t public_key[uECC_BYTES*2], uint8_t compressed[uECC_BYTES+1]); 250 | 251 | /* uECC_decompress() function. 252 | Decompress a compressed public key. 253 | 254 | Inputs: 255 | compressed - The compressed public key. 256 | 257 | Outputs: 258 | public_key - Will be filled in with the decompressed public key. 259 | */ 260 | void uECC_decompress(const uint8_t compressed[uECC_BYTES+1], uint8_t public_key[uECC_BYTES*2]); 261 | 262 | /* uECC_valid_public_key() function. 263 | Check to see if a public key is valid. 264 | 265 | Note that you are not required to check for a valid public key before using any other uECC 266 | functions. However, you may wish to avoid spending CPU time computing a shared secret or 267 | verifying a signature using an invalid public key. 268 | 269 | Inputs: 270 | public_key - The public key to check. 271 | 272 | Returns 1 if the public key is valid, 0 if it is invalid. 273 | */ 274 | int uECC_valid_public_key(const uint8_t public_key[uECC_BYTES*2]); 275 | 276 | /* uECC_compute_public_key() function. 277 | Compute the corresponding public key for a private key. 278 | 279 | Inputs: 280 | private_key - The private key to compute the public key for 281 | 282 | Outputs: 283 | public_key - Will be filled in with the corresponding public key 284 | 285 | Returns 1 if the key was computed successfully, 0 if an error occurred. 286 | */ 287 | int uECC_compute_public_key(const uint8_t private_key[uECC_BYTES], 288 | uint8_t public_key[uECC_BYTES * 2]); 289 | 290 | 291 | /* uECC_bytes() function. 292 | Returns the value of uECC_BYTES. Helpful for foreign-interfaces to higher-level languages. 293 | */ 294 | int uECC_bytes(void); 295 | 296 | /* uECC_curve() function. 297 | Returns the value of uECC_CURVE. Helpful for foreign-interfaces to higher-level languages. 298 | */ 299 | int uECC_curve(void); 300 | 301 | 302 | int getRecId(const uint8_t private_key[uECC_BYTES], 303 | const uint8_t hash[uECC_BYTES], 304 | const uint8_t signature[uECC_BYTES*2]); 305 | 306 | int checkSignature(const uint8_t* public_key, 307 | const uint8_t* hash, 308 | const uint8_t* signature); 309 | 310 | #ifdef __cplusplus 311 | } /* end of extern "C" */ 312 | #endif 313 | 314 | #endif /* _MICRO_ECC_H_ */ 315 | --------------------------------------------------------------------------------