Skip to content
This repository has been archived by the owner on Sep 25, 2018. It is now read-only.

Commit

Permalink
SM3: restructure to EVP internal and update doc to right location
Browse files Browse the repository at this point in the history
Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Richard Levitte <[email protected]>
(Merged from openssl#4616)
  • Loading branch information
ronaldtse committed Nov 5, 2017
1 parent a0c3e4f commit 67e247f
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 160 deletions.
6 changes: 3 additions & 3 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,9 @@
Build without support for the specified algorithm, where
<alg> is one of: bf, blake2, camellia, cast, chacha, cmac,
des, dh, dsa, ecdh, ecdsa, idea, md4, mdc2, ocb, poly1305,
rc2, rc4, rmd160, scrypt, seed, siphash, sm3, sm4 or
whirlpool. The "ripemd" algorithm is deprecated and if used
is synonymous with rmd160.
rc2, rc4, rmd160, scrypt, seed, siphash, sm3, sm4 or
whirlpool. The "ripemd" algorithm is deprecated and if used
is synonymous with rmd160.

-Dxxx, lxxx, -Lxxx, -Wl, -rpath, -R, -framework, -static
These system specific options will be recognised and
Expand Down
2 changes: 1 addition & 1 deletion crypto/evp/build.info
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SOURCE[../../libcrypto]=\
e_rc4.c e_aes.c names.c e_seed.c e_aria.c e_sm4.c \
e_xcbc_d.c e_rc2.c e_cast.c e_rc5.c \
m_null.c m_md2.c m_md4.c m_md5.c m_sha1.c m_wp.c \
m_md5_sha1.c m_mdc2.c m_ripemd.c m_sha3.c m_sm3.c \
m_md5_sha1.c m_mdc2.c m_ripemd.c m_sha3.c \
p_open.c p_seal.c p_sign.c p_verify.c p_lib.c p_enc.c p_dec.c \
bio_md.c bio_b64.c bio_enc.c evp_err.c e_null.c \
c_allc.c c_alld.c evp_lib.c bio_ok.c \
Expand Down
22 changes: 7 additions & 15 deletions include/openssl/sm3.h → crypto/include/internal/sm3.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 2017 [Ribose Inc.](https://www.ribose.com). All Rights Reserved.
* Copyright 2017 Ribose Inc. 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
Expand All @@ -17,12 +17,8 @@
# error SM3 is disabled.
# endif

# ifdef __cplusplus
extern "C" {
# endif

#define SM3_DIGEST_LENGTH 32
#define SM3_WORD unsigned int
# define SM3_DIGEST_LENGTH 32
# define SM3_WORD unsigned int

# define SM3_CBLOCK 64
# define SM3_LBLOCK (SM3_CBLOCK/4)
Expand All @@ -34,14 +30,10 @@ typedef struct SM3state_st {
unsigned int num;
} SM3_CTX;

int SM3_Init(SM3_CTX *c);
int SM3_Update(SM3_CTX *c, const void *data, size_t len);
int SM3_Final(unsigned char *md, SM3_CTX *c);
void SM3_Transform(SM3_CTX *c, const unsigned char *data);
unsigned char *SM3(const unsigned char *d, size_t n, unsigned char *md);
int sm3_init(SM3_CTX *c);
int sm3_update(SM3_CTX *c, const void *data, size_t len);
int sm3_final(unsigned char *md, SM3_CTX *c);

# ifdef __cplusplus
}
# endif
void sm3_block_data_order(SM3_CTX *c, const void *p, size_t num);

#endif
2 changes: 1 addition & 1 deletion crypto/sm3/build.info
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=sm3.c
SOURCE[../../libcrypto]=sm3.c m_sm3.c
13 changes: 5 additions & 8 deletions crypto/evp/m_sm3.c → crypto/sm3/m_sm3.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,26 @@
* https://www.openssl.org/source/license.html
*/

#include <stdio.h>
#include "internal/cryptlib.h"

#ifndef OPENSSL_NO_SM3

# include <openssl/evp.h>
# include <openssl/objects.h>
# include <openssl/sm3.h>
# include "internal/evp_int.h"
# include "internal/sm3.h"

static int init(EVP_MD_CTX *ctx)
{
return SM3_Init(EVP_MD_CTX_md_data(ctx));
return sm3_init(EVP_MD_CTX_md_data(ctx));
}

static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
{
return SM3_Update(EVP_MD_CTX_md_data(ctx), data, count);
return sm3_update(EVP_MD_CTX_md_data(ctx), data, count);
}

static int final(EVP_MD_CTX *ctx, unsigned char *md)
{
return SM3_Final(md, EVP_MD_CTX_md_data(ctx));
return sm3_final(md, EVP_MD_CTX_md_data(ctx));
}

static const EVP_MD sm3_md = {
Expand All @@ -51,5 +48,5 @@ const EVP_MD *EVP_sm3(void)
{
return &sm3_md;
}
#endif

#endif
25 changes: 3 additions & 22 deletions crypto/sm3/sm3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
* https://www.openssl.org/source/license.html
*/

#include <stdio.h>

#ifndef OPENSSL_NO_SM3

#include <openssl/e_os2.h>
#include "sm3_locl.h"
#include <openssl/opensslv.h>

int SM3_Init(SM3_CTX *c)
int sm3_init(SM3_CTX *c)
{
memset(c, 0, sizeof(*c));
c->A = SM3_A;
Expand All @@ -30,21 +26,6 @@ int SM3_Init(SM3_CTX *c)
return 1;
}

unsigned char *SM3(const unsigned char *d, size_t n, unsigned char *md)
{
SM3_CTX c;
static unsigned char m[SM3_DIGEST_LENGTH];

if (md == NULL)
md = m;
if (!SM3_Init(&c))
return NULL;
SM3_Update(&c, d, n);
SM3_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c)); /* security consideration */
return md;
}

void sm3_block_data_order(SM3_CTX *ctx, const void *p, size_t num)
{
const unsigned char *data = p;
Expand Down Expand Up @@ -212,4 +193,4 @@ void sm3_block_data_order(SM3_CTX *ctx, const void *p, size_t num)
ctx->H ^= H;
}
}
#endif

