-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathgost_digest_ctx.c
More file actions
162 lines (121 loc) · 3.38 KB
/
gost_digest_ctx.c
File metadata and controls
162 lines (121 loc) · 3.38 KB
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
#include "gost_digest.h"
#include "gost_digest_details.h"
#include <stdbool.h>
#include <openssl/evp.h>
struct gost_digest_ctx_st {
const GOST_digest* cls;
void* algctx;
unsigned long flags;
void* allocated_self;
};
size_t GOST_digest_ctx_size = sizeof(GOST_digest_ctx);
void* GOST_digest_ctx_data(const GOST_digest_ctx* ctx) {
return ctx->algctx;
}
void GOST_digest_ctx_set_flags(GOST_digest_ctx *ctx, unsigned long flags)
{
ctx->flags |= flags;
}
void GOST_digest_ctx_reset_flags(GOST_digest_ctx *ctx, unsigned long flags)
{
ctx->flags &= ~flags;
}
int GOST_digest_ctx_test_flags(const GOST_digest_ctx *ctx, unsigned long flags)
{
return (ctx->flags & flags);
}
GOST_digest_ctx* GOST_digest_ctx_new() {
void* buf = OPENSSL_zalloc(sizeof(GOST_digest_ctx));
if (!buf) {
return NULL;
}
GOST_digest_ctx* ctx = buf;
ctx->allocated_self = buf;
return ctx;
}
static bool GOST_digest_ctx_initialized(const GOST_digest_ctx *ctx) {
return ctx && ctx->cls && ctx->algctx;
}
void GOST_digest_ctx_free(GOST_digest_ctx *ctx)
{
if (!ctx)
return;
if (ctx->cls && ctx->algctx) {
ctx->cls->cleanup(ctx);
}
OPENSSL_free(ctx->algctx);
ctx->algctx = NULL;
OPENSSL_free(ctx->allocated_self);
}
int GOST_digest_ctx_init(GOST_digest_ctx *ctx, const GOST_digest *cls) {
if (!ctx) {
return 0;
}
if (GOST_digest_ctx_test_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT)) {
return 1;
}
if (ctx->cls && ctx->algctx && !GOST_digest_ctx_cleanup(ctx)) {
return 0;
}
ctx->cls = cls;
ctx->algctx = OPENSSL_zalloc(ctx->cls->algctx_size);
if (ctx->cls->algctx_size && !ctx->algctx) {
return 0;
}
int r = ctx->cls->init(ctx);
if (!r) {
OPENSSL_free(ctx->algctx);
ctx->algctx = NULL;
}
return r;
}
int GOST_digest_ctx_update(GOST_digest_ctx *ctx, const void *data, size_t count) {
if (!GOST_digest_ctx_initialized(ctx))
return 0;
return ctx->cls->update(ctx, data, count);
}
int GOST_digest_ctx_final(GOST_digest_ctx *ctx, unsigned char *md) {
if (!GOST_digest_ctx_initialized(ctx))
return 0;
return ctx->cls->final(ctx, md);
}
int GOST_digest_ctx_copy(GOST_digest_ctx *to, const GOST_digest_ctx *from) {
if (!to || !from || to == from) {
return 0;
}
if (!GOST_digest_ctx_initialized(from)) {
return 0;
}
if (GOST_digest_ctx_initialized(to)
&& GOST_digest_ctx_data(to) == GOST_digest_ctx_data(from)) {
to->algctx = OPENSSL_zalloc(to->cls->algctx_size);
if (to->cls->algctx_size && !to->algctx) {
return 0;
}
}
to->flags = 0;
if (!GOST_digest_ctx_init(to, from->cls)) {
return 0;
}
to->flags = from->flags;
return to->cls->copy(to, from);
}
int GOST_digest_ctx_cleanup(GOST_digest_ctx *ctx) {
if (!GOST_digest_ctx_initialized(ctx))
return 0;
ctx->flags = 0;
int r = ctx->cls->cleanup(ctx);
OPENSSL_free(ctx->algctx);
ctx->algctx = NULL;
return r;
}
int GOST_digest_ctx_ctrl(GOST_digest_ctx *ctx, int cmd, int p1, void *p2) {
if (!GOST_digest_ctx_initialized(ctx))
return 0;
return ctx->cls->ctrl(ctx, cmd, p1, p2);
}
const GOST_digest* GOST_digest_ctx_digest(GOST_digest_ctx *ctx) {
if (!GOST_digest_ctx_initialized(ctx))
return NULL;
return ctx->cls;
}