From d39a92d0e5ed8db7611d9174656afc100af8aaab Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Mon, 25 May 2020 17:52:41 +0800 Subject: [PATCH] Add JavaScript API --- js/README.md | 5 + js/sm3.js | 373 ++++++++++++++++++++++++++++++++++++++++++++++++++ js/sm4.js | 377 +++++++++++++++++++++++++++++++++++++++++++++++++++ js/test.html | 38 ++++++ 4 files changed, 793 insertions(+) create mode 100644 js/README.md create mode 100644 js/sm3.js create mode 100644 js/sm4.js create mode 100644 js/test.html diff --git a/js/README.md b/js/README.md new file mode 100644 index 00000000..17a8124a --- /dev/null +++ b/js/README.md @@ -0,0 +1,5 @@ +# GmSSL JavaScript API + +The GmSSL JavaScript API is very similar to the C API. +Input/output data should be provided as byte array with offset. +Only SM2/3/4/9 and ZUC will be supported in JavaScripit API. diff --git a/js/sm3.js b/js/sm3.js new file mode 100644 index 00000000..2ef9a41f --- /dev/null +++ b/js/sm3.js @@ -0,0 +1,373 @@ +/* + * 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. + */ + +function sm3_memcpy(dst, dst_offset, src, src_offset, len) { + while (len--) { + dst[dst_offset++] = src[src_offset++]; + } +} + +function sm3_memset(dst, offset, value, len) { + while (len--) { + dst[offset++] = value; + } +} + +function SM3_GETU32(data, offset) { + return ((data[offset] << 24) + | (data[offset + 1] << 16) + | (data[offset + 2] << 8) + | data[offset + 3]) >>> 0; +} + +function SM3_PUTU32(data, offset, value) { + data[offset + 3] = (value & 0xff) >>> 0; + value >>>= 8; + data[offset + 2] = (value & 0xff) >>> 0; + value >>>= 8; + data[offset + 1] = (value & 0xff) >>> 0; + value >>>= 8; + data[offset] = (value & 0xff) >>> 0; +} + +const SM3_DIGEST_LENGTH = 32; +const SM3_BLOCK_SIZE = 64; +const SM3_HMAC_SIZE = SM3_DIGEST_LENGTH; + +function sm3_ctx_new() { + var ctx = { + state: new Array(8), + block: new Array(64), + nblocks: 0, + num: 0 + }; + return ctx; +} + +function sm3_init(ctx) { + for (var i = 0; i < SM3_BLOCK_SIZE; i++) { + ctx.block[i] = 0; + } + ctx.nblocks = 0; + ctx.num = 0; + ctx.state[0] = 0x7380166F; + ctx.state[1] = 0x4914B2B9; + ctx.state[2] = 0x172442D7; + ctx.state[3] = 0xDA8A0600; + ctx.state[4] = 0xA96F30BC; + ctx.state[5] = 0x163138AA; + ctx.state[6] = 0xE38DEE4D; + ctx.state[7] = 0xB0FB0E4E; +} + +function sm3_compress(state, block) { + return sm3_compress_blocks(state, block, 0, 1); +} + +function sm3_update(ctx, data) { + + var data_offset = 0; + var data_len = data.length; + var blocks; + + if (ctx.num) { + var left = SM3_BLOCK_SIZE - ctx.num; + if (data_len < left) { + sm3_memcpy(ctx.block, ctx.num, data, 0, data_len); + ctx.num += data_len; + return; + } else { + sm3_memcpy(ctx.block, ctx.num, data, 0, left); + sm3_compress_blocks(ctx.state, ctx.block, 0, 1); + ctx.nblocks++; + data_offset += left; + data_len -= left; + } + } + + blocks = Math.floor(data_len / SM3_BLOCK_SIZE); + if (blocks > 0) { + sm3_compress_blocks(ctx.state, data, data_offset, blocks); + ctx.nblocks += blocks; + data_offset += SM3_BLOCK_SIZE * blocks; + data_len -= SM3_BLOCK_SIZE * blocks; + } + + ctx.num = data_len; + if (data_len) { + sm3_memcpy(ctx.block, 0, data, data_offset, data_len); + } +} + +function sm3_final(ctx, digest) +{ + ctx.block[ctx.num] = 0x80; + + if (ctx.num + 9 <= SM3_BLOCK_SIZE) { + sm3_memset(ctx.block, ctx.num + 1, 0, SM3_BLOCK_SIZE - ctx.num - 9); + } else { + sm3_memset(ctx.block, ctx.num + 1, 0, SM3_BLOCK_SIZE - ctx.num - 1); + sm3_compress(ctx.state, ctx.block); + sm3_memset(ctx.block, 0, 0, SM3_BLOCK_SIZE - 8); + } + + var hi = ctx.nblocks >>> 23; + var lo = (((ctx.nblocks << 9) + (ctx.num << 3)) & 0xffffffff) >>> 0; + + SM3_PUTU32(ctx.block, 56, hi); + SM3_PUTU32(ctx.block, 60, lo); + + sm3_compress(ctx.state, ctx.block); + + for (i = 0; i < 8; i++) { + SM3_PUTU32(digest, i*4, ctx.state[i]); + } +} + +function SM3_ROL32(x, n) { + return ((x << n) | (x >>> (32 - n))) >>> 0; +} + +function SM3_P0(x) { + return ((x) ^ SM3_ROL32((x), 9) ^ SM3_ROL32((x),17)) >>> 0; +} + +function SM3_P1(x) { + return ((x) ^ SM3_ROL32((x),15) ^ SM3_ROL32((x),23)) >>> 0; +} + +function SM3_FF00(x, y, z) { + return ((x) ^ (y) ^ (z)) >>> 0; +} + +function SM3_FF16(x,y,z) { + return (((x)&(y)) | ((x)&(z)) | ((y)&(z))) >>> 0; +} + +function SM3_GG00(x,y,z) { + return ((x) ^ (y) ^ (z)) >>> 0; +} + +function SM3_GG16(x,y,z) { + return ((((y)^(z)) & (x)) ^ (z)) >>> 0; +} + +function sm3_compress_blocks(state, data, offset, blocks) { + var A; + var B; + var C; + var D; + var E; + var F; + var G; + var H; + var W = new Array(68); + var SS1, SS2, TT1, TT2; + var j; + + const K = [ + 0x79cc4519, 0xf3988a32, 0xe7311465, 0xce6228cb, + 0x9cc45197, 0x3988a32f, 0x7311465e, 0xe6228cbc, + 0xcc451979, 0x988a32f3, 0x311465e7, 0x6228cbce, + 0xc451979c, 0x88a32f39, 0x11465e73, 0x228cbce6, + 0x9d8a7a87, 0x3b14f50f, 0x7629ea1e, 0xec53d43c, + 0xd8a7a879, 0xb14f50f3, 0x629ea1e7, 0xc53d43ce, + 0x8a7a879d, 0x14f50f3b, 0x29ea1e76, 0x53d43cec, + 0xa7a879d8, 0x4f50f3b1, 0x9ea1e762, 0x3d43cec5, + 0x7a879d8a, 0xf50f3b14, 0xea1e7629, 0xd43cec53, + 0xa879d8a7, 0x50f3b14f, 0xa1e7629e, 0x43cec53d, + 0x879d8a7a, 0x0f3b14f5, 0x1e7629ea, 0x3cec53d4, + 0x79d8a7a8, 0xf3b14f50, 0xe7629ea1, 0xcec53d43, + 0x9d8a7a87, 0x3b14f50f, 0x7629ea1e, 0xec53d43c, + 0xd8a7a879, 0xb14f50f3, 0x629ea1e7, 0xc53d43ce, + 0x8a7a879d, 0x14f50f3b, 0x29ea1e76, 0x53d43cec, + 0xa7a879d8, 0x4f50f3b1, 0x9ea1e762, 0x3d43cec5, + ]; + + + while (blocks--) { + A = state[0]; + B = state[1]; + C = state[2]; + D = state[3]; + E = state[4]; + F = state[5]; + G = state[6]; + H = state[7]; + + for (j = 0; j < 16; j++) { + W[j] = SM3_GETU32(data, offset + j*4); + } + + for (; j < 68; j++) { + W[j] = SM3_P1(W[j - 16] ^ W[j - 9] ^ SM3_ROL32(W[j - 3], 15)) + ^ SM3_ROL32(W[j - 13], 7) ^ W[j - 6]; + W[j] >>>= 0; + } + + + j = 0; + + for (; j < 16; j++) { + SS1 = SM3_ROL32(A, 12) + E + K[j]; + SS1 = (SS1 & 0xffffffff) >>> 0; + SS1 = SM3_ROL32(SS1, 7); + SS2 = SS1 ^ SM3_ROL32(A, 12); + SS2 = (SS2 & 0xffffffff) >>> 0; + TT1 = W[j] ^ W[j + 4]; + TT1 = (TT1 & 0xffffffff) >>> 0; + TT1 = SM3_FF00(A, B, C) + D + SS2 + TT1; + TT1 = (TT1 & 0xffffffff) >>> 0; + TT2 = SM3_GG00(E, F, G) + H + SS1 + W[j]; + TT2 = (TT2 & 0xffffffff) >>> 0; + D = C; + C = SM3_ROL32(B, 9); + B = A; + A = TT1; + H = G; + G = SM3_ROL32(F, 19); + F = E; + E = SM3_P0(TT2); + } + + for (; j < 64; j++) { + SS1 = SM3_ROL32(A, 12) + E + K[j]; + SS1 = (SS1 & 0xffffffff) >>> 0; + SS1 = SM3_ROL32(SS1, 7); + SS2 = SS1 ^ SM3_ROL32(A, 12); + SS2 = (SS2 & 0xffffffff) >>> 0; + TT1 = W[j] ^ W[j + 4]; + TT1 = (TT1 & 0xffffffff) >>> 0; + TT1 = SM3_FF16(A, B, C) + D + SS2 + TT1; + TT1 = (TT1 & 0xffffffff) >>> 0; + TT2 = SM3_GG16(E, F, G) + H + SS1 + W[j]; + TT2 = (TT2 & 0xffffffff) >>> 0; + D = C; + C = SM3_ROL32(B, 9); + B = A; + A = TT1; + H = G; + G = SM3_ROL32(F, 19); + F = E; + E = SM3_P0(TT2); + } + + state[0] ^= A; + state[1] ^= B; + state[2] ^= C; + state[3] ^= D; + state[4] ^= E; + state[5] ^= F; + state[6] ^= G; + state[7] ^= H; + } +} + +function sm3(m) { + var digest = new Array(SM3_DIGEST_LENGTH); + var ctx = sm3_ctx_new(); + sm3_init(ctx); + sm3_update(ctx, m); + sm3_final(ctx, digest); + delete ctx; + return digest; +} + +function sm3_test() { + const test1 = [0x61, 0x62, 0x63]; + const test2 = [ + 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64]; + const dgst1 = [ + 0x66, 0xc7, 0xf0, 0xf4, 0x62, 0xee, 0xed, 0xd9, + 0xd1, 0xf2, 0xd4, 0x6b, 0xdc, 0x10, 0xe4, 0xe2, + 0x41, 0x67, 0xc4, 0x87, 0x5c, 0xf2, 0xf7, 0xa2, + 0x29, 0x7d, 0xa0, 0x2b, 0x8f, 0x4b, 0xa8, 0xe0]; + const dgst2 = [ + 0xde, 0xbe, 0x9f, 0xf9, 0x22, 0x75, 0xb8, 0xa1, + 0x38, 0x60, 0x48, 0x89, 0xc1, 0x8e, 0x5a, 0x4d, + 0x6f, 0xdb, 0x70, 0xe5, 0x38, 0x7e, 0x57, 0x65, + 0x29, 0x3d, 0xcb, 0xa3, 0x9c, 0x0c, 0x57, 0x32]; + + + dgst = sm3(test1); + + for (var i = 0; i < 32; i++) { + if (dgst[i] != dgst1[i]) { + console.log("test1 error at " + i); + return 0; + } + } + console.log("test1 ok"); + + dgst = sm3(test2); + for (var i = 0; i < 32; i++) { + if (dgst[i] != dgst2[i]) { + console.log("test2 error at " + i); + return 0; + } + } + console.log("test2 ok"); + + return 1; +} + +function sm3_speed() { + var data = new Array(1000 * 1000); + for (var i = 0; i < data.length; i++) { + data[i] = 0x23; + } + var dgst = sm3(data); + console.log(dgst); +} diff --git a/js/sm4.js b/js/sm4.js new file mode 100644 index 00000000..a1254acb --- /dev/null +++ b/js/sm4.js @@ -0,0 +1,377 @@ +/* + * 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. + */ + + +function sm4_memcpy(dst, dst_offset, src, src_offset, len) { + while (len--) { + dst[dst_offset++] = src[src_offset++]; + } +} + +function SM4_GETU32(data, offset) { + return ((data[offset] << 24) + | (data[offset + 1] << 16) + | (data[offset + 2] << 8) + | data[offset + 3]) >>> 0; +} + +function SM4_PUTU32(data, offset, value) { + data[offset + 3] = (value & 0xff) >>> 0; + value >>>= 8; + data[offset + 2] = (value & 0xff) >>> 0; + value >>>= 8; + data[offset + 1] = (value & 0xff) >>> 0; + value >>>= 8; + data[offset] = (value & 0xff) >>> 0; +} + +const SM4_KEY_LENGTH = 16 +const SM4_BLOCK_SIZE = 16 +const SM4_IV_LENGTH = SM4_BLOCK_SIZE +const SM4_NUM_ROUNDS = 32 + +const SM4_S = [ + 0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, + 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05, + 0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, + 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, + 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, + 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62, + 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, + 0x80, 0xdf, 0x94,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, 0x84, + 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, + 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48, +]; + +const SM4_FK = [ + 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc, +]; + +const SM4_CK = [ + 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, + 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, + 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, + 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9, + 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, + 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, + 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, + 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279, +]; + +function SM4_ROL32(x, n) { + return ((x << n) | (x >>> (32 - n))) >>> 0; +} + +function SM4_S32(A) { + return ( + (SM4_S[A >>> 24] << 24) ^ + (SM4_S[(A >>> 16) & 0xff] << 16) ^ + (SM4_S[(A >>> 8) & 0xff] << 8) ^ + (SM4_S[A & 0xff])) >>> 0; +} + +function SM4_L32(x) { + return ( + x ^ + SM4_ROL32(x, 2) ^ + SM4_ROL32(x, 10) ^ + SM4_ROL32(x, 18) ^ + SM4_ROL32(x, 24)) >>> 0; +} + +function SM4_L32_(x) { + return ( + x ^ + SM4_ROL32(x, 13) ^ + SM4_ROL32(x, 23)) >>> 0; +} + + +function sm4_key_new() { + var key = { + rk: new Array(SM4_NUM_ROUNDS), + }; + return key; +} + +function sm4_key_free(key) { + for (var i = 0; i < SM4_NUM_ROUNDS; i++) { + key.rk[i] = 0; + } + delete key; +} + +function sm4_set_encrypt_key(key, user_key) { + var x0, x1, x2, x3, x4; + x0 = SM4_GETU32(user_key, 0) ^ SM4_FK[0]; + x1 = SM4_GETU32(user_key, 4) ^ SM4_FK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.rk[13] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[13]))) >>> 0; + key.rk[14] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[14]))) >>> 0; + key.rk[15] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[15]))) >>> 0; + key.rk[16] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[16]))) >>> 0; + key.rk[17] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[17]))) >>> 0; + key.rk[18] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[18]))) >>> 0; + key.rk[19] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[19]))) >>> 0; + key.rk[20] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[20]))) >>> 0; + key.rk[21] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[21]))) >>> 0; + key.rk[22] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[22]))) >>> 0; + key.rk[23] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[23]))) >>> 0; + key.rk[24] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[24]))) >>> 0; + key.rk[25] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[25]))) >>> 0; + key.rk[26] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[26]))) >>> 0; + key.rk[27] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[27]))) >>> 0; + key.rk[28] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[28]))) >>> 0; + key.rk[29] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[29]))) >>> 0; + key.rk[30] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[30]))) >>> 0; + key.rk[31] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[31]))) >>> 0; + x0 = x1 = x3 = x3 = x4 = 0; +} + +function sm4_set_decrypt_key(key, user_key) { + var x0, x1, x2, x3, x4; + x0 = SM4_GETU32(user_key, 0) ^ SM4_FK[0]; + x1 = SM4_GETU32(user_key, 4) ^ SM4_FK[1]; + x2 = SM4_GETU32(user_key, 8) ^ SM4_FK[2]; + x3 = SM4_GETU32(user_key, 12) ^ SM4_FK[3]; + key.rk[31] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[ 0]))) >>> 0; + key.rk[30] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[ 1]))) >>> 0; + key.rk[29] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[ 2]))) >>> 0; + key.rk[28] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[ 3]))) >>> 0; + key.rk[27] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[ 4]))) >>> 0; + key.rk[26] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[ 5]))) >>> 0; + key.rk[25] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[ 6]))) >>> 0; + key.rk[24] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[ 7]))) >>> 0; + key.rk[23] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[ 8]))) >>> 0; + key.rk[22] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[ 9]))) >>> 0; + key.rk[21] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[10]))) >>> 0; + key.rk[20] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[11]))) >>> 0; + key.rk[19] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[12]))) >>> 0; + key.rk[18] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[13]))) >>> 0; + key.rk[17] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[14]))) >>> 0; + key.rk[16] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[15]))) >>> 0; + key.rk[15] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[16]))) >>> 0; + key.rk[14] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[17]))) >>> 0; + key.rk[13] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[18]))) >>> 0; + key.rk[12] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[19]))) >>> 0; + key.rk[11] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[20]))) >>> 0; + key.rk[10] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[21]))) >>> 0; + key.rk[ 9] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[22]))) >>> 0; + key.rk[ 8] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[23]))) >>> 0; + key.rk[ 7] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[24]))) >>> 0; + key.rk[ 6] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[25]))) >>> 0; + key.rk[ 5] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[26]))) >>> 0; + key.rk[ 4] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[27]))) >>> 0; + key.rk[ 3] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[28]))) >>> 0; + key.rk[ 2] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[29]))) >>> 0; + key.rk[ 1] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[30]))) >>> 0; + key.rk[ 0] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[31]))) >>> 0; + x0 = x1 = x3 = x3 = x4 = 0; +} + +function sm4_encrypt(inbuf, in_offset, outbuf, out_offset, key) { + var x0, x1, x2, x3, x4; + x0 = SM4_GETU32(inbuf, in_offset); + x1 = SM4_GETU32(inbuf, in_offset + 4); + x2 = SM4_GETU32(inbuf, in_offset + 8); + x3 = SM4_GETU32(inbuf, in_offset + 12); + x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[0])); + x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[1])); + x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[2])); + x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[3])); + x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[4])); + x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[5])); + x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[6])); + x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[7])); + x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[8])); + x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[9])); + x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[10])); + x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[11])); + x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[12])); + x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[13])); + x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[14])); + x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[15])); + x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[16])); + x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[17])); + x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[18])); + x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[19])); + x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[20])); + x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[21])); + x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[22])); + x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[23])); + x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[24])); + x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[25])); + x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[26])); + x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[27])); + x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[28])); + x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[29])); + x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[30])); + x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[31])); + SM4_PUTU32(outbuf, out_offset, x0); + SM4_PUTU32(outbuf, out_offset + 4, x4); + SM4_PUTU32(outbuf, out_offset + 8, x3); + SM4_PUTU32(outbuf, out_offset + 12, x2); +} + +function sm4_decrypt(inbuf, in_offset, outbuf, out_offset, key) { + return sm4_encrypt(inbuf, in_offset, outbuf, out_offset, key); +} + +function sm4_test() { + const user_key = [ + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, + ]; + const rk = [ + 0xf12186f9, 0x41662b61, 0x5a6ab19a, 0x7ba92077, + 0x367360f4, 0x776a0c61, 0xb6bb89b3, 0x24763151, + 0xa520307c, 0xb7584dbd, 0xc30753ed, 0x7ee55b57, + 0x6988608c, 0x30d895b7, 0x44ba14af, 0x104495a1, + 0xd120b428, 0x73b55fa3, 0xcc874966, 0x92244439, + 0xe89e641f, 0x98ca015a, 0xc7159060, 0x99e1fd2e, + 0xb79bd80c, 0x1d2115b0, 0x0e228aeb, 0xf1780c81, + 0x428d3654, 0x62293496, 0x01cf72e5, 0x9124a012, + ]; + const plaintext = [ + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, + ]; + const ciphertext = [ + 0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, + 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46, + ]; + const ciphertext2 = [ + 0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f, + 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66, + ]; + + var key = sm4_key_new(); + var buf = new Array(SM4_BLOCK_SIZE); + + sm4_set_encrypt_key(key, user_key); + + for (var i = 0; i < SM4_NUM_ROUNDS; i++) { + if (key.rk[i] != rk[i]) { + console.log("sm4_set_encrypt_key failed"); + return 0; + } + } + + sm4_encrypt(plaintext, 0, buf, 0, key); + + console.log("sm4 test1"); + for (var i = 0; i < SM4_BLOCK_SIZE; i++) { + if (buf[i] != ciphertext[i]) { + console.log("sm4_encrypt failed"); + return 0; + } + } + + sm4_memcpy(buf, 0, plaintext, 0, SM4_BLOCK_SIZE); + + console.log("sm4 test2"); + for (var i = 0; i < 1000000; i++) { + sm4_encrypt(buf, 0, buf, 0, key); + } + + for (var i = 0; i < SM4_BLOCK_SIZE; i++) { + if (buf[i] != ciphertext2[i]) { + console.log("sm4_encrypt 1000000 failed"); + return 0; + } + } + + sm4_set_decrypt_key(key, user_key); + sm4_encrypt(ciphertext, 0, buf, 0, key); + + for (var i = 0; i < SM4_BLOCK_SIZE; i++) { + if (buf[i] != plaintext[i]) { + console.log("sm4_decrypt failed"); + return 0; + } + } + + sm4_key_free(key); + return 1; +} diff --git a/js/test.html b/js/test.html new file mode 100644 index 00000000..b2769cef --- /dev/null +++ b/js/test.html @@ -0,0 +1,38 @@ + + + + +

GmSSL JavaScript

+ + +