Adjust digest APIs

This commit is contained in:
Zhi Guan
2024-04-19 21:14:10 +08:00
parent 8cb306ad0b
commit 6f870889cf
11 changed files with 14 additions and 147 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
@@ -36,7 +36,6 @@ typedef struct {
void sha1_init(SHA1_CTX *ctx);
void sha1_update(SHA1_CTX *ctx, const uint8_t *data, size_t datalen);
void sha1_finish(SHA1_CTX *ctx, uint8_t dgst[SHA1_DIGEST_SIZE]);
void sha1_digest(const uint8_t *data, size_t datalen, uint8_t dgst[SHA1_DIGEST_SIZE]);
#ifdef __cplusplus

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
@@ -38,8 +38,6 @@ typedef struct {
void sha224_init(SHA224_CTX *ctx);
void sha224_update(SHA224_CTX *ctx, const uint8_t* data, size_t datalen);
void sha224_finish(SHA224_CTX *ctx, uint8_t dgst[SHA224_DIGEST_SIZE]);
void sha224_digest(const uint8_t *data, size_t datalen,
uint8_t dgst[SHA224_DIGEST_SIZE]);
#define SHA256_DIGEST_SIZE 32
@@ -56,8 +54,6 @@ typedef struct {
void sha256_init(SHA256_CTX *ctx);
void sha256_update(SHA256_CTX *ctx, const uint8_t* data, size_t datalen);
void sha256_finish(SHA256_CTX *ctx, uint8_t dgst[SHA256_DIGEST_SIZE]);
void sha256_digest(const uint8_t *data, size_t datalen,
uint8_t dgst[SHA256_DIGEST_SIZE]);
#define SHA384_DIGEST_SIZE 48
@@ -74,8 +70,6 @@ typedef struct {
void sha384_init(SHA384_CTX *ctx);
void sha384_update(SHA384_CTX *ctx, const uint8_t* data, size_t datalen);
void sha384_finish(SHA384_CTX *ctx, uint8_t dgst[SHA384_DIGEST_SIZE]);
void sha384_digest(const uint8_t *data, size_t datalen,
uint8_t dgst[SHA384_DIGEST_SIZE]);
#define SHA512_DIGEST_SIZE 64
@@ -92,8 +86,6 @@ typedef struct {
void sha512_init(SHA512_CTX *ctx);
void sha512_update(SHA512_CTX *ctx, const uint8_t* data, size_t datalen);
void sha512_finish(SHA512_CTX *ctx, uint8_t dgst[SHA512_DIGEST_SIZE]);
void sha512_digest(const uint8_t *data, size_t datalen,
uint8_t dgst[SHA512_DIGEST_SIZE]);
#ifdef __cplusplus

View File

@@ -31,13 +31,13 @@ typedef struct {
} SM2_KEY;
_gmssl_export int sm2_key_generate(SM2_KEY *key);
_gmssl_export int sm2_key_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY *key);
int sm2_key_set_private_key(SM2_KEY *key, const sm2_z256_t private_key);
int sm2_key_set_public_key(SM2_KEY *key, const SM2_Z256_POINT *public_key);
int sm2_key_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY *key);
int sm2_public_key_equ(const SM2_KEY *sm2_key, const SM2_KEY *pub_key);
int sm2_public_key_digest(const SM2_KEY *key, uint8_t dgst[32]);
int sm2_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY *pub_key);
_gmssl_export int sm2_public_key_equ(const SM2_KEY *sm2_key, const SM2_KEY *pub_key);
_gmssl_export int sm2_public_key_digest(const SM2_KEY *key, uint8_t dgst[32]);
_gmssl_export int sm2_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY *pub_key);
/*
@@ -119,11 +119,6 @@ _gmssl_export int sm2_private_key_info_decrypt_from_pem(SM2_KEY *key, const char
typedef struct {
uint8_t r[32];
uint8_t s[32];
@@ -138,7 +133,6 @@ int sm2_fast_sign(const sm2_z256_t fast_private, const sm2_z256_t k, const sm2_z
const uint8_t dgst[32], SM2_SIGNATURE *sig);
#define SM2_MIN_SIGNATURE_SIZE 8
#define SM2_MAX_SIGNATURE_SIZE 72
int sm2_signature_to_der(const SM2_SIGNATURE *sig, uint8_t **out, size_t *outlen);
@@ -254,6 +248,7 @@ _gmssl_export int sm2_decrypt_init(SM2_ENC_CTX *ctx, const SM2_KEY *sm2_key);
_gmssl_export int sm2_decrypt_update(SM2_ENC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
_gmssl_export int sm2_decrypt_finish(SM2_ENC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
#ifdef __cplusplus
}
#endif

View File

@@ -32,12 +32,11 @@ typedef struct {
size_t num;
} SM3_CTX;
void sm3_compress_blocks(uint32_t digest[8], const uint8_t *data, size_t blocks);
void sm3_init(SM3_CTX *ctx);
void sm3_update(SM3_CTX *ctx, const uint8_t *data, size_t datalen);
void sm3_finish(SM3_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]);
void sm3_digest(const uint8_t *data, size_t datalen, uint8_t dgst[SM3_DIGEST_SIZE]);
void sm3_compress_blocks(uint32_t digest[8], const uint8_t *data, size_t blocks);
#define SM3_HMAC_SIZE (SM3_DIGEST_SIZE)
@@ -50,9 +49,6 @@ typedef struct {
void sm3_hmac_init(SM3_HMAC_CTX *ctx, const uint8_t *key, size_t keylen);
void sm3_hmac_update(SM3_HMAC_CTX *ctx, const uint8_t *data, size_t datalen);
void sm3_hmac_finish(SM3_HMAC_CTX *ctx, uint8_t mac[SM3_HMAC_SIZE]);
void sm3_hmac(const uint8_t *key, size_t keylen,
const uint8_t *data, size_t datalen,
uint8_t mac[SM3_HMAC_SIZE]);
typedef struct {
@@ -63,7 +59,6 @@ typedef struct {
void sm3_kdf_init(SM3_KDF_CTX *ctx, size_t outlen);
void sm3_kdf_update(SM3_KDF_CTX *ctx, const uint8_t *in, size_t inlen);
void sm3_kdf_finish(SM3_KDF_CTX *ctx, uint8_t *out);
void sm3_kdf(const uint8_t *in, size_t inlen, size_t outlen, uint8_t *out);
#define SM3_PBKDF2_MIN_ITER 10000

View File

@@ -1,40 +0,0 @@
/*
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#ifndef GMSSL_SM3_DIGEST_H
#define GMSSL_SM3_DIGEST_H
#include <string.h>
#include <stdint.h>
#include <gmssl/sm3.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
union {
SM3_CTX sm3_ctx;
SM3_HMAC_CTX hmac_ctx;
void *handle;
};
int state;
} SM3_DIGEST_CTX;
int sm3_digest_init(SM3_DIGEST_CTX *ctx, const uint8_t *key, size_t keylen);
int sm3_digest_update(SM3_DIGEST_CTX *ctx, const uint8_t *data, size_t datalen);
int sm3_digest_finish(SM3_DIGEST_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -14,6 +14,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/api.h>
#include <gmssl/sm3.h>
#include <gmssl/sm2.h>
#include <gmssl/sm9_z256.h>

View File

@@ -14,6 +14,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/api.h>
#ifdef __cplusplus
@@ -105,13 +106,9 @@ typedef struct {
size_t block_nbytes;
} ZUC_CTX;
int zuc_encrypt_init(ZUC_CTX *ctx, const uint8_t key[ZUC_KEY_SIZE], const uint8_t iv[ZUC_IV_SIZE]);
int zuc_encrypt_update(ZUC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
int zuc_encrypt_finish(ZUC_CTX *ctx, uint8_t *out, size_t *outlen);
#define zuc_decrypt_init(ctx,key,iv) zuc_encrypt_init(ctx,key,iv)
#define zuc_decrypt_update(ctx,in,inlen,out,outlen) zuc_encrypt_update(ctx,in,inlen,out,outlen)
#define zuc_decrypt_finish(ctx,out,outlen) zuc_encrypt_finish(ctx,out,outlen)
_gmssl_export int zuc_encrypt_init(ZUC_CTX *ctx, const uint8_t key[ZUC_KEY_SIZE], const uint8_t iv[ZUC_IV_SIZE]);
_gmssl_export int zuc_encrypt_update(ZUC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
_gmssl_export int zuc_encrypt_finish(ZUC_CTX *ctx, uint8_t *out, size_t *outlen);
#ifdef __cplusplus

View File

@@ -159,13 +159,3 @@ void sha1_finish(SHA1_CTX *ctx, unsigned char *dgst)
PUTU32(dgst + i*4, ctx->state[i]);
}
}
void sha1_digest(const unsigned char *data, size_t datalen,
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(ctx));
}

View File

@@ -169,16 +169,6 @@ void sha256_finish(SHA256_CTX *ctx, unsigned char dgst[SHA256_DIGEST_SIZE])
}
}
void sha256_digest(const unsigned char *data, size_t datalen,
unsigned char dgst[SHA256_DIGEST_SIZE])
{
SHA256_CTX ctx;
sha256_init(&ctx);
sha256_update(&ctx, data, datalen);
sha256_finish(&ctx, dgst);
memset(&ctx, 0, sizeof(ctx));
}
void sha224_init(SHA224_CTX *ctx)
{
@@ -205,13 +195,3 @@ void sha224_finish(SHA224_CTX *ctx, unsigned char dgst[SHA224_DIGEST_SIZE])
memcpy(dgst, buf, SHA224_DIGEST_SIZE);
memset(buf, 0, sizeof(buf));
}
void sha224_digest(const unsigned char *data, size_t datalen,
unsigned char dgst[SHA224_DIGEST_SIZE])
{
SHA224_CTX ctx;
sha224_init(&ctx);
sha224_update(&ctx, data, datalen);
sha224_finish(&ctx, dgst);
memset(&ctx, 0, sizeof(ctx));
}

View File

@@ -175,21 +175,6 @@ static void sha512_compress_blocks(uint64_t state[8],
}
}
void sha512_compress(uint64_t state[8], const unsigned char block[64])
{
sha512_compress_blocks(state, block, 1);
}
void sha512_digest(const unsigned char *data, size_t datalen,
unsigned char dgst[SHA512_DIGEST_SIZE])
{
SHA512_CTX ctx;
sha512_init(&ctx);
sha512_update(&ctx, data, datalen);
sha512_finish(&ctx, dgst);
memset(&ctx, 0, sizeof(ctx));
}
void sha384_init(SHA384_CTX *ctx)
{
@@ -216,19 +201,3 @@ void sha384_finish(SHA384_CTX *ctx, unsigned char dgst[SHA384_DIGEST_SIZE])
memcpy(dgst, buf, SHA384_DIGEST_SIZE);
memset(buf, 0, sizeof(buf));
}
void sha384_compress(uint64_t state[8], const unsigned char block[64])
{
sha512_compress_blocks(state, block, 1);
}
void sha384_digest(const unsigned char *data, size_t datalen,
unsigned char dgst[SHA384_DIGEST_SIZE])
{
SHA384_CTX ctx;
sha384_init(&ctx);
sha384_update(&ctx, data, datalen);
sha384_finish(&ctx, dgst);
memset(&ctx, 0, sizeof(ctx));
}

View File

@@ -73,14 +73,3 @@ void sm3_hmac_finish(SM3_HMAC_CTX *ctx, uint8_t mac[SM3_HMAC_SIZE])
sm3_update(&ctx->sm3_ctx, mac, SM3_DIGEST_SIZE);
sm3_finish(&ctx->sm3_ctx, mac);
}
void sm3_hmac(const uint8_t *key, size_t key_len,
const uint8_t *data, size_t data_len,
uint8_t mac[SM3_HMAC_SIZE])
{
SM3_HMAC_CTX ctx;
sm3_hmac_init(&ctx, key, key_len);
sm3_hmac_update(&ctx, data, data_len);
sm3_hmac_finish(&ctx, mac);
memset(&ctx, 0, sizeof(ctx));
}