xref: /openbsd-src/lib/libcrypto/evp/m_sha3.c (revision 9bac3682a248384cde711fdda825c3b5960b1005)
1 /* $OpenBSD: m_sha3.c,v 1.4 2024/04/09 13:52:41 beck 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
sha3_224_init(EVP_MD_CTX * ctx)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
sha3_224_update(EVP_MD_CTX * ctx,const void * data,size_t count)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
sha3_224_final(EVP_MD_CTX * ctx,unsigned char * md)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 *
EVP_sha3_224(void)56 EVP_sha3_224(void)
57 {
58 	return &sha3_224_md;
59 }
60 LCRYPTO_ALIAS(EVP_sha3_224);
61 
62 static int
sha3_256_init(EVP_MD_CTX * ctx)63 sha3_256_init(EVP_MD_CTX *ctx)
64 {
65 	return sha3_init(ctx->md_data, SHA3_256_DIGEST_LENGTH);
66 }
67 
68 static int
sha3_256_update(EVP_MD_CTX * ctx,const void * data,size_t count)69 sha3_256_update(EVP_MD_CTX *ctx, const void *data, size_t count)
70 {
71 	return sha3_update(ctx->md_data, data, count);
72 }
73 
74 static int
sha3_256_final(EVP_MD_CTX * ctx,unsigned char * md)75 sha3_256_final(EVP_MD_CTX *ctx, unsigned char *md)
76 {
77 	return sha3_final(md, ctx->md_data);
78 }
79 
80 static const EVP_MD sha3_256_md = {
81 	.type = NID_sha3_256,
82 	.pkey_type = NID_RSA_SHA3_256,
83 	.md_size = SHA3_256_DIGEST_LENGTH,
84 	.flags = EVP_MD_FLAG_DIGALGID_ABSENT,
85 	.init = sha3_256_init,
86 	.update = sha3_256_update,
87 	.final = sha3_256_final,
88 	.copy = NULL,
89 	.cleanup = NULL,
90 	.block_size = SHA3_256_BLOCK_SIZE,
91 	.ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx),
92 };
93 
94 const EVP_MD *
EVP_sha3_256(void)95 EVP_sha3_256(void)
96 {
97 	return &sha3_256_md;
98 }
99 LCRYPTO_ALIAS(EVP_sha3_256);
100 
101 static int
sha3_384_init(EVP_MD_CTX * ctx)102 sha3_384_init(EVP_MD_CTX *ctx)
103 {
104 	return sha3_init(ctx->md_data, SHA3_384_DIGEST_LENGTH);
105 }
106 
107 static int
sha3_384_update(EVP_MD_CTX * ctx,const void * data,size_t count)108 sha3_384_update(EVP_MD_CTX *ctx, const void *data, size_t count)
109 {
110 	return sha3_update(ctx->md_data, data, count);
111 }
112 
113 static int
sha3_384_final(EVP_MD_CTX * ctx,unsigned char * md)114 sha3_384_final(EVP_MD_CTX *ctx, unsigned char *md)
115 {
116 	return sha3_final(md, ctx->md_data);
117 }
118 
119 static const EVP_MD sha3_384_md = {
120 	.type = NID_sha3_384,
121 	.pkey_type = NID_RSA_SHA3_384,
122 	.md_size = SHA3_384_DIGEST_LENGTH,
123 	.flags = EVP_MD_FLAG_DIGALGID_ABSENT,
124 	.init = sha3_384_init,
125 	.update = sha3_384_update,
126 	.final = sha3_384_final,
127 	.copy = NULL,
128 	.cleanup = NULL,
129 	.block_size = SHA3_384_BLOCK_SIZE,
130 	.ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx),
131 };
132 
133 const EVP_MD *
EVP_sha3_384(void)134 EVP_sha3_384(void)
135 {
136 	return &sha3_384_md;
137 }
138 LCRYPTO_ALIAS(EVP_sha3_384);
139 
140 static int
sha3_512_init(EVP_MD_CTX * ctx)141 sha3_512_init(EVP_MD_CTX *ctx)
142 {
143 	return sha3_init(ctx->md_data, SHA3_512_DIGEST_LENGTH);
144 }
145 
146 static int
sha3_512_update(EVP_MD_CTX * ctx,const void * data,size_t count)147 sha3_512_update(EVP_MD_CTX *ctx, const void *data, size_t count)
148 {
149 	return sha3_update(ctx->md_data, data, count);
150 }
151 
152 static int
sha3_512_final(EVP_MD_CTX * ctx,unsigned char * md)153 sha3_512_final(EVP_MD_CTX *ctx, unsigned char *md)
154 {
155 	return sha3_final(md, ctx->md_data);
156 }
157 
158 static const EVP_MD sha3_512_md = {
159 	.type = NID_sha3_512,
160 	.pkey_type = NID_RSA_SHA3_512,
161 	.md_size = SHA3_512_DIGEST_LENGTH,
162 	.flags = EVP_MD_FLAG_DIGALGID_ABSENT,
163 	.init = sha3_512_init,
164 	.update = sha3_512_update,
165 	.final = sha3_512_final,
166 	.copy = NULL,
167 	.cleanup = NULL,
168 	.block_size = SHA3_512_BLOCK_SIZE,
169 	.ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx),
170 };
171 
172 const EVP_MD *
EVP_sha3_512(void)173 EVP_sha3_512(void)
174 {
175 	return &sha3_512_md;
176 }
177 LCRYPTO_ALIAS(EVP_sha3_512);
178