mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-09 23:43:41 +08:00
jni api
This commit is contained in:
@@ -49,13 +49,11 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "sm3.h"
|
||||
#include "../byteorder.h"
|
||||
#include <string.h>
|
||||
#include "../byteorder.h"
|
||||
#include <openssl/sm3.h>
|
||||
|
||||
|
||||
int sm3_init(sm3_ctx_t *ctx)
|
||||
void sm3_init(sm3_ctx_t *ctx)
|
||||
{
|
||||
ctx->digest[0] = 0x7380166F;
|
||||
ctx->digest[1] = 0x4914B2B9;
|
||||
@@ -65,22 +63,19 @@ int sm3_init(sm3_ctx_t *ctx)
|
||||
ctx->digest[5] = 0x163138AA;
|
||||
ctx->digest[6] = 0xE38DEE4D;
|
||||
ctx->digest[7] = 0xB0FB0E4E;
|
||||
|
||||
|
||||
ctx->nblocks = 0;
|
||||
ctx->num = 0;
|
||||
if(ctx == NULL) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm3_update(sm3_ctx_t *ctx, const unsigned char* data, size_t data_len)
|
||||
void sm3_update(sm3_ctx_t *ctx, const unsigned char* data, size_t data_len)
|
||||
{
|
||||
if(ctx == NULL) return 0;
|
||||
if (ctx->num) {
|
||||
unsigned int left = SM3_BLOCK_SIZE - ctx->num;
|
||||
if (data_len < left) {
|
||||
memcpy(ctx->block + ctx->num, data, data_len);
|
||||
ctx->num += data_len;
|
||||
return 1;
|
||||
return;
|
||||
} else {
|
||||
memcpy(ctx->block + ctx->num, data, left);
|
||||
sm3_compress(ctx->digest, ctx->block);
|
||||
@@ -99,18 +94,16 @@ int sm3_update(sm3_ctx_t *ctx, const unsigned char* data, size_t data_len)
|
||||
if (data_len) {
|
||||
memcpy(ctx->block, data, data_len);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm3_final(sm3_ctx_t *ctx, unsigned char *digest)
|
||||
void sm3_final(sm3_ctx_t *ctx, unsigned char *digest)
|
||||
{
|
||||
if(ctx == NULL) return 0;
|
||||
int i;
|
||||
uint32_t *pdigest = (uint32_t *)digest;
|
||||
uint32_t *count = (uint32_t *)(ctx->block + SM3_BLOCK_SIZE - 8);
|
||||
|
||||
|
||||
ctx->block[ctx->num] = 0x80;
|
||||
|
||||
|
||||
if (ctx->num + 9 <= SM3_BLOCK_SIZE) {
|
||||
memset(ctx->block + ctx->num + 1, 0, SM3_BLOCK_SIZE - ctx->num - 9);
|
||||
} else {
|
||||
@@ -121,23 +114,22 @@ int sm3_final(sm3_ctx_t *ctx, unsigned char *digest)
|
||||
|
||||
count[0] = cpu_to_be32((ctx->nblocks) >> 23);
|
||||
count[1] = cpu_to_be32((ctx->nblocks << 9) + (ctx->num << 3));
|
||||
|
||||
|
||||
sm3_compress(ctx->digest, ctx->block);
|
||||
for (i = 0; i < sizeof(ctx->digest)/sizeof(ctx->digest[0]); i++) {
|
||||
pdigest[i] = cpu_to_be32(ctx->digest[i]);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define ROTATELEFT(X,n) (((X)<<(n)) | ((X)>>(32-(n))))
|
||||
|
||||
#define P0(x) ((x) ^ ROTATELEFT((x),9) ^ ROTATELEFT((x),17))
|
||||
#define P1(x) ((x) ^ ROTATELEFT((x),15) ^ ROTATELEFT((x),23))
|
||||
#define P0(x) ((x) ^ ROTATELEFT((x),9) ^ ROTATELEFT((x),17))
|
||||
#define P1(x) ((x) ^ ROTATELEFT((x),15) ^ ROTATELEFT((x),23))
|
||||
|
||||
#define FF0(x,y,z) ( (x) ^ (y) ^ (z))
|
||||
#define FF0(x,y,z) ( (x) ^ (y) ^ (z))
|
||||
#define FF1(x,y,z) (((x) & (y)) | ( (x) & (z)) | ( (y) & (z)))
|
||||
|
||||
#define GG0(x,y,z) ( (x) ^ (y) ^ (z))
|
||||
#define GG0(x,y,z) ( (x) ^ (y) ^ (z))
|
||||
#define GG1(x,y,z) (((x) & (y)) | ( (~(x)) & (z)) )
|
||||
|
||||
|
||||
@@ -146,7 +138,7 @@ void sm3_compress(uint32_t digest[8], const unsigned char block[64])
|
||||
int j;
|
||||
uint32_t W[68], W1[64];
|
||||
const uint32_t *pblock = (const uint32_t *)block;
|
||||
|
||||
|
||||
uint32_t A = digest[0];
|
||||
uint32_t B = digest[1];
|
||||
uint32_t C = digest[2];
|
||||
@@ -170,7 +162,7 @@ void sm3_compress(uint32_t digest[8], const unsigned char block[64])
|
||||
for(j =0; j < 16; j++) {
|
||||
|
||||
T[j] = 0x79CC4519;
|
||||
SS1 = ROTATELEFT((ROTATELEFT(A,12) + E + ROTATELEFT(T[j],j)), 7);
|
||||
SS1 = ROTATELEFT((ROTATELEFT(A,12) + E + ROTATELEFT(T[j],j)), 7);
|
||||
SS2 = SS1 ^ ROTATELEFT(A,12);
|
||||
TT1 = FF0(A,B,C) + D + SS2 + W1[j];
|
||||
TT2 = GG0(E,F,G) + H + SS1 + W[j];
|
||||
@@ -187,7 +179,7 @@ void sm3_compress(uint32_t digest[8], const unsigned char block[64])
|
||||
for(j =16; j < 64; j++) {
|
||||
|
||||
T[j] = 0x7A879D8A;
|
||||
SS1 = ROTATELEFT((ROTATELEFT(A,12) + E + ROTATELEFT(T[j],j)), 7);
|
||||
SS1 = ROTATELEFT((ROTATELEFT(A,12) + E + ROTATELEFT(T[j],j)), 7);
|
||||
SS2 = SS1 ^ ROTATELEFT(A,12);
|
||||
TT1 = FF1(A,B,C) + D + SS2 + W1[j];
|
||||
TT2 = GG1(E,F,G) + H + SS1 + W[j];
|
||||
@@ -211,7 +203,8 @@ void sm3_compress(uint32_t digest[8], const unsigned char block[64])
|
||||
digest[7] ^= H;
|
||||
}
|
||||
|
||||
void sm3(const unsigned char *msg, size_t msglen, unsigned char dgst[SM3_DIGEST_LENGTH])
|
||||
void sm3(const unsigned char *msg, size_t msglen,
|
||||
unsigned char dgst[SM3_DIGEST_LENGTH])
|
||||
{
|
||||
sm3_ctx_t ctx;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user