1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/source/bigint/words.h Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,147 @@
1.4 +/*
1.5 +* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +/**
1.23 + @file
1.24 + @internalComponent
1.25 +*/
1.26 +
1.27 +#ifndef __BIGINT_WORDS_H__
1.28 +#define __BIGINT_WORDS_H__
1.29 +
1.30 +#include "../common/inlines.h"
1.31 +
1.32 +inline void ArraySetBit(TUint* aS, TUint aJ)
1.33 + {
1.34 + aS[aJ/WORD_BITS] |= (1L << (aJ%WORD_BITS));
1.35 + }
1.36 +
1.37 +inline TUint CountWords(const TUint *X, TUint N)
1.38 + {
1.39 + while (N && X[N-1]==0)
1.40 + N--;
1.41 + return N;
1.42 + }
1.43 +
1.44 +inline void SetWords(TUint *r, TUint a, TUint n)
1.45 + {
1.46 + Mem::Fill(r, WordsToBytes(n), a);
1.47 + }
1.48 +
1.49 +inline void CopyWords(TUint *r, const TUint *a, TUint n)
1.50 + {
1.51 + Mem::Copy(r, a, WordsToBytes(n));
1.52 + }
1.53 +
1.54 +inline TUint ShiftWordsLeftByBits(TUint *r, TUint n, TUint shiftBits)
1.55 + {
1.56 + assert (shiftBits<WORD_BITS);
1.57 + TUint u, carry=0;
1.58 + if (shiftBits)
1.59 + for (TUint i=0; i<n; i++)
1.60 + {
1.61 + u = r[i];
1.62 + r[i] = (u << shiftBits) | carry;
1.63 + carry = u >> (WORD_BITS-shiftBits);
1.64 + }
1.65 + return carry;
1.66 + }
1.67 +
1.68 +inline TUint ShiftWordsRightByBits(TUint *r, TUint n, TUint shiftBits)
1.69 +{
1.70 + assert (shiftBits<WORD_BITS);
1.71 + TUint u, carry=0;
1.72 + if (shiftBits)
1.73 + for (int i=n-1; i>=0; i--)
1.74 + {
1.75 + u = r[i];
1.76 + r[i] = (u >> shiftBits) | carry;
1.77 + carry = u << (WORD_BITS-shiftBits);
1.78 + }
1.79 + return carry;
1.80 + }
1.81 +
1.82 +inline TUint CryptoMin(TUint aLeft, TUint aRight)
1.83 + {
1.84 + return(aLeft<aRight ? aLeft : aRight);
1.85 + }
1.86 +
1.87 +inline TUint CryptoMax(TUint aLeft, TUint aRight)
1.88 + {
1.89 + return(aLeft<aRight ? aRight : aLeft);
1.90 + }
1.91 +
1.92 +inline void ShiftWordsLeftByWords(TUint *r, TUint n, TUint shiftWords)
1.93 + {
1.94 + shiftWords = CryptoMin(shiftWords, n);
1.95 + if (shiftWords)
1.96 + {
1.97 + for (TUint i=n-1; i>=shiftWords; i--)
1.98 + r[i] = r[i-shiftWords];
1.99 + SetWords(r, 0, shiftWords);
1.100 + }
1.101 + }
1.102 +
1.103 +inline void ShiftWordsRightByWords(TUint *r, TUint n, TUint shiftWords)
1.104 + {
1.105 + shiftWords = CryptoMin(shiftWords, n);
1.106 + if (shiftWords)
1.107 + {
1.108 + for (TUint i=0; i+shiftWords<n; i++)
1.109 + r[i] = r[i+shiftWords];
1.110 + SetWords(r+n-shiftWords, 0, shiftWords);
1.111 + }
1.112 + }
1.113 +
1.114 +inline TUint BytePrecision(TUint aValue)
1.115 + {
1.116 + TUint x=4;
1.117 + if(aValue < 0x10000) aValue <<=16, x-=2;
1.118 + if(aValue < 0x1000000) x--;
1.119 + if(!aValue) x=0;
1.120 + return x;
1.121 + }
1.122 +
1.123 +inline TUint BitPrecision(TUint aValue)
1.124 + {
1.125 + if(!aValue) return 0;
1.126 + TUint x=32;
1.127 + if(aValue < 0x10000) aValue<<=16, x-=16;
1.128 + if(aValue < 0x1000000) aValue<<=8, x-=8;
1.129 + if(aValue < 0x10000000) aValue<<=4, x-=4;
1.130 + if(aValue < 0x40000000) aValue<<=2, x-=2;
1.131 + if(aValue < 0x80000000) --x;
1.132 + return x;
1.133 + }
1.134 +
1.135 +static const TUint RoundupSizeTable[] = {2, 2, 2, 4, 4, 8, 8, 8, 8};
1.136 +
1.137 +static inline TUint RoundupSize(TUint n)
1.138 + {
1.139 + if (n<=8)
1.140 + return RoundupSizeTable[n];
1.141 + else if (n<=16)
1.142 + return 16;
1.143 + else if (n<=32)
1.144 + return 32;
1.145 + else if (n<=64)
1.146 + return 64;
1.147 + else return 1U << BitPrecision(n-1);
1.148 + }
1.149 +
1.150 +#endif