void hmac_sm3_final(hmac_sm3_ctx_t *ctx, unsigned char mac[HMAC_SM3_MAC_SIZE]) { int i; for (i = 0; i < SM3_BLOCK_SIZE; i++) { ctx->key[i] ^= (IPAD ^ OPAD); } sm3_final(&ctx->sm3_ctx, mac); sm3_init(&ctx->sm3_ctx); sm3_update(&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE); sm3_update(&ctx->sm3_ctx, mac, SM3_DIGEST_LENGTH); sm3_final(&ctx->sm3_ctx, mac); }
int main(int argc, char **argv) { sm3_ctx_t ctx; unsigned char dgst[SM3_DIGEST_LENGTH]; unsigned char buf[4096]; ssize_t len; int i; if (argc > 1) { printf("usage: %s < file\n", basename(argv[0])); return 0; } sm3_init(&ctx); while ((len = read(STDIN_FILENO, buf, sizeof(buf))) > 0) { sm3_update(&ctx, buf, len); } memset(dgst, 0, sizeof(dgst)); sm3_final(&ctx, dgst); for (i = 0; i < sizeof(dgst); i++) { printf("%02x", dgst[i]); } printf("\n"); return 0; }
static int update(EVP_MD_CTX *ctx, const void *in, size_t inlen) { if (!ctx || !ctx->md_data || !in) { return 0; } sm3_update(ctx->md_data, in, inlen); return 1; }
void eccSetUserMessageData(unsigned long ulUserIDSize, unsigned char *rgbUserID, unsigned long ulMessageSize, unsigned char *rgbMessage, unsigned char *x, unsigned char *y, unsigned char *rgbHashData) { // Tsp 需要 unsigned char z[32]; Z_Gen(z, ulUserIDSize, rgbUserID, x, y); sm3_ctx_t ctx2; sm3_init(&ctx2); sm3_update(&ctx2, z, sizeof(z)); sm3_update(&ctx2, rgbMessage, ulMessageSize); sm3_final(&ctx2, rgbHashData); }
void sm3(const unsigned char *msg, size_t msglen, unsigned char dgst[SM3_DIGEST_LENGTH]) { sm3_ctx_t ctx; sm3_init(&ctx); sm3_update(&ctx, msg, msglen); sm3_final(&ctx, dgst); memset(&ctx, 0, sizeof(sm3_ctx_t)); }
void hmac_sm3_init(hmac_sm3_ctx_t *ctx, const unsigned char *key, size_t key_len) { int i; unsigned char ipad[SM3_DIGEST_LENGTH]; if (key_len <= SM3_BLOCK_SIZE) { memcpy(ctx->key, key, key_len); memset(ctx->key + key_len, 0, SM3_BLOCK_SIZE - key_len); } else { sm3_init(&ctx->sm3_ctx); sm3_update(&ctx->sm3_ctx, key, key_len); sm3_final(&ctx->sm3_ctx, ctx->key); memset(ctx->key + SM3_DIGEST_LENGTH, 0, SM3_BLOCK_SIZE - SM3_DIGEST_LENGTH); } for (i = 0; i < SM3_BLOCK_SIZE; i++) { ctx->key[i] ^= IPAD; } sm3_init(&ctx->sm3_ctx); sm3_update(&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE); }
void sm2_z ( unsigned char *id, unsigned int ilen, point_affn_ptr G, point_affn_ptr sG, unsigned char *hash ) { sm3_context ctx; #ifndef MUL_BITS unsigned char xt[FIELD_SIZE_IN_BYTES*2]; #else unsigned char xt[32*2]; #endif unsigned short nlen = ilen*8; int tlen; sm3_starts( &ctx ); #ifdef LT_ENDIAN xt[0]= ((unsigned char *)&nlen)[1]; xt[1]= ((unsigned char *)&nlen)[0]; #else xt[0]= ((unsigned char *)&nlen)[0]; xt[1]= ((unsigned char *)&nlen)[1]; #endif sm3_update( &ctx, xt, 2 ); // dump_mem(xt, 2, "idlen"); if(id && ilen) sm3_update( &ctx, id, ilen ); // dump_mem(id, ilen, "id"); tlen = FIELD_SIZE_IN_BYTES; I2OSP(xt, tlen, COA); sm3_update( &ctx, xt, tlen ); // dump_mem(xt, tlen, "a"); tlen = FIELD_SIZE_IN_BYTES; I2OSP(xt, tlen, COB); sm3_update( &ctx, xt, tlen ); // dump_mem(xt, tlen, "b"); tlen = sizeof(xt); EC2OSP_XY(xt, &tlen, G); sm3_update( &ctx, xt, tlen ); // dump_mem(xt, tlen, "G"); tlen = sizeof(xt); EC2OSP_XY(xt, &tlen, sG); sm3_update( &ctx, xt, tlen ); // dump_mem(xt, tlen, "sG"); sm3_finish( &ctx, hash ); memset( &ctx, 0, sizeof( sm3_context ) ); }
void sm2_hash ( unsigned char *x, int xlen, unsigned char *m, int mlen, unsigned char *y, int ylen, unsigned char *hash ) { sm3_context ctx; sm3_starts( &ctx ); sm3_update( &ctx, x, xlen ); if(x && xlen) sm3_update( &ctx, m, mlen ); if(y && ylen) sm3_update( &ctx, y, ylen ); sm3_finish( &ctx, hash ); memset( &ctx, 0, sizeof( sm3_context ) ); }
int main(void) { sm3_ctx c; int i,fail; B h[32], m[64]; for (fail=0, i=0; i<64; i++) { memset(m,0,sizeof(m)); m[i]=i; sm3_init(&c); sm3_update(&c, m, i); sm3_final(h, &c); if(memcmp(h, tv[i], 32)) { printf ("Hash for test vector %i failed\n", i+1); fail++; } } if(!fail) printf ("All SM3 tests passed\n"); return 0; }
static int update(EVP_MD_CTX *ctx, const void *in, size_t inlen) { return sm3_update(ctx->md_data, in, inlen); }
void hmac_sm3_update(hmac_sm3_ctx_t *ctx, const unsigned char *data, size_t data_len) { sm3_update(&ctx->sm3_ctx, data, data_len); }
static int update(EVP_MD_CTX *ctx, const void *data, size_t count) { return sm3_update(ctx->md_data, data, count); }
void Z_Gen(unsigned char *z, unsigned int klen, unsigned char *ID, unsigned char *x, unsigned char *y) { // Tsp 需要 // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA)。 BN_CTX *ctx = NULL; ctx = BN_CTX_new(); EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1); const EC_GROUP *ec_group = EC_KEY_get0_group(ec_key); BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL, *order = NULL, *cofactor = NULL; if ((p = BN_new()) == NULL || (a = BN_new()) == NULL || (b = BN_new()) == NULL || (order = BN_new()) == NULL || (cofactor = BN_new()) == NULL) { goto err; } int is_char_two = 0; int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(ec_group)); if (tmp_nid == NID_X9_62_characteristic_two_field) is_char_two = 1; #ifndef OPENSSL_NO_EC2M if (is_char_two) { if (!EC_GROUP_get_curve_GF2m(ec_group, p, a, b, ctx)) { goto err; } } else /* prime field */ #endif { if (!EC_GROUP_get_curve_GFp(ec_group, p, a, b, ctx)) { goto err; } } const EC_POINT *generator = EC_GROUP_get0_generator(ec_group); unsigned char g[65]; EC_POINT_point2oct(ec_group, generator, POINT_CONVERSION_UNCOMPRESSED, g, ECDH_SIZE, NULL); sm3_ctx_t ctx2; sm3_init(&ctx2); unsigned char entla[2]; entla[0] = (klen / 32); entla[1] = (klen * 8); sm3_update(&ctx2, entla, sizeof(entla)); sm3_update(&ctx2, ID, klen); unsigned char buffer[32]; BN_bn2bin(a, buffer); sm3_update(&ctx2, buffer, 32); BN_bn2bin(b, buffer); sm3_update(&ctx2, buffer, 32); sm3_update(&ctx2, g + 1, 64); sm3_update(&ctx2, x, 32); sm3_update(&ctx2, y, 32); sm3_final(&ctx2, z); err: return; }