xref: /openbmc/linux/arch/powerpc/crypto/sha1-powerpc-asm.S (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman/* SPDX-License-Identifier: GPL-2.0 */
2323a6bf1SMichael Ellerman/*
3323a6bf1SMichael Ellerman * SHA-1 implementation for PowerPC.
4323a6bf1SMichael Ellerman *
5323a6bf1SMichael Ellerman * Copyright (C) 2005 Paul Mackerras <paulus@samba.org>
6323a6bf1SMichael Ellerman */
7323a6bf1SMichael Ellerman
8323a6bf1SMichael Ellerman#include <asm/ppc_asm.h>
9323a6bf1SMichael Ellerman#include <asm/asm-offsets.h>
10323a6bf1SMichael Ellerman
1174ff6cb3SMarcelo Cerri#ifdef __BIG_ENDIAN__
1274ff6cb3SMarcelo Cerri#define LWZ(rt, d, ra)	\
1374ff6cb3SMarcelo Cerri	lwz	rt,d(ra)
1474ff6cb3SMarcelo Cerri#else
1574ff6cb3SMarcelo Cerri#define LWZ(rt, d, ra)	\
1674ff6cb3SMarcelo Cerri	li	rt,d;	\
1774ff6cb3SMarcelo Cerri	lwbrx	rt,rt,ra
1874ff6cb3SMarcelo Cerri#endif
1974ff6cb3SMarcelo Cerri
20323a6bf1SMichael Ellerman/*
21323a6bf1SMichael Ellerman * We roll the registers for T, A, B, C, D, E around on each
22323a6bf1SMichael Ellerman * iteration; T on iteration t is A on iteration t+1, and so on.
23323a6bf1SMichael Ellerman * We use registers 7 - 12 for this.
24323a6bf1SMichael Ellerman */
25323a6bf1SMichael Ellerman#define RT(t)	((((t)+5)%6)+7)
26323a6bf1SMichael Ellerman#define RA(t)	((((t)+4)%6)+7)
27323a6bf1SMichael Ellerman#define RB(t)	((((t)+3)%6)+7)
28323a6bf1SMichael Ellerman#define RC(t)	((((t)+2)%6)+7)
29323a6bf1SMichael Ellerman#define RD(t)	((((t)+1)%6)+7)
30323a6bf1SMichael Ellerman#define RE(t)	((((t)+0)%6)+7)
31323a6bf1SMichael Ellerman
32323a6bf1SMichael Ellerman/* We use registers 16 - 31 for the W values */
33323a6bf1SMichael Ellerman#define W(t)	(((t)%16)+16)
34323a6bf1SMichael Ellerman
35323a6bf1SMichael Ellerman#define LOADW(t)				\
3674ff6cb3SMarcelo Cerri	LWZ(W(t),(t)*4,r4)
37323a6bf1SMichael Ellerman
38323a6bf1SMichael Ellerman#define STEPD0_LOAD(t)				\
39323a6bf1SMichael Ellerman	andc	r0,RD(t),RB(t);		\
40323a6bf1SMichael Ellerman	and	r6,RB(t),RC(t);		\
41323a6bf1SMichael Ellerman	rotlwi	RT(t),RA(t),5;			\
42323a6bf1SMichael Ellerman	or	r6,r6,r0;			\
43323a6bf1SMichael Ellerman	add	r0,RE(t),r15;			\
44323a6bf1SMichael Ellerman	add	RT(t),RT(t),r6;		\
45323a6bf1SMichael Ellerman	add	r14,r0,W(t);			\
4674ff6cb3SMarcelo Cerri	LWZ(W((t)+4),((t)+4)*4,r4);	\
47323a6bf1SMichael Ellerman	rotlwi	RB(t),RB(t),30;			\
48323a6bf1SMichael Ellerman	add	RT(t),RT(t),r14
49323a6bf1SMichael Ellerman
50323a6bf1SMichael Ellerman#define STEPD0_UPDATE(t)			\
51323a6bf1SMichael Ellerman	and	r6,RB(t),RC(t);		\
52323a6bf1SMichael Ellerman	andc	r0,RD(t),RB(t);		\
53323a6bf1SMichael Ellerman	rotlwi	RT(t),RA(t),5;			\
54323a6bf1SMichael Ellerman	rotlwi	RB(t),RB(t),30;			\
55323a6bf1SMichael Ellerman	or	r6,r6,r0;			\
56323a6bf1SMichael Ellerman	add	r0,RE(t),r15;			\
57323a6bf1SMichael Ellerman	xor	r5,W((t)+4-3),W((t)+4-8);		\
58323a6bf1SMichael Ellerman	add	RT(t),RT(t),r6;		\
59323a6bf1SMichael Ellerman	xor	W((t)+4),W((t)+4-16),W((t)+4-14);	\
60323a6bf1SMichael Ellerman	add	r0,r0,W(t);			\
61323a6bf1SMichael Ellerman	xor	W((t)+4),W((t)+4),r5;			\
62323a6bf1SMichael Ellerman	add	RT(t),RT(t),r0;		\
63323a6bf1SMichael Ellerman	rotlwi	W((t)+4),W((t)+4),1
64323a6bf1SMichael Ellerman
65323a6bf1SMichael Ellerman#define STEPD1(t)				\
66323a6bf1SMichael Ellerman	xor	r6,RB(t),RC(t);		\
67323a6bf1SMichael Ellerman	rotlwi	RT(t),RA(t),5;			\
68323a6bf1SMichael Ellerman	rotlwi	RB(t),RB(t),30;			\
69323a6bf1SMichael Ellerman	xor	r6,r6,RD(t);			\
70323a6bf1SMichael Ellerman	add	r0,RE(t),r15;			\
71323a6bf1SMichael Ellerman	add	RT(t),RT(t),r6;		\
72323a6bf1SMichael Ellerman	add	r0,r0,W(t);			\
73323a6bf1SMichael Ellerman	add	RT(t),RT(t),r0
74323a6bf1SMichael Ellerman
75323a6bf1SMichael Ellerman#define STEPD1_UPDATE(t)				\
76323a6bf1SMichael Ellerman	xor	r6,RB(t),RC(t);		\
77323a6bf1SMichael Ellerman	rotlwi	RT(t),RA(t),5;			\
78323a6bf1SMichael Ellerman	rotlwi	RB(t),RB(t),30;			\
79323a6bf1SMichael Ellerman	xor	r6,r6,RD(t);			\
80323a6bf1SMichael Ellerman	add	r0,RE(t),r15;			\
81323a6bf1SMichael Ellerman	xor	r5,W((t)+4-3),W((t)+4-8);		\
82323a6bf1SMichael Ellerman	add	RT(t),RT(t),r6;		\
83323a6bf1SMichael Ellerman	xor	W((t)+4),W((t)+4-16),W((t)+4-14);	\
84323a6bf1SMichael Ellerman	add	r0,r0,W(t);			\
85323a6bf1SMichael Ellerman	xor	W((t)+4),W((t)+4),r5;			\
86323a6bf1SMichael Ellerman	add	RT(t),RT(t),r0;		\
87323a6bf1SMichael Ellerman	rotlwi	W((t)+4),W((t)+4),1
88323a6bf1SMichael Ellerman
89323a6bf1SMichael Ellerman#define STEPD2_UPDATE(t)			\
90323a6bf1SMichael Ellerman	and	r6,RB(t),RC(t);		\
91323a6bf1SMichael Ellerman	and	r0,RB(t),RD(t);		\
92323a6bf1SMichael Ellerman	rotlwi	RT(t),RA(t),5;			\
93323a6bf1SMichael Ellerman	or	r6,r6,r0;			\
94323a6bf1SMichael Ellerman	rotlwi	RB(t),RB(t),30;			\
95323a6bf1SMichael Ellerman	and	r0,RC(t),RD(t);		\
96323a6bf1SMichael Ellerman	xor	r5,W((t)+4-3),W((t)+4-8);	\
97323a6bf1SMichael Ellerman	or	r6,r6,r0;			\
98323a6bf1SMichael Ellerman	xor	W((t)+4),W((t)+4-16),W((t)+4-14);	\
99323a6bf1SMichael Ellerman	add	r0,RE(t),r15;			\
100323a6bf1SMichael Ellerman	add	RT(t),RT(t),r6;		\
101323a6bf1SMichael Ellerman	add	r0,r0,W(t);			\
102323a6bf1SMichael Ellerman	xor	W((t)+4),W((t)+4),r5;		\
103323a6bf1SMichael Ellerman	add	RT(t),RT(t),r0;		\
104323a6bf1SMichael Ellerman	rotlwi	W((t)+4),W((t)+4),1
105323a6bf1SMichael Ellerman
106323a6bf1SMichael Ellerman#define STEP0LD4(t)				\
107323a6bf1SMichael Ellerman	STEPD0_LOAD(t);				\
108323a6bf1SMichael Ellerman	STEPD0_LOAD((t)+1);			\
109323a6bf1SMichael Ellerman	STEPD0_LOAD((t)+2);			\
110323a6bf1SMichael Ellerman	STEPD0_LOAD((t)+3)
111323a6bf1SMichael Ellerman
112323a6bf1SMichael Ellerman#define STEPUP4(t, fn)				\
113323a6bf1SMichael Ellerman	STEP##fn##_UPDATE(t);			\
114323a6bf1SMichael Ellerman	STEP##fn##_UPDATE((t)+1);		\
115323a6bf1SMichael Ellerman	STEP##fn##_UPDATE((t)+2);		\
116323a6bf1SMichael Ellerman	STEP##fn##_UPDATE((t)+3)
117323a6bf1SMichael Ellerman
118323a6bf1SMichael Ellerman#define STEPUP20(t, fn)				\
119323a6bf1SMichael Ellerman	STEPUP4(t, fn);				\
120323a6bf1SMichael Ellerman	STEPUP4((t)+4, fn);			\
121323a6bf1SMichael Ellerman	STEPUP4((t)+8, fn);			\
122323a6bf1SMichael Ellerman	STEPUP4((t)+12, fn);			\
123323a6bf1SMichael Ellerman	STEPUP4((t)+16, fn)
124323a6bf1SMichael Ellerman
125323a6bf1SMichael Ellerman_GLOBAL(powerpc_sha_transform)
12627777890STony Breeds	PPC_STLU r1,-INT_FRAME_SIZE(r1)
127323a6bf1SMichael Ellerman	SAVE_8GPRS(14, r1)
128323a6bf1SMichael Ellerman	SAVE_10GPRS(22, r1)
129323a6bf1SMichael Ellerman
130323a6bf1SMichael Ellerman	/* Load up A - E */
131323a6bf1SMichael Ellerman	lwz	RA(0),0(r3)	/* A */
132323a6bf1SMichael Ellerman	lwz	RB(0),4(r3)	/* B */
133323a6bf1SMichael Ellerman	lwz	RC(0),8(r3)	/* C */
134323a6bf1SMichael Ellerman	lwz	RD(0),12(r3)	/* D */
135323a6bf1SMichael Ellerman	lwz	RE(0),16(r3)	/* E */
136323a6bf1SMichael Ellerman
137323a6bf1SMichael Ellerman	LOADW(0)
138323a6bf1SMichael Ellerman	LOADW(1)
139323a6bf1SMichael Ellerman	LOADW(2)
140323a6bf1SMichael Ellerman	LOADW(3)
141323a6bf1SMichael Ellerman
142323a6bf1SMichael Ellerman	lis	r15,0x5a82	/* K0-19 */
143323a6bf1SMichael Ellerman	ori	r15,r15,0x7999
144323a6bf1SMichael Ellerman	STEP0LD4(0)
145323a6bf1SMichael Ellerman	STEP0LD4(4)
146323a6bf1SMichael Ellerman	STEP0LD4(8)
147323a6bf1SMichael Ellerman	STEPUP4(12, D0)
148323a6bf1SMichael Ellerman	STEPUP4(16, D0)
149323a6bf1SMichael Ellerman
150323a6bf1SMichael Ellerman	lis	r15,0x6ed9	/* K20-39 */
151323a6bf1SMichael Ellerman	ori	r15,r15,0xeba1
152323a6bf1SMichael Ellerman	STEPUP20(20, D1)
153323a6bf1SMichael Ellerman
154323a6bf1SMichael Ellerman	lis	r15,0x8f1b	/* K40-59 */
155323a6bf1SMichael Ellerman	ori	r15,r15,0xbcdc
156323a6bf1SMichael Ellerman	STEPUP20(40, D2)
157323a6bf1SMichael Ellerman
158323a6bf1SMichael Ellerman	lis	r15,0xca62	/* K60-79 */
159323a6bf1SMichael Ellerman	ori	r15,r15,0xc1d6
160323a6bf1SMichael Ellerman	STEPUP4(60, D1)
161323a6bf1SMichael Ellerman	STEPUP4(64, D1)
162323a6bf1SMichael Ellerman	STEPUP4(68, D1)
163323a6bf1SMichael Ellerman	STEPUP4(72, D1)
164323a6bf1SMichael Ellerman	lwz	r20,16(r3)
165323a6bf1SMichael Ellerman	STEPD1(76)
166323a6bf1SMichael Ellerman	lwz	r19,12(r3)
167323a6bf1SMichael Ellerman	STEPD1(77)
168323a6bf1SMichael Ellerman	lwz	r18,8(r3)
169323a6bf1SMichael Ellerman	STEPD1(78)
170323a6bf1SMichael Ellerman	lwz	r17,4(r3)
171323a6bf1SMichael Ellerman	STEPD1(79)
172323a6bf1SMichael Ellerman
173323a6bf1SMichael Ellerman	lwz	r16,0(r3)
174323a6bf1SMichael Ellerman	add	r20,RE(80),r20
175323a6bf1SMichael Ellerman	add	RD(0),RD(80),r19
176323a6bf1SMichael Ellerman	add	RC(0),RC(80),r18
177323a6bf1SMichael Ellerman	add	RB(0),RB(80),r17
178323a6bf1SMichael Ellerman	add	RA(0),RA(80),r16
179323a6bf1SMichael Ellerman	mr	RE(0),r20
180323a6bf1SMichael Ellerman	stw	RA(0),0(r3)
181323a6bf1SMichael Ellerman	stw	RB(0),4(r3)
182323a6bf1SMichael Ellerman	stw	RC(0),8(r3)
183323a6bf1SMichael Ellerman	stw	RD(0),12(r3)
184323a6bf1SMichael Ellerman	stw	RE(0),16(r3)
185323a6bf1SMichael Ellerman
186323a6bf1SMichael Ellerman	REST_8GPRS(14, r1)
187323a6bf1SMichael Ellerman	REST_10GPRS(22, r1)
18827777890STony Breeds	addi	r1,r1,INT_FRAME_SIZE
189323a6bf1SMichael Ellerman	blr
190