From 2929a689a35c01ef21657e41743e325fb8ee07bd Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Sat, 30 May 2020 11:39:37 +0800 Subject: [PATCH] Update JavaScript lib --- js/base64.js | 111 ++++++++++ js/sm2.js | 613 +++++++++++++++++++++++++++++++++++++++++++++++++++ js/sm3.js | 30 +++ 3 files changed, 754 insertions(+) create mode 100644 js/base64.js create mode 100644 js/sm2.js diff --git a/js/base64.js b/js/base64.js new file mode 100644 index 00000000..c405f888 --- /dev/null +++ b/js/base64.js @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the GmSSL Project. + * (http://gmssl.org/)" + * + * 4. The name "GmSSL Project" must not be used to endorse or promote + * products derived from this software without prior written + * permission. For written permission, please contact + * guanzhi1980@gmail.com. + * + * 5. Products derived from this software may not be called "GmSSL" + * nor may "GmSSL" appear in their names without prior written + * permission of the GmSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the GmSSL Project + * (http://gmssl.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +const BASE64_MAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + +function base64_encode(bytes) { + var i = 0; j = 0; + var append = bytes.length % 3 > 0 ? 3 - bytes.length % 3 : 0; + for (i = 0; i < append; i++) { + bytes[bytes.length] = 0; + } + var b64 = ""; + for (i = 0; j < bytes.length; j += 3) { + if (j > 0 && j % 57 == 0) { + b64 += '\n'; + } + b64 += BASE64_MAP[bytes[j] >> 2] + + BASE64_MAP[(bytes[j] & 3) << 4 | bytes[j+1] >> 4] + + BASE64_MAP[(bytes[j+1] & 15) << 2 | bytes[j+2] >> 6] + + BASE64_MAP[bytes[j+2] & 63]; + } + for (i = 0; i < append; i++) { + b64 += '='; + } + return b64; +} + +function base64_decode(input) { + var i = 0, j = 0; + input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); + var append = input.length % 4; + if (append > 2) { + return null; + } + for (i = 0; i < append; i++) { + if (input.charAt(input.length - i - 1) != '=') { + return null; + } + } + var output = new Array((input.length - append) * 3 / 4); + var enc1, enc2, enc3, enc4; + for (i = 0, j = 0; j < output.length;) { + enc1 = BASE64_MAP.indexOf(input.charAt(i++)); + enc2 = BASE64_MAP.indexOf(input.charAt(i++)); + enc3 = BASE64_MAP.indexOf(input.charAt(i++)); + enc4 = BASE64_MAP.indexOf(input.charAt(i++)); + output[j++] = (enc1 << 2) | (enc2 >> 4); + output[j++] = ((enc2 & 15) << 4) | (enc3 >> 2); + output[j++] = ((enc3 & 3) << 6) | enc4; + } + for (i = 0; i < append; i++) { + if (output.pop() != 0) { + return null; + } + } + return output; +} + +function base64_test() { + var bin = [1, 2, 3, 4, 5]; + var b64 = base64_encode(bin); + var buf = base64_decode(b64); + console.log(b64); + console.log(buf); +} diff --git a/js/sm2.js b/js/sm2.js new file mode 100644 index 00000000..fcff285a --- /dev/null +++ b/js/sm2.js @@ -0,0 +1,613 @@ + + +

SM4

+ + + diff --git a/js/sm3.js b/js/sm3.js index 2ef9a41f..7caef21d 100644 --- a/js/sm3.js +++ b/js/sm3.js @@ -318,6 +318,36 @@ function sm3(m) { return digest; } +function sm3_compute_z(z, id, pk) { + const Z_CONST = [ + 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, + 0x28, 0xE9, 0xFA, 0x9E, 0x9D, 0x9F, 0x5E, 0x34, + 0x4D, 0x5A, 0x9E, 0x4B, 0xCF, 0x65, 0x09, 0xA7, + 0xF3, 0x97, 0x89, 0xF5, 0x15, 0xAB, 0x8F, 0x92, + 0xDD, 0xBC, 0xBD, 0x41, 0x4D, 0x94, 0x0E, 0x93, + 0x32, 0xC4, 0xAE, 0x2C, 0x1F, 0x19, 0x81, 0x19, + 0x5F, 0x99, 0x04, 0x46, 0x6A, 0x39, 0xC9, 0x94, + 0x8F, 0xE3, 0x0B, 0xBF, 0xF2, 0x66, 0x0B, 0xE1, + 0x71, 0x5A, 0x45, 0x89, 0x33, 0x4C, 0x74, 0xC7, + 0xBC, 0x37, 0x36, 0xA2, 0xF4, 0xF6, 0x77, 0x9C, + 0x59, 0xBD, 0xCE, 0xE3, 0x6B, 0x69, 0x21, 0x53, + 0xD0, 0xA9, 0x87, 0x7C, 0xC6, 0x2A, 0x47, 0x40, + 0x02, 0xDF, 0x32, 0xE5, 0x21, 0x39, 0xF0, 0xA0, + ]; + + var ctx = sm3_ctx_new(); + sm3_init(ctx); + sm3_update(ctx, [Math.floor(id.length / 65536), id.length % 65536]); + sm3_update(ctx, id); + sm3_update(ctx, Z_CONST); + sm3_update(ctx, pk); + sm3_final(ctx, z); + sm3_ctx_free(ctx); +} + function sm3_test() { const test1 = [0x61, 0x62, 0x63]; const test2 = [