This commit is contained in:
Zhi Guan
2017-02-15 15:35:07 +08:00
parent 7eb9c6664a
commit b0d309b527
14 changed files with 455 additions and 5 deletions

View File

@@ -311,7 +311,7 @@ $config{sdirs} = [
"buffer", "bio", "stack", "lhash", "rand", "err",
"evp", "asn1", "pem", "x509", "x509v3", "conf", "txt_db", "pkcs7", "pkcs12", "comp", "ocsp", "ui",
"cms", "ts", "srp", "cmac", "ct", "async", "kdf",
"sm3", "sms4", "kdf2", "ecies", "ffx", "sm2", "paillier", "cpk"
"sm3", "sms4", "kdf2", "ecies", "ffx", "sm2", "paillier", "cpk", "otp"
];
# Known TLS and DTLS protocols

View File

@@ -63,6 +63,7 @@ static ERR_STRING_DATA ERR_str_libraries[] = {
{ERR_PACK(ERR_LIB_FFX, 0, 0), "FFX routines"},
{ERR_PACK(ERR_LIB_PAILLIER, 0, 0), "PAILLIER routines"},
{ERR_PACK(ERR_LIB_CPK, 0, 0), "CPK routines"},
{ERR_PACK(ERR_LIB_OTP, 0, 0), "OTP routines"},
{0, NULL},
};
@@ -111,6 +112,7 @@ static ERR_STRING_DATA ERR_str_reasons[] = {
{ERR_R_FFX_LIB, "FFX lib"},
{ERR_R_PAILLIER_LIB, "PAILLIER lib"},
{ERR_R_CPK_LIB, "CPK lib"},
{ERR_R_OTP_LIB, "OTP lib"},
{ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
{ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},

View File

@@ -117,6 +117,9 @@ int err_load_crypto_strings_int(void)
# ifndef OPENSSL_NO_CPK
ERR_load_CPK_strings() == 0 ||
# endif
# ifndef OPENSSL_NO_OTP
ERR_load_OTP_strings() == 0 ||
# endif
#endif
ERR_load_KDF_strings() == 0)
return 0;

View File

@@ -37,8 +37,9 @@ L ASYNC include/openssl/async.h crypto/async/async_err.c
L KDF include/openssl/kdf.h crypto/kdf/kdf_err.c
L KDF2 include/openssl/kdf2.h crypto/kdf2/kdf2_err.c
L FFX include/openssl/ffx.h crypto/ffx/ffx_err.c
L PAILLIER include/openssl/paillier.h crypto/paillier/paillier_err.c
L PAILLIER include/openssl/paillier.h crypto/paillier/pai_err.c
L CPK include/openssl/cpk.h crypto/cpk/cpk_err.c
L OTP include/openssl/otp.h crypto/otp/otp_err.c
# additional header files to be scanned for function names
L NONE crypto/x509/x509_vfy.h NONE

2
crypto/otp/build.info Normal file
View File

@@ -0,0 +1,2 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=otp_err.c otp_lib.c

44
crypto/otp/otp_err.c Normal file
View File

@@ -0,0 +1,44 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#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_CMAC_FAILURE), "cmac failure"},
{ERR_REASON(OTP_R_INVALID_PARAMS), "invalid params"},
{0, NULL}
};
#endif
int 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
return 1;
}

187
crypto/otp/otp_lib.c Normal file
View File

