mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-13 20:06:24 +08:00
[crypto] move sms4_standard and zuc to engine dir
This commit is contained in:
182
engines/sm_standard/sms4/sms4_standard.c
Normal file
182
engines/sm_standard/sms4/sms4_standard.c
Normal file
@@ -0,0 +1,182 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2017 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
#include "sms4_standard.h"
|
||||
//rotate n bits to the left in a 32bit buffer
|
||||
#define SM4_ROTL32(buf, n) (((buf)<<n)|((buf)>>(32-n)))
|
||||
uint32_t SM4_CK[32] = {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
|
||||
};
|
||||
uint8_t SM4_SBOX[256] =
|
||||
{ 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, 0xfa, 0x75, 0x8f, 0x3f, 0xa6,
|
||||
0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8,
|
||||
0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35,
|
||||
0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87,
|
||||
0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e,
|
||||
0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1,
|
||||
0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3,
|
||||
0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f,
|
||||
0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51,
|
||||
0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8,
|
||||
0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0,
|
||||
0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84,
|
||||
0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48
|
||||
};
|
||||
|
||||
uint32_t SM4_FK[4] = {0xA3B1BAC6, 0x56AA3350, 0x677D9197, 0xB27022DC};
|
||||
|
||||
void sm4_standard_key_schedule(const unsigned char *key, uint32_t *rk)
|
||||
{
|
||||
uint32_t tmp, buf, K[36];
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
K[i] = SM4_FK[i] ^ ( (key[4 * i] << 24) | (key[4 * i + 1] << 16)
|
||||
| (key[4 * i + 2] << 8) | (key[4 * i + 3]) );
|
||||
}
|
||||
|
||||
for (i = 0; i < 32; i++)
|
||||
{
|
||||
tmp = K[i + 1] ^ K[i + 2] ^ K[i + 3] ^ SM4_CK[i];
|
||||
|
||||
//nonlinear operation
|
||||
buf = (SM4_SBOX[(tmp >> 24) & 0xFF]) << 24
|
||||
| (SM4_SBOX[(tmp >> 16) & 0xFF]) << 16
|
||||
| (SM4_SBOX[(tmp >> 8) & 0xFF]) << 8
|
||||
| (SM4_SBOX[tmp & 0xFF]);
|
||||
|
||||
//linear operation
|
||||
K[i + 4] = K[i] ^ ((buf) ^ (SM4_ROTL32((buf), 13)) ^ (SM4_ROTL32((buf), 23)));
|
||||
|
||||
rk[i] = K[i + 4];
|
||||
}
|
||||
}
|
||||
|
||||
void sms4_standard_encrypt(const unsigned char *in, unsigned char *out, const unsigned char *key)
|
||||
{
|
||||
uint32_t rk[32], X[36], tmp, buf;
|
||||
uint32_t i, j;
|
||||
|
||||
sm4_standard_key_schedule(key, rk);
|
||||
|
||||
for (j = 0; j < 4; j++)
|
||||
{
|
||||
X[j] = (in[j * 4] << 24) | (in[j * 4 + 1] << 16)
|
||||
| (in[j * 4 + 2] << 8) | (in[j * 4 + 3]);
|
||||
}
|
||||
|
||||
for (i = 0; i < 32; i++)
|
||||
{
|
||||
tmp = X[i + 1] ^ X[i + 2] ^ X[i + 3] ^ rk[i];
|
||||
|
||||
//nonlinear operation
|
||||
buf = ( SM4_SBOX[(tmp >> 24) & 0xFF]) << 24
|
||||
| (SM4_SBOX[(tmp >> 16) & 0xFF]) << 16
|
||||
| (SM4_SBOX[(tmp >> 8) & 0xFF]) << 8
|
||||
| (SM4_SBOX[tmp & 0xFF]);
|
||||
|
||||
//linear operation
|
||||
X[i + 4] = X[i] ^ (buf ^ SM4_ROTL32((buf), 2)^ SM4_ROTL32((buf), 10)
|
||||
^ SM4_ROTL32((buf), 18)^ SM4_ROTL32((buf), 24));
|
||||
}
|
||||
|
||||
for (j = 0; j < 4; j++)
|
||||
{
|
||||
out[4 * j] = (X[35 - j] >> 24) & 0xFF;
|
||||
out[4 * j + 1] = (X[35 - j] >> 16) & 0xFF;
|
||||
out[4 * j + 2] = (X[35 - j] >> 8) & 0xFF;
|
||||
out[4 * j + 3] = (X[35 - j]) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
void sms4_standard_decrypt(const unsigned char *in, unsigned char *out, const unsigned char *key)
|
||||
{
|
||||
uint32_t rk[32], X[36], tmp, buf;
|
||||
uint32_t i, j;
|
||||
|
||||
sm4_standard_key_schedule(key, rk);
|
||||
|
||||
for (j = 0; j < 4; j++)
|
||||
{
|
||||
X[j] = (in[j * 4] << 24) | (in[j * 4 + 1] << 16) |
|
||||
(in[j * 4 + 2] << 8) | (in[j * 4 + 3]);
|
||||
}
|
||||
|
||||
for (i = 0; i < 32; i++)
|
||||
{
|
||||
tmp = X[i + 1] ^ X[i + 2] ^ X[i + 3] ^ rk[31 - i];
|
||||
|
||||
//nonlinear operation
|
||||
buf = (SM4_SBOX[(tmp >> 24) & 0xFF]) << 24
|
||||
| (SM4_SBOX[(tmp >> 16) & 0xFF]) << 16
|
||||
| (SM4_SBOX[(tmp >> 8) & 0xFF]) << 8
|
||||
| (SM4_SBOX[tmp & 0xFF]);
|
||||
//linear operation
|
||||
X[i + 4] = X[i] ^ (buf ^ SM4_ROTL32((buf), 2)^ SM4_ROTL32((buf), 10)
|
||||
^ SM4_ROTL32((buf), 18)^ SM4_ROTL32((buf), 24));
|
||||
}
|
||||
|
||||
for (j = 0; j < 4; j++)
|
||||
{
|
||||
out[4 * j] = (X[35 - j] >> 24) & 0xFF;
|
||||
out[4 * j + 1] = (X[35 - j] >> 16) & 0xFF;
|
||||
out[4 * j + 2] = (X[35 - j] >> 8) & 0xFF;
|
||||
out[4 * j + 3] = (X[35 - j]) & 0xFF;
|
||||
}
|
||||
}
|
||||
74
engines/sm_standard/sms4/sms4_standard.h
Normal file
74
engines/sm_standard/sms4/sms4_standard.h
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2017 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
#ifndef HEADER_SMS4_STANDARD_H
|
||||
#define HEADER_SMS4_STANDARD_H
|
||||
|
||||
#include <openssl/opensslconf.h>
|
||||
#ifndef NO_GMSSL
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <openssl/e_os2.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void sms4_standard_encrypt(const unsigned char *in, unsigned char *out, const unsigned char *key);
|
||||
void sms4_standard_decrypt(const unsigned char *in, unsigned char *out, const unsigned char *key);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
107
engines/sm_standard/zuc/zuc.h
Normal file
107
engines/sm_standard/zuc/zuc.h
Normal file
@@ -0,0 +1,107 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2015 - 2017 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
unsigned char ZUC_S0[256] =
|
||||
{ 0x3e, 0x72, 0x5b, 0x47, 0xca, 0xe0, 0x00, 0x33, 0x04, 0xd1, 0x54, 0x98, 0x09, 0xb9, 0x6d, 0xcb,
|
||||
0x7b, 0x1b, 0xf9, 0x32, 0xaf, 0x9d, 0x6a, 0xa5, 0xb8, 0x2d, 0xfc, 0x1d, 0x08, 0x53, 0x03, 0x90,
|
||||
0x4d, 0x4e, 0x84, 0x99, 0xe4, 0xce, 0xd9, 0x91, 0xdd, 0xb6, 0x85, 0x48, 0x8b, 0x29, 0x6e, 0xac,
|
||||
0xcd, 0xc1, 0xf8, 0x1e, 0x73, 0x43, 0x69, 0xc6, 0xb5, 0xbd, 0xfd, 0x39, 0x63, 0x20, 0xd4, 0x38,
|
||||
0x76, 0x7d, 0xb2, 0xa7, 0xcf, 0xed, 0x57, 0xc5, 0xf3, 0x2c, 0xbb, 0x14, 0x21, 0x06, 0x55, 0x9b,
|
||||
0xe3, 0xef, 0x5e, 0x31, 0x4f, 0x7f, 0x5a, 0xa4, 0x0d, 0x82, 0x51, 0x49, 0x5f, 0xba, 0x58, 0x1c,
|
||||
0x4a, 0x16, 0xd5, 0x17, 0xa8, 0x92, 0x24, 0x1f, 0x8c, 0xff, 0xd8, 0xae, 0x2e, 0x01, 0xd3, 0xad,
|
||||
0x3b, 0x4b, 0xda, 0x46, 0xeb, 0xc9, 0xde, 0x9a, 0x8f, 0x87, 0xd7, 0x3a, 0x80, 0x6f, 0x2f, 0xc8,
|
||||
0xb1, 0xb4, 0x37, 0xf7, 0x0a, 0x22, 0x13, 0x28, 0x7c, 0xcc, 0x3c, 0x89, 0xc7, 0xc3, 0x96, 0x56,
|
||||
0x07, 0xbf, 0x7e, 0xf0, 0x0b, 0x2b, 0x97, 0x52, 0x35, 0x41, 0x79, 0x61, 0xa6, 0x4c, 0x10, 0xfe,
|
||||
0xbc, 0x26, 0x95, 0x88, 0x8a, 0xb0, 0xa3, 0xfb, 0xc0, 0x18, 0x94, 0xf2, 0xe1, 0xe5, 0xe9, 0x5d,
|
||||
0xd0, 0xdc, 0x11, 0x66, 0x64, 0x5c, 0xec, 0x59, 0x42, 0x75, 0x12, 0xf5, 0x74, 0x9c, 0xaa, 0x23,
|
||||
0x0e, 0x86, 0xab, 0xbe, 0x2a, 0x02, 0xe7, 0x67, 0xe6, 0x44, 0xa2, 0x6c, 0xc2, 0x93, 0x9f, 0xf1,
|
||||
0xf6, 0xfa, 0x36, 0xd2, 0x50, 0x68, 0x9e, 0x62, 0x71, 0x15, 0x3d, 0xd6, 0x40, 0xc4, 0xe2, 0x0f,
|
||||
0x8e, 0x83, 0x77, 0x6b, 0x25, 0x05, 0x3f, 0x0c, 0x30, 0xea, 0x70, 0xb7, 0xa1, 0xe8, 0xa9, 0x65,
|
||||
0x8d, 0x27, 0x1a, 0xdb, 0x81, 0xb3, 0xa0, 0xf4, 0x45, 0x7a, 0x19, 0xdf, 0xee, 0x78, 0x34, 0x60
|
||||
};
|
||||
|
||||
unsigned char ZUC_S1[256] =
|
||||
{ 0x55, 0xc2, 0x63, 0x71, 0x3b, 0xc8, 0x47, 0x86, 0x9f, 0x3c, 0xda, 0x5b, 0x29, 0xaa, 0xfd, 0x77,
|
||||
0x8c, 0xc5, 0x94, 0x0c, 0xa6, 0x1a, 0x13, 0x00, 0xe3, 0xa8, 0x16, 0x72, 0x40, 0xf9, 0xf8, 0x42,
|
||||
0x44, 0x26, 0x68, 0x96, 0x81, 0xd9, 0x45, 0x3e, 0x10, 0x76, 0xc6, 0xa7, 0x8b, 0x39, 0x43, 0xe1,
|
||||
0x3a, 0xb5, 0x56, 0x2a, 0xc0, 0x6d, 0xb3, 0x05, 0x22, 0x66, 0xbf, 0xdc, 0x0b, 0xfa, 0x62, 0x48,
|
||||
0xdd, 0x20, 0x11, 0x06, 0x36, 0xc9, 0xc1, 0xcf, 0xf6, 0x27, 0x52, 0xbb, 0x69, 0xf5, 0xd4, 0x87,
|
||||
0x7f, 0x84, 0x4c, 0xd2, 0x9c, 0x57, 0xa4, 0xbc, 0x4f, 0x9a, 0xdf, 0xfe, 0xd6, 0x8d, 0x7a, 0xeb,
|
||||
0x2b, 0x53, 0xd8, 0x5c, 0xa1, 0x14, 0x17, 0xfb, 0x23, 0xd5, 0x7d, 0x30, 0x67, 0x73, 0x08, 0x09,
|
||||
0xee, 0xb7, 0x70, 0x3f, 0x61, 0xb2, 0x19, 0x8e, 0x4e, 0xe5, 0x4b, 0x93, 0x8f, 0x5d, 0xdb, 0xa9,
|
||||
0xad, 0xf1, 0xae, 0x2e, 0xcb, 0x0d, 0xfc, 0xf4, 0x2d, 0x46, 0x6e, 0x1d, 0x97, 0xe8, 0xd1, 0xe9,
|
||||
0x4d, 0x37, 0xa5, 0x75, 0x5e, 0x83, 0x9e, 0xab, 0x82, 0x9d, 0xb9, 0x1c, 0xe0, 0xcd, 0x49, 0x89,
|
||||
0x01, 0xb6, 0xbd, 0x58, 0x24, 0xa2, 0x5f, 0x38, 0x78, 0x99, 0x15, 0x90, 0x50, 0xb8, 0x95, 0xe4,
|
||||
0xd0, 0x91, 0xc7, 0xce, 0xed, 0x0f, 0xb4, 0x6f, 0xa0, 0xcc, 0xf0, 0x02, 0x4a, 0x79, 0xc3, 0xde,
|
||||
0xa3, 0xef, 0xea, 0x51, 0xe6, 0x6b, 0x18, 0xec, 0x1b, 0x2c, 0x80, 0xf7, 0x74, 0xe7, 0xff, 0x21,
|
||||
0x5a, 0x6a, 0x54, 0x1e, 0x41, 0x31, 0x92, 0x35, 0xc4, 0x33, 0x07, 0x0a, 0xba, 0x7e, 0x0e, 0x34,
|
||||
0x88, 0xb1, 0x98, 0x7c, 0xf3, 0x3d, 0x60, 0x6c, 0x7b, 0xca, 0xd3, 0x1f, 0x32, 0x65, 0x04, 0x28,
|
||||
0x64, 0xbe, 0x85, 0x9b, 0x2f, 0x59, 0x8a, 0xd7, 0xb0, 0x25, 0xac, 0xaf, 0x12, 0x03, 0xe2, 0xf2
|
||||
};
|
||||
|
||||
//D value in key loading
|
||||
uint32_t ZUC_D[16] = {0x44D7, 0x26BC, 0x626B, 0x135E, 0x5789, 0x35E2, 0x7135, 0x09AF,
|
||||
0x4D78, 0x2F13, 0x6BC4, 0x1AF1, 0x5E26, 0x3C4D, 0x789A, 0x47AC
|
||||
};
|
||||
|
||||
//rotate n bits to the left in a 32bit buffer
|
||||
#define ZUC_ROTL32(x, k) (((x) << k) | ((x) >> (32 - k)))
|
||||
//si = ki¡¬ di¡¬ ivi,in key loading
|
||||
#define ZUC_LINK_TO_S(a, b, c) (((uint32_t)(a) << 23)|((uint32_t)(b) << 8)|(uint32_t)(c))
|
||||
|
||||
|
||||
uint32_t add_mod(uint32_t a, uint32_t b);
|
||||
uint32_t pow_mod(uint32_t x, uint32_t k);
|
||||
uint32_t l1(uint32_t X);
|
||||
uint32_t l2(uint32_t X);
|
||||
unsigned char bit_value(uint32_t M[], uint32_t i);
|
||||
uint32_t get_word(uint32_t k[], uint32_t i);
|
||||
void lfsr_with_init_mode(uint32_t LFSR_S[], uint32_t u) ;
|
||||
void lfsr_with_work_mode(uint32_t LFSR_S[]) ;
|
||||
void br(uint32_t LFSR_S[], uint32_t BR_X[]);
|
||||
uint32_t f(uint32_t BR_X[], uint32_t F_R[]);
|
||||
530
engines/sm_standard/zuc/zuc_standard.c
Normal file
530
engines/sm_standard/zuc/zuc_standard.c
Normal file
@@ -0,0 +1,530 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2015 - 2016 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
#include "zuc_standard.h"
|
||||
#include "zuc.h"
|
||||
|
||||
/************************************************************
|
||||
Function: add_mod
|
||||
Description: calculate a+b mod 2^31-1
|
||||
Calls:
|
||||
Called By: lfsr_with_init_mode
|
||||
Input: a,b: uint32_t(32bit)
|
||||
Output:
|
||||
Return: c, c=a+b mod 2^31-1
|
||||
Others:
|
||||
************************************************************/
|
||||
uint32_t add_mod(uint32_t a, uint32_t b)
|
||||
{
|
||||
uint32_t c = a + b;
|
||||
if (c >> 31)
|
||||
{
|
||||
c = (c & 0x7fffffff) + 1;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
/************************************************************
|
||||
Function: pow_mod
|
||||
Description: calculate x*2^k mod 2^31-1
|
||||
Calls: Called By: lfsr_with_init_mode
|
||||
Input: x: input
|
||||
k: exponential
|
||||
Output:
|
||||
Return: x*2^k mod 2^31-1
|
||||
Others:
|
||||
************************************************************/
|
||||
uint32_t pow_mod(uint32_t x, uint32_t k)
|
||||
{
|
||||
return (((x << k) | (x >> (31 - k))) & 0x7fffffff);
|
||||
}
|
||||
|
||||
|
||||
/************************************************************
|
||||
Function: l1
|
||||
Description: linear transformation l1
|
||||
Calls:
|
||||
Called By: f
|
||||
Input: X: input
|
||||
Output:
|
||||
Return: X^(X<<< 2)^(X<<<10)^(X<<<18)^(X<<<24)
|
||||
Others:
|
||||
************************************************************/
|
||||
uint32_t l1(uint32_t X)
|
||||
{
|
||||
return X ^ ZUC_ROTL32(X, 2) ^ ZUC_ROTL32(X, 10) ^ ZUC_ROTL32(X, 18) ^ ZUC_ROTL32(X, 24);
|
||||
}
|
||||
|
||||
|
||||
/************************************************************
|
||||
Function: l2
|
||||
Description: linear transformation l2
|
||||
Calls:
|
||||
Called By: f
|
||||
Input: X: input
|
||||
Output:
|
||||
Return: X^(X<<< 8)^(X<<<14)^(X<<<22)^(X<<<30)
|
||||
Others:
|
||||
************************************************************/
|
||||
uint32_t l2(uint32_t X)
|
||||
{
|
||||
return X ^ ZUC_ROTL32(X, 8) ^ ZUC_ROTL32(X, 14) ^ ZUC_ROTL32(X, 22) ^ ZUC_ROTL32(X, 30);
|
||||
}
|
||||
|
||||
|
||||
/************************************************************
|
||||
Function: bit_value
|
||||
Description: test if the value of M at the position i equals 0
|
||||
Calls:
|
||||
Called By: zuc_integrity
|
||||
Input: M: message
|
||||
i: the position i
|
||||
Output:
|
||||
Return: 0:the value of M at the position i equals 0
|
||||
1:the value of M at the position i equals 1
|
||||
Others:
|
||||
************************************************************/
|
||||
unsigned char bit_value(uint32_t M[], uint32_t i)
|
||||
{
|
||||
int j, k;
|
||||
j = i >> 5;
|
||||
k = i & 0x1f;
|
||||
if (M[j] & (0x1 << (31 - k)))
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/************************************************************
|
||||
Function: get_word
|
||||
Description: get a 32bit word ki from bit strings k[i],k[i+1]...,namely
|
||||
ki=k[i]||k[i+1]||…||k[i+31]
|
||||
Calls:
|
||||
Called By: zuc_integrity
|
||||
Input: k[]:
|
||||
i: the position i
|
||||
Output:
|
||||
Return: ki=k[i]||k[i+1]||…||k[i+31]
|
||||
Others:
|
||||
************************************************************/
|
||||
uint32_t get_word(uint32_t k[], uint32_t i)
|
||||
{
|
||||
int j, m;
|
||||
uint32_t word;
|
||||
j = i >> 5;
|
||||
m = i & 0x1f;
|
||||
if (m == 0)
|
||||
word = k[j];
|
||||
else
|
||||
word = (k[j] << m) | (k[j + 1] >> (32 - m));
|
||||
return word;
|
||||
}
|
||||
|
||||
|
||||
/************************************************************
|
||||
Function: lfsr_with_init_mode
|
||||
Description: Initialisation mode,refresh the current state of LFSR
|
||||
Calls: add_mod,pow_mod
|
||||
Called By: zuc_standard_init
|
||||
Input: LFSR_S:current state of LFSR
|
||||
u:u=W>>1
|
||||
Output: Null
|
||||
Return: Null
|
||||
Others:
|
||||
************************************************************/
|
||||
void lfsr_with_init_mode(uint32_t LFSR_S[], uint32_t u)
|
||||
{
|
||||
uint32_t v = LFSR_S[0], i;
|
||||
v = add_mod(v, pow_mod(LFSR_S[15], 15));
|
||||
v = add_mod(v, pow_mod(LFSR_S[13], 17));
|
||||
v = add_mod(v, pow_mod(LFSR_S[10], 21));
|
||||
v = add_mod(v, pow_mod(LFSR_S[4] , 20));
|
||||
v = add_mod(v, pow_mod(LFSR_S[0] , 8));
|
||||
|
||||
for (i = 0; i < 15; i++)
|
||||
{
|
||||
LFSR_S[i] = LFSR_S[i + 1];
|
||||
}
|
||||
LFSR_S[15] = add_mod(v, u);
|
||||
|
||||
if (!LFSR_S[15])
|
||||
{
|
||||
LFSR_S[15] = 0x7fffffff;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/************************************************************
|
||||
Function: lfsr_with_work_mode
|
||||
Description: working mode,refresh the current state of LFSR
|
||||
Calls: add_mod,pow_mod
|
||||
Called By: zuc_standard_work
|
||||
Input: LFSR_S:current state of LFSR
|
||||
Output: Null
|
||||
Return: Null
|
||||
Others:
|
||||
************************************************************/
|
||||
void lfsr_with_work_mode(uint32_t LFSR_S[])
|
||||
{
|
||||
uint32_t v = LFSR_S[0], i;
|
||||
v = add_mod(v, pow_mod(LFSR_S[15], 15));
|
||||
v = add_mod(v, pow_mod(LFSR_S[13], 17));
|
||||
v = add_mod(v, pow_mod(LFSR_S[10], 21));
|
||||
v = add_mod(v, pow_mod(LFSR_S[4] , 20));
|
||||
v = add_mod(v, pow_mod(LFSR_S[0] , 8));
|
||||
|
||||
for (i = 0; i < 15; i++)
|
||||
{
|
||||
LFSR_S[i] = LFSR_S[i + 1];
|
||||
}
|
||||
LFSR_S[15] = v;
|
||||
|
||||
if (!LFSR_S[15])
|
||||
{
|
||||
LFSR_S[15] = 0x7fffffff;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/************************************************************
|
||||
Function: br
|
||||
Description: Bit Reconstruction
|
||||
Calls:
|
||||
Called By: zuc_standard_init,zuc_standard_work
|
||||
Input: LFSR_S:current state of LFSR
|
||||
Output: BR_X[]:achieve X0,X1,X2,X3
|
||||
Return: Null
|
||||
Others:
|
||||
************************************************************/
|
||||
void br(uint32_t LFSR_S[], uint32_t BR_X[])
|
||||
{
|
||||
BR_X[0] = ((LFSR_S[15] & 0x7fff8000) << 1) | (LFSR_S[14] & 0x0000ffff);
|
||||
BR_X[1] = ((LFSR_S[11] & 0x0000ffff) << 16) | ((LFSR_S[9] & 0x7fff8000) >> 15);
|
||||
BR_X[2] = ((LFSR_S[7] & 0x0000ffff) << 16) | ((LFSR_S[5] & 0x7fff8000) >> 15);
|
||||
BR_X[3] = ((LFSR_S[2] & 0x0000ffff) << 16) | ((LFSR_S[0] & 0x7fff8000) >> 15);
|
||||
}
|
||||
|
||||
|
||||
/************************************************************
|
||||
Function: f
|
||||
Description: nonlinear function
|
||||
Calls:
|
||||
Called By: zuc_standard_init,zuc_standard_work
|
||||
Input: BR_X[]:words X0,X1,X2,X3 from br
|
||||
F_R[]:F_R[0]=R1,F_R[1]=R2
|
||||
Output:
|
||||
Return: W
|
||||
Others:
|
||||
************************************************************/
|
||||
uint32_t f(uint32_t BR_X[], uint32_t F_R[])
|
||||
{
|
||||
uint32_t W, W1, W2;
|
||||
|
||||
W = (BR_X[0] ^ F_R[0]) + F_R[1];
|
||||
W1 = F_R[0] + BR_X[1];
|
||||
W2 = F_R[1] ^ BR_X[2];
|
||||
F_R[0] = l1((W1 << 16) | (W2 >> 16));
|
||||
F_R[0] = (ZUC_S0[(F_R[0] >> 24) & 0xFF]) << 24
|
||||
| (ZUC_S1[(F_R[0] >> 16) & 0xFF]) << 16
|
||||
| (ZUC_S0[(F_R[0] >> 8) & 0xFF]) << 8
|
||||
| (ZUC_S1[F_R[0] & 0xFF]);
|
||||
F_R[1] = l2((W2 << 16) | (W1 >> 16));
|
||||
F_R[1] = (ZUC_S0[(F_R[1] >> 24) & 0xFF]) << 24
|
||||
| (ZUC_S1[(F_R[1] >> 16) & 0xFF]) << 16
|
||||
| (ZUC_S0[(F_R[1] >> 8) & 0xFF]) << 8
|
||||
| (ZUC_S1[F_R[1] & 0xFF]);
|
||||
|
||||
return W;
|
||||
};
|
||||
|
||||
/************************************************************
|
||||
Function: zuc_standard_init
|
||||
Description: Initialisation process of ZUC
|
||||
Calls: ZUC_LINK_TO_S,br,f,lfsr_with_init_mode
|
||||
Called By: zuc_genkeystream
|
||||
Input: k:initial key
|
||||
iv:initial vector
|
||||
Output: LFSR_S[]:the state of LFSR after initialisation:s0,s1,s2,..s15
|
||||
BR_X[] : the current value:X0,X1,X2,X3
|
||||
F_R[]:the current value:R1,R2,F_R[0]=R1,F_R[1]=R2
|
||||
Return: Null
|
||||
Others:
|
||||
************************************************************/
|
||||
void zuc_standard_init(unsigned char k[], unsigned char iv[], uint32_t LFSR_S[], uint32_t
|
||||
BR_X[], uint32_t F_R[])
|
||||
{
|
||||
unsigned char count = 32;
|
||||
int i;
|
||||
|
||||
//loading key to the LFSR s0,s1,s2....s15
|
||||
printf("\ninitial state of LFSR: S[0]-S[15]\n");
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
LFSR_S[i] = ZUC_LINK_TO_S(k[i], ZUC_D[i], iv[i]);
|
||||
printf("%08x ", LFSR_S[i]);
|
||||
}
|
||||
|
||||
F_R[0] = 0x00; //R1
|
||||
F_R[1] = 0x00; //R2
|
||||
|
||||
while (count) //32 times
|
||||
{
|
||||
uint32_t W;
|
||||
br( LFSR_S, BR_X); //BitReconstruction
|
||||
W = f(BR_X, F_R); //nonlinear function
|
||||
lfsr_with_init_mode(LFSR_S, W >> 1);
|
||||
count--;
|
||||
}
|
||||
}
|
||||
|
||||
/************************************************************
|
||||
Function: zuc_standard_work
|
||||
Description: working stage of ZUC
|
||||
Calls: br,f,lfsr_with_work_mode
|
||||
Called By: zuc_genkeystream
|
||||
Input: LFSR_S[]:the state of LFSR after initialisation:s0,s1,s2,..s15
|
||||
BR_X[] : X0,X1,X2,X3
|
||||
F_R[]:R1,R2
|
||||
Output: pKeyStream[]:key stream
|
||||
KeyStreamLen:the length of KeyStream,exporting 32bit for a beat
|
||||
Return: Null
|
||||
Others:
|
||||
************************************************************/
|
||||
void zuc_standard_work(uint32_t LFSR_S[], uint32_t BR_X[], uint32_t F_R[], uint32_t
|
||||
pKeyStream[], int KeyStreamLen)
|
||||
{
|
||||
int i = 0;
|
||||
br(LFSR_S, BR_X);
|
||||
f(BR_X, F_R);
|
||||
lfsr_with_work_mode(LFSR_S);
|
||||
|
||||
while (i < KeyStreamLen)
|
||||
{
|
||||
br( LFSR_S, BR_X);
|
||||
pKeyStream[i] = f(BR_X, F_R) ^ BR_X[3];
|
||||
lfsr_with_work_mode(LFSR_S);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************
|
||||
Function: zuc_genkeystream
|
||||
Description: generate key stream
|
||||
Calls: zuc_standard_init,zuc_standard_work
|
||||
Called By: ZUC_SelfCheck
|
||||
Input: k[] //initial key,128bit
|
||||
iv[] //initial iv,128bit
|
||||
KeyStreamLen //the byte length of KeyStream,exporting 32bit for a beat
|
||||
Output: KeyStream[] // key strem to be outputed
|
||||
Return: null
|
||||
Others:
|
||||
****************************************************************/
|
||||
void zuc_genkeystream(unsigned char k[], unsigned char iv[], uint32_t KeyStream[], int
|
||||
KeyStreamLen)
|
||||
{
|
||||
|
||||
uint32_t LFSR_S[16]; //LFSR state s0,s1,s2,...s15
|
||||
uint32_t BR_X[4]; //Bit Reconstruction X0,X1,X2,X3
|
||||
uint32_t F_R[2]; //R1,R2,variables of nonlinear function f
|
||||
int i;
|
||||
|
||||
//Initialisation
|
||||
zuc_standard_init(k, iv, LFSR_S, BR_X, F_R);
|
||||
printf("\nstate of LFSR after executing initialization: S[0]-S[15]\n");
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
printf("%08x ", LFSR_S[i]);
|
||||
}
|
||||
printf("\ninternal state of Finite State Machine:\n");
|
||||
printf("R1=%08x\n", F_R[0]);
|
||||
printf("R2=%08x\n", F_R[1]);
|
||||
|
||||
//Working
|
||||
zuc_standard_work(LFSR_S, BR_X, F_R, KeyStream, KeyStreamLen);
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************
|
||||
Function: zuc_confidentiality
|
||||
Description: the ZUC-based condifentiality algorithm
|
||||
Calls: zuc_genkeystream
|
||||
Called By: ZUC_SelfCheck
|
||||
Input: CK[] //initial key,128bit,uesed to gain the key of ZUC KeyStream
|
||||
generation algorithm
|
||||
COUNT //128bit
|
||||
BEARER //5bit,bearing layer identification,
|
||||
DIRECTION //1bit
|
||||
IBS[] //input bit stream,
|
||||
LENGTH //the bit length of IBS
|
||||
Output: OBS[] //output bit stream,
|
||||
Return: null
|
||||
Others:
|
||||
****************************************************************/
|
||||
void zuc_confidentiality(unsigned char CK[], uint32_t COUNT, unsigned char BEARER, unsigned
|
||||
char DIRECTION, uint32_t IBS[], int LENGTH, uint32_t OBS[])
|
||||
|
||||
{
|
||||
uint32_t *k;
|
||||
int L, i, t;
|
||||
unsigned char iv[16];
|
||||
|
||||
//generate vector iv1,iv2,...iv15
|
||||
iv[0] = (unsigned char)(COUNT >> 24);
|
||||
iv[1] = (unsigned char)((COUNT >> 16) & 0xff);
|
||||
iv[2] = (unsigned char)((COUNT >> 8) & 0xff);
|
||||
iv[3] = (unsigned char)(COUNT & 0xff);
|
||||
iv[4] = (((BEARER << 3) | (DIRECTION << 2)) & 0xfc);
|
||||
iv[5] = 0x00;
|
||||
iv[6] = 0x00;
|
||||
iv[7] = 0x00;
|
||||
iv[8] = iv[0];
|
||||
iv[9] = iv[1];
|
||||
iv[10] = iv[2];
|
||||
iv[11] = iv[3];
|
||||
iv[12] = iv[4];
|
||||
iv[13] = iv[5];
|
||||
iv[14] = iv[6];
|
||||
iv[15] = iv[7];
|
||||
|
||||
//L,the length of key stream,taking 32bit as a unit
|
||||
L = (LENGTH + 31) / 32;
|
||||
k = malloc(sizeof(uint32_t) * L);
|
||||
|
||||
//generate key stream k
|
||||
zuc_genkeystream(CK, iv, k, L); //generate key stream
|
||||
|
||||
//OBS=IBS^k
|
||||
for (i = 0; i < L; i++)
|
||||
{
|
||||
OBS[i] = IBS[i] ^ k[i];
|
||||
}
|
||||
t = LENGTH % 32;
|
||||
if (t)
|
||||
{
|
||||
OBS[L - 1] = ((OBS[L - 1] >> (32 - t)) << (32 - t));
|
||||
}
|
||||
free(k);
|
||||
}
|
||||
|
||||
/****************************************************************
|
||||
Function: zuc_integrity
|
||||
Description: the ZUC-based integrity algorithm
|
||||
Calls: zuc_genkeystream,bit_value,get_word
|
||||
Called By: ZUC_SelfCheck
|
||||
Input: IK[] //integrity key,128bit,uesed to gain the key of ZUC KeyStream
|
||||
generation algorithm
|
||||
COUNT //128bit
|
||||
BEARER //5bit,bearing layer identification,
|
||||
DIRECTION //1bit
|
||||
M[] //message
|
||||
LENGTH //the bit length of M
|
||||
Output:
|
||||
Return: MAC //message authentication code
|
||||
Others:
|
||||
****************************************************************/
|
||||
uint32_t zuc_integrity(unsigned char IK[], uint32_t COUNT, unsigned char BEARER, unsigned
|
||||
char DIRECTION, uint32_t M[], int LENGTH)
|
||||
{
|
||||
uint32_t *k, ki, MAC;
|
||||
int L, i;
|
||||
unsigned char iv[16];
|
||||
uint32_t T = 0;
|
||||
|
||||
//generate vector iv1,iv2,...iv15
|
||||
iv[0] = (unsigned char)(COUNT >> 24);
|
||||
iv[1] = (unsigned char)((COUNT >> 16) & 0xff);
|
||||
iv[2] = (unsigned char)((COUNT >> 8) & 0xff);
|
||||
iv[3] = (unsigned char)(COUNT & 0xff);
|
||||
iv[4] = BEARER << 3;
|
||||
iv[5] = 0x00;
|
||||
iv[6] = 0x00;
|
||||
iv[7] = 0x00;
|
||||
iv[8] = iv[0] ^ (DIRECTION << 7);
|
||||
iv[9] = iv[1];
|
||||
iv[10] = iv[2];
|
||||
iv[11] = iv[3];
|
||||
iv[12] = iv[4];
|
||||
iv[13] = iv[5];
|
||||
iv[14] = iv[6] ^ (DIRECTION << 7);
|
||||
iv[15] = iv[7];
|
||||
|
||||
//L,the length of key stream,taking 32bit as a unit
|
||||
L = (LENGTH + 31) / 32 + 2;
|
||||
k = malloc(sizeof(uint32_t) * L);
|
||||
|
||||
//generate key stream k
|
||||
zuc_genkeystream(IK, iv, k, L);
|
||||
|
||||
//T=T^ki
|
||||
for (i = 0; i < LENGTH; i++)
|
||||
{
|
||||
if (bit_value(M, i))
|
||||
{
|
||||
ki = get_word(k, i);
|
||||
T = T ^ ki;
|
||||
}
|
||||
}
|
||||
|
||||
//T=T^kLENGTH
|
||||
ki = get_word(k, LENGTH);
|
||||
T = T ^ ki;
|
||||
|
||||
//MAC=T^k(32*(L-1))
|
||||
ki = get_word(k, 32 * (L - 1));
|
||||
MAC = T ^ ki;
|
||||
|
||||
free(k);
|
||||
return MAC;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
73
engines/sm_standard/zuc/zuc_standard.h
Normal file
73
engines/sm_standard/zuc/zuc_standard.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2015 - 2016 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
#ifndef HEADER_ZUC_STANDARD_H
|
||||
#define HEADER_ZUC_STANDARD_H
|
||||
|
||||
#include <openssl/opensslconf.h>
|
||||
#ifndef NO_GMSSL
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <openssl/e_os2.h>
|
||||
|
||||
# ifdef __cplusplus
|
||||
extern "C" {
|
||||
# endif
|
||||
void zuc_standard_init(unsigned char k[], unsigned char iv[], uint32_t LFSR_S[], uint32_t BR_X[], uint32_t F_R[]);
|
||||
void zuc_standard_work(uint32_t LFSR_S[], uint32_t BR_X[], uint32_t F_R[], uint32_t pKeyStream[], int KeyStreamLen);
|
||||
void zuc_genkeystream(unsigned char k[], unsigned char iv[], uint32_t KeyStream[], int KeyStreamLen);
|
||||
void zuc_confidentiality(unsigned char CK[], uint32_t COUNT, unsigned char BEARER, unsigned
|
||||
char DIRECTION, uint32_t IBS[], int LENGTH, uint32_t OBS[]);
|
||||
uint32_t zuc_integrity(unsigned char IK[], uint32_t COUNT, unsigned char BEARER, unsigned
|
||||
char DIRECTION, uint32_t M[], int LENGTH);
|
||||
# ifdef __cplusplus
|
||||
}
|
||||
# endif
|
||||
# endif
|
||||
# endif
|
||||
Reference in New Issue
Block a user