pgsmcrypto

PostgreSQL SM Algorithm Extension

Overview

PackageVersionCategoryLicenseLanguage
pgsmcrypto0.1.1SECMITRust
IDExtensionBinLibLoadCreateTrustRelocSchema
7060pgsmcryptoNoYesNoYesNoNo-
Relatedpgsodium pgcryptokey pgcrypto pg_tde sslutils faker uuid-ossp lo

manual updated pgrx by Vonng

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY0.1.11817161514pgsmcrypto-
RPMPIGSTY0.1.11817161514pgsmcrypto_$v-
DEBPIGSTY0.1.11817161514postgresql-$v-pgsmcrypto-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
u22.x86_64
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
u22.aarch64
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
u24.x86_64
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
u24.aarch64
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1

Build

You can build the RPM / DEB packages for pgsmcrypto using pig build:

pig build pkg pgsmcrypto         # build RPM / DEB packages

Install

You can install pgsmcrypto directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pgsmcrypto;          # Install for current active PG version
pig ext install -y pgsmcrypto -v 18  # PG 18
pig ext install -y pgsmcrypto -v 17  # PG 17
pig ext install -y pgsmcrypto -v 16  # PG 16
pig ext install -y pgsmcrypto -v 15  # PG 15
pig ext install -y pgsmcrypto -v 14  # PG 14
dnf install -y pgsmcrypto_18       # PG 18
dnf install -y pgsmcrypto_17       # PG 17
dnf install -y pgsmcrypto_16       # PG 16
dnf install -y pgsmcrypto_15       # PG 15
dnf install -y pgsmcrypto_14       # PG 14
apt install -y postgresql-18-pgsmcrypto   # PG 18
apt install -y postgresql-17-pgsmcrypto   # PG 17
apt install -y postgresql-16-pgsmcrypto   # PG 16
apt install -y postgresql-15-pgsmcrypto   # PG 15
apt install -y postgresql-14-pgsmcrypto   # PG 14

Create Extension:

CREATE EXTENSION pgsmcrypto;

Usage

pgsmcrypto: SM national cryptographic algorithm extension for PostgreSQL

pgsmcrypto provides Chinese national cryptographic (SM series) algorithms for PostgreSQL, including SM3 hashing, SM2 asymmetric encryption/signing, and SM4 symmetric encryption.

CREATE EXTENSION pgsmcrypto;

SM3 Message Digest

SELECT sm3_hash_string('abc');              -- Returns 64-char hex string (32 bytes)
SELECT sm3_hash('abc'::bytea);              -- Hash bytea input
SELECT sm3_hash(E'\\x616263');              -- Hash raw hex input

SM2 Asymmetric Encryption

Key Generation

SELECT sm2_gen_keypair();                   -- Returns {private_key, public_key} array
SELECT sm2_privkey_valid('f774...');        -- Validate private key (1=valid)
SELECT sm2_pubkey_valid('8093...');         -- Validate public key (1=valid)
SELECT sm2_pk_from_sk('f774...');           -- Derive public key from private key

Key Export/Import (PEM)

SELECT sm2_keypair_to_pem_bytes('f774...');       -- Private key to PEM
SELECT sm2_pubkey_to_pem_bytes('8093...');        -- Public key to PEM
SELECT sm2_keypair_from_pem_bytes(pem_bytes);     -- Import from PEM
SELECT sm2_pubkey_from_pem_bytes(pem_bytes);      -- Import public key from PEM

Sign and Verify

-- Raw sign/verify (signs message directly)
WITH s AS (
    SELECT sm2_sign_raw('abc'::bytea, 'f774...') AS sig
)
SELECT sm2_verify_raw('abc'::bytea, sig, '8093...') FROM s;

-- Standard sign/verify (SM2 specification with id + SM3 digest)
WITH s AS (
    SELECT sm2_sign('myid'::bytea, 'abc'::bytea, 'f774...') AS sig
)
SELECT sm2_verify('myid'::bytea, 'abc'::bytea, sig, '8093...') FROM s;

Encrypt and Decrypt

-- Standard encrypt/decrypt
WITH c AS (
    SELECT sm2_encrypt('abc'::bytea, '8093...') AS enc
)
SELECT sm2_decrypt(enc, 'f774...') FROM c;

-- Also available: sm2_encrypt_c1c2c3, sm2_encrypt_asna1, sm2_encrypt_hex, sm2_encrypt_base64
-- with corresponding decrypt variants

SM4 Symmetric Encryption

-- ECB mode (key must be 16 bytes)
SELECT sm4_encrypt_ecb('abc'::bytea, '1234567812345678'::bytea);
SELECT sm4_decrypt_ecb(encrypted, '1234567812345678'::bytea);

-- CBC mode (key and IV must be 16 bytes)
SELECT sm4_encrypt_cbc('abc'::bytea, '1234567812345678'::bytea, '0000000000000000'::bytea);
SELECT sm4_decrypt_cbc(encrypted, '1234567812345678'::bytea, '0000000000000000'::bytea);

Last Modified 2026-03-12: add pg extension catalog (95749bf)