add CBC-MAC and GM OTP, not tested

This commit is contained in:
Zhi Guan
2016-05-15 20:21:51 +02:00
parent 8c0439e7d6
commit 60d14da0cc
81 changed files with 2789 additions and 1401 deletions

View File

@@ -163,7 +163,7 @@ void OpenSSL_add_all_ciphers(void)
EVP_add_cipher_alias(SN_rc5_cbc, "RC5");
#endif
#ifndef OPENSSL_NO_SMS4
#ifndef OPENSSL_NO_GMSSL
EVP_add_cipher(EVP_sms4_ecb());
EVP_add_cipher(EVP_sms4_cbc());
EVP_add_cipher(EVP_sms4_cfb());
@@ -174,9 +174,6 @@ void OpenSSL_add_all_ciphers(void)
EVP_add_cipher(EVP_sms4_wrap());
EVP_add_cipher_alias(SN_sms4_cbc,"SMS4");
EVP_add_cipher_alias(SN_sms4_cbc,"sms4");
#endif
#ifndef OPENSSL_NO_ZUC
EVP_add_cipher(EVP_zuc());
#endif

View File

@@ -67,7 +67,7 @@ void OpenSSL_add_all_digests(void)
#ifndef OPENSSL_NO_MD4
EVP_add_digest(EVP_md4());
#endif
#ifndef OPENSSL_NO_SM3
#ifndef OPENSSL_NO_GMSSL
EVP_add_digest(EVP_sm3());
#endif
#ifndef OPENSSL_NO_MD5

View File

@@ -53,7 +53,7 @@
#include <stdio.h>
#include "cryptlib.h"
#ifndef OPENSSL_NO_SMS4
#ifndef OPENSSL_NO_GMSSL
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/crypto.h>

View File

@@ -1,7 +1,7 @@
#include <stdio.h>
#include "cryptlib.h"
#ifndef OPENSSL_NO_ZUC
#ifndef OPENSSL_NO_GMSSL
#include <openssl/evp.h>
#include "evp_locl.h"

View File

@@ -117,10 +117,7 @@
# define EVP_PKEY_EC NID_X9_62_id_ecPublicKey
# define EVP_PKEY_HMAC NID_hmac
# define EVP_PKEY_CMAC NID_cmac
# ifndef OPENSSL_NO_SM2
# define EVP_PKEY_SM2 NID_sm2p256v1 /* FIXME: NID_sm2 */
# endif
# define EVP_PKEY_CBCMAC NID_cbc_mac
#ifdef __cplusplus
extern "C" {
@@ -254,14 +251,6 @@ typedef int evp_verify_method(int type, const unsigned char *m,
# define EVP_PKEY_ECDSA_method EVP_PKEY_NULL_method
# endif
# ifndef OPENSSL_NO_SM2
# define EVP_PKEY_SM2_method (evp_sign_method *)SM2_sign, \
(evp_verify_method *)SM2_verify, \
{EVP_PKEY_SM2,0,0,0}
# else
# define EVP_PKEY_SM2_method EVP_PKEY_NULL_method
# endif
# ifndef OPENSSL_NO_RSA
# define EVP_PKEY_RSA_method (evp_sign_method *)RSA_sign, \
(evp_verify_method *)RSA_verify, \
@@ -519,10 +508,6 @@ typedef int (EVP_PBE_KEYGEN) (EVP_CIPHER_CTX *ctx, const char *pass,
(char *)(eckey))
# endif
# ifndef OPENSSL_NO_SM2
# define EVP_PKEY_assign_SM2(pkey,eckey) EVP_PKEY_assign((pkey),EVP_PKEY_SM2,\
(char *)(eckey))
# endif
/* Add some extra combinations */
# define EVP_get_digestbynid(a) EVP_get_digestbyname(OBJ_nid2sn(a))
@@ -756,7 +741,7 @@ const EVP_MD *EVP_ripemd160(void);
# ifndef OPENSSL_NO_WHIRLPOOL
const EVP_MD *EVP_whirlpool(void);
# endif
# ifndef OPENSSL_NO_SM3
# ifndef OPENSSL_NO_GMSSL
const EVP_MD *EVP_sm3(void);
# endif
const EVP_CIPHER *EVP_enc_null(void); /* does nothing :-) */
@@ -845,7 +830,7 @@ const EVP_CIPHER *EVP_rc5_32_12_16_cfb64(void);
# define EVP_rc5_32_12_16_cfb EVP_rc5_32_12_16_cfb64
const EVP_CIPHER *EVP_rc5_32_12_16_ofb(void);
# endif
#ifndef OPENSSL_NO_SMS4
#ifndef OPENSSL_NO_GMSSL
const EVP_CIPHER *EVP_sms4_ecb(void);
const EVP_CIPHER *EVP_sms4_cbc(void);
const EVP_CIPHER *EVP_sms4_cfb1(void);
@@ -862,8 +847,6 @@ const EVP_CIPHER *EVP_sms4_wrap(void);
#define EVP_sm4_cbc EVP_sms4_cbc
#define EVP_sm4_cfb EVP_sms4_cfb
#define EVP_sm4_ofb EVP_sms4_ofb
#endif
#ifndef OPENSSL_NO_ZUC
const EVP_CIPHER *EVP_zuc(void);
#endif
# ifndef OPENSSL_NO_AES
@@ -1016,10 +999,6 @@ struct dh_st *EVP_PKEY_get1_DH(EVP_PKEY *pkey);
struct ec_key_st;
int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, struct ec_key_st *key);
struct ec_key_st *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey);
# ifndef OPENSSL_NO_SM2
int EVP_PKEY_set1_SM2(EVP_PKEY *pkey, struct ec_key_st *key);
struct ec_key_st *EVP_PKEY_get1_SM2(EVP_PKEY *pkey);
# endif
# endif
EVP_PKEY *EVP_PKEY_new(void);
@@ -1238,6 +1217,8 @@ void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
# define EVP_PKEY_CTRL_GET_MD 13
# define EVP_PKEY_ALG_CTRL 0x1000
# define EVP_PKEY_FLAG_AUTOARGLEN 2

