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; }
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 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); }
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; }
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); }
int crypto_sm3_finup(struct shash_desc *desc, const u8 *data, unsigned int len, u8 *hash) { sm3_base_do_update(desc, data, len, sm3_generic_block_fn); return sm3_final(desc, hash); }
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; }