summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/sha/sha3.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/sha/sha3.c77
1 files changed, 39 insertions, 38 deletions
diff --git a/src/lib/libcrypto/sha/sha3.c b/src/lib/libcrypto/sha/sha3.c
index 6a7196d582..fde0da94ff 100644
--- a/src/lib/libcrypto/sha/sha3.c
+++ b/src/lib/libcrypto/sha/sha3.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sha3.c,v 1.16 2024/11/23 15:38:12 jsing Exp $ */ 1/* $OpenBSD: sha3.c,v 1.20 2025/04/18 07:36:11 jsing Exp $ */
2/* 2/*
3 * The MIT License (MIT) 3 * The MIT License (MIT)
4 * 4 *
@@ -26,12 +26,11 @@
26#include <endian.h> 26#include <endian.h>
27#include <string.h> 27#include <string.h>
28 28
29#include "crypto_internal.h"
29#include "sha3_internal.h" 30#include "sha3_internal.h"
30 31
31#define KECCAKF_ROUNDS 24 32#define KECCAKF_ROUNDS 24
32 33
33#define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y))))
34
35static const uint64_t sha3_keccakf_rndc[24] = { 34static const uint64_t sha3_keccakf_rndc[24] = {
36 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 35 0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
37 0x8000000080008000, 0x000000000000808b, 0x0000000080000001, 36 0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
@@ -54,7 +53,7 @@ static const int sha3_keccakf_piln[24] = {
54static void 53static void
55sha3_keccakf(uint64_t st[25]) 54sha3_keccakf(uint64_t st[25])
56{ 55{
57 uint64_t t, bc[5]; 56 uint64_t t0, t1, bc[5];
58 int i, j, r; 57 int i, j, r;
59 58
60 for (i = 0; i < 25; i++) 59 for (i = 0; i < 25; i++)
@@ -67,18 +66,18 @@ sha3_keccakf(uint64_t st[25])
67 bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20]; 66 bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20];
68 67
69 for (i = 0; i < 5; i++) { 68 for (i = 0; i < 5; i++) {
70 t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1); 69 t0 = bc[(i + 4) % 5] ^ crypto_rol_u64(bc[(i + 1) % 5], 1);
71 for (j = 0; j < 25; j += 5) 70 for (j = 0; j < 25; j += 5)
72 st[j + i] ^= t; 71 st[j + i] ^= t0;
73 } 72 }
74 73
75 /* Rho Pi */ 74 /* Rho Pi */
76 t = st[1]; 75 t0 = st[1];
77 for (i = 0; i < 24; i++) { 76 for (i = 0; i < 24; i++) {
78 j = sha3_keccakf_piln[i]; 77 j = sha3_keccakf_piln[i];
79 bc[0] = st[j]; 78 t1 = st[j];
80 st[j] = ROTL64(t, sha3_keccakf_rotc[i]); 79 st[j] = crypto_rol_u64(t0, sha3_keccakf_rotc[i]);
81 t = bc[0]; 80 t0 = t1;
82 } 81 }
83 82
84 /* Chi */ 83 /* Chi */
@@ -98,75 +97,77 @@ sha3_keccakf(uint64_t st[25])
98} 97}
99 98
100int 99int
101sha3_init(sha3_ctx *c, int mdlen) 100sha3_init(sha3_ctx *ctx, int mdlen)
102{ 101{
103 if (mdlen < 0 || mdlen >= KECCAK_BYTE_WIDTH / 2) 102 if (mdlen < 0 || mdlen >= KECCAK_BYTE_WIDTH / 2)
104 return 0; 103 return 0;
105 104
106 memset(c, 0, sizeof(*c)); 105 memset(ctx, 0, sizeof(*ctx));
107 106
108 c->mdlen = mdlen; 107 ctx->mdlen = mdlen;
109 c->rsize = KECCAK_BYTE_WIDTH - 2 * mdlen; 108 ctx->rsize = KECCAK_BYTE_WIDTH - 2 * mdlen;
110 109
111 return 1; 110 return 1;
112} 111}
113 112
114int 113int
115sha3_update(sha3_ctx *c, const void *data, size_t len) 114sha3_update(sha3_ctx *ctx, const void *_data, size_t len)
116{ 115{
116 const uint8_t *data = _data;
117 size_t i, j; 117 size_t i, j;
118 118
119 j = c->pt; 119 j = ctx->pt;
120 for (i = 0; i < len; i++) { 120 for (i = 0; i < len; i++) {
121 c->state.b[j++] ^= ((const uint8_t *) data)[i]; 121 ctx->state.b[j++] ^= data[i];
122 if (j >= c->rsize) { 122 if (j >= ctx->rsize) {
123 sha3_keccakf(c->state.q); 123 sha3_keccakf(ctx->state.q);
124 j = 0; 124 j = 0;
125 } 125 }
126 } 126 }
127 c->pt = j; 127 ctx->pt = j;
128 128
129 return 1; 129 return 1;
130} 130}
131 131
132int 132int
133sha3_final(void *md, sha3_ctx *c) 133sha3_final(void *_md, sha3_ctx *ctx)
134{ 134{
135 uint8_t *md = _md;
135 int i; 136 int i;
136 137
137 c->state.b[c->pt] ^= 0x06; 138 ctx->state.b[ctx->pt] ^= 0x06;
138 c->state.b[c->rsize - 1] ^= 0x80; 139 ctx->state.b[ctx->rsize - 1] ^= 0x80;
139 sha3_keccakf(c->state.q); 140 sha3_keccakf(ctx->state.q);
140 141
141 for (i = 0; i < c->mdlen; i++) { 142 for (i = 0; i < ctx->mdlen; i++)
142 ((uint8_t *) md)[i] = c->state.b[i]; 143 md[i] = ctx->state.b[i];
143 }
144 144
145 return 1; 145 return 1;
146} 146}
147 147
148/* SHAKE128 and SHAKE256 extensible-output functionality. */ 148/* SHAKE128 and SHAKE256 extensible-output functionality. */
149void 149void
150shake_xof(sha3_ctx *c) 150shake_xof(sha3_ctx *ctx)
151{ 151{
152 c->state.b[c->pt] ^= 0x1F; 152 ctx->state.b[ctx->pt] ^= 0x1f;
153 c->state.b[c->rsize - 1] ^= 0x80; 153 ctx->state.b[ctx->rsize - 1] ^= 0x80;
154 sha3_keccakf(c->state.q); 154 sha3_keccakf(ctx->state.q);
155 c->pt = 0; 155 ctx->pt = 0;
156} 156}
157 157
158void 158void
159shake_out(sha3_ctx *c, void *out, size_t len) 159shake_out(sha3_ctx *ctx, void *_out, size_t len)
160{ 160{
161 uint8_t *out = _out;
161 size_t i, j; 162 size_t i, j;
162 163
163 j = c->pt; 164 j = ctx->pt;
164 for (i = 0; i < len; i++) { 165 for (i = 0; i < len; i++) {
165 if (j >= c->rsize) { 166 if (j >= ctx->rsize) {
166 sha3_keccakf(c->state.q); 167 sha3_keccakf(ctx->state.q);
167 j = 0; 168 j = 0;
168 } 169 }
169 ((uint8_t *) out)[i] = c->state.b[j++]; 170 out[i] = ctx->state.b[j++];
170 } 171 }
171 c->pt = j; 172 ctx->pt = j;
172} 173}