xref: /openbsd-src/lib/libcrypto/evp/m_sha3.c (revision ff0e7be1ebbcc809ea8ad2b6dafe215824da9e46)
1 /* $OpenBSD: m_sha3.c,v 1.1 2023/04/16 17:06:19 jsing Exp $ */
2 /*
3  * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <openssl/evp.h>
19 
20 #include "evp_local.h"
21 #include "sha3_internal.h"
22 
23 static int
24 sha3_224_init(EVP_MD_CTX *ctx)
25 {
26 	return sha3_init(ctx->md_data, SHA3_224_DIGEST_LENGTH);
27 }
28 
29 static int
30 sha3_224_update(EVP_MD_CTX *ctx, const void *data, size_t count)
31 {
32 	return sha3_update(ctx->md_data, data, count);
33 }
34 
35 static int
36 sha3_224_final(EVP_MD_CTX *ctx, unsigned char *md)
37 {
38 	return sha3_final(md, ctx->md_data);
39 }
40 
41 static const EVP_MD sha3_224_md = {
42 	.type = NID_sha3_224,
43 	.pkey_type = NID_RSA_SHA3_224,
44 	.md_size = SHA3_224_DIGEST_LENGTH,
45 	.flags = EVP_MD_FLAG_DIGALGID_ABSENT,
46 	.init = sha3_224_init,
47 	.update = sha3_224_update,
48 	.final = sha3_224_final,
49 	.copy = NULL,
50 	.cleanup = NULL,
51 	.block_size = SHA3_224_BLOCK_SIZE,
52 	.ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx),
53 };
54 
55 const EVP_MD *
56 EVP_sha3_224(void)
57 {
58 	return &sha3_224_md;
59 }
60 
61 static int
62 sha3_256_init(EVP_MD_CTX *ctx)
63 {
64 	return sha3_init(ctx->md_data, SHA3_256_DIGEST_LENGTH);
65 }
66 
67 static int
68 sha3_256_update(EVP_MD_CTX *ctx, const void *data, size_t count)
69 {
70 	return sha3_update(ctx->md_data, data, count);
71 }
72 
73 static int
74 sha3_256_final(EVP_MD_CTX *ctx, unsigned char *md)
75 {
76 	return sha3_final(md, ctx->md_data);
77 }
78 
79 static const EVP_MD sha3_256_md = {
80 	.type = NID_sha3_256,
81 	.pkey_type = NID_RSA_SHA3_256,
82 	.md_size = SHA3_256_DIGEST_LENGTH,
83 	.flags = EVP_MD_FLAG_DIGALGID_ABSENT,
84 	.init = sha3_256_init,
85 	.update = sha3_256_update,
86 	.final = sha3_256_final,
87 	.copy = NULL,
88 	.cleanup = NULL,
89 	.block_size = SHA3_256_BLOCK_SIZE,
90 	.ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx),
91 };
92 
93 const EVP_MD *
94 EVP_sha3_256(void)
95 {
96 	return &sha3_256_md;
97 }
98 
99 static int
100 sha3_384_init(EVP_MD_CTX *ctx)
101 {
102 	return sha3_init(ctx->md_data, SHA3_384_DIGEST_LENGTH);
103 }
104 
105 static int
106 sha3_384_update(EVP_MD_CTX *ctx, const void *data, size_t count)
107 {
108 	return sha3_update(ctx->md_data, data, count);
109 }
110 
111 static int
112 sha3_384_final(EVP_MD_CTX *ctx, unsigned char *md)
113 {
114 	return sha3_final(md, ctx->md_data);
115 }
116 
117 static const EVP_MD sha3_384_md = {
118 	.type = NID_sha3_384,
119 	.pkey_type = NID_RSA_SHA3_384,
120 	.md_size = SHA3_384_DIGEST_LENGTH,
121 	.flags = EVP_MD_FLAG_DIGALGID_ABSENT,
122 	.init = sha3_384_init,
123 	.update = sha3_384_update,
124 	.final = sha3_384_final,
125 	.copy = NULL,
126 	.cleanup = NULL,
127 	.block_size = SHA3_384_BLOCK_SIZE,
128 	.ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx),
129 };
130 
131 const EVP_MD *
132 EVP_sha3_384(void)
133 {
134 	return &sha3_384_md;
135 }
136 
137 static int
138 sha3_512_init(EVP_MD_CTX *ctx)
139 {
140 	return sha3_init(ctx->md_data, SHA3_512_DIGEST_LENGTH);
141 }
142 
143 static int
144 sha3_512_update(EVP_MD_CTX *ctx, const void *data, size_t count)
145 {
146 	return sha3_update(ctx->md_data, data, count);
147 }
148 
149 static int
150 sha3_512_final(EVP_MD_CTX *ctx, unsigned char *md)
151 {
152 	return sha3_final(md, ctx->md_data);
153 }
154 
155 static const EVP_MD sha3_512_md = {
156 	.type = NID_sha3_512,
157 	.pkey_type = NID_RSA_SHA3_512,
158 	.md_size = SHA3_512_DIGEST_LENGTH,
159 	.flags = EVP_MD_FLAG_DIGALGID_ABSENT,
160 	.init = sha3_512_init,
161 	.update = sha3_512_update,
162 	.final = sha3_512_final,
163 	.copy = NULL,
164 	.cleanup = NULL,
165 	.block_size = SHA3_512_BLOCK_SIZE,
166 	.ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx),
167 };
168 
169 const EVP_MD *
170 EVP_sha3_512(void)
171 {
172 	return &sha3_512_md;
173 }
174