mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-13 08:23:50 +08:00
Merge remote-tracking branch 'origin/master'
# Conflicts: # README.md
This commit is contained in:
@@ -1,86 +0,0 @@
|
||||
#
|
||||
# crypto/kdf/Makefile
|
||||
#
|
||||
|
||||
DIR= kdf
|
||||
TOP= ../..
|
||||
CC= cc
|
||||
INCLUDES= -I.. -I$(TOP) -I../../include
|
||||
CFLAG=-g -Wall
|
||||
MAKEFILE= Makefile
|
||||
AR= ar r
|
||||
|
||||
CFLAGS= $(INCLUDES) $(CFLAG)
|
||||
|
||||
GENERAL=Makefile
|
||||
TEST=kdftest.c
|
||||
APPS=
|
||||
|
||||
LIB=$(TOP)/libcrypto.a
|
||||
LIBSRC= kdf_x9_63.c
|
||||
LIBOBJ= kdf_x9_63.o
|
||||
|
||||
SRC= $(LIBSRC)
|
||||
|
||||
EXHEADER= kdf.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.
|
||||
|
||||
kdf_x9_63.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
|
||||
kdf_x9_63.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
|
||||
kdf_x9_63.o: ../../include/openssl/evp.h ../../include/openssl/kdf.h
|
||||
kdf_x9_63.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
|
||||
kdf_x9_63.o: ../../include/openssl/opensslconf.h
|
||||
kdf_x9_63.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
|
||||
kdf_x9_63.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
|
||||
kdf_x9_63.o: ../../include/openssl/symhacks.h kdf_x9_63.c
|
||||
@@ -1,86 +0,0 @@
|
||||
#
|
||||
# crypto/kdf/Makefile
|
||||
#
|
||||
|
||||
DIR= kdf
|
||||
TOP= ../..
|
||||
CC= cc
|
||||
INCLUDES= -I.. -I$(TOP) -I../../include
|
||||
CFLAG=-g -Wall
|
||||
MAKEFILE= Makefile
|
||||
AR= ar r
|
||||
|
||||
CFLAGS= $(INCLUDES) $(CFLAG)
|
||||
|
||||
GENERAL=Makefile
|
||||
TEST=kdftest.c
|
||||
APPS=
|
||||
|
||||
LIB=$(TOP)/libcrypto.a
|
||||
LIBSRC= kdf_x9_63.c
|
||||
LIBOBJ= kdf_x9_63.o
|
||||
|
||||
SRC= $(LIBSRC)
|
||||
|
||||
EXHEADER= kdf.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.
|
||||
|
||||
kdf_x9_63.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
|
||||
kdf_x9_63.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
|
||||
kdf_x9_63.o: ../../include/openssl/evp.h ../../include/openssl/obj_mac.h
|
||||
kdf_x9_63.o: ../../include/openssl/objects.h
|
||||
kdf_x9_63.o: ../../include/openssl/opensslconf.h
|
||||
kdf_x9_63.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
|
||||
kdf_x9_63.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
|
||||
kdf_x9_63.o: ../../include/openssl/symhacks.h kdf.h kdf_x9_63.c
|
||||
3
crypto/kdf/build.info
Normal file
3
crypto/kdf/build.info
Normal file
@@ -0,0 +1,3 @@
|
||||
LIBS=../../libcrypto
|
||||
SOURCE[../../libcrypto]=\
|
||||
tls1_prf.c kdf_err.c hkdf.c
|
||||
293
crypto/kdf/hkdf.c
Normal file
293
crypto/kdf/hkdf.c
Normal file
@@ -0,0 +1,293 @@
|
||||
/*
|
||||
* Copyright 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
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/kdf.h>
|
||||
#include <openssl/evp.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include "internal/evp_int.h"
|
||||
|
||||
#define HKDF_MAXBUF 1024
|
||||
|
||||
static unsigned char *HKDF(const EVP_MD *evp_md,
|
||||
const unsigned char *salt, size_t salt_len,
|
||||
const unsigned char *key, size_t key_len,
|
||||
const unsigned char *info, size_t info_len,
|
||||
unsigned char *okm, size_t okm_len);
|
||||
|
||||
static unsigned char *HKDF_Extract(const EVP_MD *evp_md,
|
||||
const unsigned char *salt, size_t salt_len,
|
||||
const unsigned char *key, size_t key_len,
|
||||
unsigned char *prk, size_t *prk_len);
|
||||
|
||||
static unsigned char *HKDF_Expand(const EVP_MD *evp_md,
|
||||
const unsigned char *prk, size_t prk_len,
|
||||
const unsigned char *info, size_t info_len,
|
||||
unsigned char *okm, size_t okm_len);
|
||||
|
||||
typedef struct {
|
||||
const EVP_MD *md;
|
||||
unsigned char *salt;
|
||||
size_t salt_len;
|
||||
unsigned char *key;
|
||||
size_t key_len;
|
||||
unsigned char info[HKDF_MAXBUF];
|
||||
size_t info_len;
|
||||
} HKDF_PKEY_CTX;
|
||||
|
||||
static int pkey_hkdf_init(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
HKDF_PKEY_CTX *kctx;
|
||||
|
||||
kctx = OPENSSL_zalloc(sizeof(*kctx));
|
||||
if (kctx == NULL)
|
||||
return 0;
|
||||
|
||||
ctx->data = kctx;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void pkey_hkdf_cleanup(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
HKDF_PKEY_CTX *kctx = ctx->data;
|
||||
OPENSSL_clear_free(kctx->salt, kctx->salt_len);
|
||||
OPENSSL_clear_free(kctx->key, kctx->key_len);
|
||||
OPENSSL_cleanse(kctx->info, kctx->info_len);
|
||||
OPENSSL_free(kctx);
|
||||
}
|
||||
|
||||
static int pkey_hkdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
{
|
||||
HKDF_PKEY_CTX *kctx = ctx->data;
|
||||
|
||||
switch (type) {
|
||||
case EVP_PKEY_CTRL_HKDF_MD:
|
||||
if (p2 == NULL)
|
||||
return 0;
|
||||
|
||||
kctx->md = p2;
|
||||
return 1;
|
||||
|
||||
case EVP_PKEY_CTRL_HKDF_SALT:
|
||||
if (p1 == 0 || p2 == NULL)
|
||||
return 1;
|
||||
|
||||
if (p1 < 0)
|
||||
return 0;
|
||||
|
||||
if (kctx->salt != NULL)
|
||||
OPENSSL_clear_free(kctx->salt, kctx->salt_len);
|
||||
|
||||
kctx->salt = OPENSSL_memdup(p2, p1);
|
||||
if (kctx->salt == NULL)
|
||||
return 0;
|
||||
|
||||
kctx->salt_len = p1;
|
||||
return 1;
|
||||
|
||||
case EVP_PKEY_CTRL_HKDF_KEY:
|
||||
if (p1 < 0)
|
||||
return 0;
|
||||
|
||||
if (kctx->key != NULL)
|
||||
OPENSSL_clear_free(kctx->key, kctx->key_len);
|
||||
|
||||
kctx->key = OPENSSL_memdup(p2, p1);
|
||||
if (kctx->key == NULL)
|
||||
return 0;
|
||||
|
||||
kctx->key_len = p1;
|
||||
return 1;
|
||||
|
||||
case EVP_PKEY_CTRL_HKDF_INFO:
|
||||
if (p1 == 0 || p2 == NULL)
|
||||
return 1;
|
||||
|
||||
if (p1 < 0 || p1 > (int)(HKDF_MAXBUF - kctx->info_len))
|
||||
return 0;
|
||||
|
||||
memcpy(kctx->info + kctx->info_len, p2, p1);
|
||||
kctx->info_len += p1;
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return -2;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int pkey_hkdf_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
|
||||
const char *value)
|
||||
{
|
||||
if (strcmp(type, "md") == 0)
|
||||
return EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_get_digestbyname(value));
|
||||
|
||||
if (strcmp(type, "salt") == 0)
|
||||
return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_HKDF_SALT, value);
|
||||
|
||||
if (strcmp(type, "hexsalt") == 0)
|
||||
return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_HKDF_SALT, value);
|
||||
|
||||
if (strcmp(type, "key") == 0)
|
||||
return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_HKDF_KEY, value);
|
||||
|
||||
if (strcmp(type, "hexkey") == 0)
|
||||
return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_HKDF_KEY, value);
|
||||
|
||||
if (strcmp(type, "info") == 0)
|
||||
return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_HKDF_INFO, value);
|
||||
|
||||
if (strcmp(type, "hexinfo") == 0)
|
||||
return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_HKDF_INFO, value);
|
||||
|
||||
return -2;
|
||||
}
|
||||
|
||||
static int pkey_hkdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
|
||||
size_t *keylen)
|
||||
{
|
||||
HKDF_PKEY_CTX *kctx = ctx->data;
|
||||
|
||||
if (kctx->md == NULL || kctx->key == NULL)
|
||||
return 0;
|
||||
|
||||
if (HKDF(kctx->md, kctx->salt, kctx->salt_len, kctx->key, kctx->key_len,
|
||||
kctx->info, kctx->info_len, key, *keylen) == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const EVP_PKEY_METHOD hkdf_pkey_meth = {
|
||||
EVP_PKEY_HKDF,
|
||||
0,
|
||||
pkey_hkdf_init,
|
||||
0,
|
||||
pkey_hkdf_cleanup,
|
||||
|
||||
0, 0,
|
||||
0, 0,
|
||||
|
||||
0,
|
||||
0,
|
||||
|
||||
0,
|
||||
0,
|
||||
|
||||
0, 0,
|
||||
|
||||
0, 0, 0, 0,
|
||||
|
||||
0, 0,
|
||||
|
||||
0, 0,
|
||||
|
||||
0,
|
||||
pkey_hkdf_derive,
|
||||
pkey_hkdf_ctrl,
|
||||
pkey_hkdf_ctrl_str
|
||||
};
|
||||
|
||||
static unsigned char *HKDF(const EVP_MD *evp_md,
|
||||
const unsigned char *salt, size_t salt_len,
|
||||
const unsigned char *key, size_t key_len,
|
||||
const unsigned char *info, size_t info_len,
|
||||
unsigned char *okm, size_t okm_len)
|
||||
{
|
||||
unsigned char prk[EVP_MAX_MD_SIZE];
|
||||
size_t prk_len;
|
||||
|
||||
if (!HKDF_Extract(evp_md, salt, salt_len, key, key_len, prk, &prk_len))
|
||||
return NULL;
|
||||
|
||||
return HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len);
|
||||
}
|
||||
|
||||
static unsigned char *HKDF_Extract(const EVP_MD *evp_md,
|
||||
const unsigned char *salt, size_t salt_len,
|
||||
const unsigned char *key, size_t key_len,
|
||||
unsigned char *prk, size_t *prk_len)
|
||||
{
|
||||
unsigned int tmp_len;
|
||||
|
||||
if (!HMAC(evp_md, salt, salt_len, key, key_len, prk, &tmp_len))
|
||||
return NULL;
|
||||
|
||||
*prk_len = tmp_len;
|
||||
return prk;
|
||||
}
|
||||
|
||||
static unsigned char *HKDF_Expand(const EVP_MD *evp_md,
|
||||
const unsigned char *prk, size_t prk_len,
|
||||
const unsigned char *info, size_t info_len,
|
||||
unsigned char *okm, size_t okm_len)
|
||||
{
|
||||
HMAC_CTX *hmac;
|
||||
|
||||
unsigned int i;
|
||||
|
||||
unsigned char prev[EVP_MAX_MD_SIZE];
|
||||
|
||||
size_t done_len = 0, dig_len = EVP_MD_size(evp_md);
|
||||
|
||||
size_t n = okm_len / dig_len;
|
||||
if (okm_len % dig_len)
|
||||
n++;
|
||||
|
||||
if (n > 255)
|
||||
return NULL;
|
||||
|
||||
if ((hmac = HMAC_CTX_new()) == NULL)
|
||||
return NULL;
|
||||
|
||||
if (!HMAC_Init_ex(hmac, prk, prk_len, evp_md, NULL))
|
||||
goto err;
|
||||
|
||||
for (i = 1; i <= n; i++) {
|
||||
size_t copy_len;
|
||||
const unsigned char ctr = i;
|
||||
|
||||
if (i > 1) {
|
||||
if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL))
|
||||
goto err;
|
||||
|
||||
if (!HMAC_Update(hmac, prev, dig_len))
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!HMAC_Update(hmac, info, info_len))
|
||||
goto err;
|
||||
|
||||
if (!HMAC_Update(hmac, &ctr, 1))
|
||||
goto err;
|
||||
|
||||
if (!HMAC_Final(hmac, prev, NULL))
|
||||
goto err;
|
||||
|
||||
copy_len = (done_len + dig_len > okm_len) ?
|
||||
okm_len - done_len :
|
||||
dig_len;
|
||||
|
||||
memcpy(okm + done_len, prev, copy_len);
|
||||
|
||||
done_len += copy_len;
|
||||
}
|
||||
|
||||
HMAC_CTX_free(hmac);
|
||||
return okm;
|
||||
|
||||
err:
|
||||
HMAC_CTX_free(hmac);
|
||||
return NULL;
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
/* crypto/ecies/kdf.h */
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2007 - 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_KDF_H
|
||||
#define HEADER_KDF_H
|
||||
|
||||
#include <openssl/evp.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef void *(*KDF_FUNC)(const void *in, size_t inlen, void *out, size_t *outlen);
|
||||
|
||||
KDF_FUNC KDF_get_x9_63(const EVP_MD *md);
|
||||
KDF_FUNC KDF_get_nist_concatenation(void);
|
||||
KDF_FUNC KDF_get_tls_kdf(void);
|
||||
KDF_FUNC KDF_get_ikev2_kdf(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
46
crypto/kdf/kdf_err.c
Normal file
46
crypto/kdf/kdf_err.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Generated by util/mkerr.pl DO NOT EDIT
|
||||
* Copyright 1995-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
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/kdf.h>
|
||||
|
||||
/* BEGIN ERROR CODES */
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
|
||||
# define ERR_FUNC(func) ERR_PACK(ERR_LIB_KDF,func,0)
|
||||
# define ERR_REASON(reason) ERR_PACK(ERR_LIB_KDF,0,reason)
|
||||
|
||||
static ERR_STRING_DATA KDF_str_functs[] = {
|
||||
{ERR_FUNC(KDF_F_PKEY_TLS1_PRF_CTRL_STR), "pkey_tls1_prf_ctrl_str"},
|
||||
{ERR_FUNC(KDF_F_PKEY_TLS1_PRF_DERIVE), "pkey_tls1_prf_derive"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
static ERR_STRING_DATA KDF_str_reasons[] = {
|
||||
{ERR_REASON(KDF_R_INVALID_DIGEST), "invalid digest"},
|
||||
{ERR_REASON(KDF_R_MISSING_PARAMETER), "missing parameter"},
|
||||
{ERR_REASON(KDF_R_VALUE_MISSING), "value missing"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
int ERR_load_KDF_strings(void)
|
||||
{
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
|
||||
if (ERR_func_error_string(KDF_str_functs[0].error) == NULL) {
|
||||
ERR_load_strings(0, KDF_str_functs);
|
||||
ERR_load_strings(0, KDF_str_reasons);
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
@@ -1,184 +0,0 @@
|
||||
/* crypto/kdf/kdf_x9_63.c */
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2007 - 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 <string.h>
|
||||
#include <stdint.h>
|
||||
#include <openssl/kdf.h>
|
||||
|
||||
#ifdef CPU_BIGENDIAN
|
||||
#define cpu_to_be16(v) (v)
|
||||
#define cpu_to_be32(v) (v)
|
||||
#else
|
||||
#define cpu_to_be16(v) ((v << 8) | (v >> 8))
|
||||
#define cpu_to_be32(v) ((cpu_to_be16(v) << 16) | cpu_to_be16(v >> 16))
|
||||
#endif
|
||||
|
||||
static void *x963_kdf(const EVP_MD *md, const void *in, size_t inlen,
|
||||
void *out, size_t *outlen)
|
||||
{
|
||||
EVP_MD_CTX ctx;
|
||||
uint32_t counter = 1;
|
||||
uint32_t counter_be;
|
||||
unsigned char dgst[EVP_MAX_MD_SIZE];
|
||||
unsigned int dgstlen;
|
||||
unsigned char *pout = out;
|
||||
size_t rlen = *outlen;
|
||||
size_t len;
|
||||
|
||||
EVP_MD_CTX_init(&ctx);
|
||||
|
||||
while (rlen > 0) {
|
||||
counter_be = cpu_to_be32(counter);
|
||||
counter++;
|
||||
|
||||
EVP_DigestInit(&ctx, md);
|
||||
EVP_DigestUpdate(&ctx, in, inlen);
|
||||
EVP_DigestUpdate(&ctx, &counter_be, sizeof(counter_be));
|
||||
EVP_DigestFinal(&ctx, dgst, &dgstlen);
|
||||
|
||||
len = dgstlen <= rlen ? dgstlen : rlen;
|
||||
memcpy(pout, dgst, len);
|
||||
rlen -= len;
|
||||
pout += len;
|
||||
}
|
||||
|
||||
EVP_MD_CTX_cleanup(&ctx);
|
||||
return out;
|
||||
}
|
||||
|
||||
static void *x963_md5kdf(const void *in, size_t inlen,
|
||||
void *out, size_t *outlen)
|
||||
{
|
||||
return x963_kdf(EVP_md5(), in, inlen, out, outlen);
|
||||
}
|
||||
|
||||
static void *x963_rmd160kdf(const void *in, size_t inlen,
|
||||
void *out, size_t *outlen)
|
||||
{
|
||||
return x963_kdf(EVP_ripemd160(), in, inlen, out, outlen);
|
||||
}
|
||||
|
||||
static void *x963_sha1kdf(const void *in, size_t inlen,
|
||||
void *out, size_t *outlen)
|
||||
{
|
||||
return x963_kdf(EVP_sha1(), in, inlen, out, outlen);
|
||||
}
|
||||
|
||||
static void *x963_sha224kdf(const void *in, size_t inlen,
|
||||
void *out, size_t *outlen)
|
||||
{
|
||||
return x963_kdf(EVP_sha224(), in, inlen, out, outlen);
|
||||
}
|
||||
|
||||
static void *x963_sha256kdf(const void *in, size_t inlen,
|
||||
void *out, size_t *outlen)
|
||||
{
|
||||
return x963_kdf(EVP_sha256(), in, inlen, out, outlen);
|
||||
}
|
||||
|
||||
static void *x963_sha384kdf(const void *in, size_t inlen,
|
||||
void *out, size_t *outlen)
|
||||
{
|
||||
return x963_kdf(EVP_sha384(), in, inlen, out, outlen);
|
||||
}
|
||||
|
||||
static void *x963_sha512kdf(const void *in, size_t inlen,
|
||||
void *out, size_t *outlen)
|
||||
{
|
||||
return x963_kdf(EVP_sha512(), in, inlen, out, outlen);
|
||||
}
|
||||
|
||||
static void *x963_whirlpoolkdf(const void *in, size_t inlen,
|
||||
void *out, size_t *outlen)
|
||||
{
|
||||
return x963_kdf(EVP_whirlpool(), in, inlen, out, outlen);
|
||||
}
|
||||
|
||||
static void *x963_sm3kdf(const void *in, size_t inlen,
|
||||
void *out, size_t *outlen)
|
||||
{
|
||||
return x963_kdf(EVP_sm3(), in, inlen, out, outlen);
|
||||
}
|
||||
|
||||
KDF_FUNC KDF_get_x9_63(const EVP_MD *md)
|
||||
{
|
||||
if (md == EVP_md5()) {
|
||||
return x963_md5kdf;
|
||||
|
||||
} else if (md == EVP_ripemd160()) {
|
||||
return x963_rmd160kdf;
|
||||
|
||||
} else if (md == EVP_sha1()) {
|
||||
return x963_sha1kdf;
|
||||
|
||||
} else if (md == EVP_sha224()) {
|
||||
return x963_sha224kdf;
|
||||
|
||||
} else if (md == EVP_sha256()) {
|
||||
return x963_sha256kdf;
|
||||
|
||||
} else if (md == EVP_sha384()) {
|
||||
return x963_sha384kdf;
|
||||
|
||||
} else if (md == EVP_sha512()) {
|
||||
return x963_sha512kdf;
|
||||
|
||||
} else if (md == EVP_whirlpool()) {
|
||||
return x963_whirlpoolkdf;
|
||||
|
||||
} else if (md == EVP_sm3()) {
|
||||
return x963_sm3kdf;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <openssl/kdf.h>
|
||||
#include <openssl/rand.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
KDF_FUNC kdf = KDF_get_x9_63(EVP_sm3());
|
||||
unsigned char buf[1024];
|
||||
unsigned char key[128];
|
||||
size_t keylen = 12;
|
||||
int i;
|
||||
|
||||
memset(buf, 0x32, sizeof(buf));
|
||||
kdf(buf, sizeof(buf), key, &keylen);
|
||||
|
||||
for (i = 0; i < keylen; i++) {
|
||||
printf("%02x", key[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
265
crypto/kdf/tls1_prf.c
Normal file
265
crypto/kdf/tls1_prf.c
Normal file
@@ -0,0 +1,265 @@
|
||||
/*
|
||||
* Copyright 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
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include <openssl/kdf.h>
|
||||
#include <openssl/evp.h>
|
||||
#include "internal/evp_int.h"
|
||||
|
||||
static int tls1_prf_alg(const EVP_MD *md,
|
||||
const unsigned char *sec, size_t slen,
|
||||
const unsigned char *seed, size_t seed_len,
|
||||
unsigned char *out, size_t olen);
|
||||
|
||||
#define TLS1_PRF_MAXBUF 1024
|
||||
|
||||
/* TLS KDF pkey context structure */
|
||||
|
||||
typedef struct {
|
||||
/* Digest to use for PRF */
|
||||
const EVP_MD *md;
|
||||
/* Secret value to use for PRF */
|
||||
unsigned char *sec;
|
||||
size_t seclen;
|
||||
/* Buffer of concatenated seed data */
|
||||
unsigned char seed[TLS1_PRF_MAXBUF];
|
||||
size_t seedlen;
|
||||
} TLS1_PRF_PKEY_CTX;
|
||||
|
||||
static int pkey_tls1_prf_init(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
TLS1_PRF_PKEY_CTX *kctx;
|
||||
|
||||
kctx = OPENSSL_zalloc(sizeof(*kctx));
|
||||
if (kctx == NULL)
|
||||
return 0;
|
||||
ctx->data = kctx;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void pkey_tls1_prf_cleanup(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
TLS1_PRF_PKEY_CTX *kctx = ctx->data;
|
||||
OPENSSL_clear_free(kctx->sec, kctx->seclen);
|
||||
OPENSSL_cleanse(kctx->seed, kctx->seedlen);
|
||||
OPENSSL_free(kctx);
|
||||
}
|
||||
|
||||
static int pkey_tls1_prf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
{
|
||||
TLS1_PRF_PKEY_CTX *kctx = ctx->data;
|
||||
switch (type) {
|
||||
case EVP_PKEY_CTRL_TLS_MD:
|
||||
kctx->md = p2;
|
||||
return 1;
|
||||
|
||||
case EVP_PKEY_CTRL_TLS_SECRET:
|
||||
if (p1 < 0)
|
||||
return 0;
|
||||
if (kctx->sec != NULL)
|
||||
OPENSSL_clear_free(kctx->sec, kctx->seclen);
|
||||
OPENSSL_cleanse(kctx->seed, kctx->seedlen);
|
||||
kctx->seedlen = 0;
|
||||
kctx->sec = OPENSSL_memdup(p2, p1);
|
||||
if (kctx->sec == NULL)
|
||||
return 0;
|
||||
kctx->seclen = p1;
|
||||
return 1;
|
||||
|
||||
case EVP_PKEY_CTRL_TLS_SEED:
|
||||
if (p1 == 0 || p2 == NULL)
|
||||
return 1;
|
||||
if (p1 < 0 || p1 > (int)(TLS1_PRF_MAXBUF - kctx->seedlen))
|
||||
return 0;
|
||||
memcpy(kctx->seed + kctx->seedlen, p2, p1);
|
||||
kctx->seedlen += p1;
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return -2;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int pkey_tls1_prf_ctrl_str(EVP_PKEY_CTX *ctx,
|
||||
const char *type, const char *value)
|
||||
{
|
||||
if (value == NULL) {
|
||||
KDFerr(KDF_F_PKEY_TLS1_PRF_CTRL_STR, KDF_R_VALUE_MISSING);
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(type, "md") == 0) {
|
||||
TLS1_PRF_PKEY_CTX *kctx = ctx->data;
|
||||
|
||||
const EVP_MD *md = EVP_get_digestbyname(value);
|
||||
if (md == NULL) {
|
||||
KDFerr(KDF_F_PKEY_TLS1_PRF_CTRL_STR, KDF_R_INVALID_DIGEST);
|
||||
return 0;
|
||||
}
|
||||
kctx->md = md;
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(type, "secret") == 0)
|
||||
return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_TLS_SECRET, value);
|
||||
if (strcmp(type, "hexsecret") == 0)
|
||||
return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_TLS_SECRET, value);
|
||||
if (strcmp(type, "seed") == 0)
|
||||
return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_TLS_SEED, value);
|
||||
if (strcmp(type, "hexseed") == 0)
|
||||
return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_TLS_SEED, value);
|
||||
return -2;
|
||||
}
|
||||
|
||||
static int pkey_tls1_prf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
|
||||
size_t *keylen)
|
||||
{
|
||||
TLS1_PRF_PKEY_CTX *kctx = ctx->data;
|
||||
if (kctx->md == NULL || kctx->sec == NULL || kctx->seedlen == 0) {
|
||||
KDFerr(KDF_F_PKEY_TLS1_PRF_DERIVE, KDF_R_MISSING_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
return tls1_prf_alg(kctx->md, kctx->sec, kctx->seclen,
|
||||
kctx->seed, kctx->seedlen,
|
||||
key, *keylen);
|
||||
}
|
||||
|
||||
const EVP_PKEY_METHOD tls1_prf_pkey_meth = {
|
||||
EVP_PKEY_TLS1_PRF,
|
||||
0,
|
||||
pkey_tls1_prf_init,
|
||||
0,
|
||||
pkey_tls1_prf_cleanup,
|
||||
|
||||
0, 0,
|
||||
0, 0,
|
||||
|
||||
0,
|
||||
0,
|
||||
|
||||
0,
|
||||
0,
|
||||
|
||||
0, 0,
|
||||
|
||||
0, 0, 0, 0,
|
||||
|
||||
0, 0,
|
||||
|
||||
0, 0,
|
||||
|
||||
0,
|
||||
pkey_tls1_prf_derive,
|
||||
pkey_tls1_prf_ctrl,
|
||||
pkey_tls1_prf_ctrl_str
|
||||
};
|
||||
|
||||
static int tls1_prf_P_hash(const EVP_MD *md,
|
||||
const unsigned char *sec, size_t sec_len,
|
||||
const unsigned char *seed, size_t seed_len,
|
||||
unsigned char *out, size_t olen)
|
||||
{
|
||||
int chunk;
|
||||
EVP_MD_CTX *ctx = NULL, *ctx_tmp = NULL, *ctx_init = NULL;
|
||||
EVP_PKEY *mac_key = NULL;
|
||||
unsigned char A1[EVP_MAX_MD_SIZE];
|
||||
size_t A1_len;
|
||||
int ret = 0;
|
||||
|
||||
chunk = EVP_MD_size(md);
|
||||
OPENSSL_assert(chunk >= 0);
|
||||
|
||||
ctx = EVP_MD_CTX_new();
|
||||
ctx_tmp = EVP_MD_CTX_new();
|
||||
ctx_init = EVP_MD_CTX_new();
|
||||
if (ctx == NULL || ctx_tmp == NULL || ctx_init == NULL)
|
||||
goto err;
|
||||
EVP_MD_CTX_set_flags(ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
|
||||
mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len);
|
||||
if (mac_key == NULL)
|
||||
goto err;
|
||||
if (!EVP_DigestSignInit(ctx_init, NULL, md, NULL, mac_key))
|
||||
goto err;
|
||||
if (!EVP_MD_CTX_copy_ex(ctx, ctx_init))
|
||||
goto err;
|
||||
if (seed != NULL && !EVP_DigestSignUpdate(ctx, seed, seed_len))
|
||||
goto err;
|
||||
if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
|
||||
goto err;
|
||||
|
||||
for (;;) {
|
||||
/* Reinit mac contexts */
|
||||
if (!EVP_MD_CTX_copy_ex(ctx, ctx_init))
|
||||
goto err;
|
||||
if (!EVP_DigestSignUpdate(ctx, A1, A1_len))
|
||||
goto err;
|
||||
if (olen > (size_t)chunk && !EVP_MD_CTX_copy_ex(ctx_tmp, ctx))
|
||||
goto err;
|
||||
if (seed && !EVP_DigestSignUpdate(ctx, seed, seed_len))
|
||||
goto err;
|
||||
|
||||
if (olen > (size_t)chunk) {
|
||||
size_t mac_len;
|
||||
if (!EVP_DigestSignFinal(ctx, out, &mac_len))
|
||||
goto err;
|
||||
out += mac_len;
|
||||
olen -= mac_len;
|
||||
/* calc the next A1 value */
|
||||
if (!EVP_DigestSignFinal(ctx_tmp, A1, &A1_len))
|
||||
goto err;
|
||||
} else { /* last one */
|
||||
|
||||
if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
|
||||
goto err;
|
||||
memcpy(out, A1, olen);
|
||||
break;
|
||||
}
|
||||
}
|
||||
ret = 1;
|
||||
err:
|
||||
EVP_PKEY_free(mac_key);
|
||||
EVP_MD_CTX_free(ctx);
|
||||
EVP_MD_CTX_free(ctx_tmp);
|
||||
EVP_MD_CTX_free(ctx_init);
|
||||
OPENSSL_cleanse(A1, sizeof(A1));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int tls1_prf_alg(const EVP_MD *md,
|
||||
const unsigned char *sec, size_t slen,
|
||||
const unsigned char *seed, size_t seed_len,
|
||||
unsigned char *out, size_t olen)
|
||||
{
|
||||
|
||||
if (EVP_MD_type(md) == NID_md5_sha1) {
|
||||
size_t i;
|
||||
unsigned char *tmp;
|
||||
if (!tls1_prf_P_hash(EVP_md5(), sec, slen/2 + (slen & 1),
|
||||
seed, seed_len, out, olen))
|
||||
return 0;
|
||||
|
||||
tmp = OPENSSL_malloc(olen);
|
||||
if (tmp == NULL)
|
||||
return 0;
|
||||
if (!tls1_prf_P_hash(EVP_sha1(), sec + slen/2, slen/2 + (slen & 1),
|
||||
seed, seed_len, tmp, olen)) {
|
||||
OPENSSL_clear_free(tmp, olen);
|
||||
return 0;
|
||||
}
|
||||
for (i = 0; i < olen; i++)
|
||||
out[i] ^= tmp[i];
|
||||
OPENSSL_clear_free(tmp, olen);
|
||||
return 1;
|
||||
}
|
||||
if (!tls1_prf_P_hash(md, sec, slen, seed, seed_len, out, olen))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user