/* * Multiple-Precision Modular Arithmetic Routines * * * Change log: 07/17/02 - Created. */ #if !defined(__MP2_H__) #define __MP2_H__ #if !defined(RSA_BITS) #define RSA_BITS 1024 #endif /* Data structure for big numbers */ typedef unsigned long * mp; /* we represent each mp number as an array of * 30-bit long numbers (each group of 30-bit * will be called a 'chunk') */ /* Other types needed */ typedef unsigned char u8; typedef unsigned long u32; typedef signed char s8; typedef signed long s32; #define CHARL 8 /* number of bits in a byte */ #define SIZEOFLONG 4 /* number of bytes in a long */ #define BITSOFLONG (CHARL*SIZEOFLONG) /* number of bits in a long */ #define NBITS 30 /* number of bits used per chunk, * It must be even and <32 */ #define RADIX (1L << NBITS) /* this is 'b' in the algorithms */ #define UNUSEDBITS (BITSOFLONG-NBITS) /* no. bits unused in chunk */ #define NBITSH (NBITS >> 1L) /* half the number of bits/chunk */ #define RADIXM (RADIX-1) /* RADIX mask */ #define RADIXROOT (1L<b)? a: b) /* Swaps two mp numbers */ #define mpswap(a,b) \ { \ mp tmp = a; \ a = b; \ b = tmp; \ } #if 0 /* May use BCM-MIPS32-01 CTZ instruction instead */ #define CLZ(rd, rs) \ { \ s32 _i=0; \ rd = 32; \ for (_i=31; _i>=0; _i--)\ if (rs & (1L << _i)) \ { \ rd = 31-_i; \ break; \ } \ } #else #define CLZ(rd, rs) __asm__("clz %0,%1":"=r"(rd):"r"(rs)) #endif #define iszero(a) ( *(a)==1L && (a)[1] == 0L ) #define sign(a) ( ((a)>0)? 1: ((a)<0)? -1: 0 ) #define ROTL32(a,n) (((a) << (n)) \ | ((a) >> (32 - (n)))) #define ROTR32(a,n) (((a) << (32-(n))) | (((unsigned long)(a)) >> (n))) /* Endianness Conversion (taken from nessie.h) */ #define BIG_ENDIAN #define ONE8 0xffU #define T8(x) ((x) & ONE8) #ifdef BIG_ENDIAN /* * U8TO32_BIG(c) returns the 32-bit value stored in big-endian convention * in the unsigned char array pointed to by c. */ /* TODO: Replace with BCM-MIPS32-01 big-endian load instructions */ #define U8TO32_BIG(c) (((u32)T8(*((c) + 3 )) << 24) | ((u32)T8(*((c) + 2)) << 16) | \ ((u32)T8(*((c) + 1)) << 8) | ((u32)T8(*((c))))) #define CONVERT(c) (((u32)T8(*(c)) << 24) | ((u32)T8(*((c) + 1)) << 16) | \ ((u32)T8(*((c) + 2)) << 8) | ((u32)T8(*((c) + 3)))) /* * U32TO8_BIG(c, v) stores the 32-bit-value v in big-endian convention * into the unsigned char array pointed to by c. */ /* TODO: Replace with BCM-MIPS32-01 big-endian store instruction */ #define U32TO8_BIG(c, v) do { \ u32 x = (v); \ u8 *d = (c); \ d[3] = T8(x >> 24); \ d[2] = T8(x >> 16); \ d[1] = T8(x >> 8); \ d[0] = T8(x); \ } while (0) #else #define U8TO32_BIG(c) (((u32)T8(*(c)) << 24) | ((u32)T8(*((c) + 1)) << 16) | \ ((u32)T8(*((c) + 2)) << 8) | ((u32)T8(*((c) + 3)))) #define U32TO8_BIG(c, v) do { \ u32 x = (v); \ u8 *d = (c); \ d[0] = T8(x >> 24); \ d[1] = T8(x >> 16); \ d[2] = T8(x >> 8); \ d[3] = T8(x); \ } while (0) #endif void mpclshift (mp a, u32 k, mp b); void mplshift (mp a, u32 k, mp b); void mprshift (mp a, u32 k, mp b); void unpack (mp src30, u32 *dest32, u32 n); void pack (u32 *src32, mp dest30, u32 n); /* RSA 1024 decryption function. All pointers must be 4 bytes aligned as this function will access data as unsigned int type. Failure to align data will result in unaligned access exception. */ int rsa_decrypt (unsigned long * in, unsigned long * pubkey, unsigned long * decrypted_digest); /* RSA 1024 RAW decryption function. Does not check padding. All pointers must be 4 bytes aligned as this function will access data as unsigned int type. Failure to align data will result in unaligned access exception. */ int rsa_decrypt_raw (unsigned long * in, unsigned long * pubkey, unsigned long * out); #endif /* Please do not remove! */ /* Local Variables: */ /* mode: C */ /* indent-tabs-mode: nil */ /* End: */