FreeBSD/sys/crypto/rijndael/rijndael-api-fst.c
/**
 * Encrypt data partitioned in octets, using RFC 2040-like padding.
 *
 * @param   input           data to be encrypted (octet sequence)
 * @param   inputOctets		input length in octets (not bits)
 * @param   outBuffer       encrypted output data
 *
 * @return	length in octets (not bits) of the encrypted output buffer.
 */
Encrypt data partitioned in octets, using RFC 2040-like padding.
@param   input           data to be encrypted (octet sequence)
@param   inputOctets         input length in octets (not bits)
@param   outBuffer       encrypted output data

@return      length in octets (not bits) of the encrypted output buffer.
Encrypt data partitioned in octets using RFC 2040 - like padding . @param input data to be encrypted ( octet sequence ) @param inputOctets input length in octets ( not bits ) @param outBuffer encrypted output data @return length in octets ( not bits ) of the encrypted output buffer .
int rijndael_padEncrypt(cipherInstance *cipher, keyInstance *key,
		const BYTE *input, int inputOctets, BYTE *outBuffer) {
	int i, numBlocks, padLen;
	u_int8_t block[16], *iv, *cp;
	if (cipher == NULL ||
		key == NULL ||
		key->direction == DIR_DECRYPT) {
		return BAD_CIPHER_STATE;
	}
	if (input == NULL || inputOctets <= 0) {
		return 0; 
	}
	numBlocks = inputOctets/16;
	switch (cipher->mode) {
	case MODE_ECB:
		for (i = numBlocks; i > 0; i--) {
			rijndaelEncrypt(key->rk, key->Nr, input, outBuffer);
			input += 16;
			outBuffer += 16;
		}
		padLen = 16 - (inputOctets - 16*numBlocks);
		if (padLen <= 0 || padLen > 16)
			return BAD_CIPHER_STATE;
		memcpy(block, input, 16 - padLen);
		for (cp = block + 16 - padLen; cp < block + 16; cp++)
			*cp = padLen;
		rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
		break;
	case MODE_CBC:
		iv = cipher->IV;
		for (i = numBlocks; i > 0; i--) {
			((u_int32_t*)block)[0] = ((const u_int32_t*)input)[0] ^ ((u_int32_t*)iv)[0];
			((u_int32_t*)block)[1] = ((const u_int32_t*)input)[1] ^ ((u_int32_t*)iv)[1];
			((u_int32_t*)block)[2] = ((const u_int32_t*)input)[2] ^ ((u_int32_t*)iv)[2];
			((u_int32_t*)block)[3] = ((const u_int32_t*)input)[3] ^ ((u_int32_t*)iv)[3];
			rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
			iv = outBuffer;
			input += 16;
			outBuffer += 16;
		}
		padLen = 16 - (inputOctets - 16*numBlocks);
		if (padLen <= 0 || padLen > 16)
			return BAD_CIPHER_STATE;
		for (i = 0; i < 16 - padLen; i++) {
			block[i] = input[i] ^ iv[i];
		}
		for (i = 16 - padLen; i < 16; i++) {
			block[i] = (BYTE)padLen ^ iv[i];
		}
		rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
		break;
	default:
		return BAD_CIPHER_STATE;
	}
	return 16*(numBlocks + 1);
}
int rijndael_padEncrypt ( cipherInstance * cipher , keyInstance * key , const BYTE * input , int inputOctets , BYTE * outBuffer ) { int i , numBlocks , padLen ; u_int8_t block [ 16 ] , * iv , * cp ; if ( cipher == NULL || key == NULL || key -> direction == DIR_DECRYPT ) { return BAD_CIPHER_STATE ; } if ( input == NULL || inputOctets <= 0 ) { return 0 ; } numBlocks = inputOctets / 16 ; switch ( cipher -> mode ) { case MODE_ECB : for ( i = numBlocks ; i > 0 ; i -- ) { rijndaelEncrypt ( key -> rk , key -> Nr , input , outBuffer ) ; input += 16 ; outBuffer += 16 ; } padLen = 16 - ( inputOctets - 16 * numBlocks ) ; if ( padLen <= 0 || padLen > 16 ) return BAD_CIPHER_STATE ; memcpy ( block , input , 16 - padLen ) ; for ( cp = block + 16 - padLen ; cp < block + 16 ; cp ++ ) * cp = padLen ; rijndaelEncrypt ( key -> rk , key -> Nr , block , outBuffer ) ; break ; case MODE_CBC : iv = cipher -> IV ; for ( i = numBlocks ; i > 0 ; i -- ) { ( ( u_int32_t * ) block ) [ 0 ] = ( ( const u_int32_t * ) input ) [ 0 ] ^ ( ( u_int32_t * ) iv ) [ 0 ] ; ( ( u_int32_t * ) block ) [ 1 ] = ( ( const u_int32_t * ) input ) [ 1 ] ^ ( ( u_int32_t * ) iv ) [ 1 ] ; ( ( u_int32_t * ) block ) [ 2 ] = ( ( const u_int32_t * ) input ) [ 2 ] ^ ( ( u_int32_t * ) iv ) [ 2 ] ; ( ( u_int32_t * ) block ) [ 3 ] = ( ( const u_int32_t * ) input ) [ 3 ] ^ ( ( u_int32_t * ) iv ) [ 3 ] ; rijndaelEncrypt ( key -> rk , key -> Nr , block , outBuffer ) ; iv = outBuffer ; input += 16 ; outBuffer += 16 ; } padLen = 16 - ( inputOctets - 16 * numBlocks ) ; if ( padLen <= 0 || padLen > 16 ) return BAD_CIPHER_STATE ; for ( i = 0 ; i < 16 - padLen ; i ++ ) { block [ i ] = input [ i ] ^ iv [ i ] ; } for ( i = 16 - padLen ; i < 16 ; i ++ ) { block [ i ] = ( BYTE ) padLen ^ iv [ i ] ; } rijndaelEncrypt ( key -> rk , key -> Nr , block , outBuffer ) ; break ; default : return BAD_CIPHER_STATE ; } return 16 * ( numBlocks + 1 ) ; }
Encrypt data partitioned in octets, using RFC 2040-like padding.
Encrypt data partitioned in octets using RFC 2040 - like padding .
/* nothing to do */