PageRenderTime 23ms CodeModel.GetById 2ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llmessage/llblowfishcipher.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 125 lines | 68 code | 16 blank | 41 comment | 7 complexity | a79fd098ece6f8cc22644919f011bab1 MD5 | raw file
  1/** 
  2 * @file llblowfishcipher.cpp
  3 * @brief Wrapper around OpenSSL Blowfish encryption algorithm.
  4 *
  5 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28#include "llblowfishcipher.h"
 29#include <openssl/evp.h>
 30
 31
 32LLBlowfishCipher::LLBlowfishCipher(const U8* secret, size_t secret_size)
 33:	LLCipher()
 34{
 35	llassert(secret);
 36
 37	mSecretSize = secret_size;
 38	mSecret = new U8[mSecretSize];
 39	memcpy(mSecret, secret, mSecretSize);
 40}
 41
 42LLBlowfishCipher::~LLBlowfishCipher()
 43{
 44	delete [] mSecret;
 45	mSecret = NULL;
 46}
 47
 48// virtual
 49U32 LLBlowfishCipher::encrypt(const U8* src, U32 src_len, U8* dst, U32 dst_len)
 50{
 51	if (!src || !src_len || !dst || !dst_len) return 0;
 52	if (src_len > dst_len) return 0;
 53
 54	// OpenSSL uses "cipher contexts" to hold encryption parameters.
 55    EVP_CIPHER_CTX context;
 56    EVP_CIPHER_CTX_init(&context);
 57
 58	// We want a blowfish cyclic block chain cipher, but need to set 
 59	// the key length before we pass in a key, so call EncryptInit 
 60	// first with NULLs.
 61	EVP_EncryptInit_ex(&context, EVP_bf_cbc(), NULL, NULL, NULL);
 62	EVP_CIPHER_CTX_set_key_length(&context, (int)mSecretSize);
 63	
 64	// Complete initialization.  Per EVP_EncryptInit man page, the
 65	// cipher pointer must be NULL.  Apparently initial_vector must
 66	// be 8 bytes for blowfish, as this is the block size.
 67    unsigned char initial_vector[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 68	EVP_EncryptInit_ex(&context, NULL, NULL, mSecret, initial_vector);
 69
 70    int blocksize = EVP_CIPHER_CTX_block_size(&context);
 71    int keylen = EVP_CIPHER_CTX_key_length(&context);
 72    int iv_length = EVP_CIPHER_CTX_iv_length(&context);
 73    lldebugs << "LLBlowfishCipher blocksize " << blocksize
 74		<< " keylen " << keylen
 75		<< " iv_len " << iv_length
 76		<< llendl;
 77
 78	int output_len = 0;
 79	int temp_len = 0;
 80	if (!EVP_EncryptUpdate(&context,
 81			dst,
 82			&output_len,
 83			src,
 84			src_len))
 85	{
 86		llwarns << "LLBlowfishCipher::encrypt EVP_EncryptUpdate failure" << llendl;
 87		goto ERROR;
 88	}
 89
 90	// There may be some final data left to encrypt if the input is
 91	// not an exact multiple of the block size.
 92	if (!EVP_EncryptFinal_ex(&context, (unsigned char*)(dst + output_len), &temp_len))
 93	{
 94		llwarns << "LLBlowfishCipher::encrypt EVP_EncryptFinal failure" << llendl;
 95		goto ERROR;
 96	}
 97	output_len += temp_len;
 98
 99	EVP_CIPHER_CTX_cleanup(&context);
100	return output_len;
101
102ERROR:
103	EVP_CIPHER_CTX_cleanup(&context);
104	return 0;
105}
106
107// virtual
108U32 LLBlowfishCipher::decrypt(const U8* src, U32 src_len, U8* dst, U32 dst_len)
109{
110	llerrs << "LLBlowfishCipher decrypt unsupported" << llendl;
111	return 0;
112}
113
114// virtual
115U32 LLBlowfishCipher::requiredEncryptionSpace(U32 len) const
116{
117	// *HACK: We know blowfish uses an 8 byte block size.
118	// Oddly, sometimes EVP_Encrypt produces an extra block
119	// if the input is an exact multiple of the block size.
120	// So round up.
121	const U32 BLOCK_SIZE = 8;
122	len += BLOCK_SIZE;
123	len -= (len % BLOCK_SIZE);
124	return len;
125}