firmware/2lib/2rsa.c
/**
 * In-place public exponentiation.
 *
 * @param key		Key to use in signing
 * @param inout		Input and output big-endian byte array
 * @param workbuf32	Work buffer; caller must verify this is
 *			(3 * key->arrsize) elements long.
 * @param exp		RSA public exponent: either 65537 (F4) or 3
 */
In-place public exponentiation.
@param key           Key to use in signing
@param inout         Input and output big-endian byte array
@param workbuf32     Work buffer; caller must verify this is
(3 * key->arrsize) elements long.
@param exp           RSA public exponent: either 65537 (F4) or 3
In - place public exponentiation . @param key Key to use in signing @param inout Input and output big - endian byte array @param workbuf32 Work buffer ; caller must verify this is ( 3 * key - > arrsize ) elements long . @param exp RSA public exponent : either 65537 ( F4 ) or 3
static void modpow(const struct vb2_public_key *key, uint8_t *inout,
		uint32_t *workbuf32, int exp)
{
	uint32_t *a = workbuf32;
	uint32_t *aR = a + key->arrsize;
	uint32_t *aaR = aR + key->arrsize;
	uint32_t *aaa = aaR;  
	int i;
	for (i = 0; i < (int)key->arrsize; ++i) {
		uint32_t tmp =
			((uint32_t)inout[((key->arrsize - 1 - i) * 4) + 0]
				<< 24) |
			(inout[((key->arrsize - 1 - i) * 4) + 1] << 16) |
			(inout[((key->arrsize - 1 - i) * 4) + 2] << 8) |
			(inout[((key->arrsize - 1 - i) * 4) + 3] << 0);
		a[i] = tmp;
	}
	montMul(key, aR, a, key->rr);  
	if (exp == 3) {
		montMul(key, aaR, aR, aR); 
		montMul(key, a, aaR, aR); 
		montMul1(key, aaa, a); 
	} else {
		for (i = 0; i < 16; i+=2) {
			montMul(key, aaR, aR, aR);  
			montMul(key, aR, aaR, aaR);  
		}
		montMul(key, aaa, aR, a);  
	}
	if (vb2_mont_ge(key, aaa)) {
		subM(key, aaa);
	}
	for (i = (int)key->arrsize - 1; i >= 0; --i) {
		uint32_t tmp = aaa[i];
		*inout++ = (uint8_t)(tmp >> 24);
		*inout++ = (uint8_t)(tmp >> 16);
		*inout++ = (uint8_t)(tmp >>  8);
		*inout++ = (uint8_t)(tmp >>  0);
	}
}
static void modpow ( const struct vb2_public_key * key , uint8_t * inout , uint32_t * workbuf32 , int exp ) { uint32_t * a = workbuf32 ; uint32_t * aR = a + key -> arrsize ; uint32_t * aaR = aR + key -> arrsize ; uint32_t * aaa = aaR ; int i ; for ( i = 0 ; i < ( int ) key -> arrsize ; ++ i ) { uint32_t tmp = ( ( uint32_t ) inout [ ( ( key -> arrsize - 1 - i ) * 4 ) + 0 ] << 24 ) | ( inout [ ( ( key -> arrsize - 1 - i ) * 4 ) + 1 ] << 16 ) | ( inout [ ( ( key -> arrsize - 1 - i ) * 4 ) + 2 ] << 8 ) | ( inout [ ( ( key -> arrsize - 1 - i ) * 4 ) + 3 ] << 0 ) ; a [ i ] = tmp ; } montMul ( key , aR , a , key -> rr ) ; if ( exp == 3 ) { montMul ( key , aaR , aR , aR ) ; montMul ( key , a , aaR , aR ) ; montMul1 ( key , aaa , a ) ; } else { for ( i = 0 ; i < 16 ; i += 2 ) { montMul ( key , aaR , aR , aR ) ; montMul ( key , aR , aaR , aaR ) ; } montMul ( key , aaa , aR , a ) ; } if ( vb2_mont_ge ( key , aaa ) ) { subM ( key , aaa ) ; } for ( i = ( int ) key -> arrsize - 1 ; i >= 0 ; -- i ) { uint32_t tmp = aaa [ i ] ; * inout ++ = ( uint8_t ) ( tmp >> 24 ) ; * inout ++ = ( uint8_t ) ( tmp >> 16 ) ; * inout ++ = ( uint8_t ) ( tmp >> 8 ) ; * inout ++ = ( uint8_t ) ( tmp >> 0 ) ; } }
In-place public exponentiation.
In - place public exponentiation .
/* Re-use location. */ /* Convert from big endian byte array to little endian word array. */ /* aR = a * RR / R mod M   */ /* aaR = aR * aR / R mod M */ /* a = aaR * aR / R mod M */ /* aaa = a * 1 / R mod M */ /* Exponent 65537 */ /* aaR = aR * aR / R mod M */ /* aR = aaR * aaR / R mod M */ /* aaa = aR * a / R mod M */ /* Make sure aaa < mod; aaa is at most 1x mod too large. */ /* Convert to bigendian byte array */