|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Apple Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * |
| 13 | + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +let passwordStrings = [ |
| 27 | + '123456', |
| 28 | + 'p@assw0rd', |
| 29 | + 'qwerty', |
| 30 | + '汉字漢字', |
| 31 | + 'كلمة المرور', |
| 32 | + 'Z7ihQxGE93', |
| 33 | + '0pjTjkrnsM', |
| 34 | + '6Kg3AmWnVc', |
| 35 | +]; |
| 36 | + |
| 37 | +// Emscripten doesn't have a way to manage your pointers for you so these will free them when the wrapper dies. |
| 38 | +const mallocRegistry = new FinalizationRegistry((ptr) => Module._free(ptr)); |
| 39 | + |
| 40 | +class CString { |
| 41 | + constructor(string) { |
| 42 | + this.ptr = Module.stringToNewUTF8(string); |
| 43 | + this.length = Module._strlen(this.ptr); |
| 44 | + mallocRegistry.register(this, this.ptr); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +class MallocPtr { |
| 49 | + constructor(size) { |
| 50 | + this.ptr = Module._malloc(size); |
| 51 | + this.size = size; |
| 52 | + mallocRegistry.register(this, this.ptr); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +const tCost = 2; |
| 57 | +const mCost = 1024; |
| 58 | +const parallelism = 1; |
| 59 | +const argon2NumberOfTypes = 2; |
| 60 | +const version = 0x13; |
| 61 | +const saltLength = 12; |
| 62 | + |
| 63 | +class Benchmark { |
| 64 | + async runIteration() { |
| 65 | + // Instantiate the Wasm module before the first run. |
| 66 | + if (!Module._argon2_hash) { |
| 67 | + await setupModule(Module); |
| 68 | + } |
| 69 | + |
| 70 | + for (let i = 0; i < passwordStrings.length; ++i) |
| 71 | + this.hashAndVerify(passwordStrings[i], i % argon2NumberOfTypes); |
| 72 | + } |
| 73 | + |
| 74 | + randomSalt() { |
| 75 | + let result = new MallocPtr(saltLength); |
| 76 | + const numWords = saltLength / 4; |
| 77 | + for (let i = 0; i < numWords; ++i) |
| 78 | + Module.HEAPU32[result.ptr + i] = Math.floor(Math.random() * (2 ** 32)); |
| 79 | + return result; |
| 80 | + } |
| 81 | + |
| 82 | + hashAndVerify(password, argon2Type) { |
| 83 | + password = new CString(password); |
| 84 | + let salt = this.randomSalt(); |
| 85 | + this.hashBuffer = new MallocPtr(24); |
| 86 | + this.encodedBuffer = new MallocPtr(Module._argon2_encodedlen(tCost, mCost, parallelism, saltLength, this.hashBuffer.size, argon2Type) + 1); |
| 87 | + |
| 88 | + let status = Module._argon2_hash(tCost, mCost, parallelism, password.ptr, password.length, salt.ptr, salt.size, this.hashBuffer.ptr, this.hashBuffer.size, this.encodedBuffer.ptr, this.encodedBuffer.size, argon2Type, version); |
| 89 | + if (status !== 0) |
| 90 | + throw new Error(`argon2_hash exited with status: ${status} (${Module.UTF8ToString(Module._argon2_error_message(status))})`); |
| 91 | + |
| 92 | + status = Module._argon2_verify(this.encodedBuffer.ptr, password.ptr, password.length, argon2Type); |
| 93 | + if (status !== 0) |
| 94 | + throw new Error(`argon2_verify exited with status: ${status} (${Module.UTF8ToString(Module._argon2_error_message(status))})`); |
| 95 | + } |
| 96 | +} |
0 commit comments