-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsm3.h
129 lines (101 loc) · 3.7 KB
/
sm3.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
Copyright © 2017 Odzhan. 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. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY AUTHORS "AS IS" AND ANY EXPRESS 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 AUTHOR 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. */
#ifndef SM3_H
#define SM3_H
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
#if defined(__INTEL_COMPILER)
#define SWAP32(v) _bswap(v)
#define SWAP64(v) _bswap64(v)
#else
#define SWAP32(v) _byteswap_ulong(v)
#define SWAP64(v) _byteswap_uint64(v)
#endif
#define memcpy(x,y,z) __movsb(x,y,z)
#define memset(x,y,z) __stosb(x,y,z)
#define ROTL32(x, y) _lrotl(x, y)
#define ROTR32(x, y) _lrotr(x, y)
#else
#define U8V(v) ((uint8_t)(v) & 0xFFU)
#define U16V(v) ((uint16_t)(v) & 0xFFFFU)
#define U32V(v) ((uint32_t)(v) & 0xFFFFFFFFUL)
#define U64V(v) ((uint64_t)(v) & 0xFFFFFFFFFFFFFFFFULL)
#define ROTL8(v, n) \
(U8V((v) << (n)) | ((v) >> (8 - (n))))
#define ROTL16(v, n) \
(U16V((v) << (n)) | ((v) >> (16 - (n))))
#define ROTL32(v, n) \
(U32V((v) << (n)) | ((v) >> (32 - (n))))
#define ROTL64(v, n) \
(U64V((v) << (n)) | ((v) >> (64 - (n))))
#define ROTR8(v, n) ROTL8(v, 8 - (n))
#define ROTR16(v, n) ROTL16(v, 16 - (n))
#define ROTR32(v, n) ROTL32(v, 32 - (n))
#define ROTR64(v, n) ROTL64(v, 64 - (n))
#define SWAP16(v) \
ROTL16(v, 8)
#define SWAP32(v) \
((ROTL32(v, 8) & 0x00FF00FFUL) | \
(ROTL32(v, 24) & 0xFF00FF00UL))
#define SWAP64(v) \
((ROTL64(v, 8) & 0x000000FF000000FFULL) | \
(ROTL64(v, 24) & 0x0000FF000000FF00ULL) | \
(ROTL64(v, 40) & 0x00FF000000FF0000ULL) | \
(ROTL64(v, 56) & 0xFF000000FF000000ULL))
#endif
#define SM3_CBLOCK 64
#define SM3_DIGEST_LENGTH 32
#define SM3_LBLOCK SM3_DIGEST_LENGTH/4
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define XCHG(x, y) (t) = (x); (x) = (y); (y) = (t);
#pragma pack(push, 1)
typedef struct _SM3_CTX {
uint64_t len;
union {
uint8_t b[SM3_DIGEST_LENGTH];
uint32_t w[SM3_DIGEST_LENGTH/4];
uint64_t q[SM3_DIGEST_LENGTH/8];
} s;
union {
uint8_t b[SM3_CBLOCK];
uint32_t w[SM3_CBLOCK/4];
uint64_t q[SM3_CBLOCK/8];
} buf;
} SM3_CTX;
#pragma pack(pop)
#ifdef __cplusplus
extern "C" {
#endif
void SM3_Init(SM3_CTX*);
void SM3_Update(SM3_CTX*, void*, uint32_t);
void SM3_Final(void*, SM3_CTX*);
void SM3_Initx(SM3_CTX*);
void SM3_Updatex(SM3_CTX*, void*, uint32_t);
void SM3_Finalx(void*, SM3_CTX*);
#ifdef __cplusplus
}
#endif
#endif