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