mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-23 13:54:07 +08:00
jni api
This commit is contained in:
14
demos/gmssl/Makefile
Normal file
14
demos/gmssl/Makefile
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
all:
|
||||
cc -o ciphers -Wall ciphers.c -L/usr/local/lib -lcrypto
|
||||
cc -o sm3 -Wall sm3.c -L/usr/local/lib -lcrypto
|
||||
cc -o evpsm3 -Wall evpsm3.c -L/usr/local/lib -lcrypto
|
||||
cc -o hmacsm3 -Wall hmacsm3.c -L/usr/local/lib -lcrypto
|
||||
|
||||
clean:
|
||||
rm -fr a.out *.o
|
||||
rm -fr ciphers
|
||||
rm -fr sm3
|
||||
rm -fr evpsm3
|
||||
rm -fr hmacsm3
|
||||
|
||||
BIN
demos/gmssl/a.out
Executable file
BIN
demos/gmssl/a.out
Executable file
Binary file not shown.
@@ -1,13 +0,0 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIB9jCCAZ2gAwIBAgIJAI6saFfpzqpLMAoGCCqGSM49BAMCMFgxCzAJBgNVBAYT
|
||||
AkNOMQwwCgYDVQQIDANQS1UxDDAKBgNVBAcMA1BLVTEMMAoGA1UECgwDUEtVMQ0w
|
||||
CwYDVQQLDARERFNTMRAwDgYDVQQDDAdERFNTLUFTMB4XDTE2MDUxODExNTgyMVoX
|
||||
DTI2MDUxNjExNTgyMVowWDELMAkGA1UEBhMCQ04xDDAKBgNVBAgMA1BLVTEMMAoG
|
||||
A1UEBwwDUEtVMQwwCgYDVQQKDANQS1UxDTALBgNVBAsMBEREU1MxEDAOBgNVBAMM
|
||||
B0REU1MtQVMwWTATBgcqhkjOPQIBBggqgRzPVQGCLQNCAATFBdPQp/tqJHjfL+eZ
|
||||
Jv1tUCMFpWCzoskQgDImhLP8+snkNSmZhRtHeerUr8oP6FtWAPnhUzwMOVb4JcNC
|
||||
CYSbo1AwTjAdBgNVHQ4EFgQUCz8gNn0NMxyIW/gRF13zl6ExeLUwHwYDVR0jBBgw
|
||||
FoAUCz8gNn0NMxyIW/gRF13zl6ExeLUwDAYDVR0TBAUwAwEB/zAKBggqhkjOPQQD
|
||||
AgNHADBEAiARFx9dY1LE+ELs/SWIkMLxbikA3P4YE0JZZkAmXZVo/gIgEs8G6fJw
|
||||
8AEbDwMcMiLLyJ7RhcUhEX+oj3Ibm8JgQXo=
|
||||
-----END CERTIFICATE-----
|
||||
89
demos/gmssl/ciphers.c
Normal file
89
demos/gmssl/ciphers.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/* demo/gmssl/ciphers.c */
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2015 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
char *names[] = {
|
||||
"sms4-ecb",
|
||||
"sms4-cbc",
|
||||
"sms4-cfb",
|
||||
"sms4-ofb",
|
||||
"sms4-ctr",
|
||||
"zuc",
|
||||
};
|
||||
const EVP_CIPHER *cipher;
|
||||
|
||||
OpenSSL_add_all_ciphers();
|
||||
|
||||
printf("%s New Ciphers:\n\n", OPENSSL_VERSION_TEXT);
|
||||
|
||||
for (i = 0; i < sizeof(names)/sizeof(names[i]); i++) {
|
||||
if (!(cipher = EVP_get_cipherbyname(names[i]))) {
|
||||
fprintf(stderr, "cipher \"%s\" is not supported\n", names[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
printf(" cipher nid : %d\n", EVP_CIPHER_nid(cipher));
|
||||
printf(" cipher name : %s\n", EVP_CIPHER_name(cipher));
|
||||
printf(" block size : %d\n", EVP_CIPHER_block_size(cipher));
|
||||
printf(" key length : %d\n", EVP_CIPHER_key_length(cipher));
|
||||
printf(" iv length : %d\n", EVP_CIPHER_iv_length(cipher));
|
||||
printf(" flags : 0x%016lx\n", EVP_CIPHER_flags(cipher));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
261
demos/gmssl/ectool.c
Normal file
261
demos/gmssl/ectool.c
Normal file
@@ -0,0 +1,261 @@
|
||||
/*
|
||||
* ecc - elliptic curve cryptography toolkit
|
||||
* a command line tool perform basic ecc cryptography
|
||||
* include ecpoint add, double, scalar multiply, invert
|
||||
* ecdsa sign and verify
|
||||
* ec -sign -key a.file -pass 123456
|
||||
* ec -verify <signature> -key a.file
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <popt.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/ecdh.h>
|
||||
#include <openssl/ecdsa.h>
|
||||
#include <openssl/objects.h>
|
||||
|
||||
enum commands {
|
||||
ECC_PRINT = 1,
|
||||
ECC_CHECK_POINT,
|
||||
ECC_RAND_SKEY,
|
||||
ECC_RAND_KEYPAIR,
|
||||
ECC_ADD,
|
||||
ECC_DOUBLE,
|
||||
ECC_MUL,
|
||||
ECC_MUL_G,
|
||||
ECC_INVERT,
|
||||
ECC_SIGN,
|
||||
ECC_VERIFY,
|
||||
};
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
int r;
|
||||
int ok = 0;
|
||||
char *prog = "ecc";
|
||||
|
||||
|
||||
// libpopt var
|
||||
poptContext popt_ctx;
|
||||
const char **rest;
|
||||
int command = 0;
|
||||
char *curve_name = "secp192k1";
|
||||
int point_compressed = 0;
|
||||
point_conversion_form_t point_form;
|
||||
|
||||
struct poptOption options[] = {
|
||||
{"curve-name", 'c', POPT_ARG_STRING, &curve_name, 0, "elliptic curve name", "NAME"},
|
||||
{"point-compressed", 'z', POPT_ARG_NONE, &point_compressed, 0, "point format, compress or uncompress", NULL},
|
||||
{"print-curve", 'p', POPT_ARG_VAL, &command, ECC_PRINT, "print elliptic curve parameters", NULL},
|
||||
{"random-private-key", 0, POPT_ARG_VAL, &command, ECC_RAND_SKEY, "random generate a private key\n", NULL},
|
||||
{"random-keypair", 0, POPT_ARG_VAL, &command, ECC_RAND_KEYPAIR, "generate a random key pair\n", NULL},
|
||||
{"check-point", 'e', POPT_ARG_VAL, &command, ECC_CHECK_POINT, "check if point is valid\n", NULL},
|
||||
{"point-add", 'a', POPT_ARG_VAL, &command, ECC_ADD, "elliptic curve point addition", NULL},
|
||||
{"point-double", 'b', POPT_ARG_VAL, &command, ECC_DOUBLE, "elliptic curve point double", NULL},
|
||||
{"point-mul", 'x', POPT_ARG_VAL, &command, ECC_MUL, "k*G", NULL},
|
||||
{"point-mul-generator", 'X', POPT_ARG_VAL, &command, ECC_MUL_G, "elliptic curve point scalar multiply", NULL},
|
||||
{"point-invert", 'i', POPT_ARG_VAL, &command, ECC_INVERT, "elliptic curve point inverse", NULL},
|
||||
{"ecdsa-sign", 's', POPT_ARG_VAL, &command, ECC_SIGN, "ecdsa sign", NULL},
|
||||
{"ecdsa-verify", 'v', POPT_ARG_VAL, &command, ECC_VERIFY, "ecdsa verify", NULL},
|
||||
POPT_AUTOHELP
|
||||
POPT_TABLEEND
|
||||
};
|
||||
|
||||
// openssl var
|
||||
EC_GROUP *ec_group = NULL;
|
||||
EC_POINT *P = NULL;
|
||||
EC_POINT *Q = NULL;
|
||||
EC_POINT *R = NULL;
|
||||
BIGNUM *k = BN_new();
|
||||
BN_CTX *bn_ctx = BN_CTX_new();
|
||||
|
||||
|
||||
// argument parsing
|
||||
popt_ctx = poptGetContext(argv[0], argc, argv, options, 0);
|
||||
if ((r = poptGetNextOpt(popt_ctx)) < -1) {
|
||||
fprintf(stderr, "%s: bad argument %s: %s\n", argv[0],
|
||||
poptBadOption(popt_ctx, POPT_BADOPTION_NOALIAS),
|
||||
poptStrerror(r));
|
||||
goto exit;
|
||||
}
|
||||
rest = poptGetArgs(popt_ctx);
|
||||
|
||||
|
||||
// check arguments
|
||||
ec_group = EC_GROUP_new_by_curve_name(OBJ_txt2nid(curve_name));
|
||||
if (ec_group == NULL) {
|
||||
fprintf(stderr, "%s: unknown curve name\n", prog);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
P = EC_POINT_new(ec_group);
|
||||
Q = EC_POINT_new(ec_group);
|
||||
R = EC_POINT_new(ec_group);
|
||||
|
||||
point_form = point_compressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED;
|
||||
|
||||
switch (command) {
|
||||
case ECC_PRINT:
|
||||
{
|
||||
BIGNUM *p = BN_new();
|
||||
BIGNUM *a = BN_new();
|
||||
BIGNUM *b = BN_new();
|
||||
char *generator;
|
||||
BIGNUM *order = BN_new();
|
||||
BIGNUM *cofactor = BN_new();
|
||||
|
||||
EC_GROUP_get_curve_GFp(ec_group, p, a, b, bn_ctx);
|
||||
generator = EC_POINT_point2hex(ec_group, EC_GROUP_get0_generator(ec_group), point_form, bn_ctx);
|
||||
EC_GROUP_get_order(ec_group, order, bn_ctx);
|
||||
EC_GROUP_get_cofactor(ec_group, cofactor, bn_ctx);
|
||||
|
||||
fprintf(stdout, "Name : %s\n", OBJ_nid2sn(EC_GROUP_get_curve_name(ec_group)));
|
||||
fprintf(stdout, "FieldType : %s\n", "PrimeField");
|
||||
fprintf(stdout, "Prime : %s\n", BN_bn2hex(p));
|
||||
fprintf(stdout, "A : %s\n", BN_bn2hex(a));
|
||||
fprintf(stdout, "B : %s\n", BN_bn2hex(b));
|
||||
fprintf(stdout, "Generator : %s\n", generator);
|
||||
fprintf(stdout, "Order : %s\n", BN_bn2hex(order));
|
||||
fprintf(stdout, "Cofactor : %s\n", BN_bn2hex(cofactor));
|
||||
|
||||
BN_free(p);
|
||||
BN_free(a);
|
||||
BN_free(b);
|
||||
BN_free(order);
|
||||
BN_free(cofactor);
|
||||
|
||||
break;
|
||||
}
|
||||
case ECC_CHECK_POINT:
|
||||
{
|
||||
if (!rest) {
|
||||
fprintf(stderr, "%s: short of point\n", prog);
|
||||
goto exit;
|
||||
}
|
||||
if (!rest[0]) {
|
||||
fprintf(stderr, "%s: short of point\n", prog);
|
||||
goto exit;
|
||||
}
|
||||
if (EC_POINT_hex2point(ec_group, rest[0], P, bn_ctx))
|
||||
fprintf(stdout, "ture\n");
|
||||
else
|
||||
fprintf(stdout, "false\n");
|
||||
break;
|
||||
}
|
||||
case ECC_RAND_SKEY:
|
||||
{
|
||||
EC_KEY *ec_key = EC_KEY_new();
|
||||
EC_KEY_set_group(ec_key, ec_group);
|
||||
EC_KEY_generate_key(ec_key);
|
||||
fprintf(stdout, "%s\n", BN_bn2hex(EC_KEY_get0_private_key(ec_key)));
|
||||
EC_KEY_free(ec_key);
|
||||
break;
|
||||
}
|
||||
case ECC_RAND_KEYPAIR:
|
||||
{
|
||||
EC_KEY *ec_key = EC_KEY_new();
|
||||
EC_KEY_set_group(ec_key, ec_group);
|
||||
EC_KEY_generate_key(ec_key);
|
||||
fprintf(stdout, "%s\n", BN_bn2hex(EC_KEY_get0_private_key(ec_key)));
|
||||
fprintf(stdout, "%s\n", EC_POINT_point2hex(ec_group, EC_KEY_get0_public_key(ec_key), point_form, bn_ctx));
|
||||
EC_KEY_free(ec_key);
|
||||
break;
|
||||
}
|
||||
case ECC_ADD:
|
||||
{
|
||||
if (!rest) {
|
||||
fprintf(stderr, "%s: short of point\n", prog);
|
||||
goto exit;
|
||||
}
|
||||
if (!rest[0] || !rest[1]) {
|
||||
fprintf(stderr, "%s: short of point\n", prog);
|
||||
goto exit;
|
||||
}
|
||||
if (!EC_POINT_hex2point(ec_group, rest[1], P, bn_ctx)) {
|
||||
fprintf(stderr, "%s: first point invalid\n", prog);
|
||||
goto exit;
|
||||
}
|
||||
if (!EC_POINT_hex2point(ec_group, rest[1], Q, bn_ctx)) {
|
||||
fprintf(stderr, "%s: second point invalid\n", prog);
|
||||
goto exit;
|
||||
}
|
||||
EC_POINT_add(ec_group, R, P, Q, bn_ctx);
|
||||
fprintf(stdout, "%s\n", EC_POINT_point2hex(ec_group, R, point_form, bn_ctx));
|
||||
break;
|
||||
}
|
||||
case ECC_DOUBLE:
|
||||
{
|
||||
EC_POINT_dbl(ec_group, R, P, bn_ctx);
|
||||
fprintf(stdout, "%s\n", EC_POINT_point2hex(ec_group, R, point_form, bn_ctx));
|
||||
break;
|
||||
}
|
||||
case ECC_MUL:
|
||||
{
|
||||
BIGNUM *order = NULL;
|
||||
|
||||
if (!BN_hex2bn(&k, rest[0])) {
|
||||
fprintf(stderr, "%s: integer invalid\n", prog);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
order = BN_new();
|
||||
EC_GROUP_get_order(ec_group, order, bn_ctx);
|
||||
if (BN_cmp(k, order) >= 0) {
|
||||
fprintf(stderr, "%s: integer value invalid\n", prog);
|
||||
BN_free(order);
|
||||
goto exit;
|
||||
}
|
||||
BN_free(order);
|
||||
|
||||
if (!EC_POINT_hex2point(ec_group, rest[1], P, bn_ctx)) {
|
||||
fprintf(stderr, "%s: point invalid\n", prog);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
EC_POINT_mul(ec_group, R, k, P, NULL, bn_ctx);
|
||||
fprintf(stdout, "%s\n", EC_POINT_point2hex(ec_group, R, point_form, bn_ctx));
|
||||
|
||||
break;
|
||||
}
|
||||
case ECC_MUL_G:
|
||||
{
|
||||
BIGNUM *order = NULL;
|
||||
if (!BN_hex2bn(&k, rest[0])) {
|
||||
fprintf(stderr, "%s: integer format invalid\n", prog);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
order = BN_new();
|
||||
EC_GROUP_get_order(ec_group, order, bn_ctx);
|
||||
if (BN_cmp(k, order) >= 0) {
|
||||
fprintf(stderr, "%s: integer value invalid\n", prog);
|
||||
BN_free(order);
|
||||
goto exit;
|
||||
}
|
||||
BN_free(order);
|
||||
|
||||
EC_POINT_mul(ec_group, R, k, EC_GROUP_get0_generator(ec_group), NULL, bn_ctx);
|
||||
fprintf(stdout, "%s\n", EC_POINT_point2hex(ec_group, R, point_form, bn_ctx));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
fprintf(stderr, "%s: command is required\n", prog);
|
||||
break;
|
||||
}
|
||||
ok = 1;
|
||||
|
||||
exit:
|
||||
if (ec_group) EC_GROUP_free(ec_group);
|
||||
if (P) EC_POINT_free(P);
|
||||
if (k) BN_free(k);
|
||||
if (bn_ctx) BN_CTX_free(bn_ctx);
|
||||
|
||||
return ok ? 0 : -1;
|
||||
}
|
||||
109
demos/gmssl/evpsm3.c
Normal file
109
demos/gmssl/evpsm3.c
Normal file
@@ -0,0 +1,109 @@
|
||||
/* demo/gmssl/sm3evp.c */
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2015 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int ret = -1;
|
||||
FILE *fp = stdin;
|
||||
unsigned char buf[1024];
|
||||
size_t len;
|
||||
const EVP_MD *md;
|
||||
EVP_MD_CTX mdctx;
|
||||
unsigned char dgst[EVP_MAX_MD_SIZE];
|
||||
unsigned int dgstlen, i;
|
||||
|
||||
if (argc == 2) {
|
||||
if (!(fp = fopen(argv[1], "r"))) {
|
||||
fprintf(stderr, "open file %s failed\n", argv[1]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
OpenSSL_add_all_digests();
|
||||
if (!(md = EVP_get_digestbyname("sm3"))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_DigestInit(&mdctx, md)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
while ((len = fread(buf, 1, sizeof(buf), fp))) {
|
||||
if (!EVP_DigestUpdate(&mdctx, buf, len)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (!EVP_DigestFinal(&mdctx, dgst, &dgstlen)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
for (i = 0; i < dgstlen; i++) {
|
||||
printf("%02x", dgst[i]);
|
||||
}
|
||||
printf("\n");
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
fclose(fp);
|
||||
EVP_cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -18,10 +18,15 @@ $gmssl genpkey -algorithm EC -out sm2key.pem -pkeyopt ec_paramgen_curve:sm2p256v
|
||||
#$gmssl pkey -text -noout -in sm2key.pem
|
||||
#$gmssl pkey -in sm2key.pem -pubout -out sm2pubkey.pem
|
||||
#$gmssl pkey -text -noout -pubin -in $pubkeyfile
|
||||
|
||||
echo hello | $gmssl pkeyutl -sign -inkey sm2key.pem -pkeyopt ec_sign_algor:sm2 > sm2sig.der
|
||||
|
||||
echo hello | $gmssl pkeyutl -verify -inkey sm2key.pem -sigfile sm2sig.der -pkeyopt ec_sign_algor:sm2
|
||||
|
||||
echo hello | $gmssl pkeyutl -encrypt -inkey sm2key.pem -pkeyopt ec_encrypt_algor:sm2 > sm2ciphertext.bin
|
||||
|
||||
|
||||
cat sm2ciphertext.bin | $gmssl pkeyutl -decrypt -inkey sm2key.pem -pkeyopt ec_encrypt_algor:sm2
|
||||
|
||||
$gmssl req -new -x509 -days 3650 -key sm2key.pem -out cert.pem -pkeyopt ec_sign_algor:sm2
|
||||
$gmssl req -new -x509 -days 3650 -key sm2key.pem -out cert.pem
|
||||
#$gmssl x509 -text -noout -in $DIR/cacert.pem
|
||||
|
||||
@@ -84,7 +84,7 @@ int main(int argc, char **argv)
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
HMAC_Init_ex(&hmctx, key, sizeof(key), md, NULL);
|
||||
|
||||
while ((len = fread(buf, 1, sizeof(buf), fp))) {
|
||||
@@ -1,38 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/opensslv.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
char *names[] = {
|
||||
"sms4-ecb",
|
||||
"sms4-cbc",
|
||||
"sms4-cfb",
|
||||
"sms4-ofb",
|
||||
"sms4-ctr",
|
||||
};
|
||||
const EVP_CIPHER *cipher;
|
||||
|
||||
OpenSSL_add_all_ciphers();
|
||||
|
||||
printf("%s new ciphers:\n\n", OPENSSL_VERSION_TEXT);
|
||||
|
||||
for (i = 0; i < sizeof(names)/sizeof(names[i]); i++) {
|
||||
if (!(cipher = EVP_get_cipherbyname(names[i]))) {
|
||||
fprintf(stderr, "cipher \"%s\" is not supported\n", names[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
printf(" cipher nid : %d\n", EVP_CIPHER_nid(cipher));
|
||||
printf(" cipher name : %s\n", EVP_CIPHER_name(cipher));
|
||||
printf(" block size : %d\n", EVP_CIPHER_block_size(cipher));
|
||||
printf(" key length : %d\n", EVP_CIPHER_key_length(cipher));
|
||||
printf(" iv length : %d\n", EVP_CIPHER_iv_length(cipher));
|
||||
printf(" flags : 0x%016lx\n", EVP_CIPHER_flags(cipher));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
20
demos/gmssl/memleak.c
Normal file
20
demos/gmssl/memleak.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/conf.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
BIO *bio_err;
|
||||
void *ptr;
|
||||
|
||||
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
|
||||
bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
|
||||
|
||||
ptr = OPENSSL_malloc(1024);
|
||||
|
||||
CRYPTO_mem_leaks(bio_err);
|
||||
BIO_free(bio_err);
|
||||
|
||||
return (0);
|
||||
}
|
||||
@@ -4,104 +4,55 @@
|
||||
#include <openssl/conf.h>
|
||||
#include <openssl/x509v3.h>
|
||||
|
||||
int mkit(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days)
|
||||
{
|
||||
X509 *x;
|
||||
EVP_PKEY *pk;
|
||||
EC_KEY *ec_key;
|
||||
X509_NAME *name = NULL;
|
||||
X509_NAME_ENTRY *ne = NULL;
|
||||
X509_EXTENSION *ex = NULL;
|
||||
|
||||
if ((pkeyp == NULL) || (*pkeyp == NULL)) {
|
||||
if ((pk = EVP_PKEY_new()) == NULL) {
|
||||
abort();
|
||||
return (0);
|
||||
}
|
||||
} else
|
||||
pk = *pkeyp;
|
||||
|
||||
if ((x509p == NULL) || (*x509p == NULL)) {
|
||||
if ((x = X509_new()) == NULL)
|
||||
goto err;
|
||||
} else {
|
||||
x = *x509p;
|
||||
}
|
||||
|
||||
|
||||
ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1);
|
||||
EC_KEY_generate_key(ec_key);
|
||||
|
||||
if (!EVP_PKEY_assign_EC_KEY(pk, ec_key)) {
|
||||
abort();
|
||||
goto err;
|
||||
}
|
||||
ec_key = NULL;
|
||||
|
||||
X509_set_version(x, 3);
|
||||
ASN1_INTEGER_set(X509_get_serialNumber(x), serial);
|
||||
X509_gmtime_adj(X509_get_notBefore(x), 0);
|
||||
X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * days);
|
||||
X509_set_pubkey(x, pk);
|
||||
|
||||
name = X509_get_subject_name(x);
|
||||
|
||||
/*
|
||||
* This function creates and adds the entry, working out the correct
|
||||
* string type and performing checks on its length. Normally we'd check
|
||||
* the return value for errors...
|
||||
*/
|
||||
X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, "UK", -1, -1, 0);
|
||||
X509_NAME_add_entry_by_txt(name, "CN",
|
||||
MBSTRING_ASC, "OpenSSL Group", -1, -1, 0);
|
||||
|
||||
X509_set_issuer_name(x, name);
|
||||
|
||||
/*
|
||||
* Add extension using V3 code: we can set the config file as NULL
|
||||
* because we wont reference any other sections. We can also set the
|
||||
* context to NULL because none of these extensions below will need to
|
||||
* access it.
|
||||
*/
|
||||
|
||||
ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_cert_type, "server");
|
||||
X509_add_ext(x, ex, -1);
|
||||
X509_EXTENSION_free(ex);
|
||||
|
||||
ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_comment,
|
||||
"example comment extension");
|
||||
X509_add_ext(x, ex, -1);
|
||||
X509_EXTENSION_free(ex);
|
||||
|
||||
ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_ssl_server_name,
|
||||
"www.openssl.org");
|
||||
|
||||
X509_add_ext(x, ex, -1);
|
||||
X509_EXTENSION_free(ex);
|
||||
|
||||
|
||||
if (!X509_sign(x, pk, EVP_sm3()))
|
||||
goto err;
|
||||
|
||||
*x509p = x;
|
||||
*pkeyp = pk;
|
||||
return (1);
|
||||
err:
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
BIO *bio_err;
|
||||
X509 *x509 = NULL;
|
||||
|
||||
EC_KEY *ec_key = NULL;
|
||||
EVP_PKEY *pkey = NULL;
|
||||
X509 *x509 = NULL;
|
||||
int serial = 123;
|
||||
int days = 365;
|
||||
X509_NAME *name = NULL;
|
||||
X509_NAME_ENTRY *ne = NULL;
|
||||
X509_EXTENSION *ex = NULL;
|
||||
|
||||
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
|
||||
|
||||
bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
|
||||
|
||||
ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1);
|
||||
EC_KEY_generate_key(ec_key);
|
||||
pkey = EVP_PKEY_new();
|
||||
EVP_PKEY_set1_EC_KEY(pkey, ec_key);
|
||||
ec_key = NULL;
|
||||
|
||||
mkit(&x509, &pkey, 512, 0, 365);
|
||||
x509 = X509_new();
|
||||
X509_set_version(x509, 3);
|
||||
ASN1_INTEGER_set(X509_get_serialNumber(x509), serial);
|
||||
X509_gmtime_adj(X509_get_notBefore(x509), 0);
|
||||
X509_gmtime_adj(X509_get_notAfter(x509), (long)60 * 60 * 24 * days);
|
||||
X509_set_pubkey(x509, pkey);
|
||||
|
||||
name = X509_get_subject_name(x509);
|
||||
X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, "UK", -1, -1, 0);
|
||||
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, "OpenSSL Group", -1, -1, 0);
|
||||
X509_set_issuer_name(x509, name);
|
||||
|
||||
ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_cert_type, "server");
|
||||
X509_add_ext(x509, ex, -1);
|
||||
X509_EXTENSION_free(ex);
|
||||
|
||||
ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_comment, "example comment extension");
|
||||
X509_add_ext(x509, ex, -1);
|
||||
X509_EXTENSION_free(ex);
|
||||
|
||||
ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_ssl_server_name, "www.openssl.org");
|
||||
|
||||
X509_add_ext(x509, ex, -1);
|
||||
X509_EXTENSION_free(ex);
|
||||
|
||||
X509_sign(x509, pkey, EVP_sm3());
|
||||
|
||||
EC_KEY_print_fp(stdout, pkey->pkey.ec, 0);
|
||||
X509_print_fp(stdout, x509);
|
||||
@@ -114,5 +65,6 @@ int main()
|
||||
|
||||
CRYPTO_mem_leaks(bio_err);
|
||||
BIO_free(bio_err);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
ÙÁ'‘Þ•R.—ÜäN¹áZzÆ+âûÐÓÜ
|
||||
N&‰Jš€Ê<E282AC>¾×bl0Þwya’>Ã7]"ˆ~½æß»·ÍC„9»Šû5ýO4ïWJqå/ûŠœ€æZ|Ãïihí[
|
||||
@@ -1,5 +1,5 @@
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIGHAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBG0wawIBAQQgyeMq+RmwB95Ohl+U
|
||||
K1KmE5/3OzxoG1lOpbyMu8sZxrqhRANCAATGmXcprKn9kYmMBKBLaxckcTFqDzNF
|
||||
qDwzk8rTcWr5/2CmI9KGeSMbp7G9X/v8qh/RIattztrYXlrVP0h7Zk+A
|
||||
MIGHAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBG0wawIBAQQgMfsFkjZrB5XuA5eX
|
||||
Utr1XlgaqbL4aJHLSvtOteur2oehRANCAASygUdsxLAxiuHlr42o93jHr7lJnAbW
|
||||
AsGM8lpqpz9803V6jGREo8Ajdh5lhqo0KPsfqwnVqjAE4CVLwCobJ6GZ
|
||||
-----END PRIVATE KEY-----
|
||||
|
||||
Binary file not shown.
@@ -1,32 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/ec.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
BIO *in = BIO_new_fp(stdin, BIO_NOCLOSE);
|
||||
EC_GROUP *group = NULL;
|
||||
EC_KEY *ec_key = NULL;
|
||||
ERR_load_crypto_strings();
|
||||
|
||||
|
||||
|
||||
group = PEM_read_bio_SM2PKParameters(in, NULL, NULL, NULL);
|
||||
if (!group) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!EC_GROUP_check(group, NULL)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
ec_key = EC_KEY_new();
|
||||
EC_KEY_set_group(ec_key, group);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/* demo/gmssl/sm3.c */
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2015 The GmSSL Project. All rights reserved.
|
||||
* Copyright (c) 2014 - 2016 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -50,60 +50,48 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
#include <unistd.h>
|
||||
#include <libgen.h>
|
||||
#include <openssl/sm3.h>
|
||||
|
||||
/*
|
||||
* usage of sm3dgst:
|
||||
* ./sm3dgst <file>
|
||||
* 324234234234235234234234234234
|
||||
*
|
||||
* echo "hello world" | sm3dgst
|
||||
* lksjdlfksdjlfkjsdlfkjsdlfkjsdljkfffffffldjfk=
|
||||
*
|
||||
*/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int ret = -1;
|
||||
FILE *fp = stdin;
|
||||
unsigned char buf[1024];
|
||||
size_t len;
|
||||
const EVP_MD *md;
|
||||
EVP_MD_CTX mdctx;
|
||||
unsigned char dgst[EVP_MAX_MD_SIZE];
|
||||
unsigned int dgstlen, i;
|
||||
sm3_ctx_t ctx;
|
||||
unsigned char dgst[SM3_DIGEST_LENGTH];
|
||||
unsigned char buf[4096];
|
||||
ssize_t len;
|
||||
int i;
|
||||
|
||||
if (argc == 2) {
|
||||
if (!(fp = fopen(argv[1], "r"))) {
|
||||
fprintf(stderr, "open file %s failed\n", argv[1]);
|
||||
return -1;
|
||||
}
|
||||
if (argc > 1) {
|
||||
printf("usage: %s < file\n", basename(argv[0]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
OpenSSL_add_all_digests();
|
||||
if (!(md = EVP_get_digestbyname("sm3"))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
sm3_init(&ctx);
|
||||
|
||||
if (!EVP_DigestInit(&mdctx, md)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
while ((len = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
|
||||
sm3_update(&ctx, buf, len);
|
||||
}
|
||||
memset(dgst, 0, sizeof(dgst));
|
||||
sm3_final(&ctx, dgst);
|
||||
|
||||
while ((len = fread(buf, 1, sizeof(buf), fp))) {
|
||||
if (!EVP_DigestUpdate(&mdctx, buf, len)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (!EVP_DigestFinal(&mdctx, dgst, &dgstlen)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
for (i = 0; i < dgstlen; i++) {
|
||||
for (i = 0; i < sizeof(dgst); i++) {
|
||||
printf("%02x", dgst[i]);
|
||||
}
|
||||
printf("\n");
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
fclose(fp);
|
||||
EVP_cleanup();
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user