1 /******************************************************************************* 2 * Copyright 2013-2018 Intel Corporation 3 * All Rights Reserved. 4 * 5 * If this software was obtained under the Intel Simplified Software License, 6 * the following terms apply: 7 * 8 * The source code, information and material ("Material") contained herein is 9 * owned by Intel Corporation or its suppliers or licensors, and title to such 10 * Material remains with Intel Corporation or its suppliers or licensors. The 11 * Material contains proprietary information of Intel or its suppliers and 12 * licensors. The Material is protected by worldwide copyright laws and treaty 13 * provisions. No part of the Material may be used, copied, reproduced, 14 * modified, published, uploaded, posted, transmitted, distributed or disclosed 15 * in any way without Intel's prior express written permission. No license under 16 * any patent, copyright or other intellectual property rights in the Material 17 * is granted to or conferred upon you, either expressly, by implication, 18 * inducement, estoppel or otherwise. Any license under such intellectual 19 * property rights must be express and approved by Intel in writing. 20 * 21 * Unless otherwise agreed by Intel in writing, you may not remove or alter this 22 * notice or any other notice embedded in Materials by Intel or Intel's 23 * suppliers or licensors in any way. 24 * 25 * 26 * If this software was obtained under the Apache License, Version 2.0 (the 27 * "License"), the following terms apply: 28 * 29 * You may not use this file except in compliance with the License. You may 30 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 31 * 32 * 33 * Unless required by applicable law or agreed to in writing, software 34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 36 * 37 * See the License for the specific language governing permissions and 38 * limitations under the License. 39 *******************************************************************************/ 40 41 /* 42 // 43 // Purpose: 44 // Cryptography Primitive. 45 // Fixed window exponentiation scramble/unscramble 46 // 47 // Contents: 48 // gsScramblePut() 49 // gsScrambleGet() 50 // gsScrambleGet_sscm() 51 // 52 */ 53 #include "owncp.h" 54 #include "gsscramble.h" 55 #include "pcpmask_ct.h" 56 57 int gsGetScrambleBufferSize(int modulusLen, int w) 58 { 59 /* size of resource to store 2^w values of modulusLen*sizeof(BNU_CHUNK_T) each */ 60 int size = (1<<w) * modulusLen * sizeof(BNU_CHUNK_T); 61 /* padd it up to CACHE_LINE_SIZE */ 62 size += (CACHE_LINE_SIZE - (size % CACHE_LINE_SIZE)) %CACHE_LINE_SIZE; 63 return size/sizeof(BNU_CHUNK_T); 64 } 65 66 void gsScramblePut(BNU_CHUNK_T* tbl, int idx, const BNU_CHUNK_T* val, int vLen, int w) 67 { 68 int width = 1 << w; 69 int i, j; 70 for(i=0, j=idx; i<vLen; i++, j+= width) { 71 tbl[j] = val[i]; 72 } 73 } 74 75 void gsScrambleGet(BNU_CHUNK_T* val, int vLen, const BNU_CHUNK_T* tbl, int idx, int w) 76 { 77 int width = 1 << w; 78 int i, j; 79 for(i=0, j=idx; i<vLen; i++, j+= width) { 80 val[i] = tbl[j]; 81 } 82 } 83 84 void gsScrambleGet_sscm(BNU_CHUNK_T* val, int vLen, const BNU_CHUNK_T* tbl, int idx, int w) 85 { 86 BNU_CHUNK_T mask[1<<MAX_W]; 87 88 int width = 1 << w; 89 90 int n, i; 91 switch (w) { 92 case 6: 93 for(n=0; n<(1<<6); n++) 94 mask[n] = cpIsEqu_ct(n, idx); 95 break; 96 case 5: 97 for(n=0; n<(1<<5); n++) 98 mask[n] = cpIsEqu_ct(n, idx); 99 break; 100 case 4: 101 for(n=0; n<(1<<4); n++) 102 mask[n] = cpIsEqu_ct(n, idx); 103 break; 104 case 3: 105 for(n=0; n<(1<<3); n++) 106 mask[n] = cpIsEqu_ct(n, idx); 107 break; 108 case 2: 109 for(n=0; n<(1<<2); n++) 110 mask[n] = cpIsEqu_ct(n, idx); 111 break; 112 default: 113 mask[0] = cpIsEqu_ct(0, idx); 114 mask[1] = cpIsEqu_ct(1, idx); 115 break; 116 } 117 118 for(i=0; i<vLen; i++, tbl += width) { 119 BNU_CHUNK_T acc = 0; 120 121 switch (w) { 122 case 6: 123 for(n=0; n<(1<<6); n++) 124 acc |= tbl[n] & mask[n]; 125 break; 126 case 5: 127 for(n=0; n<(1<<5); n++) 128 acc |= tbl[n] & mask[n]; 129 break; 130 case 4: 131 for(n=0; n<(1<<4); n++) 132 acc |= tbl[n] & mask[n]; 133 break; 134 case 3: 135 for(n=0; n<(1<<3); n++) 136 acc |= tbl[n] & mask[n]; 137 break; 138 case 2: 139 for(n=0; n<(1<<2); n++) 140 acc |= tbl[n] & mask[n]; 141 break; 142 default: 143 acc |= tbl[0] & mask[0]; 144 acc |= tbl[1] & mask[1]; 145 break; 146 } 147 148 val[i] = acc; 149 } 150 } 151