Esempio n. 1
0
inline void Mode_BASE::Process(byte* out, const byte* in, word32 sz)
{
    if (mode_ == ECB)
        ECB_Process(out, in, sz);
    else if (mode_ == CBC)
        if (dir_ == ENCRYPTION)
            CBC_Encrypt(out, in, sz);
        else
            CBC_Decrypt(out, in, sz);
}
Esempio n. 2
0
C4Err CBC_EncryptPAD(Cipher_Algorithm algorithm,
                     uint8_t *key, size_t key_len,
                     const uint8_t *iv,
                     const uint8_t *in, size_t in_len,
                     uint8_t **outData, size_t *outSize)
{
    C4Err    err     = kC4Err_NoErr;
    CBC_ContextRef      cbc = kInvalidCBC_ContextRef;
    
    uint8_t     bytes2Pad;
    uint8_t     *buffer = NULL;
    size_t      buffLen = 0;
    
    /* check Key length and algorithm */
    switch(algorithm)
    {
        case kCipher_Algorithm_AES128:
            ValidateParam (key_len == 16); break;
            
        case kCipher_Algorithm_AES192:
            ValidateParam (key_len == 24); break;
            
        case kCipher_Algorithm_AES256:
            ValidateParam (key_len == 32); break;
            
        case kCipher_Algorithm_2FISH256:
            ValidateParam (key_len == 32); break;
            
        default:
            RETERR(kC4Err_BadParams);
    }
    
    
    /* calclulate Pad byte */
    if(in_len < MIN_MSG_BLOCKSIZE)
    {
        bytes2Pad =  MIN_MSG_BLOCKSIZE - in_len;
    }
    else
    {
        bytes2Pad =  roundup(in_len, MSG_BLOCKSIZE) +  MSG_BLOCKSIZE - in_len;
    };
    
    buffLen = in_len + bytes2Pad;
    buffer = XMALLOC(buffLen);
    
    memcpy(buffer, in, in_len);
    memset(buffer+in_len, bytes2Pad, bytes2Pad);
    
    err = CBC_Init(algorithm, key, iv,  &cbc);CKERR;
    
    err = CBC_Encrypt(cbc, buffer, buffLen, buffer); CKERR;
    
    
    *outData = buffer;
    *outSize = buffLen;
    
done:
    
    if(IsC4Err(err))
    {
        if(buffer)
        {
            memset(buffer, buffLen, 0);
            XFREE(buffer);
        }
    }
    
    CBC_Free(cbc);
    
    return err;
}