60 changes: 30 additions & 30 deletions crypto/sm3/sm3_locl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,33 @@
* https://www.openssl.org/source/license.html
*/

#include <stdlib.h>
#include <string.h>
#include <openssl/e_os2.h>
#include <openssl/sm3.h>

void sm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
#include "internal/sm3.h"

#define DATA_ORDER_IS_BIG_ENDIAN

#define HASH_LONG SM3_WORD
#define HASH_CTX SM3_CTX
#define HASH_CBLOCK SM3_CBLOCK
#define HASH_UPDATE SM3_Update
#define HASH_TRANSFORM SM3_Transform
#define HASH_FINAL SM3_Final
#define HASH_MAKE_STRING(c,s) do { \
unsigned long ll; \
ll=(c)->A; (void)HOST_l2c(ll,(s)); \
ll=(c)->B; (void)HOST_l2c(ll,(s)); \
ll=(c)->C; (void)HOST_l2c(ll,(s)); \
ll=(c)->D; (void)HOST_l2c(ll,(s)); \
ll=(c)->E; (void)HOST_l2c(ll,(s)); \
ll=(c)->F; (void)HOST_l2c(ll,(s)); \
ll=(c)->G; (void)HOST_l2c(ll,(s)); \
ll=(c)->H; (void)HOST_l2c(ll,(s)); \
} while (0)
#define HASH_UPDATE sm3_update
#define HASH_TRANSFORM sm3_transform
#define HASH_FINAL sm3_final
#define HASH_MAKE_STRING(c, s) \
do { \
unsigned long ll; \
ll=(c)->A; (void)HOST_l2c(ll, (s)); \
ll=(c)->B; (void)HOST_l2c(ll, (s)); \
ll=(c)->C; (void)HOST_l2c(ll, (s)); \
ll=(c)->D; (void)HOST_l2c(ll, (s)); \
ll=(c)->E; (void)HOST_l2c(ll, (s)); \
ll=(c)->F; (void)HOST_l2c(ll, (s)); \
ll=(c)->G; (void)HOST_l2c(ll, (s)); \
ll=(c)->H; (void)HOST_l2c(ll, (s)); \
} while (0)
#define HASH_BLOCK_DATA_ORDER sm3_block_data_order

void sm3_transform(SM3_CTX *c, const unsigned char *data);

#include "internal/md32_common.h"

#define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17))
Expand All @@ -51,17 +50,18 @@ void sm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
#define EXPAND(W0,W7,W13,W3,W10) \
(P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10)

