18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Fast MD5 implementation for PPC
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <asm/ppc_asm.h>
88c2ecf20Sopenharmony_ci#include <asm/asm-offsets.h>
98c2ecf20Sopenharmony_ci#include <asm/asm-compat.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define rHP	r3
128c2ecf20Sopenharmony_ci#define rWP	r4
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define rH0	r0
158c2ecf20Sopenharmony_ci#define rH1	r6
168c2ecf20Sopenharmony_ci#define rH2	r7
178c2ecf20Sopenharmony_ci#define rH3	r5
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define rW00	r8
208c2ecf20Sopenharmony_ci#define rW01	r9
218c2ecf20Sopenharmony_ci#define rW02	r10
228c2ecf20Sopenharmony_ci#define rW03	r11
238c2ecf20Sopenharmony_ci#define rW04	r12
248c2ecf20Sopenharmony_ci#define rW05	r14
258c2ecf20Sopenharmony_ci#define rW06	r15
268c2ecf20Sopenharmony_ci#define rW07	r16
278c2ecf20Sopenharmony_ci#define rW08	r17
288c2ecf20Sopenharmony_ci#define rW09	r18
298c2ecf20Sopenharmony_ci#define rW10	r19
308c2ecf20Sopenharmony_ci#define rW11	r20
318c2ecf20Sopenharmony_ci#define rW12	r21
328c2ecf20Sopenharmony_ci#define rW13	r22
338c2ecf20Sopenharmony_ci#define rW14	r23
348c2ecf20Sopenharmony_ci#define rW15	r24
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define rT0	r25
378c2ecf20Sopenharmony_ci#define rT1	r26
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define INITIALIZE \
408c2ecf20Sopenharmony_ci	PPC_STLU r1,-INT_FRAME_SIZE(r1); \
418c2ecf20Sopenharmony_ci	SAVE_8GPRS(14, r1);		/* push registers onto stack	*/ \
428c2ecf20Sopenharmony_ci	SAVE_4GPRS(22, r1);						   \
438c2ecf20Sopenharmony_ci	SAVE_GPR(26, r1)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define FINALIZE \
468c2ecf20Sopenharmony_ci	REST_8GPRS(14, r1);		/* pop registers from stack	*/ \
478c2ecf20Sopenharmony_ci	REST_4GPRS(22, r1);						   \
488c2ecf20Sopenharmony_ci	REST_GPR(26, r1);						   \
498c2ecf20Sopenharmony_ci	addi	r1,r1,INT_FRAME_SIZE;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN__
528c2ecf20Sopenharmony_ci#define LOAD_DATA(reg, off) \
538c2ecf20Sopenharmony_ci	lwbrx		reg,0,rWP;	/* load data			*/
548c2ecf20Sopenharmony_ci#define INC_PTR \
558c2ecf20Sopenharmony_ci	addi		rWP,rWP,4;	/* increment per word		*/
568c2ecf20Sopenharmony_ci#define NEXT_BLOCK			/* nothing to do		*/
578c2ecf20Sopenharmony_ci#else
588c2ecf20Sopenharmony_ci#define LOAD_DATA(reg, off) \
598c2ecf20Sopenharmony_ci	lwz		reg,off(rWP);	/* load data			*/
608c2ecf20Sopenharmony_ci#define INC_PTR				/* nothing to do		*/
618c2ecf20Sopenharmony_ci#define NEXT_BLOCK \
628c2ecf20Sopenharmony_ci	addi		rWP,rWP,64;	/* increment per block		*/
638c2ecf20Sopenharmony_ci#endif
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define R_00_15(a, b, c, d, w0, w1, p, q, off, k0h, k0l, k1h, k1l) \
668c2ecf20Sopenharmony_ci	LOAD_DATA(w0, off)		/*    W				*/ \
678c2ecf20Sopenharmony_ci	and		rT0,b,c;	/* 1: f = b and c		*/ \
688c2ecf20Sopenharmony_ci	INC_PTR				/*    ptr++			*/ \
698c2ecf20Sopenharmony_ci	andc		rT1,d,b;	/* 1: f' = ~b and d		*/ \
708c2ecf20Sopenharmony_ci	LOAD_DATA(w1, off+4)		/*    W				*/ \
718c2ecf20Sopenharmony_ci	or		rT0,rT0,rT1;	/* 1: f = f or f'		*/ \
728c2ecf20Sopenharmony_ci	addi		w0,w0,k0l;	/* 1: wk = w + k		*/ \
738c2ecf20Sopenharmony_ci	add		a,a,rT0;	/* 1: a = a + f			*/ \
748c2ecf20Sopenharmony_ci	addis		w0,w0,k0h;	/* 1: wk = w + k'		*/ \
758c2ecf20Sopenharmony_ci	addis		w1,w1,k1h;	/* 2: wk = w + k		*/ \
768c2ecf20Sopenharmony_ci	add		a,a,w0;		/* 1: a = a + wk		*/ \
778c2ecf20Sopenharmony_ci	addi		w1,w1,k1l;	/* 2: wk = w + k'		*/ \
788c2ecf20Sopenharmony_ci	rotrwi		a,a,p;		/* 1: a = a rotl x		*/ \
798c2ecf20Sopenharmony_ci	add		d,d,w1;		/* 2: a = a + wk		*/ \
808c2ecf20Sopenharmony_ci	add		a,a,b;		/* 1: a = a + b			*/ \
818c2ecf20Sopenharmony_ci	and		rT0,a,b;	/* 2: f = b and c		*/ \
828c2ecf20Sopenharmony_ci	andc		rT1,c,a;	/* 2: f' = ~b and d		*/ \
838c2ecf20Sopenharmony_ci	or		rT0,rT0,rT1;	/* 2: f = f or f'		*/ \
848c2ecf20Sopenharmony_ci	add		d,d,rT0;	/* 2: a = a + f			*/ \
858c2ecf20Sopenharmony_ci	INC_PTR				/*    ptr++			*/ \
868c2ecf20Sopenharmony_ci	rotrwi		d,d,q;		/* 2: a = a rotl x		*/ \
878c2ecf20Sopenharmony_ci	add		d,d,a;		/* 2: a = a + b			*/
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define R_16_31(a, b, c, d, w0, w1, p, q, k0h, k0l, k1h, k1l) \
908c2ecf20Sopenharmony_ci	andc		rT0,c,d;	/* 1: f = c and ~d		*/ \
918c2ecf20Sopenharmony_ci	and		rT1,b,d;	/* 1: f' = b and d		*/ \
928c2ecf20Sopenharmony_ci	addi		w0,w0,k0l;	/* 1: wk = w + k		*/ \
938c2ecf20Sopenharmony_ci	or		rT0,rT0,rT1;	/* 1: f = f or f'		*/ \
948c2ecf20Sopenharmony_ci	addis		w0,w0,k0h;	/* 1: wk = w + k'		*/ \
958c2ecf20Sopenharmony_ci	add		a,a,rT0;	/* 1: a = a + f			*/ \
968c2ecf20Sopenharmony_ci	addi		w1,w1,k1l;	/* 2: wk = w + k		*/ \
978c2ecf20Sopenharmony_ci	add		a,a,w0;		/* 1: a = a + wk		*/ \
988c2ecf20Sopenharmony_ci	addis		w1,w1,k1h;	/* 2: wk = w + k'		*/ \
998c2ecf20Sopenharmony_ci	andc		rT0,b,c;	/* 2: f = c and ~d		*/ \
1008c2ecf20Sopenharmony_ci	rotrwi		a,a,p;		/* 1: a = a rotl x		*/ \
1018c2ecf20Sopenharmony_ci	add		a,a,b;		/* 1: a = a + b			*/ \
1028c2ecf20Sopenharmony_ci	add		d,d,w1;		/* 2: a = a + wk		*/ \
1038c2ecf20Sopenharmony_ci	and		rT1,a,c;	/* 2: f' = b and d		*/ \
1048c2ecf20Sopenharmony_ci	or		rT0,rT0,rT1;	/* 2: f = f or f'		*/ \
1058c2ecf20Sopenharmony_ci	add		d,d,rT0;	/* 2: a = a + f			*/ \
1068c2ecf20Sopenharmony_ci	rotrwi		d,d,q;		/* 2: a = a rotl x		*/ \
1078c2ecf20Sopenharmony_ci	add		d,d,a;		/* 2: a = a +b			*/
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define R_32_47(a, b, c, d, w0, w1, p, q, k0h, k0l, k1h, k1l) \
1108c2ecf20Sopenharmony_ci	xor		rT0,b,c;	/* 1: f' = b xor c		*/ \
1118c2ecf20Sopenharmony_ci	addi		w0,w0,k0l;	/* 1: wk = w + k		*/ \
1128c2ecf20Sopenharmony_ci	xor		rT1,rT0,d;	/* 1: f = f xor f'		*/ \
1138c2ecf20Sopenharmony_ci	addis		w0,w0,k0h;	/* 1: wk = w + k'		*/ \
1148c2ecf20Sopenharmony_ci	add		a,a,rT1;	/* 1: a = a + f			*/ \
1158c2ecf20Sopenharmony_ci	addi		w1,w1,k1l;	/* 2: wk = w + k		*/ \
1168c2ecf20Sopenharmony_ci	add		a,a,w0;		/* 1: a = a + wk		*/ \
1178c2ecf20Sopenharmony_ci	addis		w1,w1,k1h;	/* 2: wk = w + k'		*/ \
1188c2ecf20Sopenharmony_ci	rotrwi		a,a,p;		/* 1: a = a rotl x		*/ \
1198c2ecf20Sopenharmony_ci	add		d,d,w1;		/* 2: a = a + wk		*/ \
1208c2ecf20Sopenharmony_ci	add		a,a,b;		/* 1: a = a + b			*/ \
1218c2ecf20Sopenharmony_ci	xor		rT1,rT0,a;	/* 2: f = b xor f'		*/ \
1228c2ecf20Sopenharmony_ci	add		d,d,rT1;	/* 2: a = a + f			*/ \
1238c2ecf20Sopenharmony_ci	rotrwi		d,d,q;		/* 2: a = a rotl x		*/ \
1248c2ecf20Sopenharmony_ci	add		d,d,a;		/* 2: a = a + b			*/
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#define R_48_63(a, b, c, d, w0, w1, p, q, k0h, k0l, k1h, k1l) \
1278c2ecf20Sopenharmony_ci	addi		w0,w0,k0l;	/* 1: w = w + k			*/ \
1288c2ecf20Sopenharmony_ci	orc		rT0,b,d;	/* 1: f = b or ~d		*/ \
1298c2ecf20Sopenharmony_ci	addis		w0,w0,k0h;	/* 1: w = w + k'		*/ \
1308c2ecf20Sopenharmony_ci	xor		rT0,rT0,c;	/* 1: f = f xor c		*/ \
1318c2ecf20Sopenharmony_ci	add		a,a,w0;		/* 1: a = a + wk		*/ \
1328c2ecf20Sopenharmony_ci	addi		w1,w1,k1l;	/* 2: w = w + k			*/ \
1338c2ecf20Sopenharmony_ci	add		a,a,rT0;	/* 1: a = a + f			*/ \
1348c2ecf20Sopenharmony_ci	addis		w1,w1,k1h;	/* 2: w = w + k'		*/ \
1358c2ecf20Sopenharmony_ci	rotrwi		a,a,p;		/* 1: a = a rotl x		*/ \
1368c2ecf20Sopenharmony_ci	add		a,a,b;		/* 1: a = a + b			*/ \
1378c2ecf20Sopenharmony_ci	orc		rT0,a,c;	/* 2: f = b or ~d		*/ \
1388c2ecf20Sopenharmony_ci	add		d,d,w1;		/* 2: a = a + wk		*/ \
1398c2ecf20Sopenharmony_ci	xor		rT0,rT0,b;	/* 2: f = f xor c		*/ \
1408c2ecf20Sopenharmony_ci	add		d,d,rT0;	/* 2: a = a + f			*/ \
1418c2ecf20Sopenharmony_ci	rotrwi		d,d,q;		/* 2: a = a rotl x		*/ \
1428c2ecf20Sopenharmony_ci	add		d,d,a;		/* 2: a = a + b			*/
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci_GLOBAL(ppc_md5_transform)
1458c2ecf20Sopenharmony_ci	INITIALIZE
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	mtctr		r5
1488c2ecf20Sopenharmony_ci	lwz		rH0,0(rHP)
1498c2ecf20Sopenharmony_ci	lwz		rH1,4(rHP)
1508c2ecf20Sopenharmony_ci	lwz		rH2,8(rHP)
1518c2ecf20Sopenharmony_ci	lwz		rH3,12(rHP)
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cippc_md5_main:
1548c2ecf20Sopenharmony_ci	R_00_15(rH0, rH1, rH2, rH3, rW00, rW01, 25, 20, 0,
1558c2ecf20Sopenharmony_ci		0xd76b, -23432, 0xe8c8, -18602)
1568c2ecf20Sopenharmony_ci	R_00_15(rH2, rH3, rH0, rH1, rW02, rW03, 15, 10, 8,
1578c2ecf20Sopenharmony_ci		0x2420, 0x70db, 0xc1be, -12562)
1588c2ecf20Sopenharmony_ci	R_00_15(rH0, rH1, rH2, rH3, rW04, rW05, 25, 20, 16,
1598c2ecf20Sopenharmony_ci		0xf57c, 0x0faf, 0x4788, -14806)
1608c2ecf20Sopenharmony_ci	R_00_15(rH2, rH3, rH0, rH1, rW06, rW07, 15, 10, 24,
1618c2ecf20Sopenharmony_ci		0xa830, 0x4613, 0xfd47, -27391)
1628c2ecf20Sopenharmony_ci	R_00_15(rH0, rH1, rH2, rH3, rW08, rW09, 25, 20, 32,
1638c2ecf20Sopenharmony_ci		0x6981, -26408, 0x8b45,  -2129)
1648c2ecf20Sopenharmony_ci	R_00_15(rH2, rH3, rH0, rH1, rW10, rW11, 15, 10, 40,
1658c2ecf20Sopenharmony_ci		0xffff, 0x5bb1, 0x895d, -10306)
1668c2ecf20Sopenharmony_ci	R_00_15(rH0, rH1, rH2, rH3, rW12, rW13, 25, 20, 48,
1678c2ecf20Sopenharmony_ci		0x6b90, 0x1122, 0xfd98, 0x7193)
1688c2ecf20Sopenharmony_ci	R_00_15(rH2, rH3, rH0, rH1, rW14, rW15, 15, 10, 56,
1698c2ecf20Sopenharmony_ci		0xa679, 0x438e, 0x49b4, 0x0821)
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	R_16_31(rH0, rH1, rH2, rH3, rW01, rW06, 27, 23,
1728c2ecf20Sopenharmony_ci		0x0d56, 0x6e0c, 0x1810, 0x6d2d)
1738c2ecf20Sopenharmony_ci	R_16_31(rH2, rH3, rH0, rH1, rW11, rW00, 18, 12,
1748c2ecf20Sopenharmony_ci		0x9d02, -32109, 0x124c, 0x2332)
1758c2ecf20Sopenharmony_ci	R_16_31(rH0, rH1, rH2, rH3, rW05, rW10, 27, 23,
1768c2ecf20Sopenharmony_ci		0x8ea7, 0x4a33, 0x0245, -18270)
1778c2ecf20Sopenharmony_ci	R_16_31(rH2, rH3, rH0, rH1, rW15, rW04, 18, 12,
1788c2ecf20Sopenharmony_ci		0x8eee,  -8608, 0xf258,  -5095)
1798c2ecf20Sopenharmony_ci	R_16_31(rH0, rH1, rH2, rH3, rW09, rW14, 27, 23,
1808c2ecf20Sopenharmony_ci		0x969d, -10697, 0x1cbe, -15288)
1818c2ecf20Sopenharmony_ci	R_16_31(rH2, rH3, rH0, rH1, rW03, rW08, 18, 12,
1828c2ecf20Sopenharmony_ci		0x3317, 0x3e99, 0xdbd9, 0x7c15)
1838c2ecf20Sopenharmony_ci	R_16_31(rH0, rH1, rH2, rH3, rW13, rW02, 27, 23,
1848c2ecf20Sopenharmony_ci		0xac4b, 0x7772, 0xd8cf, 0x331d)
1858c2ecf20Sopenharmony_ci	R_16_31(rH2, rH3, rH0, rH1, rW07, rW12, 18, 12,
1868c2ecf20Sopenharmony_ci		0x6a28, 0x6dd8, 0x219a, 0x3b68)
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	R_32_47(rH0, rH1, rH2, rH3, rW05, rW08, 28, 21,
1898c2ecf20Sopenharmony_ci		0x29cb, 0x28e5, 0x4218,  -7788)
1908c2ecf20Sopenharmony_ci	R_32_47(rH2, rH3, rH0, rH1, rW11, rW14, 16,  9,
1918c2ecf20Sopenharmony_ci		0x473f, 0x06d1, 0x3aae, 0x3036)
1928c2ecf20Sopenharmony_ci	R_32_47(rH0, rH1, rH2, rH3, rW01, rW04, 28, 21,
1938c2ecf20Sopenharmony_ci		0xaea1, -15134, 0x640b, -11295)
1948c2ecf20Sopenharmony_ci	R_32_47(rH2, rH3, rH0, rH1, rW07, rW10, 16,  9,
1958c2ecf20Sopenharmony_ci		0x8f4c, 0x4887, 0xbc7c, -22499)
1968c2ecf20Sopenharmony_ci	R_32_47(rH0, rH1, rH2, rH3, rW13, rW00, 28, 21,
1978c2ecf20Sopenharmony_ci		0x7eb8, -27199, 0x00ea, 0x6050)
1988c2ecf20Sopenharmony_ci	R_32_47(rH2, rH3, rH0, rH1, rW03, rW06, 16,  9,
1998c2ecf20Sopenharmony_ci		0xe01a, 0x22fe, 0x4447, 0x69c5)
2008c2ecf20Sopenharmony_ci	R_32_47(rH0, rH1, rH2, rH3, rW09, rW12, 28, 21,
2018c2ecf20Sopenharmony_ci		0xb7f3, 0x0253, 0x59b1, 0x4d5b)
2028c2ecf20Sopenharmony_ci	R_32_47(rH2, rH3, rH0, rH1, rW15, rW02, 16,  9,
2038c2ecf20Sopenharmony_ci		0x4701, -27017, 0xc7bd, -19859)
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	R_48_63(rH0, rH1, rH2, rH3, rW00, rW07, 26, 22,
2068c2ecf20Sopenharmony_ci		0x0988,  -1462, 0x4c70, -19401)
2078c2ecf20Sopenharmony_ci	R_48_63(rH2, rH3, rH0, rH1, rW14, rW05, 17, 11,
2088c2ecf20Sopenharmony_ci		0xadaf,  -5221, 0xfc99, 0x66f7)
2098c2ecf20Sopenharmony_ci	R_48_63(rH0, rH1, rH2, rH3, rW12, rW03, 26, 22,
2108c2ecf20Sopenharmony_ci		0x7e80, -16418, 0xba1e, -25587)
2118c2ecf20Sopenharmony_ci	R_48_63(rH2, rH3, rH0, rH1, rW10, rW01, 17, 11,
2128c2ecf20Sopenharmony_ci		0x4130, 0x380d, 0xe0c5, 0x738d)
2138c2ecf20Sopenharmony_ci	lwz		rW00,0(rHP)
2148c2ecf20Sopenharmony_ci	R_48_63(rH0, rH1, rH2, rH3, rW08, rW15, 26, 22,
2158c2ecf20Sopenharmony_ci		0xe837, -30770, 0xde8a, 0x69e8)
2168c2ecf20Sopenharmony_ci	lwz		rW14,4(rHP)
2178c2ecf20Sopenharmony_ci	R_48_63(rH2, rH3, rH0, rH1, rW06, rW13, 17, 11,
2188c2ecf20Sopenharmony_ci		0x9e79, 0x260f, 0x256d, -27941)
2198c2ecf20Sopenharmony_ci	lwz		rW12,8(rHP)
2208c2ecf20Sopenharmony_ci	R_48_63(rH0, rH1, rH2, rH3, rW04, rW11, 26, 22,
2218c2ecf20Sopenharmony_ci		0xab75, -20775, 0x4f9e, -28397)
2228c2ecf20Sopenharmony_ci	lwz		rW10,12(rHP)
2238c2ecf20Sopenharmony_ci	R_48_63(rH2, rH3, rH0, rH1, rW02, rW09, 17, 11,
2248c2ecf20Sopenharmony_ci		0x662b, 0x7c56, 0x11b2, 0x0358)
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	add		rH0,rH0,rW00
2278c2ecf20Sopenharmony_ci	stw		rH0,0(rHP)
2288c2ecf20Sopenharmony_ci	add		rH1,rH1,rW14
2298c2ecf20Sopenharmony_ci	stw		rH1,4(rHP)
2308c2ecf20Sopenharmony_ci	add		rH2,rH2,rW12
2318c2ecf20Sopenharmony_ci	stw		rH2,8(rHP)
2328c2ecf20Sopenharmony_ci	add		rH3,rH3,rW10
2338c2ecf20Sopenharmony_ci	stw		rH3,12(rHP)
2348c2ecf20Sopenharmony_ci	NEXT_BLOCK
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	bdnz		ppc_md5_main
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	FINALIZE
2398c2ecf20Sopenharmony_ci	blr
240