View File

@@ -228,14 +228,3 @@ int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
return 0;
}
#ifndef OPENSSL_NO_GMSSL
int EVP_PKEY_ec_to_sm2(EVP_PKEY *pkey, int only_sm2_curve)
{
return 0;
}
int EVP_PKEY_sm2_to_ec(EVP_PKEY *pkey)
{
return 0;
}
#endif

View File

@@ -52,7 +52,7 @@
#include <stdio.h>
#include "cryptlib.h"
#ifndef OPENSSL_NO_SM3
#ifndef OPENSSL_NO_GMSSL
#include <openssl/evp.h>
#include <openssl/objects.h>
@@ -76,18 +76,20 @@ static int final(EVP_MD_CTX *ctx, unsigned char *md)
}
static const EVP_MD sm3_md = {
NID_sm3,
NID_sm2sign_with_sm3,
SM3_DIGEST_LENGTH,
0,
init,
update,
final,
NULL,
NULL,
EVP_PKEY_SM2_method,
SM3_BLOCK_SIZE,
sizeof(EVP_MD *) + sizeof(sm3_ctx_t),
NID_sm3,
NID_sm2sign_with_sm3,
SM3_DIGEST_LENGTH,
0,
init,
update,
final,
NULL,
NULL,
(evp_sign_method *)SM2_sign,
(evp_verify_method *)SM2_verify,
{EVP_PKEY_EC, 0, 0, 0},
SM3_BLOCK_SIZE,
sizeof(EVP_MD *) + sizeof(sm3_ctx_t),
};
const EVP_MD *EVP_sm3(void)

View File

