2011-11-24 17:17:31 -05:00
|
|
|
/*
|
|
|
|
|
2012-05-04 13:29:02 -04:00
|
|
|
Copyright (c) 2010 Samuel Lidén Borell <samuel@kodafritt.se>
|
2011-11-24 17:17:31 -05: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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-01-29 12:16:36 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#define __AVAILABILITYMACROS__
|
|
|
|
#define DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
|
|
|
|
#endif
|
|
|
|
|
2011-11-24 17:17:31 -05: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-17 18:49:59 -05:00
|
|
|
/* ! " # $ % & ' ( ) * + , - . / */
|
2011-11-24 17:17:31 -05:00
|
|
|
IB,IB,IB,IB,IB,IB,IB,IB, IB,IB,IB,IB,IB,IB, 0, 1,
|
2014-12-17 18:49:59 -05:00
|
|
|
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
|
2011-11-24 17:17:31 -05:00
|
|
|
2, 3, 4, 5, 6, 7, 8, 9, 10,11,IB,IB,IB,IB,IB,IB,
|
2014-12-17 18:49:59 -05:00
|
|
|
/* @ A B C D E F G H I J K L M N O */
|
2011-11-24 17:17:31 -05:00
|
|
|
IB,38,39,40,41,42,43,44, 45,46,47,48,49,50,51,52,
|
2014-12-17 18:49:59 -05:00
|
|
|
/* P Q R S T U V W X Y Z [ \ ] ^ _*/
|
2011-11-24 17:17:31 -05:00
|
|
|
53,54,55,56,57,58,59,60, 61,62,63,IB,IB,IB,IB,IB,
|
2014-12-17 18:49:59 -05:00
|
|
|
/* ` a b c d e f g h i j k l m n o */
|
2011-11-24 17:17:31 -05:00
|
|
|
IB,12,13,14,15,16,17,18, 19,20,21,22,23,24,25,26,
|
2014-12-17 18:49:59 -05:00
|
|
|
/* p q r s t u v w x y z { | } ~ <del> */
|
2011-11-24 17:17:31 -05: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 04:24:33 -05:00
|
|
|
size_t messagelen;
|
|
|
|
size_t i;
|
|
|
|
int j;
|
|
|
|
char *encrypted;
|
|
|
|
char *end;
|
|
|
|
unsigned char bit;
|
|
|
|
unsigned char word;
|
|
|
|
unsigned char d;
|
2011-11-24 17:17:31 -05:00
|
|
|
BF_set_key(&bfkey, keylen, (const unsigned char*)key);
|
|
|
|
|
|
|
|
messagelen = strlen(message);
|
|
|
|
if (messagelen == 0) return NULL;
|
2014-12-17 18:49:59 -05:00
|
|
|
encrypted = g_malloc(((messagelen - 1) / 8) * 12 + 12 + 1); /* each 8-byte block becomes 12 bytes */
|
2011-11-24 17:17:31 -05:00
|
|
|
end = encrypted;
|
|
|
|
|
|
|
|
while (*message) {
|
2014-12-17 18:49:59 -05:00
|
|
|
/* Read 8 bytes (a Blowfish block) */
|
2011-11-24 17:17:31 -05: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-17 18:49:59 -05:00
|
|
|
/* Encrypt block */
|
2011-11-24 17:17:31 -05:00
|
|
|
BF_encrypt(binary, &bfkey);
|
|
|
|
|
2014-12-17 18:49:59 -05:00
|
|
|
/* Emit FiSH-BASE64 */
|
2011-11-24 17:17:31 -05: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-17 18:49:59 -05:00
|
|
|
/* Stop if a null terminator was found */
|
2011-11-24 17:17:31 -05: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 04:24:33 -05:00
|
|
|
size_t i;
|
|
|
|
char *decrypted;
|
|
|
|
char *end;
|
|
|
|
unsigned char bit;
|
|
|
|
unsigned char word;
|
|
|
|
unsigned char d;
|
2011-11-24 17:17:31 -05:00
|
|
|
BF_set_key(&bfkey, keylen, (const unsigned char*)key);
|
|
|
|
|
2014-12-28 06:08:20 -05:00
|
|
|
decrypted = g_malloc(strlen(data) + 1);
|
2011-11-24 17:17:31 -05:00
|
|
|
end = decrypted;
|
|
|
|
|
|
|
|
while (*data) {
|
2014-12-17 18:49:59 -05:00
|
|
|
/* Convert from FiSH-BASE64 */
|
2011-11-24 17:17:31 -05: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;
|
2014-02-15 17:10:43 -05:00
|
|
|
binary[word] |= (unsigned long)d << bit;
|
2011-11-24 17:17:31 -05:00
|
|
|
bit += 6;
|
|
|
|
if (i == 5) {
|
|
|
|
bit = 0;
|
|
|
|
word = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 18:49:59 -05:00
|
|
|
/* Decrypt block */
|
2011-11-24 17:17:31 -05:00
|
|
|
BF_decrypt(binary, &bfkey);
|
|
|
|
|
2014-12-17 18:49:59 -05:00
|
|
|
/* Copy to buffer */
|
2011-11-24 17:17:31 -05: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-17 18:49:59 -05:00
|
|
|
/* Look for key */
|
2011-11-24 17:17:31 -05:00
|
|
|
key = keystore_get_key(nick);
|
|
|
|
if (!key) return NULL;
|
|
|
|
|
2014-12-17 18:49:59 -05:00
|
|
|
/* Encrypt */
|
2011-11-24 17:17:31 -05:00
|
|
|
encrypted = fish_encrypt(key, strlen(key), data);
|
|
|
|
|
2014-12-28 06:08:20 -05:00
|
|
|
g_free(key);
|
2011-11-24 17:17:31 -05: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-17 18:49:59 -05:00
|
|
|
/* Look for key */
|
2011-11-24 17:17:31 -05:00
|
|
|
key = keystore_get_key(nick);
|
|
|
|
if (!key) return NULL;
|
|
|
|
|
2014-12-17 18:49:59 -05:00
|
|
|
/* Decrypt */
|
2011-11-24 17:17:31 -05:00
|
|
|
decrypted = fish_decrypt(key, strlen(key), data);
|
|
|
|
|
2014-12-28 06:08:20 -05:00
|
|
|
g_free(key);
|
2011-11-24 17:17:31 -05:00
|
|
|
return decrypted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|