Annotation of /linden_release/linden/indra/llmessage/llblowfishcipher.cpp
Parent Directory
|
Revision Log
Revision 57 - (view) (download)
| 1 : | mjm | 57 | /** |
| 2 : | * @file llblowfishcipher.cpp | ||
| 3 : | * @brief Wrapper around OpenSSL Blowfish encryption algorithm. | ||
| 4 : | * | ||
| 5 : | * $LicenseInfo:firstyear=2007&license=viewergpl$ | ||
| 6 : | * | ||
| 7 : | * Copyright (c) 2007-2008, Linden Research, Inc. | ||
| 8 : | * | ||
| 9 : | * Second Life Viewer Source Code | ||
| 10 : | * The source code in this file ("Source Code") is provided by Linden Lab | ||
| 11 : | * to you under the terms of the GNU General Public License, version 2.0 | ||
| 12 : | * ("GPL"), unless you have obtained a separate licensing agreement | ||
| 13 : | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
| 14 : | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
| 15 : | * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2 | ||
| 16 : | * | ||
| 17 : | * There are special exceptions to the terms and conditions of the GPL as | ||
| 18 : | * it is applied to this Source Code. View the full text of the exception | ||
| 19 : | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
| 20 : | * online at http://secondlifegrid.net/programs/open_source/licensing/flossexception | ||
| 21 : | * | ||
| 22 : | * By copying, modifying or distributing this software, you acknowledge | ||
| 23 : | * that you have read and understood your obligations described above, | ||
| 24 : | * and agree to abide by those obligations. | ||
| 25 : | * | ||
| 26 : | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
| 27 : | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
| 28 : | * COMPLETENESS OR PERFORMANCE. | ||
| 29 : | * $/LicenseInfo$ | ||
| 30 : | */ | ||
| 31 : | |||
| 32 : | #include "linden_common.h" | ||
| 33 : | #include "llblowfishcipher.h" | ||
| 34 : | #include <openssl/evp.h> | ||
| 35 : | |||
| 36 : | |||
| 37 : | LLBlowfishCipher::LLBlowfishCipher(const U8* secret, size_t secret_size) | ||
| 38 : | : LLCipher() | ||
| 39 : | { | ||
| 40 : | llassert(secret); | ||
| 41 : | |||
| 42 : | mSecretSize = secret_size; | ||
| 43 : | mSecret = new U8[mSecretSize]; | ||
| 44 : | memcpy(mSecret, secret, mSecretSize); | ||
| 45 : | } | ||
| 46 : | |||
| 47 : | LLBlowfishCipher::~LLBlowfishCipher() | ||
| 48 : | { | ||
| 49 : | delete [] mSecret; | ||
| 50 : | mSecret = NULL; | ||
| 51 : | } | ||
| 52 : | |||
| 53 : | // virtual | ||
| 54 : | U32 LLBlowfishCipher::encrypt(const U8* src, U32 src_len, U8* dst, U32 dst_len) | ||
| 55 : | { | ||
| 56 : | if (!src || !src_len || !dst || !dst_len) return 0; | ||
| 57 : | if (src_len > dst_len) return 0; | ||
| 58 : | |||
| 59 : | // OpenSSL uses "cipher contexts" to hold encryption parameters. | ||
| 60 : | EVP_CIPHER_CTX context; | ||
| 61 : | EVP_CIPHER_CTX_init(&context); | ||
| 62 : | |||
| 63 : | // We want a blowfish cyclic block chain cipher, but need to set | ||
| 64 : | // the key length before we pass in a key, so call EncryptInit | ||
| 65 : | // first with NULLs. | ||
| 66 : | EVP_EncryptInit_ex(&context, EVP_bf_cbc(), NULL, NULL, NULL); | ||
| 67 : | EVP_CIPHER_CTX_set_key_length(&context, (int)mSecretSize); | ||
| 68 : | |||
| 69 : | // Complete initialization. Per EVP_EncryptInit man page, the | ||
| 70 : | // cipher pointer must be NULL. Apparently initial_vector must | ||
| 71 : | // be 8 bytes for blowfish, as this is the block size. | ||
| 72 : | unsigned char initial_vector[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | ||
| 73 : | EVP_EncryptInit_ex(&context, NULL, NULL, mSecret, initial_vector); | ||
| 74 : | |||
| 75 : | int blocksize = EVP_CIPHER_CTX_block_size(&context); | ||
| 76 : | int keylen = EVP_CIPHER_CTX_key_length(&context); | ||
| 77 : | int iv_length = EVP_CIPHER_CTX_iv_length(&context); | ||
| 78 : | lldebugs << "LLBlowfishCipher blocksize " << blocksize | ||
| 79 : | << " keylen " << keylen | ||
| 80 : | << " iv_len " << iv_length | ||
| 81 : | << llendl; | ||
| 82 : | |||
| 83 : | int output_len = 0; | ||
| 84 : | int temp_len = 0; | ||
| 85 : | if (!EVP_EncryptUpdate(&context, | ||
| 86 : | dst, | ||
| 87 : | &output_len, | ||
| 88 : | src, | ||
| 89 : | src_len)) | ||
| 90 : | { | ||
| 91 : | llwarns << "LLBlowfishCipher::encrypt EVP_EncryptUpdate failure" << llendl; | ||
| 92 : | goto ERROR; | ||
| 93 : | } | ||
| 94 : | |||
| 95 : | // There may be some final data left to encrypt if the input is | ||
| 96 : | // not an exact multiple of the block size. | ||
| 97 : | if (!EVP_EncryptFinal_ex(&context, (unsigned char*)(dst + output_len), &temp_len)) | ||
| 98 : | { | ||
| 99 : | llwarns << "LLBlowfishCipher::encrypt EVP_EncryptFinal failure" << llendl; | ||
| 100 : | goto ERROR; | ||
| 101 : | } | ||
| 102 : | output_len += temp_len; | ||
| 103 : | |||
| 104 : | EVP_CIPHER_CTX_cleanup(&context); | ||
| 105 : | return output_len; | ||
| 106 : | |||
| 107 : | ERROR: | ||
| 108 : | EVP_CIPHER_CTX_cleanup(&context); | ||
| 109 : | return 0; | ||
| 110 : | } | ||
| 111 : | |||
| 112 : | // virtual | ||
| 113 : | U32 LLBlowfishCipher::decrypt(const U8* src, U32 src_len, U8* dst, U32 dst_len) | ||
| 114 : | { | ||
| 115 : | llerrs << "LLBlowfishCipher decrypt unsupported" << llendl; | ||
| 116 : | return 0; | ||
| 117 : | } | ||
| 118 : | |||
| 119 : | // virtual | ||
| 120 : | U32 LLBlowfishCipher::requiredEncryptionSpace(U32 len) const | ||
| 121 : | { | ||
| 122 : | // *HACK: We know blowfish uses an 8 byte block size. | ||
| 123 : | // Oddly, sometimes EVP_Encrypt produces an extra block | ||
| 124 : | // if the input is an exact multiple of the block size. | ||
| 125 : | // So round up. | ||
| 126 : | const U32 BLOCK_SIZE = 8; | ||
| 127 : | len += BLOCK_SIZE; | ||
| 128 : | len -= (len % BLOCK_SIZE); | ||
| 129 : | return len; | ||
| 130 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

