mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-24 22:24:03 +08:00
add sms4 module
This commit is contained in:
@@ -13,7 +13,8 @@ SOURCE[../../libcrypto]=\
|
||||
e_old.c pmeth_lib.c pmeth_fn.c pmeth_gn.c m_sigver.c \
|
||||
e_aes_cbc_hmac_sha1.c e_aes_cbc_hmac_sha256.c e_rc4_hmac_md5.c \
|
||||
e_chacha20_poly1305.c cmeth_lib.c \
|
||||
m_sm3.c
|
||||
m_sm3.c \
|
||||
e_sms4.c e_sms4_ccm.c e_sms4_gcm.c e_sms4_ocb.c e_sms4_wrap.c e_sms4_xts.c
|
||||
|
||||
INCLUDE[e_aes.o]=.. ../modes
|
||||
INCLUDE[e_aes_cbc_hmac_sha1.o]=../modes
|
||||
|
||||
@@ -216,5 +216,26 @@ void openssl_add_all_ciphers_int(void)
|
||||
# ifndef OPENSSL_NO_POLY1305
|
||||
EVP_add_cipher(EVP_chacha20_poly1305());
|
||||
# endif
|
||||
|
||||
#ifndef OPENSSL_NO_SMS4
|
||||
EVP_add_cipher(EVP_sms4_ecb());
|
||||
EVP_add_cipher(EVP_sms4_cbc());
|
||||
EVP_add_cipher(EVP_sms4_cfb());
|
||||
EVP_add_cipher(EVP_sms4_cfb1());
|
||||
EVP_add_cipher(EVP_sms4_cfb8());
|
||||
EVP_add_cipher(EVP_sms4_ofb());
|
||||
EVP_add_cipher(EVP_sms4_ctr());
|
||||
EVP_add_cipher(EVP_sms4_ccm());
|
||||
EVP_add_cipher(EVP_sms4_gcm());
|
||||
# ifndef OPENSSL_NO_OCB
|
||||
EVP_add_cipher(EVP_sms4_ocb());
|
||||
# endif
|
||||
EVP_add_cipher(EVP_sms4_xts());
|
||||
EVP_add_cipher(EVP_sms4_wrap());
|
||||
EVP_add_cipher(EVP_sms4_wrap_pad());
|
||||
EVP_add_cipher_alias(SN_sms4_cbc,"SMS4");
|
||||
EVP_add_cipher_alias(SN_sms4_cbc,"sms4");
|
||||
#endif
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
200
crypto/evp/e_sms4.c
Normal file
200
crypto/evp/e_sms4.c
Normal file
@@ -0,0 +1,200 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2017 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-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/evp.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
#include "../modes/modes_lcl.h"
|
||||
|
||||
#ifndef OPENSSL_NO_SMS4
|
||||
|
||||
# include <openssl/sms4.h>
|
||||
|
||||
typedef struct {
|
||||
sms4_key_t ks;
|
||||
} EVP_SMS4_KEY;
|
||||
|
||||
static int sms4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
if (!enc) {
|
||||
if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE)
|
||||
enc = 1;
|
||||
else if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE)
|
||||
enc = 1; //encrypt key == decrypt key
|
||||
}
|
||||
|
||||
if (enc)
|
||||
sms4_set_encrypt_key(ctx->cipher_data, key);
|
||||
else sms4_set_decrypt_key(ctx->cipher_data, key);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
IMPLEMENT_BLOCK_CIPHER(sms4, ks, sms4, EVP_SMS4_KEY, NID_sms4,
|
||||
SMS4_BLOCK_SIZE, SMS4_KEY_LENGTH, SMS4_IV_LENGTH, 128, 0,
|
||||
sms4_init_key, NULL, NULL, NULL, NULL)
|
||||
|
||||
# define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
|
||||
|
||||
static int sms4_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_SMS4_KEY *sms4_key = (EVP_SMS4_KEY *)ctx->cipher_data;
|
||||
|
||||
if (ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) {
|
||||
CRYPTO_cfb128_1_encrypt(in, out, len, &sms4_key->ks,
|
||||
ctx->iv, &ctx->num, ctx->encrypt, (block128_f)sms4_encrypt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (len >= MAXBITCHUNK) {
|
||||
CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, &sms4_key->ks,
|
||||
ctx->iv, &ctx->num, ctx->encrypt, (block128_f)sms4_encrypt);
|
||||
len -= MAXBITCHUNK;
|
||||
}
|
||||
|
||||
if (len) {
|
||||
CRYPTO_cfb128_1_encrypt(in, out, len * 8, &sms4_key->ks,
|
||||
ctx->iv, &ctx->num, ctx->encrypt, (block128_f)sms4_encrypt);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const EVP_CIPHER sms4_cfb1 = {
|
||||
NID_sms4_cfb1,
|
||||
SMS4_BLOCK_SIZE,
|
||||
SMS4_KEY_LENGTH,
|
||||
SMS4_IV_LENGTH,
|
||||
EVP_CIPH_CTR_MODE,
|
||||
sms4_init_key,
|
||||
sms4_cfb1_cipher,
|
||||
NULL,
|
||||
sizeof(EVP_SMS4_KEY),
|
||||
NULL,NULL,NULL,NULL,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_sms4_cfb1(void)
|
||||
{
|
||||
return &sms4_cfb1;
|
||||
}
|
||||
|
||||
static int sms4_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_SMS4_KEY *sms4_key = (EVP_SMS4_KEY *)ctx->cipher_data;
|
||||
|
||||
CRYPTO_cfb128_8_encrypt(in, out, len, &sms4_key->ks,
|
||||
ctx->iv, &ctx->num, ctx->encrypt, (block128_f)sms4_encrypt);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const EVP_CIPHER sms4_cfb8 = {
|
||||
NID_sms4_cfb8,
|
||||
SMS4_BLOCK_SIZE,
|
||||
SMS4_KEY_LENGTH,
|
||||
SMS4_IV_LENGTH,
|
||||
EVP_CIPH_CTR_MODE,
|
||||
sms4_init_key,
|
||||
sms4_cfb8_cipher,
|
||||
NULL,
|
||||
sizeof(EVP_SMS4_KEY),
|
||||
NULL,NULL,NULL,NULL,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_sms4_cfb8(void)
|
||||
{
|
||||
return &sms4_cfb8;
|
||||
}
|
||||
|
||||
static int sms4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
unsigned int num = ctx->num;
|
||||
EVP_SMS4_KEY *sms4 = (EVP_SMS4_KEY *)ctx->cipher_data;
|
||||
|
||||
CRYPTO_ctr128_encrypt_ctr32(in, out, len, &sms4->ks, ctx->iv, ctx->buf,
|
||||
&num, (ctr128_f)sms4_encrypt);
|
||||
|
||||
ctx->num = (size_t)num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const EVP_CIPHER sms4_ctr = {
|
||||
NID_sms4_ctr,
|
||||
SMS4_BLOCK_SIZE,
|
||||
SMS4_KEY_LENGTH,
|
||||
SMS4_IV_LENGTH,
|
||||
EVP_CIPH_CTR_MODE,
|
||||
sms4_init_key,
|
||||
sms4_ctr_cipher,
|
||||
NULL, /* cleanup() */
|
||||
sizeof(EVP_SMS4_KEY),
|
||||
NULL,NULL,NULL,NULL,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_sms4_ctr(void)
|
||||
{
|
||||
return &sms4_ctr;
|
||||
}
|
||||
#endif /* OPENSSL_NO_SMS4 */
|
||||
340
crypto/evp/e_sms4_ccm.c
Normal file
340
crypto/evp/e_sms4_ccm.c
Normal file
@@ -0,0 +1,340 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2017 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-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/evp.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
#include "../modes/modes_lcl.h"
|
||||
|
||||
#ifndef OPENSSL_NO_SMS4
|
||||
|
||||
#include <openssl/sms4.h>
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
double align;
|
||||
sms4_key_t ks;
|
||||
} ks; /* SMS4 key schedule to use */
|
||||
int key_set; /* Set if key initialised */
|
||||
int iv_set; /* Set if an iv is set */
|
||||
int tag_set; /* Set if tag is valid */
|
||||
int len_set; /* Set if message length set */
|
||||
int L, M; /* L and M parameters from RFC3610 */
|
||||
int tls_aad_len; /* TLS AAD length */
|
||||
CCM128_CONTEXT ccm;
|
||||
ccm128_f str;
|
||||
} EVP_SMS4_CCM_CTX;
|
||||
|
||||
static int sms4_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
|
||||
{
|
||||
EVP_SMS4_CCM_CTX *cctx = EVP_C_DATA(EVP_SMS4_CCM_CTX,c);
|
||||
switch (type) {
|
||||
case EVP_CTRL_INIT:
|
||||
cctx->key_set = 0;
|
||||
cctx->iv_set = 0;
|
||||
cctx->L = 8;
|
||||
cctx->M = 12;
|
||||
cctx->tag_set = 0;
|
||||
cctx->len_set = 0;
|
||||
cctx->tls_aad_len = -1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_TLS1_AAD:
|
||||
/* Save the AAD for later use */
|
||||
if (arg != EVP_AEAD_TLS1_AAD_LEN)
|
||||
return 0;
|
||||
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
|
||||
cctx->tls_aad_len = arg;
|
||||
{
|
||||
uint16_t len =
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
|
||||
| EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
|
||||
/* Correct length for explicit IV */
|
||||
len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
/* If decrypting correct for tag too */
|
||||
if (!EVP_CIPHER_CTX_encrypting(c))
|
||||
len -= cctx->M;
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
|
||||
}
|
||||
/* Extra padding: tag appended to record */
|
||||
return cctx->M;
|
||||
|
||||
case EVP_CTRL_CCM_SET_IV_FIXED:
|
||||
/* Sanity check length */
|
||||
if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
|
||||
return 0;
|
||||
/* Just copy to first part of IV */
|
||||
memcpy(EVP_CIPHER_CTX_iv_noconst(c), ptr, arg);
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_IVLEN:
|
||||
arg = 15 - arg;
|
||||
case EVP_CTRL_CCM_SET_L:
|
||||
if (arg < 2 || arg > 8)
|
||||
return 0;
|
||||
cctx->L = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_TAG:
|
||||
if ((arg & 1) || arg < 4 || arg > 16)
|
||||
return 0;
|
||||
if (EVP_CIPHER_CTX_encrypting(c) && ptr)
|
||||
return 0;
|
||||
if (ptr) {
|
||||
cctx->tag_set = 1;
|
||||
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
|
||||
}
|
||||
cctx->M = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_GET_TAG:
|
||||
if (!EVP_CIPHER_CTX_encrypting(c) || !cctx->tag_set)
|
||||
return 0;
|
||||
if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
|
||||
return 0;
|
||||
cctx->tag_set = 0;
|
||||
cctx->iv_set = 0;
|
||||
cctx->len_set = 0;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_COPY:
|
||||
{
|
||||
EVP_CIPHER_CTX *out = ptr;
|
||||
EVP_SMS4_CCM_CTX *cctx_out = EVP_C_DATA(EVP_SMS4_CCM_CTX,out);
|
||||
if (cctx->ccm.key) {
|
||||
if (cctx->ccm.key != &cctx->ks)
|
||||
return 0;
|
||||
cctx_out->ccm.key = &cctx_out->ks;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
default:
|
||||
return -1;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int sms4_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
EVP_SMS4_CCM_CTX *cctx = EVP_C_DATA(EVP_SMS4_CCM_CTX,ctx);
|
||||
if (!iv && !key)
|
||||
return 1;
|
||||
if (key)
|
||||
do {
|
||||
sms4_set_encrypt_key(&cctx->ks.ks, key);
|
||||
CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
|
||||
&cctx->ks, (block128_f) sms4_encrypt);
|
||||
cctx->str = NULL;
|
||||
cctx->key_set = 1;
|
||||
} while (0);
|
||||
if (iv) {
|
||||
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L);
|
||||
cctx->iv_set = 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int sms4_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_SMS4_CCM_CTX *cctx = EVP_C_DATA(EVP_SMS4_CCM_CTX,ctx);
|
||||
CCM128_CONTEXT *ccm = &cctx->ccm;
|
||||
/* Encrypt/decrypt must be performed in place */
|
||||
if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M))
|
||||
return -1;
|
||||
/* If encrypting set explicit IV from sequence number (start of AAD) */
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx))
|
||||
memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx),
|
||||
EVP_CCM_TLS_EXPLICIT_IV_LEN);
|
||||
/* Get rest of IV from explicit IV */
|
||||
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx) + EVP_CCM_TLS_FIXED_IV_LEN, in,
|
||||
EVP_CCM_TLS_EXPLICIT_IV_LEN);
|
||||
/* Correct length value */
|
||||
len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
|
||||
if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx), 15 - cctx->L,
|
||||
len))
|
||||
return -1;
|
||||
/* Use saved AAD */
|
||||
CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx), cctx->tls_aad_len);
|
||||
/* Fix buffer to point to payload */
|
||||
in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
|
||||
cctx->str) :
|
||||
CRYPTO_ccm128_encrypt(ccm, in, out, len))
|
||||
return -1;
|
||||
if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M))
|
||||
return -1;
|
||||
return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
|
||||
} else {
|
||||
if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
|
||||
cctx->str) :
|
||||
!CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
|
||||
unsigned char tag[16];
|
||||
if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
|
||||
if (!CRYPTO_memcmp(tag, in + len, cctx->M))
|
||||
return len;
|
||||
}
|
||||
}
|
||||
OPENSSL_cleanse(out, len);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int sms4_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_SMS4_CCM_CTX *cctx = EVP_C_DATA(EVP_SMS4_CCM_CTX,ctx);
|
||||
CCM128_CONTEXT *ccm = &cctx->ccm;
|
||||
/* If not set up, return error */
|
||||
if (!cctx->key_set)
|
||||
return -1;
|
||||
|
||||
if (cctx->tls_aad_len >= 0)
|
||||
return sms4_ccm_tls_cipher(ctx, out, in, len);
|
||||
|
||||
if (!cctx->iv_set)
|
||||
return -1;
|
||||
|
||||
if (!EVP_CIPHER_CTX_encrypting(ctx) && !cctx->tag_set)
|
||||
return -1;
|
||||
if (!out) {
|
||||
if (!in) {
|
||||
if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
15 - cctx->L, len))
|
||||
return -1;
|
||||
cctx->len_set = 1;
|
||||
return len;
|
||||
}
|
||||
/* If have AAD need message length */
|
||||
if (!cctx->len_set && len)
|
||||
return -1;
|
||||
CRYPTO_ccm128_aad(ccm, in, len);
|
||||
return len;
|
||||
}
|
||||
/* EVP_*Final() doesn't return any data */
|
||||
if (!in)
|
||||
return 0;
|
||||
/* If not set length yet do it */
|
||||
if (!cctx->len_set) {
|
||||
if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
15 - cctx->L, len))
|
||||
return -1;
|
||||
cctx->len_set = 1;
|
||||
}
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
|
||||
cctx->str) :
|
||||
CRYPTO_ccm128_encrypt(ccm, in, out, len))
|
||||
return -1;
|
||||
cctx->tag_set = 1;
|
||||
return len;
|
||||
} else {
|
||||
int rv = -1;
|
||||
if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
|
||||
cctx->str) :
|
||||
!CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
|
||||
unsigned char tag[16];
|
||||
if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
|
||||
if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx),
|
||||
cctx->M))
|
||||
rv = len;
|
||||
}
|
||||
}
|
||||
if (rv == -1)
|
||||
OPENSSL_cleanse(out, len);
|
||||
cctx->iv_set = 0;
|
||||
cctx->tag_set = 0;
|
||||
cctx->len_set = 0;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
#define SMS4_CCM_BLOCK_SIZE 1
|
||||
#define SMS4_CCM_IV_LENGTH 12
|
||||
|
||||
#define SMS4_CCM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
|
||||
| EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
|
||||
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
|
||||
| EVP_CIPH_CUSTOM_COPY \
|
||||
| EVP_CIPH_FLAG_AEAD_CIPHER)
|
||||
|
||||
static const EVP_CIPHER sms4_ccm = {
|
||||
NID_sms4_ccm,
|
||||
SMS4_CCM_BLOCK_SIZE,
|
||||
SMS4_KEY_LENGTH,
|
||||
SMS4_CCM_IV_LENGTH,
|
||||
SMS4_CCM_FLAGS,
|
||||
sms4_ccm_init_key,
|
||||
sms4_ccm_cipher,
|
||||
NULL,
|
||||
sizeof(EVP_SMS4_CCM_CTX),
|
||||
NULL, NULL, sms4_ccm_ctrl, NULL
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_sms4_ccm(void) {
|
||||
return &sms4_ccm;
|
||||
}
|
||||
#endif /* OPENSSL_NO_SMS4 */
|
||||
463
crypto/evp/e_sms4_gcm.c
Normal file
463
crypto/evp/e_sms4_gcm.c
Normal file
@@ -0,0 +1,463 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2017 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-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/evp.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
#include "../modes/modes_lcl.h"
|
||||
|
||||
#ifndef OPENSSL_NO_SMS4
|
||||
|
||||
#include <openssl/sms4.h>
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
double align;
|
||||
sms4_key_t ks;
|
||||
} ks; /* SMS4 key schedule to use */
|
||||
int key_set; /* Set if key initialised */
|
||||
int iv_set; /* Set if an iv is set */
|
||||
GCM128_CONTEXT gcm;
|
||||
unsigned char *iv; /* Temporary IV store */
|
||||
int ivlen; /* IV length */
|
||||
int taglen;
|
||||
int iv_gen; /* It is OK to generate IVs */
|
||||
int tls_aad_len; /* TLS AAD length */
|
||||
ctr128_f ctr;
|
||||
} EVP_SMS4_GCM_CTX;
|
||||
|
||||
|
||||
static int sms4_gcm_cleanup(EVP_CIPHER_CTX *c)
|
||||
{
|
||||
EVP_SMS4_GCM_CTX *gctx = EVP_C_DATA(EVP_SMS4_GCM_CTX,c);
|
||||
OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
|
||||
if (gctx->iv != EVP_CIPHER_CTX_iv_noconst(c))
|
||||
OPENSSL_free(gctx->iv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* increment counter (64-bit int) by 1 */
|
||||
static void ctr64_inc(unsigned char *counter)
|
||||
{
|
||||
int n = 8;
|
||||
unsigned char c;
|
||||
|
||||
do {
|
||||
--n;
|
||||
c = counter[n];
|
||||
++c;
|
||||
counter[n] = c;
|
||||
if (c)
|
||||
return;
|
||||
} while (n);
|
||||
}
|
||||
|
||||
static int sms4_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
|
||||
{
|
||||
EVP_SMS4_GCM_CTX *gctx = EVP_C_DATA(EVP_SMS4_GCM_CTX,c);
|
||||
switch (type) {
|
||||
case EVP_CTRL_INIT:
|
||||
gctx->key_set = 0;
|
||||
gctx->iv_set = 0;
|
||||
gctx->ivlen = EVP_CIPHER_CTX_iv_length(c);
|
||||
gctx->iv = EVP_CIPHER_CTX_iv_noconst(c);
|
||||
gctx->taglen = -1;
|
||||
gctx->iv_gen = 0;
|
||||
gctx->tls_aad_len = -1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_IVLEN:
|
||||
if (arg <= 0)
|
||||
return 0;
|
||||
/* Allocate memory for IV if needed */
|
||||
if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) {
|
||||
if (gctx->iv != EVP_CIPHER_CTX_iv_noconst(c))
|
||||
OPENSSL_free(gctx->iv);
|
||||
gctx->iv = OPENSSL_malloc(arg);
|
||||
if (gctx->iv == NULL)
|
||||
return 0;
|
||||
}
|
||||
gctx->ivlen = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_TAG:
|
||||
if (arg <= 0 || arg > 16 || EVP_CIPHER_CTX_encrypting(c))
|
||||
return 0;
|
||||
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
|
||||
gctx->taglen = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_GET_TAG:
|
||||
if (arg <= 0 || arg > 16 || !EVP_CIPHER_CTX_encrypting(c)
|
||||
|| gctx->taglen < 0)
|
||||
return 0;
|
||||
memcpy(ptr, EVP_CIPHER_CTX_buf_noconst(c), arg);
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_GCM_SET_IV_FIXED:
|
||||
/* Special case: -1 length restores whole IV */
|
||||
if (arg == -1) {
|
||||
memcpy(gctx->iv, ptr, gctx->ivlen);
|
||||
gctx->iv_gen = 1;
|
||||
return 1;
|
||||
}
|
||||
/*
|
||||
* Fixed field must be at least 4 bytes and invocation field at least
|
||||
* 8.
|
||||
*/
|
||||
if ((arg < 4) || (gctx->ivlen - arg) < 8)
|
||||
return 0;
|
||||
if (arg)
|
||||
memcpy(gctx->iv, ptr, arg);
|
||||
if (EVP_CIPHER_CTX_encrypting(c)
|
||||
&& RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
|
||||
return 0;
|
||||
gctx->iv_gen = 1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_GCM_IV_GEN:
|
||||
if (gctx->iv_gen == 0 || gctx->key_set == 0)
|
||||
return 0;
|
||||
CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
|
||||
if (arg <= 0 || arg > gctx->ivlen)
|
||||
arg = gctx->ivlen;
|
||||
memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
|
||||
/*
|
||||
* Invocation field will be at least 8 bytes in size and so no need
|
||||
* to check wrap around or increment more than last 8 bytes.
|
||||
*/
|
||||
ctr64_inc(gctx->iv + gctx->ivlen - 8);
|
||||
gctx->iv_set = 1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_GCM_SET_IV_INV:
|
||||
if (gctx->iv_gen == 0 || gctx->key_set == 0
|
||||
|| EVP_CIPHER_CTX_encrypting(c))
|
||||
return 0;
|
||||
memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
|
||||
CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
|
||||
gctx->iv_set = 1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_TLS1_AAD:
|
||||
/* Save the AAD for later use */
|
||||
if (arg != EVP_AEAD_TLS1_AAD_LEN)
|
||||
return 0;
|
||||
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
|
||||
gctx->tls_aad_len = arg;
|
||||
{
|
||||
unsigned int len =
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
|
||||
| EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
|
||||
/* Correct length for explicit IV */
|
||||
len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
/* If decrypting correct for tag too */
|
||||
if (!EVP_CIPHER_CTX_encrypting(c))
|
||||
len -= EVP_GCM_TLS_TAG_LEN;
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
|
||||
}
|
||||
/* Extra padding: tag appended to record */
|
||||
return EVP_GCM_TLS_TAG_LEN;
|
||||
|
||||
case EVP_CTRL_COPY:
|
||||
{
|
||||
EVP_CIPHER_CTX *out = ptr;
|
||||
EVP_SMS4_GCM_CTX *gctx_out = EVP_C_DATA(EVP_SMS4_GCM_CTX,out);
|
||||
if (gctx->gcm.key) {
|
||||
if (gctx->gcm.key != &gctx->ks)
|
||||
return 0;
|
||||
gctx_out->gcm.key = &gctx_out->ks;
|
||||
}
|
||||
if (gctx->iv == EVP_CIPHER_CTX_iv_noconst(c))
|
||||
gctx_out->iv = EVP_CIPHER_CTX_iv_noconst(out);
|
||||
else {
|
||||
gctx_out->iv = OPENSSL_malloc(gctx->ivlen);
|
||||
if (gctx_out->iv == NULL)
|
||||
return 0;
|
||||
memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
default:
|
||||
return -1;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int sms4_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
EVP_SMS4_GCM_CTX *gctx = EVP_C_DATA(EVP_SMS4_GCM_CTX,ctx);
|
||||
if (!iv && !key)
|
||||
return 1;
|
||||
if (key) {
|
||||
do {
|
||||
(void)0; /* terminate potentially open 'else' */
|
||||
|
||||
sms4_set_encrypt_key(&gctx->ks.ks, key);
|
||||
CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
|
||||
(block128_f)sms4_encrypt);
|
||||
gctx->ctr = NULL;
|
||||
} while (0);
|
||||
|
||||
/*
|
||||
* If we have an iv can set it directly, otherwise use saved IV.
|
||||
*/
|
||||
if (iv == NULL && gctx->iv_set)
|
||||
iv = gctx->iv;
|
||||
if (iv) {
|
||||
CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
|
||||
gctx->iv_set = 1;
|
||||
}
|
||||
gctx->key_set = 1;
|
||||
} else {
|
||||
/* If key set use IV, otherwise copy */
|
||||
if (gctx->key_set)
|
||||
CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
|
||||
else
|
||||
memcpy(gctx->iv, iv, gctx->ivlen);
|
||||
gctx->iv_set = 1;
|
||||
gctx->iv_gen = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle TLS GCM packet format. This consists of the last portion of the IV
|
||||
* followed by the payload and finally the tag. On encrypt generate IV,
|
||||
* encrypt payload and write the tag. On verify retrieve IV, decrypt payload
|
||||
* and verify tag.
|
||||
*/
|
||||
|
||||
static int sms4_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_SMS4_GCM_CTX *gctx = EVP_C_DATA(EVP_SMS4_GCM_CTX,ctx);
|
||||
int rv = -1;
|
||||
/* Encrypt/decrypt must be performed in place */
|
||||
if (out != in
|
||||
|| len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
|
||||
return -1;
|
||||
/*
|
||||
* Set IV from start of buffer or generate IV and write to start of
|
||||
* buffer.
|
||||
*/
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CIPHER_CTX_encrypting(ctx) ?
|
||||
EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
|
||||
EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
|
||||
goto err;
|
||||
/* Use saved AAD */
|
||||
if (CRYPTO_gcm128_aad(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
|
||||
gctx->tls_aad_len))
|
||||
goto err;
|
||||
/* Fix buffer and length to point to payload */
|
||||
in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
/* Encrypt payload */
|
||||
if (gctx->ctr) {
|
||||
size_t bulk = 0;
|
||||
if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
|
||||
in + bulk,
|
||||
out + bulk,
|
||||
len - bulk, gctx->ctr))
|
||||
goto err;
|
||||
} else {
|
||||
size_t bulk = 0;
|
||||
if (CRYPTO_gcm128_encrypt(&gctx->gcm,
|
||||
in + bulk, out + bulk, len - bulk))
|
||||
goto err;
|
||||
}
|
||||
out += len;
|
||||
/* Finally write tag */
|
||||
CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
|
||||
rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
|
||||
} else {
|
||||
/* Decrypt */
|
||||
if (gctx->ctr) {
|
||||
size_t bulk = 0;
|
||||
if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
|
||||
in + bulk,
|
||||
out + bulk,
|
||||
len - bulk, gctx->ctr))
|
||||
goto err;
|
||||
} else {
|
||||
size_t bulk = 0;
|
||||
if (CRYPTO_gcm128_decrypt(&gctx->gcm,
|
||||
in + bulk, out + bulk, len - bulk))
|
||||
goto err;
|
||||
}
|
||||
/* Retrieve tag */
|
||||
CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
|
||||
EVP_GCM_TLS_TAG_LEN);
|
||||
/* If tag mismatch wipe buffer */
|
||||
if (CRYPTO_memcmp(EVP_CIPHER_CTX_buf_noconst(ctx), in + len,
|
||||
EVP_GCM_TLS_TAG_LEN)) {
|
||||
OPENSSL_cleanse(out, len);
|
||||
goto err;
|
||||
}
|
||||
rv = len;
|
||||
}
|
||||
|
||||
err:
|
||||
gctx->iv_set = 0;
|
||||
gctx->tls_aad_len = -1;
|
||||
return rv;
|
||||
}
|
||||
|
||||
static int sms4_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_SMS4_GCM_CTX *gctx = EVP_C_DATA(EVP_SMS4_GCM_CTX,ctx);
|
||||
/* If not set up, return error */
|
||||
if (!gctx->key_set)
|
||||
return -1;
|
||||
|
||||
if (gctx->tls_aad_len >= 0)
|
||||
return sms4_gcm_tls_cipher(ctx, out, in, len);
|
||||
|
||||
if (!gctx->iv_set)
|
||||
return -1;
|
||||
if (in) {
|
||||
if (out == NULL) {
|
||||
if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
|
||||
return -1;
|
||||
} else if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (gctx->ctr) {
|
||||
size_t bulk = 0;
|
||||
if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
|
||||
in + bulk,
|
||||
out + bulk,
|
||||
len - bulk, gctx->ctr))
|
||||
return -1;
|
||||
} else {
|
||||
size_t bulk = 0;
|
||||
if (CRYPTO_gcm128_encrypt(&gctx->gcm,
|
||||
in + bulk, out + bulk, len - bulk))
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (gctx->ctr) {
|
||||
size_t bulk = 0;
|
||||
if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
|
||||
in + bulk,
|
||||
out + bulk,
|
||||
len - bulk, gctx->ctr))
|
||||
return -1;
|
||||
} else {
|
||||
size_t bulk = 0;
|
||||
if (CRYPTO_gcm128_decrypt(&gctx->gcm,
|
||||
in + bulk, out + bulk, len - bulk))
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return len;
|
||||
} else {
|
||||
if (!EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (gctx->taglen < 0)
|
||||
return -1;
|
||||
if (CRYPTO_gcm128_finish(&gctx->gcm,
|
||||
EVP_CIPHER_CTX_buf_noconst(ctx),
|
||||
gctx->taglen) != 0)
|
||||
return -1;
|
||||
gctx->iv_set = 0;
|
||||
return 0;
|
||||
}
|
||||
CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), 16);
|
||||
gctx->taglen = 16;
|
||||
/* Don't reuse the IV */
|
||||
gctx->iv_set = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#define SMS4_GCM_BLOCK_SIZE 1
|
||||
#define SMS4_GCM_IV_LENGTH 12
|
||||
|
||||
#define SMS4_GCM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
|
||||
| EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
|
||||
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
|
||||
| EVP_CIPH_CUSTOM_COPY \
|
||||
| EVP_CIPH_FLAG_AEAD_CIPHER)
|
||||
|
||||
static const EVP_CIPHER sms4_gcm = {
|
||||
NID_sms4_gcm,
|
||||
SMS4_GCM_BLOCK_SIZE,
|
||||
SMS4_KEY_LENGTH,
|
||||
SMS4_GCM_IV_LENGTH,
|
||||
SMS4_GCM_FLAGS,
|
||||
sms4_gcm_init_key,
|
||||
sms4_gcm_cipher,
|
||||
sms4_gcm_cleanup,
|
||||
sizeof(EVP_SMS4_GCM_CTX),
|
||||
NULL, NULL, sms4_gcm_ctrl, NULL
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_sms4_gcm(void) {
|
||||
return &sms4_gcm;
|
||||
}
|
||||
|
||||
#endif /* OPENSSL_NO_SMS4 */
|
||||
373
crypto/evp/e_sms4_ocb.c
Normal file
373
crypto/evp/e_sms4_ocb.c
Normal file
@@ -0,0 +1,373 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2017 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-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/evp.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
#include "../modes/modes_lcl.h"
|
||||
|
||||
#ifndef OPENSSL_NO_SMS4
|
||||
|
||||
# include <openssl/sms4.h>
|
||||
|
||||
# ifndef OPENSSL_NO_OCB
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
double align;
|
||||
sms4_key_t ks;
|
||||
} ksenc; /* SMS4 key schedule to use for encryption */
|
||||
union {
|
||||
double align;
|
||||
sms4_key_t ks;
|
||||
} ksdec; /* SMS4 key schedule to use for decryption */
|
||||
int key_set; /* Set if key initialised */
|
||||
int iv_set; /* Set if an iv is set */
|
||||
OCB128_CONTEXT ocb;
|
||||
unsigned char *iv; /* Temporary IV store */
|
||||
unsigned char tag[16];
|
||||
unsigned char data_buf[16]; /* Store partial data blocks */
|
||||
unsigned char aad_buf[16]; /* Store partial AAD blocks */
|
||||
int data_buf_len;
|
||||
int aad_buf_len;
|
||||
int ivlen; /* IV length */
|
||||
int taglen;
|
||||
} EVP_SMS4_OCB_CTX;
|
||||
|
||||
static int sms4_ocb_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
|
||||
{
|
||||
EVP_SMS4_OCB_CTX *octx = EVP_C_DATA(EVP_SMS4_OCB_CTX,c);
|
||||
EVP_CIPHER_CTX *newc;
|
||||
EVP_SMS4_OCB_CTX *new_octx;
|
||||
|
||||
switch (type) {
|
||||
case EVP_CTRL_INIT:
|
||||
octx->key_set = 0;
|
||||
octx->iv_set = 0;
|
||||
octx->ivlen = EVP_CIPHER_CTX_iv_length(c);
|
||||
octx->iv = EVP_CIPHER_CTX_iv_noconst(c);
|
||||
octx->taglen = 16;
|
||||
octx->data_buf_len = 0;
|
||||
octx->aad_buf_len = 0;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_IVLEN:
|
||||
/* IV len must be 1 to 15 */
|
||||
if (arg <= 0 || arg > 15)
|
||||
return 0;
|
||||
|
||||
octx->ivlen = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_TAG:
|
||||
if (!ptr) {
|
||||
/* Tag len must be 0 to 16 */
|
||||
if (arg < 0 || arg > 16)
|
||||
return 0;
|
||||
|
||||
octx->taglen = arg;
|
||||
return 1;
|
||||
}
|
||||
if (arg != octx->taglen || EVP_CIPHER_CTX_encrypting(c))
|
||||
return 0;
|
||||
memcpy(octx->tag, ptr, arg);
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_GET_TAG:
|
||||
if (arg != octx->taglen || !EVP_CIPHER_CTX_encrypting(c))
|
||||
return 0;
|
||||
|
||||
memcpy(ptr, octx->tag, arg);
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_COPY:
|
||||
newc = (EVP_CIPHER_CTX *)ptr;
|
||||
new_octx = EVP_C_DATA(EVP_SMS4_OCB_CTX,newc);
|
||||
return CRYPTO_ocb128_copy_ctx(&new_octx->ocb, &octx->ocb,
|
||||
&new_octx->ksenc.ks,
|
||||
&new_octx->ksdec.ks);
|
||||
|
||||
default:
|
||||
return -1;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int sms4_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
EVP_SMS4_OCB_CTX *octx = EVP_C_DATA(EVP_SMS4_OCB_CTX,ctx);
|
||||
if (!iv && !key)
|
||||
return 1;
|
||||
if (key) {
|
||||
do {
|
||||
/*
|
||||
* We set both the encrypt and decrypt key here because decrypt
|
||||
* needs both. We could possibly optimise to remove setting the
|
||||
* decrypt for an encryption operation.
|
||||
*/
|
||||
sms4_set_encrypt_key(&octx->ksenc.ks, key);
|
||||
sms4_set_decrypt_key(&octx->ksdec.ks, key);
|
||||
if (!CRYPTO_ocb128_init(&octx->ocb,
|
||||
&octx->ksenc.ks, &octx->ksdec.ks,
|
||||
(block128_f)sms4_encrypt,
|
||||
(block128_f)sms4_encrypt,
|
||||
NULL))
|
||||
return 0;
|
||||
}
|
||||
while (0);
|
||||
|
||||
/*
|
||||
* If we have an iv we can set it directly, otherwise use saved IV.
|
||||
*/
|
||||
if (iv == NULL && octx->iv_set)
|
||||
iv = octx->iv;
|
||||
if (iv) {
|
||||
if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen)
|
||||
!= 1)
|
||||
return 0;
|
||||
octx->iv_set = 1;
|
||||
}
|
||||
octx->key_set = 1;
|
||||
} else {
|
||||
/* If key set use IV, otherwise copy */
|
||||
if (octx->key_set)
|
||||
CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
|
||||
else
|
||||
memcpy(octx->iv, iv, octx->ivlen);
|
||||
octx->iv_set = 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int sms4_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
int *buf_len;
|
||||
int written_len = 0;
|
||||
size_t trailing_len;
|
||||
EVP_SMS4_OCB_CTX *octx = EVP_C_DATA(EVP_SMS4_OCB_CTX,ctx);
|
||||
|
||||
/* If IV or Key not set then return error */
|
||||
if (!octx->iv_set)
|
||||
return -1;
|
||||
|
||||
if (!octx->key_set)
|
||||
return -1;
|
||||
|
||||
if (in) {
|
||||
/*
|
||||
* Need to ensure we are only passing full blocks to low level OCB
|
||||
* routines. We do it here rather than in EVP_EncryptUpdate/
|
||||
* EVP_DecryptUpdate because we need to pass full blocks of AAD too
|
||||
* and those routines don't support that
|
||||
*/
|
||||
|
||||
/* Are we dealing with AAD or normal data here? */
|
||||
if (out == NULL) {
|
||||
buf = octx->aad_buf;
|
||||
buf_len = &(octx->aad_buf_len);
|
||||
} else {
|
||||
buf = octx->data_buf;
|
||||
buf_len = &(octx->data_buf_len);
|
||||
}
|
||||
|
||||
/*
|
||||
* If we've got a partially filled buffer from a previous call then
|
||||
* use that data first
|
||||
*/
|
||||
if (*buf_len) {
|
||||
unsigned int remaining;
|
||||
|
||||
remaining = 16 - (*buf_len);
|
||||
if (remaining > len) {
|
||||
memcpy(buf + (*buf_len), in, len);
|
||||
*(buf_len) += len;
|
||||
return 0;
|
||||
}
|
||||
memcpy(buf + (*buf_len), in, remaining);
|
||||
|
||||
/*
|
||||
* If we get here we've filled the buffer, so process it
|
||||
*/
|
||||
len -= remaining;
|
||||
in += remaining;
|
||||
if (out == NULL) {
|
||||
if (!CRYPTO_ocb128_aad(&octx->ocb, buf, 16))
|
||||
return -1;
|
||||
} else if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (!CRYPTO_ocb128_encrypt(&octx->ocb, buf, out, 16))
|
||||
return -1;
|
||||
} else {
|
||||
if (!CRYPTO_ocb128_decrypt(&octx->ocb, buf, out, 16))
|
||||
return -1;
|
||||
}
|
||||
written_len = 16;
|
||||
*buf_len = 0;
|
||||
}
|
||||
|
||||
/* Do we have a partial block to handle at the end? */
|
||||
trailing_len = len % 16;
|
||||
|
||||
/*
|
||||
* If we've got some full blocks to handle, then process these first
|
||||
*/
|
||||
if (len != trailing_len) {
|
||||
if (out == NULL) {
|
||||
if (!CRYPTO_ocb128_aad(&octx->ocb, in, len - trailing_len))
|
||||
return -1;
|
||||
} else if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (!CRYPTO_ocb128_encrypt
|
||||
(&octx->ocb, in, out, len - trailing_len))
|
||||
return -1;
|
||||
} else {
|
||||
if (!CRYPTO_ocb128_decrypt
|
||||
(&octx->ocb, in, out, len - trailing_len))
|
||||
return -1;
|
||||
}
|
||||
written_len += len - trailing_len;
|
||||
in += len - trailing_len;
|
||||
}
|
||||
|
||||
/* Handle any trailing partial block */
|
||||
if (trailing_len) {
|
||||
memcpy(buf, in, trailing_len);
|
||||
*buf_len = trailing_len;
|
||||
}
|
||||
|
||||
return written_len;
|
||||
} else {
|
||||
/*
|
||||
* First of all empty the buffer of any partial block that we might
|
||||
* have been provided - both for data and AAD
|
||||
*/
|
||||
if (octx->data_buf_len) {
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (!CRYPTO_ocb128_encrypt(&octx->ocb, octx->data_buf, out,
|
||||
octx->data_buf_len))
|
||||
return -1;
|
||||
} else {
|
||||
if (!CRYPTO_ocb128_decrypt(&octx->ocb, octx->data_buf, out,
|
||||
octx->data_buf_len))
|
||||
return -1;
|
||||
}
|
||||
written_len = octx->data_buf_len;
|
||||
octx->data_buf_len = 0;
|
||||
}
|
||||
if (octx->aad_buf_len) {
|
||||
if (!CRYPTO_ocb128_aad
|
||||
(&octx->ocb, octx->aad_buf, octx->aad_buf_len))
|
||||
return -1;
|
||||
octx->aad_buf_len = 0;
|
||||
}
|
||||
/* If decrypting then verify */
|
||||
if (!EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (octx->taglen < 0)
|
||||
return -1;
|
||||
if (CRYPTO_ocb128_finish(&octx->ocb,
|
||||
octx->tag, octx->taglen) != 0)
|
||||
return -1;
|
||||
octx->iv_set = 0;
|
||||
return written_len;
|
||||
}
|
||||
/* If encrypting then just get the tag */
|
||||
if (CRYPTO_ocb128_tag(&octx->ocb, octx->tag, 16) != 1)
|
||||
return -1;
|
||||
/* Don't reuse the IV */
|
||||
octx->iv_set = 0;
|
||||
return written_len;
|
||||
}
|
||||
}
|
||||
|
||||
static int sms4_ocb_cleanup(EVP_CIPHER_CTX *c)
|
||||
{
|
||||
EVP_SMS4_OCB_CTX *octx = EVP_C_DATA(EVP_SMS4_OCB_CTX,c);
|
||||
CRYPTO_ocb128_cleanup(&octx->ocb);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define SMS4_OCB_IV_LENGTH 12
|
||||
|
||||
#define SMS4_OCB_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
|
||||
| EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
|
||||
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
|
||||
| EVP_CIPH_CUSTOM_COPY \
|
||||
| EVP_CIPH_FLAG_AEAD_CIPHER)
|
||||
|
||||
static const EVP_CIPHER sms4_ocb = {
|
||||
NID_sms4_ocb,
|
||||
SMS4_BLOCK_SIZE,
|
||||
SMS4_KEY_LENGTH,
|
||||
SMS4_OCB_IV_LENGTH,
|
||||
SMS4_OCB_FLAGS,
|
||||
sms4_ocb_init_key,
|
||||
sms4_ocb_cipher,
|
||||
sms4_ocb_cleanup,
|
||||
sizeof(EVP_SMS4_OCB_CTX),
|
||||
NULL, NULL, sms4_ocb_ctrl, NULL
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_sms4_ocb(void) {
|
||||
return &sms4_ocb;
|
||||
}
|
||||
# endif /* OPENSSL_NO_OCB */
|
||||
#endif /* OPENSSL_NO_SMS4 */
|
||||
188
crypto/evp/e_sms4_wrap.c
Normal file
188
crypto/evp/e_sms4_wrap.c
Normal file
@@ -0,0 +1,188 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2017 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-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/evp.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
#include "../modes/modes_lcl.h"
|
||||
|
||||
#ifndef OPENSSL_NO_SMS4
|
||||
# include <openssl/sms4.h>
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
double align;
|
||||
sms4_key_t ks;
|
||||
} ks;
|
||||
/* Indicates if IV has been set */
|
||||
unsigned char *iv;
|
||||
} EVP_SMS4_WRAP_CTX;
|
||||
|
||||
static int sms4_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
EVP_SMS4_WRAP_CTX *wctx = EVP_C_DATA(EVP_SMS4_WRAP_CTX,ctx);
|
||||
if (!iv && !key)
|
||||
return 1;
|
||||
if (key) {
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx))
|
||||
sms4_set_encrypt_key(&wctx->ks.ks, key);
|
||||
else
|
||||
sms4_set_decrypt_key(&wctx->ks.ks, key);
|
||||
if (!iv)
|
||||
wctx->iv = NULL;
|
||||
}
|
||||
if (iv) {
|
||||
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, EVP_CIPHER_CTX_iv_length(ctx));
|
||||
wctx->iv = EVP_CIPHER_CTX_iv_noconst(ctx);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int sms4_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inlen)
|
||||
{
|
||||
EVP_SMS4_WRAP_CTX *wctx = EVP_C_DATA(EVP_SMS4_WRAP_CTX,ctx);
|
||||
size_t rv;
|
||||
/* SMS4 wrap with padding has IV length of 4, without padding 8 */
|
||||
int pad = EVP_CIPHER_CTX_iv_length(ctx) == 4;
|
||||
/* No final operation so always return zero length */
|
||||
if (!in)
|
||||
return 0;
|
||||
/* Input length must always be non-zero */
|
||||
if (!inlen)
|
||||
return -1;
|
||||
/* If decrypting need at least 16 bytes and multiple of 8 */
|
||||
if (!EVP_CIPHER_CTX_encrypting(ctx) && (inlen < 16 || inlen & 0x7))
|
||||
return -1;
|
||||
/* If not padding input must be multiple of 8 */
|
||||
if (!pad && inlen & 0x7)
|
||||
return -1;
|
||||
if (!out) {
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
/* If padding round up to multiple of 8 */
|
||||
if (pad)
|
||||
inlen = (inlen + 7) / 8 * 8;
|
||||
/* 8 byte prefix */
|
||||
return inlen + 8;
|
||||
} else {
|
||||
/*
|
||||
* If not padding output will be exactly 8 bytes smaller than
|
||||
* input. If padding it will be at least 8 bytes smaller but we
|
||||
* don't know how much.
|
||||
*/
|
||||
return inlen - 8;
|
||||
}
|
||||
}
|
||||
if (pad) {
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx))
|
||||
rv = CRYPTO_128_wrap_pad(&wctx->ks.ks, wctx->iv,
|
||||
out, in, inlen,
|
||||
(block128_f)sms4_encrypt);
|
||||
else
|
||||
rv = CRYPTO_128_unwrap_pad(&wctx->ks.ks, wctx->iv,
|
||||
out, in, inlen,
|
||||
(block128_f)sms4_encrypt);
|
||||
} else {
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx))
|
||||
rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv,
|
||||
out, in, inlen, (block128_f)sms4_encrypt);
|
||||
else
|
||||
rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv,
|
||||
out, in, inlen, (block128_f)sms4_encrypt);
|
||||
}
|
||||
return rv ? (int)rv : -1;
|
||||
}
|
||||
|
||||
#define SMS4_WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
|
||||
| EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
|
||||
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
|
||||
|
||||
|
||||
|
||||
static const EVP_CIPHER sms4_wrap = {
|
||||
NID_sms4_wrap,
|
||||
8, 16, 8, SMS4_WRAP_FLAGS,
|
||||
sms4_wrap_init_key, sms4_wrap_cipher,
|
||||
NULL,
|
||||
sizeof(EVP_SMS4_WRAP_CTX),
|
||||
NULL, NULL, NULL, NULL
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_sms4_wrap(void)
|
||||
{
|
||||
return &sms4_wrap;
|
||||
}
|
||||
|
||||
static const EVP_CIPHER sms4_wrap_pad = {
|
||||
NID_sms4_wrap_pad,
|
||||
8, 16, 4, SMS4_WRAP_FLAGS,
|
||||
sms4_wrap_init_key, sms4_wrap_cipher,
|
||||
NULL,
|
||||
sizeof(EVP_SMS4_WRAP_CTX),
|
||||
NULL, NULL, NULL, NULL
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_sms4_wrap_pad(void)
|
||||
{
|
||||
return &sms4_wrap_pad;
|
||||
}
|
||||
#endif /* OPENSSL_NO_SMS4 */
|
||||
183
crypto/evp/e_sms4_xts.c
Normal file
183
crypto/evp/e_sms4_xts.c
Normal file
@@ -0,0 +1,183 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2017 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.
|
||||
* ====================================================================
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-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/evp.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
#include "../modes/modes_lcl.h"
|
||||
|
||||
#ifndef OPENSSL_NO_SMS4
|
||||
# include <openssl/sms4.h>
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
double align;
|
||||
sms4_key_t ks;
|
||||
} ks1, ks2; /* SMS4 key schedules to use */
|
||||
XTS128_CONTEXT xts;
|
||||
void (*stream) (const unsigned char *in,
|
||||
unsigned char *out, size_t length,
|
||||
const sms4_key_t *key1, const sms4_key_t *key2,
|
||||
const unsigned char iv[16]);
|
||||
} EVP_SMS4_XTS_CTX;
|
||||
|
||||
static int sms4_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
|
||||
{
|
||||
EVP_SMS4_XTS_CTX *xctx = EVP_C_DATA(EVP_SMS4_XTS_CTX,c);
|
||||
if (type == EVP_CTRL_COPY) {
|
||||
EVP_CIPHER_CTX *out = ptr;
|
||||
EVP_SMS4_XTS_CTX *xctx_out = EVP_C_DATA(EVP_SMS4_XTS_CTX,out);
|
||||
if (xctx->xts.key1) {
|
||||
if (xctx->xts.key1 != &xctx->ks1)
|
||||
return 0;
|
||||
xctx_out->xts.key1 = &xctx_out->ks1;
|
||||
}
|
||||
if (xctx->xts.key2) {
|
||||
if (xctx->xts.key2 != &xctx->ks2)
|
||||
return 0;
|
||||
xctx_out->xts.key2 = &xctx_out->ks2;
|
||||
}
|
||||
return 1;
|
||||
} else if (type != EVP_CTRL_INIT)
|
||||
return -1;
|
||||
/* key1 and key2 are used as an indicator both key and IV are set */
|
||||
xctx->xts.key1 = NULL;
|
||||
xctx->xts.key2 = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int sms4_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
EVP_SMS4_XTS_CTX *xctx = EVP_C_DATA(EVP_SMS4_XTS_CTX,ctx);
|
||||
if (!iv && !key)
|
||||
return 1;
|
||||
|
||||
if (key)
|
||||
do {
|
||||
xctx->stream = NULL;
|
||||
/* key_len is two SMS4 keys */
|
||||
(void)0; /* terminate potentially open 'else' */
|
||||
|
||||
if (enc) {
|
||||
sms4_set_encrypt_key(&xctx->ks1.ks, key);
|
||||
xctx->xts.block1 = (block128_f)sms4_encrypt;
|
||||
} else {
|
||||
sms4_set_decrypt_key(&xctx->ks1.ks, key);
|
||||
xctx->xts.block1 = (block128_f)sms4_encrypt;
|
||||
}
|
||||
|
||||
sms4_set_encrypt_key(&xctx->ks2.ks, key + EVP_CIPHER_CTX_key_length(ctx)/2);
|
||||
xctx->xts.block2 = (block128_f)sms4_encrypt;
|
||||
|
||||
xctx->xts.key1 = &xctx->ks1;
|
||||
} while (0);
|
||||
|
||||
if (iv) {
|
||||
xctx->xts.key2 = &xctx->ks2;
|
||||
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 16);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int sms4_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_SMS4_XTS_CTX *xctx = EVP_C_DATA(EVP_SMS4_XTS_CTX,ctx);
|
||||
if (!xctx->xts.key1 || !xctx->xts.key2)
|
||||
return 0;
|
||||
if (!out || !in || len < SMS4_BLOCK_SIZE)
|
||||
return 0;
|
||||
if (xctx->stream)
|
||||
(*xctx->stream) (in, out, len,
|
||||
xctx->xts.key1, xctx->xts.key2,
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx));
|
||||
else if (CRYPTO_xts128_encrypt(&xctx->xts, EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
in, out, len,
|
||||
EVP_CIPHER_CTX_encrypting(ctx)))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define SMS4_XTS_BLOCK_SIZE 1
|
||||
|
||||
#define SMS4_XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
|
||||
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
|
||||
| EVP_CIPH_CUSTOM_COPY)
|
||||
|
||||
static const EVP_CIPHER sms4_xts = {
|
||||
NID_sms4_xts,
|
||||
SMS4_XTS_BLOCK_SIZE,
|
||||
SMS4_KEY_LENGTH,
|
||||
SMS4_IV_LENGTH,
|
||||
SMS4_XTS_FLAGS,
|
||||
sms4_xts_init_key,
|
||||
sms4_xts_cipher,
|
||||
NULL,
|
||||
sizeof(EVP_SMS4_XTS_CTX),
|
||||
NULL, NULL, sms4_xts_ctrl, NULL
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_sms4_xts(void) {
|
||||
return &sms4_xts;
|
||||
}
|
||||
#endif /* OPENSSL_NO_SMS4 */
|
||||
Reference in New Issue
Block a user