@@ -0,0 +1,187 @@
/* ====================================================================
* Copyright (c) 2015 - 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
* 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 <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/cmac.h>
#include <openssl/kdf2.h>
#include <openssl/otp.h>
#include "../modes/modes_lcl.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;
CMAC_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, ERR_R_MALLOC_FAILURE);
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_new())) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_MALLOC_FAILURE);
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 = CMAC_CTX_new())) {
OTPerr(OTP_F_OTP_GENERATE, ERR_R_MALLOC_FAILURE);
goto end;
}
if (!CMAC_Init(cmctx, key, keylen, cipher, NULL)) {
OTPerr(OTP_F_OTP_GENERATE, OTP_R_CMAC_FAILURE);
goto end;
}
if (!CMAC_Update(cmctx, id, idlen)) {
OTPerr(OTP_F_OTP_GENERATE, OTP_R_CMAC_FAILURE);
goto end;
}
if (!CMAC_Final(cmctx, s, &slen)) {
OTPerr(OTP_F_OTP_GENERATE, OTP_R_CMAC_FAILURE);
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_free(mdctx);
CMAC_CTX_free(cmctx);
return ret;
}

View File

@@ -1,6 +1,6 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy

View File

@@ -97,6 +97,7 @@ typedef struct err_state_st {
# define ERR_LIB_FFX 54
# define ERR_LIB_PAILLIER 55
# define ERR_LIB_CPK 56
# define ERR_LIB_OTP 57
# define ERR_LIB_USER 128
@@ -139,6 +140,7 @@ typedef struct err_state_st {
# define FFXerr(f,r) ERR_PUT_error(ERR_LIB_FFX,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
# define PAILLIERerr(f,r) ERR_PUT_error(ERR_LIB_PAILLIER,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
# define CPKerr(f,r) ERR_PUT_error(ERR_LIB_CPK,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
# define OTPerr(f,r) ERR_PUT_error(ERR_LIB_OTP,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
# define ERR_PACK(l,f,r) ( \
(((unsigned int)(l) & 0x0FF) << 24L) | \
@@ -190,6 +192,7 @@ typedef struct err_state_st {
# define ERR_R_FFX_LIB ERR_LIB_FFX/* 54 */
# define ERR_R_PAILLIER_LIB ERR_LIB_PAILLIER/* 55 */
# define ERR_R_CPK_LIB ERR_LIB_CPK/* 56 */
# define ERR_R_OTP_LIB ERR_LIB_OTP/* 57 */
# define ERR_R_NESTED_ASN1_ERROR 58
# define ERR_R_MISSING_ASN1_EOS 63

101
include/openssl/otp.h Normal file
View File

@@ -0,0 +1,101 @@
/* ====================================================================
* Copyright (c) 2015 - 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
* 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.
* ====================================================================
*/
/* The implementation of
* GM/T 0021-2012 One Time Password Application of Cryptography Algorithm
*/
#ifndef HEADER_OTP_H
#define HEADER_OTP_H
#ifdef __cplusplus
extern "C" {
#endif
/* 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()
*/
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;
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.
*/
int ERR_load_OTP_strings(void);
/* Error codes for the OTP functions. */
/* Function codes. */
# define OTP_F_OTP_GENERATE 100
/* Reason codes. */
# define OTP_R_CMAC_FAILURE 101
# define OTP_R_INVALID_PARAMS 100
# ifdef __cplusplus
}
# endif
#endif

View File

@@ -17,7 +17,8 @@ IF[{- !$disabled{tests} -}]
dtlsv1listentest ct_test threadstest afalgtest d2i_test \
ssl_test_ctx_test ssl_test x509aux cipherlist_test asynciotest \
bioprinttest sslapitest dtlstest sslcorrupttest bio_enc_test \
sm3test sms4test kdf2test eciestest ffxtest sm2test pailliertest cpktest
sm3test sms4test kdf2test eciestest ffxtest sm2test \
pailliertest cpktest otptest
SOURCE[aborttest]=aborttest.c
INCLUDE[aborttest]=../include
@@ -316,6 +317,10 @@ IF[{- !$disabled{tests} -}]
INCLUDE[cpktest]=../include
DEPEND[cpktest]=../libcrypto
SOURCE[otptest]=otptest.c
INCLUDE[otptest]=../include
DEPEND[otptest]=../libcrypto
IF[{- !$disabled{shared} -}]
PROGRAMS_NO_INST=shlibloadtest
SOURCE[shlibloadtest]=shlibloadtest.c

90
test/otptest.c Normal file
View File

@@ -0,0 +1,90 @@
/* ====================================================================
* 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
* 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 <stdlib.h>
#include "../e_os.h"
#ifdef OPENSSL_NO_OTP
int main(int argc, char **argv)
{
printf("NO OTP support\n");
return 0;
}
#else
# 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;
}
#endif

View File

@@ -0,0 +1,12 @@
#! /usr/bin/env perl
# Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the OpenSSL license (the "License"). You may not use
# this file except in compliance with the License. You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
use OpenSSL::Test::Simple;
simple_test("test_otp", "otptest", "otp");

View File

@@ -132,7 +132,7 @@ my @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
"CMAC",
# APPLINK (win build feature?)
"APPLINK",
"SM3", "SMS4", "KDF2", "ECIES", "FFX", "PAILLIER", "CPK"
"SM3", "SMS4", "KDF2", "ECIES", "FFX", "PAILLIER", "CPK", "OTP"
);
my %disabled_algorithms;