#define RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF,GG) do { \
const SM3_WORD A12 = ROTATE(A, 12); \
const SM3_WORD A12_SM = A12 + E + TJ; \
const SM3_WORD SS1 = ROTATE(A12_SM, 7); \
const SM3_WORD TT1 = FF(A,B,C) + D + (SS1 ^ A12) + (Wj); \
const SM3_WORD TT2 = GG(E,F,G) + H + SS1 + Wi; \
B = ROTATE(B, 9); \
D = TT1; \
F = ROTATE(F, 19); \
H = P0(TT2); \
} while(0);
#define RND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF, GG) \
do { \
const SM3_WORD A12 = ROTATE(A, 12); \
const SM3_WORD A12_SM = A12 + E + TJ; \
const SM3_WORD SS1 = ROTATE(A12_SM, 7); \
const SM3_WORD TT1 = FF(A, B, C) + D + (SS1 ^ A12) + (Wj); \
const SM3_WORD TT2 = GG(E, F, G) + H + SS1 + Wi; \
B = ROTATE(B, 9); \
D = TT1; \
F = ROTATE(F, 19); \
H = P0(TT2); \
} while(0)

#define R1(A,B,C,D,E,F,G,H,TJ,Wi,Wj) \
RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF0,GG0)
Expand Down
1 change: 1 addition & 0 deletions doc/man3/EVP_DigestInit.pod
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ L<EVP_ripemd160(3)>,
L<EVP_sha1(3)>,
L<EVP_sha224(3)>,
L<EVP_sha3_224(3)>,
L<EVP_sm3(3)>,
L<EVP_whirlpool(3)>

=head1 HISTORY
Expand Down
54 changes: 54 additions & 0 deletions doc/man3/EVP_sm3.pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
=pod

=head1 NAME

EVP_sm3
- SM3 for EVP

=head1 SYNOPSIS

#include <openssl/evp.h>

const EVP_MD *EVP_sm3(void)

=head1 DESCRIPTION

SM3 is a cryptographic hash function with a 256-bit output, defined in GB/T
32905-2016.

=over 4

=item EVP_sm3()

The SM3 hash function.

=back


=head1 RETURN VALUES

These functions return a B<EVP_MD> structure that contains the
implementation of the symmetric cipher. See L<EVP_MD_meth_new(3)> for
details of the B<EVP_MD> structure.

=head1 CONFORMING TO

GB/T 32905-2016 and GM/T 0004-2012.

=head1 SEE ALSO

L<evp(7)>,
L<EVP_DigestInit(3)>

=head1 COPYRIGHT

Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
Copyright 2017 Ribose Inc. 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
L<https://www.openssl.org/source/license.html>.

=cut

76 changes: 0 additions & 76 deletions doc/man3/SM3.pod

This file was deleted.

2 changes: 2 additions & 0 deletions fuzz/oids.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1015,3 +1015,5 @@ OBJ_sm4_ctr="\x2A\x81\x1C\xCF\x55\x01\x68\x07"
OBJ_ISO_CN="\x2A\x81\x1C"
OBJ_oscca="\x2A\x81\x1C\xCF\x55"
OBJ_sm_scheme="\x2A\x81\x1C\xCF\x55\x01"
OBJ_sm3="\x2A\x81\x1C\xCF\x55\x01\x83\x11"
OBJ_sm3WithRSAEncryption="\x2A\x81\x1C\xCF\x55\x01\x83\x78"
4 changes: 0 additions & 4 deletions util/libcrypto.num
Original file line number Diff line number Diff line change
Expand Up @@ -4426,7 +4426,3 @@ EVP_sm4_ofb 4370 1_1_1 EXIST::FUNCTION:SM4
EVP_sm4_ecb 4371 1_1_1 EXIST::FUNCTION:SM4
EVP_sm4_cfb128 4372 1_1_1 EXIST::FUNCTION:SM4
EVP_sm3 4373 1_1_1 EXIST::FUNCTION:SM3
SM3_Update 4374 1_1_1 EXIST::FUNCTION:SM3
SM3 4375 1_1_1 EXIST::FUNCTION:SM3
SM3_Init 4376 1_1_1 EXIST::FUNCTION:SM3
SM3_Final 4377 1_1_1 EXIST::FUNCTION:SM3

0 comments on commit 67e247f

Please sign in to comment.