-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsm4_ctr_sm3_hmac.c
171 lines (157 loc) · 4.05 KB
/
sm4_ctr_sm3_hmac.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm4_ctr_sm3_hmac.h>
#include <gmssl/mem.h>
#include <gmssl/error.h>
int sm4_ctr_sm3_hmac_encrypt_init(SM4_CTR_SM3_HMAC_CTX *ctx,
const uint8_t key[48], const uint8_t iv[16],
const uint8_t *aad, size_t aadlen)
{
if (!ctx || !key || !iv || (!aad && aadlen)) {
error_print();
return -1;
}
memset(ctx, 0, sizeof(*ctx));
if (sm4_ctr_encrypt_init(&ctx->enc_ctx, key, iv) != 1) {
error_print();
return -1;
}
sm3_hmac_init(&ctx->mac_ctx, key + SM4_KEY_SIZE, SM3_HMAC_SIZE);
if (aad && aadlen) {
sm3_hmac_update(&ctx->mac_ctx, aad, aadlen);
}
return 1;
}
int sm4_ctr_sm3_hmac_encrypt_update(SM4_CTR_SM3_HMAC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
{
if (!ctx || !in || !out || !outlen) {
error_print();
return -1;
}
if (sm4_ctr_encrypt_update(&ctx->enc_ctx, in, inlen, out, outlen) != 1) {
error_print();
return -1;
}
sm3_hmac_update(&ctx->mac_ctx, out, *outlen);
return 1;
}
int sm4_ctr_sm3_hmac_encrypt_finish(SM4_CTR_SM3_HMAC_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (!ctx || !out || !outlen) {
error_print();
return -1;
}
if (sm4_ctr_encrypt_finish(&ctx->enc_ctx, out, outlen) != 1) {
error_print();
return -1;
}
sm3_hmac_update(&ctx->mac_ctx, out, *outlen);
sm3_hmac_finish(&ctx->mac_ctx, out + *outlen);
*outlen += SM3_HMAC_SIZE;
return 1;
}
int sm4_ctr_sm3_hmac_decrypt_init(SM4_CTR_SM3_HMAC_CTX *ctx,
const uint8_t key[48], const uint8_t iv[16],
const uint8_t *aad, size_t aadlen)
{
if (!ctx || !key || !iv || (!aad && aadlen)) {
error_print();
return -1;
}
memset(ctx, 0, sizeof(*ctx));
if (sm4_ctr_encrypt_init(&ctx->enc_ctx, key, iv) != 1) {
error_print();
return -1;
}
sm3_hmac_init(&ctx->mac_ctx, key + SM4_KEY_SIZE, SM3_HMAC_SIZE);
if (aad && aadlen) {
sm3_hmac_update(&ctx->mac_ctx, aad, aadlen);
}
return 1;
}
int sm4_ctr_sm3_hmac_decrypt_update(SM4_CTR_SM3_HMAC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
{
size_t len;
if (!ctx || !in || !out || !outlen) {
error_print();
return -1;
}
if (ctx->maclen > SM3_HMAC_SIZE) {
error_print();
return -1;
}
if (ctx->maclen < SM3_HMAC_SIZE) {
len = SM3_HMAC_SIZE - ctx->maclen;
if (inlen <= len) {
memcpy(ctx->mac + ctx->maclen, in, inlen);
ctx->maclen += inlen;
return 1;
} else {
memcpy(ctx->mac + ctx->maclen, in, len);
ctx->maclen += len;
in += len;
inlen -= len;
}
}
if (inlen <= SM3_HMAC_SIZE) {
uint8_t tmp[SM3_HMAC_SIZE];
sm3_hmac_update(&ctx->mac_ctx, ctx->mac, inlen);
if (sm4_ctr_encrypt_update(&ctx->enc_ctx, ctx->mac, inlen, out, outlen) != 1) {
error_print();
return -1;
}
len = SM3_HMAC_SIZE - inlen;
memcpy(tmp, ctx->mac + inlen, len);
memcpy(tmp + len, in, inlen);
memcpy(ctx->mac, tmp, SM3_HMAC_SIZE);
} else {
sm3_hmac_update(&ctx->mac_ctx, ctx->mac, SM3_HMAC_SIZE);
if (sm4_ctr_encrypt_update(&ctx->enc_ctx, ctx->mac, SM3_HMAC_SIZE, out, outlen) != 1) {
error_print();
return -1;
}
out += *outlen;
inlen -= SM3_HMAC_SIZE;
sm3_hmac_update(&ctx->mac_ctx, in, inlen);
if (sm4_ctr_encrypt_update(&ctx->enc_ctx, in, inlen, out, &len) != 1) {
error_print();
return -1;
}
*outlen += len;
memcpy(ctx->mac, in + inlen, SM3_HMAC_SIZE);
}
return 1;
}
int sm4_ctr_sm3_hmac_decrypt_finish(SM4_CTR_SM3_HMAC_CTX *ctx, uint8_t *out, size_t *outlen)
{
uint8_t mac[SM3_HMAC_SIZE];
if (!ctx || !out || !outlen) {
error_print();
return -1;
}
if (ctx->maclen != SM3_HMAC_SIZE) {
error_print();
return -1;
}
sm3_hmac_finish(&ctx->mac_ctx, mac);
if (sm4_ctr_encrypt_finish(&ctx->enc_ctx, out, outlen) != 1) {
error_print();
return -1;
}
if (memcmp(mac, ctx->mac, SM3_HMAC_SIZE) != 0) {
error_print();
return -1;
}
memset(ctx->mac, 0, SM3_HMAC_SIZE);
ctx->maclen = 0;
return 1;
}