@@ -66,49 +66,29 @@
#include <openssl/objects.h>
#include <openssl/x509.h>
static int ossl_EVP_PKEY_encrypt_old(unsigned char *ek, const unsigned char *key,
int key_len, EVP_PKEY *pubk)
{
int ret = 0;
if (pubk->type != EVP_PKEY_RSA) {
EVPerr(EVP_F_EVP_PKEY_ENCRYPT_OLD, EVP_R_PUBLIC_KEY_NOT_RSA);
goto err;
}
ret =
RSA_public_encrypt(key_len, key, ek, pubk->pkey.rsa,
RSA_PKCS1_PADDING);
err:
return (ret);
}
// OPENSSL_NO_GMSSL
// here is to mark changes
/* GMSSL: EVP_PKEY_encrypt_old() is modified */
int EVP_PKEY_encrypt_old(unsigned char *out, const unsigned char *in,
int inlen, EVP_PKEY *pkey)
{
int ret = 0;
EVP_PKEY_CTX *ctx = NULL;
size_t outlen;
size_t size;
if (pkey->type == EVP_PKEY_RSA) {
return ossl_EVP_PKEY_encrypt_old(out, in, inlen, pkey);
ret = RSA_public_encrypt(inlen, in, out, pkey->pkey.rsa,
RSA_PKCS1_PADDING);
} else {
if (!(ctx = EVP_PKEY_CTX_new(pkey, NULL))) {
return 0;
}
if (1 != EVP_PKEY_encrypt_init(ctx)) {
return 0;
}
if (1 != EVP_PKEY_encrypt(ctx, out, &size, in, inlen)) {
goto end;
}
ret = (int)size;
}
if (!(ctx = EVP_PKEY_CTX_new(pkey, NULL))) {
return 0;
}
if (EVP_PKEY_encrypt_init(ctx) <= 0) {
goto end;
}
/* ctrl operations can be added here */
if (EVP_PKEY_encrypt(ctx, out, &outlen, in, inlen) <= 0) {
goto end;
}
ret = (int)outlen;
end:
EVP_PKEY_CTX_free(ctx);
return ret;

View File

@@ -80,9 +80,6 @@
#include "asn1_locl.h"
#ifndef OPENSSL_NO_GMSSL
#include "../ec/ec_lcl.h"
#endif
static void EVP_PKEY_free_it(EVP_PKEY *x);
@@ -120,15 +117,6 @@ int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
return (ret);
}
#endif
#ifndef OPENSSL_NO_SM2
if (pkey->type == EVP_PKEY_SM2) {
int ret = pkey->save_parameters;
if (mode >= 0)
pkey->save_parameters = mode;
return (ret);
}
#endif
return (0);
}
@@ -338,27 +326,6 @@ EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
}
#endif
#ifndef OPENSSL_NO_SM2
int EVP_PKEY_set1_SM2(EVP_PKEY *pkey, EC_KEY *key)
{
int ret = EVP_PKEY_assign_SM2(pkey, key);
if (ret)
EC_KEY_up_ref(key);
return ret;
}
EC_KEY *EVP_PKEY_get1_SM2(EVP_PKEY *pkey)
{
/* FIXME: reconsider the SM2 and EC_KEY relationship */
if (pkey->type != EVP_PKEY_SM2) {
EVPerr(EVP_F_EVP_PKEY_GET1_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);//FIXME:errno
return NULL;
}
EC_KEY_up_ref(pkey->pkey.ec);
return pkey->pkey.ec;
}
#endif
#ifndef OPENSSL_NO_DH
int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)

View File

@@ -75,7 +75,7 @@ STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth;
extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth, cmac_pkey_meth;
extern const EVP_PKEY_METHOD dhx_pkey_meth, sm2_pkey_meth;
extern const EVP_PKEY_METHOD dhx_pkey_meth, cbcmac_pkey_meth;
static const EVP_PKEY_METHOD *standard_methods[] = {
#ifndef OPENSSL_NO_RSA
@@ -89,14 +89,14 @@ static const EVP_PKEY_METHOD *standard_methods[] = {
#endif
#ifndef OPENSSL_NO_EC
&ec_pkey_meth,
#endif
#ifndef OPENSSL_NO_SM2
&sm2_pkey_meth,
#endif
&hmac_pkey_meth,
&cmac_pkey_meth,
#ifndef OPENSSL_NO_DH
&dhx_pkey_meth
&dhx_pkey_meth,
#endif
#ifndef OPENSSL_NO_GMSSL
&cbcmac_pkey_meth,
#endif
};
@@ -119,28 +119,14 @@ const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
tmp.pkey_id = type;
if (app_pkey_methods) {
int idx;
//fprintf(stderr, "check %s %d\n", __FILE__, __LINE__);
idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
if (idx >= 0)
return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
//fprintf(stderr, "check %s %d\n", __FILE__, __LINE__);
}
//fprintf(stderr, "%s %d: t->pkey_id = %d\n", __FILE__, __LINE__, t->pkey_id);
int i;
for (i = 0; i < sizeof(standard_methods) / sizeof(EVP_PKEY_METHOD *); i++) {
if (type == standard_methods[i]->pkey_id) {
return standard_methods[i];
}
}
/*
ret = OBJ_bsearch_pmeth(&t, standard_methods,
sizeof(standard_methods) /
sizeof(EVP_PKEY_METHOD *));
*/
if (!ret || !*ret) {
//fprintf(stderr, "check %s %d\n", __FILE__, __LINE__);
return NULL;
}
return *ret;
@@ -152,7 +138,6 @@ static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
const EVP_PKEY_METHOD *pmeth;
if (id == -1) {
if (!pkey || !pkey->ameth) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
return NULL;
}
id = pkey->ameth->pkey_id;