Skip to content

Commit 7f5f30f

Browse files
authored
Fix: redefine sm4 in crypto and pgcrypto (#394)
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.
1 parent 35fafd2 commit 7f5f30f

5 files changed

Lines changed: 13 additions & 13 deletions

File tree

contrib/pgcrypto/openssl_redirect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ bool px_find_cipher_support_redirect(const char *name) {
3636
return true;
3737
}
3838

39-
if (strcmp("sm4-128-cbc", name) == 0) {
39+
if (strcmp("sm4-128-ecb", name) == 0) {
4040
return true;
4141
}
4242
return false;

src/backend/crypto/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ include $(top_builddir)/src/Makefile.global
1414

1515
OBJS = \
1616
bufenc.o \
17-
sm4.o \
17+
sm4_ofb.o \
1818
kmgr.o
1919

2020
include $(top_srcdir)/src/backend/common.mk

src/backend/crypto/bufenc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "access/gist.h"
2020
#include "access/xlog.h"
2121
#include "crypto/bufenc.h"
22-
#include "crypto/sm4.h"
22+
#include "crypto/sm4_ofb.h"
2323
#include "storage/bufpage.h"
2424
#include "storage/fd.h"
2525
#include "storage/shmem.h"
@@ -57,8 +57,8 @@ InitializeBufferEncryption(void)
5757

5858
BufDecCtx = ShmemInitStruct("sm4 encryption method decrypt ctx",
5959
sizeof(sm4_ctx), &found);
60-
sm4_setkey_enc((sm4_ctx *)BufEncCtx, (unsigned char *)key->key);
61-
sm4_setkey_dec((sm4_ctx *)BufDecCtx, (unsigned char *)key->key);
60+
sm4_ofb_setkey_enc((sm4_ctx *)BufEncCtx, (unsigned char *)key->key);
61+
sm4_ofb_setkey_dec((sm4_ctx *)BufDecCtx, (unsigned char *)key->key);
6262
}
6363
else
6464
{
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "postgres.h"
22
#include <sys/param.h>
3-
#include "crypto/sm4.h"
3+
#include "crypto/sm4_ofb.h"
44

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

371-
void sm4_setkey_enc(sm4_ctx *ctx, uint8_t* key)
371+
void sm4_ofb_setkey_enc(sm4_ctx *ctx, uint8_t* key)
372372
{
373373
ossl_sm4_set_key(key, &ctx->rkey);
374374
ctx->encrypt = SM4_ENCRYPT;
375375
}
376376

377-
void sm4_setkey_dec(sm4_ctx *ctx, uint8_t* key)
377+
void sm4_ofb_setkey_dec(sm4_ctx *ctx, uint8_t* key)
378378
{
379379
ossl_sm4_set_key(key, &ctx->rkey);
380380
ctx->encrypt = SM4_DECRYPT;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
*
99
*-------------------------------------------------------------------------
1010
*/
11-
#ifndef _SM4_H_
12-
#define _SM4_H_
11+
#ifndef _SM4_OFB_H_
12+
#define _SM4_OFB_H_
1313
#include "c.h"
1414

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

5757
void ossl_sm4_decrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks);
58-
void sm4_setkey_enc(sm4_ctx *ctx, uint8_t* key);
59-
void sm4_setkey_dec(sm4_ctx *ctx, uint8_t* key);
58+
void sm4_ofb_setkey_enc(sm4_ctx *ctx, uint8_t* key);
59+
void sm4_ofb_setkey_dec(sm4_ctx *ctx, uint8_t* key);
6060
int sm4_ofb_cipher(sm4_ctx *ctx, unsigned char *out,
6161
const unsigned char *in, size_t input_len,
6262
unsigned char ivec[16]);
63-
#endif /* _SM4_H_ */
63+
#endif /* _SM4_OFB_H_ */

0 commit comments

Comments
 (0)