mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-11 10:56:17 +08:00
add CBC-MAC and GM OTP, not tested
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
-----BEGIN EC PARAMETERS-----
|
||||
BggqhkjOPQMBAQ==
|
||||
-----END EC PARAMETERS-----
|
||||
-----BEGIN EC PRIVATE KEY-----
|
||||
MF8CAQEEGFKLikRIH3/XDuvS4Ih6SFr+bWVAnAAv1aAKBggqhkjOPQMBAaE0AzIA
|
||||
BP2x9MuZ06z72j1BMoUe6zYbmenKt3RgZZufuTMbAoz2XIeFwlmDk2pX6XS2+uiU
|
||||
gA==
|
||||
-----END EC PRIVATE KEY-----
|
||||
@@ -1,10 +0,0 @@
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIBYQIBADCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEA/////v//////
|
||||
//////////////8AAAAA//////////8wRAQg/////v////////////////////8A
|
||||
AAAA//////////wEICjp+p6dn140TVqeS89lCafzl4n1FauPkt28vUFNlA6TBEEE
|
||||
MsSuLB8ZgRlfmQRGajnJlI/jC7/yZgvhcVpFiTNMdMe8Nzai9PZ3nFm9zuNraSFT
|
||||
0KmHfMYqR0AC3zLlITnwoAIhAP////7///////////////9yA99rIcYFK1O79Ak5
|
||||
1UEjAgEBBG0wawIBAQQgPiMJOFBUJIqDZgYNyIei38Yknx9O9PpMAcmLGVx4PQqh
|
||||
RANCAARZKqeiImjJ27a/49Cquf0Zz8U0429NlCFxY6YmS1Lu9i9ApqUH7UfY7tb0
|
||||
9w8CpoqgJk4TjDz9ZQxNJPA2kZlq
|
||||
-----END PRIVATE KEY-----
|
||||
@@ -1,7 +0,0 @@
|
||||
-----BEGIN EC PARAMETERS-----
|
||||
MIHgAgEBMCwGByqGSM49AQECIQD////+/////////////////////wAAAAD/////
|
||||
/////zBEBCD////+/////////////////////wAAAAD//////////AQgKOn6np2f
|
||||
XjRNWp5Lz2UJp/OXifUVq4+S3by9QU2UDpMEQQQyxK4sHxmBGV+ZBEZqOcmUj+ML
|
||||
v/JmC+FxWkWJM0x0x7w3NqL09necWb3O42tpIVPQqYd8xipHQALfMuUhOfCgAiEA
|
||||
/////v///////////////3ID32shxgUrU7v0CTnVQSMCAQE=
|
||||
-----END EC PARAMETERS-----
|
||||
@@ -1,9 +0,0 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEA/////v//////////
|
||||
//////////8AAAAA//////////8wRAQg/////v////////////////////8AAAAA
|
||||
//////////wEICjp+p6dn140TVqeS89lCafzl4n1FauPkt28vUFNlA6TBEEEMsSu
|
||||
LB8ZgRlfmQRGajnJlI/jC7/yZgvhcVpFiTNMdMe8Nzai9PZ3nFm9zuNraSFT0KmH
|
||||
fMYqR0AC3zLlITnwoAIhAP////7///////////////9yA99rIcYFK1O79Ak51UEj
|
||||
AgEBA0IABFkqp6IiaMnbtr/j0Kq5/RnPxTTjb02UIXFjpiZLUu72L0CmpQftR9ju
|
||||
1vT3DwKmiqAmThOMPP1lDE0k8DaRmWo=
|
||||
-----END PUBLIC KEY-----
|
||||
@@ -17,11 +17,25 @@ $gmssl version
|
||||
$gmssl ecparam -list_curves | grep sm2
|
||||
$gmssl ecparam -text -noout -name sm2p256v1 -param_enc explicit
|
||||
|
||||
$gmssl genpkey -genparam -algorithm SM2 $ecpkeyopt -out $paramfile
|
||||
$gmssl genpkey -algorithm SM2 $ecpkeyopt -out $keyfile
|
||||
$gmssl pkey -text -noout -in $keyfile
|
||||
gmssl genpkey -genparam -algorithm EC -out sm2p256v1.pem \
|
||||
-pkeyopt ec_paramgen_curve:sm2p256v1 \
|
||||
-pkeyopt ec_param_enc:named_curve
|
||||
|
||||
$gmssl pkey -in $keyfile -pubout -out $pubkeyfile
|
||||
$gmssl pkey -text -noout -pubin -in $pubkeyfile
|
||||
gmssl genpkey -algorithm EC -out sm2key.pem \
|
||||
-pkeyopt ec_paramgen_curve:sm2p256v1 \
|
||||
-pkeyopt ec_param_enc:named_curve
|
||||
|
||||
echo hello | $gmssl pkeyutl -sign -inkey $keyfile -hexdump
|
||||
# print private key
|
||||
#gmssl pkey -text -noout -in sm2key.pem
|
||||
|
||||
# export public key
|
||||
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
|
||||
|
||||
|
||||
50
demos/gmssl/server.c
Normal file
50
demos/gmssl/server.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/x509v3.h>
|
||||
|
||||
char *cert_file = "server.pem";
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
SSL_CTX *ctx = NULL;
|
||||
SSL *ssl = NULL;
|
||||
|
||||
SSL_library_init();
|
||||
SSL_load_error_strings();
|
||||
|
||||
|
||||
|
||||
if (!(ctx = SSL_CTX_new(GMSSLv1_method()))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!SSL_CTX_use_certificate_chain_file(ctx, cert_file)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!SSL_CTX_use_PrivateKey_file(ctx, cert_file, SSL_FILETYPE_PEM)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(ssl = SSL_new(ctx))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (SSL_accept(ssl) <= 0) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
#!/bin/bash -x
|
||||
|
||||
KEY_FILE=user.key
|
||||
REQ_FILE=user.req
|
||||
CERT_FILE=user.pem
|
||||
|
||||
gmssl ecparam -genkey -name sm2p256v1 -text -out $KEY_FILE
|
||||
gmssl req -new -key $KEY_FILE -out $REQ_FILE
|
||||
gmssl ca -out $CERT_FILE -outdir . -infiles $REQ_FILE
|
||||
gmssl pkcs12 -export -out user.pfx -in $CERT_FILE -inkey $KEY_FILE -certfile ./demoCA/cacert.pem
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
DIR=demoCA
|
||||
|
||||
rm -fr $DIR
|
||||
mkdir $DIR
|
||||
mkdir $DIR/certs
|
||||
mkdir $DIR/crl
|
||||
mkdir $DIR/newcerts
|
||||
mkdir $DIR/private/
|
||||
touch $DIR/index.txt
|
||||
touch $DIR/crlnumber
|
||||
touch $DIR/private/.rand
|
||||
echo 01 > $DIR/serial
|
||||
|
||||
gmssl ecparam -genkey -name sm2p256v1 -text -out $DIR/private/cakey.pem
|
||||
gmssl req -new -x509 -days 3650 -key $DIR/private/cakey.pem -out $DIR/cacert.pem
|
||||
gmssl x509 -text -noout -in $DIR/cacert.pem
|
||||
|
||||
109
demos/gmssl/sm2enc.c
Normal file
109
demos/gmssl/sm2enc.c
Normal file
@@ -0,0 +1,109 @@
|
||||
/* demo/gmssl/sm3.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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user