add sdf and skf

This commit is contained in:
Zhi Guan
2021-08-03 17:09:35 +08:00
parent d6feba3749
commit a57193836b
71 changed files with 11100 additions and 765 deletions

View File

@@ -114,7 +114,7 @@ static uint32_t rot_word(uint32_t A)
#ifdef CRYPTO_INFO
static void print_rk(const AES_KEY *aes_key)
{
int i;
size_t i;
for (i = 0; i <= aes_key->rounds; i++) {
printf("%08x ", aes_key->rk[4 * i]);
printf("%08x ", aes_key->rk[4 * i + 1]);
@@ -134,7 +134,7 @@ int aes_set_encrypt_key(AES_KEY *aes_key, const uint8_t *key, size_t keylen)
*/
uint32_t *W = (uint32_t *)aes_key->rk;
size_t Nk = keylen/sizeof(uint32_t);
int i;
size_t i;
switch (keylen) {
case AES128_KEY_SIZE:
@@ -177,7 +177,7 @@ int aes_set_decrypt_key(AES_KEY *aes_key, const uint8_t *key, size_t keylen)
{
int ret = 0;
AES_KEY enc_key;
int i;
size_t i;
if (!aes_set_encrypt_key(&enc_key, key, keylen)) {
goto end;
@@ -402,7 +402,7 @@ static void print_state(const uint8_t S[4][4])
void aes_encrypt(const AES_KEY *key, const uint8_t in[16], uint8_t out[16])
{
uint8_t state[4][4];
int i;
size_t i;
/* fill state columns */
for (i = 0; i < 4; i++) {
@@ -442,7 +442,7 @@ void aes_encrypt(const AES_KEY *key, const uint8_t in[16], uint8_t out[16])
void aes_decrypt(const AES_KEY *aes_key, const uint8_t in[16], uint8_t out[16])
{
uint8_t state[4][4];
int i;
size_t i;
/* fill state columns */
for (i = 0; i < 4; i++) {

View File

@@ -86,7 +86,7 @@ int aes_cbc_padding_encrypt(const AES_KEY *key, const uint8_t iv[16],
{
uint8_t block[16];
size_t rem = inlen % 16;
int padding = 16 - rem;
int padding = 16 - inlen % 16;
if (in) {
memcpy(block, in + inlen - rem, rem);
@@ -107,14 +107,15 @@ int aes_cbc_padding_decrypt(const AES_KEY *key, const uint8_t iv[16],
uint8_t *out, size_t *outlen)
{
uint8_t block[16];
size_t len = sizeof(block);
int padding;
if (inlen == 0) {
error_print("warning: input lenght = 0");
error_print();
return 0;
}
if (inlen%16 != 0 || inlen < 16) {
error_print("invalid cbc ciphertext length");
error_print();
return -1;
}
if (inlen > 16) {
@@ -127,7 +128,8 @@ int aes_cbc_padding_decrypt(const AES_KEY *key, const uint8_t iv[16],
error_print();
return -1;
}
memcpy(out + inlen - 16, block, 16 - padding);
len -= padding;
memcpy(out + inlen - 16, block, len);
*outlen = inlen - padding;
return 1;
}

View File

@@ -89,7 +89,7 @@ static char *asn1_tag_index[] = {
const char *asn1_tag_name(int tag)
{
if (tag < 0 || tag > 0xff) {
error_print("invalid tag value\n");
error_print();
return NULL;
}
@@ -127,7 +127,7 @@ const char *asn1_tag_name(int tag)
case ASN1_TAG_SET: return "SET";
}
error_print("unknown universal tag %d\n", tag);
error_print();
return NULL;
}
@@ -252,7 +252,7 @@ int asn1_length_from_der(size_t *plen, const uint8_t **pin, size_t *pinlen)
}
if (inlen < len) {
error_print("inlen (%zu) < length(%zu)", inlen, len);
error_print();
return -1;
}
@@ -573,6 +573,7 @@ int asn1_utc_time_to_der_ex(int tag, time_t a, uint8_t **out, size_t *outlen)
return -1;
}
// 注意这个函数可能在Windows上是没有的
gmtime_r(&a, &tm_val);
strftime(buf, sizeof(buf), "%y%m%d%H%M%SZ", &tm_val);
@@ -886,6 +887,21 @@ int asn1_ia5_string_from_der_ex(int tag, const char **a, size_t *alen, const uin
return asn1_type_from_der(tag, (const uint8_t **)a, alen, in, inlen);
}
/*
int hh, mm, ss;
struct tm when = {0};
sscanf_s(date, "%d:%d:%d", &hh, &mm, &ss);
when.tm_hour = hh;
when.tm_min = mm;
when.tm_sec = ss;
time_t converted;
converted = mktime(&when);
*/
int asn1_utc_time_from_der_ex(int tag, time_t *t, const uint8_t **pin, size_t *pinlen)
{
const uint8_t *in = *pin;
@@ -920,13 +936,14 @@ int asn1_utc_time_from_der_ex(int tag, time_t *t, const uint8_t **pin, size_t *p
buf[1] = '0';
}
if (len == sizeof("YYMMDDHHMMSSZ")-1) {
if (!strptime(buf, "%Y%m%d%H%M%SZ", &tm_val)) {
// 这里应该自己写一个函数来解析
if (!strptime(buf, "%Y%m%d%H%M%SZ", &tm_val)) { // 注意这个函数在Windows上没有
return -1;
}
} else {
return -1;
}
*t = timegm(&tm_val);
*t = timegm(&tm_val); // FIXME: Windows !
*pin = in + len;
*pinlen = inlen - len;

View File

@@ -283,7 +283,7 @@ static int sha1_digest_finish(DIGEST_CTX *ctx, unsigned char *dgst)
static const DIGEST sha1_digest_object = {
OID_sha1,
SHA1_DIGEST_LENGTH,
SHA1_DIGEST_SIZE,
SHA1_BLOCK_SIZE,
sizeof(SHA1_CTX),
sha1_digest_init,

View File

@@ -85,14 +85,14 @@
(p)[3] = (uint8_t)(V))
#define PUTU64(p,V) \
((p)[0] = (uint64_t)((V) >> 56), \
(p)[1] = (uint64_t)((V) >> 48), \
(p)[2] = (uint64_t)((V) >> 40), \
(p)[3] = (uint64_t)((V) >> 32), \
(p)[4] = (uint64_t)((V) >> 24), \
(p)[5] = (uint64_t)((V) >> 16), \
(p)[6] = (uint64_t)((V) >> 8), \
(p)[7] = (uint64_t)(V))
((p)[0] = (uint8_t)((V) >> 56), \
(p)[1] = (uint8_t)((V) >> 48), \
(p)[2] = (uint8_t)((V) >> 40), \
(p)[3] = (uint8_t)((V) >> 32), \
(p)[4] = (uint8_t)((V) >> 24), \
(p)[5] = (uint8_t)((V) >> 16), \
(p)[6] = (uint8_t)((V) >> 8), \
(p)[7] = (uint8_t)(V))
/* Little Endian R/W */

View File

@@ -81,11 +81,12 @@ int gf128_equ_hex(gf128_t a, const char *s)
return memcmp(bin1, bin2, sizeof(bin1)) == 0;
}
// FIXME: 这个函数不支持struct
void gf128_print_bits(gf128_t a)
{
int i;
for (i = 0; i < 128; i++) {
printf("%d", (int)(a % 2));
printf("%d", (int)(a % 2)); //FIXME
a >>= 1;
}
printf("\n");

View File

@@ -212,7 +212,8 @@ end:
*/
static void drbg_add(uint8_t *R, const uint8_t *A, size_t seedlen)
{
int temp = 0, i;
int temp = 0;
size_t i;
for (i = seedlen - 1; i >= 0; i--) {
temp += R[i] + A[i];
R[i] = temp & 0xff;
@@ -222,7 +223,8 @@ static void drbg_add(uint8_t *R, const uint8_t *A, size_t seedlen)
static void drbg_add1(uint8_t *R, size_t seedlen)
{
int temp = 1, i;
int temp = 1;
size_t i;
for (i = seedlen - 1; i >= 0; i--) {
temp += R[i];
R[i] = temp & 0xff;
@@ -265,7 +267,7 @@ static int drbg_hashgen(HASH_DRBG *drbg, size_t outlen, uint8_t *out)
ret = 1;
end:
digest_ctx_cleanup(&ctx);
memset(&ctx, 0, sizeof(ctx));
memset(data, 0, sizeof(data));
return ret;
}
@@ -339,8 +341,3 @@ end:
memset(dgst, 0, sizeof(dgst));
return ret;
}
void hash_drbg_cleanup(HASH_DRBG *drbg)
{
//mem_cleanup(drbg, sizeof(HASH_DRBG));
}

View File

@@ -143,13 +143,13 @@ int hex2bin(const char *in, size_t inlen, uint8_t *out)
{
int c;
if (inlen % 2) {
error_print("hex %s len = %zu\n", in, inlen);
error_print_msg("hex %s len = %zu\n", in, inlen);
return -1;
}
while (inlen) {
if ((c = hexchar2int(*in++)) < 0) {
error_print();
error_print_msg("%d", 5);
return -1;
}
*out = (uint8_t)c << 4;
@@ -193,3 +193,14 @@ void gmssl_memxor(void *r, const void *a, const void *b, size_t len)
pr[i] = pa[i] ^ pb[i];
}
}
int gmssl_memcmp(const void *s1, const void *s2, size_t n)
{
return memcmp(s1, s2, n);
}

133
src/md5.c
View File

@@ -52,73 +52,6 @@
#include "endian.h"
static void md5_compress_blocks(uint32_t state[4],
const unsigned char *data, size_t blocks);
void md5_init(MD5_CTX *ctx)
{
memset(ctx, 0, sizeof(*ctx));
ctx->state[0] = 0x67452301;
ctx->state[1] = 0xefcdab89;
ctx->state[2] = 0x98badcfe;
ctx->state[3] = 0x10325476;
}
void md5_update(MD5_CTX *ctx, const unsigned char *data, size_t datalen)
{
size_t blocks;
if (ctx->num) {
unsigned int left = MD5_BLOCK_SIZE - ctx->num;
if (datalen < left) {
memcpy(ctx->block + ctx->num, data, datalen);
ctx->num += datalen;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
md5_compress_blocks(ctx->state, ctx->block, 1);
ctx->nblocks++;
data += left;
datalen -= left;
}
}
blocks = datalen / MD5_BLOCK_SIZE;
md5_compress_blocks(ctx->state, data, blocks);
ctx->nblocks += blocks;
data += MD5_BLOCK_SIZE * blocks;
datalen -= MD5_BLOCK_SIZE * blocks;
ctx->num = datalen;
if (datalen) {
memcpy(ctx->block, data, datalen);
}
}
void md5_finish(MD5_CTX *ctx, unsigned char *dgst)
{
int i;
ctx->block[ctx->num] = 0x80;
if (ctx->num + 9 <= MD5_BLOCK_SIZE) {
memset(ctx->block + ctx->num + 1, 0, MD5_BLOCK_SIZE - ctx->num - 9);
} else {
memset(ctx->block + ctx->num + 1, 0, MD5_BLOCK_SIZE - ctx->num - 1);
md5_compress_blocks(ctx->state, ctx->block, 1);
memset(ctx->block, 0, MD5_BLOCK_SIZE - 8);
}
PUTU64_LE(ctx->block + 56, (ctx->nblocks << 9) + (ctx->num << 3));
md5_compress_blocks(ctx->state, ctx->block, 1);
for (i = 0; i < 4; i++) {
//PUTU32_LE(dgst, ctx->state[i]);
*(uint32_t *)dgst = ctx->state[i];
dgst += sizeof(uint32_t);
}
}
//#define ROL32(X, n) (((X) << (n)) | ((X) >> (32-(n))))
#define F(B, C, D) (((B) & (C)) | ((~(B)) & (D)))
#define G(B, C, D) (((B) & (D)) | ((C) & (~(D))))
#define H(B, C, D) ((B) ^ (C) ^ (D))
@@ -169,8 +102,7 @@ static void md5_compress_blocks(uint32_t state[4],
D = state[3];
for (i = 0; i < 16; i++) {
//W[i] = GETU32_LE(data);
W[i] = *((uint32_t *)data);
W[i] = GETU32_LE(data);
data += sizeof(uint32_t);
}
@@ -213,9 +145,68 @@ static void md5_compress_blocks(uint32_t state[4],
}
}
void md5_compress(uint32_t state[4], const unsigned char block[64])
void md5_init(MD5_CTX *ctx)
{
return md5_compress_blocks(state, block, 1);
memset(ctx, 0, sizeof(*ctx));
ctx->state[0] = 0x67452301;
ctx->state[1] = 0xefcdab89;
ctx->state[2] = 0x98badcfe;
ctx->state[3] = 0x10325476;
}
void md5_update(MD5_CTX *ctx, const unsigned char *data, size_t datalen)
{
size_t blocks;
ctx->num &= 0x3f;
if (ctx->num) {
size_t left = MD5_BLOCK_SIZE - ctx->num;
if (datalen < left) {
memcpy(ctx->block + ctx->num, data, datalen);
ctx->num += datalen;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
md5_compress_blocks(ctx->state, ctx->block, 1);
ctx->nblocks++;
data += left;
datalen -= left;
}
}
blocks = datalen / MD5_BLOCK_SIZE;
md5_compress_blocks(ctx->state, data, blocks);
ctx->nblocks += blocks;
data += MD5_BLOCK_SIZE * blocks;
datalen -= MD5_BLOCK_SIZE * blocks;
ctx->num = datalen;
if (datalen) {
memcpy(ctx->block, data, datalen);
}
}
void md5_finish(MD5_CTX *ctx, unsigned char *dgst)
{
int i;
ctx->num &= 0x3f;
ctx->block[ctx->num] = 0x80;
if (ctx->num <= MD5_BLOCK_SIZE - 9) {
memset(ctx->block + ctx->num + 1, 0, MD5_BLOCK_SIZE - ctx->num - 9);
} else {
memset(ctx->block + ctx->num + 1, 0, MD5_BLOCK_SIZE - ctx->num - 1);
md5_compress_blocks(ctx->state, ctx->block, 1);
memset(ctx->block, 0, MD5_BLOCK_SIZE - 8);
}
PUTU64_LE(ctx->block + 56, (ctx->nblocks << 9) + (ctx->num << 3));
md5_compress_blocks(ctx->state, ctx->block, 1);
for (i = 0; i < 4; i++) {
PUTU32_LE(dgst, ctx->state[i]);
dgst += sizeof(uint32_t);
}
memset(ctx, 0, sizeof(*ctx));
}
void md5_digest(const unsigned char *data, size_t datalen,

View File

@@ -54,5 +54,7 @@
void memxor(void *r, const void *a, size_t len);
void gmssl_memxor(void *r, const void *a, const void *b, size_t len);
int gmssl_memcmp(const void *s1, const void *s2, size_t n);
#endif

View File

@@ -574,7 +574,6 @@ int asn1_x509_kp_oid_from_octets(const uint8_t *in, size_t inlen)
int asn1_x509_kp_oid_from_name(const char *name)
{
int i;
assert(i >= 0 && i < sizeof(x509_kp_oids)/sizeof(x509_kp_oids[0]));
for (i = 0; i < sizeof(x509_kp_oids)/sizeof(x509_kp_oids[0]); i++) {
if (strcmp(name, x509_kp_oids[i].name) == 0) {
return OID_kp_serverAuth + i;
@@ -598,7 +597,7 @@ void asn1_oid_to_octets(int oid, uint8_t *out, size_t *outlen)
} else if (oid <= OID_at_role) {
asn1_x509_oid_to_octets(oid, out, outlen);
} else {
error_print("unknown oid %d\n", oid);
error_print();
assert(0);
}
}
@@ -639,7 +638,7 @@ int asn1_oid_from_octets(const uint8_t *in, size_t inlen)
}
if (ret < 0) {
error_print("invalid der\n");
error_print();
}
return ret;
}
@@ -907,7 +906,7 @@ int test_asn1_object_identifier_to_der(int oid)
printf("\n");
if (roid != oid) {
error_print("oid = %d, parsed oid = %d\n", oid, roid);
error_print();
return -1;
}
if (len != 0) {

View File

@@ -125,7 +125,7 @@
int pbkdf2_genkey(const DIGEST *digest,
const char *pass, size_t passlen,
const uint8_t *salt, size_t saltlen, unsigned int count,
const uint8_t *salt, size_t saltlen, size_t count,
size_t outlen, uint8_t *out)
{
HMAC_CTX ctx;
@@ -139,7 +139,7 @@ int pbkdf2_genkey(const DIGEST *digest,
hmac_init(&ctx_tmpl, digest, (uint8_t *)pass, passlen);
while (outlen > 0) {
int i;
size_t i;
PUTU32(iter_be, iter);
iter++;

View File

@@ -280,7 +280,7 @@ int pbes2_enc_algor_from_der(int *cipher, const uint8_t **iv, size_t *ivlen, con
*cipher = OID_sm4_cbc;
} else {
size_t i;
error_print("unknown cipher oid :");
error_print();
for (i = 0; i < nodes_count; i++) {
fprintf(stderr, " %d", nodes[i]);
}
@@ -339,7 +339,7 @@ int pbes2_params_from_der(
return -1;
}
if (keylen >= 0 && keylen != 16) {
error_print("keylen = %d\n", keylen);
error_print();
return -1;
}
return 1;
@@ -436,10 +436,6 @@ int pkcs8_enced_private_key_info_from_der(
int ret;
const uint8_t *data;
size_t datalen;
const uint8_t *algid;
size_t algidlen;
uint32_t nodes[32];
size_t nodes_count;
if ((ret = asn1_sequence_from_der(&data, &datalen, in, inlen)) != 1) {
if (ret < 0) error_print();

View File

@@ -1,5 +1,5 @@
/* ====================================================================
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
/*
* Copyright (c) 2014 - 2021 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
@@ -44,7 +44,6 @@
* 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 <string.h>
@@ -52,73 +51,6 @@
#include "endian.h"
static void sha1_compress_blocks(uint32_t dgst[5],
const unsigned char *data, size_t blocks);
void sha1_init(SHA1_CTX *ctx)
{
memset(ctx, 0, sizeof(*ctx));
ctx->state[0] = 0x67452301;
ctx->state[1] = 0xEFCDAB89;
ctx->state[2] = 0x98BADCFE;
ctx->state[3] = 0x10325476;
ctx->state[4] = 0xC3D2E1F0;
}
void sha1_update(SHA1_CTX *ctx, const unsigned char *data, size_t datalen)
{
size_t blocks;
if (ctx->num) {
unsigned int left = SHA1_BLOCK_SIZE - ctx->num;
if (datalen < left) {
memcpy(ctx->block + ctx->num, data, datalen);
ctx->num += datalen;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
sha1_compress_blocks(ctx->state, ctx->block, 1);
ctx->nblocks++;
data += left;
datalen -= left;
}
}
blocks = datalen / SHA1_BLOCK_SIZE;
sha1_compress_blocks(ctx->state, data, blocks);
ctx->nblocks += blocks;
data += SHA1_BLOCK_SIZE * blocks;
datalen -= SHA1_BLOCK_SIZE * blocks;
ctx->num = datalen;
if (datalen) {
memcpy(ctx->block, data, datalen);
}
}
void sha1_finish(SHA1_CTX *ctx, unsigned char *dgst)
{
int i;
ctx->block[ctx->num] = 0x80;
if (ctx->num + 9 <= SHA1_BLOCK_SIZE) {
memset(ctx->block + ctx->num + 1, 0, SHA1_BLOCK_SIZE - ctx->num - 9);
} else {
memset(ctx->block + ctx->num + 1, 0, SHA1_BLOCK_SIZE - ctx->num - 1);
sha1_compress_blocks(ctx->state, ctx->block, 1);
memset(ctx->block, 0, SHA1_BLOCK_SIZE - 8);
}
PUTU32(ctx->block + 56, ctx->nblocks >> 23);
PUTU32(ctx->block + 60, (ctx->nblocks << 9) + (ctx->num << 3));
sha1_compress_blocks(ctx->state, ctx->block, 1);
for (i = 0; i < 5; i++) {
PUTU32(dgst + i*4, ctx->state[i]);
}
}
#define F0(B, C, D) (((B) & (C)) | ((~(B)) & (D)))
#define F1(B, C, D) ((B) ^ (C) ^ (D))
#define F2(B, C, D) (((B) & (C)) | ((B) & (D)) | ((C) & (D)))
@@ -157,7 +89,6 @@ static void sha1_compress_blocks(uint32_t state[5],
W[i] = ROL32(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
}
/* see https://en.wikipedia.org/wiki/SHA-1#/media/File:SHA-1.svg */
for (i = 0; i < 20; i++) {
T = E + F0(B, C, D) + ROL32(A, 5) + W[i] + K0;
@@ -200,19 +131,77 @@ static void sha1_compress_blocks(uint32_t state[5],
}
}
void sha1_compress(uint32_t state[5], const unsigned char block[64])
void sha1_init(SHA1_CTX *ctx)
{
return sha1_compress_blocks(state, block, 1);
memset(ctx, 0, sizeof(*ctx));
ctx->state[0] = 0x67452301;
ctx->state[1] = 0xEFCDAB89;
ctx->state[2] = 0x98BADCFE;
ctx->state[3] = 0x10325476;
ctx->state[4] = 0xC3D2E1F0;
}
void sha1_update(SHA1_CTX *ctx, const unsigned char *data, size_t datalen)
{
size_t blocks;
ctx->num &= 0x3f;
if (ctx->num) {
unsigned int left = SHA1_BLOCK_SIZE - ctx->num;
if (datalen < left) {
memcpy(ctx->block + ctx->num, data, datalen);
ctx->num += datalen;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
sha1_compress_blocks(ctx->state, ctx->block, 1);
ctx->nblocks++;
data += left;
datalen -= left;
}
}
blocks = datalen / SHA1_BLOCK_SIZE;
sha1_compress_blocks(ctx->state, data, blocks);
ctx->nblocks += blocks;
data += SHA1_BLOCK_SIZE * blocks;
datalen -= SHA1_BLOCK_SIZE * blocks;
ctx->num = datalen;
if (datalen) {
memcpy(ctx->block, data, datalen);
}
}
void sha1_finish(SHA1_CTX *ctx, unsigned char *dgst)
{
int i;
ctx->num &= 0x3f;
ctx->block[ctx->num] = 0x80;
if (ctx->num <= SHA1_BLOCK_SIZE - 9) {
memset(ctx->block + ctx->num + 1, 0, SHA1_BLOCK_SIZE - ctx->num - 9);
} else {
memset(ctx->block + ctx->num + 1, 0, SHA1_BLOCK_SIZE - ctx->num - 1);
sha1_compress_blocks(ctx->state, ctx->block, 1);
memset(ctx->block, 0, SHA1_BLOCK_SIZE - 8);
}
PUTU32(ctx->block + 56, ctx->nblocks >> 23);
PUTU32(ctx->block + 60, (ctx->nblocks << 9) + (ctx->num << 3));
sha1_compress_blocks(ctx->state, ctx->block, 1);
for (i = 0; i < 5; i++) {
PUTU32(dgst + i*4, ctx->state[i]);
}
memset(ctx, 0, sizeof(*ctx));
}
void sha1_digest(const unsigned char *data, size_t datalen,
unsigned char dgst[SHA1_DIGEST_LENGTH])
unsigned char dgst[SHA1_DIGEST_SIZE])
{
SHA1_CTX ctx;
sha1_init(&ctx);
sha1_update(&ctx, data, datalen);
sha1_finish(&ctx, dgst);
memset(&ctx, 0, sizeof(SHA1_CTX));
memset(&ctx, 0, sizeof(ctx));
}

View File

@@ -1,5 +1,5 @@
/* ====================================================================
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
/*
* Copyright (c) 2014 - 2021 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
@@ -44,87 +44,13 @@
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmssl/sha2.h>
#include "endian.h"
static void sha256_compress_blocks(uint32_t state[8],
const unsigned char *data, size_t blocks);
void sha256_init(SHA256_CTX *ctx)
{
memset(ctx, 0, sizeof(*ctx));
ctx->state[0] = 0x6a09e667;
ctx->state[1] = 0xbb67ae85;
ctx->state[2] = 0x3c6ef372;
ctx->state[3] = 0xa54ff53a;
ctx->state[4] = 0x510e527f;
ctx->state[5] = 0x9b05688c;
ctx->state[6] = 0x1f83d9ab;
ctx->state[7] = 0x5be0cd19;
}
void sha256_update(SHA256_CTX *ctx, const unsigned char *data, size_t datalen)
{
size_t blocks;
if (ctx->num) {
unsigned int left = SHA256_BLOCK_SIZE - ctx->num;
if (datalen < left) {
memcpy(ctx->block + ctx->num, data, datalen);
ctx->num += datalen;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
sha256_compress_blocks(ctx->state, ctx->block, 1);
ctx->nblocks++;
data += left;
datalen -= left;
}
}
blocks = datalen / SHA256_BLOCK_SIZE;
sha256_compress_blocks(ctx->state, data, blocks);
ctx->nblocks += blocks;
data += SHA256_BLOCK_SIZE * blocks;
datalen -= SHA256_BLOCK_SIZE * blocks;
ctx->num = datalen;
if (datalen) {
memcpy(ctx->block, data, datalen);
}
}
void sha256_finish(SHA256_CTX *ctx, unsigned char dgst[SHA256_DIGEST_SIZE])
{
int i;
ctx->block[ctx->num] = 0x80;
if (ctx->num + 9 <= SHA256_BLOCK_SIZE) {
memset(ctx->block + ctx->num + 1, 0, SHA256_BLOCK_SIZE - ctx->num - 9);
} else {
memset(ctx->block + ctx->num + 1, 0, SHA256_BLOCK_SIZE - ctx->num - 1);
sha256_compress_blocks(ctx->state, ctx->block, 1);
memset(ctx->block, 0, SHA256_BLOCK_SIZE - 8);
}
PUTU32(ctx->block + 56, ctx->nblocks >> 23);
PUTU32(ctx->block + 60, (ctx->nblocks << 9) + (ctx->num << 3));
sha256_compress_blocks(ctx->state, ctx->block, 1);
for (i = 0; i < 8; i++) {
PUTU32(dgst, ctx->state[i]);
dgst += sizeof(uint32_t);
}
memset(ctx, 0, sizeof(SHA256_CTX));
}
#define Ch(X, Y, Z) (((X) & (Y)) ^ ((~(X)) & (Z)))
#define Maj(X, Y, Z) (((X) & (Y)) ^ ((X) & (Z)) ^ ((Y) & (Z)))
#define Sigma0(X) (ROR32((X), 2) ^ ROR32((X), 13) ^ ROR32((X), 22))
@@ -209,9 +135,75 @@ static void sha256_compress_blocks(uint32_t state[8],
}
}
void sha256_compress(uint32_t state[8], const unsigned char block[64])
void sha256_init(SHA256_CTX *ctx)
{
sha256_compress_blocks(state, block, 1);
memset(ctx, 0, sizeof(*ctx));
ctx->state[0] = 0x6a09e667;
ctx->state[1] = 0xbb67ae85;
ctx->state[2] = 0x3c6ef372;
ctx->state[3] = 0xa54ff53a;
ctx->state[4] = 0x510e527f;
ctx->state[5] = 0x9b05688c;
ctx->state[6] = 0x1f83d9ab;
ctx->state[7] = 0x5be0cd19;
}
void sha256_update(SHA256_CTX *ctx, const unsigned char *data, size_t datalen)
{
size_t blocks;
ctx->num &= 0x3f;
if (ctx->num) {
unsigned int left = SHA256_BLOCK_SIZE - ctx->num;
if (datalen < left) {
memcpy(ctx->block + ctx->num, data, datalen);
ctx->num += datalen;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
sha256_compress_blocks(ctx->state, ctx->block, 1);
ctx->nblocks++;
data += left;
datalen -= left;
}
}
blocks = datalen / SHA256_BLOCK_SIZE;
sha256_compress_blocks(ctx->state, data, blocks);
ctx->nblocks += blocks;
data += SHA256_BLOCK_SIZE * blocks;
datalen -= SHA256_BLOCK_SIZE * blocks;
ctx->num = datalen;
if (datalen) {
memcpy(ctx->block, data, datalen);
}
}
void sha256_finish(SHA256_CTX *ctx, unsigned char dgst[SHA256_DIGEST_SIZE])
{
int i;
ctx->num &= 0x3f;
ctx->block[ctx->num] = 0x80;
if (ctx->num <= SHA256_BLOCK_SIZE - 9) {
memset(ctx->block + ctx->num + 1, 0, SHA256_BLOCK_SIZE - ctx->num - 9);
} else {
memset(ctx->block + ctx->num + 1, 0, SHA256_BLOCK_SIZE - ctx->num - 1);
sha256_compress_blocks(ctx->state, ctx->block, 1);
memset(ctx->block, 0, SHA256_BLOCK_SIZE - 8);
}
PUTU32(ctx->block + 56, ctx->nblocks >> 23);
PUTU32(ctx->block + 60, (ctx->nblocks << 9) + (ctx->num << 3));
sha256_compress_blocks(ctx->state, ctx->block, 1);
for (i = 0; i < 8; i++) {
PUTU32(dgst, ctx->state[i]);
dgst += sizeof(uint32_t);
}
memset(ctx, 0, sizeof(*ctx));
}
void sha256_digest(const unsigned char *data, size_t datalen,
@@ -244,17 +236,12 @@ void sha224_update(SHA224_CTX *ctx, const unsigned char *data, size_t datalen)
void sha224_finish(SHA224_CTX *ctx, unsigned char dgst[SHA224_DIGEST_SIZE])
{
unsigned char buf[SHA256_DIGEST_SIZE];
uint8_t buf[SHA256_DIGEST_SIZE];
sha256_finish((SHA256_CTX *)ctx, buf);
memcpy(dgst, buf, SHA224_DIGEST_SIZE);
memset(buf, 0, sizeof(buf));
}
void sha224_compress(uint32_t state[8], const unsigned char block[64])
{
sha256_compress_blocks(state, block, 1);
}
void sha224_digest(const unsigned char *data, size_t datalen,
unsigned char dgst[SHA224_DIGEST_SIZE])
{

View File

@@ -236,7 +236,7 @@ static int bn_print(FILE *fp, const bignum_t a, int format, int indent)
if (a[i] >= ((uint64_t)1 << 32)) {
printf("bn_print check failed\n");
}
ret += fprintf(fp, "%08llx", a[i]);
ret += fprintf(fp, "%08x", (uint32_t)a[i]);
}
ret += fprintf(fp, "\n");
return ret;
@@ -340,8 +340,9 @@ static void bn_rand_range(bignum_t r, const bignum_t range)
static void fp_add(bignum_t r, const bignum_t a, const bignum_t b)
{
bn_add(r, a, b);
if (bn_cmp(r, SM2_P) >= 0)
return bn_sub(r, r, SM2_P);
if (bn_cmp(r, SM2_P) >= 0) {
bn_sub(r, r, SM2_P);
}
}
static void fp_sub(bignum_t r, const bignum_t a, const bignum_t b)
@@ -540,7 +541,7 @@ static void fn_add(bignum_t r, const bignum_t a, const bignum_t b)
{
bn_add(r, a, b);
if (bn_cmp(r, SM2_N) >= 0) {
return bn_sub(r, r, SM2_N);
bn_sub(r, r, SM2_N);
}
}
@@ -714,7 +715,7 @@ static void fn_mul(bignum_t r, const bignum_t a, const bignum_t b)
static void fn_sqr(bignum_t r, const bignum_t a)
{
return fn_mul(r, a, a);
fn_mul(r, a, a);
}
static void fn_exp(bignum_t r, const bignum_t a, const bignum_t e)
@@ -747,7 +748,7 @@ static void fn_inv(bignum_t r, const bignum_t a)
static void fn_rand(bignum_t r)
{
return bn_rand_range(r, SM2_N);
bn_rand_range(r, SM2_N);
}
#define hex_fp_add_x_y "eefbe4cf140ff8b5b956d329d5a2eae8608c933cb89053217439786e54866567"
@@ -1011,8 +1012,8 @@ static void point_dbl(point_t *R, const point_t *P)
}
// 这个函数有一个严重的问题就是我们假定Q是一个已经正规化的点
// 因此如果这个点不是仿射坐标的就出现问题了
// FIXME: Q must be affine coordinate
// change API!
static void point_add(point_t *R, const point_t *P, const point_t *Q)
{
const uint64_t *X1 = P->X;
@@ -1090,7 +1091,7 @@ static void point_mul(point_t *R, const bignum_t k, const point_t *P)
point_t _T, *T = &_T;
int i;
// point_add要求输入的P必须为仿射坐标
// FIXME: point_add need affine, so we can not use point_add
if (!bn_is_one(P->Z)) {
bignum_t x;
bignum_t y;
@@ -1600,7 +1601,7 @@ int sm2_do_decrypt(const SM2_KEY *key, const SM2_CIPHERTEXT *in, uint8_t *out, s
uint8_t hash[32];
int i;
// FIXME: 检查SM2_CIPHERTEXT格式
// FIXME: check SM2_CIPHERTEXT format
// check C1
point_from_bytes(P, (uint8_t *)&in->point);

View File

@@ -90,7 +90,7 @@ int sm2_point_from_der(SM2_POINT *a, const uint8_t **in, size_t *inlen)
int ret;
const uint8_t *data;
size_t datalen;
error_print("inlen = %zu\n", *inlen);
error_print_msg("inlen = %zu\n", *inlen);
if ((ret = asn1_octet_string_from_der(&data, &datalen, in, inlen)) != 1) {
if (ret < 0) error_print();
@@ -101,7 +101,7 @@ int sm2_point_from_der(SM2_POINT *a, const uint8_t **in, size_t *inlen)
error_print();
return -1;
}
error_print("inlen = %zu\n", *inlen);
error_print_msg("inlen = %zu\n", *inlen);
return 1;
}

328
src/sm3.c
View File

@@ -1,5 +1,5 @@
/* ====================================================================
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
/*
* Copyright (c) 2014 - 2021 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
@@ -44,13 +44,10 @@
* 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 <string.h>
#include <gmssl/sm3.h>
//#include "bswap.h"
//#include "rotate.h"
#include "endian.h"
#ifdef SM3_SSE3
@@ -61,165 +58,6 @@
_mm_xor_si128(_mm_slli_epi32((X),(i)), _mm_srli_epi32((X),32-(i)))
#endif
void sm3_compress_blocks(uint32_t digest[8], const uint8_t *data, size_t blocks);
void sm3_init(SM3_CTX *ctx)
{
memset(ctx, 0, sizeof(*ctx));
ctx->digest[0] = 0x7380166F;
ctx->digest[1] = 0x4914B2B9;
ctx->digest[2] = 0x172442D7;
ctx->digest[3] = 0xDA8A0600;
ctx->digest[4] = 0xA96F30BC;
ctx->digest[5] = 0x163138AA;
ctx->digest[6] = 0xE38DEE4D;
ctx->digest[7] = 0xB0FB0E4E;
}
#if 0
void sm3_compute_id_digest(uint8_t z[32], const char *id,
const uint8_t x[32], const uint8_t y[32])
{
uint8_t zin[] = {
0x00, 0x80,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
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,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x06, 0x90,
};
if (!id || strcmp(id, "1234567812345678")) {
unsigned int digest[8] = {
0xadadedb5U, 0x0446043fU, 0x08a87aceU, 0xe86d2243U,
0x8e232383U, 0xbfc81fe2U, 0xcf9117c8U, 0x4707011dU,
};
memcpy(&zin[128], x, 32);
memcpy(&zin[160], y, 32);
sm3_compress_blocks(digest, zin, 2);
PUTU32(z , digest[0]);
PUTU32(z + 4, digest[1]);
PUTU32(z + 8, digest[2]);
PUTU32(z + 12, digest[3]);
PUTU32(z + 16, digest[4]);
PUTU32(z + 20, digest[5]);
PUTU32(z + 24, digest[6]);
PUTU32(z + 28, digest[7]);
} else {
SM3_CTX ctx;
uint8_t idbits[2];
size_t len;
len = strlen(id);
idbits[0] = (uint8_t)(len >> 5);
idbits[1] = (uint8_t)(len << 3);
sm3_init(&ctx);
sm3_update(&ctx, idbits, 2);
sm3_update(&ctx, (uint8_t *)id, len);
sm3_update(&ctx, zin + 18, 128);
sm3_update(&ctx, x, 32);
sm3_update(&ctx, y, 32);
sm3_finish(&ctx, z);
}
}
int sm3_sm2_init(SM3_CTX *ctx, const char *id,
const uint8_t *x, const uint8_t *y)
{
uint8_t z[32];
if ((id && strlen(id) > 65535/8) || !x || !y) {
return 0;
}
sm3_compute_id_digest(z, id, x, y);
sm3_init(ctx);
sm3_update(ctx, z, 32);
return 1;
}
#endif
void sm3_update(SM3_CTX *ctx, const uint8_t *data, size_t data_len)
{
size_t blocks;
if (ctx->num) {
unsigned int left = SM3_BLOCK_SIZE - ctx->num;
if (data_len < left) {
memcpy(ctx->block + ctx->num, data, data_len);
ctx->num += data_len;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
sm3_compress_blocks(ctx->digest, ctx->block, 1);
ctx->nblocks++;
data += left;
data_len -= left;
}
}
blocks = data_len / SM3_BLOCK_SIZE;
sm3_compress_blocks(ctx->digest, data, blocks);
ctx->nblocks += blocks;
data += SM3_BLOCK_SIZE * blocks;
data_len -= SM3_BLOCK_SIZE * blocks;
ctx->num = data_len;
if (data_len) {
memcpy(ctx->block, data, data_len);
}
}
void sm3_finish(SM3_CTX *ctx, uint8_t *digest)
{
int i;
ctx->block[ctx->num] = 0x80;
if (ctx->num + 9 <= SM3_BLOCK_SIZE) {
memset(ctx->block + ctx->num + 1, 0, SM3_BLOCK_SIZE - ctx->num - 9);
} else {
memset(ctx->block + ctx->num + 1, 0, SM3_BLOCK_SIZE - ctx->num - 1);
sm3_compress(ctx->digest, ctx->block);
memset(ctx->block, 0, SM3_BLOCK_SIZE - 8);
}
PUTU32(ctx->block + 56, ctx->nblocks >> 23);
PUTU32(ctx->block + 60, (ctx->nblocks << 9) + (ctx->num << 3));
sm3_compress(ctx->digest, ctx->block);
for (i = 0; i < 8; i++) {
PUTU32(digest + i*4, ctx->digest[i]);
}
memset(ctx, 0, sizeof(SM3_CTX));
}
#define ROTL(x,n) (((x)<<(n)) | ((x)>>(32-(n))))
#define P0(x) ((x) ^ ROL32((x), 9) ^ ROL32((x),17))
@@ -492,9 +330,167 @@ void sm3_compress_blocks(uint32_t digest[8], const uint8_t *data, size_t blocks)
}
}
void sm3_compress(uint32_t digest[8], const uint8_t block[64])
void sm3_init(SM3_CTX *ctx)
{
return sm3_compress_blocks(digest, block, 1);
memset(ctx, 0, sizeof(*ctx));
ctx->digest[0] = 0x7380166F;
ctx->digest[1] = 0x4914B2B9;
ctx->digest[2] = 0x172442D7;
ctx->digest[3] = 0xDA8A0600;
ctx->digest[4] = 0xA96F30BC;
ctx->digest[5] = 0x163138AA;
ctx->digest[6] = 0xE38DEE4D;
ctx->digest[7] = 0xB0FB0E4E;
}
#if 0
void sm3_compute_id_digest(uint8_t z[32], const char *id,
const uint8_t x[32], const uint8_t y[32])
{
uint8_t zin[] = {
0x00, 0x80,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
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,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x06, 0x90,
};
if (!id || strcmp(id, "1234567812345678")) {
unsigned int digest[8] = {
0xadadedb5U, 0x0446043fU, 0x08a87aceU, 0xe86d2243U,
0x8e232383U, 0xbfc81fe2U, 0xcf9117c8U, 0x4707011dU,
};
memcpy(&zin[128], x, 32);
memcpy(&zin[160], y, 32);
sm3_compress_blocks(digest, zin, 2);
PUTU32(z , digest[0]);
PUTU32(z + 4, digest[1]);
PUTU32(z + 8, digest[2]);
PUTU32(z + 12, digest[3]);
PUTU32(z + 16, digest[4]);
PUTU32(z + 20, digest[5]);
PUTU32(z + 24, digest[6]);
PUTU32(z + 28, digest[7]);
} else {
SM3_CTX ctx;
uint8_t idbits[2];
size_t len;
len = strlen(id);
idbits[0] = (uint8_t)(len >> 5);
idbits[1] = (uint8_t)(len << 3);
sm3_init(&ctx);
sm3_update(&ctx, idbits, 2);
sm3_update(&ctx, (uint8_t *)id, len);
sm3_update(&ctx, zin + 18, 128);
sm3_update(&ctx, x, 32);
sm3_update(&ctx, y, 32);
sm3_finish(&ctx, z);
}
}
int sm3_sm2_init(SM3_CTX *ctx, const char *id,
const uint8_t *x, const uint8_t *y)
{
uint8_t z[32];
if ((id && strlen(id) > 65535/8) || !x || !y) {
return 0;
}
sm3_compute_id_digest(z, id, x, y);
sm3_init(ctx);
sm3_update(ctx, z, 32);
return 1;
}
#endif
void sm3_update(SM3_CTX *ctx, const uint8_t *data, size_t data_len)
{
size_t blocks;
ctx->num &= 0x3f;
if (ctx->num) {
unsigned int left = SM3_BLOCK_SIZE - ctx->num;
if (data_len < left) {
memcpy(ctx->block + ctx->num, data, data_len);
ctx->num += data_len;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
sm3_compress_blocks(ctx->digest, ctx->block, 1);
ctx->nblocks++;
data += left;
data_len -= left;
}
}
blocks = data_len / SM3_BLOCK_SIZE;
sm3_compress_blocks(ctx->digest, data, blocks);
ctx->nblocks += blocks;
data += SM3_BLOCK_SIZE * blocks;
data_len -= SM3_BLOCK_SIZE * blocks;
ctx->num = data_len;
if (data_len) {
memcpy(ctx->block, data, data_len);
}
}
void sm3_finish(SM3_CTX *ctx, uint8_t *digest)
{
int i;
ctx->num &= 0x3f;
ctx->block[ctx->num] = 0x80;
if (ctx->num <= SM3_BLOCK_SIZE - 9) {
memset(ctx->block + ctx->num + 1, 0, SM3_BLOCK_SIZE - ctx->num - 9);
} else {
memset(ctx->block + ctx->num + 1, 0, SM3_BLOCK_SIZE - ctx->num - 1);
sm3_compress_blocks(ctx->digest, ctx->block, 1);
memset(ctx->block, 0, SM3_BLOCK_SIZE - 8);
}
PUTU32(ctx->block + 56, ctx->nblocks >> 23);
PUTU32(ctx->block + 60, (ctx->nblocks << 9) + (ctx->num << 3));
sm3_compress_blocks(ctx->digest, ctx->block, 1);
for (i = 0; i < 8; i++) {
PUTU32(digest + i*4, ctx->digest[i]);
}
memset(ctx, 0, sizeof(SM3_CTX));
}
void sm3_digest(const uint8_t *msg, size_t msglen,

View File

@@ -110,26 +110,7 @@ void sm3_hmac_finish(SM3_HMAC_CTX *ctx, uint8_t mac[SM3_HMAC_SIZE])
sm3_update(&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE);
sm3_update(&ctx->sm3_ctx, mac, SM3_DIGEST_SIZE);
sm3_finish(&ctx->sm3_ctx, mac);
}
int sm3_hmac_finish_and_verify(SM3_HMAC_CTX *ctx, const uint8_t mac[SM3_HMAC_SIZE])
{
uint8_t buf[32];
sm3_hmac_finish(ctx, buf);
if (memcmp(mac, buf, sizeof(buf)) == 0) {
return 1;
} else {
error_print("sm3_hmac verify failure");
return 0;
}
}
void sm3_hmac_reset(SM3_HMAC_CTX *ctx)
{
sm3_init(&ctx->sm3_ctx);
sm3_update(&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE);
// 不应该保留原始密钥而是应该保持这次update之后的状态
// 这样可以降低reset的工作量
memset(ctx, 0, sizeof(*ctx));
}
void sm3_hmac(const uint8_t *data, size_t data_len,
@@ -140,5 +121,4 @@ void sm3_hmac(const uint8_t *data, size_t data_len,
sm3_hmac_init(&ctx, key, key_len);
sm3_hmac_update(&ctx, data, data_len);
sm3_hmac_finish(&ctx, mac);
memset(&ctx, 0, sizeof(ctx));
}

View File

@@ -81,7 +81,7 @@ int sm4_cbc_padding_encrypt(const SM4_KEY *key, const uint8_t iv[16],
{
uint8_t block[16];
size_t rem = inlen % 16;
int padding = 16 - rem;
int padding = 16 - inlen % 16;
if (in) {
memcpy(block, in + inlen - rem, rem);
@@ -102,14 +102,15 @@ int sm4_cbc_padding_decrypt(const SM4_KEY *key, const uint8_t iv[16],
uint8_t *out, size_t *outlen)
{
uint8_t block[16];
size_t len = sizeof(block);
int padding;
if (inlen == 0) {
error_print("warning: input lenght = 0");
error_puts("warning: input lenght = 0");
return 0;
}
if (inlen%16 != 0 || inlen < 16) {
error_print("invalid cbc ciphertext length");
error_puts("invalid cbc ciphertext length");
return -1;
}
if (inlen > 16) {
@@ -122,7 +123,8 @@ int sm4_cbc_padding_decrypt(const SM4_KEY *key, const uint8_t iv[16],
error_print();
return -1;
}
memcpy(out + inlen - 16, block, 16 - padding);
len -= padding;
memcpy(out + inlen - 16, block, len);
*outlen = inlen - padding;
return 1;
}

View File

@@ -341,7 +341,7 @@ int tlcp_connect(TLS_CONNECT *conn, const char *hostname, int port,
return -1;
}
if ( sm2_verify_finish(&verify_ctx, sig, siglen) != 1) {
error_print("ServerKeyExchange signature verification failure");
error_puts("ServerKeyExchange signature verification failure");
return -1;
}
@@ -535,7 +535,7 @@ int tlcp_connect(TLS_CONNECT *conn, const char *hostname, int port,
return -1;
}
if (memcmp(local_verify_data, verify_data, 12) != 0) {
error_print("server_finished.verify_data verification failure");
error_puts("server_finished.verify_data verification failure");
return -1;
}
@@ -595,7 +595,7 @@ int tlcp_accept(TLS_CONNECT *conn, int port,
return -1;
}
error_print("start listen ...");
error_puts("start listen ...");
listen(sock, 5);
memset(conn, 0, sizeof(*conn));
@@ -608,7 +608,7 @@ int tlcp_accept(TLS_CONNECT *conn, int port,
return -1;
}
error_print("connected\n");
error_puts("connected\n");
@@ -638,7 +638,7 @@ int tlcp_accept(TLS_CONNECT *conn, int port,
}
}
if (conn->cipher_suite == 0) {
error_print("no common cipher_suite");
error_puts("no common cipher_suite");
return -1;
}
sm3_update(&sm3_ctx, record + 5, recordlen - 5);
@@ -893,7 +893,7 @@ int tlcp_accept(TLS_CONNECT *conn, int port,
return -1;
}
if (memcmp(local_verify_data, verify_data, 12) != 0) {
error_print("client_finished.verify_data verification failure");
error_puts("client_finished.verify_data verification failure");
return -1;
}

View File

@@ -337,7 +337,7 @@ int tls_cbc_encrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *enc_key,
return -1;
}
if (inlen > (1 << 14)) {
error_print("invalid tls record data length %zu\n", inlen);
error_print_msg("invalid tls record data length %zu\n", inlen);
return -1;
}
@@ -384,6 +384,7 @@ int tls_cbc_decrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *dec_key,
const uint8_t *mac;
uint8_t header[5];
int padding_len;
uint8_t hmac[32];
int i;
if (!inited_hmac_ctx || !dec_key || !seq_num || !enced_header || !in || !inlen || !out || !outlen) {
@@ -393,7 +394,7 @@ int tls_cbc_decrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *dec_key,
if (inlen % 16
|| inlen < (16 + 0 + 32 + 16) // iv + data + mac + padding
|| inlen > (16 + (1<<14) + 32 + 256)) {
error_print("invalid tls cbc ciphertext length %zu\n", inlen);
error_print_msg("invalid tls cbc ciphertext length %zu\n", inlen);
return -1;
}
@@ -411,7 +412,7 @@ int tls_cbc_decrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *dec_key,
}
for (i = 0; i < padding_len; i++) {
if (padding[i] != padding_len) {
error_print("tls ciphertext cbc-padding check failure");
error_puts("tls ciphertext cbc-padding check failure");
return -1;
}
}
@@ -428,8 +429,9 @@ int tls_cbc_decrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *dec_key,
sm3_hmac_update(&hmac_ctx, seq_num, 8);
sm3_hmac_update(&hmac_ctx, header, 5);
sm3_hmac_update(&hmac_ctx, out, *outlen);
if (sm3_hmac_finish_and_verify(&hmac_ctx, mac) != 1) {
error_print("tls ciphertext mac check failure");
sm3_hmac_finish(&hmac_ctx, hmac);
if (memcmp(mac, hmac, sizeof(hmac)) != 0) { //FIXME: const time memcmp!
error_puts("tls ciphertext mac check failure");
return -1;
}
return 1;
@@ -634,7 +636,7 @@ int tls_record_set_handshake(uint8_t *record, size_t *recordlen,
return -1;
}
if (datalen > (1 << 14) - 4) {
error_print("gmssl does not support handshake longer than record");
error_puts("gmssl does not support handshake longer than record");
return -1;
}
handshakelen = 4 + datalen;
@@ -885,7 +887,7 @@ int tls_record_get_handshake_server_hello(const uint8_t *record,
return -1;
}
if (!tls_cipher_suite_name(*cipher_suite)) {
error_print("unknown server cipher_suite 0x%04x", *cipher_suite);
error_print_msg("unknown server cipher_suite 0x%04x", *cipher_suite);
return -1;
}
if (comp_meth != TLS_compression_null) {
@@ -894,7 +896,7 @@ int tls_record_get_handshake_server_hello(const uint8_t *record,
}
if (len > 0) {
if (tls_record_version(record) < TLS_version_tls12) {
error_print("warning: should not have extentions");
error_puts("warning: should not have extentions");
return -1;
}
// FIXME: 用 tls_extensions_from_bytes() 解析
@@ -1355,7 +1357,7 @@ int tls_record_get_alert(const uint8_t *record,
return -1;
}
if (!tls_alert_description_text(*alert_description)) {
error_print("warning");
error_puts("warning");
return -1;
}
return 1;
@@ -1394,7 +1396,7 @@ int tls_record_get_change_cipher_spec(const uint8_t *record)
return -1;
}
if (record[5] != TLS_change_cipher_spec) {
error_print("unknown ChangeCipherSpec value %d", record[5]);
error_print_msg("unknown ChangeCipherSpec value %d", record[5]);
return -1;
}
return 1;
@@ -1470,11 +1472,11 @@ int tls_record_recv(uint8_t *record, size_t *recordlen, int sock)
}
if (!tls_record_type_name(record[0])) {
error_print("invalid record type: %d\n", record[0]);
error_print_msg("invalid record type: %d\n", record[0]);
return -1;
}
if (!tls_version_text(tls_record_version(record))) {
error_print("invalid record version: %d.%d\n", record[1], record[2]);
error_print_msg("invalid record version: %d.%d\n", record[1], record[2]);
return -1;
}
len = (size_t)record[3] << 8 | record[4];
@@ -1589,3 +1591,9 @@ int tls_recv(TLS_CONNECT *conn, uint8_t *data, size_t *datalen)
*datalen = mlen - 5;
return 1;
}
//FIXME: any difference in TLS 1.2 and TLS 1.3?
int tls_shutdown(TLS_CONNECT *conn)
{
return -1;
}

View File

@@ -560,7 +560,7 @@ int tls12_connect(TLS_CONNECT *conn, const char *hostname, int port,
sm3_hash, 32, NULL, 0,
12, local_verify_data);
if (memcmp(local_verify_data, verify_data, 12) != 0) {
error_print("server_finished.verify_data verification failure");
error_puts("server_finished.verify_data verification failure");
return -1;
}
@@ -625,7 +625,7 @@ int tls12_accept(TLS_CONNECT *conn, int port,
return -1;
}
error_print("start listen ...");
error_puts("start listen ...");
listen(sock, 5);
memset(conn, 0, sizeof(*conn));
@@ -638,7 +638,7 @@ int tls12_accept(TLS_CONNECT *conn, int port,
return -1;
}
error_print("connected\n");
error_puts("connected\n");
@@ -673,7 +673,7 @@ int tls12_accept(TLS_CONNECT *conn, int port,
}
}
if (conn->cipher_suite == 0) {
error_print("no common cipher_suite");
error_puts("no common cipher_suite");
return -1;
}
sm3_update(&sm3_ctx, record + 5, recordlen - 5);
@@ -930,7 +930,7 @@ int tls12_accept(TLS_CONNECT *conn, int port,
sm3_hash, 32, NULL, 0,
12, local_verify_data);
if (memcmp(local_verify_data, verify_data, 12) != 0) {
error_print("client_finished.verify_data verification failure");
error_puts("client_finished.verify_data verification failure");
return -1;
}

View File

@@ -1709,7 +1709,7 @@ int tls13_accept(TLS_CONNECT *conn, int port,
return -1;
}
error_print("start listen ...");
error_puts("start listen ...");
listen(sock, 5);
memset(conn, 0, sizeof(*conn));
@@ -1722,7 +1722,7 @@ int tls13_accept(TLS_CONNECT *conn, int port,
return -1;
}
error_print("connected\n");
error_puts("connected\n");
// 1. Recv ClientHello
@@ -1753,7 +1753,7 @@ int tls13_accept(TLS_CONNECT *conn, int port,
}
}
if (conn->cipher_suite == 0) {
error_print("no common cipher_suite");
error_puts("no common cipher_suite");
return -1;
}
if (tls13_client_hello_extensions_get(exts, extslen, &client_ecdhe_public) != 1) {

View File

@@ -222,7 +222,7 @@ const char *tls_alert_level_name(int level)
case TLS_alert_level_warning: return "warning";
case TLS_alert_level_fatal: return "fatal";
}
error_print("unknown alert level %d", level);
error_print_msg("unknown alert level %d", level);
return NULL;
}
@@ -260,7 +260,7 @@ const char *tls_alert_description_text(int description)
case TLS_alert_unsupported_ibcparam: return "unsupported_ibcparam";
case TLS_alert_identity_need: return "identity_need";
}
error_print("unknown alert description %d", description);
error_print_msg("unknown alert description %d", description);
return NULL;
}

View File

@@ -182,7 +182,7 @@ int x509_encryption_algor_from_der(int *cipher,
*cipher = OID_sm4_cbc;
} else {
size_t i;
error_print("unknown cipher oid :");
error_puts("unknown cipher oid :");
for (i = 0; i < nodes_count; i++) {
fprintf(stderr, " %d", nodes[i]);
}
@@ -273,7 +273,7 @@ int x509_signature_algor_from_der(int *oid, const uint8_t **in, size_t *inlen)
return -1;
}
if (datalen > 0) {
error_print("datalen = %zu", datalen);
error_print_msg("datalen = %zu", datalen);
error_print();
return -1;
}

View File

@@ -79,7 +79,7 @@ int x509_version_to_der(int version, uint8_t **out, size_t *outlen)
case X509_version_v3:
break;
default:
error_print("invalid version");
error_puts("invalid version");
return -1;
}
if (asn1_int_to_der(version, NULL, &len) != 1
@@ -112,7 +112,7 @@ int x509_version_from_der(int *version, const uint8_t **in, size_t *inlen)
}
switch (*version) {
case X509_version_v1:
error_print("warning: version v1 should not be encoded");
error_puts("warning: version v1 should not be encoded");
break;
case X509_version_v2:
case X509_version_v3:
@@ -260,10 +260,10 @@ int x509_directory_string_to_der(int tag, const char *a, size_t alen, uint8_t **
case ASN1_TAG_TeletexString:
case ASN1_TAG_UniversalString:
case ASN1_TAG_BMPString:
error_print("not implemented");
error_print();
return -1;
default:
error_print("invalid tag");
error_print();
return -1;
}
@@ -293,7 +293,7 @@ int x509_directory_string_from_der(int *tag, const char **a, size_t *alen, const
case ASN1_TAG_BMPString:
break;
default:
error_print("DirectoryString tag = %d\n", *tag);
error_print();
return -1;
}
return 1;
@@ -335,7 +335,7 @@ static int x509_rdn_check(int oid, int tag, const char *str, int len)
case ASN1_TAG_BMPString:
break;
default:
error_print("tag = %d\n", tag);
error_print();
return -1;
}
if (x509_rdns[i].is_printable_string_only && tag != ASN1_TAG_PrintableString) {
@@ -511,7 +511,7 @@ const char *x509_name_rdn(const X509_NAME *name, int oid)
case OID_at_dnQualifier:
return name->dn_qualifier;
}
error_print("unsupported X509 NAME OID %d\n", oid);
error_print();
return NULL;
}
@@ -1015,7 +1015,7 @@ int x509_tbs_certificate_from_der(X509_TBS_CERTIFICATE *a, const uint8_t **in, s
|| (is_ext = x509_extensions_from_der(&a->extensions, &data, &datalen)) < 0
|| datalen > 0) {
error_print();
if (datalen > 0) error_print("datalen = %zu\n", datalen);
if (datalen > 0) error_print();
return -1;
}

View File

@@ -355,7 +355,7 @@ int x509_key_purpose_to_der(int oid, uint8_t **out, size_t *outlen)
return 1;
}
}
error_print("unknown key purpose oid %d", oid);
error_print_msg("unknown key purpose oid %d", oid);
return -1;
}
@@ -382,7 +382,7 @@ int x509_key_purpose_from_der(int *oid, const uint8_t **in, size_t *inlen)
}
}
// 这种情况下应该把这个值打印出来
error_print("unknown ExtKeyUsage OID");
error_puts("unknown ExtKeyUsage OID");
return -1;
}

View File

@@ -388,8 +388,11 @@ void zuc_mac_finish(ZUC_MAC_CTX *ctx, const unsigned char *data, size_t nbits, u
ctx->T = T;
PUTU32(mac, T);
memset(ctx, 0, sizeof(*ctx));
}
typedef unsigned char ZUC_UINT7;
static const ZUC_UINT7 ZUC256_D[][16] = {
@@ -598,4 +601,6 @@ void zuc256_mac_finish(ZUC256_MAC_CTX *ctx, const unsigned char *data, size_t nb
PUTU32(mac, ctx->T[j]);
mac += 4;
}
memset(ctx, 0, sizeof(*ctx));
}