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