2022-08-08 17:23:33 +00:00
|
|
|
/* radare - LGPL - Copyright 2017-2022 - Sylvain Pelissier
|
|
|
|
* Implementation of SM4 block cipher
|
|
|
|
* https://datatracker.ietf.org/doc/html/draft-ribose-cfrg-sm4-10
|
|
|
|
*
|
|
|
|
* */
|
|
|
|
|
2022-09-08 14:01:55 +00:00
|
|
|
#include <r_crypto/r_sm4.h>
|
2022-09-14 15:28:22 +00:00
|
|
|
#include <r_crypto.h>
|
2022-08-08 17:23:33 +00:00
|
|
|
#include <memory.h>
|
|
|
|
|
|
|
|
static void sm4_crypt(const ut32 *sk, const ut8 *inbuf, ut8 *outbuf, int buflen) {
|
2022-09-14 15:28:22 +00:00
|
|
|
for (; buflen > 0; buflen -= SM4_BLOCK_SIZE) {
|
2022-09-15 01:25:53 +00:00
|
|
|
sm4_round (sk, inbuf, outbuf);
|
2022-09-14 15:28:22 +00:00
|
|
|
inbuf += SM4_BLOCK_SIZE;
|
|
|
|
outbuf += SM4_BLOCK_SIZE;
|
2022-08-08 17:23:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
static bool sm4_set_key(RCryptoJob *cj, const ut8 *key, int keylen, int mode, int direction) {
|
|
|
|
cj->dir = direction;
|
2022-09-15 16:55:57 +00:00
|
|
|
return sm4_init (cj->sm4_sk, key, keylen, direction);
|
2022-08-08 17:23:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
static int sm4_get_key_size(RCryptoJob *cry) {
|
2022-08-08 17:23:33 +00:00
|
|
|
return SM4_KEY_SIZE;
|
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
static bool update(RCryptoJob *cj, const ut8 *buf, int len) {
|
|
|
|
r_return_val_if_fail (cj&& buf, false);
|
2022-08-08 17:23:33 +00:00
|
|
|
ut8 *obuf = calloc (1, len);
|
|
|
|
if (!obuf) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* SM4 encryption or decryption */
|
2022-09-15 01:25:53 +00:00
|
|
|
sm4_crypt (cj->sm4_sk, buf, obuf, len);
|
|
|
|
r_crypto_job_append (cj, obuf, len);
|
2022-08-08 17:23:33 +00:00
|
|
|
free (obuf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
static bool end(RCryptoJob *cj, const ut8 *buf, int len) {
|
|
|
|
return update (cj, buf, len);
|
2022-08-08 17:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCryptoPlugin r_crypto_plugin_sm4 = {
|
|
|
|
.name = "sm4-ecb",
|
2022-09-14 15:28:22 +00:00
|
|
|
.implements = "sm4-ecb",
|
2022-09-15 01:25:53 +00:00
|
|
|
.author = "Sylvain Pelissier",
|
2022-08-08 17:23:33 +00:00
|
|
|
.license = "LGPL3",
|
|
|
|
.set_key = sm4_set_key,
|
|
|
|
.get_key_size = sm4_get_key_size,
|
|
|
|
.update = update,
|
2022-09-15 01:25:53 +00:00
|
|
|
.end = end
|
2022-08-08 17:23:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef R2_PLUGIN_INCORE
|
|
|
|
R_API RLibStruct radare_plugin = {
|
|
|
|
.type = R_LIB_TYPE_CRYPTO,
|
|
|
|
.data = &r_crypto_plugin_sm4,
|
|
|
|
.version = R2_VERSION
|
|
|
|
};
|
|
|
|
#endif
|