Path: blob/master/arch/powerpc/crypto/curve25519-ppc64le_asm.S
26450 views
/* SPDX-License-Identifier: GPL-2.0-or-later */1#2# This code is taken from CRYPTOGAMs[1] and is included here using the option3# in the license to distribute the code under the GPL. Therefore this program4# is free software; you can redistribute it and/or modify it under the terms of5# the GNU General Public License version 2 as published by the Free Software6# Foundation.7#8# [1] https://github.com/dot-asm/cryptogams/910# Copyright (c) 2006-2017, CRYPTOGAMS by <[email protected]>11# All rights reserved.12#13# Redistribution and use in source and binary forms, with or without14# modification, are permitted provided that the following conditions15# are met:16#17# * Redistributions of source code must retain copyright notices,18# this list of conditions and the following disclaimer.19#20# * Redistributions in binary form must reproduce the above21# copyright notice, this list of conditions and the following22# disclaimer in the documentation and/or other materials23# provided with the distribution.24#25# * Neither the name of the CRYPTOGAMS nor the names of its26# copyright holder and contributors may be used to endorse or27# promote products derived from this software without specific28# prior written permission.29#30# ALTERNATIVELY, provided that this notice is retained in full, this31# product may be distributed under the terms of the GNU General Public32# License (GPL), in which case the provisions of the GPL apply INSTEAD OF33# those given above.34#35# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS36# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT37# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR38# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT39# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,40# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT41# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,42# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY43# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT44# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE45# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.4647# ====================================================================48# Written by Andy Polyakov <[email protected]> for the OpenSSL49# project. The module is, however, dual licensed under OpenSSL and50# CRYPTOGAMS licenses depending on where you obtain it. For further51# details see https://www.openssl.org/~appro/cryptogams/.52# ====================================================================5354#55# ====================================================================56# Written and Modified by Danny Tsen <[email protected]>57# - Added x25519_fe51_sqr_times, x25519_fe51_frombytes, x25519_fe51_tobytes58# and x25519_cswap59#60# Copyright 2024- IBM Corp.61#62# X25519 lower-level primitives for PPC64.63#6465#include <linux/linkage.h>6667.text6869.align 570SYM_FUNC_START(x25519_fe51_mul)7172stdu 1,-144(1)73std 21,56(1)74std 22,64(1)75std 23,72(1)76std 24,80(1)77std 25,88(1)78std 26,96(1)79std 27,104(1)80std 28,112(1)81std 29,120(1)82std 30,128(1)83std 31,136(1)8485ld 6,0(5)86ld 7,0(4)87ld 8,8(4)88ld 9,16(4)89ld 10,24(4)90ld 11,32(4)9192mulld 22,7,693mulhdu 23,7,69495mulld 24,8,696mulhdu 25,8,69798mulld 30,11,699mulhdu 31,11,6100ld 4,8(5)101mulli 11,11,19102103mulld 26,9,6104mulhdu 27,9,6105106mulld 28,10,6107mulhdu 29,10,6108mulld 12,11,4109mulhdu 21,11,4110addc 22,22,12111adde 23,23,21112113mulld 12,7,4114mulhdu 21,7,4115addc 24,24,12116adde 25,25,21117118mulld 12,10,4119mulhdu 21,10,4120ld 6,16(5)121mulli 10,10,19122addc 30,30,12123adde 31,31,21124125mulld 12,8,4126mulhdu 21,8,4127addc 26,26,12128adde 27,27,21129130mulld 12,9,4131mulhdu 21,9,4132addc 28,28,12133adde 29,29,21134mulld 12,10,6135mulhdu 21,10,6136addc 22,22,12137adde 23,23,21138139mulld 12,11,6140mulhdu 21,11,6141addc 24,24,12142adde 25,25,21143144mulld 12,9,6145mulhdu 21,9,6146ld 4,24(5)147mulli 9,9,19148addc 30,30,12149adde 31,31,21150151mulld 12,7,6152mulhdu 21,7,6153addc 26,26,12154adde 27,27,21155156mulld 12,8,6157mulhdu 21,8,6158addc 28,28,12159adde 29,29,21160mulld 12,9,4161mulhdu 21,9,4162addc 22,22,12163adde 23,23,21164165mulld 12,10,4166mulhdu 21,10,4167addc 24,24,12168adde 25,25,21169170mulld 12,8,4171mulhdu 21,8,4172ld 6,32(5)173mulli 8,8,19174addc 30,30,12175adde 31,31,21176177mulld 12,11,4178mulhdu 21,11,4179addc 26,26,12180adde 27,27,21181182mulld 12,7,4183mulhdu 21,7,4184addc 28,28,12185adde 29,29,21186mulld 12,8,6187mulhdu 21,8,6188addc 22,22,12189adde 23,23,21190191mulld 12,9,6192mulhdu 21,9,6193addc 24,24,12194adde 25,25,21195196mulld 12,10,6197mulhdu 21,10,6198addc 26,26,12199adde 27,27,21200201mulld 12,11,6202mulhdu 21,11,6203addc 28,28,12204adde 29,29,21205206mulld 12,7,6207mulhdu 21,7,6208addc 30,30,12209adde 31,31,21210211.Lfe51_reduce:212li 0,-1213srdi 0,0,13214215srdi 12,26,51216and 9,26,0217insrdi 12,27,51,0218srdi 21,22,51219and 7,22,0220insrdi 21,23,51,0221addc 28,28,12222addze 29,29223addc 24,24,21224addze 25,25225226srdi 12,28,51227and 10,28,0228insrdi 12,29,51,0229srdi 21,24,51230and 8,24,0231insrdi 21,25,51,0232addc 30,30,12233addze 31,31234add 9,9,21235236srdi 12,30,51237and 11,30,0238insrdi 12,31,51,0239mulli 12,12,19240241add 7,7,12242243srdi 21,9,51244and 9,9,0245add 10,10,21246247srdi 12,7,51248and 7,7,0249add 8,8,12250251std 9,16(3)252std 10,24(3)253std 11,32(3)254std 7,0(3)255std 8,8(3)256257ld 21,56(1)258ld 22,64(1)259ld 23,72(1)260ld 24,80(1)261ld 25,88(1)262ld 26,96(1)263ld 27,104(1)264ld 28,112(1)265ld 29,120(1)266ld 30,128(1)267ld 31,136(1)268addi 1,1,144269blr270SYM_FUNC_END(x25519_fe51_mul)271272.align 5273SYM_FUNC_START(x25519_fe51_sqr)274275stdu 1,-144(1)276std 21,56(1)277std 22,64(1)278std 23,72(1)279std 24,80(1)280std 25,88(1)281std 26,96(1)282std 27,104(1)283std 28,112(1)284std 29,120(1)285std 30,128(1)286std 31,136(1)287288ld 7,0(4)289ld 8,8(4)290ld 9,16(4)291ld 10,24(4)292ld 11,32(4)293294add 6,7,7295mulli 21,11,19296297mulld 22,7,7298mulhdu 23,7,7299mulld 24,8,6300mulhdu 25,8,6301mulld 26,9,6302mulhdu 27,9,6303mulld 28,10,6304mulhdu 29,10,6305mulld 30,11,6306mulhdu 31,11,6307add 6,8,8308mulld 12,11,21309mulhdu 11,11,21310addc 28,28,12311adde 29,29,11312313mulli 5,10,19314315mulld 12,8,8316mulhdu 11,8,8317addc 26,26,12318adde 27,27,11319mulld 12,9,6320mulhdu 11,9,6321addc 28,28,12322adde 29,29,11323mulld 12,10,6324mulhdu 11,10,6325addc 30,30,12326adde 31,31,11327mulld 12,21,6328mulhdu 11,21,6329add 6,10,10330addc 22,22,12331adde 23,23,11332mulld 12,10,5333mulhdu 10,10,5334addc 24,24,12335adde 25,25,10336mulld 12,6,21337mulhdu 10,6,21338add 6,9,9339addc 26,26,12340adde 27,27,10341342mulld 12,9,9343mulhdu 10,9,9344addc 30,30,12345adde 31,31,10346mulld 12,5,6347mulhdu 10,5,6348addc 22,22,12349adde 23,23,10350mulld 12,21,6351mulhdu 10,21,6352addc 24,24,12353adde 25,25,10354355b .Lfe51_reduce356SYM_FUNC_END(x25519_fe51_sqr)357358.align 5359SYM_FUNC_START(x25519_fe51_mul121666)360361stdu 1,-144(1)362std 21,56(1)363std 22,64(1)364std 23,72(1)365std 24,80(1)366std 25,88(1)367std 26,96(1)368std 27,104(1)369std 28,112(1)370std 29,120(1)371std 30,128(1)372std 31,136(1)373374lis 6,1375ori 6,6,56130376ld 7,0(4)377ld 8,8(4)378ld 9,16(4)379ld 10,24(4)380ld 11,32(4)381382mulld 22,7,6383mulhdu 23,7,6384mulld 24,8,6385mulhdu 25,8,6386mulld 26,9,6387mulhdu 27,9,6388mulld 28,10,6389mulhdu 29,10,6390mulld 30,11,6391mulhdu 31,11,6392393b .Lfe51_reduce394SYM_FUNC_END(x25519_fe51_mul121666)395396.align 5397SYM_FUNC_START(x25519_fe51_sqr_times)398399stdu 1,-144(1)400std 21,56(1)401std 22,64(1)402std 23,72(1)403std 24,80(1)404std 25,88(1)405std 26,96(1)406std 27,104(1)407std 28,112(1)408std 29,120(1)409std 30,128(1)410std 31,136(1)411412ld 7,0(4)413ld 8,8(4)414ld 9,16(4)415ld 10,24(4)416ld 11,32(4)417418mtctr 5419420.Lsqr_times_loop:421add 6,7,7422mulli 21,11,19423424mulld 22,7,7425mulhdu 23,7,7426mulld 24,8,6427mulhdu 25,8,6428mulld 26,9,6429mulhdu 27,9,6430mulld 28,10,6431mulhdu 29,10,6432mulld 30,11,6433mulhdu 31,11,6434add 6,8,8435mulld 12,11,21436mulhdu 11,11,21437addc 28,28,12438adde 29,29,11439440mulli 5,10,19441442mulld 12,8,8443mulhdu 11,8,8444addc 26,26,12445adde 27,27,11446mulld 12,9,6447mulhdu 11,9,6448addc 28,28,12449adde 29,29,11450mulld 12,10,6451mulhdu 11,10,6452addc 30,30,12453adde 31,31,11454mulld 12,21,6455mulhdu 11,21,6456add 6,10,10457addc 22,22,12458adde 23,23,11459mulld 12,10,5460mulhdu 10,10,5461addc 24,24,12462adde 25,25,10463mulld 12,6,21464mulhdu 10,6,21465add 6,9,9466addc 26,26,12467adde 27,27,10468469mulld 12,9,9470mulhdu 10,9,9471addc 30,30,12472adde 31,31,10473mulld 12,5,6474mulhdu 10,5,6475addc 22,22,12476adde 23,23,10477mulld 12,21,6478mulhdu 10,21,6479addc 24,24,12480adde 25,25,10481482# fe51_reduce483li 0,-1484srdi 0,0,13485486srdi 12,26,51487and 9,26,0488insrdi 12,27,51,0489srdi 21,22,51490and 7,22,0491insrdi 21,23,51,0492addc 28,28,12493addze 29,29494addc 24,24,21495addze 25,25496497srdi 12,28,51498and 10,28,0499insrdi 12,29,51,0500srdi 21,24,51501and 8,24,0502insrdi 21,25,51,0503addc 30,30,12504addze 31,31505add 9,9,21506507srdi 12,30,51508and 11,30,0509insrdi 12,31,51,0510mulli 12,12,19511512add 7,7,12513514srdi 21,9,51515and 9,9,0516add 10,10,21517518srdi 12,7,51519and 7,7,0520add 8,8,12521522bdnz .Lsqr_times_loop523524std 9,16(3)525std 10,24(3)526std 11,32(3)527std 7,0(3)528std 8,8(3)529530ld 21,56(1)531ld 22,64(1)532ld 23,72(1)533ld 24,80(1)534ld 25,88(1)535ld 26,96(1)536ld 27,104(1)537ld 28,112(1)538ld 29,120(1)539ld 30,128(1)540ld 31,136(1)541addi 1,1,144542blr543SYM_FUNC_END(x25519_fe51_sqr_times)544545.align 5546SYM_FUNC_START(x25519_fe51_frombytes)547548li 12, -1549srdi 12, 12, 13 # 0x7ffffffffffff550551ld 5, 0(4)552ld 6, 8(4)553ld 7, 16(4)554ld 8, 24(4)555556srdi 10, 5, 51557and 5, 5, 12 # h0558559sldi 11, 6, 13560or 11, 10, 11 # h1t561srdi 10, 6, 38562and 6, 11, 12 # h1563564sldi 11, 7, 26565or 10, 10, 11 # h2t566567srdi 11, 7, 25568and 7, 10, 12 # h2569sldi 10, 8, 39570or 11, 11, 10 # h3t571572srdi 9, 8, 12573and 8, 11, 12 # h3574and 9, 9, 12 # h4575576std 5, 0(3)577std 6, 8(3)578std 7, 16(3)579std 8, 24(3)580std 9, 32(3)581582blr583SYM_FUNC_END(x25519_fe51_frombytes)584585.align 5586SYM_FUNC_START(x25519_fe51_tobytes)587588ld 5, 0(4)589ld 6, 8(4)590ld 7, 16(4)591ld 8, 24(4)592ld 9, 32(4)593594li 12, -1595srdi 12, 12, 13 # 0x7ffffffffffff596597# Full reducuction598addi 10, 5, 19599srdi 10, 10, 51600add 10, 10, 6601srdi 10, 10, 51602add 10, 10, 7603srdi 10, 10, 51604add 10, 10, 8605srdi 10, 10, 51606add 10, 10, 9607srdi 10, 10, 51608609mulli 10, 10, 19610add 5, 5, 10611srdi 11, 5, 51612add 6, 6, 11613srdi 11, 6, 51614add 7, 7, 11615srdi 11, 7, 51616add 8, 8, 11617srdi 11, 8, 51618add 9, 9, 11619620and 5, 5, 12621and 6, 6, 12622and 7, 7, 12623and 8, 8, 12624and 9, 9, 12625626sldi 10, 6, 51627or 5, 5, 10 # s0628629srdi 11, 6, 13630sldi 10, 7, 38631or 6, 11, 10 # s1632633srdi 11, 7, 26634sldi 10, 8, 25635or 7, 11, 10 # s2636637srdi 11, 8, 39638sldi 10, 9, 12639or 8, 11, 10 # s4640641std 5, 0(3)642std 6, 8(3)643std 7, 16(3)644std 8, 24(3)645646blr647SYM_FUNC_END(x25519_fe51_tobytes)648649.align 5650SYM_FUNC_START(x25519_cswap)651652li 7, 5653neg 6, 5654mtctr 7655656.Lswap_loop:657ld 8, 0(3)658ld 9, 0(4)659xor 10, 8, 9660and 10, 10, 6661xor 11, 8, 10662xor 12, 9, 10663std 11, 0(3)664addi 3, 3, 8665std 12, 0(4)666addi 4, 4, 8667bdnz .Lswap_loop668669blr670SYM_FUNC_END(x25519_cswap)671672673