Path: blob/main/crypto/openssl/crypto/bn/rsaz_exp.c
34875 views
/*1* Copyright 2013-2023 The OpenSSL Project Authors. All Rights Reserved.2* Copyright (c) 2012, Intel Corporation. All Rights Reserved.3*4* Licensed under the Apache License 2.0 (the "License"). You may not use5* this file except in compliance with the License. You can obtain a copy6* in the file LICENSE in the source distribution or at7* https://www.openssl.org/source/license.html8*9* Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1)10* (1) Intel Corporation, Israel Development Center, Haifa, Israel11* (2) University of Haifa, Israel12*/1314#include <openssl/opensslconf.h>15#include "internal/common.h"16#include "rsaz_exp.h"1718#ifndef RSAZ_ENABLED19NON_EMPTY_TRANSLATION_UNIT20#else2122/*23* See crypto/bn/asm/rsaz-avx2.pl for further details.24*/25void rsaz_1024_norm2red_avx2(void *red, const void *norm);26void rsaz_1024_mul_avx2(void *ret, const void *a, const void *b,27const void *n, BN_ULONG k);28void rsaz_1024_sqr_avx2(void *ret, const void *a, const void *n, BN_ULONG k,29int cnt);30void rsaz_1024_scatter5_avx2(void *tbl, const void *val, int i);31void rsaz_1024_gather5_avx2(void *val, const void *tbl, int i);32void rsaz_1024_red2norm_avx2(void *norm, const void *red);3334#if defined(__SUNPRO_C)35# pragma align 64(one,two80)36#endif3738ALIGN64 static const BN_ULONG one[40] = {391, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,400, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 041};4243ALIGN64 static const BN_ULONG two80[40] = {440, 0, 1 << 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 046};4748void RSAZ_1024_mod_exp_avx2(BN_ULONG result_norm[16],49const BN_ULONG base_norm[16],50const BN_ULONG exponent[16],51const BN_ULONG m_norm[16], const BN_ULONG RR[16],52BN_ULONG k0)53{54unsigned char storage[320 * 3 + 32 * 9 * 16 + 64]; /* 5.5KB */55unsigned char *p_str = storage + (64 - ((size_t)storage % 64));56unsigned char *a_inv, *m, *result;57unsigned char *table_s = p_str + 320 * 3;58unsigned char *R2 = table_s; /* borrow */59int index;60int wvalue;61BN_ULONG tmp[16];6263if ((((size_t)p_str & 4095) + 320) >> 12) {64result = p_str;65a_inv = p_str + 320;66m = p_str + 320 * 2; /* should not cross page */67} else {68m = p_str; /* should not cross page */69result = p_str + 320;70a_inv = p_str + 320 * 2;71}7273rsaz_1024_norm2red_avx2(m, m_norm);74rsaz_1024_norm2red_avx2(a_inv, base_norm);75rsaz_1024_norm2red_avx2(R2, RR);7677rsaz_1024_mul_avx2(R2, R2, R2, m, k0);78rsaz_1024_mul_avx2(R2, R2, two80, m, k0);7980/* table[0] = 1 */81rsaz_1024_mul_avx2(result, R2, one, m, k0);82/* table[1] = a_inv^1 */83rsaz_1024_mul_avx2(a_inv, a_inv, R2, m, k0);8485rsaz_1024_scatter5_avx2(table_s, result, 0);86rsaz_1024_scatter5_avx2(table_s, a_inv, 1);8788/* table[2] = a_inv^2 */89rsaz_1024_sqr_avx2(result, a_inv, m, k0, 1);90rsaz_1024_scatter5_avx2(table_s, result, 2);91#if 092/* this is almost 2x smaller and less than 1% slower */93for (index = 3; index < 32; index++) {94rsaz_1024_mul_avx2(result, result, a_inv, m, k0);95rsaz_1024_scatter5_avx2(table_s, result, index);96}97#else98/* table[4] = a_inv^4 */99rsaz_1024_sqr_avx2(result, result, m, k0, 1);100rsaz_1024_scatter5_avx2(table_s, result, 4);101/* table[8] = a_inv^8 */102rsaz_1024_sqr_avx2(result, result, m, k0, 1);103rsaz_1024_scatter5_avx2(table_s, result, 8);104/* table[16] = a_inv^16 */105rsaz_1024_sqr_avx2(result, result, m, k0, 1);106rsaz_1024_scatter5_avx2(table_s, result, 16);107/* table[17] = a_inv^17 */108rsaz_1024_mul_avx2(result, result, a_inv, m, k0);109rsaz_1024_scatter5_avx2(table_s, result, 17);110111/* table[3] */112rsaz_1024_gather5_avx2(result, table_s, 2);113rsaz_1024_mul_avx2(result, result, a_inv, m, k0);114rsaz_1024_scatter5_avx2(table_s, result, 3);115/* table[6] */116rsaz_1024_sqr_avx2(result, result, m, k0, 1);117rsaz_1024_scatter5_avx2(table_s, result, 6);118/* table[12] */119rsaz_1024_sqr_avx2(result, result, m, k0, 1);120rsaz_1024_scatter5_avx2(table_s, result, 12);121/* table[24] */122rsaz_1024_sqr_avx2(result, result, m, k0, 1);123rsaz_1024_scatter5_avx2(table_s, result, 24);124/* table[25] */125rsaz_1024_mul_avx2(result, result, a_inv, m, k0);126rsaz_1024_scatter5_avx2(table_s, result, 25);127128/* table[5] */129rsaz_1024_gather5_avx2(result, table_s, 4);130rsaz_1024_mul_avx2(result, result, a_inv, m, k0);131rsaz_1024_scatter5_avx2(table_s, result, 5);132/* table[10] */133rsaz_1024_sqr_avx2(result, result, m, k0, 1);134rsaz_1024_scatter5_avx2(table_s, result, 10);135/* table[20] */136rsaz_1024_sqr_avx2(result, result, m, k0, 1);137rsaz_1024_scatter5_avx2(table_s, result, 20);138/* table[21] */139rsaz_1024_mul_avx2(result, result, a_inv, m, k0);140rsaz_1024_scatter5_avx2(table_s, result, 21);141142/* table[7] */143rsaz_1024_gather5_avx2(result, table_s, 6);144rsaz_1024_mul_avx2(result, result, a_inv, m, k0);145rsaz_1024_scatter5_avx2(table_s, result, 7);146/* table[14] */147rsaz_1024_sqr_avx2(result, result, m, k0, 1);148rsaz_1024_scatter5_avx2(table_s, result, 14);149/* table[28] */150rsaz_1024_sqr_avx2(result, result, m, k0, 1);151rsaz_1024_scatter5_avx2(table_s, result, 28);152/* table[29] */153rsaz_1024_mul_avx2(result, result, a_inv, m, k0);154rsaz_1024_scatter5_avx2(table_s, result, 29);155156/* table[9] */157rsaz_1024_gather5_avx2(result, table_s, 8);158rsaz_1024_mul_avx2(result, result, a_inv, m, k0);159rsaz_1024_scatter5_avx2(table_s, result, 9);160/* table[18] */161rsaz_1024_sqr_avx2(result, result, m, k0, 1);162rsaz_1024_scatter5_avx2(table_s, result, 18);163/* table[19] */164rsaz_1024_mul_avx2(result, result, a_inv, m, k0);165rsaz_1024_scatter5_avx2(table_s, result, 19);166167/* table[11] */168rsaz_1024_gather5_avx2(result, table_s, 10);169rsaz_1024_mul_avx2(result, result, a_inv, m, k0);170rsaz_1024_scatter5_avx2(table_s, result, 11);171/* table[22] */172rsaz_1024_sqr_avx2(result, result, m, k0, 1);173rsaz_1024_scatter5_avx2(table_s, result, 22);174/* table[23] */175rsaz_1024_mul_avx2(result, result, a_inv, m, k0);176rsaz_1024_scatter5_avx2(table_s, result, 23);177178/* table[13] */179rsaz_1024_gather5_avx2(result, table_s, 12);180rsaz_1024_mul_avx2(result, result, a_inv, m, k0);181rsaz_1024_scatter5_avx2(table_s, result, 13);182/* table[26] */183rsaz_1024_sqr_avx2(result, result, m, k0, 1);184rsaz_1024_scatter5_avx2(table_s, result, 26);185/* table[27] */186rsaz_1024_mul_avx2(result, result, a_inv, m, k0);187rsaz_1024_scatter5_avx2(table_s, result, 27);188189/* table[15] */190rsaz_1024_gather5_avx2(result, table_s, 14);191rsaz_1024_mul_avx2(result, result, a_inv, m, k0);192rsaz_1024_scatter5_avx2(table_s, result, 15);193/* table[30] */194rsaz_1024_sqr_avx2(result, result, m, k0, 1);195rsaz_1024_scatter5_avx2(table_s, result, 30);196/* table[31] */197rsaz_1024_mul_avx2(result, result, a_inv, m, k0);198rsaz_1024_scatter5_avx2(table_s, result, 31);199#endif200201/* load first window */202p_str = (unsigned char *)exponent;203wvalue = p_str[127] >> 3;204rsaz_1024_gather5_avx2(result, table_s, wvalue);205206index = 1014;207208while (index > -1) { /* loop for the remaining 127 windows */209210rsaz_1024_sqr_avx2(result, result, m, k0, 5);211212wvalue = (p_str[(index / 8) + 1] << 8) | p_str[index / 8];213wvalue = (wvalue >> (index % 8)) & 31;214index -= 5;215216rsaz_1024_gather5_avx2(a_inv, table_s, wvalue); /* borrow a_inv */217rsaz_1024_mul_avx2(result, result, a_inv, m, k0);218}219220/* square four times */221rsaz_1024_sqr_avx2(result, result, m, k0, 4);222223wvalue = p_str[0] & 15;224225rsaz_1024_gather5_avx2(a_inv, table_s, wvalue); /* borrow a_inv */226rsaz_1024_mul_avx2(result, result, a_inv, m, k0);227228/* from Montgomery */229rsaz_1024_mul_avx2(result, result, one, m, k0);230231rsaz_1024_red2norm_avx2(result_norm, result);232233bn_reduce_once_in_place(result_norm, /*carry=*/0, m_norm, tmp, 16);234235OPENSSL_cleanse(storage, sizeof(storage));236OPENSSL_cleanse(tmp, sizeof(tmp));237}238239/*240* See crypto/bn/rsaz-x86_64.pl for further details.241*/242void rsaz_512_mul(void *ret, const void *a, const void *b, const void *n,243BN_ULONG k);244void rsaz_512_mul_scatter4(void *ret, const void *a, const void *n,245BN_ULONG k, const void *tbl, unsigned int power);246void rsaz_512_mul_gather4(void *ret, const void *a, const void *tbl,247const void *n, BN_ULONG k, unsigned int power);248void rsaz_512_mul_by_one(void *ret, const void *a, const void *n, BN_ULONG k);249void rsaz_512_sqr(void *ret, const void *a, const void *n, BN_ULONG k,250int cnt);251void rsaz_512_scatter4(void *tbl, const BN_ULONG *val, int power);252void rsaz_512_gather4(BN_ULONG *val, const void *tbl, int power);253254void RSAZ_512_mod_exp(BN_ULONG result[8],255const BN_ULONG base[8], const BN_ULONG exponent[8],256const BN_ULONG m[8], BN_ULONG k0, const BN_ULONG RR[8])257{258unsigned char storage[16 * 8 * 8 + 64 * 2 + 64]; /* 1.2KB */259unsigned char *table = storage + (64 - ((size_t)storage % 64));260BN_ULONG *a_inv = (BN_ULONG *)(table + 16 * 8 * 8);261BN_ULONG *temp = (BN_ULONG *)(table + 16 * 8 * 8 + 8 * 8);262unsigned char *p_str = (unsigned char *)exponent;263int index;264unsigned int wvalue;265BN_ULONG tmp[8];266267/* table[0] = 1_inv */268temp[0] = 0 - m[0];269temp[1] = ~m[1];270temp[2] = ~m[2];271temp[3] = ~m[3];272temp[4] = ~m[4];273temp[5] = ~m[5];274temp[6] = ~m[6];275temp[7] = ~m[7];276rsaz_512_scatter4(table, temp, 0);277278/* table [1] = a_inv^1 */279rsaz_512_mul(a_inv, base, RR, m, k0);280rsaz_512_scatter4(table, a_inv, 1);281282/* table [2] = a_inv^2 */283rsaz_512_sqr(temp, a_inv, m, k0, 1);284rsaz_512_scatter4(table, temp, 2);285286for (index = 3; index < 16; index++)287rsaz_512_mul_scatter4(temp, a_inv, m, k0, table, index);288289/* load first window */290wvalue = p_str[63];291292rsaz_512_gather4(temp, table, wvalue >> 4);293rsaz_512_sqr(temp, temp, m, k0, 4);294rsaz_512_mul_gather4(temp, temp, table, m, k0, wvalue & 0xf);295296for (index = 62; index >= 0; index--) {297wvalue = p_str[index];298299rsaz_512_sqr(temp, temp, m, k0, 4);300rsaz_512_mul_gather4(temp, temp, table, m, k0, wvalue >> 4);301302rsaz_512_sqr(temp, temp, m, k0, 4);303rsaz_512_mul_gather4(temp, temp, table, m, k0, wvalue & 0x0f);304}305306/* from Montgomery */307rsaz_512_mul_by_one(result, temp, m, k0);308309bn_reduce_once_in_place(result, /*carry=*/0, m, tmp, 8);310311OPENSSL_cleanse(storage, sizeof(storage));312OPENSSL_cleanse(tmp, sizeof(tmp));313}314315#endif316317318