summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2023-04-16 17:06:19 +0000
committerjsing <>2023-04-16 17:06:19 +0000
commit3142f6dc26e453ca269ab50d6c1b81f138e16f52 (patch)
tree43d762c93e59f7c4bfec4130d545027ef48f43fe /src
parent8d66484d5bc1482f15919f88c5812dc2994b81e7 (diff)
downloadopenbsd-3142f6dc26e453ca269ab50d6c1b81f138e16f52.tar.gz
openbsd-3142f6dc26e453ca269ab50d6c1b81f138e16f52.tar.bz2
openbsd-3142f6dc26e453ca269ab50d6c1b81f138e16f52.zip
Provide EVP methods for SHA3 224/256/384/512.
ok tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/evp/evp.h10
-rw-r--r--src/lib/libcrypto/evp/m_sha3.c173
-rw-r--r--src/lib/libcrypto/sha/sha3_internal.h12
3 files changed, 193 insertions, 2 deletions
diff --git a/src/lib/libcrypto/evp/evp.h b/src/lib/libcrypto/evp/evp.h
index 8b3c1d9ae7..830774a740 100644
--- a/src/lib/libcrypto/evp/evp.h
+++ b/src/lib/libcrypto/evp/evp.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: evp.h,v 1.115 2023/04/16 16:42:06 jsing Exp $ */ 1/* $OpenBSD: evp.h,v 1.116 2023/04/16 17:06:19 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -626,6 +626,14 @@ const EVP_MD *EVP_sha512_224(void);
626const EVP_MD *EVP_sha512_256(void); 626const EVP_MD *EVP_sha512_256(void);
627#endif 627#endif
628#endif 628#endif
629#ifndef OPENSSL_NO_SHA3
630#if defined(LIBRESSL_INTERNAL) || defined(LIBRESSL_NEXT_API)
631const EVP_MD *EVP_sha3_224(void);
632const EVP_MD *EVP_sha3_256(void);
633const EVP_MD *EVP_sha3_384(void);
634const EVP_MD *EVP_sha3_512(void);
635#endif
636#endif
629#ifndef OPENSSL_NO_SM3 637#ifndef OPENSSL_NO_SM3
630const EVP_MD *EVP_sm3(void); 638const EVP_MD *EVP_sm3(void);
631#endif 639#endif
diff --git a/src/lib/libcrypto/evp/m_sha3.c b/src/lib/libcrypto/evp/m_sha3.c
new file mode 100644
index 0000000000..9944ec979d
--- /dev/null
+++ b/src/lib/libcrypto/evp/m_sha3.c
@@ -0,0 +1,173 @@
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
23static int
24sha3_224_init(EVP_MD_CTX *ctx)
25{
26 return sha3_init(ctx->md_data, SHA3_224_DIGEST_LENGTH);
27}
28
29static int
30sha3_224_update(EVP_MD_CTX *ctx, const void *data, size_t count)
31{
32 return sha3_update(ctx->md_data, data, count);
33}
34
35static int
36sha3_224_final(EVP_MD_CTX *ctx, unsigned char *md)
37{
38 return sha3_final(md, ctx->md_data);
39}
40
41static 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
55const EVP_MD *
56EVP_sha3_224(void)
57{
58 return &sha3_224_md;
59}
60
61static int
62sha3_256_init(EVP_MD_CTX *ctx)
63{
64 return sha3_init(ctx->md_data, SHA3_256_DIGEST_LENGTH);
65}
66
67static int
68sha3_256_update(EVP_MD_CTX *ctx, const void *data, size_t count)
69{
70 return sha3_update(ctx->md_data, data, count);
71}
72
73static int
74sha3_256_final(EVP_MD_CTX *ctx, unsigned char *md)
75{
76 return sha3_final(md, ctx->md_data);
77}
78
79static 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
93const EVP_MD *
94EVP_sha3_256(void)
95{
96 return &sha3_256_md;
97}
98
99static int
100sha3_384_init(EVP_MD_CTX *ctx)
101{
102 return sha3_init(ctx->md_data, SHA3_384_DIGEST_LENGTH);
103}
104
105static int
106sha3_384_update(EVP_MD_CTX *ctx, const void *data, size_t count)
107{
108 return sha3_update(ctx->md_data, data, count);
109}
110
111static int
112sha3_384_final(EVP_MD_CTX *ctx, unsigned char *md)
113{
114 return sha3_final(md, ctx->md_data);
115}
116
117static 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
131const EVP_MD *
132EVP_sha3_384(void)
133{
134 return &sha3_384_md;
135}
136
137static int
138sha3_512_init(EVP_MD_CTX *ctx)
139{
140 return sha3_init(ctx->md_data, SHA3_512_DIGEST_LENGTH);
141}
142
143static int
144sha3_512_update(EVP_MD_CTX *ctx, const void *data, size_t count)
145{
146 return sha3_update(ctx->md_data, data, count);
147}
148
149static int
150sha3_512_final(EVP_MD_CTX *ctx, unsigned char *md)
151{
152 return sha3_final(md, ctx->md_data);
153}
154
155static 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
169const EVP_MD *
170EVP_sha3_512(void)
171{
172 return &sha3_512_md;
173}
diff --git a/src/lib/libcrypto/sha/sha3_internal.h b/src/lib/libcrypto/sha/sha3_internal.h
index 91b1a43b25..d6fe3b8345 100644
--- a/src/lib/libcrypto/sha/sha3_internal.h
+++ b/src/lib/libcrypto/sha/sha3_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: sha3_internal.h,v 1.13 2023/04/15 20:00:24 jsing Exp $ */ 1/* $OpenBSD: sha3_internal.h,v 1.14 2023/04/16 17:06:19 jsing Exp $ */
2/* 2/*
3 * The MIT License (MIT) 3 * The MIT License (MIT)
4 * 4 *
@@ -29,6 +29,16 @@
29#ifndef HEADER_SHA3_INTERNAL_H 29#ifndef HEADER_SHA3_INTERNAL_H
30#define HEADER_SHA3_INTERNAL_H 30#define HEADER_SHA3_INTERNAL_H
31 31
32#define NID_sha3_224 1031
33#define NID_sha3_256 1032
34#define NID_sha3_384 1033
35#define NID_sha3_512 1034
36
37#define NID_RSA_SHA3_224 1049
38#define NID_RSA_SHA3_256 1050
39#define NID_RSA_SHA3_384 1051
40#define NID_RSA_SHA3_512 1052
41
32#define KECCAK_BIT_WIDTH 1600 42#define KECCAK_BIT_WIDTH 1600
33#define KECCAK_BYTE_WIDTH (KECCAK_BIT_WIDTH / 8) 43#define KECCAK_BYTE_WIDTH (KECCAK_BIT_WIDTH / 8)
34 44