XU XUv0.16.0

#crypto/sm4

import: #crypto/sm4

counts: 15 functions, 8 methods, 2 types, 1 constants, 0 method sets

SM4 block plus CBC/CTR/CFB/OFB/GCM. Pure Xu, not commercial-crypto certified.

const BLOCK_SIZE

XuAPI
pub const BLOCK_SIZE = 16

Purpose

Constant BLOCK_SIZE, defining protocol or algorithm parameters.

struct Cipher

XuAPI
pub struct Cipher {
    pub rk []int
    pub sbox []int
}

Purpose

Sm4 Cipher context for block or stream encryption/decryption.

Public fields

rk []int
field ([]int)
sbox []int
field ([]int)

(&Cipher) crypt_arr16

XuAPI
pub fn (c &Cipher) crypt_arr16(dst $&[16]int, src &[16]int, inv int) int

Purpose

Operation on &Cipher: crypt arr16.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
dst $&[16]int
destination (exclusive borrow; may modify the original; type $&[16]int)
src &[16]int
source (read-only borrow; caller keeps the original; type &[16]int)
inv int
inv (int)

Returns

int — a value of this type

new_cipher

XuAPI
pub fn new_cipher(key &string) Cipher

Purpose

Construct cipher. Throws on failure; handle the error or propagate it to the caller.

Parameters

key &string
key (read-only borrow; caller keeps the original; type &string)

Returns

Cipher — a value of this type

(&Cipher) block_size

XuAPI
pub fn (c &Cipher) block_size() int

Purpose

Operation on &Cipher: block size.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).

Returns

int — a value of this type

(&Cipher) encrypt_arr16

XuAPI
pub fn (c &Cipher) encrypt_arr16(dst $&[16]int, src &[16]int) int

Purpose

Operation on &Cipher: encrypt arr16.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
dst $&[16]int
destination (exclusive borrow; may modify the original; type $&[16]int)
src &[16]int
source (read-only borrow; caller keeps the original; type &[16]int)

Returns

int — a value of this type

(&Cipher) decrypt_arr16

XuAPI
pub fn (c &Cipher) decrypt_arr16(dst $&[16]int, src &[16]int) int

Purpose

Operation on &Cipher: decrypt arr16.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
dst $&[16]int
destination (exclusive borrow; may modify the original; type $&[16]int)
src &[16]int
source (read-only borrow; caller keeps the original; type &[16]int)

Returns

int — a value of this type

(&Cipher) encrypt

XuAPI
pub fn (c &Cipher) encrypt(src &string) string

Purpose

Operation on &Cipher: encrypt. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

(&Cipher) decrypt

XuAPI
pub fn (c &Cipher) decrypt(src &string) string

Purpose

Operation on &Cipher: decrypt. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

struct GcmKey

XuAPI
pub struct GcmKey {

}

Purpose

GcmKey context or state structure.

Public fields

This type has no public fields.

empty_gcm

XuAPI
pub fn empty_gcm() GcmKey

Purpose

Empty gcm.

Parameters

No arguments are required.

Returns

GcmKey — a value of this type

new_gcm

XuAPI
pub fn new_gcm(c Cipher) GcmKey

Purpose

Construct gcm. Throws on failure; handle the error or propagate it to the caller.

Parameters

c Cipher
byte or character code (Cipher)

Returns

GcmKey — a value of this type

(&GcmKey) seal

XuAPI
pub fn (g &GcmKey) seal(nonce &string, plaintext &string, aad &string) string

Purpose

Execute seal on &GcmKey. Throws on failure; handle the error or propagate it to the caller.

Parameters

g &GcmKey
The GcmKey instance used by this call (borrow).
nonce &string
nonce (read-only borrow; caller keeps the original; type &string)
plaintext &string
plaintext (read-only borrow; caller keeps the original; type &string)
aad &string
aad (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

(&GcmKey) open

XuAPI
pub fn (g &GcmKey) open(nonce &string, sealed &string, aad &string) string

Purpose

Operation on &GcmKey: open resource or establish connection. Throws on failure; handle the error or propagate it to the caller.

Parameters

g &GcmKey
The GcmKey instance used by this call (borrow).
nonce &string
nonce (read-only borrow; caller keeps the original; type &string)
sealed &string
sealed (read-only borrow; caller keeps the original; type &string)
aad &string
aad (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

seal_gcm

XuAPI
pub fn seal_gcm(c &Cipher, nonce &string, plaintext &string, aad &string) string

Purpose

Seal gcm. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
nonce &string
nonce (read-only borrow; caller keeps the original; type &string)
plaintext &string
plaintext (read-only borrow; caller keeps the original; type &string)
aad &string
aad (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

open_gcm

XuAPI
pub fn open_gcm(c &Cipher, nonce &string, sealed &string, aad &string) string

Purpose

Open gcm. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
nonce &string
nonce (read-only borrow; caller keeps the original; type &string)
sealed &string
sealed (read-only borrow; caller keeps the original; type &string)
aad &string
aad (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

encrypt_cbc

XuAPI
pub fn encrypt_cbc(c &Cipher, iv &string, src &string) string

Purpose

Encrypt cbc. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
iv &string
int payload or initialization vector (read-only borrow; caller keeps the original; type &string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

decrypt_cbc

XuAPI
pub fn decrypt_cbc(c &Cipher, iv &string, src &string) string

Purpose

Decrypt cbc. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
iv &string
int payload or initialization vector (read-only borrow; caller keeps the original; type &string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

encrypt_cbc_pkcs7

XuAPI
pub fn encrypt_cbc_pkcs7(c &Cipher, iv &string, src &string) string

Purpose

Encrypt cbc pkcs7. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
iv &string
int payload or initialization vector (read-only borrow; caller keeps the original; type &string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

decrypt_cbc_pkcs7

XuAPI
pub fn decrypt_cbc_pkcs7(c &Cipher, iv &string, src &string) string

Purpose

Decrypt cbc pkcs7. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
iv &string
int payload or initialization vector (read-only borrow; caller keeps the original; type &string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

encrypt_ctr

XuAPI
pub fn encrypt_ctr(c &Cipher, iv &string, src &string) string

Purpose

Encrypt ctr. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
iv &string
int payload or initialization vector (read-only borrow; caller keeps the original; type &string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

decrypt_ctr

XuAPI
pub fn decrypt_ctr(c &Cipher, iv &string, src &string) string

Purpose

Decrypt ctr. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
iv &string
int payload or initialization vector (read-only borrow; caller keeps the original; type &string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

encrypt_cfb

XuAPI
pub fn encrypt_cfb(c &Cipher, iv &string, src &string) string

Purpose

Encrypt cfb. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
iv &string
int payload or initialization vector (read-only borrow; caller keeps the original; type &string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

decrypt_cfb

XuAPI
pub fn decrypt_cfb(c &Cipher, iv &string, src &string) string

Purpose

Decrypt cfb. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
iv &string
int payload or initialization vector (read-only borrow; caller keeps the original; type &string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

encrypt_ofb

XuAPI
pub fn encrypt_ofb(c &Cipher, iv &string, src &string) string

Purpose

Encrypt ofb. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
iv &string
int payload or initialization vector (read-only borrow; caller keeps the original; type &string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

decrypt_ofb

XuAPI
pub fn decrypt_ofb(c &Cipher, iv &string, src &string) string

Purpose

Decrypt ofb. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
byte or character code (read-only borrow; caller keeps the original; type &Cipher)
iv &string
int payload or initialization vector (read-only borrow; caller keeps the original; type &string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string