Cryptography Reference
In-Depth Information
load_huge( &m, padded_block, modulus_length );
mod_pow( &m, public_key->exponent, public_key->modulus, &c );
*output = ( unsigned char * ) realloc( *output, encrypted_size );
unload_huge( &c, *output + ( encrypted_size - modulus_length ),
modulus_length );
len -= block_size;
input += block_size;
free_huge( &m );
free_huge( &c );
}
free( padded_block );
return encrypted_size;
}
0
2R
R
RRR
R
R
...
R0D
D
DDD
D
Block Type
Random filler bytes
Actual Payload
Figure 3-4: RSA Padding
3. Figure out how long the block size is. It should be the same as the length
of the modulus, which is usually 512, 1024, or 2048 bits. There's no fun-
damental reason why you couldn't use any other modulus lengths if you
wanted, but these are the usual lengths. The encrypted result is the same
length as the modulus:
int modulus_length = public_key->modulus->size;
4. Allocate that much space and then fi ll up this block with the padding, as
described earlier, and encrypt it using rsa_compute .
unsigned char *padded_block = ( unsigned char * )
malloc( modulus_length );
5. Operate on the input data until there is no more. Figure out if you're deal-
ing with a whole block ( modulus-length - 11 bytes) or less than that,
copy the input to the end of the block (remember that in RSA, the padding
goes at the beginning), and set the padding type to 2.
while ( len )
{
encrypted_size += modulus_length;
block_size = ( len < modulus_length - 11 ) ?
len : ( modulus_length - 11 );
Search WWH ::




Custom Search