mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-07 08:56:17 +08:00
add gmssl command
This commit is contained in:
148
src/gf128.c
148
src/gf128.c
@@ -59,6 +59,7 @@
|
||||
#include <gmssl/gf128.h>
|
||||
#include "endian.h"
|
||||
|
||||
|
||||
gf128_t gf128_zero(void)
|
||||
{
|
||||
uint8_t zero[16] = {0};
|
||||
@@ -87,92 +88,31 @@ int gf128_equ_hex(gf128_t a, const char *s)
|
||||
void gf128_print_bits(gf128_t a)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 128; i++) {
|
||||
printf("%d", (int)(a % 2)); //FIXME
|
||||
a >>= 1;
|
||||
for (i = 0; i < 64; i++) {
|
||||
printf("%d", (int)(a.hi % 2));
|
||||
a.hi >>= 1;
|
||||
}
|
||||
for (i = 0; i < 64; i++) {
|
||||
printf("%d", (int)(a.lo % 2));
|
||||
a.lo >>= 1;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void gf128_print(const char *s, gf128_t a)
|
||||
int gf128_print(FILE *fp, int fmt, int ind, const char *label, gf128_t a)
|
||||
{
|
||||
uint8_t be[16];
|
||||
int i;
|
||||
|
||||
printf("%s", s);
|
||||
printf("%s", label);
|
||||
gf128_to_bytes(a, be);
|
||||
for (i = 0; i < 16; i++) {
|
||||
printf("%02X", be[i]);
|
||||
}
|
||||
printf("\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef GMSSL_HAVE_UINT128
|
||||
gf128_t gf128_mul(gf128_t a, gf128_t b)
|
||||
{
|
||||
const gf128_t mask = (gf128_t)1 << 127;
|
||||
|
||||
gf128_t r = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 128; i++) {
|
||||
// r = r * 2
|
||||
if (r & mask)
|
||||
r = (r << 1) ^ 0x87;
|
||||
else r <<= 1;
|
||||
|
||||
// if b[127-i] == 1, r = r + a
|
||||
if (b & mask)
|
||||
r ^= a;
|
||||
b <<= 1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
gf128_t gf128_add(gf128_t a, gf128_t b)
|
||||
{
|
||||
return a ^ b;
|
||||
}
|
||||
|
||||
gf128_t gf128_mul2(gf128_t a)
|
||||
{
|
||||
if (a & ((gf128_t)1 << 127))
|
||||
return (a << 1) ^ 0x87;
|
||||
else return (a << 1);
|
||||
}
|
||||
|
||||
gf128_t gf128_reverse(gf128_t a)
|
||||
{
|
||||
gf128_t r = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 128; i++) {
|
||||
r = (r << 1) | (a & 1);
|
||||
a >>= 1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
gf128_t gf128_from_bytes(const uint8_t p[16])
|
||||
{
|
||||
uint64_t hi = GETU64(p);
|
||||
uint64_t lo = GETU64(p + 8);
|
||||
gf128_t r = (gf128_t)hi << 64 | lo;
|
||||
r = gf128_reverse(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
void gf128_to_bytes(gf128_t a, uint8_t p[16])
|
||||
{
|
||||
a = gf128_reverse(a);
|
||||
uint64_t hi = a >> 64;
|
||||
uint64_t lo = a;
|
||||
PUTU64(p, hi);
|
||||
PUTU64(p + 8, lo);
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
gf128_t gf128_from_bytes(const uint8_t p[16])
|
||||
{
|
||||
gf128_t r;
|
||||
@@ -253,4 +193,68 @@ gf128_t gf128_mul2(gf128_t a)
|
||||
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
gf128_t gf128_mul(gf128_t a, gf128_t b)
|
||||
{
|
||||
const gf128_t mask = (gf128_t)1 << 127;
|
||||
|
||||
gf128_t r = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 128; i++) {
|
||||
// r = r * 2
|
||||
if (r & mask)
|
||||
r = (r << 1) ^ 0x87;
|
||||
else r <<= 1;
|
||||
|
||||
// if b[127-i] == 1, r = r + a
|
||||
if (b & mask)
|
||||
r ^= a;
|
||||
b <<= 1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
gf128_t gf128_add(gf128_t a, gf128_t b)
|
||||
{
|
||||
return a ^ b;
|
||||
}
|
||||
|
||||
gf128_t gf128_mul2(gf128_t a)
|
||||
{
|
||||
if (a & ((gf128_t)1 << 127))
|
||||
return (a << 1) ^ 0x87;
|
||||
else return (a << 1);
|
||||
}
|
||||
|
||||
gf128_t gf128_reverse(gf128_t a)
|
||||
{
|
||||
gf128_t r = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 128; i++) {
|
||||
r = (r << 1) | (a & 1);
|
||||
a >>= 1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
gf128_t gf128_from_bytes(const uint8_t p[16])
|
||||
{
|
||||
uint64_t hi = GETU64(p);
|
||||
uint64_t lo = GETU64(p + 8);
|
||||
gf128_t r = (gf128_t)hi << 64 | lo;
|
||||
r = gf128_reverse(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
void gf128_to_bytes(gf128_t a, uint8_t p[16])
|
||||
{
|
||||
a = gf128_reverse(a);
|
||||
uint64_t hi = a >> 64;
|
||||
uint64_t lo = a;
|
||||
PUTU64(p, hi);
|
||||
PUTU64(p + 8, lo);
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -239,7 +239,6 @@ int sm4_gcm_decrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen,
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int sm4_cbc_encrypt_init(SM4_CBC_CTX *ctx,
|
||||
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t iv[SM4_BLOCK_SIZE])
|
||||
{
|
||||
@@ -303,9 +302,6 @@ int sm4_cbc_encrypt_finish(SM4_CBC_CTX *ctx, uint8_t *out, size_t *outlen)
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
format_bytes(stderr, 0, 0, "encrypt_finish", ctx->block, ctx->block_nbytes);
|
||||
|
||||
if (sm4_cbc_padding_encrypt(&ctx->sm4_key, ctx->iv, ctx->block, ctx->block_nbytes, out, outlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
@@ -370,13 +366,74 @@ int sm4_cbc_decrypt_finish(SM4_CBC_CTX *ctx, uint8_t *out, size_t *outlen)
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_bytes(stderr, 0, 0, "block", ctx->block, ctx->block_nbytes);
|
||||
format_bytes(stderr, 0, 0, "iv", ctx->iv, 16);
|
||||
|
||||
|
||||
if (sm4_cbc_padding_decrypt(&ctx->sm4_key, ctx->iv, ctx->block, SM4_BLOCK_SIZE, out, outlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm4_ctr_encrypt_init(SM4_CTR_CTX *ctx,
|
||||
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t ctr[SM4_BLOCK_SIZE])
|
||||
{
|
||||
sm4_set_encrypt_key(&ctx->sm4_key, key);
|
||||
memcpy(ctx->ctr, ctr, SM4_BLOCK_SIZE);
|
||||
memset(ctx->block, 0, SM4_BLOCK_SIZE);
|
||||
ctx->block_nbytes = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm4_ctr_encrypt_update(SM4_CTR_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
|
||||
{
|
||||
size_t left;
|
||||
size_t nblocks;
|
||||
size_t len;
|
||||
|
||||
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
*outlen = 0;
|
||||
if (ctx->block_nbytes) {
|
||||
left = SM4_BLOCK_SIZE - ctx->block_nbytes;
|
||||
if (inlen < left) {
|
||||
memcpy(ctx->block + ctx->block_nbytes, in, inlen);
|
||||
ctx->block_nbytes += inlen;
|
||||
return 1;
|
||||
}
|
||||
memcpy(ctx->block + ctx->block_nbytes, in, left);
|
||||
sm4_ctr_encrypt(&ctx->sm4_key, ctx->ctr, ctx->block, SM4_BLOCK_SIZE, out);
|
||||
in += left;
|
||||
inlen -= left;
|
||||
out += SM4_BLOCK_SIZE;
|
||||
*outlen += SM4_BLOCK_SIZE;
|
||||
}
|
||||
if (inlen >= SM4_BLOCK_SIZE) {
|
||||
nblocks = inlen / SM4_BLOCK_SIZE;
|
||||
len = nblocks * SM4_BLOCK_SIZE;
|
||||
sm4_ctr_encrypt(&ctx->sm4_key, ctx->ctr, in, len, out);
|
||||
in += len;
|
||||
inlen -= len;
|
||||
out += len;
|
||||
*outlen += len;
|
||||
}
|
||||
if (inlen) {
|
||||
memcpy(ctx->block, in, inlen);
|
||||
}
|
||||
ctx->block_nbytes = inlen;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm4_ctr_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen)
|
||||
{
|
||||
size_t left;
|
||||
size_t i;
|
||||
|
||||
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
sm4_ctr_encrypt(&ctx->sm4_key, ctx->ctr, ctx->block, ctx->block_nbytes, out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user