-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
providers: Add SM4 GCM implementation
The GCM mode of the SM4 algorithm is specifieded by RFC8998. Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com> Reviewed-by: Paul Yang <kaishen.yy@antfin.com> Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from #16491) (cherry picked from commit c2ee608)
- Loading branch information
Showing
11 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License 2.0 (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 | ||
*/ | ||
|
||
/* Dispatch functions for SM4 CCM mode */ | ||
|
||
#include "cipher_sm4_ccm.h" | ||
#include "prov/implementations.h" | ||
#include "prov/providercommon.h" | ||
|
||
static OSSL_FUNC_cipher_freectx_fn sm4_ccm_freectx; | ||
|
||
static void *sm4_ccm_newctx(void *provctx, size_t keybits) | ||
{ | ||
PROV_SM4_CCM_CTX *ctx; | ||
|
||
if (!ossl_prov_is_running()) | ||
return NULL; | ||
|
||
ctx = OPENSSL_zalloc(sizeof(*ctx)); | ||
if (ctx != NULL) | ||
ossl_ccm_initctx(&ctx->base, keybits, ossl_prov_sm4_hw_ccm(keybits)); | ||
return ctx; | ||
} | ||
|
||
static void sm4_ccm_freectx(void *vctx) | ||
{ | ||
PROV_SM4_CCM_CTX *ctx = (PROV_SM4_CCM_CTX *)vctx; | ||
|
||
OPENSSL_clear_free(ctx, sizeof(*ctx)); | ||
} | ||
|
||
/* sm4128ccm functions */ | ||
IMPLEMENT_aead_cipher(sm4, ccm, CCM, AEAD_FLAGS, 128, 8, 96); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License 2.0 (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 "crypto/sm4.h" | ||
#include "prov/ciphercommon.h" | ||
#include "prov/ciphercommon_ccm.h" | ||
|
||
typedef struct prov_sm4_ccm_ctx_st { | ||
PROV_CCM_CTX base; /* Must be first */ | ||
union { | ||
OSSL_UNION_ALIGN; | ||
SM4_KEY ks; | ||
} ks; /* SM4 key schedule to use */ | ||
} PROV_SM4_CCM_CTX; | ||
|
||
const PROV_CCM_HW *ossl_prov_sm4_hw_ccm(size_t keylen); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License 2.0 (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 | ||
*/ | ||
|
||
/*- | ||
* Generic support for SM4 CCM. | ||
*/ | ||
|
||
#include "cipher_sm4_ccm.h" | ||
|
||
static int ccm_sm4_initkey(PROV_CCM_CTX *ctx, | ||
const unsigned char *key, size_t keylen) | ||
{ | ||
PROV_SM4_CCM_CTX *actx = (PROV_SM4_CCM_CTX *)ctx; | ||
|
||
ossl_sm4_set_key(key, &actx->ks.ks); | ||
CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ks.ks, | ||
(block128_f)ossl_sm4_encrypt); | ||
ctx->str = NULL; | ||
ctx->key_set = 1; | ||
return 1; | ||
} | ||
|
||
static const PROV_CCM_HW ccm_sm4 = { | ||
ccm_sm4_initkey, | ||
ossl_ccm_generic_setiv, | ||
ossl_ccm_generic_setaad, | ||
ossl_ccm_generic_auth_encrypt, | ||
ossl_ccm_generic_auth_decrypt, | ||
ossl_ccm_generic_gettag | ||
}; | ||
|
||
const PROV_CCM_HW *ossl_prov_sm4_hw_ccm(size_t keybits) | ||
{ | ||
return &ccm_sm4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License 2.0 (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 | ||
*/ | ||
|
||
/* Dispatch functions for SM4 GCM mode */ | ||
|
||
#include "cipher_sm4_gcm.h" | ||
#include "prov/implementations.h" | ||
#include "prov/providercommon.h" | ||
|
||
static OSSL_FUNC_cipher_freectx_fn sm4_gcm_freectx; | ||
|
||
static void *sm4_gcm_newctx(void *provctx, size_t keybits) | ||
{ | ||
PROV_SM4_GCM_CTX *ctx; | ||
|
||
if (!ossl_prov_is_running()) | ||
return NULL; | ||
|
||
ctx = OPENSSL_zalloc(sizeof(*ctx)); | ||
if (ctx != NULL) | ||
ossl_gcm_initctx(provctx, &ctx->base, keybits, | ||
ossl_prov_sm4_hw_gcm(keybits)); | ||
return ctx; | ||
} | ||
|
||
static void sm4_gcm_freectx(void *vctx) | ||
{ | ||
PROV_SM4_GCM_CTX *ctx = (PROV_SM4_GCM_CTX *)vctx; | ||
|
||
OPENSSL_clear_free(ctx, sizeof(*ctx)); | ||
} | ||
|
||
/* ossl_sm4128gcm_functions */ | ||
IMPLEMENT_aead_cipher(sm4, gcm, GCM, AEAD_FLAGS, 128, 8, 96); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License 2.0 (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 "crypto/sm4.h" | ||
#include "prov/ciphercommon.h" | ||
#include "prov/ciphercommon_gcm.h" | ||
|
||
typedef struct prov_sm4_gcm_ctx_st { | ||
PROV_GCM_CTX base; /* must be first entry in struct */ | ||
union { | ||
OSSL_UNION_ALIGN; | ||
SM4_KEY ks; | ||
} ks; | ||
} PROV_SM4_GCM_CTX; | ||
|
||
const PROV_GCM_HW *ossl_prov_sm4_hw_gcm(size_t keybits); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License 2.0 (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 | ||
*/ | ||
|
||
/*- | ||
* Generic support for SM4 GCM. | ||
*/ | ||
|
||
#include "cipher_sm4_gcm.h" | ||
|
||
static int sm4_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key, | ||
size_t keylen) | ||
{ | ||
PROV_SM4_GCM_CTX *actx = (PROV_SM4_GCM_CTX *)ctx; | ||
SM4_KEY *ks = &actx->ks.ks; | ||
|
||
ctx->ks = ks; | ||
ossl_sm4_set_key(key, ks); | ||
CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f)ossl_sm4_encrypt); | ||
ctx->ctr = (ctr128_f)NULL; | ||
ctx->key_set = 1; | ||
|
||
return 1; | ||
} | ||
|
||
static const PROV_GCM_HW sm4_gcm = { | ||
sm4_gcm_initkey, | ||
ossl_gcm_setiv, | ||
ossl_gcm_aad_update, | ||
ossl_gcm_cipher_update, | ||
ossl_gcm_cipher_final, | ||
ossl_gcm_one_shot | ||
}; | ||
|
||
const PROV_GCM_HW *ossl_prov_sm4_hw_gcm(size_t keybits) | ||
{ | ||
return &sm4_gcm; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters