Skip to content

Commit

Permalink
Fix: redefine sm4 in crypto and pgcrypto (apache#394)
Browse files Browse the repository at this point in the history
After the pgcrypto module supported sm4, the sm4-128-ofb mode was added in backend/sm4.c
This approach is very unclean. 
And the implementation of sm4 in pgcrypto is overwritten.
  • Loading branch information
jiaqizho authored and my-ship-it committed Mar 19, 2024
1 parent 678115c commit 8f457c1
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion contrib/pgcrypto/openssl_redirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bool px_find_cipher_support_redirect(const char *name) {
return true;
}

if (strcmp("sm4-128-cbc", name) == 0) {
if (strcmp("sm4-128-ecb", name) == 0) {
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/backend/crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include $(top_builddir)/src/Makefile.global

OBJS = \
bufenc.o \
sm4.o \
sm4_ofb.o \
kmgr.o

include $(top_srcdir)/src/backend/common.mk
6 changes: 3 additions & 3 deletions src/backend/crypto/bufenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "access/gist.h"
#include "access/xlog.h"
#include "crypto/bufenc.h"
#include "crypto/sm4.h"
#include "crypto/sm4_ofb.h"
#include "storage/bufpage.h"
#include "storage/fd.h"
#include "storage/shmem.h"
Expand Down Expand Up @@ -57,8 +57,8 @@ InitializeBufferEncryption(void)

BufDecCtx = ShmemInitStruct("sm4 encryption method decrypt ctx",
sizeof(sm4_ctx), &found);
sm4_setkey_enc((sm4_ctx *)BufEncCtx, (unsigned char *)key->key);
sm4_setkey_dec((sm4_ctx *)BufDecCtx, (unsigned char *)key->key);
sm4_ofb_setkey_enc((sm4_ctx *)BufEncCtx, (unsigned char *)key->key);
sm4_ofb_setkey_dec((sm4_ctx *)BufDecCtx, (unsigned char *)key->key);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions src/backend/crypto/sm4.c → src/backend/crypto/sm4_ofb.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "postgres.h"
#include <sys/param.h>
#include "crypto/sm4.h"
#include "crypto/sm4_ofb.h"

static const uint8_t SM4_S[256] = {
0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2,
Expand Down Expand Up @@ -368,13 +368,13 @@ void ossl_sm4_decrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks)
store_u32_be(B0, out + 12);
}

void sm4_setkey_enc(sm4_ctx *ctx, uint8_t* key)
void sm4_ofb_setkey_enc(sm4_ctx *ctx, uint8_t* key)
{
ossl_sm4_set_key(key, &ctx->rkey);
ctx->encrypt = SM4_ENCRYPT;
}

void sm4_setkey_dec(sm4_ctx *ctx, uint8_t* key)
void sm4_ofb_setkey_dec(sm4_ctx *ctx, uint8_t* key)
{
ossl_sm4_set_key(key, &ctx->rkey);
ctx->encrypt = SM4_DECRYPT;
Expand Down
10 changes: 5 additions & 5 deletions src/include/crypto/sm4.h → src/include/crypto/sm4_ofb.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*
*-------------------------------------------------------------------------
*/
#ifndef _SM4_H_
#define _SM4_H_
#ifndef _SM4_OFB_H_
#define _SM4_OFB_H_
#include "c.h"

# define SM4_ENCRYPT 1
Expand Down Expand Up @@ -55,9 +55,9 @@ int ossl_sm4_set_key(const uint8_t *key, SM4_KEY *ks);
void ossl_sm4_encrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks);

void ossl_sm4_decrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks);
void sm4_setkey_enc(sm4_ctx *ctx, uint8_t* key);
void sm4_setkey_dec(sm4_ctx *ctx, uint8_t* key);
void sm4_ofb_setkey_enc(sm4_ctx *ctx, uint8_t* key);
void sm4_ofb_setkey_dec(sm4_ctx *ctx, uint8_t* key);
int sm4_ofb_cipher(sm4_ctx *ctx, unsigned char *out,
const unsigned char *in, size_t input_len,
unsigned char ivec[16]);
#endif /* _SM4_H_ */
#endif /* _SM4_OFB_H_ */

0 comments on commit 8f457c1

Please sign in to comment.