├── .gitignore ├── Cargo.toml ├── LICENSE ├── Makefile ├── README.md ├── res └── transform.wasm ├── src └── main.rs └── wasm-sign ├── Cargo.toml ├── Makefile ├── README.md └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | /target/ 4 | 5 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 6 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock 7 | Cargo.lock 8 | 9 | # These are backup files generated by rustfmt 10 | **/*.rs.bk 11 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wasm-sign-cli" 3 | version = "0.2.0" 4 | authors = ["Frank Rehberger "] 5 | description = "WebAssembly module signing and verification tool to proof authenticity and integrity of WebAssembly bytecodes. The signature is attached as Custom-Section to the end of th module. The signed module can be transmitted over network. Recipients parsing the signed module will 'see' an additional Custom-Section of type 0 and name 'signature'. The Signature adds an overhead of 118 bytes." 6 | 7 | license = "Apache-2.0" 8 | readme = "README.md" 9 | keywords = ["webasm", "security", "signature"] 10 | documentation = "https://github.com/frehberg/wasm-sign" 11 | homepage = "https://github.com/frehberg/wasm-sign" 12 | repository = "https://github.com/frehberg/wasm-sign" 13 | 14 | [[bin]] 15 | name = "wasm-sign" 16 | path = "src/main.rs" 17 | 18 | [dependencies] 19 | env_logger = { version = "0.5", default-features = false } 20 | getopts = "0.2" 21 | parity-wasm = "0.24.1" 22 | wasm-sign = { path = "wasm-sign", version = "0.2" } 23 | log = "0.4" 24 | base64 = "0.9" 25 | 26 | # requires libssl-dev being installed 27 | # on debian do 28 | # sudo apt-get install pkg-config libssl-dev 29 | # If you want to use non-default installation, set the 30 | # environment variable 31 | # OPENSSL_DIR= 32 | openssl = "0.10.3" 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | PRIV_FILE=signerkey.pem 3 | PUB_FILE=signerkey.pub.pem 4 | 5 | PRIV_384_FILE=signerkey384.pem 6 | PUB_384_FILE=signerkey384.pub.pem 7 | 8 | WASM_FILE=res/transform.wasm 9 | SIGNED_WASM_FILE=res/transform-signed.wasm 10 | 11 | $(PRIV_FILE): 12 | openssl ecparam -name secp256k1 -genkey -noout -out $(PRIV_FILE) 13 | 14 | $(PRIV_384_FILE): 15 | openssl ecparam -name secp384r1 -genkey -noout -out $(PRIV_384_FILE) 16 | 17 | $(PUB_FILE): $(PRIV_FILE) 18 | openssl ec -in $(PRIV_FILE) -pubout -outform pem -out $(PUB_FILE) 19 | 20 | $(PUB_384_FILE): $(PRIV_384_FILE) 21 | openssl ec -in $(PRIV_384_FILE) -pubout -outform pem -out $(PUB_384_FILE) 22 | 23 | all: $(PRIV_FILE) $(PRIV_384_FILE) $(PUB_FILE) $(PUB_384_FILE) 24 | 25 | test: $(PRIV_FILE) $(PUB_FILE) 26 | make -C wasm-sign test 27 | cargo run -- -k $(PRIV_FILE) $(WASM_FILE) $(SIGNED_WASM_FILE) 28 | cargo run -- -v -k $(PUB_FILE) $(SIGNED_WASM_FILE) 29 | 30 | asn1parse: 31 | openssl asn1parse -in $(PRIV_FILE) -inform pem 32 | openssl asn1parse -in $(PRIV_384_FILE) -inform pem 33 | 34 | list-curves: 35 | openssl ecparam -list_curves 36 | 37 | clean: 38 | rm -f *.pem 39 | 40 | 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WebAssembly Module Signature 2 | 3 | This repository describing the design and high-level overview of the WebAssembly Module Signature (WAMS) 4 | 5 | ## Overview 6 | 7 | The WebAssembly Module Signature shall proof authenticity and integrity of the WebAssembly module, as file or parsed. 8 | 9 | ## Requirements 10 | 11 | The WebAssembly Module Signature shall provide the following features 12 | 13 | - It shall be possible to sign a WebAssembly Module file without parsing 14 | - The signature signs the serialized, binary form of data in rest and data in transit. 15 | - A module shall be signed as is, completely, including all custom-sections. 16 | - The module sections shall not be re-ordered to meet a canonical form. 17 | - All module signatures shall have equal byte-size. 18 | - It shall be possible to verify a signature without parsing of the module 19 | - It shall be possible with simple tools to split a WebAssembly module into both parts, the signature and the signed content 20 | - Signatures shall use the ECDSA algorithm with SHA256. 21 | - The public key may be publicly available and may be distributed using DNS-TXT-Records. 22 | 23 | ## Proposal 24 | 25 | - The WebAssembly module signature is a Custom-Section with type id 0, the section name must be formed by 9 characters, so all signatures have equal byte-size 26 | - A module shall contain just one signature (may be extended in future) 27 | - A signature signs all sections of the module, as is. 28 | - The custom-section with name "signature" (9 chars) is the default signature. 29 | - The payload of the custom-section contains the ECDSA signature; the complete section has octet-size 118. 30 | - New signature-sections shall be appended to the end of a module only. 31 | - Removing the last 118 bytes from a module-bytecode, the signature-section is cut off. 32 | - The tool shall support secp256k1 in initial version and in future versions Ed25519 and secp384r1 (hence the reserved bytes at end) 33 | 34 | 35 | Each signature section ist formed of a sequence of 118 octets: 36 | 37 | Fields | Bytes 38 | ------------------------ | ----------------------------------------------------------------------------- 39 | Section Type (Custom): | `0` 40 | Section Size: | `116` 41 | Section Name Length: | `9` 42 | Section Name Octets [9]: | `[115, 105, 103, 110, 97, 116, 117, 114, 101]` (read as "signature") 43 | Signature Type : | `0` which stands for ECDSA/SHA256 with max digest length of max. 72 bytes 44 | Signature length: | Single byte value 72 or less 45 | Signature: | `[...]` 46 | Padding bytes: | 0..33 padding bytes filling extending the digest-length to up to 104 bytes (secp384r1) 47 | 48 | The signature is always attached to the end of a WASM-file. If receiving a signed WASM-file, the last 118 characters can be cut off to get the WASM-Signature section. The following indeces permit verification of the signature using command line tools or Javascript, without the need to parse the WASM-module-bytecodes: 49 | 50 | - **Index 0..11** Fixed byte-sequence `[0, 116, 9, 115, 105, 103, 110, 97, 116, 117, 114, 101]` 51 | - **Index 12** SIGNATURE_TYPE (the only valid value is '0' for curve secp256k1/SHA256 for now) 52 | - **Index 13** DIGEST_DATA_LENGTH, may range between 65..104 (if using secp256k1 usually a value of 70 or 71 or 72) 53 | - **Index 14** DIGEST_DATA_START, first byte of digest 54 | - **Index 14+DIGEST_DATA_LENGTH**, end of the digest 55 | 56 | In case the digest has byte size 72 (secp256k1) the preamble looks like (followed by the ECDSA digest): 57 | ``` 58 | [0, 116, 9, 115, 105, 103, 110, 97, 116, 117, 114, 101, 0, 72 ] 59 | ``` 60 | 61 | The digest is calculated using ciphers secp256k1/SHA256. 62 | Trailing padding bytes fill up to total length of 118 bytes. 63 | 64 | ## Usage 65 | 66 | ### Private Key Generation 67 | This is the key that must be kept secret and is used to sign your WASM files. 68 | ``` 69 | openssl ecparam -name secp256k1 -genkey -noout -out signerkey.pem 70 | ``` 71 | 72 | ### Public Key Generation 73 | This is the key that should be published or embedded in your application. 74 | ``` 75 | openssl ec -in signerkey.pem -pubout -outform pem -out signerkey.pub.pem 76 | ``` 77 | 78 | ### Signing the WASM file 79 | The command line tool returns with exit code 0 on success, otherwise with error code 1 80 | ``` 81 | wasm-sign -k signerkey.pem module.wasm signed-module.wasm 82 | ``` 83 | 84 | ### Verifying the WASM file 85 | The command line tool returns with exit code 0 on success, otherwise with error code 1 86 | ``` 87 | wasm-sign -v -k signerkey.pub.pem signed-module.wasm 88 | ``` 89 | 90 | ### Signature Example 91 | Output of `hexdump -C signed-module.wasm`: 92 | 93 | ``` 94 | 00000110 95 | 00000120 xx xx xx xx xx xx xx 00 74 09 73 69 67 6e 61 74 |j$......t.signat| 96 | 00000130 75 72 65 00 47 30 45 02 20 58 20 79 4b 91 52 39 |ure.G0E. X yK.R9| 97 | 00000140 75 52 69 f0 cf dc 81 8a 7c d8 ab 08 1d 49 ab c2 |uRi.....|....I..| 98 | 00000150 fa 19 79 f5 03 92 e9 9b 87 02 21 00 8f a0 ad 5a |..y.......!....Z| 99 | 00000160 f2 55 ce cf c6 ed 82 15 5a ed 7a 47 43 d9 e5 4e |.U......Z.zGC..N| 100 | 00000170 fd 74 79 e8 80 4e 82 9c 08 eb 8e 9a 00 00 00 00 |.ty..N..........| 101 | 00000180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 102 | 00000190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |.............. | 103 | ``` 104 | 105 | ## Build 106 | ``` 107 | cargo build --release 108 | ``` 109 | ## Unit Test 110 | ``` 111 | cargo test 112 | ``` 113 | ## End2End Test with command line tool 114 | ``` 115 | make test 116 | ``` 117 | -------------------------------------------------------------------------------- /res/transform.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frehberg/wasm-sign/1abc5c6a0f605f058f1a8d5f4ad714b6616a554c/res/transform.wasm -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate env_logger; 2 | extern crate getopts; 3 | extern crate base64; 4 | extern crate wasm_sign; 5 | extern crate openssl; 6 | extern crate parity_wasm; 7 | 8 | use std::env; 9 | use std::fs::File; 10 | use std::io::{Read, Write}; 11 | use std::vec::Vec; 12 | use openssl::ec::EcKey; 13 | use openssl::pkey::PKey; 14 | use openssl::error::ErrorStack; 15 | use getopts::Options; 16 | //use parity_wasm::elements::Error as ParityWasmError; 17 | // use parity_wasm::deserialize_buffer; 18 | use parity_wasm::elements::{ 19 | Module, 20 | Error as ParityError, 21 | Deserialize, 22 | }; 23 | 24 | use wasm_sign::{Error, Config}; 25 | 26 | 27 | 28 | /// parse EC private key from byte-sequence in PEM format 29 | #[allow(dead_code)] 30 | fn to_private_key(pemkey: &[u8]) -> Result, ErrorStack> 31 | { 32 | let eckey = EcKey::private_key_from_pem(pemkey)?; 33 | let pkey = PKey::from_ec_key(eckey); 34 | return pkey; 35 | } 36 | 37 | /// parse EC public key from byte-sequence in PEM format 38 | #[allow(dead_code)] 39 | fn to_public_key(pemkey: &[u8]) -> Result, ErrorStack> 40 | { 41 | let pkey = PKey::public_key_from_pem(&pemkey); 42 | return pkey; 43 | } 44 | 45 | #[allow(dead_code)] 46 | fn to_module(mut bytecode: &[u8]) -> Result 47 | { 48 | Module::deserialize(&mut bytecode) 49 | } 50 | 51 | 52 | fn main() { 53 | env_logger::init(); 54 | 55 | let mut opts = Options::new(); 56 | opts.optflag("v", "verify", "verify the signature of a wasm file"); 57 | opts.reqopt("k", "key", "key file", "KEY"); 58 | opts.optflag("d", "der", "key is DER form"); 59 | opts.optflag("p", "pem", "key is PEM form [default]"); 60 | opts.optopt("o", "out", "set output file name", "NAME"); 61 | 62 | opts.optflag("h", "help", "print this help menu"); 63 | let args: Vec<_> = env::args().collect(); 64 | let program = args[0].clone(); 65 | let matches = match opts.parse(&args[1..]) { 66 | Ok(m) => m, 67 | Err(f) => panic!(f.to_string()), 68 | }; 69 | if matches.opt_present("h") { 70 | return print_usage(&program, opts); 71 | } 72 | let (input, output) = match matches.free.len() { 73 | 0 => return print_usage(&program, opts), 74 | 1 => { 75 | let input = matches.free[0].clone(); 76 | match matches.opt_str("o") { 77 | None => (input.clone(), input), 78 | Some(s) => (input, s), 79 | } 80 | } 81 | 2 => (matches.free[0].clone(), matches.free[1].clone()), 82 | _ => return print_usage(&program, opts), 83 | }; 84 | 85 | let keyfile = matches.opt_str("k").unwrap(); 86 | 87 | let mut bytecode = Vec::new(); 88 | File::open(&input) 89 | .unwrap() 90 | .read_to_end(&mut bytecode) 91 | .unwrap(); 92 | 93 | let mut keybytes = Vec::new(); 94 | File::open(&keyfile) 95 | .unwrap() 96 | .read_to_end(&mut keybytes) 97 | .unwrap(); 98 | 99 | // Validate WASM module file 100 | let _module = to_module(bytecode.as_mut()) 101 | .expect("Error, WASM module malformed"); 102 | 103 | let config = Config::new(); 104 | 105 | if matches.opt_present("v") { 106 | // verify signature 107 | let pkey = match to_public_key(keybytes.as_ref()) { 108 | Ok(key) => key, 109 | _ => { 110 | println!("Error: failed to parse public key from {}\n", keyfile); 111 | 112 | return print_usage(&program, opts); 113 | } 114 | }; 115 | let retcode = match config.verify(&pkey, bytecode.as_ref()) { 116 | Ok(true) => 0, 117 | Ok(false) => { 118 | println!("no valid signature\n"); 119 | 1 120 | } 121 | Err(Error::BadCipher) => { 122 | println!("Error, bad key cipher\n"); 123 | 1 124 | } 125 | Err(Error::BadVersion) => { 126 | println!("Error, bad encoded version in signature\n"); 127 | 1 128 | } 129 | Err(Error::NoSignature) => { 130 | println!("Error, no signature\n"); 131 | 1 132 | } 133 | Err(Error::MalformedModule) => { 134 | println!("Error, malformed WASM module {}\n", input); 135 | 1 136 | } 137 | _ => { 138 | println!("Error, internal error\n"); 139 | 1 140 | } 141 | }; 142 | ::std::process::exit(retcode); 143 | } else { 144 | // Sign 145 | let pkey = match to_private_key(keybytes.as_ref()) { 146 | Ok(key) => key, 147 | _ => { 148 | println!("Error: failed to parse private key from {}\n", keyfile); 149 | 150 | return print_usage(&program, opts); 151 | } 152 | }; 153 | let retcode = match config.sign(&pkey, bytecode.as_ref()) { 154 | Ok(signed_bytecode) => { 155 | let mut owned = signed_bytecode; 156 | match File::create(&output) { 157 | Ok(file) => { 158 | let mut owned_file = file; 159 | match owned_file.write_all(owned.as_mut()) { 160 | Ok(_) => 0, 161 | _ => { 162 | println!("Error, write failure to {}\n", output); 163 | 1 164 | } 165 | } 166 | } 167 | _ => { 168 | println!("Error, could not create output file {}\n", output); 169 | 1 170 | } 171 | } 172 | } 173 | Err(Error::BadCipher) => { 174 | println!("Error, bad key cipher\n"); 175 | 1 176 | } 177 | Err(Error::MalformedModule) => { 178 | println!("Error, malformed wasm byte code\n"); 179 | 1 180 | } 181 | _ => { 182 | println!("Error, internal error\n"); 183 | 1 184 | } 185 | }; 186 | 187 | ::std::process::exit(retcode); 188 | } 189 | } 190 | 191 | fn print_usage(program: &str, opts: Options) { 192 | let brief = format!("Usage: {} [options] [OUTPUT]", program); 193 | print!("{}", opts.usage(&brief)); 194 | println!( 195 | " 196 | A postprocessing command to sign/verify a wasm file. 197 | 198 | Usage of this command typically looks like: 199 | 200 | # Sign a wasm file and write output to another file 201 | wasm-sign -k PRIVATE_KEY -o signed-foo.wasm foo.wasm 202 | 203 | # Verify the signature of a wasm file 204 | wasm-sign -v -k PUBLIC_KEY -o signed-foo.wasm foo.wasm 205 | 206 | Please reports bugs to https://github.com/alexcrichton/wasm-gc if you find 207 | them! 208 | " 209 | ); 210 | } 211 | 212 | -------------------------------------------------------------------------------- /wasm-sign/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wasm-sign" 3 | version = "0.2.0" 4 | authors = ["Frank Rehberger "] 5 | description = "WebAssembly module signing and verification tool to proof authenticity and integrity of WebAssembly bytecodes. The signature is attached as Custom-Section to the end of th module. The signed module can be transmitted over network. Recipients parsing the signed module will 'see' an additional Custom-Section of type 0 and name 'signature'. The Signature adds an overhead of 118 bytes." 6 | 7 | license = "Apache-2.0" 8 | readme = "README.md" 9 | keywords = ["webasm", "security", "signature"] 10 | documentation = "https://github.com/frehberg/wasm-sign" 11 | homepage = "https://github.com/frehberg/wasm-sign" 12 | repository = "https://github.com/frehberg/wasm-sign" 13 | 14 | [dependencies] 15 | env_logger = { version = "0.5", default-features = false } 16 | getopts = "0.2" 17 | parity-wasm = "0.24.1" 18 | log = "0.4" 19 | rustc-demangle = "0.1.5" 20 | base64 = "0.9" 21 | 22 | # requires libssl-dev being installed 23 | # on debian do 24 | # sudo apt-get install pkg-config libssl-dev 25 | # If you want to use non-default installation, set the 26 | # environment variable 27 | # OPENSSL_DIR= 28 | openssl = "0.10.3" 29 | -------------------------------------------------------------------------------- /wasm-sign/Makefile: -------------------------------------------------------------------------------- 1 | 2 | PRIV_FILE=signerkey.pem 3 | PUB_FILE=signerkey.pub.pem 4 | 5 | PRIV_384_FILE=signerkey384.pem 6 | PUB_384_FILE=signerkey384.pub.pem 7 | 8 | WASM_FILE=res/transform.wasm 9 | SIGNED_WASM_FILE=res/transform-signed.wasm 10 | 11 | $(PRIV_FILE): 12 | openssl ecparam -name secp256k1 -genkey -noout -out $(PRIV_FILE) 13 | 14 | $(PRIV_384_FILE): 15 | openssl ecparam -name secp384r1 -genkey -noout -out $(PRIV_384_FILE) 16 | 17 | $(PUB_FILE): $(PRIV_FILE) 18 | openssl ec -in $(PRIV_FILE) -pubout -outform pem -out $(PUB_FILE) 19 | 20 | $(PUB_384_FILE): $(PRIV_384_FILE) 21 | openssl ec -in $(PRIV_384_FILE) -pubout -outform pem -out $(PUB_384_FILE) 22 | 23 | test: 24 | cargo test 25 | 26 | asn1parse: 27 | openssl asn1parse -in $(PRIV_FILE) -inform pem 28 | openssl asn1parse -in $(PRIV_384_FILE) -inform pem 29 | 30 | list-curves: 31 | openssl ecparam -list_curves 32 | 33 | clean: 34 | rm -f *.pem 35 | 36 | 37 | -------------------------------------------------------------------------------- /wasm-sign/README.md: -------------------------------------------------------------------------------- 1 | # WebAssembly Module Signature 2 | 3 | This repository describing the design and high-level overview of the WebAssembly Module Signature (WAMS) 4 | 5 | ## Overview 6 | 7 | The WebAssembly Module Signature shall proof authenticity and integrity of the WebAssembly module, as file or parsed. 8 | 9 | ## Requirements 10 | 11 | The WebAssembly Module Signature shall provide the following features 12 | 13 | - It shall be possible to sign a WebAssembly Module file without parsing 14 | - The signature signs the serialized, binary form of data in rest and data in transit. 15 | - A module shall be signed as is, completely, including all custom-sections. 16 | - The module sections shall not be re-ordered to meet a canonical form. 17 | - All module signatures shall have equal byte-size. 18 | - It shall be possible to verify a signature without parsing of the module 19 | - It shall be possible with simple tools to split a WebAssembly module into both parts, the signature and the signed content 20 | - Signatures shall use the ECDSA algorithm with SHA256. 21 | - The public key may be publicly available and may be distributed using DNS-TXT-Records. 22 | 23 | ## Proposal 24 | 25 | - The WebAssembly module signature is a Custom-Section with type id 0, the section name must be formed by 9 characters, so all signatures have equal byte-size 26 | - A module shall contain just one signature (may be extended in future) 27 | - A signature signs all sections of the module, as is. 28 | - The custom-section with name "signature" (9 chars) is the default signature. 29 | - The payload of the custom-section contains the ECDSA signature; the complete section has octet-size 118. 30 | - New signature-sections shall be appended to the end of a module only. 31 | - Removing the last 118 bytes from a module-bytecode, the signature-section is cut off. 32 | - The tool shall support secp256k1 in initial version and in future versions Ed25519 and secp384r1 (hence the reserved bytes at end) 33 | 34 | 35 | Each signature section ist formed of a sequence of 118 octets: 36 | 37 | Fields | Bytes 38 | ------------------------ | ----------------------------------------------------------------------------- 39 | Section Type (Custom): | `0` 40 | Section Size: | `116` 41 | Section Name Length: | `9` 42 | Section Name Octets [9]: | `[115, 105, 103, 110, 97, 116, 117, 114, 101]` (read as "signature") 43 | Signature Type : | `0` which stands for ECDSA/SHA256 with max digest length of max. 72 bytes 44 | Signature length: | Single byte value 72 or less 45 | Signature: | `[...]` 46 | Padding bytes: | 0..33 padding bytes filling extending the digest-length to up to 104 bytes (secp384r1) 47 | 48 | The signature is always attached to the end of a WASM-file. If receiving a signed WASM-file, the last 118 characters can be cut off to get the WASM-Signature section. The following indeces permit verification of the signature using command line tools or Javascript, without the need to parse the WASM-module-bytecodes: 49 | 50 | - **Index 0..11** Fixed byte-sequence `[0, 116, 9, 115, 105, 103, 110, 97, 116, 117, 114, 101]` 51 | - **Index 12** SIGNATURE_TYPE (the only valid value is '0' for curve secp256k1/SHA256 for now) 52 | - **Index 13** DIGEST_DATA_LENGTH, may range between 65..104 (if using secp256k1 usually a value of 70 or 71 or 72) 53 | - **Index 14** DIGEST_DATA_START, first byte of digest 54 | - **Index 14+DIGEST_DATA_LENGTH**, end of the digest 55 | 56 | In case the digest has byte size 72 (secp256k1) the preamble looks like (followed by the ECDSA digest): 57 | ``` 58 | [0, 116, 9, 115, 105, 103, 110, 97, 116, 117, 114, 101, 0, 72 ] 59 | ``` 60 | 61 | The digest is calculated using ciphers secp256k1/SHA256. 62 | Trailing padding bytes fill up to total length of 118 bytes. 63 | 64 | ## Usage 65 | 66 | ### Private Key Generation 67 | This is the key that must be kept secret and is used to sign your WASM files. 68 | ``` 69 | openssl ecparam -name secp256k1 -genkey -noout -out signerkey.pem 70 | ``` 71 | 72 | ### Public Key Generation 73 | This is the key that should be published or embedded in your application. 74 | ``` 75 | openssl ec -in signerkey.pem -pubout -outform pem -out signerkey.pub.pem 76 | ``` 77 | 78 | ### Signing the WASM file 79 | The command line tool returns with exit code 0 on success, otherwise with error code 1 80 | ``` 81 | wasm-sign -k signerkey.pem module.wasm signed-module.wasm 82 | ``` 83 | 84 | ### Verifying the WASM file 85 | The command line tool returns with exit code 0 on success, otherwise with error code 1 86 | ``` 87 | wasm-sign -v -k signerkey.pub.pem signed-module.wasm 88 | ``` 89 | 90 | ### Signature Example 91 | Output of `hexdump -C signed-module.wasm`: 92 | 93 | ``` 94 | 00000110 95 | 00000120 xx xx xx xx xx xx xx 00 74 09 73 69 67 6e 61 74 |j$......t.signat| 96 | 00000130 75 72 65 00 47 30 45 02 20 58 20 79 4b 91 52 39 |ure.G0E. X yK.R9| 97 | 00000140 75 52 69 f0 cf dc 81 8a 7c d8 ab 08 1d 49 ab c2 |uRi.....|....I..| 98 | 00000150 fa 19 79 f5 03 92 e9 9b 87 02 21 00 8f a0 ad 5a |..y.......!....Z| 99 | 00000160 f2 55 ce cf c6 ed 82 15 5a ed 7a 47 43 d9 e5 4e |.U......Z.zGC..N| 100 | 00000170 fd 74 79 e8 80 4e 82 9c 08 eb 8e 9a 00 00 00 00 |.ty..N..........| 101 | 00000180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 102 | 00000190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |.............. | 103 | ``` 104 | 105 | ## Build 106 | ``` 107 | cargo build --release 108 | ``` 109 | ## Unit Test 110 | ``` 111 | cargo test 112 | ``` 113 | ## End2End Test with command line tool 114 | ``` 115 | make test 116 | ``` 117 | -------------------------------------------------------------------------------- /wasm-sign/src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate env_logger; 2 | extern crate getopts; 3 | extern crate openssl; 4 | extern crate base64; 5 | extern crate parity_wasm; 6 | 7 | use std::vec::Vec; 8 | use openssl::sign::{Signer, Verifier}; 9 | use openssl::ec::EcKey; 10 | use openssl::pkey::PKey; 11 | use openssl::error::ErrorStack; 12 | use openssl::hash::MessageDigest; 13 | 14 | //use parity_wasm::elements::Error as ParityWasmError; 15 | // use parity_wasm::deserialize_buffer; 16 | use parity_wasm::elements::{ 17 | Module, 18 | Error as ParityError, 19 | Section, 20 | CustomSection, 21 | Serialize, 22 | Deserialize, 23 | }; 24 | 25 | const DEFAULT_SECTION_NAME: &str = "signature"; 26 | const CUSTOM_SECTION_HEAD_SIZE: usize = 12; 27 | const CUSTOM_SECTION_PAYLOAD_START: usize = CUSTOM_SECTION_HEAD_SIZE; 28 | 29 | const PAYLOAD_VER_IDX: usize = 0; 30 | const PAYLOAD_LEN_IDX: usize = 1; 31 | const PAYLOAD_ECDSA_IDX: usize = 2; 32 | const ECDSA_SIGNATURE_SIZE_MAX: usize = 104; // secp256k1 72 bytes, secp384r1 104 bytes 33 | const CUSTOM_SECTION_SIZE: usize = CUSTOM_SECTION_HEAD_SIZE + 2 + ECDSA_SIGNATURE_SIZE_MAX; // 118 34 | const VERSION: u8 = 0; 35 | const PAD: u8 = 0; 36 | 37 | 38 | #[derive(Debug)] 39 | pub enum Error { NoSignature, BadVersion, BadCipher, MalformedKey, MalformedModule, HasSignature, InternalError } 40 | 41 | /// Configuration structure 42 | pub struct Config { 43 | name: String, 44 | } 45 | 46 | 47 | /// parse EC private key from byte-sequence in PEM format 48 | #[allow(dead_code)] 49 | fn to_private_key(pemkey: &[u8]) -> Result, ErrorStack> 50 | { 51 | let eckey = EcKey::private_key_from_pem(pemkey)?; 52 | let pkey = PKey::from_ec_key(eckey); 53 | return pkey; 54 | } 55 | 56 | /// parse EC public key from byte-sequence in PEM format 57 | #[allow(dead_code)] 58 | fn to_public_key(pemkey: &[u8]) -> Result, ErrorStack> 59 | { 60 | let pkey = PKey::public_key_from_pem(&pemkey); 61 | return pkey; 62 | } 63 | 64 | #[allow(dead_code)] 65 | fn to_module(mut bytecode: &[u8]) -> Result 66 | { 67 | Module::deserialize(&mut bytecode) 68 | } 69 | 70 | fn create_signer<'a>(pkey: &'a PKey ) -> Result, Error> 71 | { 72 | match Signer::new(MessageDigest::sha256(), &pkey) { 73 | Ok(signer) => match signer.len() { 74 | Ok(len) if len <= ECDSA_SIGNATURE_SIZE_MAX => Result::Ok(signer), 75 | Ok(len) if len > ECDSA_SIGNATURE_SIZE_MAX => Result::Err(Error::BadCipher), 76 | _ => Result::Err(Error::BadCipher), 77 | }, 78 | _ => Result::Err(Error::BadCipher), 79 | } 80 | } 81 | 82 | fn create_verifier<'a>(pkey: &'a PKey) -> Result, Error> 83 | { 84 | match Verifier::new(MessageDigest::sha256(), &pkey) { 85 | Ok(verifier) => Result::Ok(verifier), 86 | _ => Result::Err(Error::BadCipher), 87 | } 88 | } 89 | 90 | /// create the signature 91 | fn sign<'a>(signer: &mut Signer<'a>, bytecode: &[u8]) -> Result, Error> 92 | { 93 | match signer.update(bytecode.as_ref()) { 94 | Ok(_) => (), 95 | Err(_) => return Result::Err(Error::InternalError), 96 | }; 97 | 98 | match signer.sign_to_vec() { 99 | Ok(vec) => Ok(vec), 100 | Err(_) => Err(Error::InternalError) 101 | } 102 | } 103 | 104 | /// verify the signature 105 | fn verify<'a>(verifier: &mut Verifier<'a>, bytecode: &[u8], signature: &[u8]) -> Result 106 | { 107 | match verifier.update(bytecode.as_ref()) { 108 | Ok(_) => (), 109 | Err(_) => return Result::Err(Error::InternalError), 110 | }; 111 | 112 | match verifier.verify(&signature) { 113 | Ok(verdict) => Ok(verdict), 114 | Err(_) => Err(Error::InternalError), 115 | } 116 | } 117 | 118 | 119 | impl Config { 120 | pub fn new() -> Config { 121 | Config { 122 | name: DEFAULT_SECTION_NAME.to_owned(), 123 | } 124 | } 125 | 126 | /// Signing the bytecode using the private key 127 | pub fn sign<'a>(&self, pkey: &'a PKey, bytecode: &[u8]) -> Result, Error> 128 | { 129 | let mut result = bytecode.to_owned(); 130 | 131 | let mut signer = create_signer(&pkey)?; 132 | 133 | let mut signature = sign(&mut signer, bytecode.as_ref())?; 134 | 135 | let required_padding = ECDSA_SIGNATURE_SIZE_MAX - signature.len(); 136 | 137 | let mut custom: CustomSection = Default::default(); 138 | 139 | // In CustomSection set the name, append the signature with padding 140 | // append to end of module bytecode 141 | custom.name_mut().push_str(self.name.as_ref()); 142 | 143 | custom.payload_mut().push(VERSION); 144 | assert!(signature.len() < 256); 145 | custom.payload_mut().push(signature.len() as u8); 146 | custom.payload_mut().append(&mut signature); 147 | 148 | // add trailing padding 149 | for _i in 0..required_padding { 150 | custom.payload_mut().push(PAD); 151 | } 152 | 153 | let section = Section::Custom(custom); 154 | 155 | let mut section_bytecode = Vec::with_capacity(CUSTOM_SECTION_SIZE); 156 | 157 | if section.serialize(&mut section_bytecode).is_err() { 158 | return Result::Err(Error::InternalError); 159 | } 160 | 161 | result.append(section_bytecode.as_mut()); 162 | 163 | return Result::Ok(result); 164 | } 165 | 166 | /// Verifying the signature at the end of the bytecode using the public key 167 | pub fn verify<'a>(&self, pkey: &'a PKey, signed_bytecode: &[u8]) -> Result 168 | { 169 | let mut verifier = create_verifier(&pkey)?; 170 | 171 | if signed_bytecode.len() <= CUSTOM_SECTION_SIZE { 172 | return Result::Err(Error::MalformedModule); 173 | } 174 | 175 | let (bytecode, custom_section_bytecode) = 176 | signed_bytecode.split_at(signed_bytecode.len() - CUSTOM_SECTION_SIZE); 177 | 178 | let custom_section_payload = 179 | &custom_section_bytecode[CUSTOM_SECTION_PAYLOAD_START..]; 180 | 181 | let ver: &u8 = &custom_section_payload[PAYLOAD_VER_IDX]; 182 | let len = custom_section_payload[PAYLOAD_LEN_IDX] as usize; 183 | 184 | let ecdsa_data = 185 | &custom_section_payload[PAYLOAD_ECDSA_IDX..(len + PAYLOAD_ECDSA_IDX)]; 186 | 187 | if *ver != VERSION || len > ECDSA_SIGNATURE_SIZE_MAX { 188 | return Result::Err(Error::NoSignature); 189 | } 190 | 191 | verify(&mut verifier, bytecode, ecdsa_data) 192 | } 193 | } 194 | 195 | 196 | 197 | #[cfg(test)] 198 | mod tests { 199 | use super::*; 200 | use base64::decode; 201 | 202 | 203 | const PRIVATE_KEY_SECP256K1: &[u8] = b"-----BEGIN EC PRIVATE KEY----- 204 | MHQCAQEEILz/A1lrSfoGyINIiy0Ip7OTNHCbpH5W89235ulbVneOoAcGBSuBBAAK 205 | oUQDQgAEEglOsMyoScjUMuUomECq1U6gaPUEfOmvOYBjxBEdd8fN5ZfFHYeQwNAs 206 | +kK96P1/ODkqQTTKv18kDanmsavXYw== 207 | -----END EC PRIVATE KEY----- 208 | "; 209 | 210 | const PUBLIC_KEY_SECP256K1: &[u8] = b"-----BEGIN PUBLIC KEY----- 211 | MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEEglOsMyoScjUMuUomECq1U6gaPUEfOmv 212 | OYBjxBEdd8fN5ZfFHYeQwNAs+kK96P1/ODkqQTTKv18kDanmsavXYw== 213 | -----END PUBLIC KEY-----"; 214 | 215 | 216 | const PRIVATE_KEY_SECP384R1: &[u8] = b"-----BEGIN EC PRIVATE KEY----- 217 | MIGkAgEBBDDp7Z74viFQDPzWuk4tt5SahPbyCm6WQbU9HdMg4jK9OfzAd/YpBDju 218 | Xu7YstRrJYSgBwYFK4EEACKhZANiAARDf5u2T0SdjCsOsNbxBCidgozBeWHZ3luE 219 | aIFsOGDGOgiDynKaTAUhf7oOvMhJi0r32ocfATgPyPso7fvLvjJKJ7PaHRxErqZg 220 | wAqxaRDUW47eolEjWjhgvljw8K2Ib4s= 221 | -----END EC PRIVATE KEY----- 222 | "; 223 | 224 | const PUBLIC_KEY_SECP384R1: &[u8] = b"-----BEGIN PUBLIC KEY----- 225 | MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEQ3+btk9EnYwrDrDW8QQonYKMwXlh2d5b 226 | hGiBbDhgxjoIg8pymkwFIX+6DrzISYtK99qHHwE4D8j7KO37y74ySiez2h0cRK6m 227 | YMAKsWkQ1FuO3qJRI1o4YL5Y8PCtiG+L 228 | -----END PUBLIC KEY----- 229 | "; 230 | 231 | 232 | const WASM_BASE64: &str = 233 | "AGFzbQEAAAAADAZkeWxpbmuAgMACAAGKgICAAAJgAn9/AX9gAAACwYCAgAAEA2VudgptZW1vcnlCYXNl\ 234 | A38AA2VudgZtZW1vcnkCAIACA2VudgV0YWJsZQFwAAADZW52CXRhYmxlQmFzZQN/AAOEgICAAAMAAQEGi\ 235 | 4CAgAACfwFBAAt/AUEACwejgICAAAIKX3RyYW5zZm9ybQAAEl9fcG9zdF9pbnN0YW50aWF0ZQACCYGAgI\ 236 | AAAArpgICAAAPBgICAAAECfwJ/IABBAEoEQEEAIQIFIAAPCwNAIAEgAmoiAywAAEHpAEYEQCADQfkAOgA\ 237 | ACyACQQFqIgIgAEcNAAsgAAsLg4CAgAAAAQuVgICAAAACQCMAJAIjAkGAgMACaiQDEAELCw=="; 238 | 239 | #[test] 240 | fn test_to_private_key_secp256k1() { 241 | assert!(PRIVATE_KEY_SECP256K1.len() > 0); 242 | assert!(to_private_key(PRIVATE_KEY_SECP256K1).is_ok()); 243 | } 244 | 245 | #[test] 246 | fn test_to_public_key_secp256k1() { 247 | assert!(PUBLIC_KEY_SECP256K1.len() > 0); 248 | assert!(to_public_key(PUBLIC_KEY_SECP256K1).is_ok()); 249 | } 250 | 251 | #[test] 252 | fn test_to_private_key_secp384r1() { 253 | assert!(PRIVATE_KEY_SECP384R1.len() > 0); 254 | assert!(to_private_key(PRIVATE_KEY_SECP384R1).is_ok()); 255 | 256 | let pkey = to_private_key(PRIVATE_KEY_SECP384R1).unwrap(); 257 | let signer = Signer::new(MessageDigest::sha384(), &pkey).unwrap(); 258 | println!("secp384r1-len={}", signer.len().unwrap()); 259 | 260 | } 261 | 262 | #[test] 263 | fn test_to_public_key_secp384r1() { 264 | assert!(PUBLIC_KEY_SECP384R1.len() > 0); 265 | assert!(to_public_key(PUBLIC_KEY_SECP384R1).is_ok()); 266 | 267 | } 268 | 269 | #[test] 270 | fn test_wasm_parse() { 271 | assert!(WASM_BASE64.len() > 0); 272 | 273 | let mut bytecode = decode(WASM_BASE64).unwrap(); 274 | assert!(bytecode.len() > 0); 275 | 276 | let module_result = to_module(bytecode.as_mut()); 277 | assert!(module_result.is_ok()); 278 | } 279 | 280 | #[test] 281 | fn test_basic_sign_verify() { 282 | assert!(WASM_BASE64.len() > 0); 283 | 284 | let mut bytecode = decode(WASM_BASE64).unwrap(); 285 | assert!(bytecode.len() > 0); 286 | 287 | let module_result = to_module(bytecode.as_mut()); 288 | assert!(module_result.is_ok()); 289 | 290 | let privkey = to_private_key(PRIVATE_KEY_SECP256K1).unwrap(); 291 | 292 | let pubkey = to_public_key(PUBLIC_KEY_SECP256K1).unwrap(); 293 | 294 | let mut signer = create_signer(&privkey).unwrap(); 295 | let mut verifier = create_verifier(&pubkey).unwrap(); 296 | 297 | let signature = 298 | sign(&mut signer, bytecode.as_ref()); 299 | 300 | assert!(signature.is_ok()); 301 | 302 | assert!(verify(&mut verifier, 303 | bytecode.as_ref(), 304 | signature.unwrap().as_ref()) 305 | .unwrap()); 306 | } 307 | 308 | #[test] 309 | fn test_wasm_sign_verify() { 310 | assert!(WASM_BASE64.len() > 0); 311 | 312 | let mut bytecode = decode(WASM_BASE64).unwrap(); 313 | assert!(bytecode.len() > 0); 314 | 315 | let module_result = to_module(bytecode.as_mut()); 316 | assert!(module_result.is_ok()); 317 | 318 | let privkey = to_private_key(PRIVATE_KEY_SECP256K1).unwrap(); 319 | 320 | let pubkey = to_public_key(PUBLIC_KEY_SECP256K1).unwrap(); 321 | 322 | let config = Config::new(); 323 | 324 | let signed_bytecode = 325 | config.sign(&privkey, bytecode.as_ref()).unwrap(); 326 | 327 | assert!(to_module(signed_bytecode.as_ref()).is_ok()); 328 | 329 | assert_eq!( 330 | to_module(bytecode.as_ref()).unwrap().sections().len() + 1, 331 | to_module(signed_bytecode.as_ref()).unwrap().sections().len()); 332 | 333 | config.verify(&pubkey, signed_bytecode.as_ref()).unwrap(); 334 | } 335 | } --------------------------------------------------------------------------------