This commit is contained in:
Zhi Guan
2015-10-07 15:23:34 +08:00
parent 254a1266d6
commit b4ad0da508
27 changed files with 590 additions and 88 deletions

78
crypto/cpk/Makefile Normal file
View File

@@ -0,0 +1,78 @@
#
# crypto/cpk/Makefile
#
DIR= cpk
TOP= ../..
CC= cc
INCLUDES= -I.. -I$(TOP) -I../../include
CFLAG=-g -Wall
MAKEFILE= Makefile
AR= ar r
CFLAGS= $(INCLUDES) $(CFLAG)
GENERAL=Makefile
TEST=cpktest.c
APPS=
LIB=$(TOP)/libcrypto.a
LIBSRC= cpk_lib.c cpk_map.c cpk_kap.c cpk_asn1.c cpk_prn.c cpk_err.c
LIBOBJ= cpk_lib.o cpk_map.o cpk_kap.o cpk_asn1.o cpk_prn.o cpk_err.o
SRC= $(LIBSRC)
EXHEADER= cpk.h
HEADER= $(EXHEADER)
ALL= $(GENERAL) $(SRC) $(HEADER)
top:
(cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
all: lib
lib: $(LIBOBJ)
$(AR) $(LIB) $(LIBOBJ)
$(RANLIB) $(LIB) || echo Never mind.
@touch lib
files:
$(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO
links:
@$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER)
@$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST)
@$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS)
install:
@[ -n "$(INSTALLTOP)" ] # should be set by top Makefile...
@headerlist="$(EXHEADER)"; for i in $$headerlist; \
do \
(cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \
chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \
done;
tags:
ctags $(SRC)
tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
update: depend
depend:
@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
dclean:
$(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
mv -f Makefile.new $(MAKEFILE)
clean:
rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
# DO NOT DELETE THIS LINE -- make depend depends on it.

View File

@@ -4,7 +4,7 @@
#include <openssl/err.h>
#include <openssl/x509.h>
#include <openssl/ossl_typ.h>
#include "ecies.h"
#include <openssl/ecies.h>
#ifdef __cplusplus
extern "C" {

169
crypto/cpk/cpktest.c Normal file
View File

@@ -0,0 +1,169 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <openssl/ec.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/objects.h>
#include "cpk.h"
#include "kdf.h"
#include "ecies.h"
const char *id_short = "id";
const char *id_long =
"123456789022345678903234567890423456789052345678906234567890"
"123456789022345678903234567890423456789052345678906234567890";
int EVP_PKEY_print_fp(const EVP_PKEY *pkey, FILE *fp)
{
ASN1_PCTX *ctx = ASN1_PCTX_new();
BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
EVP_PKEY_print_params(bio, pkey, 0, ctx);
EVP_PKEY_print_public(bio, pkey, 0, ctx);
EVP_PKEY_print_private(bio, pkey, 0, NULL);
return 0;
}
int main(int argc, char **argv)
{
int r, i;
KDF_FUNC kdf = NULL;
EC_GROUP *ec_group = NULL;
EC_KEY *ec_key = NULL;
EVP_PKEY *pkey = NULL;
EVP_PKEY *pub_key = NULL;
EVP_PKEY *priv_key = NULL;
X509_ALGOR *map = NULL;
CPK_MASTER_SECRET *master = NULL;
CPK_PUBLIC_PARAMS *params = NULL;
BIO *bio_out = NULL;
unsigned char *buf = NULL;
unsigned char *p;
const unsigned char *cp;
int len;
/* init openssl global functions */
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
/* prepare cpk setup parameters */
ec_key = EC_KEY_new_by_curve_name(OBJ_sn2nid("prime192v1"));
assert(ec_key != NULL);
EC_GROUP_set_asn1_flag((EC_GROUP *)EC_KEY_get0_group(ec_key), OPENSSL_EC_NAMED_CURVE);
r = EC_KEY_generate_key(ec_key);
assert(r == 1);
pkey = EVP_PKEY_new();
assert(pkey != NULL);
r = EVP_PKEY_set1_EC_KEY(pkey, ec_key);
assert(r == 1);
map = CPK_MAP_new_default();
assert(map != NULL);
//EVP_PKEY_print_fp(pkey, stdout);
/* generate master_secret and public_params */
master = CPK_MASTER_SECRET_create("domainid", pkey, map);
OPENSSL_assert(master);
bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
OPENSSL_assert(bio_out);
r = CPK_MASTER_SECRET_print(bio_out, master, 0, 0);
assert(r == 1);
EVP_PKEY_free(pkey);
pkey = NULL;
pkey = CPK_MASTER_SECRET_extract_private_key(master, "id");
assert(pkey != NULL);
EVP_PKEY_free(pkey);
//pkey = CPK_MASTER_SECRET_extract_private_key(master, NULL);
//assert(pkey == NULL);
pkey = CPK_MASTER_SECRET_extract_private_key(master, id_long);
assert(pkey != NULL);
printf("EVP_PKEY of '%s':\n", id_long);
EVP_PKEY_print_fp(pkey, stdout);
printf("\n");
params = CPK_MASTER_SECRET_extract_public_params(master);
assert(params);
r = CPK_PUBLIC_PARAMS_print(bio_out, params, 0, 0);
assert(r == 1);
printf("\n");
printf("test CPK_PUBLIC_PARAMS_extract_public_key()\n");
pub_key = CPK_PUBLIC_PARAMS_extract_public_key(params, id_short);
assert(pub_key != NULL);
EVP_PKEY_free(pub_key);
pub_key = CPK_PUBLIC_PARAMS_extract_public_key(params, id_long);
assert(pub_key != NULL);
printf("Public Key of '%s':\n", id_long);
EVP_PKEY_print_fp(pkey, stdout);
printf("\n");
r = CPK_MASTER_SECRET_validate_public_params(master, params);
assert(r == 1);
if (priv_key) EVP_PKEY_free(priv_key);
priv_key = CPK_MASTER_SECRET_extract_private_key(master, "identity");
assert(priv_key);
r = CPK_PUBLIC_PARAMS_validate_private_key(params, "identity", priv_key);
assert(r == 1);
r = CPK_PUBLIC_PARAMS_validate_private_key(params, "id", priv_key);
assert(r == 0);
/* der encoding and decoding */
len = i2d_CPK_MASTER_SECRET(master, NULL);
assert(len > 0);
if (buf != NULL) OPENSSL_free(buf);
buf = OPENSSL_malloc(len);
assert(buf != NULL);
p = buf;
len = i2d_CPK_MASTER_SECRET(master, &p);
assert(len > 0);
assert(p - buf == len);
cp = buf;
if (master) CPK_MASTER_SECRET_free(master);
master = NULL;
master = d2i_CPK_MASTER_SECRET(NULL, &cp, len);
assert(master != NULL);
r = CPK_MASTER_SECRET_validate_public_params(master, params);
assert(r == 1);
kdf = KDF_get_x9_63(EVP_sha1());
assert(kdf != NULL);
if (priv_key != NULL) EVP_PKEY_free(priv_key);
priv_key = CPK_MASTER_SECRET_extract_private_key(master, "Alice");
assert(priv_key != NULL);
if (buf != NULL) OPENSSL_free(buf);
buf = OPENSSL_malloc(1024);
assert(buf != NULL);
r = CPK_PUBLIC_PARAMS_compute_share_key(params, buf, 64, "Bob", priv_key, kdf);
for (i = 0; i < 64; i++) printf("%02x", buf[i]); printf("\n");
if (priv_key != NULL)
EVP_PKEY_free(priv_key);
priv_key = CPK_MASTER_SECRET_extract_private_key(master, "Bob");
assert(priv_key != NULL);
r = CPK_PUBLIC_PARAMS_compute_share_key(params, buf, 64, "Alice", priv_key, kdf);
for (i = 0; i < 64; i++) printf("%02x", buf[i]); printf("\n");
printf("ok\n");
return 0;
}