hexchat/plugins/fishlim/fish.c

196 lines
5.6 KiB
C
Raw Permalink Normal View History

2011-11-25 02:17:31 +04:00
/*
2012-05-04 21:29:02 +04:00
Copyright (c) 2010 Samuel Lidén Borell <samuel@kodafritt.se>
2011-11-25 02:17:31 +04:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifdef __APPLE__
#define __AVAILABILITYMACROS__
#define DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
#endif
2011-11-25 02:17:31 +04:00
#include <stdlib.h>
#include <string.h>
#include <openssl/blowfish.h>
#include "keystore.h"
#include "fish.h"
#define IB 64
static const char fish_base64[64] = "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const signed char fish_unbase64[256] = {
IB,IB,IB,IB,IB,IB,IB,IB, IB,IB,IB,IB,IB,IB,IB,IB,
IB,IB,IB,IB,IB,IB,IB,IB, IB,IB,IB,IB,IB,IB,IB,IB,
2014-12-18 02:49:59 +03:00
/* ! " # $ % & ' ( ) * + , - . / */
2011-11-25 02:17:31 +04:00
IB,IB,IB,IB,IB,IB,IB,IB, IB,IB,IB,IB,IB,IB, 0, 1,
2014-12-18 02:49:59 +03:00
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
2011-11-25 02:17:31 +04:00
2, 3, 4, 5, 6, 7, 8, 9, 10,11,IB,IB,IB,IB,IB,IB,
2014-12-18 02:49:59 +03:00
/* @ A B C D E F G H I J K L M N O */
2011-11-25 02:17:31 +04:00
IB,38,39,40,41,42,43,44, 45,46,47,48,49,50,51,52,
2014-12-18 02:49:59 +03:00
/* P Q R S T U V W X Y Z [ \ ] ^ _*/
2011-11-25 02:17:31 +04:00
53,54,55,56,57,58,59,60, 61,62,63,IB,IB,IB,IB,IB,
2014-12-18 02:49:59 +03:00
/* ` a b c d e f g h i j k l m n o */
2011-11-25 02:17:31 +04:00
IB,12,13,14,15,16,17,18, 19,20,21,22,23,24,25,26,
2014-12-18 02:49:59 +03:00
/* p q r s t u v w x y z { | } ~ <del> */
2011-11-25 02:17:31 +04:00
27,28,29,30,31,32,33,34, 35,36,37,IB,IB,IB,IB,IB,
};
#define GET_BYTES(dest, source) do { \
*((dest)++) = ((source) >> 24) & 0xFF; \
*((dest)++) = ((source) >> 16) & 0xFF; \
*((dest)++) = ((source) >> 8) & 0xFF; \
*((dest)++) = (source) & 0xFF; \
} while (0);
char *fish_encrypt(const char *key, size_t keylen, const char *message) {
BF_KEY bfkey;
2011-11-25 13:24:33 +04:00
size_t messagelen;
size_t i;
int j;
char *encrypted;
char *end;
unsigned char bit;
unsigned char word;
unsigned char d;
2011-11-25 02:17:31 +04:00
BF_set_key(&bfkey, keylen, (const unsigned char*)key);
messagelen = strlen(message);
if (messagelen == 0) return NULL;
2014-12-18 02:49:59 +03:00
encrypted = g_malloc(((messagelen - 1) / 8) * 12 + 12 + 1); /* each 8-byte block becomes 12 bytes */
2011-11-25 02:17:31 +04:00
end = encrypted;
while (*message) {
2014-12-18 02:49:59 +03:00
/* Read 8 bytes (a Blowfish block) */
2011-11-25 02:17:31 +04:00
BF_LONG binary[2] = { 0, 0 };
unsigned char c;
for (i = 0; i < 8; i++) {
c = message[i];
binary[i >> 2] |= c << 8*(3 - (i&3));
if (c == '\0') break;
}
message += 8;
2014-12-18 02:49:59 +03:00
/* Encrypt block */
2011-11-25 02:17:31 +04:00
BF_encrypt(binary, &bfkey);
2014-12-18 02:49:59 +03:00
/* Emit FiSH-BASE64 */
2011-11-25 02:17:31 +04:00
bit = 0;
word = 1;
for (j = 0; j < 12; j++) {
d = fish_base64[(binary[word] >> bit) & 63];
*(end++) = d;
bit += 6;
if (j == 5) {
bit = 0;
word = 0;
}
}
2014-12-18 02:49:59 +03:00
/* Stop if a null terminator was found */
2011-11-25 02:17:31 +04:00
if (c == '\0') break;
}
*end = '\0';
return encrypted;
}
char *fish_decrypt(const char *key, size_t keylen, const char *data) {
BF_KEY bfkey;
2011-11-25 13:24:33 +04:00
size_t i;
char *decrypted;
char *end;
unsigned char bit;
unsigned char word;
unsigned char d;
2011-11-25 02:17:31 +04:00
BF_set_key(&bfkey, keylen, (const unsigned char*)key);
decrypted = g_malloc(strlen(data) + 1);
2011-11-25 02:17:31 +04:00
end = decrypted;
while (*data) {
2014-12-18 02:49:59 +03:00
/* Convert from FiSH-BASE64 */
2011-11-25 02:17:31 +04:00
BF_LONG binary[2] = { 0, 0 };
bit = 0;
word = 1;
for (i = 0; i < 12; i++) {
d = fish_unbase64[(const unsigned char)*(data++)];
if (d == IB) goto decrypt_end;
binary[word] |= (unsigned long)d << bit;
2011-11-25 02:17:31 +04:00
bit += 6;
if (i == 5) {
bit = 0;
word = 0;
}
}
2014-12-18 02:49:59 +03:00
/* Decrypt block */
2011-11-25 02:17:31 +04:00
BF_decrypt(binary, &bfkey);
2014-12-18 02:49:59 +03:00
/* Copy to buffer */
2011-11-25 02:17:31 +04:00
GET_BYTES(end, binary[0]);
GET_BYTES(end, binary[1]);
}
decrypt_end:
*end = '\0';
return decrypted;
}
/**
* Encrypts a message (see fish_decrypt). The key is searched for in the
* key store.
*/
char *fish_encrypt_for_nick(const char *nick, const char *data) {
char *key;
char *encrypted;
2014-12-18 02:49:59 +03:00
/* Look for key */
2011-11-25 02:17:31 +04:00
key = keystore_get_key(nick);
if (!key) return NULL;
2014-12-18 02:49:59 +03:00
/* Encrypt */
2011-11-25 02:17:31 +04:00
encrypted = fish_encrypt(key, strlen(key), data);
g_free(key);
2011-11-25 02:17:31 +04:00
return encrypted;
}
/**
* Decrypts a message (see fish_decrypt). The key is searched for in the
* key store.
*/
char *fish_decrypt_from_nick(const char *nick, const char *data) {
char *key;
char *decrypted;
2014-12-18 02:49:59 +03:00
/* Look for key */
2011-11-25 02:17:31 +04:00
key = keystore_get_key(nick);
if (!key) return NULL;
2014-12-18 02:49:59 +03:00
/* Decrypt */
2011-11-25 02:17:31 +04:00
decrypted = fish_decrypt(key, strlen(key), data);
g_free(key);
2011-11-25 02:17:31 +04:00
return decrypted;
}