]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_md2012.c
Start converting the engine to provider
[openssl-gost/engine.git] / gost_md2012.c
1 #include <openssl/core.h>
2 #include <openssl/core_numbers.h>
3 #include <openssl/params.h>
4
5 #include "gost_prov.h"
6 #include "gosthash2012.h"
7
8 const char micalg_256[] = "gostr3411-2012-256";
9 const char micalg_512[] = "gostr3411-2012-512";
10
11 /* Context management */
12 static void *STREEBOG256_newctx(void *provctx);
13 static void STREEBOG_freectx(void *dctx);
14 static void *STREEBOG_dupctx(void *dctx);
15
16 /* Digest generation */
17 static int STREEBOG256_digest_init(void *dctx);
18 static int STREEBOG_digest_update(void *dctx, const unsigned char *in, size_t inl);
19 static int STREEBOG_digest_final(void *dctx, unsigned char *out, size_t *outl,
20                     size_t outsz);
21
22 /* Digest parameter descriptors */
23 static const OSSL_PARAM *STREEBOG_gettable_params(void);
24 static int STREEBOG256_digest_get_params(OSSL_PARAM params[]);
25
26 OSSL_DISPATCH streebog256_funcs[] = {
27         { OSSL_FUNC_DIGEST_NEWCTX, (funcptr_t)STREEBOG256_newctx },
28         { OSSL_FUNC_DIGEST_FREECTX, (funcptr_t)STREEBOG_freectx },
29         { OSSL_FUNC_DIGEST_DUPCTX, (funcptr_t)STREEBOG_dupctx },
30
31         { OSSL_FUNC_DIGEST_INIT, (funcptr_t)STREEBOG256_digest_init },
32         { OSSL_FUNC_DIGEST_UPDATE, (funcptr_t)STREEBOG_digest_update },
33         { OSSL_FUNC_DIGEST_FINAL, (funcptr_t)STREEBOG_digest_final },
34
35         { OSSL_FUNC_DIGEST_GETTABLE_PARAMS, (funcptr_t)STREEBOG_gettable_params },
36         { OSSL_FUNC_DIGEST_GET_PARAMS, (funcptr_t)STREEBOG256_digest_get_params },
37
38         { 0, NULL },
39 };
40
41 static void *STREEBOG256_newctx(void *provctx)
42 {
43         gost2012_hash_ctx *pctx = OPENSSL_zalloc(sizeof(gost2012_hash_ctx));
44         return pctx;
45 }
46
47 static void STREEBOG_freectx(void *dctx)
48 {
49         OPENSSL_free(dctx);
50 }
51
52 static void *STREEBOG_dupctx(void *dctx) 
53 {
54         gost2012_hash_ctx *pctx = OPENSSL_zalloc(sizeof(gost2012_hash_ctx));
55         if (pctx == NULL)
56                 return NULL;
57         
58         if (pctx)
59                 memcpy(pctx, dctx, sizeof(gost2012_hash_ctx));
60         
61         return pctx;
62 }
63
64 static int STREEBOG256_digest_init(void *dctx)
65 {
66         init_gost2012_hash_ctx((gost2012_hash_ctx *)dctx, 256);
67         return 1;
68 }
69
70 static int STREEBOG_digest_update(void *dctx, const unsigned char *in, size_t inl)
71 {
72     gost2012_hash_block((gost2012_hash_ctx *)dctx, in, inl);
73     return 1;
74 }
75
76 static int STREEBOG_digest_final(void *dctx, unsigned char *out, size_t *outl,
77                     size_t outsz)
78 {
79         gost2012_hash_ctx *pctx = (gost2012_hash_ctx *)dctx;
80
81         if (pctx->digest_size/8 > outsz)
82                 return 0;
83
84         gost2012_finish_hash(pctx, out);
85         *outl = pctx->digest_size/8;
86         return 1;
87 }
88
89 static const OSSL_PARAM *STREEBOG_gettable_params(void)
90 {
91     static const OSSL_PARAM table[] = {
92         OSSL_PARAM_size_t("blocksize", NULL),
93         OSSL_PARAM_size_t("size", NULL),
94   /*      OSSL_PARAM_utf8_ptr("micalg", NULL, strlen(micalg_256)+1), */
95         OSSL_PARAM_END
96     };
97
98     return table;
99 }
100
101 static int STREEBOG256_digest_get_params(OSSL_PARAM params[])
102 {
103     OSSL_PARAM *p;
104
105     if ((p = OSSL_PARAM_locate(params, "blocksize")) != NULL)
106         if (!OSSL_PARAM_set_size_t(p, 64))
107             return 0;
108     if ((p = OSSL_PARAM_locate(params, "size")) != NULL)
109         if (!OSSL_PARAM_set_size_t(p, 32))
110             return 0;
111 /*    if ((p = OSSL_PARAM_locate(params, "micalg")) != NULL)
112         if (!OSSL_PARAM_set_utf8_ptr(p, micalg_256))
113             return 0; */
114     return 1;
115 }