Merge remote-tracking branch 'origin/master'

# Conflicts:
#	README.md
This commit is contained in:
Zhi Guan
2017-02-14 16:12:29 +08:00
parent d2254170b8
commit 43fed1108d
3503 changed files with 320546 additions and 408546 deletions

View File

@@ -1,100 +0,0 @@
#
# OpenSSL/crypto/sm3/Makefile
#
DIR= otp
TOP= ../..
CC= cc
CPP= $(CC) -E
INCLUDES=-I.. -I$(TOP) -I../../include
CFLAG=-g
MAKEFILE= Makefile
AR= ar r
SM3_ASM_OBJ=
CFLAGS= $(INCLUDES) $(CFLAG)
ASFLAGS= $(INCLUDES) $(ASFLAG)
AFLAGS= $(ASFLAGS)
GENERAL=Makefile
TEST=otptest.c
APPS=
LIB=$(TOP)/libcrypto.a
LIBSRC=otp.c otp_err.c
LIBOBJ=otp.o otp_err.o
SRC= $(LIBSRC)
EXHEADER= otp.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 *.s *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
# DO NOT DELETE THIS LINE -- make depend depends on it.
otp.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
otp.o: ../../include/openssl/cbcmac.h ../../include/openssl/crypto.h
otp.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
otp.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h
otp.o: ../../include/openssl/modes.h ../../include/openssl/obj_mac.h
otp.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
otp.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
otp.o: ../../include/openssl/otp.h ../../include/openssl/safestack.h
otp.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
otp.o: ../modes/modes_lcl.h otp.c
otp_err.o: ../../include/openssl/bio.h ../../include/openssl/crypto.h
otp_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
otp_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
otp_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
otp_err.o: ../../include/openssl/otp.h ../../include/openssl/safestack.h
otp_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
otp_err.o: otp_err.c

View File

@@ -1,100 +0,0 @@
#
# OpenSSL/crypto/sm3/Makefile
#
DIR= otp
TOP= ../..
CC= cc
CPP= $(CC) -E
INCLUDES=-I.. -I$(TOP) -I../../include
CFLAG=-g
MAKEFILE= Makefile
AR= ar r
SM3_ASM_OBJ=
CFLAGS= $(INCLUDES) $(CFLAG)
ASFLAGS= $(INCLUDES) $(ASFLAG)
AFLAGS= $(ASFLAGS)
GENERAL=Makefile
TEST=otptest.c
APPS=
LIB=$(TOP)/libcrypto.a
LIBSRC=otp.c otp_err.c
LIBOBJ=otp.o otp_err.o
SRC= $(LIBSRC)
EXHEADER= otp.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 *.s *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
# DO NOT DELETE THIS LINE -- make depend depends on it.
otp.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
otp.o: ../../include/openssl/cbcmac.h ../../include/openssl/crypto.h
otp.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
otp.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h
otp.o: ../../include/openssl/modes.h ../../include/openssl/obj_mac.h
otp.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
otp.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
otp.o: ../../include/openssl/otp.h ../../include/openssl/safestack.h
otp.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
otp.o: ../modes/modes_lcl.h otp.c
otp_err.o: ../../include/openssl/bio.h ../../include/openssl/crypto.h
otp_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
otp_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
otp_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
otp_err.o: ../../include/openssl/otp.h ../../include/openssl/safestack.h
otp_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
otp_err.o: otp_err.c

View File

@@ -1,137 +0,0 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/crypto.h>
#include <openssl/cbcmac.h>
#include "../modes/modes_lcl.h"
#include <openssl/otp.h>
static int pow_table[] = {
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
};
static int check_params(const OTP_PARAMS *params)
{
if ((params->te < 1 || params->te > 60) ||
(params->type != NID_sm3 && params->type != NID_sms4_ecb) || /* about to change */
(params->otp_digits >= sizeof(pow_table) || params->otp_digits < 4)) {
return 0;
}
return 1;
}
int OTP_generate(const OTP_PARAMS *params, const void *event, size_t eventlen,
unsigned int *otp, const unsigned char *key, size_t keylen)
{
int ret = 0;
time_t t = 0;
unsigned char *id = NULL;
size_t idlen;
const EVP_MD *md;
const EVP_CIPHER *cipher;
EVP_MD_CTX *mdctx = NULL;
CBCMAC_CTX *cmctx = NULL;
unsigned char s[EVP_MAX_MD_SIZE];
size_t slen;
uint32_t od;
int i, n;
OPENSSL_assert(sizeof(time_t) == 8);
if (!check_params(params)) {
OTPerr(OTP_F_OTP_GENERATE, OTP_R_INVALID_PARAMS);
return 0;
}
idlen = sizeof(uint64_t) + eventlen + params->option_size;
if (idlen < 16) {
idlen = 16;
}
if (!(id = OPENSSL_malloc(idlen))) {
OTPerr(OTP_F_OTP_GENERATE, OTP_R_MALLOC_FAILED);
goto end;
}
memset(id, 0, idlen);
t = time(NULL) + params->offset;
t /= params->te;
memcpy(id, &t, sizeof(t));
memcpy(id + sizeof(t), event, eventlen);
memcpy(id + sizeof(t) + eventlen, params->option, params->option_size);
/* FIXME: try to get md and cipher, and check if cipher is ECB */
if (params->type == NID_sm3) {
md = EVP_get_digestbynid(params->type);
if (!(mdctx = EVP_MD_CTX_create())) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestInit_ex(mdctx, md, NULL)) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(mdctx, key, keylen)) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(mdctx, id, idlen)) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestFinal_ex(mdctx, s, (unsigned int *)&slen)) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_EVP_LIB);
goto end;
}
} else if (params->type == NID_sms4_ecb) {
cipher = EVP_get_cipherbynid(params->type);
if (!(cmctx = CBCMAC_CTX_new())) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_CBCMAC_LIB);
goto end;
}
if (!CBCMAC_Init(cmctx, key, keylen, cipher, NULL)) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_CBCMAC_LIB);
goto end;
}
if (!CBCMAC_Update(cmctx, id, idlen)) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_CBCMAC_LIB);
goto end;
}
if (!CBCMAC_Final(cmctx, s, &slen)) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_CBCMAC_LIB);
goto end;
}
} else {
goto end;
}
OPENSSL_assert(slen % 4 == 0);
od = 0;
n = (int)slen;
for (i = 0; i < n/4; i++) {
od += GETU32(&s[i * 4]);
}
*otp = od % pow_table[params->otp_digits];
ret = 1;
end:
OPENSSL_free(id);
EVP_MD_CTX_destroy(mdctx);
CBCMAC_CTX_free(cmctx);
return ret;
}

View File

@@ -1,101 +0,0 @@
/* crypto/otp/otp.h */
/* ====================================================================
* Copyright (c) 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.
* ====================================================================
*
*/
#ifndef HEADER_OTP_H
#define HEADER_OTP_H
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct OTP_PARAMS_st {
int type;
int te;
void *option;
size_t option_size;
int otp_digits;
/* adjust the clock in seconds */
int offset;
} OTP_PARAMS;
/* OTP reference to the GM/T OTP specification
* type should be a valid md nid or a ECB cipher nid
* te is the time period in the range [1, 60]
* event is the C in ID = {T|C|O}
* opt is the O in ID = {T|C|O}
* otp_digits is the number of digits of otp, choose in the range [4, 8]
* otp the output otp value, convert to digits with snprintf()
*/
int OTP_generate(const OTP_PARAMS *params, const void *event, size_t eventlen,
unsigned int *otp, const unsigned char *key, size_t keylen);
/* BEGIN ERROR CODES */
/*
* The following lines are auto generated by the script mkerr.pl. Any changes
* made after this point may be overwritten when the script is next run.
*/
void ERR_load_OTP_strings(void);
/* Error codes for the OTP functions. */
/* Function codes. */
# define OTP_F_OTP_GENERATE 100
/* Reason codes. */
# define OTP_R_INVALID_PARAMS 101
# define OTP_R_MALLOC_FAILED 100
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,94 +0,0 @@
/* crypto/otp/otp_err.c */
/* ====================================================================
* Copyright (c) 1999-2016 The OpenSSL 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 OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL 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 OpenSSL 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.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
/*
* NOTE: this file was auto generated by the mkerr.pl script: any changes
* made to it will be overwritten when the script next updates this file,
* only reason strings will be preserved.
*/
#include <stdio.h>
#include <openssl/err.h>
#include <openssl/otp.h>
/* BEGIN ERROR CODES */
#ifndef OPENSSL_NO_ERR
# define ERR_FUNC(func) ERR_PACK(ERR_LIB_OTP,func,0)
# define ERR_REASON(reason) ERR_PACK(ERR_LIB_OTP,0,reason)
static ERR_STRING_DATA OTP_str_functs[] = {
{ERR_FUNC(OTP_F_OTP_GENERATE), "OTP_generate"},
{0, NULL}
};
static ERR_STRING_DATA OTP_str_reasons[] = {
{ERR_REASON(OTP_R_INVALID_PARAMS), "invalid params"},
{ERR_REASON(OTP_R_MALLOC_FAILED), "malloc failed"},
{0, NULL}
};
#endif
void ERR_load_OTP_strings(void)
{
#ifndef OPENSSL_NO_ERR
if (ERR_func_error_string(OTP_str_functs[0].error) == NULL) {
ERR_load_strings(0, OTP_str_functs);
ERR_load_strings(0, OTP_str_reasons);
}
#endif
}

View File

@@ -1,30 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/otp.h>
int main(int argc, char **argv)
{
OTP_PARAMS params;
unsigned char key[] = {1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8};
unsigned char event[] = "this is a fixed value";
unsigned int otp;
params.type = NID_sm3;
params.te = 60;
params.option = NULL;
params.option_size = 0;
params.otp_digits = 6;
OpenSSL_add_all_algorithms();
if (!OTP_generate(&params, event, sizeof(event), &otp, key, sizeof(key))) {
printf("OTP_generate() failed\n");
return -1;
}
printf("OTP = %06u\n", otp);
return 0;
}