Skip to content

Commit

Permalink
Adjust SM4 source files
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Apr 10, 2024
1 parent ccab5f1 commit 2d0378f
Show file tree
Hide file tree
Showing 13 changed files with 1,029 additions and 911 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ set(src
src/version.c
src/debug.c
src/sm4.c
src/sm4_modes.c
src/sm4_cbc.c
src/sm4_ctr.c
src/sm4_gcm.c
src/sm3.c
src/sm3_hmac.c
src/sm3_kdf.c
Expand All @@ -40,8 +42,10 @@ set(src
src/hkdf.c
src/pbkdf2.c
src/gf128.c
src/ghash.c
src/gcm.c
src/aead.c
src/sm4_cbc_sm3_hmac.c
src/sm4_ctr_sm3_hmac.c
src/pkcs8.c
src/ec.c
src/rsa.c
Expand Down
25 changes: 0 additions & 25 deletions include/gmssl/aead.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,7 @@ _gmssl_export int sm4_ctr_sm3_hmac_decrypt_finish(SM4_CTR_SM3_HMAC_CTX *ctx,
uint8_t *out, size_t *outlen);


typedef struct {
SM4_CTR_CTX enc_ctx;
GHASH_CTX mac_ctx;
uint8_t Y[16]; // E(K, Y_0)
size_t taglen;
uint8_t mac[16];
size_t maclen;
} SM4_GCM_CTX;

#define SM4_GCM_KEY_SIZE 16
#define SM4_GCM_DEFAULT_TAG_SIZE 16

_gmssl_export int sm4_gcm_encrypt_init(SM4_GCM_CTX *ctx,
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
const uint8_t *aad, size_t aadlen, size_t taglen);
_gmssl_export int sm4_gcm_encrypt_update(SM4_GCM_CTX *ctx,
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
_gmssl_export int sm4_gcm_encrypt_finish(SM4_GCM_CTX *ctx,
uint8_t *out, size_t *outlen);
_gmssl_export int sm4_gcm_decrypt_init(SM4_GCM_CTX *ctx,
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
const uint8_t *aad, size_t aadlen, size_t taglen);
_gmssl_export int sm4_gcm_decrypt_update(SM4_GCM_CTX *ctx,
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
_gmssl_export int sm4_gcm_decrypt_finish(SM4_GCM_CTX *ctx,
uint8_t *out, size_t *outlen);


#ifdef __cplusplus
Expand Down
25 changes: 2 additions & 23 deletions include/gmssl/gcm.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
/*
* 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.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/


#ifndef GMSSL_GCM_H
#define GMSSL_GCM_H


#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <gmssl/gf128.h>
#include <gmssl/ghash.h>
#include <gmssl/block_cipher.h>


Expand All @@ -35,29 +34,9 @@ extern "C" {
#define GCM_MAX_PLAINTEXT_SIZE ((((uint64_t)1 << 39) - 256) >> 3)


#define GHASH_SIZE (16)


#define GCM_IS_LITTLE_ENDIAN 1


void ghash(const uint8_t h[16], const uint8_t *aad, size_t aadlen,
const uint8_t *c, size_t clen, uint8_t out[16]);

typedef struct {
gf128_t H;
gf128_t X;
size_t aadlen;
size_t clen;
uint8_t block[16];
size_t num;
} GHASH_CTX;

void ghash_init(GHASH_CTX *ctx, const uint8_t h[16], const uint8_t *aad, size_t aadlen);
void ghash_update(GHASH_CTX *ctx, const uint8_t *c, size_t clen);
void ghash_finish(GHASH_CTX *ctx, uint8_t out[16]);


int gcm_encrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *iv, size_t ivlen,
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
uint8_t *out, size_t taglen, uint8_t *tag);
Expand Down
49 changes: 49 additions & 0 deletions include/gmssl/ghash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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_GHASH_H
#define GMSSL_GHASH_H


#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <gmssl/gf128.h>


#ifdef __cplusplus
extern "C" {
#endif


#define GHASH_SIZE (16)



void ghash(const uint8_t h[16], const uint8_t *aad, size_t aadlen,
const uint8_t *c, size_t clen, uint8_t out[16]);

typedef struct {
gf128_t H;
gf128_t X;
size_t aadlen;
size_t clen;
uint8_t block[16];
size_t num;
} GHASH_CTX;

void ghash_init(GHASH_CTX *ctx, const uint8_t h[16], const uint8_t *aad, size_t aadlen);
void ghash_update(GHASH_CTX *ctx, const uint8_t *c, size_t clen);
void ghash_finish(GHASH_CTX *ctx, uint8_t out[16]);


#ifdef __cplusplus
}
#endif
#endif
33 changes: 33 additions & 0 deletions include/gmssl/sm4.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,39 @@ int sm4_ctr_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen);
#define sm4_ctr_decrypt_finish(ctx,out,outlen) sm4_ctr_encrypt_finish(ctx,out,outlen)


#include <gmssl/ghash.h>
#include <gmssl/api.h>

typedef struct {
SM4_CTR_CTX enc_ctx;
GHASH_CTX mac_ctx;
uint8_t Y[16]; // E(K, Y_0)
size_t taglen;
uint8_t mac[16];
size_t maclen;
} SM4_GCM_CTX;

#define SM4_GCM_KEY_SIZE 16
#define SM4_GCM_DEFAULT_TAG_SIZE 16

_gmssl_export int sm4_gcm_encrypt_init(SM4_GCM_CTX *ctx,
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
const uint8_t *aad, size_t aadlen, size_t taglen);
_gmssl_export int sm4_gcm_encrypt_update(SM4_GCM_CTX *ctx,
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
_gmssl_export int sm4_gcm_encrypt_finish(SM4_GCM_CTX *ctx,
uint8_t *out, size_t *outlen);
_gmssl_export int sm4_gcm_decrypt_init(SM4_GCM_CTX *ctx,
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
const uint8_t *aad, size_t aadlen, size_t taglen);
_gmssl_export int sm4_gcm_decrypt_update(SM4_GCM_CTX *ctx,
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
_gmssl_export int sm4_gcm_decrypt_finish(SM4_GCM_CTX *ctx,
uint8_t *out, size_t *outlen);




#ifdef ENABLE_SM4_ECB
// call `sm4_set_decrypt_key` before decrypt
void sm4_ecb_encrypt(const SM4_KEY *key, const uint8_t *in, size_t nblocks, uint8_t *out);
Expand Down
Loading

0 comments on commit 2d0378f

Please sign in to comment.