mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-11-16 17:45:04 +08:00
Update CPK sub-lib
Use simple NID as arguments, and fix some bugs
This commit is contained in:
@@ -45,7 +45,6 @@
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
@@ -53,77 +52,119 @@
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/cpk.h>
|
||||
#include "cpk_lcl.h"
|
||||
#include "../../e_os.h"
|
||||
|
||||
X509_ALGOR *CPK_MAP_new_default()
|
||||
/* Currently we only support fixed 32 indexes
|
||||
* this means EC_POINT_add() called 32 times */
|
||||
#define CPK_NUM_INDEXES 32
|
||||
|
||||
typedef struct {
|
||||
int map_nid;
|
||||
int md_nid;
|
||||
} CPK_MAP_TABLE;
|
||||
|
||||
static CPK_MAP_TABLE map_table[] = {
|
||||
{NID_cpk_map_sm3, NID_sm3},
|
||||
{NID_cpk_map_sha1, NID_sha1},
|
||||
{NID_cpk_map_sha256, NID_sha256},
|
||||
{NID_cpk_map_sha384, NID_sha384},
|
||||
{NID_cpk_map_sha512, NID_sha512}
|
||||
};
|
||||
|
||||
static const EVP_MD *cpk_map2md(int type)
|
||||
{
|
||||
X509_ALGOR *algor = NULL;
|
||||
const EVP_MD *md = EVP_sha1();
|
||||
int i;
|
||||
for (i = 0; i < OSSL_NELEM(map_table); i++) {
|
||||
if (map_table[i].map_nid == type) {
|
||||
return EVP_get_digestbynid(map_table[i].md_nid);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (md != EVP_sha1() && md != EVP_sha384()) {
|
||||
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, CPK_R_BAD_ARGUMENT);
|
||||
goto end;
|
||||
static const EVP_MD *CPK_MAP_get_md(const X509_ALGOR *algor)
|
||||
{
|
||||
const EVP_MD *md;
|
||||
if (!algor->algorithm) {
|
||||
CPKerr(CPK_F_CPK_MAP_GET_MD, CPK_R_INVALID_ARGUMENT);
|
||||
return NULL;
|
||||
}
|
||||
if (!(md = cpk_map2md(OBJ_obj2nid(algor->algorithm)))) {
|
||||
CPKerr(CPK_F_CPK_MAP_GET_MD, ERR_R_CPK_LIB);
|
||||
return NULL;
|
||||
}
|
||||
return md;
|
||||
}
|
||||
|
||||
X509_ALGOR *CPK_MAP_new(int type)
|
||||
{
|
||||
X509_ALGOR *ret = NULL;
|
||||
X509_ALGOR *algor = NULL;
|
||||
|
||||
if (!cpk_map2md(type)) {
|
||||
CPKerr(CPK_F_CPK_MAP_NEW, CPK_R_INVALID_MAP_ALGOR);
|
||||
return NULL;
|
||||
}
|
||||
if (!(algor = X509_ALGOR_new())) {
|
||||
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, ERR_R_X509_LIB);
|
||||
CPKerr(CPK_F_CPK_MAP_NEW, ERR_R_X509_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!X509_ALGOR_set0(algor, OBJ_nid2obj(EVP_MD_nid(md)),
|
||||
V_ASN1_UNDEF, NULL)) {
|
||||
X509_ALGOR_free(algor);
|
||||
algor = NULL;
|
||||
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, ERR_R_X509_LIB);
|
||||
if (!X509_ALGOR_set0(algor, OBJ_nid2obj(type), V_ASN1_UNDEF, NULL)) {
|
||||
CPKerr(CPK_F_CPK_MAP_NEW, ERR_R_X509_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = algor;
|
||||
algor = NULL;
|
||||
|
||||
end:
|
||||
return algor;
|
||||
X509_ALGOR_free(algor);
|
||||
return ret;
|
||||
}
|
||||
|
||||
X509_ALGOR *CPK_MAP_new_default(void)
|
||||
{
|
||||
return CPK_MAP_new(NID_cpk_map_sha1);
|
||||
}
|
||||
|
||||
int CPK_MAP_is_valid(const X509_ALGOR *algor)
|
||||
{
|
||||
OPENSSL_assert(algor);
|
||||
OPENSSL_assert(algor->algorithm);
|
||||
switch (OBJ_obj2nid(algor->algorithm)) {
|
||||
case NID_sha1:
|
||||
case NID_sha384:
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CPK_MAP_num_subset(const X509_ALGOR *algor)
|
||||
{
|
||||
OPENSSL_assert(algor);
|
||||
OPENSSL_assert(algor->algorithm);
|
||||
switch (OBJ_obj2nid(algor->algorithm)) {
|
||||
case NID_sha1:
|
||||
return 32;
|
||||
case NID_sha384:
|
||||
return 4096;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int CPK_MAP_num_factors(const X509_ALGOR *algor)
|
||||
{
|
||||
return 1024;
|
||||
return CPK_MAP_get_md(algor) != NULL;
|
||||
}
|
||||
|
||||
int CPK_MAP_num_indexes(const X509_ALGOR *algor)
|
||||
{
|
||||
return 32;
|
||||
if (!CPK_MAP_is_valid(algor)) {
|
||||
CPKerr(CPK_F_CPK_MAP_NUM_INDEXES, CPK_R_INVALID_MAP_ALGOR);
|
||||
return 0;
|
||||
}
|
||||
/* current only use fixed num_indexes */
|
||||
return CPK_NUM_INDEXES;
|
||||
}
|
||||
|
||||
int CPK_MAP_num_index(const X509_ALGOR *algor)
|
||||
int CPK_MAP_num_subset(const X509_ALGOR *algor)
|
||||
{
|
||||
OPENSSL_assert(algor);
|
||||
OPENSSL_assert(algor->algorithm);
|
||||
switch (OBJ_obj2nid(algor->algorithm)) {
|
||||
case NID_sha1:
|
||||
return 32;
|
||||
case NID_sha384:
|
||||
return 32;
|
||||
const EVP_MD *md;
|
||||
if (!(md = CPK_MAP_get_md(algor))) {
|
||||
CPKerr(CPK_F_CPK_MAP_NUM_SUBSET, ERR_R_CPK_LIB);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
return 1 << ((EVP_MD_size(md) * 8) / CPK_MAP_num_indexes(algor));
|
||||
}
|
||||
|
||||
int CPK_MAP_num_factors(const X509_ALGOR *algor)
|
||||
{
|
||||
int num_indexes;
|
||||
int num_subset;
|
||||
if (!(num_indexes = CPK_MAP_num_indexes(algor))) {
|
||||
CPKerr(CPK_F_CPK_MAP_NUM_FACTORS, ERR_R_CPK_LIB);
|
||||
return 0;
|
||||
}
|
||||
if (!(num_subset = CPK_MAP_num_subset(algor))) {
|
||||
CPKerr(CPK_F_CPK_MAP_NUM_FACTORS, ERR_R_CPK_LIB);
|
||||
return 0;
|
||||
}
|
||||
return num_indexes * num_subset;
|
||||
}
|
||||
|
||||
int CPK_MAP_str2index(const X509_ALGOR *algor, const char *str, int *index)
|
||||
@@ -133,46 +174,46 @@ int CPK_MAP_str2index(const X509_ALGOR *algor, const char *str, int *index)
|
||||
unsigned char dgst[EVP_MAX_MD_SIZE];
|
||||
unsigned int dgstlen;
|
||||
BIGNUM *bn = NULL;
|
||||
int i, num_index, num_subset;
|
||||
int i;
|
||||
int num_indexes, num_subset;
|
||||
|
||||
OPENSSL_assert(algor);
|
||||
OPENSSL_assert(algor->algorithm);
|
||||
OPENSSL_assert(str);
|
||||
OPENSSL_assert(strlen(str) > 0);
|
||||
|
||||
if (!CPK_MAP_is_valid(algor)) {
|
||||
if (!(md = CPK_MAP_get_md(algor))
|
||||
|| !(num_indexes = CPK_MAP_num_indexes(algor))
|
||||
|| !(num_subset = CPK_MAP_num_subset(algor))) {
|
||||
CPKerr(CPK_F_CPK_MAP_STR2INDEX, CPK_R_INVALID_MAP_ALGOR);
|
||||
goto err;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!index) {
|
||||
ret = CPK_MAP_num_index(algor);
|
||||
goto err;
|
||||
}
|
||||
if (!(md = EVP_get_digestbyobj(algor->algorithm))) {
|
||||
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_EVP_LIB);
|
||||
goto err;
|
||||
return CPK_MAP_num_indexes(algor);
|
||||
}
|
||||
|
||||
if (!EVP_Digest(str, strlen(str), dgst, &dgstlen, md, NULL)) {
|
||||
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_EVP_LIB);
|
||||
return 0;
|
||||
}
|
||||
if (!(bn = BN_new())) {
|
||||
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bin2bn(dgst, dgstlen, bn)) {
|
||||
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
goto end;
|
||||
}
|
||||
num_index = CPK_MAP_num_index(algor);
|
||||
num_subset = CPK_MAP_num_subset(algor);
|
||||
|
||||
for (i = 0; i < num_index; i++) {
|
||||
for (i = 0; i < num_indexes; i++) {
|
||||
int r = BN_mod_word(bn, num_subset);
|
||||
BN_div_word(bn, num_subset);
|
||||
index[i] = num_subset * i + r;
|
||||
}
|
||||
ret = num_index;
|
||||
err:
|
||||
if (bn) BN_free(bn);
|
||||
|
||||
ret = num_indexes;
|
||||
end:
|
||||
BN_free(bn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user