mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-12 19:36:27 +08:00
add CBC-MAC and GM OTP, not tested
This commit is contained in:
81
crypto/otp/Makefile
Normal file
81
crypto/otp/Makefile
Normal file
@@ -0,0 +1,81 @@
|
||||
#
|
||||
# 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
|
||||
LIBOBJ=otp.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
|
||||
|
||||
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.
|
||||
|
||||
123
crypto/otp/otp.c
Normal file
123
crypto/otp/otp.c
Normal file
@@ -0,0 +1,123 @@
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <openssl/evp.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;
|
||||
|
||||
OPENSSL_assert(sizeof(time_t) == 8);
|
||||
|
||||
if (!check_params(params)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
idlen = sizeof(uint64_t) + eventlen + params->option_size;
|
||||
if (idlen < 16) {
|
||||
idlen = 16;
|
||||
}
|
||||
if (!(id = OPENSSL_malloc(idlen))) {
|
||||
goto end;
|
||||
}
|
||||
bzero(id, idlen);
|
||||
|
||||
t = time(NULL);
|
||||
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())) {
|
||||
goto end;
|
||||
}
|
||||
if (!EVP_DigestInit_ex(mdctx, md, NULL)) {
|
||||
goto end;
|
||||
}
|
||||
if (!EVP_DigestUpdate(mdctx, key, keylen)) {
|
||||
goto end;
|
||||
}
|
||||
if (!EVP_DigestUpdate(mdctx, id, idlen)) {
|
||||
goto end;
|
||||
}
|
||||
if (!EVP_DigestFinal_ex(mdctx, s, (unsigned int *)&slen)) {
|
||||
goto end;
|
||||
}
|
||||
} else if (params->type == NID_sms4_ecb) {
|
||||
cipher = EVP_get_cipherbynid(params->type);
|
||||
if (!(cmctx = CBCMAC_CTX_new())) {
|
||||
goto end;
|
||||
}
|
||||
if (!CBCMAC_Init(cmctx, key, keylen, cipher, NULL)) {
|
||||
goto end;
|
||||
}
|
||||
if (!CBCMAC_Update(cmctx, id, idlen)) {
|
||||
goto end;
|
||||
}
|
||||
if (!CBCMAC_Final(cmctx, s, &slen)) {
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
goto end;
|
||||
}
|
||||
OPENSSL_assert(slen % 4 == 0);
|
||||
|
||||
od = 0;
|
||||
for (i = 0; i < slen/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;
|
||||
}
|
||||
|
||||
85
crypto/otp/otp.h
Normal file
85
crypto/otp/otp.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/* 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;
|
||||
} 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);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user