target/mips: add Octeon HSH COP2 helpers
Add helper support for the Octeon HSH hash selectors. This includes the base HSH data/IV windows, MD5, SHA1, SHA256, and SHA512 transform paths, and the shared HSH/SHA512 register-window readback and write operations. The SHA512 path shares the wide HSH register bank with SHA3, SNOW3G, and ZUC. Keep the aliased readback and write paths centralized so selector decode can route register accesses through these helpers when side effects are required. Signed-off-by: James Hilliard <james.hilliard1@gmail.com> Message-ID: <20260608-mips-octeon-missing-insns-v2-v16-12-daef7a0d8b04@gmail.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
James Hilliard committed
Jun 8, 2026 at 12:59 UTC
900c423717ddb062b89954f533c174760705b8d7
2 files changed
+378
target/mips/helper.h
+5
@@ -69,6 +69,11 @@ DEF_HELPER_2(octeon_cp2_mt_des3_dec, void, env, i64)
69
DEF_HELPER_2(octeon_cp2_mt_camellia_fl, void, env, i64)
70
DEF_HELPER_2(octeon_cp2_mt_camellia_flinv, void, env, i64)
71
DEF_HELPER_2(octeon_cp2_mt_camellia_round, void, env, i64)
72
+DEF_HELPER_2(octeon_cp2_mt_hsh_startsha1_compat, void, env, i64)
73
+DEF_HELPER_2(octeon_cp2_mt_hsh_startmd5, void, env, i64)
74
+DEF_HELPER_2(octeon_cp2_mt_hsh_startsha256, void, env, i64)
75
+DEF_HELPER_2(octeon_cp2_mt_hsh_startsha, void, env, i64)
76
+DEF_HELPER_2(octeon_cp2_mt_hsh_startsha512, void, env, i64)
77
78
/* microMIPS functions */
79
DEF_HELPER_4(lwm, void, env, tl, tl, i32)
target/mips/tcg/octeon_crypto.c
+373
@@ -153,6 +153,346 @@ static void octeon_gfm_mul64_uia2(const uint64_t x[2], const uint64_t y[2],
153
out[1] = revbit64(res);
154
}
155
156
+static uint32_t octeon_hsh_get32(const uint64_t *regs, unsigned int index)
157
+{
158
+ return regs[index];
159
+}
160
+
161
+static void octeon_hsh_set32(uint64_t *regs, unsigned int index, uint32_t value)
162
+{
163
+ regs[index] = (regs[index] & ~(uint64_t)UINT32_MAX) | value;
164
+}
165
+
166
+static void octeon_hsh_set_pair(uint64_t *regs, unsigned int index,
167
+ uint64_t value)
168
+{
169
+ octeon_hsh_set32(regs, index * 2, value >> 32);
170
+ octeon_hsh_set32(regs, index * 2 + 1, value);
171
+}
172
+
173
+static void octeon_md5_transform(MIPSOcteonCryptoState *crypto)
174
+{
175
+ static const uint32_t k[64] = {
176
+ 0xd76aa478U, 0xe8c7b756U, 0x242070dbU, 0xc1bdceeeU,
177
+ 0xf57c0fafU, 0x4787c62aU, 0xa8304613U, 0xfd469501U,
178
+ 0x698098d8U, 0x8b44f7afU, 0xffff5bb1U, 0x895cd7beU,
179
+ 0x6b901122U, 0xfd987193U, 0xa679438eU, 0x49b40821U,
180
+ 0xf61e2562U, 0xc040b340U, 0x265e5a51U, 0xe9b6c7aaU,
181
+ 0xd62f105dU, 0x02441453U, 0xd8a1e681U, 0xe7d3fbc8U,
182
+ 0x21e1cde6U, 0xc33707d6U, 0xf4d50d87U, 0x455a14edU,
183
+ 0xa9e3e905U, 0xfcefa3f8U, 0x676f02d9U, 0x8d2a4c8aU,
184
+ 0xfffa3942U, 0x8771f681U, 0x6d9d6122U, 0xfde5380cU,
185
+ 0xa4beea44U, 0x4bdecfa9U, 0xf6bb4b60U, 0xbebfbc70U,
186
+ 0x289b7ec6U, 0xeaa127faU, 0xd4ef3085U, 0x04881d05U,
187
+ 0xd9d4d039U, 0xe6db99e5U, 0x1fa27cf8U, 0xc4ac5665U,
188
+ 0xf4292244U, 0x432aff97U, 0xab9423a7U, 0xfc93a039U,
189
+ 0x655b59c3U, 0x8f0ccc92U, 0xffeff47dU, 0x85845dd1U,
190
+ 0x6fa87e4fU, 0xfe2ce6e0U, 0xa3014314U, 0x4e0811a1U,
191
+ 0xf7537e82U, 0xbd3af235U, 0x2ad7d2bbU, 0xeb86d391U,
192
+ };
193
+ static const uint8_t s[64] = {
194
+ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
195
+ 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
196
+ 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
197
+ 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
198
+ };
199
+ uint32_t m[16];
200
+ uint32_t a, b, c, d;
201
+ uint32_t aa, bb, cc, dd;
202
+ int i;
203
+
204
+ for (i = 0; i < 16; i++) {
205
+ m[i] = bswap32(octeon_hsh_get32(crypto->hsh_dat, i));
206
+ }
207
+
208
+ a = bswap32(octeon_hsh_get32(crypto->hsh_iv, 0));
209
+ b = bswap32(octeon_hsh_get32(crypto->hsh_iv, 1));
210
+ c = bswap32(octeon_hsh_get32(crypto->hsh_iv, 2));
211
+ d = bswap32(octeon_hsh_get32(crypto->hsh_iv, 3));
212
+ aa = a;
213
+ bb = b;
214
+ cc = c;
215
+ dd = d;
216
+
217
+ for (i = 0; i < 64; i++) {
218
+ uint32_t f, g, tmp;
219
+
220
+ if (i < 16) {
221
+ f = (b & c) | ((~b) & d);
222
+ g = i;
223
+ } else if (i < 32) {
224
+ f = (d & b) | ((~d) & c);
225
+ g = (5 * i + 1) & 0xf;
226
+ } else if (i < 48) {
227
+ f = b ^ c ^ d;
228
+ g = (3 * i + 5) & 0xf;
229
+ } else {
230
+ f = c ^ (b | (~d));
231
+ g = (7 * i) & 0xf;
232
+ }
233
+
234
+ tmp = d;
235
+ d = c;
236
+ c = b;
237
+ b = b + rol32(a + f + k[i] + m[g], s[i]);
238
+ a = tmp;
239
+ }
240
+
241
+ a += aa;
242
+ b += bb;
243
+ c += cc;
244
+ d += dd;
245
+ octeon_hsh_set32(crypto->hsh_iv, 0, bswap32(a));
246
+ octeon_hsh_set32(crypto->hsh_iv, 1, bswap32(b));
247
+ octeon_hsh_set32(crypto->hsh_iv, 2, bswap32(c));
248
+ octeon_hsh_set32(crypto->hsh_iv, 3, bswap32(d));
249
+}
250
+
251
+static void octeon_sha1_transform(MIPSOcteonCryptoState *crypto)
252
+{
253
+ uint32_t w[80];
254
+ uint32_t a, b, c, d, e;
255
+ uint32_t orig[5];
256
+ int i;
257
+
258
+ for (i = 0; i < 16; i++) {
259
+ w[i] = octeon_hsh_get32(crypto->hsh_dat, i);
260
+ }
261
+ for (i = 16; i < 80; i++) {
262
+ w[i] = rol32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
263
+ }
264
+
265
+ for (i = 0; i < 5; i++) {
266
+ orig[i] = octeon_hsh_get32(crypto->hsh_iv, i);
267
+ }
268
+ a = orig[0];
269
+ b = orig[1];
270
+ c = orig[2];
271
+ d = orig[3];
272
+ e = orig[4];
273
+
274
+ for (i = 0; i < 80; i++) {
275
+ uint32_t f, k, temp;
276
+
277
+ if (i < 20) {
278
+ f = (b & c) | ((~b) & d);
279
+ k = 0x5a827999;
280
+ } else if (i < 40) {
281
+ f = b ^ c ^ d;
282
+ k = 0x6ed9eba1;
283
+ } else if (i < 60) {
284
+ f = (b & c) | (b & d) | (c & d);
285
+ k = 0x8f1bbcdc;
286
+ } else {
287
+ f = b ^ c ^ d;
288
+ k = 0xca62c1d6;
289
+ }
290
+
291
+ temp = rol32(a, 5) + f + e + k + w[i];
292
+ e = d;
293
+ d = c;
294
+ c = rol32(b, 30);
295
+ b = a;
296
+ a = temp;
297
+ }
298
+
299
+ orig[0] += a;
300
+ orig[1] += b;
301
+ orig[2] += c;
302
+ orig[3] += d;
303
+ orig[4] += e;
304
+ for (i = 0; i < 5; i++) {
305
+ octeon_hsh_set32(crypto->hsh_iv, i, orig[i]);
306
+ }
307
+}
308
+
309
+static void octeon_sha256_transform(MIPSOcteonCryptoState *crypto)
310
+{
311
+ static const uint32_t k[64] = {
312
+ 0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U,
313
+ 0x3956c25bU, 0x59f111f1U, 0x923f82a4U, 0xab1c5ed5U,
314
+ 0xd807aa98U, 0x12835b01U, 0x243185beU, 0x550c7dc3U,
315
+ 0x72be5d74U, 0x80deb1feU, 0x9bdc06a7U, 0xc19bf174U,
316
+ 0xe49b69c1U, 0xefbe4786U, 0x0fc19dc6U, 0x240ca1ccU,
317
+ 0x2de92c6fU, 0x4a7484aaU, 0x5cb0a9dcU, 0x76f988daU,
318
+ 0x983e5152U, 0xa831c66dU, 0xb00327c8U, 0xbf597fc7U,
319
+ 0xc6e00bf3U, 0xd5a79147U, 0x06ca6351U, 0x14292967U,
320
+ 0x27b70a85U, 0x2e1b2138U, 0x4d2c6dfcU, 0x53380d13U,
321
+ 0x650a7354U, 0x766a0abbU, 0x81c2c92eU, 0x92722c85U,
322
+ 0xa2bfe8a1U, 0xa81a664bU, 0xc24b8b70U, 0xc76c51a3U,
323
+ 0xd192e819U, 0xd6990624U, 0xf40e3585U, 0x106aa070U,
324
+ 0x19a4c116U, 0x1e376c08U, 0x2748774cU, 0x34b0bcb5U,
325
+ 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU, 0x682e6ff3U,
326
+ 0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U,
327
+ 0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U,
328
+ };
329
+ uint32_t w[64];
330
+ uint32_t a, b, c, d, e, f, g, h;
331
+ uint32_t orig[8];
332
+ int i;
333
+
334
+ for (i = 0; i < 16; i++) {
335
+ w[i] = octeon_hsh_get32(crypto->hsh_dat, i);
336
+ }
337
+ for (i = 16; i < 64; i++) {
338
+ uint32_t s0 = ror32(w[i - 15], 7) ^
339
+ ror32(w[i - 15], 18) ^
340
+ (w[i - 15] >> 3);
341
+ uint32_t s1 = ror32(w[i - 2], 17) ^
342
+ ror32(w[i - 2], 19) ^
343
+ (w[i - 2] >> 10);
344
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1;
345
+ }
346
+
347
+ for (i = 0; i < 8; i++) {
348
+ orig[i] = octeon_hsh_get32(crypto->hsh_iv, i);
349
+ }
350
+ a = orig[0];
351
+ b = orig[1];
352
+ c = orig[2];
353
+ d = orig[3];
354
+ e = orig[4];
355
+ f = orig[5];
356
+ g = orig[6];
357
+ h = orig[7];
358
+
359
+ for (i = 0; i < 64; i++) {
360
+ uint32_t s1 = ror32(e, 6) ^
361
+ ror32(e, 11) ^
362
+ ror32(e, 25);
363
+ uint32_t ch = (e & f) ^ ((~e) & g);
364
+ uint32_t temp1 = h + s1 + ch + k[i] + w[i];
365
+ uint32_t s0 = ror32(a, 2) ^
366
+ ror32(a, 13) ^
367
+ ror32(a, 22);
368
+ uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
369
+ uint32_t temp2 = s0 + maj;
370
+
371
+ h = g;
372
+ g = f;
373
+ f = e;
374
+ e = d + temp1;
375
+ d = c;
376
+ c = b;
377
+ b = a;
378
+ a = temp1 + temp2;
379
+ }
380
+
381
+ orig[0] += a;
382
+ orig[1] += b;
383
+ orig[2] += c;
384
+ orig[3] += d;
385
+ orig[4] += e;
386
+ orig[5] += f;
387
+ orig[6] += g;
388
+ orig[7] += h;
389
+ for (i = 0; i < 8; i++) {
390
+ octeon_hsh_set32(crypto->hsh_iv, i, orig[i]);
391
+ }
392
+}
393
+
394
+static void octeon_sha512_transform(MIPSOcteonCryptoState *crypto)
395
+{
396
+ static const uint64_t k[80] = {
397
+ 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
398
+ 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
399
+ 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
400
+ 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
401
+ 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
402
+ 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
403
+ 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
404
+ 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
405
+ 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
406
+ 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
407
+ 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
408
+ 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
409
+ 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
410
+ 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
411
+ 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
412
+ 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
413
+ 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
414
+ 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
415
+ 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
416
+ 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
417
+ 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
418
+ 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
419
+ 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
420
+ 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
421
+ 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
422
+ 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
423
+ 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
424
+ 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
425
+ 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
426
+ 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
427
+ 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
428
+ 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
429
+ 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
430
+ 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
431
+ 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
432
+ 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
433
+ 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
434
+ 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
435
+ 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
436
+ 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
437
+ };
438
+ uint64_t w[80];
439
+ uint64_t a, b, c, d, e, f, g, h;
440
+ int i;
441
+
442
+ for (i = 0; i < 16; i++) {
443
+ w[i] = crypto->hsh_dat[i];
444
+ }
445
+ for (i = 16; i < 80; i++) {
446
+ uint64_t s0 = ror64(w[i - 15], 1) ^
447
+ ror64(w[i - 15], 8) ^
448
+ (w[i - 15] >> 7);
449
+ uint64_t s1 = ror64(w[i - 2], 19) ^
450
+ ror64(w[i - 2], 61) ^
451
+ (w[i - 2] >> 6);
452
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1;
453
+ }
454
+
455
+ a = crypto->hsh_iv[0];
456
+ b = crypto->hsh_iv[1];
457
+ c = crypto->hsh_iv[2];
458
+ d = crypto->hsh_iv[3];
459
+ e = crypto->hsh_iv[4];
460
+ f = crypto->hsh_iv[5];
461
+ g = crypto->hsh_iv[6];
462
+ h = crypto->hsh_iv[7];
463
+
464
+ for (i = 0; i < 80; i++) {
465
+ uint64_t s0 = ror64(a, 28) ^
466
+ ror64(a, 34) ^
467
+ ror64(a, 39);
468
+ uint64_t s1 = ror64(e, 14) ^
469
+ ror64(e, 18) ^
470
+ ror64(e, 41);
471
+ uint64_t ch = (e & f) ^ ((~e) & g);
472
+ uint64_t maj = (a & b) ^ (a & c) ^ (b & c);
473
+ uint64_t temp1 = h + s1 + ch + k[i] + w[i];
474
+ uint64_t temp2 = s0 + maj;
475
+
476
+ h = g;
477
+ g = f;
478
+ f = e;
479
+ e = d + temp1;
480
+ d = c;
481
+ c = b;
482
+ b = a;
483
+ a = temp1 + temp2;
484
+ }
485
+
486
+ crypto->hsh_iv[0] += a;
487
+ crypto->hsh_iv[1] += b;
488
+ crypto->hsh_iv[2] += c;
489
+ crypto->hsh_iv[3] += d;
490
+ crypto->hsh_iv[4] += e;
491
+ crypto->hsh_iv[5] += f;
492
+ crypto->hsh_iv[6] += g;
493
+ crypto->hsh_iv[7] += h;
494
+}
495
+
496
static const uint64_t octeon_sha3_round_constants[24] = {
497
0x0000000000000001ULL, 0x0000000000008082ULL,
498
0x800000000000808aULL, 0x8000000080008000ULL,
@@ -1713,6 +2053,39 @@ void helper_octeon_cp2_mt_zuc_more(CPUMIPSState *env, uint64_t value)
2053
octeon_zuc_more(&env->octeon_crypto, value);
2054
}
2055
2056
+void helper_octeon_cp2_mt_hsh_startsha1_compat(CPUMIPSState *env,
2057
+ uint64_t value)
2058
+{
2059
+ octeon_hsh_set_pair(env->octeon_crypto.hsh_dat, 7, value);
2060
+ octeon_sha1_transform(&env->octeon_crypto);
2061
+}
2062
+
2063
+void helper_octeon_cp2_mt_hsh_startmd5(CPUMIPSState *env, uint64_t value)
2064
+{
2065
+ octeon_hsh_set_pair(env->octeon_crypto.hsh_dat, 7, value);
2066
+ octeon_md5_transform(&env->octeon_crypto);
2067
+}
2068
+
2069
+void helper_octeon_cp2_mt_hsh_startsha256(CPUMIPSState *env, uint64_t value)
2070
+{
2071
+ octeon_hsh_set_pair(env->octeon_crypto.hsh_dat, 7, value);
2072
+ octeon_sha256_transform(&env->octeon_crypto);
2073
+}
2074
+
2075
+void helper_octeon_cp2_mt_hsh_startsha(CPUMIPSState *env, uint64_t value)
2076
+{
2077
+ octeon_hsh_set_pair(env->octeon_crypto.hsh_dat, 7, value);
2078
+ octeon_sha1_transform(&env->octeon_crypto);
2079
+}
2080
+
2081
+void helper_octeon_cp2_mt_hsh_startsha512(CPUMIPSState *env, uint64_t value)
2082
+{
2083
+ MIPSOcteonCryptoState *crypto = &env->octeon_crypto;
2084
+
2085
+ crypto->hsh_dat[15] = value;
2086
+ octeon_sha512_transform(crypto);
2087
+}
2088
+
2089
uint64_t helper_octeon_cp2_mf_crc_iv_reflect(CPUMIPSState *env)
2090
{
2091
return octeon_crc_reflect32_by_byte(env->octeon_crypto.crc_iv);