wmi-1.3.16 from opsview.com
This commit is contained in:
Executable
+124
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 2003 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
|
||||
RCSID("$Id: aes.c,v 1.5 2005/06/18 22:46:35 lha Exp $");
|
||||
#endif
|
||||
|
||||
#ifdef KRB5
|
||||
#include <krb5-types.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "rijndael-alg-fst.h"
|
||||
#include "aes.h"
|
||||
|
||||
int
|
||||
AES_set_encrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key)
|
||||
{
|
||||
key->rounds = rijndaelKeySetupEnc(key->key, userkey, bits);
|
||||
if (key->rounds == 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
AES_set_decrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key)
|
||||
{
|
||||
key->rounds = rijndaelKeySetupDec(key->key, userkey, bits);
|
||||
if (key->rounds == 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
|
||||
{
|
||||
rijndaelEncrypt(key->key, key->rounds, in, out);
|
||||
}
|
||||
|
||||
void
|
||||
AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
|
||||
{
|
||||
rijndaelDecrypt(key->key, key->rounds, in, out);
|
||||
}
|
||||
|
||||
void
|
||||
AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
|
||||
unsigned long size, const AES_KEY *key,
|
||||
unsigned char *iv, int forward_encrypt)
|
||||
{
|
||||
unsigned char tmp[AES_BLOCK_SIZE];
|
||||
int i;
|
||||
|
||||
if (forward_encrypt) {
|
||||
while (size >= AES_BLOCK_SIZE) {
|
||||
for (i = 0; i < AES_BLOCK_SIZE; i++)
|
||||
tmp[i] = in[i] ^ iv[i];
|
||||
AES_encrypt(tmp, out, key);
|
||||
memcpy(iv, out, AES_BLOCK_SIZE);
|
||||
size -= AES_BLOCK_SIZE;
|
||||
in += AES_BLOCK_SIZE;
|
||||
out += AES_BLOCK_SIZE;
|
||||
}
|
||||
if (size) {
|
||||
for (i = 0; i < size; i++)
|
||||
tmp[i] = in[i] ^ iv[i];
|
||||
for (i = size; i < AES_BLOCK_SIZE; i++)
|
||||
tmp[i] = iv[i];
|
||||
AES_encrypt(tmp, out, key);
|
||||
memcpy(iv, out, AES_BLOCK_SIZE);
|
||||
}
|
||||
} else {
|
||||
while (size >= AES_BLOCK_SIZE) {
|
||||
memcpy(tmp, in, AES_BLOCK_SIZE);
|
||||
AES_decrypt(tmp, out, key);
|
||||
for (i = 0; i < AES_BLOCK_SIZE; i++)
|
||||
out[i] ^= iv[i];
|
||||
memcpy(iv, tmp, AES_BLOCK_SIZE);
|
||||
size -= AES_BLOCK_SIZE;
|
||||
in += AES_BLOCK_SIZE;
|
||||
out += AES_BLOCK_SIZE;
|
||||
}
|
||||
if (size) {
|
||||
memcpy(tmp, in, AES_BLOCK_SIZE);
|
||||
AES_decrypt(tmp, out, key);
|
||||
for (i = 0; i < size; i++)
|
||||
out[i] ^= iv[i];
|
||||
memcpy(iv, tmp, AES_BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+71
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2003-2004 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: aes.h,v 1.6 2006/05/05 11:06:35 lha Exp $ */
|
||||
|
||||
#ifndef HEIM_AES_H
|
||||
#define HEIM_AES_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define AES_set_encrypt_key hc_AES_set_encrypt_key
|
||||
#define AES_set_decrypt_key hc_AES_decrypt_key
|
||||
#define AES_encrypt hc_AES_encrypt
|
||||
#define AES_decrypt hc_AES_decrypt
|
||||
#define AES_cbc_encrypt hc_AES_cbc_encrypt
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#define AES_BLOCK_SIZE 16
|
||||
#define AES_MAXNR 14
|
||||
|
||||
#define AES_ENCRYPT 1
|
||||
#define AES_DECRYPT 0
|
||||
|
||||
typedef struct aes_key {
|
||||
uint32_t key[(AES_MAXNR+1)*4];
|
||||
int rounds;
|
||||
} AES_KEY;
|
||||
|
||||
int AES_set_encrypt_key(const unsigned char *, const int, AES_KEY *);
|
||||
int AES_set_decrypt_key(const unsigned char *, const int, AES_KEY *);
|
||||
|
||||
void AES_encrypt(const unsigned char *, unsigned char *, const AES_KEY *);
|
||||
void AES_decrypt(const unsigned char *, unsigned char *, const AES_KEY *);
|
||||
|
||||
void AES_cbc_encrypt(const unsigned char *, unsigned char *,
|
||||
const unsigned long, const AES_KEY *,
|
||||
unsigned char *, int);
|
||||
|
||||
#endif /* HEIM_AES_H */
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* $Id: bn.h,v 1.3 2006/01/13 08:27:50 lha Exp $
|
||||
*/
|
||||
|
||||
#ifndef _HEIM_BN_H
|
||||
#define _HEIM_BN_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define BN_GENCB_call hc_BN_GENCB_call
|
||||
#define BN_GENCB_set hc_BN_GENCB_set
|
||||
#define BN_bin2bn hc_BN_bin2bn
|
||||
#define BN_bn2bin hc_BN_bn2bin
|
||||
#define BN_bn2hex hc_BN_bn2hex
|
||||
#define BN_clear hc_BN_clear
|
||||
#define BN_clear_bit hc_BN_clear_bit
|
||||
#define BN_clear_free hc_BN_clear_free
|
||||
#define BN_cmp hc_BN_cmp
|
||||
#define BN_dup hc_BN_dup
|
||||
#define BN_free hc_BN_free
|
||||
#define BN_is_negative hc_BN_is_negative
|
||||
#define BN_get_word hc_BN_get_word
|
||||
#define BN_hex2bn hc_BN_hex2bn
|
||||
#define BN_is_bit_set hc_BN_is_bit_set
|
||||
#define BN_new hc_BN_new
|
||||
#define BN_num_bits hc_BN_num_bits
|
||||
#define BN_num_bytes hc_BN_num_bytes
|
||||
#define BN_rand hc_BN_rand
|
||||
#define BN_set_bit hc_BN_set_bit
|
||||
#define BN_set_negative hc_BN_set_negative
|
||||
#define BN_set_word hc_BN_set_word
|
||||
#define BN_uadd hc_BN_uadd
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
typedef void BIGNUM;
|
||||
typedef struct BN_GENCB BN_GENCB;
|
||||
typedef void BN_CTX;
|
||||
typedef void BN_MONT_CTX;
|
||||
typedef void BN_BLINDING;
|
||||
|
||||
struct BN_GENCB {
|
||||
unsigned int ver;
|
||||
void *arg;
|
||||
union {
|
||||
int (*cb_2)(int, int, BN_GENCB *);
|
||||
} cb;
|
||||
};
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
BIGNUM *BN_new(void);
|
||||
void BN_free(BIGNUM *);
|
||||
void BN_clear_free(BIGNUM *);
|
||||
void BN_clear(BIGNUM *);
|
||||
BIGNUM *BN_dup(const BIGNUM *);
|
||||
|
||||
int BN_num_bits(const BIGNUM *);
|
||||
int BN_num_bytes(const BIGNUM *);
|
||||
|
||||
int BN_cmp(const BIGNUM *, const BIGNUM *);
|
||||
|
||||
void BN_set_negative(BIGNUM *, int);
|
||||
int BN_is_negative(BIGNUM *);
|
||||
|
||||
int BN_is_bit_set(const BIGNUM *, int);
|
||||
int BN_set_bit(BIGNUM *, int);
|
||||
int BN_clear_bit(BIGNUM *, int);
|
||||
|
||||
int BN_set_word(BIGNUM *, unsigned long);
|
||||
unsigned long BN_get_word(const BIGNUM *);
|
||||
|
||||
BIGNUM *BN_bin2bn(const void *,int len,BIGNUM *);
|
||||
int BN_bn2bin(const BIGNUM *, void *);
|
||||
int BN_hex2bn(BIGNUM **, const char *);
|
||||
char * BN_bn2hex(const BIGNUM *);
|
||||
|
||||
int BN_uadd(BIGNUM *, const BIGNUM *, const BIGNUM *);
|
||||
|
||||
int BN_rand(BIGNUM *, int, int, int);
|
||||
|
||||
void BN_GENCB_set(BN_GENCB *, int (*)(int, int, BN_GENCB *), void *);
|
||||
int BN_GENCB_call(BN_GENCB *, int, int);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,196 @@
|
||||
/* GENERATE FILE from gen-des.pl, do not edit */
|
||||
|
||||
/* pc1_c_3 bit pattern 5 13 21 */
|
||||
static int pc1_c_3[8] = {
|
||||
0x00000000, 0x00000010, 0x00001000, 0x00001010,
|
||||
0x00100000, 0x00100010, 0x00101000, 0x00101010
|
||||
};
|
||||
/* pc1_c_4 bit pattern 1 9 17 25 */
|
||||
static int pc1_c_4[16] = {
|
||||
0x00000000, 0x00000001, 0x00000100, 0x00000101,
|
||||
0x00010000, 0x00010001, 0x00010100, 0x00010101,
|
||||
0x01000000, 0x01000001, 0x01000100, 0x01000101,
|
||||
0x01010000, 0x01010001, 0x01010100, 0x01010101
|
||||
};
|
||||
/* pc1_d_3 bit pattern 49 41 33 */
|
||||
static int pc1_d_3[8] = {
|
||||
0x00000000, 0x01000000, 0x00010000, 0x01010000,
|
||||
0x00000100, 0x01000100, 0x00010100, 0x01010100
|
||||
};
|
||||
/* pc1_d_4 bit pattern 57 53 45 37 */
|
||||
static int pc1_d_4[16] = {
|
||||
0x00000000, 0x00100000, 0x00001000, 0x00101000,
|
||||
0x00000010, 0x00100010, 0x00001010, 0x00101010,
|
||||
0x00000001, 0x00100001, 0x00001001, 0x00101001,
|
||||
0x00000011, 0x00100011, 0x00001011, 0x00101011
|
||||
};
|
||||
/* pc2_c_1 bit pattern 5 24 7 16 6 10 */
|
||||
static int pc2_c_1[64] = {
|
||||
0x00000000, 0x00004000, 0x00040000, 0x00044000,
|
||||
0x00000100, 0x00004100, 0x00040100, 0x00044100,
|
||||
0x00020000, 0x00024000, 0x00060000, 0x00064000,
|
||||
0x00020100, 0x00024100, 0x00060100, 0x00064100,
|
||||
0x00000001, 0x00004001, 0x00040001, 0x00044001,
|
||||
0x00000101, 0x00004101, 0x00040101, 0x00044101,
|
||||
0x00020001, 0x00024001, 0x00060001, 0x00064001,
|
||||
0x00020101, 0x00024101, 0x00060101, 0x00064101,
|
||||
0x00080000, 0x00084000, 0x000c0000, 0x000c4000,
|
||||
0x00080100, 0x00084100, 0x000c0100, 0x000c4100,
|
||||
0x000a0000, 0x000a4000, 0x000e0000, 0x000e4000,
|
||||
0x000a0100, 0x000a4100, 0x000e0100, 0x000e4100,
|
||||
0x00080001, 0x00084001, 0x000c0001, 0x000c4001,
|
||||
0x00080101, 0x00084101, 0x000c0101, 0x000c4101,
|
||||
0x000a0001, 0x000a4001, 0x000e0001, 0x000e4001,
|
||||
0x000a0101, 0x000a4101, 0x000e0101, 0x000e4101
|
||||
};
|
||||
/* pc2_c_2 bit pattern 20 18 12 3 15 23 */
|
||||
static int pc2_c_2[64] = {
|
||||
0x00000000, 0x00000002, 0x00000200, 0x00000202,
|
||||
0x00200000, 0x00200002, 0x00200200, 0x00200202,
|
||||
0x00001000, 0x00001002, 0x00001200, 0x00001202,
|
||||
0x00201000, 0x00201002, 0x00201200, 0x00201202,
|
||||
0x00000040, 0x00000042, 0x00000240, 0x00000242,
|
||||
0x00200040, 0x00200042, 0x00200240, 0x00200242,
|
||||
0x00001040, 0x00001042, 0x00001240, 0x00001242,
|
||||
0x00201040, 0x00201042, 0x00201240, 0x00201242,
|
||||
0x00000010, 0x00000012, 0x00000210, 0x00000212,
|
||||
0x00200010, 0x00200012, 0x00200210, 0x00200212,
|
||||
0x00001010, 0x00001012, 0x00001210, 0x00001212,
|
||||
0x00201010, 0x00201012, 0x00201210, 0x00201212,
|
||||
0x00000050, 0x00000052, 0x00000250, 0x00000252,
|
||||
0x00200050, 0x00200052, 0x00200250, 0x00200252,
|
||||
0x00001050, 0x00001052, 0x00001250, 0x00001252,
|
||||
0x00201050, 0x00201052, 0x00201250, 0x00201252
|
||||
};
|
||||
/* pc2_c_3 bit pattern 1 9 19 2 14 22 */
|
||||
static int pc2_c_3[64] = {
|
||||
0x00000000, 0x00000004, 0x00000400, 0x00000404,
|
||||
0x00400000, 0x00400004, 0x00400400, 0x00400404,
|
||||
0x00000020, 0x00000024, 0x00000420, 0x00000424,
|
||||
0x00400020, 0x00400024, 0x00400420, 0x00400424,
|
||||
0x00008000, 0x00008004, 0x00008400, 0x00008404,
|
||||
0x00408000, 0x00408004, 0x00408400, 0x00408404,
|
||||
0x00008020, 0x00008024, 0x00008420, 0x00008424,
|
||||
0x00408020, 0x00408024, 0x00408420, 0x00408424,
|
||||
0x00800000, 0x00800004, 0x00800400, 0x00800404,
|
||||
0x00c00000, 0x00c00004, 0x00c00400, 0x00c00404,
|
||||
0x00800020, 0x00800024, 0x00800420, 0x00800424,
|
||||
0x00c00020, 0x00c00024, 0x00c00420, 0x00c00424,
|
||||
0x00808000, 0x00808004, 0x00808400, 0x00808404,
|
||||
0x00c08000, 0x00c08004, 0x00c08400, 0x00c08404,
|
||||
0x00808020, 0x00808024, 0x00808420, 0x00808424,
|
||||
0x00c08020, 0x00c08024, 0x00c08420, 0x00c08424
|
||||
};
|
||||
/* pc2_c_4 bit pattern 11 13 4 17 21 8 */
|
||||
static int pc2_c_4[64] = {
|
||||
0x00000000, 0x00010000, 0x00000008, 0x00010008,
|
||||
0x00000080, 0x00010080, 0x00000088, 0x00010088,
|
||||
0x00100000, 0x00110000, 0x00100008, 0x00110008,
|
||||
0x00100080, 0x00110080, 0x00100088, 0x00110088,
|
||||
0x00000800, 0x00010800, 0x00000808, 0x00010808,
|
||||
0x00000880, 0x00010880, 0x00000888, 0x00010888,
|
||||
0x00100800, 0x00110800, 0x00100808, 0x00110808,
|
||||
0x00100880, 0x00110880, 0x00100888, 0x00110888,
|
||||
0x00002000, 0x00012000, 0x00002008, 0x00012008,
|
||||
0x00002080, 0x00012080, 0x00002088, 0x00012088,
|
||||
0x00102000, 0x00112000, 0x00102008, 0x00112008,
|
||||
0x00102080, 0x00112080, 0x00102088, 0x00112088,
|
||||
0x00002800, 0x00012800, 0x00002808, 0x00012808,
|
||||
0x00002880, 0x00012880, 0x00002888, 0x00012888,
|
||||
0x00102800, 0x00112800, 0x00102808, 0x00112808,
|
||||
0x00102880, 0x00112880, 0x00102888, 0x00112888
|
||||
};
|
||||
/* pc2_d_1 bit pattern 51 35 31 52 39 45 */
|
||||
static int pc2_d_1[64] = {
|
||||
0x00000000, 0x00000080, 0x00002000, 0x00002080,
|
||||
0x00000001, 0x00000081, 0x00002001, 0x00002081,
|
||||
0x00200000, 0x00200080, 0x00202000, 0x00202080,
|
||||
0x00200001, 0x00200081, 0x00202001, 0x00202081,
|
||||
0x00020000, 0x00020080, 0x00022000, 0x00022080,
|
||||
0x00020001, 0x00020081, 0x00022001, 0x00022081,
|
||||
0x00220000, 0x00220080, 0x00222000, 0x00222080,
|
||||
0x00220001, 0x00220081, 0x00222001, 0x00222081,
|
||||
0x00000002, 0x00000082, 0x00002002, 0x00002082,
|
||||
0x00000003, 0x00000083, 0x00002003, 0x00002083,
|
||||
0x00200002, 0x00200082, 0x00202002, 0x00202082,
|
||||
0x00200003, 0x00200083, 0x00202003, 0x00202083,
|
||||
0x00020002, 0x00020082, 0x00022002, 0x00022082,
|
||||
0x00020003, 0x00020083, 0x00022003, 0x00022083,
|
||||
0x00220002, 0x00220082, 0x00222002, 0x00222082,
|
||||
0x00220003, 0x00220083, 0x00222003, 0x00222083
|
||||
};
|
||||
/* pc2_d_2 bit pattern 50 32 43 36 29 48 */
|
||||
static int pc2_d_2[64] = {
|
||||
0x00000000, 0x00000010, 0x00800000, 0x00800010,
|
||||
0x00010000, 0x00010010, 0x00810000, 0x00810010,
|
||||
0x00000200, 0x00000210, 0x00800200, 0x00800210,
|
||||
0x00010200, 0x00010210, 0x00810200, 0x00810210,
|
||||
0x00100000, 0x00100010, 0x00900000, 0x00900010,
|
||||
0x00110000, 0x00110010, 0x00910000, 0x00910010,
|
||||
0x00100200, 0x00100210, 0x00900200, 0x00900210,
|
||||
0x00110200, 0x00110210, 0x00910200, 0x00910210,
|
||||
0x00000004, 0x00000014, 0x00800004, 0x00800014,
|
||||
0x00010004, 0x00010014, 0x00810004, 0x00810014,
|
||||
0x00000204, 0x00000214, 0x00800204, 0x00800214,
|
||||
0x00010204, 0x00010214, 0x00810204, 0x00810214,
|
||||
0x00100004, 0x00100014, 0x00900004, 0x00900014,
|
||||
0x00110004, 0x00110014, 0x00910004, 0x00910014,
|
||||
0x00100204, 0x00100214, 0x00900204, 0x00900214,
|
||||
0x00110204, 0x00110214, 0x00910204, 0x00910214
|
||||
};
|
||||
/* pc2_d_3 bit pattern 41 38 47 33 40 42 */
|
||||
static int pc2_d_3[64] = {
|
||||
0x00000000, 0x00000400, 0x00001000, 0x00001400,
|
||||
0x00080000, 0x00080400, 0x00081000, 0x00081400,
|
||||
0x00000020, 0x00000420, 0x00001020, 0x00001420,
|
||||
0x00080020, 0x00080420, 0x00081020, 0x00081420,
|
||||
0x00004000, 0x00004400, 0x00005000, 0x00005400,
|
||||
0x00084000, 0x00084400, 0x00085000, 0x00085400,
|
||||
0x00004020, 0x00004420, 0x00005020, 0x00005420,
|
||||
0x00084020, 0x00084420, 0x00085020, 0x00085420,
|
||||
0x00000800, 0x00000c00, 0x00001800, 0x00001c00,
|
||||
0x00080800, 0x00080c00, 0x00081800, 0x00081c00,
|
||||
0x00000820, 0x00000c20, 0x00001820, 0x00001c20,
|
||||
0x00080820, 0x00080c20, 0x00081820, 0x00081c20,
|
||||
0x00004800, 0x00004c00, 0x00005800, 0x00005c00,
|
||||
0x00084800, 0x00084c00, 0x00085800, 0x00085c00,
|
||||
0x00004820, 0x00004c20, 0x00005820, 0x00005c20,
|
||||
0x00084820, 0x00084c20, 0x00085820, 0x00085c20
|
||||
};
|
||||
/* pc2_d_4 bit pattern 49 37 30 46 34 44 */
|
||||
static int pc2_d_4[64] = {
|
||||
0x00000000, 0x00000100, 0x00040000, 0x00040100,
|
||||
0x00000040, 0x00000140, 0x00040040, 0x00040140,
|
||||
0x00400000, 0x00400100, 0x00440000, 0x00440100,
|
||||
0x00400040, 0x00400140, 0x00440040, 0x00440140,
|
||||
0x00008000, 0x00008100, 0x00048000, 0x00048100,
|
||||
0x00008040, 0x00008140, 0x00048040, 0x00048140,
|
||||
0x00408000, 0x00408100, 0x00448000, 0x00448100,
|
||||
0x00408040, 0x00408140, 0x00448040, 0x00448140,
|
||||
0x00000008, 0x00000108, 0x00040008, 0x00040108,
|
||||
0x00000048, 0x00000148, 0x00040048, 0x00040148,
|
||||
0x00400008, 0x00400108, 0x00440008, 0x00440108,
|
||||
0x00400048, 0x00400148, 0x00440048, 0x00440148,
|
||||
0x00008008, 0x00008108, 0x00048008, 0x00048108,
|
||||
0x00008048, 0x00008148, 0x00048048, 0x00048148,
|
||||
0x00408008, 0x00408108, 0x00448008, 0x00448108,
|
||||
0x00408048, 0x00408148, 0x00448048, 0x00448148
|
||||
};
|
||||
static unsigned char odd_parity[256] = {
|
||||
1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,
|
||||
16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
|
||||
32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
|
||||
49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
|
||||
64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
|
||||
81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
|
||||
97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,
|
||||
112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
|
||||
128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
|
||||
145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
|
||||
161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
|
||||
176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
|
||||
193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
|
||||
208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
|
||||
224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
|
||||
241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254,
|
||||
};
|
||||
@@ -0,0 +1,967 @@
|
||||
/*
|
||||
* Copyright (c) 2005 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The document that got me started for real was "Efficient
|
||||
* Implementation of the Data Encryption Standard" by Dag Arne Osvik.
|
||||
* I never got to the PC1 transformation was working, instead I used
|
||||
* table-lookup was used for all key schedule setup. The document was
|
||||
* very useful since it de-mystified other implementations for me.
|
||||
*
|
||||
* The core DES function (SBOX + P transformation) is from Richard
|
||||
* Outerbridge public domain DES implementation. My sanity is saved
|
||||
* thanks to his work. Thank you Richard.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
RCSID("$Id: des.c,v 1.18 2006/04/24 14:26:19 lha Exp $");
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <krb5-types.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "des.h"
|
||||
#include "ui.h"
|
||||
|
||||
static void desx(uint32_t [2], DES_key_schedule *, int);
|
||||
static void IP(uint32_t [2]);
|
||||
static void FP(uint32_t [2]);
|
||||
|
||||
#include "des-tables.h"
|
||||
|
||||
#define ROTATE_LEFT28(x,one) \
|
||||
if (one) { \
|
||||
x = ( ((x)<<(1)) & 0xffffffe) | ((x) >> 27); \
|
||||
} else { \
|
||||
x = ( ((x)<<(2)) & 0xffffffc) | ((x) >> 26); \
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
int
|
||||
DES_set_odd_parity(DES_cblock *key)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < DES_CBLOCK_LEN; i++)
|
||||
(*key)[i] = odd_parity[(*key)[i]];
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
/* FIPS 74 */
|
||||
static DES_cblock weak_keys[] = {
|
||||
{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}, /* weak keys */
|
||||
{0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
|
||||
{0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
|
||||
{0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
|
||||
{0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE}, /* semi-weak keys */
|
||||
{0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
|
||||
{0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
|
||||
{0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
|
||||
{0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
|
||||
{0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
|
||||
{0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
|
||||
{0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
|
||||
{0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
|
||||
{0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
|
||||
{0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
|
||||
{0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}
|
||||
};
|
||||
|
||||
int
|
||||
DES_is_weak_key(DES_cblock *key)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < sizeof(weak_keys)/sizeof(weak_keys[0]); i++) {
|
||||
if (memcmp(weak_keys[i], key, DES_CBLOCK_LEN) == 0)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
int
|
||||
DES_set_key(DES_cblock *key, DES_key_schedule *ks)
|
||||
{
|
||||
uint32_t t1, t2;
|
||||
uint32_t c, d;
|
||||
int shifts[16] = { 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
|
||||
uint32_t *k = &ks->ks[0];
|
||||
int i;
|
||||
|
||||
t1 = (*key)[0] << 24 | (*key)[1] << 16 | (*key)[2] << 8 | (*key)[3];
|
||||
t2 = (*key)[4] << 24 | (*key)[5] << 16 | (*key)[6] << 8 | (*key)[7];
|
||||
|
||||
c = (pc1_c_3[(t1 >> (5 )) & 0x7] << 3)
|
||||
| (pc1_c_3[(t1 >> (5 + 8 )) & 0x7] << 2)
|
||||
| (pc1_c_3[(t1 >> (5 + 8 + 8 )) & 0x7] << 1)
|
||||
| (pc1_c_3[(t1 >> (5 + 8 + 8 + 8)) & 0x7] << 0)
|
||||
| (pc1_c_4[(t2 >> (4 )) & 0xf] << 3)
|
||||
| (pc1_c_4[(t2 >> (4 + 8 )) & 0xf] << 2)
|
||||
| (pc1_c_4[(t2 >> (4 + 8 + 8 )) & 0xf] << 1)
|
||||
| (pc1_c_4[(t2 >> (4 + 8 + 8 + 8)) & 0xf] << 0);
|
||||
|
||||
|
||||
d = (pc1_d_3[(t2 >> (1 )) & 0x7] << 3)
|
||||
| (pc1_d_3[(t2 >> (1 + 8 )) & 0x7] << 2)
|
||||
| (pc1_d_3[(t2 >> (1 + 8 + 8 )) & 0x7] << 1)
|
||||
| (pc1_d_3[(t2 >> (1 + 8 + 8 + 8)) & 0x7] << 0)
|
||||
| (pc1_d_4[(t1 >> (1 )) & 0xf] << 3)
|
||||
| (pc1_d_4[(t1 >> (1 + 8 )) & 0xf] << 2)
|
||||
| (pc1_d_4[(t1 >> (1 + 8 + 8 )) & 0xf] << 1)
|
||||
| (pc1_d_4[(t1 >> (1 + 8 + 8 + 8)) & 0xf] << 0);
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
uint32_t kc, kd;
|
||||
|
||||
ROTATE_LEFT28(c, shifts[i]);
|
||||
ROTATE_LEFT28(d, shifts[i]);
|
||||
|
||||
kc = pc2_c_1[(c >> 22) & 0x3f] |
|
||||
pc2_c_2[((c >> 16) & 0x30) | ((c >> 15) & 0xf)] |
|
||||
pc2_c_3[((c >> 9 ) & 0x3c) | ((c >> 8 ) & 0x3)] |
|
||||
pc2_c_4[((c >> 2 ) & 0x20) | ((c >> 1) & 0x18) | (c & 0x7)];
|
||||
kd = pc2_d_1[(d >> 22) & 0x3f] |
|
||||
pc2_d_2[((d >> 15) & 0x30) | ((d >> 14) & 0xf)] |
|
||||
pc2_d_3[ (d >> 7 ) & 0x3f] |
|
||||
pc2_d_4[((d >> 1 ) & 0x3c) | ((d ) & 0x3)];
|
||||
|
||||
/* Change to byte order used by the S boxes */
|
||||
*k = (kc & 0x00fc0000L) << 6;
|
||||
*k |= (kc & 0x00000fc0L) << 10;
|
||||
*k |= (kd & 0x00fc0000L) >> 10;
|
||||
*k++ |= (kd & 0x00000fc0L) >> 6;
|
||||
*k = (kc & 0x0003f000L) << 12;
|
||||
*k |= (kc & 0x0000003fL) << 16;
|
||||
*k |= (kd & 0x0003f000L) >> 4;
|
||||
*k++ |= (kd & 0x0000003fL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
int
|
||||
DES_set_key_checked(DES_cblock *key, DES_key_schedule *ks)
|
||||
{
|
||||
if (DES_is_weak_key(key)) {
|
||||
memset(ks, 0, sizeof(*ks));
|
||||
return 1;
|
||||
}
|
||||
return DES_set_key(key, ks);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compatibility function for eay libdes
|
||||
*/
|
||||
|
||||
int
|
||||
DES_key_sched(DES_cblock *key, DES_key_schedule *ks)
|
||||
{
|
||||
return DES_set_key(key, ks);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
static void
|
||||
load(const unsigned char *b, uint32_t v[2])
|
||||
{
|
||||
v[0] = b[0] << 24;
|
||||
v[0] |= b[1] << 16;
|
||||
v[0] |= b[2] << 8;
|
||||
v[0] |= b[3] << 0;
|
||||
v[1] = b[4] << 24;
|
||||
v[1] |= b[5] << 16;
|
||||
v[1] |= b[6] << 8;
|
||||
v[1] |= b[7] << 0;
|
||||
}
|
||||
|
||||
static void
|
||||
store(const uint32_t v[2], unsigned char *b)
|
||||
{
|
||||
b[0] = (v[0] >> 24) & 0xff;
|
||||
b[1] = (v[0] >> 16) & 0xff;
|
||||
b[2] = (v[0] >> 8) & 0xff;
|
||||
b[3] = (v[0] >> 0) & 0xff;
|
||||
b[4] = (v[1] >> 24) & 0xff;
|
||||
b[5] = (v[1] >> 16) & 0xff;
|
||||
b[6] = (v[1] >> 8) & 0xff;
|
||||
b[7] = (v[1] >> 0) & 0xff;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
DES_encrypt(uint32_t u[2], DES_key_schedule *ks, int forward_encrypt)
|
||||
{
|
||||
IP(u);
|
||||
desx(u, ks, forward_encrypt);
|
||||
FP(u);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
DES_ecb_encrypt(DES_cblock *input, DES_cblock *output,
|
||||
DES_key_schedule *ks, int forward_encrypt)
|
||||
{
|
||||
uint32_t u[2];
|
||||
load(*input, u);
|
||||
DES_encrypt(u, ks, forward_encrypt);
|
||||
store(u, *output);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
DES_cbc_encrypt(const void *in, void *out, long length,
|
||||
DES_key_schedule *ks, DES_cblock *iv, int forward_encrypt)
|
||||
{
|
||||
const unsigned char *input = in;
|
||||
unsigned char *output = out;
|
||||
uint32_t u[2];
|
||||
uint32_t uiv[2];
|
||||
|
||||
load(*iv, uiv);
|
||||
|
||||
if (forward_encrypt) {
|
||||
while (length >= DES_CBLOCK_LEN) {
|
||||
load(input, u);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
DES_encrypt(u, ks, 1);
|
||||
uiv[0] = u[0]; uiv[1] = u[1];
|
||||
store(u, output);
|
||||
|
||||
length -= DES_CBLOCK_LEN;
|
||||
input += DES_CBLOCK_LEN;
|
||||
output += DES_CBLOCK_LEN;
|
||||
}
|
||||
if (length) {
|
||||
unsigned char tmp[DES_CBLOCK_LEN];
|
||||
memcpy(tmp, input, length);
|
||||
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
|
||||
load(tmp, u);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
DES_encrypt(u, ks, 1);
|
||||
store(u, output);
|
||||
}
|
||||
} else {
|
||||
uint32_t t[2];
|
||||
while (length >= DES_CBLOCK_LEN) {
|
||||
load(input, u);
|
||||
t[0] = u[0]; t[1] = u[1];
|
||||
DES_encrypt(u, ks, 0);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
store(u, output);
|
||||
uiv[0] = t[0]; uiv[1] = t[1];
|
||||
|
||||
length -= DES_CBLOCK_LEN;
|
||||
input += DES_CBLOCK_LEN;
|
||||
output += DES_CBLOCK_LEN;
|
||||
}
|
||||
if (length) {
|
||||
unsigned char tmp[DES_CBLOCK_LEN];
|
||||
memcpy(tmp, input, length);
|
||||
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
|
||||
load(tmp, u);
|
||||
DES_encrypt(u, ks, 0);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
store(u, output);
|
||||
}
|
||||
}
|
||||
uiv[0] = 0; u[0] = 0; uiv[1] = 0; u[1] = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
DES_pcbc_encrypt(const void *in, void *out, long length,
|
||||
DES_key_schedule *ks, DES_cblock *iv, int forward_encrypt)
|
||||
{
|
||||
const unsigned char *input = in;
|
||||
unsigned char *output = out;
|
||||
uint32_t u[2];
|
||||
uint32_t uiv[2];
|
||||
|
||||
load(*iv, uiv);
|
||||
|
||||
if (forward_encrypt) {
|
||||
uint32_t t[2];
|
||||
while (length >= DES_CBLOCK_LEN) {
|
||||
load(input, u);
|
||||
t[0] = u[0]; t[1] = u[1];
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
DES_encrypt(u, ks, 1);
|
||||
uiv[0] = u[0] ^ t[0]; uiv[1] = u[1] ^ t[1];
|
||||
store(u, output);
|
||||
|
||||
length -= DES_CBLOCK_LEN;
|
||||
input += DES_CBLOCK_LEN;
|
||||
output += DES_CBLOCK_LEN;
|
||||
}
|
||||
if (length) {
|
||||
unsigned char tmp[DES_CBLOCK_LEN];
|
||||
memcpy(tmp, input, length);
|
||||
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
|
||||
load(tmp, u);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
DES_encrypt(u, ks, 1);
|
||||
store(u, output);
|
||||
}
|
||||
} else {
|
||||
uint32_t t[2];
|
||||
while (length >= DES_CBLOCK_LEN) {
|
||||
load(input, u);
|
||||
t[0] = u[0]; t[1] = u[1];
|
||||
DES_encrypt(u, ks, 0);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
store(u, output);
|
||||
uiv[0] = t[0] ^ u[0]; uiv[1] = t[1] ^ u[1];
|
||||
|
||||
length -= DES_CBLOCK_LEN;
|
||||
input += DES_CBLOCK_LEN;
|
||||
output += DES_CBLOCK_LEN;
|
||||
}
|
||||
if (length) {
|
||||
unsigned char tmp[DES_CBLOCK_LEN];
|
||||
memcpy(tmp, input, length);
|
||||
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
|
||||
load(tmp, u);
|
||||
DES_encrypt(u, ks, 0);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
}
|
||||
}
|
||||
uiv[0] = 0; u[0] = 0; uiv[1] = 0; u[1] = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
static void
|
||||
_des3_encrypt(uint32_t u[2], DES_key_schedule *ks1, DES_key_schedule *ks2,
|
||||
DES_key_schedule *ks3, int forward_encrypt)
|
||||
{
|
||||
IP(u);
|
||||
if (forward_encrypt) {
|
||||
desx(u, ks1, 1); /* IP + FP cancel out each other */
|
||||
desx(u, ks2, 0);
|
||||
desx(u, ks3, 1);
|
||||
} else {
|
||||
desx(u, ks3, 0);
|
||||
desx(u, ks2, 1);
|
||||
desx(u, ks1, 0);
|
||||
}
|
||||
FP(u);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
DES_ecb3_encrypt(DES_cblock *input,
|
||||
DES_cblock *output,
|
||||
DES_key_schedule *ks1,
|
||||
DES_key_schedule *ks2,
|
||||
DES_key_schedule *ks3,
|
||||
int forward_encrypt)
|
||||
{
|
||||
uint32_t u[2];
|
||||
load(*input, u);
|
||||
_des3_encrypt(u, ks1, ks2, ks3, forward_encrypt);
|
||||
store(u, *output);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
DES_ede3_cbc_encrypt(const void *in, void *out,
|
||||
long length, DES_key_schedule *ks1,
|
||||
DES_key_schedule *ks2, DES_key_schedule *ks3,
|
||||
DES_cblock *iv, int forward_encrypt)
|
||||
{
|
||||
const unsigned char *input = in;
|
||||
unsigned char *output = out;
|
||||
uint32_t u[2];
|
||||
uint32_t uiv[2];
|
||||
|
||||
load(*iv, uiv);
|
||||
|
||||
if (forward_encrypt) {
|
||||
while (length >= DES_CBLOCK_LEN) {
|
||||
load(input, u);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
_des3_encrypt(u, ks1, ks2, ks3, 1);
|
||||
uiv[0] = u[0]; uiv[1] = u[1];
|
||||
store(u, output);
|
||||
|
||||
length -= DES_CBLOCK_LEN;
|
||||
input += DES_CBLOCK_LEN;
|
||||
output += DES_CBLOCK_LEN;
|
||||
}
|
||||
if (length) {
|
||||
unsigned char tmp[DES_CBLOCK_LEN];
|
||||
memcpy(tmp, input, length);
|
||||
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
|
||||
load(tmp, u);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
_des3_encrypt(u, ks1, ks2, ks3, 1);
|
||||
store(u, output);
|
||||
}
|
||||
} else {
|
||||
uint32_t t[2];
|
||||
while (length >= DES_CBLOCK_LEN) {
|
||||
load(input, u);
|
||||
t[0] = u[0]; t[1] = u[1];
|
||||
_des3_encrypt(u, ks1, ks2, ks3, 0);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
store(u, output);
|
||||
uiv[0] = t[0]; uiv[1] = t[1];
|
||||
|
||||
length -= DES_CBLOCK_LEN;
|
||||
input += DES_CBLOCK_LEN;
|
||||
output += DES_CBLOCK_LEN;
|
||||
}
|
||||
if (length) {
|
||||
unsigned char tmp[DES_CBLOCK_LEN];
|
||||
memcpy(tmp, input, length);
|
||||
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
|
||||
load(tmp, u);
|
||||
_des3_encrypt(u, ks1, ks2, ks3, 0);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
store(u, output);
|
||||
}
|
||||
}
|
||||
store(uiv, *iv);
|
||||
uiv[0] = 0; u[0] = 0; uiv[1] = 0; u[1] = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
DES_cfb64_encrypt(const void *in, void *out,
|
||||
long length, DES_key_schedule *ks, DES_cblock *iv,
|
||||
int *num, int forward_encrypt)
|
||||
{
|
||||
const unsigned char *input = in;
|
||||
unsigned char *output = out;
|
||||
unsigned char tmp[DES_CBLOCK_LEN];
|
||||
uint32_t uiv[2];
|
||||
|
||||
load(*iv, uiv);
|
||||
|
||||
assert(*num >= 0 && *num < DES_CBLOCK_LEN);
|
||||
|
||||
if (forward_encrypt) {
|
||||
int i = *num;
|
||||
|
||||
while (length > 0) {
|
||||
if (i == 0)
|
||||
DES_encrypt(uiv, ks, 1);
|
||||
store(uiv, tmp);
|
||||
for (; i < DES_CBLOCK_LEN && i < length; i++) {
|
||||
output[i] = tmp[i] ^ input[i];
|
||||
}
|
||||
if (i == DES_CBLOCK_LEN)
|
||||
load(output, uiv);
|
||||
output += i;
|
||||
input += i;
|
||||
length -= i;
|
||||
if (i == DES_CBLOCK_LEN)
|
||||
i = 0;
|
||||
}
|
||||
store(uiv, *iv);
|
||||
*num = i;
|
||||
} else {
|
||||
int i = *num;
|
||||
unsigned char c;
|
||||
|
||||
while (length > 0) {
|
||||
if (i == 0) {
|
||||
DES_encrypt(uiv, ks, 1);
|
||||
store(uiv, tmp);
|
||||
}
|
||||
for (; i < DES_CBLOCK_LEN && i < length; i++) {
|
||||
c = input[i];
|
||||
output[i] = tmp[i] ^ input[i];
|
||||
(*iv)[i] = c;
|
||||
}
|
||||
output += i;
|
||||
input += i;
|
||||
length -= i;
|
||||
if (i == DES_CBLOCK_LEN) {
|
||||
i = 0;
|
||||
load(*iv, uiv);
|
||||
}
|
||||
}
|
||||
store(uiv, *iv);
|
||||
*num = i;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
uint32_t
|
||||
DES_cbc_cksum(const void *in, DES_cblock *output,
|
||||
long length, DES_key_schedule *ks, DES_cblock *iv)
|
||||
{
|
||||
const unsigned char *input = in;
|
||||
uint32_t uiv[2];
|
||||
uint32_t u[2] = { 0, 0 };
|
||||
|
||||
load(*iv, uiv);
|
||||
|
||||
while (length >= DES_CBLOCK_LEN) {
|
||||
load(input, u);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
DES_encrypt(u, ks, 1);
|
||||
uiv[0] = u[0]; uiv[1] = u[1];
|
||||
|
||||
length -= DES_CBLOCK_LEN;
|
||||
input += DES_CBLOCK_LEN;
|
||||
}
|
||||
if (length) {
|
||||
unsigned char tmp[DES_CBLOCK_LEN];
|
||||
memcpy(tmp, input, length);
|
||||
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
|
||||
load(tmp, u);
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
DES_encrypt(u, ks, 1);
|
||||
}
|
||||
if (output)
|
||||
store(u, *output);
|
||||
|
||||
uiv[0] = 0; u[0] = 0; uiv[1] = 0;
|
||||
return u[1];
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
static unsigned char
|
||||
bitswap8(unsigned char b)
|
||||
{
|
||||
unsigned char r = 0;
|
||||
int i;
|
||||
for (i = 0; i < 8; i++) {
|
||||
r = r << 1 | (b & 1);
|
||||
b = b >> 1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void
|
||||
DES_string_to_key(const char *str, DES_cblock *key)
|
||||
{
|
||||
const unsigned char *s;
|
||||
unsigned char *k;
|
||||
DES_key_schedule ks;
|
||||
size_t i, len;
|
||||
|
||||
memset(key, 0, sizeof(*key));
|
||||
k = *key;
|
||||
s = (const unsigned char *)str;
|
||||
|
||||
len = strlen(str);
|
||||
for (i = 0; i < len; i++) {
|
||||
if ((i % 16) < 8)
|
||||
k[i % 8] ^= s[i] << 1;
|
||||
else
|
||||
k[7 - (i % 8)] ^= bitswap8(s[i]);
|
||||
}
|
||||
DES_set_odd_parity(key);
|
||||
if (DES_is_weak_key(key))
|
||||
k[7] ^= 0xF0;
|
||||
DES_set_key(key, &ks);
|
||||
DES_cbc_cksum(s, key, len, &ks, key);
|
||||
memset(&ks, 0, sizeof(ks));
|
||||
DES_set_odd_parity(key);
|
||||
if (DES_is_weak_key(key))
|
||||
k[7] ^= 0xF0;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
int
|
||||
DES_read_password(DES_cblock *key, char *prompt, int verify)
|
||||
{
|
||||
char buf[512];
|
||||
int ret;
|
||||
|
||||
ret = UI_UTIL_read_pw_string(buf, sizeof(buf) - 1, prompt, verify);
|
||||
if (ret == 0)
|
||||
DES_string_to_key(buf, key);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
void
|
||||
_DES_ipfp_test(void)
|
||||
{
|
||||
DES_cblock k = "\x01\x02\x04\x08\x10\x20\x40\x80", k2;
|
||||
uint32_t u[2] = { 1, 0 };
|
||||
IP(u);
|
||||
FP(u);
|
||||
IP(u);
|
||||
FP(u);
|
||||
if (u[0] != 1 || u[1] != 0)
|
||||
abort();
|
||||
|
||||
load(k, u);
|
||||
store(u, k2);
|
||||
if (memcmp(k, k2, 8) != 0)
|
||||
abort();
|
||||
}
|
||||
|
||||
/* D3DES (V5.09) -
|
||||
*
|
||||
* A portable, public domain, version of the Data Encryption Standard.
|
||||
*
|
||||
* Written with Symantec's THINK (Lightspeed) C by Richard Outerbridge.
|
||||
* Thanks to: Dan Hoey for his excellent Initial and Inverse permutation
|
||||
* code; Jim Gillogly & Phil Karn for the DES key schedule code; Dennis
|
||||
* Ferguson, Eric Young and Dana How for comparing notes; and Ray Lau,
|
||||
* for humouring me on.
|
||||
*
|
||||
* Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge.
|
||||
* (GEnie : OUTER; CIS : [71755,204]) Graven Imagery, 1992.
|
||||
*/
|
||||
|
||||
static uint32_t SP1[64] = {
|
||||
0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,
|
||||
0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,
|
||||
0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,
|
||||
0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,
|
||||
0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,
|
||||
0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,
|
||||
0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,
|
||||
0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,
|
||||
0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,
|
||||
0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,
|
||||
0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,
|
||||
0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,
|
||||
0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,
|
||||
0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,
|
||||
0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,
|
||||
0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L };
|
||||
|
||||
static uint32_t SP2[64] = {
|
||||
0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,
|
||||
0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,
|
||||
0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,
|
||||
0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,
|
||||
0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,
|
||||
0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,
|
||||
0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,
|
||||
0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,
|
||||
0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,
|
||||
0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,
|
||||
0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,
|
||||
0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,
|
||||
0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,
|
||||
0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,
|
||||
0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,
|
||||
0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L };
|
||||
|
||||
static uint32_t SP3[64] = {
|
||||
0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,
|
||||
0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,
|
||||
0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,
|
||||
0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,
|
||||
0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,
|
||||
0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,
|
||||
0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,
|
||||
0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,
|
||||
0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,
|
||||
0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,
|
||||
0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,
|
||||
0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,
|
||||
0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,
|
||||
0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,
|
||||
0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,
|
||||
0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L };
|
||||
|
||||
static uint32_t SP4[64] = {
|
||||
0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
|
||||
0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,
|
||||
0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,
|
||||
0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,
|
||||
0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,
|
||||
0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,
|
||||
0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,
|
||||
0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,
|
||||
0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,
|
||||
0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,
|
||||
0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,
|
||||
0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
|
||||
0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,
|
||||
0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,
|
||||
0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,
|
||||
0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L };
|
||||
|
||||
static uint32_t SP5[64] = {
|
||||
0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,
|
||||
0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,
|
||||
0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,
|
||||
0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,
|
||||
0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,
|
||||
0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,
|
||||
0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,
|
||||
0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,
|
||||
0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,
|
||||
0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L,
|
||||
0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L,
|
||||
0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L,
|
||||
0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L,
|
||||
0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L,
|
||||
0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L,
|
||||
0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L };
|
||||
|
||||
static uint32_t SP6[64] = {
|
||||
0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L,
|
||||
0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L,
|
||||
0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L,
|
||||
0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L,
|
||||
0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L,
|
||||
0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L,
|
||||
0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L,
|
||||
0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L,
|
||||
0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L,
|
||||
0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L,
|
||||
0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L,
|
||||
0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L,
|
||||
0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L,
|
||||
0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L,
|
||||
0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L,
|
||||
0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L };
|
||||
|
||||
static uint32_t SP7[64] = {
|
||||
0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,
|
||||
0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,
|
||||
0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,
|
||||
0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,
|
||||
0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,
|
||||
0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,
|
||||
0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,
|
||||
0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,
|
||||
0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,
|
||||
0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,
|
||||
0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,
|
||||
0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,
|
||||
0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,
|
||||
0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,
|
||||
0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,
|
||||
0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L };
|
||||
|
||||
static uint32_t SP8[64] = {
|
||||
0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,
|
||||
0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,
|
||||
0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,
|
||||
0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,
|
||||
0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,
|
||||
0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,
|
||||
0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,
|
||||
0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,
|
||||
0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,
|
||||
0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,
|
||||
0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,
|
||||
0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,
|
||||
0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,
|
||||
0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,
|
||||
0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,
|
||||
0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L };
|
||||
|
||||
static void
|
||||
IP(uint32_t v[2])
|
||||
{
|
||||
uint32_t work;
|
||||
|
||||
work = ((v[0] >> 4) ^ v[1]) & 0x0f0f0f0fL;
|
||||
v[1] ^= work;
|
||||
v[0] ^= (work << 4);
|
||||
work = ((v[0] >> 16) ^ v[1]) & 0x0000ffffL;
|
||||
v[1] ^= work;
|
||||
v[0] ^= (work << 16);
|
||||
work = ((v[1] >> 2) ^ v[0]) & 0x33333333L;
|
||||
v[0] ^= work;
|
||||
v[1] ^= (work << 2);
|
||||
work = ((v[1] >> 8) ^ v[0]) & 0x00ff00ffL;
|
||||
v[0] ^= work;
|
||||
v[1] ^= (work << 8);
|
||||
v[1] = ((v[1] << 1) | ((v[1] >> 31) & 1L)) & 0xffffffffL;
|
||||
work = (v[0] ^ v[1]) & 0xaaaaaaaaL;
|
||||
v[0] ^= work;
|
||||
v[1] ^= work;
|
||||
v[0] = ((v[0] << 1) | ((v[0] >> 31) & 1L)) & 0xffffffffL;
|
||||
}
|
||||
|
||||
static void
|
||||
FP(uint32_t v[2])
|
||||
{
|
||||
uint32_t work;
|
||||
|
||||
v[0] = (v[0] << 31) | (v[0] >> 1);
|
||||
work = (v[1] ^ v[0]) & 0xaaaaaaaaL;
|
||||
v[1] ^= work;
|
||||
v[0] ^= work;
|
||||
v[1] = (v[1] << 31) | (v[1] >> 1);
|
||||
work = ((v[1] >> 8) ^ v[0]) & 0x00ff00ffL;
|
||||
v[0] ^= work;
|
||||
v[1] ^= (work << 8);
|
||||
work = ((v[1] >> 2) ^ v[0]) & 0x33333333L;
|
||||
v[0] ^= work;
|
||||
v[1] ^= (work << 2);
|
||||
work = ((v[0] >> 16) ^ v[1]) & 0x0000ffffL;
|
||||
v[1] ^= work;
|
||||
v[0] ^= (work << 16);
|
||||
work = ((v[0] >> 4) ^ v[1]) & 0x0f0f0f0fL;
|
||||
v[1] ^= work;
|
||||
v[0] ^= (work << 4);
|
||||
}
|
||||
|
||||
static void
|
||||
desx(uint32_t block[2], DES_key_schedule *ks, int forward_encrypt)
|
||||
{
|
||||
uint32_t *keys;
|
||||
uint32_t fval, work, right, left;
|
||||
int round;
|
||||
|
||||
left = block[0];
|
||||
right = block[1];
|
||||
|
||||
if (forward_encrypt) {
|
||||
keys = &ks->ks[0];
|
||||
|
||||
for( round = 0; round < 8; round++ ) {
|
||||
work = (right << 28) | (right >> 4);
|
||||
work ^= *keys++;
|
||||
fval = SP7[ work & 0x3fL];
|
||||
fval |= SP5[(work >> 8) & 0x3fL];
|
||||
fval |= SP3[(work >> 16) & 0x3fL];
|
||||
fval |= SP1[(work >> 24) & 0x3fL];
|
||||
work = right ^ *keys++;
|
||||
fval |= SP8[ work & 0x3fL];
|
||||
fval |= SP6[(work >> 8) & 0x3fL];
|
||||
fval |= SP4[(work >> 16) & 0x3fL];
|
||||
fval |= SP2[(work >> 24) & 0x3fL];
|
||||
left ^= fval;
|
||||
work = (left << 28) | (left >> 4);
|
||||
work ^= *keys++;
|
||||
fval = SP7[ work & 0x3fL];
|
||||
fval |= SP5[(work >> 8) & 0x3fL];
|
||||
fval |= SP3[(work >> 16) & 0x3fL];
|
||||
fval |= SP1[(work >> 24) & 0x3fL];
|
||||
work = left ^ *keys++;
|
||||
fval |= SP8[ work & 0x3fL];
|
||||
fval |= SP6[(work >> 8) & 0x3fL];
|
||||
fval |= SP4[(work >> 16) & 0x3fL];
|
||||
fval |= SP2[(work >> 24) & 0x3fL];
|
||||
right ^= fval;
|
||||
}
|
||||
} else {
|
||||
keys = &ks->ks[30];
|
||||
|
||||
for( round = 0; round < 8; round++ ) {
|
||||
work = (right << 28) | (right >> 4);
|
||||
work ^= *keys++;
|
||||
fval = SP7[ work & 0x3fL];
|
||||
fval |= SP5[(work >> 8) & 0x3fL];
|
||||
fval |= SP3[(work >> 16) & 0x3fL];
|
||||
fval |= SP1[(work >> 24) & 0x3fL];
|
||||
work = right ^ *keys++;
|
||||
fval |= SP8[ work & 0x3fL];
|
||||
fval |= SP6[(work >> 8) & 0x3fL];
|
||||
fval |= SP4[(work >> 16) & 0x3fL];
|
||||
fval |= SP2[(work >> 24) & 0x3fL];
|
||||
left ^= fval;
|
||||
work = (left << 28) | (left >> 4);
|
||||
keys -= 4;
|
||||
work ^= *keys++;
|
||||
fval = SP7[ work & 0x3fL];
|
||||
fval |= SP5[(work >> 8) & 0x3fL];
|
||||
fval |= SP3[(work >> 16) & 0x3fL];
|
||||
fval |= SP1[(work >> 24) & 0x3fL];
|
||||
work = left ^ *keys++;
|
||||
fval |= SP8[ work & 0x3fL];
|
||||
fval |= SP6[(work >> 8) & 0x3fL];
|
||||
fval |= SP4[(work >> 16) & 0x3fL];
|
||||
fval |= SP2[(work >> 24) & 0x3fL];
|
||||
right ^= fval;
|
||||
keys -= 4;
|
||||
}
|
||||
}
|
||||
block[0] = right;
|
||||
block[1] = left;
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 2005 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: des.h,v 1.25 2006/01/08 21:47:28 lha Exp $ */
|
||||
|
||||
#ifndef _DESperate_H
|
||||
#define _DESperate_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define DES_set_odd_parity hc_DES_set_odd_parity
|
||||
#define DES_is_weak_key hc_DES_is_weak_key
|
||||
#define DES_key_sched hc_DES_key_sched
|
||||
#define DES_set_key hc_DES_set_key
|
||||
#define DES_set_key_checked hc_DES_set_key_checked
|
||||
#define DES_set_key_sched hc_DES_set_key_sched
|
||||
#define DES_new_random_key hc_DES_new_random_key
|
||||
#define DES_string_to_key hc_DES_string_to_key
|
||||
#define DES_read_password hc_DES_read_password
|
||||
#define DES_rand_data hc_DES_rand_data
|
||||
#define DES_set_random_generator_seed hc_DES_set_random_generator_seed
|
||||
#define DES_generate_random_block hc_DES_generate_random_block
|
||||
#define DES_set_sequence_number hc_DES_set_sequence_number
|
||||
#define DES_init_random_number_generator hc_DES_init_random_number_generator
|
||||
#define DES_random_key hc_DES_random_key
|
||||
#define DES_encrypt hc_DES_encrypt
|
||||
#define DES_ecb_encrypt hc_DES_ecb_encrypt
|
||||
#define DES_ecb3_encrypt hc_DES_ecb3_encrypt
|
||||
#define DES_pcbc_encrypt hc_DES_pcbc_encrypt
|
||||
#define DES_cbc_encrypt hc_DES_cbc_encrypt
|
||||
#define DES_cbc_cksum hc_DES_cbc_cksum
|
||||
#define DES_ede3_cbc_encrypt hc_DES_ede3_cbc_encrypt
|
||||
#define DES_cfb64_encrypt hc_DES_cfb64_encrypt
|
||||
#define _DES_ipfp_test _hc_DES_ipfp_test
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#define DES_CBLOCK_LEN 8
|
||||
#define DES_KEY_SZ 8
|
||||
|
||||
#define DES_ENCRYPT 1
|
||||
#define DES_DECRYPT 0
|
||||
|
||||
typedef unsigned char DES_cblock[DES_CBLOCK_LEN];
|
||||
typedef struct DES_key_schedule
|
||||
{
|
||||
uint32_t ks[32];
|
||||
} DES_key_schedule;
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
int DES_set_odd_parity(DES_cblock *);
|
||||
int DES_is_weak_key(DES_cblock *);
|
||||
int DES_set_key(DES_cblock *, DES_key_schedule *);
|
||||
int DES_set_key_checked(DES_cblock *, DES_key_schedule *);
|
||||
int DES_key_sched(DES_cblock *, DES_key_schedule *);
|
||||
int DES_new_random_key(DES_cblock *);
|
||||
void DES_string_to_key(const char *, DES_cblock *);
|
||||
int DES_read_password(DES_cblock *, char *, int);
|
||||
|
||||
void DES_rand_data(void *, int);
|
||||
void DES_set_random_generator_seed(DES_cblock *);
|
||||
void DES_generate_random_block(DES_cblock *);
|
||||
void DES_set_sequence_number(void *);
|
||||
void DES_init_random_number_generator(DES_cblock *);
|
||||
void DES_random_key(DES_cblock *);
|
||||
|
||||
|
||||
void DES_encrypt(uint32_t [2], DES_key_schedule *, int);
|
||||
void DES_ecb_encrypt(DES_cblock *, DES_cblock *, DES_key_schedule *, int);
|
||||
void DES_ecb3_encrypt(DES_cblock *,DES_cblock *, DES_key_schedule *,
|
||||
DES_key_schedule *, DES_key_schedule *, int);
|
||||
void DES_pcbc_encrypt(const void *, void *, long,
|
||||
DES_key_schedule *, DES_cblock *, int);
|
||||
void DES_cbc_encrypt(const void *, void *, long,
|
||||
DES_key_schedule *, DES_cblock *, int);
|
||||
void DES_ede3_cbc_encrypt(const void *, void *, long,
|
||||
DES_key_schedule *, DES_key_schedule *,
|
||||
DES_key_schedule *, DES_cblock *, int);
|
||||
void DES_cfb64_encrypt(const void *, void *, long,
|
||||
DES_key_schedule *, DES_cblock *, int *, int);
|
||||
|
||||
|
||||
uint32_t DES_cbc_cksum(const void *, DES_cblock *,
|
||||
long, DES_key_schedule *, DES_cblock *);
|
||||
|
||||
|
||||
void _DES_ipfp_test(void);
|
||||
|
||||
|
||||
#endif /* _DESperate_H */
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* $Id: dh.h,v 1.6 2006/05/06 13:11:15 lha Exp $
|
||||
*/
|
||||
|
||||
#ifndef _HEIM_DH_H
|
||||
#define _HEIM_DH_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define DH_null_method hc_DH_null_method
|
||||
#define DH_imath_method hc_DH_imath_method
|
||||
#define DH_new hc_DH_new
|
||||
#define DH_new_method hc_DH_new_method
|
||||
#define DH_free hc_DH_free
|
||||
#define DH_up_ref hc_DH_up_ref
|
||||
#define DH_size hc_DH_size
|
||||
#define DH_set_default_method hc_DH_set_default_method
|
||||
#define DH_get_default_method hc_DH_get_default_method
|
||||
#define DH_set_method hc_DH_set_method
|
||||
#define DH_get_method hc_DH_get_method
|
||||
#define DH_set_ex_data hc_DH_set_ex_data
|
||||
#define DH_get_ex_data hc_DH_get_ex_data
|
||||
#define DH_generate_parameters_ex hc_DH_generate_parameters_ex
|
||||
#define DH_check_pubkey hc_DH_check_pubkey
|
||||
#define DH_generate_key hc_DH_generate_key
|
||||
#define DH_compute_key hc_DH_compute_key
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
typedef struct DH DH;
|
||||
typedef struct DH_METHOD DH_METHOD;
|
||||
|
||||
#include <hcrypto/bn.h>
|
||||
#include <hcrypto/engine.h>
|
||||
|
||||
struct DH_METHOD {
|
||||
const char *name;
|
||||
int (*generate_key)(DH *);
|
||||
int (*compute_key)(unsigned char *,const BIGNUM *,DH *);
|
||||
int (*bn_mod_exp)(const DH *, BIGNUM *, const BIGNUM *,
|
||||
const BIGNUM *, const BIGNUM *, BN_CTX *,
|
||||
BN_MONT_CTX *);
|
||||
int (*init)(DH *);
|
||||
int (*finish)(DH *);
|
||||
int flags;
|
||||
void *app_data;
|
||||
int (*generate_params)(DH *, int, int, BN_GENCB *);
|
||||
};
|
||||
|
||||
struct DH {
|
||||
int pad;
|
||||
int version;
|
||||
BIGNUM *p;
|
||||
BIGNUM *g;
|
||||
long length;
|
||||
BIGNUM *pub_key;
|
||||
BIGNUM *priv_key;
|
||||
int flags;
|
||||
void *method_mont_p;
|
||||
BIGNUM *q;
|
||||
BIGNUM *j;
|
||||
void *seed;
|
||||
int seedlen;
|
||||
BIGNUM *counter;
|
||||
int references;
|
||||
struct CRYPTO_EX_DATA {
|
||||
void *sk;
|
||||
int dummy;
|
||||
} ex_data;
|
||||
const DH_METHOD *meth;
|
||||
ENGINE *engine;
|
||||
};
|
||||
|
||||
/* DH_check_pubkey return codes in `codes' argument. */
|
||||
#define DH_CHECK_PUBKEY_TOO_SMALL 1
|
||||
#define DH_CHECK_PUBKEY_TOO_LARGE 2
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
const DH_METHOD *DH_null_method(void);
|
||||
const DH_METHOD *DH_imath_method(void);
|
||||
|
||||
DH * DH_new(void);
|
||||
DH * DH_new_method(ENGINE *);
|
||||
void DH_free(DH *);
|
||||
int DH_up_ref(DH *);
|
||||
|
||||
int DH_size(const DH *);
|
||||
|
||||
|
||||
void DH_set_default_method(const DH_METHOD *);
|
||||
const DH_METHOD *
|
||||
DH_get_default_method(void);
|
||||
int DH_set_method(DH *, const DH_METHOD *);
|
||||
|
||||
int DH_set_ex_data(DH *, int, void *);
|
||||
void * DH_get_ex_data(DH *, int);
|
||||
|
||||
int DH_generate_parameters_ex(DH *, int, int, BN_GENCB *);
|
||||
int DH_check_pubkey(const DH *, const BIGNUM *, int *);
|
||||
int DH_generate_key(DH *);
|
||||
int DH_compute_key(unsigned char *,const BIGNUM *,DH *);
|
||||
|
||||
#endif /* _HEIM_DH_H */
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* $Id: dsa.h,v 1.2 2006/01/13 15:26:52 lha Exp $
|
||||
*/
|
||||
|
||||
#ifndef _HEIM_DSA_H
|
||||
#define _HEIM_DSA_H 1
|
||||
|
||||
#include <hcrypto/bn.h>
|
||||
|
||||
/* symbol renaming */
|
||||
#define DSA_null_method hc_DSA_null_method
|
||||
#define DSA_new hc_DSA_new
|
||||
#define DSA_free hc_DSA_free
|
||||
#define DSA_up_ref hc_DSA_up_ref
|
||||
#define DSA_set_default_method hc_DSA_set_default_method
|
||||
#define DSA_get_default_method hc_DSA_get_default_method
|
||||
#define DSA_set_method hc_DSA_set_method
|
||||
#define DSA_get_method hc_DSA_get_method
|
||||
#define DSA_set_app_data hc_DSA_set_app_data
|
||||
#define DSA_get_app_data hc_DSA_get_app_data
|
||||
#define DSA_size hc_DSA_size
|
||||
#define DSA_verify hc_DSA_verify
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
typedef struct DSA DSA;
|
||||
typedef struct DSA_METHOD DSA_METHOD;
|
||||
typedef struct DSA_SIG DSA_SIG;
|
||||
|
||||
struct DSA_SIG {
|
||||
BIGNUM *r;
|
||||
BIGNUM *s;
|
||||
};
|
||||
|
||||
struct DSA_METHOD {
|
||||
const char *name;
|
||||
DSA_SIG * (*dsa_do_sign)(const unsigned char *, int, DSA *);
|
||||
int (*dsa_sign_setup)(DSA *, BN_CTX *, BIGNUM **, BIGNUM **);
|
||||
int (*dsa_do_verify)(const unsigned char *, int, DSA_SIG *, DSA *);
|
||||
int (*dsa_mod_exp)(DSA *, BIGNUM *, BIGNUM *, BIGNUM *,
|
||||
BIGNUM *, BIGNUM *, BIGNUM *, BN_CTX *,
|
||||
BN_MONT_CTX *);
|
||||
int (*bn_mod_exp)(DSA *, BIGNUM *, BIGNUM *, const BIGNUM *,
|
||||
const BIGNUM *, BN_CTX *,
|
||||
BN_MONT_CTX *);
|
||||
int (*init)(DSA *);
|
||||
int (*finish)(DSA *);
|
||||
int flags;
|
||||
void *app_data;
|
||||
};
|
||||
|
||||
struct DSA {
|
||||
int pad;
|
||||
long version;
|
||||
int write_params;
|
||||
BIGNUM *p;
|
||||
BIGNUM *q;
|
||||
BIGNUM *g;
|
||||
|
||||
BIGNUM *pub_key;
|
||||
BIGNUM *priv_key;
|
||||
|
||||
BIGNUM *kinv;
|
||||
BIGNUM *r;
|
||||
int flags;
|
||||
void *method_mont_p;
|
||||
int references;
|
||||
struct dsa_CRYPTO_EX_DATA {
|
||||
void *sk;
|
||||
int dummy;
|
||||
} ex_data;
|
||||
const DSA_METHOD *meth;
|
||||
void *engine;
|
||||
};
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
const DSA_METHOD *DSA_null_method(void);
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
DSA * DSA_new(void);
|
||||
void DSA_free(DSA *);
|
||||
int DSA_up_ref(DSA *);
|
||||
|
||||
void DSA_set_default_method(const DSA_METHOD *);
|
||||
const DSA_METHOD * DSA_get_default_method(void);
|
||||
|
||||
const DSA_METHOD * DSA_get_method(const DSA *);
|
||||
int DSA_set_method(DSA *, const DSA_METHOD *);
|
||||
|
||||
void DSA_set_app_data(DSA *, void *arg);
|
||||
void * DSA_get_app_data(DSA *);
|
||||
|
||||
int DSA_size(const DSA *);
|
||||
|
||||
int DSA_verify(int, const unsigned char *, int,
|
||||
const unsigned char *, int, DSA *);
|
||||
|
||||
#endif /* _HEIM_DSA_H */
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* $Id: engine.h,v 1.6 2006/05/06 12:34:36 lha Exp $
|
||||
*/
|
||||
|
||||
#ifndef _HEIM_ENGINE_H
|
||||
#define _HEIM_ENGINE_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define ENGINE_add_conf_module hc_ENGINE_add_conf_module
|
||||
#define ENGINE_by_dso hc_ENGINE_by_dso
|
||||
#define ENGINE_by_id hc_ENGINE_by_id
|
||||
#define ENGINE_finish hc_ENGINE_finish
|
||||
#define ENGINE_get_DH hc_ENGINE_get_DH
|
||||
#define ENGINE_get_RSA hc_ENGINE_get_RSA
|
||||
#define ENGINE_get_RAND hc_ENGINE_get_RAND
|
||||
#define ENGINE_get_id hc_ENGINE_get_id
|
||||
#define ENGINE_get_name hc_ENGINE_get_name
|
||||
#define ENGINE_load_builtin_engines hc_ENGINE_load_builtin_engines
|
||||
#define ENGINE_set_DH hc_ENGINE_set_DH
|
||||
#define ENGINE_set_RSA hc_ENGINE_set_RSA
|
||||
#define ENGINE_set_id hc_ENGINE_set_id
|
||||
#define ENGINE_set_name hc_ENGINE_set_name
|
||||
#define ENGINE_set_destroy_function hc_ENGINE_set_destroy_function
|
||||
#define ENGINE_up_ref hc_ENGINE_up_ref
|
||||
#define ENGINE_get_default_DH hc_ENGINE_get_default_DH
|
||||
#define ENGINE_get_default_RSA hc_ENGINE_get_default_RSA
|
||||
#define ENGINE_set_default_DH hc_ENGINE_set_default_DH
|
||||
#define ENGINE_set_default_RSA hc_ENGINE_set_default_RSA
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
typedef struct hc_engine ENGINE;
|
||||
|
||||
#include <hcrypto/rsa.h>
|
||||
#include <hcrypto/dsa.h>
|
||||
#include <hcrypto/dh.h>
|
||||
#include <hcrypto/rand.h>
|
||||
|
||||
#define OPENSSL_DYNAMIC_VERSION (unsigned long)0x00020000
|
||||
|
||||
typedef int (*openssl_bind_engine)(ENGINE *, const char *, const void *);
|
||||
typedef unsigned long (*openssl_v_check)(unsigned long);
|
||||
|
||||
void ENGINE_add_conf_module(void);
|
||||
void ENGINE_load_builtin_engines(void);
|
||||
ENGINE *ENGINE_by_id(const char *);
|
||||
ENGINE *ENGINE_by_dso(const char *, const char *);
|
||||
int ENGINE_finish(ENGINE *);
|
||||
int ENGINE_up_ref(ENGINE *);
|
||||
int ENGINE_set_id(ENGINE *, const char *);
|
||||
int ENGINE_set_name(ENGINE *, const char *);
|
||||
int ENGINE_set_RSA(ENGINE *, const RSA_METHOD *);
|
||||
int ENGINE_set_DH(ENGINE *, const DH_METHOD *);
|
||||
int ENGINE_set_destroy_function(ENGINE *, void (*)(ENGINE *));
|
||||
|
||||
const char * ENGINE_get_id(const ENGINE *);
|
||||
const char * ENGINE_get_name(const ENGINE *);
|
||||
const RSA_METHOD * ENGINE_get_RSA(const ENGINE *);
|
||||
const DH_METHOD * ENGINE_get_DH(const ENGINE *);
|
||||
const RAND_METHOD * ENGINE_get_RAND(const ENGINE *);
|
||||
|
||||
int ENGINE_set_default_RSA(ENGINE *);
|
||||
ENGINE * ENGINE_get_default_RSA(void);
|
||||
int ENGINE_set_default_DH(ENGINE *);
|
||||
ENGINE * ENGINE_get_default_DH(void);
|
||||
|
||||
|
||||
#endif /* _HEIM_ENGINE_H */
|
||||
@@ -0,0 +1,905 @@
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <evp.h>
|
||||
|
||||
#include <krb5-types.h>
|
||||
|
||||
#include <aes.h>
|
||||
#include <des.h>
|
||||
#include <sha.h>
|
||||
#include <rc2.h>
|
||||
#include <rc4.h>
|
||||
#include <md2.h>
|
||||
#include <md4.h>
|
||||
#include <md5.h>
|
||||
|
||||
typedef int (*evp_md_init)(EVP_MD_CTX *);
|
||||
typedef int (*evp_md_update)(EVP_MD_CTX *,const void *, size_t);
|
||||
typedef int (*evp_md_final)(void *, EVP_MD_CTX *);
|
||||
typedef int (*evp_md_cleanup)(EVP_MD_CTX *);
|
||||
|
||||
struct hc_evp_md {
|
||||
int hash_size;
|
||||
int block_size;
|
||||
int ctx_size;
|
||||
evp_md_init init;
|
||||
evp_md_update update;
|
||||
evp_md_final final;
|
||||
evp_md_cleanup cleanup;
|
||||
};
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
size_t
|
||||
EVP_MD_size(const EVP_MD *md)
|
||||
{
|
||||
return md->hash_size;
|
||||
}
|
||||
|
||||
size_t
|
||||
EVP_MD_block_size(const EVP_MD *md)
|
||||
{
|
||||
return md->block_size;
|
||||
}
|
||||
|
||||
EVP_MD_CTX *
|
||||
EVP_MD_CTX_create(void)
|
||||
{
|
||||
return calloc(1, sizeof(EVP_MD_CTX));
|
||||
}
|
||||
|
||||
void
|
||||
EVP_MD_CTX_init(EVP_MD_CTX *ctx)
|
||||
{
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
|
||||
void
|
||||
EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
|
||||
{
|
||||
EVP_MD_CTX_cleanup(ctx);
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
int
|
||||
EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
|
||||
{
|
||||
if (ctx->md && ctx->md->cleanup)
|
||||
(ctx->md->cleanup)(ctx);
|
||||
ctx->md = NULL;
|
||||
ctx->engine = NULL;
|
||||
free(ctx->ptr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
const EVP_MD *
|
||||
EVP_MD_CTX_md(EVP_MD_CTX *ctx)
|
||||
{
|
||||
return ctx->md;
|
||||
}
|
||||
|
||||
size_t
|
||||
EVP_MD_CTX_size(EVP_MD_CTX *ctx)
|
||||
{
|
||||
return EVP_MD_size(ctx->md);
|
||||
}
|
||||
|
||||
size_t
|
||||
EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
|
||||
{
|
||||
return EVP_MD_block_size(ctx->md);
|
||||
}
|
||||
|
||||
int
|
||||
EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine)
|
||||
{
|
||||
if (ctx->md != md || ctx->engine != engine) {
|
||||
EVP_MD_CTX_cleanup(ctx);
|
||||
ctx->md = md;
|
||||
ctx->engine = engine;
|
||||
|
||||
ctx->ptr = calloc(1, md->ctx_size);
|
||||
if (ctx->ptr == NULL)
|
||||
return 0;
|
||||
}
|
||||
(ctx->md->init)(ctx->ptr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size)
|
||||
{
|
||||
(ctx->md->update)(ctx->ptr, data, size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size)
|
||||
{
|
||||
(ctx->md->final)(hash, ctx->ptr);
|
||||
if (size)
|
||||
*size = ctx->md->hash_size;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize,
|
||||
const EVP_MD *md, ENGINE *engine)
|
||||
{
|
||||
EVP_MD_CTX *ctx;
|
||||
int ret;
|
||||
|
||||
ctx = EVP_MD_CTX_create();
|
||||
if (ctx == NULL)
|
||||
return 0;
|
||||
ret = EVP_DigestInit_ex(ctx, md, engine);
|
||||
if (ret != 1)
|
||||
return ret;
|
||||
ret = EVP_DigestUpdate(ctx, data, dsize);
|
||||
if (ret != 1)
|
||||
return ret;
|
||||
ret = EVP_DigestFinal_ex(ctx, hash, hsize);
|
||||
if (ret != 1)
|
||||
return ret;
|
||||
EVP_MD_CTX_destroy(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
const EVP_MD *
|
||||
EVP_sha256(void)
|
||||
{
|
||||
static const struct hc_evp_md sha256 = {
|
||||
32,
|
||||
64,
|
||||
sizeof(SHA256_CTX),
|
||||
(evp_md_init)SHA256_Init,
|
||||
(evp_md_update)SHA256_Update,
|
||||
(evp_md_final)SHA256_Final,
|
||||
NULL
|
||||
};
|
||||
return &sha256;
|
||||
}
|
||||
|
||||
static const struct hc_evp_md sha1 = {
|
||||
20,
|
||||
64,
|
||||
sizeof(SHA_CTX),
|
||||
(evp_md_init)SHA1_Init,
|
||||
(evp_md_update)SHA1_Update,
|
||||
(evp_md_final)SHA1_Final,
|
||||
NULL
|
||||
};
|
||||
|
||||
const EVP_MD *
|
||||
EVP_sha1(void)
|
||||
{
|
||||
return &sha1;
|
||||
}
|
||||
|
||||
const EVP_MD *
|
||||
EVP_sha(void)
|
||||
{
|
||||
return &sha1;
|
||||
}
|
||||
|
||||
const EVP_MD *
|
||||
EVP_md5(void)
|
||||
{
|
||||
static const struct hc_evp_md md5 = {
|
||||
16,
|
||||
64,
|
||||
sizeof(MD5_CTX),
|
||||
(evp_md_init)MD5_Init,
|
||||
(evp_md_update)MD5_Update,
|
||||
(evp_md_final)MD5_Final,
|
||||
NULL
|
||||
};
|
||||
return &md5;
|
||||
}
|
||||
|
||||
const EVP_MD *
|
||||
EVP_md4(void)
|
||||
{
|
||||
static const struct hc_evp_md md4 = {
|
||||
16,
|
||||
64,
|
||||
sizeof(MD4_CTX),
|
||||
(evp_md_init)MD4_Init,
|
||||
(evp_md_update)MD4_Update,
|
||||
(evp_md_final)MD4_Final,
|
||||
NULL
|
||||
};
|
||||
return &md4;
|
||||
}
|
||||
|
||||
const EVP_MD *
|
||||
EVP_md2(void)
|
||||
{
|
||||
static const struct hc_evp_md md2 = {
|
||||
16,
|
||||
16,
|
||||
sizeof(MD2_CTX),
|
||||
(evp_md_init)MD2_Init,
|
||||
(evp_md_update)MD2_Update,
|
||||
(evp_md_final)MD2_Final,
|
||||
NULL
|
||||
};
|
||||
return &md2;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
static void
|
||||
null_Init (void *m)
|
||||
{
|
||||
}
|
||||
static void
|
||||
null_Update (void *m, const void * data, size_t size)
|
||||
{
|
||||
}
|
||||
static void
|
||||
null_Final(void *res, struct md5 *m)
|
||||
{
|
||||
}
|
||||
|
||||
const EVP_MD *
|
||||
EVP_md_null(void)
|
||||
{
|
||||
static const struct hc_evp_md null = {
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
(evp_md_init)null_Init,
|
||||
(evp_md_update)null_Update,
|
||||
(evp_md_final)null_Final,
|
||||
NULL
|
||||
};
|
||||
return &null;
|
||||
}
|
||||
|
||||
#if 0
|
||||
void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
|
||||
int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
|
||||
int EVP_DigestFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s);
|
||||
int EVP_SignFinal(EVP_MD_CTX *, void *, size_t *, EVP_PKEY *);
|
||||
int EVP_VerifyFinal(EVP_MD_CTX *, const void *, size_t, EVP_PKEY *);
|
||||
#endif
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
size_t
|
||||
EVP_CIPHER_block_size(const EVP_CIPHER *c)
|
||||
{
|
||||
return c->block_size;
|
||||
}
|
||||
|
||||
size_t
|
||||
EVP_CIPHER_key_length(const EVP_CIPHER *c)
|
||||
{
|
||||
return c->key_len;
|
||||
}
|
||||
|
||||
size_t
|
||||
EVP_CIPHER_iv_length(const EVP_CIPHER *c)
|
||||
{
|
||||
return c->iv_len;
|
||||
}
|
||||
|
||||
void
|
||||
EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
|
||||
{
|
||||
memset(c, 0, sizeof(*c));
|
||||
}
|
||||
|
||||
int
|
||||
EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
|
||||
{
|
||||
if (c->cipher && c->cipher->cleanup)
|
||||
c->cipher->cleanup(c);
|
||||
if (c->cipher_data) {
|
||||
free(c->cipher_data);
|
||||
c->cipher_data = NULL;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if 0
|
||||
int
|
||||
EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
return ctx->cipher;
|
||||
}
|
||||
|
||||
size_t
|
||||
EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
return EVP_CIPHER_block_size(ctx->cipher);
|
||||
}
|
||||
|
||||
size_t
|
||||
EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
return EVP_CIPHER_key_length(ctx->cipher);
|
||||
}
|
||||
|
||||
size_t
|
||||
EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
return EVP_CIPHER_iv_length(ctx->cipher);
|
||||
}
|
||||
|
||||
unsigned long
|
||||
EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
return ctx->cipher->flags;
|
||||
}
|
||||
|
||||
int
|
||||
EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
|
||||
}
|
||||
|
||||
void *
|
||||
EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
return ctx->app_data;
|
||||
}
|
||||
|
||||
void
|
||||
EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
|
||||
{
|
||||
ctx->app_data = data;
|
||||
}
|
||||
|
||||
int
|
||||
EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
|
||||
const void *key, const void *iv, int encp)
|
||||
{
|
||||
if (encp == -1)
|
||||
encp = ctx->encrypt;
|
||||
else
|
||||
ctx->encrypt = (encp ? 1 : 0);
|
||||
|
||||
if (c && (c != ctx->cipher)) {
|
||||
EVP_CIPHER_CTX_cleanup(ctx);
|
||||
ctx->cipher = c;
|
||||
ctx->key_len = c->key_len;
|
||||
|
||||
ctx->cipher_data = malloc(c->ctx_size);
|
||||
if (ctx->cipher_data == NULL && c->ctx_size != 0)
|
||||
return 0;
|
||||
|
||||
} else if (ctx->cipher == NULL) {
|
||||
/* reuse of cipher, but not any cipher ever set! */
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (EVP_CIPHER_CTX_flags(ctx)) {
|
||||
case EVP_CIPH_CBC_MODE:
|
||||
|
||||
assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
|
||||
|
||||
if (iv)
|
||||
memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
|
||||
memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
|
||||
ctx->cipher->init(ctx, key, iv, encp);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
|
||||
{
|
||||
return ctx->cipher->do_cipher(ctx, out, in, size);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
static int
|
||||
enc_null_init(EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char * key,
|
||||
const unsigned char * iv,
|
||||
int encp)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
|
||||
unsigned char *out,
|
||||
const unsigned char *in,
|
||||
unsigned int size)
|
||||
{
|
||||
memmove(out, in, size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
enc_null_cleanup(EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_enc_null(void)
|
||||
{
|
||||
static const EVP_CIPHER enc_null = {
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
EVP_CIPH_CBC_MODE,
|
||||
enc_null_init,
|
||||
enc_null_do_cipher,
|
||||
enc_null_cleanup,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
return &enc_null;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
struct rc2_cbc {
|
||||
unsigned int maximum_effective_key;
|
||||
RC2_KEY key;
|
||||
};
|
||||
|
||||
static int
|
||||
rc2_init(EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char * key,
|
||||
const unsigned char * iv,
|
||||
int encp)
|
||||
{
|
||||
struct rc2_cbc *k = ctx->cipher_data;
|
||||
k->maximum_effective_key = EVP_CIPHER_CTX_key_length(ctx) * 8;
|
||||
RC2_set_key(&k->key,
|
||||
EVP_CIPHER_CTX_key_length(ctx),
|
||||
key,
|
||||
k->maximum_effective_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
rc2_do_cipher(EVP_CIPHER_CTX *ctx,
|
||||
unsigned char *out,
|
||||
const unsigned char *in,
|
||||
unsigned int size)
|
||||
{
|
||||
struct rc2_cbc *k = ctx->cipher_data;
|
||||
RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
rc2_cleanup(EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
memset(ctx->cipher_data, 0, sizeof(struct rc2_cbc));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_rc2_cbc(void)
|
||||
{
|
||||
static const EVP_CIPHER rc2_cbc = {
|
||||
0,
|
||||
RC2_BLOCK_SIZE,
|
||||
RC2_KEY_LENGTH,
|
||||
RC2_BLOCK_SIZE,
|
||||
EVP_CIPH_CBC_MODE,
|
||||
rc2_init,
|
||||
rc2_do_cipher,
|
||||
rc2_cleanup,
|
||||
sizeof(struct rc2_cbc),
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
return &rc2_cbc;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_rc2_40_cbc(void)
|
||||
{
|
||||
static const EVP_CIPHER rc2_40_cbc = {
|
||||
0,
|
||||
RC2_BLOCK_SIZE,
|
||||
5,
|
||||
RC2_BLOCK_SIZE,
|
||||
EVP_CIPH_CBC_MODE,
|
||||
rc2_init,
|
||||
rc2_do_cipher,
|
||||
rc2_cleanup,
|
||||
sizeof(struct rc2_cbc),
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
return &rc2_40_cbc;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_rc2_64_cbc(void)
|
||||
{
|
||||
static const EVP_CIPHER rc2_64_cbc = {
|
||||
0,
|
||||
RC2_BLOCK_SIZE,
|
||||
8,
|
||||
RC2_BLOCK_SIZE,
|
||||
EVP_CIPH_CBC_MODE,
|
||||
rc2_init,
|
||||
rc2_do_cipher,
|
||||
rc2_cleanup,
|
||||
sizeof(struct rc2_cbc),
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
return &rc2_64_cbc;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_rc4(void)
|
||||
{
|
||||
printf("evp rc4\n");
|
||||
abort();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_rc4_40(void)
|
||||
{
|
||||
printf("evp rc4_40\n");
|
||||
abort();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
struct des_ede3_cbc {
|
||||
DES_key_schedule ks[3];
|
||||
};
|
||||
|
||||
static int
|
||||
des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char * key,
|
||||
const unsigned char * iv,
|
||||
int encp)
|
||||
{
|
||||
struct des_ede3_cbc *k = ctx->cipher_data;
|
||||
|
||||
DES_key_sched((DES_cblock *)(key), &k->ks[0]);
|
||||
DES_key_sched((DES_cblock *)(key + 8), &k->ks[1]);
|
||||
DES_key_sched((DES_cblock *)(key + 16), &k->ks[2]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
des_ede3_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
|
||||
unsigned char *out,
|
||||
const unsigned char *in,
|
||||
unsigned int size)
|
||||
{
|
||||
struct des_ede3_cbc *k = ctx->cipher_data;
|
||||
DES_ede3_cbc_encrypt(in, out, size,
|
||||
&k->ks[0], &k->ks[1], &k->ks[2],
|
||||
(DES_cblock *)ctx->iv, ctx->encrypt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
des_ede3_cbc_cleanup(EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
memset(ctx->cipher_data, 0, sizeof(struct des_ede3_cbc));
|
||||
return 1;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_des_ede3_cbc(void)
|
||||
{
|
||||
static const EVP_CIPHER des_ede3_cbc = {
|
||||
0,
|
||||
8,
|
||||
24,
|
||||
8,
|
||||
EVP_CIPH_CBC_MODE,
|
||||
des_ede3_cbc_init,
|
||||
des_ede3_cbc_do_cipher,
|
||||
des_ede3_cbc_cleanup,
|
||||
sizeof(struct des_ede3_cbc),
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
return &des_ede3_cbc;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
static int
|
||||
aes_init(EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char * key,
|
||||
const unsigned char * iv,
|
||||
int encp)
|
||||
{
|
||||
AES_KEY *k = ctx->cipher_data;
|
||||
if (ctx->encrypt)
|
||||
AES_set_encrypt_key(key, ctx->cipher->key_len * 8, k);
|
||||
else
|
||||
AES_set_decrypt_key(key, ctx->cipher->key_len * 8, k);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
aes_do_cipher(EVP_CIPHER_CTX *ctx,
|
||||
unsigned char *out,
|
||||
const unsigned char *in,
|
||||
unsigned int size)
|
||||
{
|
||||
AES_KEY *k = ctx->cipher_data;
|
||||
AES_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
aes_cleanup(EVP_CIPHER_CTX *ctx)
|
||||
{
|
||||
memset(ctx->cipher_data, 0, sizeof(AES_KEY));
|
||||
return 1;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_aes_128_cbc(void)
|
||||
{
|
||||
static const EVP_CIPHER aes_128_cbc = {
|
||||
0,
|
||||
16,
|
||||
16,
|
||||
16,
|
||||
EVP_CIPH_CBC_MODE,
|
||||
aes_init,
|
||||
aes_do_cipher,
|
||||
aes_cleanup,
|
||||
sizeof(AES_KEY),
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
return &aes_128_cbc;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_aes_192_cbc(void)
|
||||
{
|
||||
static const EVP_CIPHER aes_192_cbc = {
|
||||
0,
|
||||
16,
|
||||
24,
|
||||
16,
|
||||
EVP_CIPH_CBC_MODE,
|
||||
aes_init,
|
||||
aes_do_cipher,
|
||||
aes_cleanup,
|
||||
sizeof(AES_KEY),
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
return &aes_192_cbc;
|
||||
}
|
||||
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_aes_256_cbc(void)
|
||||
{
|
||||
static const EVP_CIPHER aes_256_cbc = {
|
||||
0,
|
||||
16,
|
||||
32,
|
||||
16,
|
||||
EVP_CIPH_CBC_MODE,
|
||||
aes_init,
|
||||
aes_do_cipher,
|
||||
aes_cleanup,
|
||||
sizeof(AES_KEY),
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
return &aes_256_cbc;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
static const struct cipher_name {
|
||||
const char *name;
|
||||
const EVP_CIPHER *(*func)(void);
|
||||
} cipher_name[] = {
|
||||
{ "des-ede3-cbc", EVP_des_ede3_cbc },
|
||||
{ "aes-128-cbc", EVP_aes_128_cbc },
|
||||
{ "aes-192-cbc", EVP_aes_192_cbc },
|
||||
{ "aes-256-cbc", EVP_aes_256_cbc }
|
||||
};
|
||||
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_get_cipherbyname(const char *name)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
|
||||
if (strcasecmp(cipher_name[i].name, name) == 0)
|
||||
return (*cipher_name[i].func)();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef min
|
||||
#define min(a,b) (((a)>(b))?(b):(a))
|
||||
#endif
|
||||
|
||||
int
|
||||
EVP_BytesToKey(const EVP_CIPHER *type,
|
||||
const EVP_MD *md,
|
||||
const void *salt,
|
||||
const void *data, size_t datalen,
|
||||
unsigned int count,
|
||||
void *keydata,
|
||||
void *ivdata)
|
||||
{
|
||||
int ivlen, keylen, first = 0;
|
||||
unsigned int mds = 0, i;
|
||||
unsigned char *key = keydata;
|
||||
unsigned char *iv = ivdata;
|
||||
unsigned char *buf;
|
||||
EVP_MD_CTX c;
|
||||
|
||||
keylen = EVP_CIPHER_key_length(type);
|
||||
ivlen = EVP_CIPHER_iv_length(type);
|
||||
|
||||
if (data == NULL)
|
||||
return keylen;
|
||||
|
||||
buf = malloc(EVP_MD_size(md));
|
||||
if (buf == NULL)
|
||||
return -1;
|
||||
|
||||
EVP_MD_CTX_init(&c);
|
||||
|
||||
first = 1;
|
||||
while (1) {
|
||||
EVP_DigestInit_ex(&c, md, NULL);
|
||||
if (!first)
|
||||
EVP_DigestUpdate(&c, buf, mds);
|
||||
first = 0;
|
||||
EVP_DigestUpdate(&c,data,datalen);
|
||||
|
||||
#define PKCS5_SALT_LEN 8
|
||||
|
||||
if (salt)
|
||||
EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
|
||||
|
||||
EVP_DigestFinal_ex(&c, buf, &mds);
|
||||
assert(mds == EVP_MD_size(md));
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
EVP_DigestInit_ex(&c, md, NULL);
|
||||
EVP_DigestUpdate(&c, buf, mds);
|
||||
EVP_DigestFinal_ex(&c, buf, &mds);
|
||||
assert(mds == EVP_MD_size(md));
|
||||
}
|
||||
|
||||
i = 0;
|
||||
if (keylen) {
|
||||
size_t sz = min(keylen, mds);
|
||||
if (key) {
|
||||
memcpy(key, buf, sz);
|
||||
key += sz;
|
||||
}
|
||||
keylen -= sz;
|
||||
i += sz;
|
||||
}
|
||||
if (ivlen && mds > i) {
|
||||
size_t sz = min(ivlen, (mds - i));
|
||||
if (iv) {
|
||||
memcpy(iv, &buf[i], sz);
|
||||
iv += sz;
|
||||
}
|
||||
ivlen -= sz;
|
||||
}
|
||||
if (keylen == 0 && ivlen == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
EVP_MD_CTX_cleanup(&c);
|
||||
free(buf);
|
||||
|
||||
return EVP_CIPHER_key_length(type);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
OpenSSL_add_all_algorithms(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
OpenSSL_add_all_algorithms_conf(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
OpenSSL_add_all_algorithms_noconf(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
* Copyright (c) 2005 - 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: evp.h,v 1.11 2006/10/07 17:21:24 lha Exp $ */
|
||||
|
||||
#ifndef HEIM_EVP_H
|
||||
#define HEIM_EVP_H 1
|
||||
|
||||
#include <hcrypto/engine.h>
|
||||
|
||||
/* symbol renaming */
|
||||
#define EVP_CIPHER_CTX_block_size hc_EVP_CIPHER_CTX_block_size
|
||||
#define EVP_CIPHER_CTX_cipher hc_EVP_CIPHER_CTX_cipher
|
||||
#define EVP_CIPHER_CTX_cleanup hc_EVP_CIPHER_CTX_cleanup
|
||||
#define EVP_CIPHER_CTX_flags hc_EVP_CIPHER_CTX_flags
|
||||
#define EVP_CIPHER_CTX_get_app_data hc_EVP_CIPHER_CTX_get_app_data
|
||||
#define EVP_CIPHER_CTX_init hc_EVP_CIPHER_CTX_init
|
||||
#define EVP_CIPHER_CTX_iv_length hc_EVP_CIPHER_CTX_iv_length
|
||||
#define EVP_CIPHER_CTX_key_length hc_EVP_CIPHER_CTX_key_length
|
||||
#define EVP_CIPHER_CTX_mode hc_EVP_CIPHER_CTX_mode
|
||||
#define EVP_CIPHER_CTX_set_app_data hc_EVP_CIPHER_CTX_set_app_data
|
||||
#define EVP_CIPHER_CTX_set_key_length hc_EVP_CIPHER_CTX_set_key_length
|
||||
#define EVP_CIPHER_CTX_set_padding hc_EVP_CIPHER_CTX_set_padding
|
||||
#define EVP_CIPHER_block_size hc_EVP_CIPHER_block_size
|
||||
#define EVP_CIPHER_iv_length hc_EVP_CIPHER_iv_length
|
||||
#define EVP_CIPHER_key_length hc_EVP_CIPHER_key_length
|
||||
#define EVP_Cipher hc_EVP_Cipher
|
||||
#define EVP_CipherInit_ex hc_EVP_CipherInit_ex
|
||||
#define EVP_Digest hc_EVP_Digest
|
||||
#define EVP_DigestFinal_ex hc_EVP_DigestFinal_ex
|
||||
#define EVP_DigestInit_ex hc_EVP_DigestInit_ex
|
||||
#define EVP_DigestUpdate hc_EVP_DigestUpdate
|
||||
#define EVP_MD_CTX_block_size hc_EVP_MD_CTX_block_size
|
||||
#define EVP_MD_CTX_cleanup hc_EVP_MD_CTX_cleanup
|
||||
#define EVP_MD_CTX_create hc_EVP_MD_CTX_create
|
||||
#define EVP_MD_CTX_init hc_EVP_MD_CTX_init
|
||||
#define EVP_MD_CTX_destroy hc_EVP_MD_CTX_destroy
|
||||
#define EVP_MD_CTX_md hc_EVP_MD_CTX_md
|
||||
#define EVP_MD_CTX_size hc_EVP_MD_CTX_size
|
||||
#define EVP_MD_block_size hc_EVP_MD_block_size
|
||||
#define EVP_MD_size hc_EVP_MD_size
|
||||
#define EVP_aes_128_cbc hc_EVP_aes_128_cbc
|
||||
#define EVP_aes_192_cbc hc_EVP_aes_192_cbc
|
||||
#define EVP_aes_256_cbc hc_EVP_aes_256_cbc
|
||||
#define EVP_des_ede3_cbc hc_EVP_des_ede3_cbc
|
||||
#define EVP_enc_null hc_EVP_enc_null
|
||||
#define EVP_md2 hc_EVP_md2
|
||||
#define EVP_md4 hc_EVP_md4
|
||||
#define EVP_md5 hc_EVP_md5
|
||||
#define EVP_md_null hc_EVP_md_null
|
||||
#define EVP_rc2_40_cbc hc_EVP_rc2_40_cbc
|
||||
#define EVP_rc2_64_cbc hc_EVP_rc2_64_cbc
|
||||
#define EVP_rc2_cbc hc_EVP_rc2_cbc
|
||||
#define EVP_rc4 hc_EVP_rc4
|
||||
#define EVP_rc4_40 hc_EVP_rc4_40
|
||||
#define EVP_sha hc_EVP_sha
|
||||
#define EVP_sha1 hc_EVP_sha1
|
||||
#define EVP_sha256 hc_EVP_sha256
|
||||
#define PKCS5_PBKDF2_HMAC_SHA1 hc_PKCS5_PBKDF2_HMAC_SHA1
|
||||
#define EVP_BytesToKey hc_EVP_BytesToKey
|
||||
#define EVP_get_cipherbyname hc_EVP_get_cipherbyname
|
||||
#define OpenSSL_add_all_algorithms hc_OpenSSL_add_all_algorithms
|
||||
#define OpenSSL_add_all_algorithms_conf hc_OpenSSL_add_all_algorithms_conf
|
||||
#define OpenSSL_add_all_algorithms_noconf hc_OpenSSL_add_all_algorithms_noconf
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
typedef struct hc_EVP_MD_CTX EVP_MD_CTX;
|
||||
typedef struct hc_evp_pkey EVP_PKEY;
|
||||
typedef struct hc_evp_md EVP_MD;
|
||||
typedef struct hc_CIPHER EVP_CIPHER;
|
||||
typedef struct hc_CIPHER_CTX EVP_CIPHER_CTX;
|
||||
|
||||
#define EVP_MAX_IV_LENGTH 16
|
||||
#define EVP_MAX_BLOCK_LENGTH 32
|
||||
|
||||
#define EVP_MAX_MD_SIZE 64
|
||||
|
||||
struct hc_CIPHER {
|
||||
int nid;
|
||||
int block_size;
|
||||
int key_len;
|
||||
int iv_len;
|
||||
unsigned long flags;
|
||||
/* The lowest 3 bits is used as integer field for the mode the
|
||||
* cipher is used in (use EVP_CIPHER.._mode() to extract the
|
||||
* mode). The rest of the flag field is a bitfield.
|
||||
*/
|
||||
#define EVP_CIPH_CBC_MODE 2
|
||||
#define EVP_CIPH_MODE 0x7
|
||||
|
||||
#define EVP_CIPH_ALWAYS_CALL_INIT 0x20
|
||||
|
||||
int (*init)(EVP_CIPHER_CTX*,const unsigned char*,const unsigned char*,int);
|
||||
int (*do_cipher)(EVP_CIPHER_CTX *, unsigned char *,
|
||||
const unsigned char *, unsigned int);
|
||||
int (*cleanup)(EVP_CIPHER_CTX *);
|
||||
int ctx_size;
|
||||
void *set_asn1_parameters;
|
||||
void *get_asn1_parameters;
|
||||
void *ctrl;
|
||||
void *app_data;
|
||||
};
|
||||
|
||||
struct hc_CIPHER_CTX {
|
||||
const EVP_CIPHER *cipher;
|
||||
ENGINE *engine;
|
||||
int encrypt;
|
||||
int buf_len;
|
||||
unsigned char oiv[EVP_MAX_IV_LENGTH];
|
||||
unsigned char iv[EVP_MAX_IV_LENGTH];
|
||||
unsigned char buf[EVP_MAX_BLOCK_LENGTH];
|
||||
int num;
|
||||
void *app_data;
|
||||
int key_len;
|
||||
unsigned long flags;
|
||||
void *cipher_data;
|
||||
int final_used;
|
||||
int block_mask;
|
||||
unsigned char final[EVP_MAX_BLOCK_LENGTH];
|
||||
};
|
||||
|
||||
struct hc_EVP_MD_CTX {
|
||||
const EVP_MD *md;
|
||||
ENGINE *engine;
|
||||
void *ptr;
|
||||
};
|
||||
|
||||
/*
|
||||
* Avaible crypto algs
|
||||
*/
|
||||
|
||||
const EVP_MD *EVP_md_null(void);
|
||||
const EVP_MD *EVP_md2(void);
|
||||
const EVP_MD *EVP_md4(void);
|
||||
const EVP_MD *EVP_md5(void);
|
||||
const EVP_MD *EVP_sha(void);
|
||||
const EVP_MD *EVP_sha1(void);
|
||||
const EVP_MD *EVP_sha256(void);
|
||||
|
||||
const EVP_CIPHER * EVP_aes_128_cbc(void);
|
||||
const EVP_CIPHER * EVP_aes_192_cbc(void);
|
||||
const EVP_CIPHER * EVP_aes_256_cbc(void);
|
||||
const EVP_CIPHER * EVP_des_ede3_cbc(void);
|
||||
const EVP_CIPHER * EVP_enc_null(void);
|
||||
const EVP_CIPHER * EVP_rc2_40_cbc(void);
|
||||
const EVP_CIPHER * EVP_rc2_64_cbc(void);
|
||||
const EVP_CIPHER * EVP_rc2_cbc(void);
|
||||
const EVP_CIPHER * EVP_rc4(void);
|
||||
const EVP_CIPHER * EVP_rc4_40(void);
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
size_t EVP_MD_size(const EVP_MD *);
|
||||
size_t EVP_MD_block_size(const EVP_MD *);
|
||||
|
||||
const EVP_MD *
|
||||
EVP_MD_CTX_md(EVP_MD_CTX *);
|
||||
size_t EVP_MD_CTX_size(EVP_MD_CTX *);
|
||||
size_t EVP_MD_CTX_block_size(EVP_MD_CTX *);
|
||||
|
||||
EVP_MD_CTX *
|
||||
EVP_MD_CTX_create(void);
|
||||
void EVP_MD_CTX_init(EVP_MD_CTX *);
|
||||
void EVP_MD_CTX_destroy(EVP_MD_CTX *);
|
||||
int EVP_MD_CTX_cleanup(EVP_MD_CTX *);
|
||||
|
||||
int EVP_DigestInit_ex(EVP_MD_CTX *, const EVP_MD *, ENGINE *);
|
||||
int EVP_DigestUpdate(EVP_MD_CTX *,const void *, size_t);
|
||||
int EVP_DigestFinal_ex(EVP_MD_CTX *, void *, unsigned int *);
|
||||
int EVP_Digest(const void *, size_t, void *, unsigned int *,
|
||||
const EVP_MD *, ENGINE *);
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_get_cipherbyname(const char *);
|
||||
|
||||
size_t EVP_CIPHER_block_size(const EVP_CIPHER *);
|
||||
size_t EVP_CIPHER_key_length(const EVP_CIPHER *);
|
||||
size_t EVP_CIPHER_iv_length(const EVP_CIPHER *);
|
||||
|
||||
void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *);
|
||||
int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
|
||||
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *, int);
|
||||
int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);
|
||||
unsigned long
|
||||
EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *);
|
||||
int EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *);
|
||||
|
||||
const EVP_CIPHER *
|
||||
EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *);
|
||||
size_t EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *);
|
||||
size_t EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *);
|
||||
size_t EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *);
|
||||
void * EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *);
|
||||
void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *, void *);
|
||||
|
||||
int EVP_CipherInit_ex(EVP_CIPHER_CTX *,const EVP_CIPHER *, ENGINE *,
|
||||
const void *, const void *, int);
|
||||
|
||||
int EVP_Cipher(EVP_CIPHER_CTX *,void *,const void *,size_t);
|
||||
|
||||
int PKCS5_PBKDF2_HMAC_SHA1(const void *, size_t, const void *, size_t,
|
||||
unsigned long, size_t, void *);
|
||||
|
||||
int EVP_BytesToKey(const EVP_CIPHER *, const EVP_MD *,
|
||||
const void *, const void *, size_t,
|
||||
unsigned int, void *, void *);
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
void OpenSSL_add_all_algorithms(void);
|
||||
void OpenSSL_add_all_algorithms_conf(void);
|
||||
void OpenSSL_add_all_algorithms_noconf(void);
|
||||
|
||||
#endif /* HEIM_EVP_H */
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 1999 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of KTH nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
||||
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
/* $Id: hash.h,v 1.4 2006/05/05 11:06:49 lha Exp $ */
|
||||
|
||||
/* stuff in common between md4, md5, and sha1 */
|
||||
|
||||
#ifndef __hash_h__
|
||||
#define __hash_h__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#ifdef KRB5
|
||||
#include <krb5-types.h>
|
||||
#endif
|
||||
|
||||
#ifndef min
|
||||
#define min(a,b) (((a)>(b))?(b):(a))
|
||||
#endif
|
||||
|
||||
/* Vector Crays doesn't have a good 32-bit type, or more precisely,
|
||||
int32_t as defined by <bind/bitypes.h> isn't 32 bits, and we don't
|
||||
want to depend in being able to redefine this type. To cope with
|
||||
this we have to clamp the result in some places to [0,2^32); no
|
||||
need to do this on other machines. Did I say this was a mess?
|
||||
*/
|
||||
|
||||
#ifdef _CRAY
|
||||
#define CRAYFIX(X) ((X) & 0xffffffff)
|
||||
#else
|
||||
#define CRAYFIX(X) (X)
|
||||
#endif
|
||||
|
||||
static inline uint32_t
|
||||
cshift (uint32_t x, unsigned int n)
|
||||
{
|
||||
x = CRAYFIX(x);
|
||||
return CRAYFIX((x << n) | (x >> (32 - n)));
|
||||
}
|
||||
|
||||
#endif /* __hash_h__ */
|
||||
@@ -0,0 +1,122 @@
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <hmac.h>
|
||||
|
||||
void
|
||||
HMAC_CTX_init(HMAC_CTX *ctx)
|
||||
{
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
|
||||
void
|
||||
HMAC_CTX_cleanup(HMAC_CTX *ctx)
|
||||
{
|
||||
if (ctx->buf) {
|
||||
memset(ctx->buf, 0, ctx->key_length);
|
||||
free(ctx->buf);
|
||||
ctx->buf = NULL;
|
||||
}
|
||||
if (ctx->opad) {
|
||||
memset(ctx->ipad, 0, ctx->key_length);
|
||||
free(ctx->opad);
|
||||
ctx->opad = NULL;
|
||||
}
|
||||
if (ctx->ipad) {
|
||||
memset(ctx->ipad, 0, ctx->key_length);
|
||||
free(ctx->ipad);
|
||||
ctx->ipad = NULL;
|
||||
}
|
||||
if (ctx->ctx) {
|
||||
EVP_MD_CTX_destroy(ctx->ctx);
|
||||
ctx->ctx = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
size_t
|
||||
HMAC_size(const HMAC_CTX *ctx)
|
||||
{
|
||||
return EVP_MD_size(ctx->md);
|
||||
}
|
||||
|
||||
void
|
||||
HMAC_Init_ex(HMAC_CTX *ctx,
|
||||
const void *key,
|
||||
size_t keylen,
|
||||
const EVP_MD *md,
|
||||
ENGINE *engine)
|
||||
{
|
||||
unsigned char *p;
|
||||
size_t i;
|
||||
|
||||
if (ctx->md != md) {
|
||||
ctx->md = md;
|
||||
if (ctx->buf)
|
||||
free (ctx->buf);
|
||||
ctx->key_length = EVP_MD_size(ctx->md);
|
||||
ctx->buf = malloc(ctx->key_length);
|
||||
}
|
||||
#if 0
|
||||
ctx->engine = engine;
|
||||
#endif
|
||||
|
||||
if (keylen > EVP_MD_block_size(ctx->md)) {
|
||||
EVP_Digest(key, keylen, ctx->buf, NULL, ctx->md, engine);
|
||||
key = ctx->buf;
|
||||
keylen = EVP_MD_size(ctx->md);
|
||||
}
|
||||
|
||||
if (ctx->opad)
|
||||
free(ctx->opad);
|
||||
if (ctx->ipad)
|
||||
free(ctx->ipad);
|
||||
|
||||
ctx->opad = malloc(EVP_MD_block_size(ctx->md));
|
||||
ctx->ipad = malloc(EVP_MD_block_size(ctx->md));
|
||||
memset(ctx->ipad, 0x36, EVP_MD_block_size(ctx->md));
|
||||
memset(ctx->opad, 0x5c, EVP_MD_block_size(ctx->md));
|
||||
|
||||
for (i = 0, p = ctx->ipad; i < keylen; i++)
|
||||
p[i] ^= ((const unsigned char *)key)[i];
|
||||
for (i = 0, p = ctx->opad; i < keylen; i++)
|
||||
p[i] ^= ((const unsigned char *)key)[i];
|
||||
|
||||
ctx->ctx = EVP_MD_CTX_create();
|
||||
|
||||
EVP_DigestInit_ex(ctx->ctx, ctx->md, ctx->engine);
|
||||
EVP_DigestUpdate(ctx->ctx, ctx->ipad, EVP_MD_block_size(ctx->md));
|
||||
}
|
||||
|
||||
void
|
||||
HMAC_Update(HMAC_CTX *ctx, const void *data, size_t len)
|
||||
{
|
||||
EVP_DigestUpdate(ctx->ctx, data, len);
|
||||
}
|
||||
|
||||
void
|
||||
HMAC_Final(HMAC_CTX *ctx, void *md, unsigned int *len)
|
||||
{
|
||||
EVP_DigestFinal_ex(ctx->ctx, ctx->buf, NULL);
|
||||
|
||||
EVP_DigestInit_ex(ctx->ctx, ctx->md, ctx->engine);
|
||||
EVP_DigestUpdate(ctx->ctx, ctx->opad, EVP_MD_block_size(ctx->md));
|
||||
EVP_DigestUpdate(ctx->ctx, ctx->buf, ctx->key_length);
|
||||
EVP_DigestFinal_ex(ctx->ctx, md, len);
|
||||
}
|
||||
|
||||
void *
|
||||
HMAC(const EVP_MD *md,
|
||||
const void *key, size_t key_size,
|
||||
const void *data, size_t data_size,
|
||||
void *hash, unsigned int *hash_len)
|
||||
{
|
||||
HMAC_CTX ctx;
|
||||
|
||||
HMAC_CTX_init(&ctx);
|
||||
HMAC_Init_ex(&ctx, key, key_size, md, NULL);
|
||||
HMAC_Update(&ctx, data, data_size);
|
||||
HMAC_Final(&ctx, hash, hash_len);
|
||||
HMAC_CTX_cleanup(&ctx);
|
||||
return hash;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (c) 2005 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: hmac.h,v 1.3 2006/01/13 15:26:52 lha Exp $ */
|
||||
|
||||
#ifndef HEIM_HMAC_H
|
||||
#define HEIM_HMAC_H 1
|
||||
|
||||
#include <hcrypto/evp.h>
|
||||
|
||||
/* symbol renaming */
|
||||
#define HMAC_CTX_init hc_HMAC_CTX_init
|
||||
#define HMAC_CTX_cleanup hc_HMAC_CTX_cleanup
|
||||
#define HMAC_size hc_HMAC_size
|
||||
#define HMAC_Init_ex hc_HMAC_Init_ex
|
||||
#define HMAC_Update hc_HMAC_Update
|
||||
#define HMAC_Final hc_HMAC_Final
|
||||
#define HMAC hc_HMAC
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#define HMAC_MAX_MD_CBLOCK 64
|
||||
|
||||
typedef struct hc_HMAC_CTX HMAC_CTX;
|
||||
|
||||
struct hc_HMAC_CTX {
|
||||
const EVP_MD *md;
|
||||
ENGINE *engine;
|
||||
EVP_MD_CTX *ctx;
|
||||
size_t key_length;
|
||||
void *opad;
|
||||
void *ipad;
|
||||
void *buf;
|
||||
};
|
||||
|
||||
|
||||
void HMAC_CTX_init(HMAC_CTX *);
|
||||
void HMAC_CTX_cleanup(HMAC_CTX *ctx);
|
||||
|
||||
size_t HMAC_size(const HMAC_CTX *ctx);
|
||||
|
||||
void HMAC_Init_ex(HMAC_CTX *, const void *, size_t,
|
||||
const EVP_MD *, ENGINE *);
|
||||
void HMAC_Update(HMAC_CTX *ctx, const void *data, size_t len);
|
||||
void HMAC_Final(HMAC_CTX *ctx, void *md, unsigned int *len);
|
||||
|
||||
void * HMAC(const EVP_MD *evp_md, const void *key, size_t key_len,
|
||||
const void *data, size_t n, void *md, unsigned int *md_len);
|
||||
|
||||
#endif /* HEIM_HMAC_H */
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
|
||||
RCSID("$Id: md2.c,v 1.1 2006/01/08 21:47:28 lha Exp $");
|
||||
#endif
|
||||
|
||||
#include "hash.h"
|
||||
#include "md2.h"
|
||||
|
||||
static const unsigned char subst[256] = {
|
||||
41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
|
||||
19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
|
||||
76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
|
||||
138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
|
||||
245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
|
||||
148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
|
||||
39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
|
||||
181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
|
||||
150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
|
||||
112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
|
||||
96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
|
||||
85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
|
||||
234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
|
||||
129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
|
||||
8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
|
||||
203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
|
||||
166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
|
||||
31, 26, 219, 153, 141, 51, 159, 17, 131, 20
|
||||
};
|
||||
|
||||
void
|
||||
MD2_Init (struct md2 *m)
|
||||
{
|
||||
memset(m, 0, sizeof(*m));
|
||||
}
|
||||
|
||||
static void
|
||||
calc(struct md2 *m, const void *v)
|
||||
{
|
||||
unsigned char x[48], L;
|
||||
const unsigned char *p = v;
|
||||
int i, j, t;
|
||||
|
||||
L = m->checksum[15];
|
||||
for (i = 0; i < 16; i++)
|
||||
L = m->checksum[i] ^= subst[p[i] ^ L];
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
x[i] = m->state[i];
|
||||
x[i + 16] = p[i];
|
||||
x[i + 32] = x[i] ^ p[i];
|
||||
}
|
||||
|
||||
t = 0;
|
||||
for (i = 0; i < 18; i++) {
|
||||
for (j = 0; j < 48; j++)
|
||||
t = x[j] ^= subst[t];
|
||||
t = (t + i) & 0xff;
|
||||
}
|
||||
|
||||
memcpy(m->state, x, 16);
|
||||
memset(x, 0, sizeof(x));
|
||||
}
|
||||
|
||||
void
|
||||
MD2_Update (struct md2 *m, const void *v, size_t len)
|
||||
{
|
||||
size_t idx = m->len & 0xf;
|
||||
const unsigned char *p = v;
|
||||
|
||||
m->len += len;
|
||||
if (len + idx >= 16) {
|
||||
if (idx) {
|
||||
memcpy(m->data + idx, p, 16 - idx);
|
||||
calc(m, m->data);
|
||||
p += 16;
|
||||
len -= 16 - idx;
|
||||
}
|
||||
while (len >= 16) {
|
||||
calc(m, p);
|
||||
p += 16;
|
||||
len -= 16;
|
||||
}
|
||||
idx = 0;
|
||||
}
|
||||
|
||||
memcpy(m->data + idx, p, len);
|
||||
}
|
||||
|
||||
void
|
||||
MD2_Final (void *res, struct md2 *m)
|
||||
{
|
||||
unsigned char pad[16];
|
||||
size_t padlen;
|
||||
|
||||
padlen = 16 - (m->len % 16);
|
||||
memset(pad, padlen, padlen);
|
||||
|
||||
MD2_Update(m, pad, padlen);
|
||||
memcpy(pad, m->checksum, 16);
|
||||
MD2_Update(m, pad, 16);
|
||||
|
||||
memcpy(res, m->state, MD2_DIGEST_LENGTH);
|
||||
memset(m, 0, sizeof(m));
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: md2.h,v 1.1 2006/01/08 21:47:28 lha Exp $ */
|
||||
|
||||
#ifndef HEIM_MD2_H
|
||||
#define HEIM_MD2_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define MD2_Init hc_MD2_Init
|
||||
#define MD2_Update hc_MD2_Update
|
||||
#define MD2_Final hc_MD2_Final
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#define MD2_DIGEST_LENGTH 16
|
||||
|
||||
struct md2 {
|
||||
size_t len;
|
||||
unsigned char data[16]; /* stored unalligned data between Update's */
|
||||
unsigned char checksum[16];
|
||||
unsigned char state[16]; /* lower 16 bytes of X */
|
||||
};
|
||||
|
||||
typedef struct md2 MD2_CTX;
|
||||
|
||||
void MD2_Init (struct md2 *m);
|
||||
void MD2_Update (struct md2 *m, const void *p, size_t len);
|
||||
void MD2_Final (void *res, struct md2 *m);
|
||||
|
||||
#endif /* HEIM_MD2_H */
|
||||
@@ -0,0 +1,250 @@
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
|
||||
RCSID("$Id: md4.c,v 1.18 2006/05/05 10:22:04 lha Exp $");
|
||||
#endif
|
||||
|
||||
#include "hash.h"
|
||||
#include "md4.h"
|
||||
|
||||
#define A m->counter[0]
|
||||
#define B m->counter[1]
|
||||
#define C m->counter[2]
|
||||
#define D m->counter[3]
|
||||
#define X data
|
||||
|
||||
void
|
||||
MD4_Init (struct md4 *m)
|
||||
{
|
||||
m->sz[0] = 0;
|
||||
m->sz[1] = 0;
|
||||
D = 0x10325476;
|
||||
C = 0x98badcfe;
|
||||
B = 0xefcdab89;
|
||||
A = 0x67452301;
|
||||
}
|
||||
|
||||
#define F(x,y,z) CRAYFIX((x & y) | (~x & z))
|
||||
#define G(x,y,z) ((x & y) | (x & z) | (y & z))
|
||||
#define H(x,y,z) (x ^ y ^ z)
|
||||
|
||||
#define DOIT(a,b,c,d,k,s,i,OP) \
|
||||
a = cshift(a + OP(b,c,d) + X[k] + i, s)
|
||||
|
||||
#define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
|
||||
#define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
|
||||
#define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
|
||||
|
||||
static inline void
|
||||
calc (struct md4 *m, uint32_t *data)
|
||||
{
|
||||
uint32_t AA, BB, CC, DD;
|
||||
|
||||
AA = A;
|
||||
BB = B;
|
||||
CC = C;
|
||||
DD = D;
|
||||
|
||||
/* Round 1 */
|
||||
|
||||
DO1(A,B,C,D,0,3,0);
|
||||
DO1(D,A,B,C,1,7,0);
|
||||
DO1(C,D,A,B,2,11,0);
|
||||
DO1(B,C,D,A,3,19,0);
|
||||
|
||||
DO1(A,B,C,D,4,3,0);
|
||||
DO1(D,A,B,C,5,7,0);
|
||||
DO1(C,D,A,B,6,11,0);
|
||||
DO1(B,C,D,A,7,19,0);
|
||||
|
||||
DO1(A,B,C,D,8,3,0);
|
||||
DO1(D,A,B,C,9,7,0);
|
||||
DO1(C,D,A,B,10,11,0);
|
||||
DO1(B,C,D,A,11,19,0);
|
||||
|
||||
DO1(A,B,C,D,12,3,0);
|
||||
DO1(D,A,B,C,13,7,0);
|
||||
DO1(C,D,A,B,14,11,0);
|
||||
DO1(B,C,D,A,15,19,0);
|
||||
|
||||
/* Round 2 */
|
||||
|
||||
DO2(A,B,C,D,0,3,0x5A827999);
|
||||
DO2(D,A,B,C,4,5,0x5A827999);
|
||||
DO2(C,D,A,B,8,9,0x5A827999);
|
||||
DO2(B,C,D,A,12,13,0x5A827999);
|
||||
|
||||
DO2(A,B,C,D,1,3,0x5A827999);
|
||||
DO2(D,A,B,C,5,5,0x5A827999);
|
||||
DO2(C,D,A,B,9,9,0x5A827999);
|
||||
DO2(B,C,D,A,13,13,0x5A827999);
|
||||
|
||||
DO2(A,B,C,D,2,3,0x5A827999);
|
||||
DO2(D,A,B,C,6,5,0x5A827999);
|
||||
DO2(C,D,A,B,10,9,0x5A827999);
|
||||
DO2(B,C,D,A,14,13,0x5A827999);
|
||||
|
||||
DO2(A,B,C,D,3,3,0x5A827999);
|
||||
DO2(D,A,B,C,7,5,0x5A827999);
|
||||
DO2(C,D,A,B,11,9,0x5A827999);
|
||||
DO2(B,C,D,A,15,13,0x5A827999);
|
||||
|
||||
/* Round 3 */
|
||||
|
||||
DO3(A,B,C,D,0,3,0x6ED9EBA1);
|
||||
DO3(D,A,B,C,8,9,0x6ED9EBA1);
|
||||
DO3(C,D,A,B,4,11,0x6ED9EBA1);
|
||||
DO3(B,C,D,A,12,15,0x6ED9EBA1);
|
||||
|
||||
DO3(A,B,C,D,2,3,0x6ED9EBA1);
|
||||
DO3(D,A,B,C,10,9,0x6ED9EBA1);
|
||||
DO3(C,D,A,B,6,11,0x6ED9EBA1);
|
||||
DO3(B,C,D,A,14,15,0x6ED9EBA1);
|
||||
|
||||
DO3(A,B,C,D,1,3,0x6ED9EBA1);
|
||||
DO3(D,A,B,C,9,9,0x6ED9EBA1);
|
||||
DO3(C,D,A,B,5,11,0x6ED9EBA1);
|
||||
DO3(B,C,D,A,13,15,0x6ED9EBA1);
|
||||
|
||||
DO3(A,B,C,D,3,3,0x6ED9EBA1);
|
||||
DO3(D,A,B,C,11,9,0x6ED9EBA1);
|
||||
DO3(C,D,A,B,7,11,0x6ED9EBA1);
|
||||
DO3(B,C,D,A,15,15,0x6ED9EBA1);
|
||||
|
||||
A += AA;
|
||||
B += BB;
|
||||
C += CC;
|
||||
D += DD;
|
||||
}
|
||||
|
||||
/*
|
||||
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
|
||||
*/
|
||||
|
||||
#if defined(WORDS_BIGENDIAN)
|
||||
static inline uint32_t
|
||||
swap_uint32_t (uint32_t t)
|
||||
{
|
||||
uint32_t temp1, temp2;
|
||||
|
||||
temp1 = cshift(t, 16);
|
||||
temp2 = temp1 >> 8;
|
||||
temp1 &= 0x00ff00ff;
|
||||
temp2 &= 0x00ff00ff;
|
||||
temp1 <<= 8;
|
||||
return temp1 | temp2;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct x32{
|
||||
unsigned int a:32;
|
||||
unsigned int b:32;
|
||||
};
|
||||
|
||||
void
|
||||
MD4_Update (struct md4 *m, const void *v, size_t len)
|
||||
{
|
||||
const unsigned char *p = v;
|
||||
size_t old_sz = m->sz[0];
|
||||
size_t offset;
|
||||
|
||||
m->sz[0] += len * 8;
|
||||
if (m->sz[0] < old_sz)
|
||||
++m->sz[1];
|
||||
offset = (old_sz / 8) % 64;
|
||||
while(len > 0) {
|
||||
size_t l = min(len, 64 - offset);
|
||||
memcpy(m->save + offset, p, l);
|
||||
offset += l;
|
||||
p += l;
|
||||
len -= l;
|
||||
if(offset == 64) {
|
||||
#if defined(WORDS_BIGENDIAN)
|
||||
int i;
|
||||
uint32_t current[16];
|
||||
struct x32 *u = (struct x32*)m->save;
|
||||
for(i = 0; i < 8; i++){
|
||||
current[2*i+0] = swap_uint32_t(u[i].a);
|
||||
current[2*i+1] = swap_uint32_t(u[i].b);
|
||||
}
|
||||
calc(m, current);
|
||||
#else
|
||||
calc(m, (uint32_t*)m->save);
|
||||
#endif
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
MD4_Final (void *res, struct md4 *m)
|
||||
{
|
||||
unsigned char zeros[72];
|
||||
unsigned offset = (m->sz[0] / 8) % 64;
|
||||
unsigned int dstart = (120 - offset - 1) % 64 + 1;
|
||||
|
||||
*zeros = 0x80;
|
||||
memset (zeros + 1, 0, sizeof(zeros) - 1);
|
||||
zeros[dstart+0] = (m->sz[0] >> 0) & 0xff;
|
||||
zeros[dstart+1] = (m->sz[0] >> 8) & 0xff;
|
||||
zeros[dstart+2] = (m->sz[0] >> 16) & 0xff;
|
||||
zeros[dstart+3] = (m->sz[0] >> 24) & 0xff;
|
||||
zeros[dstart+4] = (m->sz[1] >> 0) & 0xff;
|
||||
zeros[dstart+5] = (m->sz[1] >> 8) & 0xff;
|
||||
zeros[dstart+6] = (m->sz[1] >> 16) & 0xff;
|
||||
zeros[dstart+7] = (m->sz[1] >> 24) & 0xff;
|
||||
MD4_Update (m, zeros, dstart + 8);
|
||||
{
|
||||
int i;
|
||||
unsigned char *r = (unsigned char *)res;
|
||||
|
||||
for (i = 0; i < 4; ++i) {
|
||||
r[4*i] = m->counter[i] & 0xFF;
|
||||
r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
|
||||
r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
|
||||
r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
|
||||
}
|
||||
}
|
||||
#if 0
|
||||
{
|
||||
int i;
|
||||
uint32_t *r = (uint32_t *)res;
|
||||
|
||||
for (i = 0; i < 4; ++i)
|
||||
r[i] = swap_uint32_t (m->counter[i]);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: md4.h,v 1.11 2006/05/05 11:07:01 lha Exp $ */
|
||||
|
||||
#ifndef HEIM_MD4_H
|
||||
#define HEIM_MD4_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define MD4_Init hc_MD4_Init
|
||||
#define MD4_Update hc_MD4_Update
|
||||
#define MD4_Final hc_MD4_Final
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#define MD4_DIGEST_LENGTH 16
|
||||
|
||||
struct md4 {
|
||||
unsigned int sz[2];
|
||||
uint32_t counter[4];
|
||||
unsigned char save[64];
|
||||
};
|
||||
|
||||
typedef struct md4 MD4_CTX;
|
||||
|
||||
void MD4_Init (struct md4 *m);
|
||||
void MD4_Update (struct md4 *m, const void *p, size_t len);
|
||||
void MD4_Final (void *res, struct md4 *m);
|
||||
|
||||
#endif /* HEIM_MD4_H */
|
||||
@@ -0,0 +1,274 @@
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
|
||||
RCSID("$Id: md5.c,v 1.18 2006/05/05 10:22:35 lha Exp $");
|
||||
#endif
|
||||
|
||||
#include "hash.h"
|
||||
#include "md5.h"
|
||||
|
||||
#define A m->counter[0]
|
||||
#define B m->counter[1]
|
||||
#define C m->counter[2]
|
||||
#define D m->counter[3]
|
||||
#define X data
|
||||
|
||||
void
|
||||
MD5_Init (struct md5 *m)
|
||||
{
|
||||
m->sz[0] = 0;
|
||||
m->sz[1] = 0;
|
||||
D = 0x10325476;
|
||||
C = 0x98badcfe;
|
||||
B = 0xefcdab89;
|
||||
A = 0x67452301;
|
||||
}
|
||||
|
||||
#define F(x,y,z) CRAYFIX((x & y) | (~x & z))
|
||||
#define G(x,y,z) CRAYFIX((x & z) | (y & ~z))
|
||||
#define H(x,y,z) (x ^ y ^ z)
|
||||
#define I(x,y,z) CRAYFIX(y ^ (x | ~z))
|
||||
|
||||
#define DOIT(a,b,c,d,k,s,i,OP) \
|
||||
a = b + cshift(a + OP(b,c,d) + X[k] + (i), s)
|
||||
|
||||
#define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
|
||||
#define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
|
||||
#define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
|
||||
#define DO4(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,I)
|
||||
|
||||
static inline void
|
||||
calc (struct md5 *m, uint32_t *data)
|
||||
{
|
||||
uint32_t AA, BB, CC, DD;
|
||||
|
||||
AA = A;
|
||||
BB = B;
|
||||
CC = C;
|
||||
DD = D;
|
||||
|
||||
/* Round 1 */
|
||||
|
||||
DO1(A,B,C,D,0,7,0xd76aa478);
|
||||
DO1(D,A,B,C,1,12,0xe8c7b756);
|
||||
DO1(C,D,A,B,2,17,0x242070db);
|
||||
DO1(B,C,D,A,3,22,0xc1bdceee);
|
||||
|
||||
DO1(A,B,C,D,4,7,0xf57c0faf);
|
||||
DO1(D,A,B,C,5,12,0x4787c62a);
|
||||
DO1(C,D,A,B,6,17,0xa8304613);
|
||||
DO1(B,C,D,A,7,22,0xfd469501);
|
||||
|
||||
DO1(A,B,C,D,8,7,0x698098d8);
|
||||
DO1(D,A,B,C,9,12,0x8b44f7af);
|
||||
DO1(C,D,A,B,10,17,0xffff5bb1);
|
||||
DO1(B,C,D,A,11,22,0x895cd7be);
|
||||
|
||||
DO1(A,B,C,D,12,7,0x6b901122);
|
||||
DO1(D,A,B,C,13,12,0xfd987193);
|
||||
DO1(C,D,A,B,14,17,0xa679438e);
|
||||
DO1(B,C,D,A,15,22,0x49b40821);
|
||||
|
||||
/* Round 2 */
|
||||
|
||||
DO2(A,B,C,D,1,5,0xf61e2562);
|
||||
DO2(D,A,B,C,6,9,0xc040b340);
|
||||
DO2(C,D,A,B,11,14,0x265e5a51);
|
||||
DO2(B,C,D,A,0,20,0xe9b6c7aa);
|
||||
|
||||
DO2(A,B,C,D,5,5,0xd62f105d);
|
||||
DO2(D,A,B,C,10,9,0x2441453);
|
||||
DO2(C,D,A,B,15,14,0xd8a1e681);
|
||||
DO2(B,C,D,A,4,20,0xe7d3fbc8);
|
||||
|
||||
DO2(A,B,C,D,9,5,0x21e1cde6);
|
||||
DO2(D,A,B,C,14,9,0xc33707d6);
|
||||
DO2(C,D,A,B,3,14,0xf4d50d87);
|
||||
DO2(B,C,D,A,8,20,0x455a14ed);
|
||||
|
||||
DO2(A,B,C,D,13,5,0xa9e3e905);
|
||||
DO2(D,A,B,C,2,9,0xfcefa3f8);
|
||||
DO2(C,D,A,B,7,14,0x676f02d9);
|
||||
DO2(B,C,D,A,12,20,0x8d2a4c8a);
|
||||
|
||||
/* Round 3 */
|
||||
|
||||
DO3(A,B,C,D,5,4,0xfffa3942);
|
||||
DO3(D,A,B,C,8,11,0x8771f681);
|
||||
DO3(C,D,A,B,11,16,0x6d9d6122);
|
||||
DO3(B,C,D,A,14,23,0xfde5380c);
|
||||
|
||||
DO3(A,B,C,D,1,4,0xa4beea44);
|
||||
DO3(D,A,B,C,4,11,0x4bdecfa9);
|
||||
DO3(C,D,A,B,7,16,0xf6bb4b60);
|
||||
DO3(B,C,D,A,10,23,0xbebfbc70);
|
||||
|
||||
DO3(A,B,C,D,13,4,0x289b7ec6);
|
||||
DO3(D,A,B,C,0,11,0xeaa127fa);
|
||||
DO3(C,D,A,B,3,16,0xd4ef3085);
|
||||
DO3(B,C,D,A,6,23,0x4881d05);
|
||||
|
||||
DO3(A,B,C,D,9,4,0xd9d4d039);
|
||||
DO3(D,A,B,C,12,11,0xe6db99e5);
|
||||
DO3(C,D,A,B,15,16,0x1fa27cf8);
|
||||
DO3(B,C,D,A,2,23,0xc4ac5665);
|
||||
|
||||
/* Round 4 */
|
||||
|
||||
DO4(A,B,C,D,0,6,0xf4292244);
|
||||
DO4(D,A,B,C,7,10,0x432aff97);
|
||||
DO4(C,D,A,B,14,15,0xab9423a7);
|
||||
DO4(B,C,D,A,5,21,0xfc93a039);
|
||||
|
||||
DO4(A,B,C,D,12,6,0x655b59c3);
|
||||
DO4(D,A,B,C,3,10,0x8f0ccc92);
|
||||
DO4(C,D,A,B,10,15,0xffeff47d);
|
||||
DO4(B,C,D,A,1,21,0x85845dd1);
|
||||
|
||||
DO4(A,B,C,D,8,6,0x6fa87e4f);
|
||||
DO4(D,A,B,C,15,10,0xfe2ce6e0);
|
||||
DO4(C,D,A,B,6,15,0xa3014314);
|
||||
DO4(B,C,D,A,13,21,0x4e0811a1);
|
||||
|
||||
DO4(A,B,C,D,4,6,0xf7537e82);
|
||||
DO4(D,A,B,C,11,10,0xbd3af235);
|
||||
DO4(C,D,A,B,2,15,0x2ad7d2bb);
|
||||
DO4(B,C,D,A,9,21,0xeb86d391);
|
||||
|
||||
A += AA;
|
||||
B += BB;
|
||||
C += CC;
|
||||
D += DD;
|
||||
}
|
||||
|
||||
/*
|
||||
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
|
||||
*/
|
||||
|
||||
#if defined(WORDS_BIGENDIAN)
|
||||
static inline uint32_t
|
||||
swap_uint32_t (uint32_t t)
|
||||
{
|
||||
uint32_t temp1, temp2;
|
||||
|
||||
temp1 = cshift(t, 16);
|
||||
temp2 = temp1 >> 8;
|
||||
temp1 &= 0x00ff00ff;
|
||||
temp2 &= 0x00ff00ff;
|
||||
temp1 <<= 8;
|
||||
return temp1 | temp2;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct x32{
|
||||
unsigned int a:32;
|
||||
unsigned int b:32;
|
||||
};
|
||||
|
||||
void
|
||||
MD5_Update (struct md5 *m, const void *v, size_t len)
|
||||
{
|
||||
const unsigned char *p = v;
|
||||
size_t old_sz = m->sz[0];
|
||||
size_t offset;
|
||||
|
||||
m->sz[0] += len * 8;
|
||||
if (m->sz[0] < old_sz)
|
||||
++m->sz[1];
|
||||
offset = (old_sz / 8) % 64;
|
||||
while(len > 0){
|
||||
size_t l = min(len, 64 - offset);
|
||||
memcpy(m->save + offset, p, l);
|
||||
offset += l;
|
||||
p += l;
|
||||
len -= l;
|
||||
if(offset == 64){
|
||||
#if defined(WORDS_BIGENDIAN)
|
||||
int i;
|
||||
uint32_t current[16];
|
||||
struct x32 *u = (struct x32*)m->save;
|
||||
for(i = 0; i < 8; i++){
|
||||
current[2*i+0] = swap_uint32_t(u[i].a);
|
||||
current[2*i+1] = swap_uint32_t(u[i].b);
|
||||
}
|
||||
calc(m, current);
|
||||
#else
|
||||
calc(m, (uint32_t*)m->save);
|
||||
#endif
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
MD5_Final (void *res, struct md5 *m)
|
||||
{
|
||||
unsigned char zeros[72];
|
||||
unsigned offset = (m->sz[0] / 8) % 64;
|
||||
unsigned int dstart = (120 - offset - 1) % 64 + 1;
|
||||
|
||||
*zeros = 0x80;
|
||||
memset (zeros + 1, 0, sizeof(zeros) - 1);
|
||||
zeros[dstart+0] = (m->sz[0] >> 0) & 0xff;
|
||||
zeros[dstart+1] = (m->sz[0] >> 8) & 0xff;
|
||||
zeros[dstart+2] = (m->sz[0] >> 16) & 0xff;
|
||||
zeros[dstart+3] = (m->sz[0] >> 24) & 0xff;
|
||||
zeros[dstart+4] = (m->sz[1] >> 0) & 0xff;
|
||||
zeros[dstart+5] = (m->sz[1] >> 8) & 0xff;
|
||||
zeros[dstart+6] = (m->sz[1] >> 16) & 0xff;
|
||||
zeros[dstart+7] = (m->sz[1] >> 24) & 0xff;
|
||||
MD5_Update (m, zeros, dstart + 8);
|
||||
{
|
||||
int i;
|
||||
unsigned char *r = (unsigned char *)res;
|
||||
|
||||
for (i = 0; i < 4; ++i) {
|
||||
r[4*i] = m->counter[i] & 0xFF;
|
||||
r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
|
||||
r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
|
||||
r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
|
||||
}
|
||||
}
|
||||
#if 0
|
||||
{
|
||||
int i;
|
||||
uint32_t *r = (uint32_t *)res;
|
||||
|
||||
for (i = 0; i < 4; ++i)
|
||||
r[i] = swap_uint32_t (m->counter[i]);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: md5.h,v 1.11 2006/05/05 11:07:11 lha Exp $ */
|
||||
|
||||
#ifndef HEIM_MD5_H
|
||||
#define HEIM_MD5_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define MD5_Init hc_MD5_Init
|
||||
#define MD5_Update hc_MD5_Update
|
||||
#define MD5_Final hc_MD5_Final
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#define MD5_DIGEST_LENGTH 16
|
||||
|
||||
struct md5 {
|
||||
unsigned int sz[2];
|
||||
uint32_t counter[4];
|
||||
unsigned char save[64];
|
||||
};
|
||||
|
||||
typedef struct md5 MD5_CTX;
|
||||
|
||||
void MD5_Init (struct md5 *m);
|
||||
void MD5_Update (struct md5 *m, const void *p, size_t len);
|
||||
void MD5_Final (void *res, struct md5 *m); /* uint32_t res[4] */
|
||||
|
||||
#endif /* HEIM_MD5_H */
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* $Id: pkcs12.h,v 1.2 2006/01/13 15:26:52 lha Exp $
|
||||
*/
|
||||
|
||||
#ifndef _HEIM_PKCS12_H
|
||||
#define _HEIM_PKCS12_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define PKCS12_key_gen hc_PKCS12_key_gen
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#include <hcrypto/evp.h>
|
||||
|
||||
#define PKCS12_KEY_ID 1
|
||||
#define PKCS12_IV_ID 2
|
||||
|
||||
int PKCS12_key_gen(const void *, size_t, const void *,
|
||||
size_t, int, int, size_t, void *, const EVP_MD *);
|
||||
|
||||
|
||||
#endif /* _HEIM_PKCS12_H */
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
RCSID("$Id: pkcs5.c,v 1.3 2006/05/05 10:23:11 lha Exp $");
|
||||
|
||||
#ifdef KRB5
|
||||
#include <krb5-types.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <evp.h>
|
||||
#include <hmac.h>
|
||||
|
||||
#include <roken.h>
|
||||
|
||||
int
|
||||
PKCS5_PBKDF2_HMAC_SHA1(const void * password, size_t password_len,
|
||||
const void * salt, size_t salt_len,
|
||||
unsigned long iter,
|
||||
size_t keylen, void *key)
|
||||
{
|
||||
size_t datalen, leftofkey, checksumsize;
|
||||
char *data, *tmpcksum;
|
||||
uint32_t keypart;
|
||||
const EVP_MD *md;
|
||||
unsigned long i;
|
||||
int j;
|
||||
char *p;
|
||||
unsigned int hmacsize;
|
||||
|
||||
md = EVP_sha1();
|
||||
checksumsize = EVP_MD_size(md);
|
||||
datalen = salt_len + 4;
|
||||
|
||||
tmpcksum = malloc(checksumsize + datalen);
|
||||
if (tmpcksum == NULL)
|
||||
return 0;
|
||||
|
||||
data = &tmpcksum[checksumsize];
|
||||
|
||||
memcpy(data, salt, salt_len);
|
||||
|
||||
keypart = 1;
|
||||
leftofkey = keylen;
|
||||
p = key;
|
||||
|
||||
while (leftofkey) {
|
||||
int len;
|
||||
|
||||
if (leftofkey > checksumsize)
|
||||
len = checksumsize;
|
||||
else
|
||||
len = leftofkey;
|
||||
|
||||
data[datalen - 4] = (keypart >> 24) & 0xff;
|
||||
data[datalen - 3] = (keypart >> 16) & 0xff;
|
||||
data[datalen - 2] = (keypart >> 8) & 0xff;
|
||||
data[datalen - 1] = (keypart) & 0xff;
|
||||
|
||||
HMAC(md, password, password_len, data, datalen,
|
||||
tmpcksum, &hmacsize);
|
||||
|
||||
memcpy(p, tmpcksum, len);
|
||||
for (i = 1; i < iter; i++) {
|
||||
HMAC(md, password, password_len, tmpcksum, checksumsize,
|
||||
tmpcksum, &hmacsize);
|
||||
|
||||
for (j = 0; j < len; j++)
|
||||
p[j] ^= tmpcksum[j];
|
||||
}
|
||||
|
||||
p += len;
|
||||
leftofkey -= len;
|
||||
keypart++;
|
||||
}
|
||||
|
||||
free(tmpcksum);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
RCSID("$Id: rand-unix.c,v 1.2 2006/10/21 21:09:14 lha Exp $");
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <rand.h>
|
||||
|
||||
#include <roken.h>
|
||||
|
||||
/*
|
||||
* Unix /dev/random
|
||||
*/
|
||||
|
||||
static int
|
||||
get_device_fd(int flags)
|
||||
{
|
||||
static const char *rnd_devices[] = {
|
||||
"/dev/urandom",
|
||||
"/dev/random",
|
||||
"/dev/srandom",
|
||||
"/dev/arandom",
|
||||
NULL
|
||||
};
|
||||
const char **p;
|
||||
|
||||
for(p = rnd_devices; *p; p++) {
|
||||
int fd = open(*p, flags | O_NDELAY);
|
||||
if(fd >= 0)
|
||||
return fd;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void
|
||||
unix_seed(const void *indata, int size)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (size <= 0)
|
||||
return;
|
||||
|
||||
fd = get_device_fd(O_WRONLY);
|
||||
if (fd < 0)
|
||||
return;
|
||||
|
||||
write(fd, indata, size);
|
||||
close(fd);
|
||||
|
||||
}
|
||||
|
||||
static int
|
||||
unix_bytes(unsigned char *outdata, int size)
|
||||
{
|
||||
ssize_t count;
|
||||
int fd;
|
||||
|
||||
if (size <= 0)
|
||||
return 0;
|
||||
|
||||
fd = get_device_fd(O_RDONLY);
|
||||
if (fd < 0)
|
||||
return 0;
|
||||
|
||||
while (size > 0) {
|
||||
count = read (fd, outdata, size);
|
||||
if (count < 0 && errno == EINTR)
|
||||
continue;
|
||||
else if (count <= 0) {
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
outdata += count;
|
||||
size -= count;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
unix_cleanup(void)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
unix_add(const void *indata, int size, double entropi)
|
||||
{
|
||||
unix_seed(indata, size);
|
||||
}
|
||||
|
||||
static int
|
||||
unix_pseudorand(unsigned char *outdata, int size)
|
||||
{
|
||||
return unix_bytes(outdata, size);
|
||||
}
|
||||
|
||||
static int
|
||||
unix_status(void)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = get_device_fd(O_RDONLY);
|
||||
if (fd < 0)
|
||||
return 0;
|
||||
close(fd);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const RAND_METHOD hc_rand_unix_method = {
|
||||
unix_seed,
|
||||
unix_bytes,
|
||||
unix_cleanup,
|
||||
unix_add,
|
||||
unix_pseudorand,
|
||||
unix_status
|
||||
};
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
RCSID("$Id: rand.c,v 1.7 2006/10/16 10:23:01 lha Exp $");
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <rand.h>
|
||||
|
||||
#include <roken.h>
|
||||
|
||||
extern RAND_METHOD hc_rand_unix_method;
|
||||
static const RAND_METHOD *selected_meth = &hc_rand_unix_method;
|
||||
|
||||
void
|
||||
RAND_seed(const void *indata, size_t size)
|
||||
{
|
||||
(*selected_meth->seed)(indata, size);
|
||||
}
|
||||
|
||||
int
|
||||
RAND_bytes(void *outdata, size_t size)
|
||||
{
|
||||
return (*selected_meth->bytes)(outdata, size);
|
||||
}
|
||||
|
||||
void
|
||||
RAND_cleanup(void)
|
||||
{
|
||||
(*selected_meth->cleanup)();
|
||||
}
|
||||
|
||||
void
|
||||
RAND_add(const void *indata, size_t size, double entropi)
|
||||
{
|
||||
(*selected_meth->add)(indata, size, entropi);
|
||||
}
|
||||
|
||||
int
|
||||
RAND_pseudo_bytes(void *outdata, size_t size)
|
||||
{
|
||||
return (*selected_meth->pseudorand)(outdata, size);
|
||||
}
|
||||
|
||||
int
|
||||
RAND_status(void)
|
||||
{
|
||||
return (*selected_meth->status)();
|
||||
}
|
||||
|
||||
int
|
||||
RAND_set_rand_method(const RAND_METHOD *meth)
|
||||
{
|
||||
selected_meth = meth;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const RAND_METHOD *
|
||||
RAND_get_rand_method(void)
|
||||
{
|
||||
return selected_meth;
|
||||
}
|
||||
|
||||
int
|
||||
RAND_set_rand_engine(ENGINE *engine)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
RAND_load_file(const char *filename, size_t size)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
RAND_write_file(const char *filename)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
RAND_egd(const char *filename)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* $Id: rand.h,v 1.4 2006/04/17 13:23:04 lha Exp $
|
||||
*/
|
||||
|
||||
#ifndef _HEIM_RAND_H
|
||||
#define _HEIM_RAND_H 1
|
||||
|
||||
typedef struct RAND_METHOD RAND_METHOD;
|
||||
|
||||
#include <hcrypto/bn.h>
|
||||
#include <hcrypto/engine.h>
|
||||
|
||||
/* symbol renaming */
|
||||
#define RAND_bytes hc_RAND_bytes
|
||||
#define RAND_pseudo_bytes hc_RAND_pseudo_bytes
|
||||
#define RAND_seed hc_RAND_seed
|
||||
#define RAND_cleanup hc_RAND_cleanup
|
||||
#define RAND_add hc_RAND_add
|
||||
#define RAND_set_rand_method hc_RAND_set_rand_method
|
||||
#define RAND_get_rand_method hc_RAND_get_rand_method
|
||||
#define RAND_set_rand_engine hc_RAND_set_rand_engine
|
||||
#define RAND_load_file hc_RAND_load_file
|
||||
#define RAND_write_file hc_RAND_write_file
|
||||
#define RAND_status hc_RAND_status
|
||||
#define RAND_egd hc_RAND_egd
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
struct RAND_METHOD
|
||||
{
|
||||
void (*seed)(const void *, int);
|
||||
int (*bytes)(unsigned char *, int);
|
||||
void (*cleanup)(void);
|
||||
void (*add)(const void *, int, double);
|
||||
int (*pseudorand)(unsigned char *, int);
|
||||
int (*status)(void);
|
||||
};
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
int RAND_bytes(void *, size_t num);
|
||||
int RAND_pseudo_bytes(void *, size_t);
|
||||
void RAND_seed(const void *, size_t);
|
||||
void RAND_cleanup(void);
|
||||
void RAND_add(const void *, size_t, double);
|
||||
|
||||
int RAND_set_rand_method(const RAND_METHOD *);
|
||||
const RAND_METHOD *
|
||||
RAND_get_rand_method(void);
|
||||
int RAND_set_rand_engine(ENGINE *);
|
||||
|
||||
int RAND_load_file(const char *, size_t);
|
||||
int RAND_write_file(const char *);
|
||||
int RAND_status(void);
|
||||
int RAND_egd(const char *);
|
||||
|
||||
|
||||
#endif /* _HEIM_RAND_H */
|
||||
Executable
+245
@@ -0,0 +1,245 @@
|
||||
/*
|
||||
* Copyright (c) 2004 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
RCSID("$Id: rc2.c,v 1.7 2006/04/09 17:03:21 lha Exp $");
|
||||
#endif
|
||||
|
||||
#include "rc2.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* Implemented from Peter Gutmann's "Specification for Ron Rivests Cipher No.2"
|
||||
* rfc2268 and "On the Design and Security of RC2" was also useful.
|
||||
*/
|
||||
|
||||
static unsigned int Sbox[256] = {
|
||||
0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed,
|
||||
0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d,
|
||||
0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e,
|
||||
0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2,
|
||||
0x17, 0x9a, 0x59, 0xf5, 0x87, 0xb3, 0x4f, 0x13,
|
||||
0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32,
|
||||
0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b,
|
||||
0xf0, 0x95, 0x21, 0x22, 0x5c, 0x6b, 0x4e, 0x82,
|
||||
0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c,
|
||||
0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc,
|
||||
0x12, 0x75, 0xca, 0x1f, 0x3b, 0xbe, 0xe4, 0xd1,
|
||||
0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26,
|
||||
0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57,
|
||||
0x27, 0xf2, 0x1d, 0x9b, 0xbc, 0x94, 0x43, 0x03,
|
||||
0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7,
|
||||
0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7,
|
||||
0x08, 0xe8, 0xea, 0xde, 0x80, 0x52, 0xee, 0xf7,
|
||||
0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a,
|
||||
0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74,
|
||||
0x4b, 0x9f, 0xd0, 0x5e, 0x04, 0x18, 0xa4, 0xec,
|
||||
0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc,
|
||||
0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39,
|
||||
0x99, 0x7c, 0x3a, 0x85, 0x23, 0xb8, 0xb4, 0x7a,
|
||||
0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31,
|
||||
0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae,
|
||||
0x05, 0xdf, 0x29, 0x10, 0x67, 0x6c, 0xba, 0xc9,
|
||||
0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c,
|
||||
0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9,
|
||||
0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0,
|
||||
0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e,
|
||||
0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77,
|
||||
0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad
|
||||
};
|
||||
|
||||
void
|
||||
RC2_set_key(RC2_KEY *key, int len, const unsigned char *data, int bits)
|
||||
{
|
||||
unsigned char k[128];
|
||||
int j, T8, TM;
|
||||
|
||||
if (len <= 0)
|
||||
abort();
|
||||
if (len > 128)
|
||||
len = 128;
|
||||
if (bits <= 0 || bits > 1024)
|
||||
bits = 1024;
|
||||
|
||||
for (j = 0; j < len; j++)
|
||||
k[j] = data[j];
|
||||
for (; j < 128; j++)
|
||||
k[j] = Sbox[(k[j - len] + k[j - 1]) & 0xff];
|
||||
|
||||
T8 = (bits + 7) / 8;
|
||||
j = (8*T8 - bits);
|
||||
TM = 0xff >> j;
|
||||
|
||||
k[128 - T8] = Sbox[k[128 - T8] & TM];
|
||||
|
||||
for (j = 127 - T8; j >= 0; j--)
|
||||
k[j] = Sbox[k[j + 1] ^ k[j + T8]];
|
||||
|
||||
for (j = 0; j < 64; j++)
|
||||
key->data[j] = k[(j * 2) + 0] | (k[(j * 2) + 1] << 8);
|
||||
memset(k, 0, sizeof(k));
|
||||
}
|
||||
|
||||
#define ROT16L(w,n) ((w<<n)|(w>>(16-n)))
|
||||
#define ROT16R(w,n) ((w>>n)|(w<<(16-n)))
|
||||
|
||||
void
|
||||
RC2_encryptc(unsigned char *in, unsigned char *out, const RC2_KEY *key)
|
||||
{
|
||||
int i, j;
|
||||
int w0, w1, w2, w3;
|
||||
int t0, t1, t2, t3;
|
||||
|
||||
w0 = in[0] | (in[1] << 8);
|
||||
w1 = in[2] | (in[3] << 8);
|
||||
w2 = in[4] | (in[5] << 8);
|
||||
w3 = in[6] | (in[7] << 8);
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
j = i * 4;
|
||||
t0 = (w0 + (w1 & ~w3) + (w2 & w3) + key->data[j + 0]) & 0xffff;
|
||||
w0 = ROT16L(t0, 1);
|
||||
t1 = (w1 + (w2 & ~w0) + (w3 & w0) + key->data[j + 1]) & 0xffff;
|
||||
w1 = ROT16L(t1, 2);
|
||||
t2 = (w2 + (w3 & ~w1) + (w0 & w1) + key->data[j + 2]) & 0xffff;
|
||||
w2 = ROT16L(t2, 3);
|
||||
t3 = (w3 + (w0 & ~w2) + (w1 & w2) + key->data[j + 3]) & 0xffff;
|
||||
w3 = ROT16L(t3, 5);
|
||||
if(i == 4 || i == 10) {
|
||||
w0 += key->data[w3 & 63];
|
||||
w1 += key->data[w0 & 63];
|
||||
w2 += key->data[w1 & 63];
|
||||
w3 += key->data[w2 & 63];
|
||||
}
|
||||
}
|
||||
|
||||
out[0] = w0 & 0xff;
|
||||
out[1] = (w0 >> 8) & 0xff;
|
||||
out[2] = w1 & 0xff;
|
||||
out[3] = (w1 >> 8) & 0xff;
|
||||
out[4] = w2 & 0xff;
|
||||
out[5] = (w2 >> 8) & 0xff;
|
||||
out[6] = w3 & 0xff;
|
||||
out[7] = (w3 >> 8) & 0xff;
|
||||
}
|
||||
|
||||
void
|
||||
RC2_decryptc(unsigned char *in, unsigned char *out, const RC2_KEY *key)
|
||||
{
|
||||
int i, j;
|
||||
int w0, w1, w2, w3;
|
||||
int t0, t1, t2, t3;
|
||||
|
||||
w0 = in[0] | (in[1] << 8);
|
||||
w1 = in[2] | (in[3] << 8);
|
||||
w2 = in[4] | (in[5] << 8);
|
||||
w3 = in[6] | (in[7] << 8);
|
||||
|
||||
for (i = 15; i >= 0; i--) {
|
||||
j = i * 4;
|
||||
|
||||
if(i == 4 || i == 10) {
|
||||
w3 = (w3 - key->data[w2 & 63]) & 0xffff;
|
||||
w2 = (w2 - key->data[w1 & 63]) & 0xffff;
|
||||
w1 = (w1 - key->data[w0 & 63]) & 0xffff;
|
||||
w0 = (w0 - key->data[w3 & 63]) & 0xffff;
|
||||
}
|
||||
|
||||
t3 = ROT16R(w3, 5);
|
||||
w3 = (t3 - (w0 & ~w2) - (w1 & w2) - key->data[j + 3]) & 0xffff;
|
||||
t2 = ROT16R(w2, 3);
|
||||
w2 = (t2 - (w3 & ~w1) - (w0 & w1) - key->data[j + 2]) & 0xffff;
|
||||
t1 = ROT16R(w1, 2);
|
||||
w1 = (t1 - (w2 & ~w0) - (w3 & w0) - key->data[j + 1]) & 0xffff;
|
||||
t0 = ROT16R(w0, 1);
|
||||
w0 = (t0 - (w1 & ~w3) - (w2 & w3) - key->data[j + 0]) & 0xffff;
|
||||
|
||||
}
|
||||
out[0] = w0 & 0xff;
|
||||
out[1] = (w0 >> 8) & 0xff;
|
||||
out[2] = w1 & 0xff;
|
||||
out[3] = (w1 >> 8) & 0xff;
|
||||
out[4] = w2 & 0xff;
|
||||
out[5] = (w2 >> 8) & 0xff;
|
||||
out[6] = w3 & 0xff;
|
||||
out[7] = (w3 >> 8) & 0xff;
|
||||
}
|
||||
|
||||
void
|
||||
RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long size,
|
||||
RC2_KEY *key, unsigned char *iv, int forward_encrypt)
|
||||
{
|
||||
unsigned char tmp[RC2_BLOCK_SIZE];
|
||||
int i;
|
||||
|
||||
if (forward_encrypt) {
|
||||
while (size >= RC2_BLOCK_SIZE) {
|
||||
for (i = 0; i < RC2_BLOCK_SIZE; i++)
|
||||
tmp[i] = in[i] ^ iv[i];
|
||||
RC2_encryptc(tmp, out, key);
|
||||
memcpy(iv, out, RC2_BLOCK_SIZE);
|
||||
size -= RC2_BLOCK_SIZE;
|
||||
in += RC2_BLOCK_SIZE;
|
||||
out += RC2_BLOCK_SIZE;
|
||||
}
|
||||
if (size) {
|
||||
for (i = 0; i < size; i++)
|
||||
tmp[i] = in[i] ^ iv[i];
|
||||
for (i = size; i < RC2_BLOCK_SIZE; i++)
|
||||
tmp[i] = iv[i];
|
||||
RC2_encryptc(tmp, out, key);
|
||||
memcpy(iv, out, RC2_BLOCK_SIZE);
|
||||
}
|
||||
} else {
|
||||
while (size >= RC2_BLOCK_SIZE) {
|
||||
memcpy(tmp, in, RC2_BLOCK_SIZE);
|
||||
RC2_decryptc(tmp, out, key);
|
||||
for (i = 0; i < RC2_BLOCK_SIZE; i++)
|
||||
out[i] ^= iv[i];
|
||||
memcpy(iv, tmp, RC2_BLOCK_SIZE);
|
||||
size -= RC2_BLOCK_SIZE;
|
||||
in += RC2_BLOCK_SIZE;
|
||||
out += RC2_BLOCK_SIZE;
|
||||
}
|
||||
if (size) {
|
||||
memcpy(tmp, in, RC2_BLOCK_SIZE);
|
||||
RC2_decryptc(tmp, out, key);
|
||||
for (i = 0; i < size; i++)
|
||||
out[i] ^= iv[i];
|
||||
memcpy(iv, tmp, RC2_BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+71
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2004 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: rc2.h,v 1.2 2006/01/08 21:47:29 lha Exp $ */
|
||||
|
||||
/* symbol renaming */
|
||||
#define RC2_set_key hc_RC2_set_key
|
||||
#define RC2_encryptc hc_RC2_encryptc
|
||||
#define RC2_decryptc hc_RC2_decryptc
|
||||
#define RC2_cbc_encrypt hc_RC2_cbc_encrypt
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#define RC2_ENCRYPT 1
|
||||
#define RC2_DECRYPT 0
|
||||
|
||||
#define RC2_BLOCK_SIZE 8
|
||||
#define RC2_BLOCK RC2_BLOCK_SIZE
|
||||
#define RC2_KEY_LENGTH 16
|
||||
|
||||
typedef struct rc2_key {
|
||||
unsigned int data[64];
|
||||
} RC2_KEY;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void RC2_set_key(RC2_KEY *, int, const unsigned char *,int);
|
||||
|
||||
void RC2_encryptc(unsigned char *, unsigned char *, const RC2_KEY *);
|
||||
void RC2_decryptc(unsigned char *, unsigned char *, const RC2_KEY *);
|
||||
|
||||
void RC2_cbc_encrypt(const unsigned char *, unsigned char *, long,
|
||||
RC2_KEY *, unsigned char *, int);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Executable
+82
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (c) 2004 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* implemented from description in draft-kaukonen-cipher-arcfour-03.txt */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
|
||||
RCSID("$Id: rc4.c,v 1.1 2004/03/25 16:40:59 lha Exp $");
|
||||
#endif
|
||||
|
||||
#include <rc4.h>
|
||||
|
||||
#define SWAP(k,x,y) \
|
||||
{ unsigned int _t; \
|
||||
_t = k->state[x]; \
|
||||
k->state[x] = k->state[y]; \
|
||||
k->state[y] = _t; \
|
||||
}
|
||||
|
||||
void
|
||||
RC4_set_key(RC4_KEY *key, const int len, unsigned char *data)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < 256; i++)
|
||||
key->state[i] = i;
|
||||
for (i = 0, j = 0; i < 256; i++) {
|
||||
j = (j + key->state[i] + data[i % len]) % 256;
|
||||
SWAP(key, i, j);
|
||||
}
|
||||
key->x = key->y = 0;
|
||||
}
|
||||
|
||||
void
|
||||
RC4(RC4_KEY *key, const int len, const unsigned char *in, unsigned char *out)
|
||||
{
|
||||
int i, t;
|
||||
unsigned x, y;
|
||||
|
||||
x = key->x;
|
||||
y = key->y;
|
||||
for (i = 0; i < len; i++) {
|
||||
x = (x + 1) % 256;
|
||||
y = (y + key->state[x]) % 256;
|
||||
SWAP(key, x, y);
|
||||
t = (key->state[x] + key->state[y]) % 256;
|
||||
*out++ = key->state[t] ^ *in++;
|
||||
}
|
||||
key->x = x;
|
||||
key->y = y;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2004 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: rc4.h,v 1.4 2006/01/08 21:47:29 lha Exp $ */
|
||||
|
||||
/* symbol renaming */
|
||||
#define RC4_set_key hc_RC4_set_key
|
||||
#define RC4 hc_RC4
|
||||
|
||||
typedef struct rc4_key {
|
||||
unsigned int x, y;
|
||||
unsigned int state[256];
|
||||
} RC4_KEY;
|
||||
|
||||
void RC4_set_key(RC4_KEY *, const int, unsigned char *);
|
||||
void RC4(RC4_KEY *, const int, const unsigned char *, unsigned char *);
|
||||
+1231
File diff suppressed because it is too large
Load Diff
+46
@@ -0,0 +1,46 @@
|
||||
/* $NetBSD: rijndael-alg-fst.h,v 1.2 2000/10/02 17:19:15 itojun Exp $ */
|
||||
/* $KAME: rijndael-alg-fst.h,v 1.5 2003/07/15 10:47:16 itojun Exp $ */
|
||||
/**
|
||||
* rijndael-alg-fst.h
|
||||
*
|
||||
* @version 3.0 (December 2000)
|
||||
*
|
||||
* Optimised ANSI C code for the Rijndael cipher (now AES)
|
||||
*
|
||||
* @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
|
||||
* @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
|
||||
* @author Paulo Barreto <paulo.barreto@terra.com.br>
|
||||
*
|
||||
* This code is hereby placed in the public domain.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
||||
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef __RIJNDAEL_ALG_FST_H
|
||||
#define __RIJNDAEL_ALG_FST_H
|
||||
|
||||
/* symbol renaming */
|
||||
#define rijndaelKeySetupEnc _hc_rijndaelKeySetupEnc
|
||||
#define rijndaelKeySetupDec _hc_rijndaelKeySetupDec
|
||||
#define rijndaelEncrypt _hc_rijndaelEncrypt
|
||||
#define rijndaelDecrypt _hc_rijndaelDecrypt
|
||||
|
||||
#define RIJNDAEL_MAXKC (256/32)
|
||||
#define RIJNDAEL_MAXKB (256/8)
|
||||
#define RIJNDAEL_MAXNR 14
|
||||
|
||||
int rijndaelKeySetupEnc(uint32_t rk[/*4*(Nr + 1)*/], const uint8_t cipherKey[], int keyBits);
|
||||
int rijndaelKeySetupDec(uint32_t rk[/*4*(Nr + 1)*/], const uint8_t cipherKey[], int keyBits);
|
||||
void rijndaelEncrypt(const uint32_t rk[/*4*(Nr + 1)*/], int Nr, const uint8_t pt[16], uint8_t ct[16]);
|
||||
void rijndaelDecrypt(const uint32_t rk[/*4*(Nr + 1)*/], int Nr, const uint8_t ct[16], uint8_t pt[16]);
|
||||
|
||||
#endif /* __RIJNDAEL_ALG_FST_H */
|
||||
@@ -0,0 +1,509 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 1996, 1997, 1999 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
|
||||
RCSID("$Id: rnd_keys.c,v 1.71 2006/05/05 10:24:31 lha Exp $");
|
||||
#endif
|
||||
|
||||
#ifdef KRB5
|
||||
#include <krb5-types.h>
|
||||
#endif
|
||||
#include <des.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef TIME_WITH_SYS_TIME
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#elif defined(HAVE_SYS_TIME_H)
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#ifdef HAVE_IO_H
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SIGNAL_H
|
||||
#include <signal.h>
|
||||
#endif
|
||||
#ifdef HAVE_FCNTL_H
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Generate "random" data by checksumming a file.
|
||||
*
|
||||
* Returns -1 if there were any problems with permissions or I/O
|
||||
* errors.
|
||||
*/
|
||||
static
|
||||
int
|
||||
sumFile (const char *name, int len, void *res)
|
||||
{
|
||||
uint32_t sum[2] = { 0, 0 };
|
||||
uint32_t buf[1024*2];
|
||||
int fd, i;
|
||||
|
||||
fd = open (name, 0);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
while (len > 0)
|
||||
{
|
||||
int n = read(fd, buf, sizeof(buf));
|
||||
if (n < 0)
|
||||
{
|
||||
close(fd);
|
||||
return n;
|
||||
}
|
||||
for (i = 0; i < (n/sizeof(buf[0])); i++)
|
||||
{
|
||||
sum[0] += buf[i];
|
||||
i++;
|
||||
sum[1] += buf[i];
|
||||
}
|
||||
len -= n;
|
||||
}
|
||||
close (fd);
|
||||
memcpy (res, &sum, sizeof(sum));
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static
|
||||
int
|
||||
md5sumFile (const char *name, int len, int32_t sum[4])
|
||||
{
|
||||
int32_t buf[1024*2];
|
||||
int fd, cnt;
|
||||
struct md5 md5;
|
||||
|
||||
fd = open (name, 0);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
md5_init(&md5);
|
||||
while (len > 0)
|
||||
{
|
||||
int n = read(fd, buf, sizeof(buf));
|
||||
if (n < 0)
|
||||
{
|
||||
close(fd);
|
||||
return n;
|
||||
}
|
||||
md5_update(&md5, buf, n);
|
||||
len -= n;
|
||||
}
|
||||
md5_finito(&md5, (unsigned char *)sum);
|
||||
close (fd);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Create a sequence of random 64 bit blocks.
|
||||
* The sequence is indexed with a long long and
|
||||
* based on an initial des key used as a seed.
|
||||
*/
|
||||
static DES_key_schedule sequence_seed;
|
||||
static uint32_t sequence_index[2];
|
||||
|
||||
/*
|
||||
* Random number generator based on ideas from truerand in cryptolib
|
||||
* as described on page 424 in Applied Cryptography 2 ed. by Bruce
|
||||
* Schneier.
|
||||
*/
|
||||
|
||||
static volatile int counter;
|
||||
static volatile unsigned char *gdata; /* Global data */
|
||||
static volatile int igdata; /* Index into global data */
|
||||
static int gsize;
|
||||
|
||||
#if !defined(WIN32) && !defined(__EMX__) && !defined(__OS2__) && !defined(__CYGWIN32__)
|
||||
/* Visual C++ 4.0 (Windows95/NT) */
|
||||
|
||||
static
|
||||
RETSIGTYPE
|
||||
sigALRM(int sig)
|
||||
{
|
||||
if (igdata < gsize)
|
||||
gdata[igdata++] ^= counter & 0xff;
|
||||
|
||||
#ifndef HAVE_SIGACTION
|
||||
signal(SIGALRM, sigALRM); /* Reinstall SysV signal handler */
|
||||
#endif
|
||||
SIGRETURN(0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(HAVE_RANDOM) && defined(HAVE_RAND)
|
||||
#ifndef srandom
|
||||
#define srandom srand
|
||||
#endif
|
||||
#ifndef random
|
||||
#define random rand
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined(HAVE_SETITIMER) || defined(WIN32) || defined(__EMX__) || defined(__OS2__) || defined(__CYGWIN32__)
|
||||
static void
|
||||
des_not_rand_data(unsigned char *data, int size)
|
||||
{
|
||||
int i;
|
||||
|
||||
srandom (time (NULL));
|
||||
|
||||
for(i = 0; i < size; ++i)
|
||||
data[i] ^= random() % 0x100;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(WIN32) && !defined(__EMX__) && !defined(__OS2__) && !defined(__CYGWIN32__)
|
||||
|
||||
#ifndef HAVE_SETITIMER
|
||||
static void
|
||||
pacemaker(struct timeval *tv)
|
||||
{
|
||||
fd_set fds;
|
||||
pid_t pid;
|
||||
pid = getppid();
|
||||
while(1){
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(0, &fds);
|
||||
select(1, &fds, NULL, NULL, tv);
|
||||
kill(pid, SIGALRM);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SIGACTION
|
||||
/* XXX ugly hack, should perhaps use function from roken */
|
||||
static RETSIGTYPE
|
||||
(*fake_signal(int sig, RETSIGTYPE (*f)(int)))(int)
|
||||
{
|
||||
struct sigaction sa, osa;
|
||||
sa.sa_handler = f;
|
||||
sa.sa_flags = 0;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sigaction(sig, &sa, &osa);
|
||||
return osa.sa_handler;
|
||||
}
|
||||
#define signal(S, F) fake_signal((S), (F))
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Generate size bytes of "random" data using timed interrupts.
|
||||
* It takes about 40ms/byte random data.
|
||||
* It's not neccessary to be root to run it.
|
||||
*/
|
||||
void
|
||||
DES_rand_data(void *outdata, int size)
|
||||
{
|
||||
unsigned char *data = outdata;
|
||||
struct itimerval tv, otv;
|
||||
RETSIGTYPE (*osa)(int);
|
||||
int i, j;
|
||||
#ifndef HAVE_SETITIMER
|
||||
RETSIGTYPE (*ochld)(int);
|
||||
pid_t pid;
|
||||
#endif
|
||||
const char *rnd_devices[] = {"/dev/random",
|
||||
"/dev/srandom",
|
||||
"/dev/urandom",
|
||||
"/dev/arandom",
|
||||
NULL};
|
||||
const char **p;
|
||||
|
||||
for(p = rnd_devices; *p; p++) {
|
||||
int fd = open(*p, O_RDONLY | O_NDELAY);
|
||||
|
||||
if(fd >= 0 && read(fd, data, size) == size) {
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
/* Paranoia? Initialize data from /dev/mem if we can read it. */
|
||||
if (size >= 8)
|
||||
sumFile("/dev/mem", (1024*1024*2), data);
|
||||
|
||||
gdata = data;
|
||||
gsize = size;
|
||||
igdata = 0;
|
||||
|
||||
osa = signal(SIGALRM, sigALRM);
|
||||
|
||||
/* Start timer */
|
||||
tv.it_value.tv_sec = 0;
|
||||
tv.it_value.tv_usec = 10 * 1000; /* 10 ms */
|
||||
tv.it_interval = tv.it_value;
|
||||
#ifdef HAVE_SETITIMER
|
||||
setitimer(ITIMER_REAL, &tv, &otv);
|
||||
#else
|
||||
ochld = signal(SIGCHLD, SIG_IGN);
|
||||
pid = fork();
|
||||
if(pid == -1){
|
||||
signal(SIGCHLD, ochld != SIG_ERR ? ochld : SIG_DFL);
|
||||
des_not_rand_data(data, size);
|
||||
return;
|
||||
}
|
||||
if(pid == 0)
|
||||
pacemaker(&tv.it_interval);
|
||||
#endif
|
||||
|
||||
for(i = 0; i < 4; i++) {
|
||||
for (igdata = 0; igdata < size;) /* igdata++ in sigALRM */
|
||||
counter++;
|
||||
for (j = 0; j < size; j++) /* Only use 2 bits each lap */
|
||||
gdata[j] = (gdata[j]>>2) | (gdata[j]<<6);
|
||||
}
|
||||
#ifdef HAVE_SETITIMER
|
||||
setitimer(ITIMER_REAL, &otv, 0);
|
||||
#else
|
||||
kill(pid, SIGKILL);
|
||||
while(waitpid(pid, NULL, 0) != pid);
|
||||
signal(SIGCHLD, ochld != SIG_ERR ? ochld : SIG_DFL);
|
||||
#endif
|
||||
signal(SIGALRM, osa != SIG_ERR ? osa : SIG_DFL);
|
||||
}
|
||||
#else
|
||||
void
|
||||
DES_rand_data(unsigned char *p, int s)
|
||||
{
|
||||
des_not_rand_data (p, s);
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
DES_generate_random_block(DES_cblock *block)
|
||||
{
|
||||
DES_rand_data((unsigned char *)block, sizeof(*block));
|
||||
}
|
||||
|
||||
#define DES_rand_data_key hc_DES_rand_data_key
|
||||
|
||||
void
|
||||
DES_rand_data_key(DES_cblock *key);
|
||||
|
||||
/*
|
||||
* Generate a "random" DES key.
|
||||
*/
|
||||
void
|
||||
DES_rand_data_key(DES_cblock *key)
|
||||
{
|
||||
unsigned char data[8];
|
||||
DES_key_schedule sched;
|
||||
do {
|
||||
DES_rand_data(data, sizeof(data));
|
||||
DES_rand_data((unsigned char*)key, sizeof(DES_cblock));
|
||||
DES_set_odd_parity(key);
|
||||
DES_set_key(key, &sched);
|
||||
DES_ecb_encrypt(&data, key, &sched, DES_ENCRYPT);
|
||||
memset(&data, 0, sizeof(data));
|
||||
memset(&sched, 0, sizeof(sched));
|
||||
DES_set_odd_parity(key);
|
||||
} while(DES_is_weak_key(key));
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate "random" data by checksumming /dev/mem
|
||||
*
|
||||
* It's neccessary to be root to run it. Returns -1 if there were any
|
||||
* problems with permissions.
|
||||
*/
|
||||
|
||||
#define DES_mem_rand8 hc_DES_mem_rand8
|
||||
|
||||
int
|
||||
DES_mem_rand8(unsigned char *data);
|
||||
|
||||
int
|
||||
DES_mem_rand8(unsigned char *data)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* In case the generator does not get initialized use this as fallback.
|
||||
*/
|
||||
static int initialized;
|
||||
|
||||
static void
|
||||
do_initialize(void)
|
||||
{
|
||||
DES_cblock default_seed;
|
||||
do {
|
||||
DES_generate_random_block(&default_seed);
|
||||
DES_set_odd_parity(&default_seed);
|
||||
} while (DES_is_weak_key(&default_seed));
|
||||
DES_init_random_number_generator(&default_seed);
|
||||
}
|
||||
|
||||
#define zero_long_long(ll) do { ll[0] = ll[1] = 0; } while (0)
|
||||
|
||||
#define incr_long_long(ll) do { if (++ll[0] == 0) ++ll[1]; } while (0)
|
||||
|
||||
#define set_sequence_number(ll) \
|
||||
memcpy((char *)sequence_index, (ll), sizeof(sequence_index));
|
||||
|
||||
/*
|
||||
* Set the sequnce number to this value (a long long).
|
||||
*/
|
||||
void
|
||||
DES_set_sequence_number(void *ll)
|
||||
{
|
||||
set_sequence_number(ll);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the generator seed and reset the sequence number to 0.
|
||||
*/
|
||||
void
|
||||
DES_set_random_generator_seed(DES_cblock *seed)
|
||||
{
|
||||
DES_set_key(seed, &sequence_seed);
|
||||
zero_long_long(sequence_index);
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate a sequence of random des keys
|
||||
* using the random block sequence, fixup
|
||||
* parity and skip weak keys.
|
||||
*/
|
||||
int
|
||||
DES_new_random_key(DES_cblock *key)
|
||||
{
|
||||
if (!initialized)
|
||||
do_initialize();
|
||||
|
||||
do {
|
||||
DES_ecb_encrypt((DES_cblock *) sequence_index,
|
||||
key,
|
||||
&sequence_seed,
|
||||
DES_ENCRYPT);
|
||||
incr_long_long(sequence_index);
|
||||
/* random key must have odd parity and not be weak */
|
||||
DES_set_odd_parity(key);
|
||||
} while (DES_is_weak_key(key));
|
||||
return(0);
|
||||
}
|
||||
|
||||
/*
|
||||
* des_init_random_number_generator:
|
||||
*
|
||||
* Initialize the sequence of random 64 bit blocks. The input seed
|
||||
* can be a secret key since it should be well hidden and is also not
|
||||
* kept.
|
||||
*
|
||||
*/
|
||||
void
|
||||
DES_init_random_number_generator(DES_cblock *seed)
|
||||
{
|
||||
struct timeval now;
|
||||
DES_cblock uniq;
|
||||
DES_cblock new_key;
|
||||
|
||||
gettimeofday(&now, (struct timezone *)0);
|
||||
DES_generate_random_block(&uniq);
|
||||
|
||||
/* Pick a unique random key from the shared sequence. */
|
||||
DES_set_random_generator_seed(seed);
|
||||
set_sequence_number((unsigned char *)&uniq);
|
||||
DES_new_random_key(&new_key);
|
||||
|
||||
/* Select a new nonshared sequence, */
|
||||
DES_set_random_generator_seed(&new_key);
|
||||
|
||||
/* and use the current time to pick a key for the new sequence. */
|
||||
set_sequence_number((unsigned char *)&now);
|
||||
DES_new_random_key(&new_key);
|
||||
DES_set_random_generator_seed(&new_key);
|
||||
}
|
||||
|
||||
/* This is for backwards compatibility. */
|
||||
void
|
||||
DES_random_key(DES_cblock *ret)
|
||||
{
|
||||
DES_new_random_key(ret);
|
||||
}
|
||||
|
||||
#ifdef TESTRUN
|
||||
int
|
||||
main()
|
||||
{
|
||||
unsigned char data[8];
|
||||
int i;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (sumFile("/dev/mem", (1024*1024*8), data) != 0)
|
||||
{ perror("sumFile"); exit(1); }
|
||||
for (i = 0; i < 8; i++)
|
||||
printf("%02x", data[i]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef TESTRUN2
|
||||
int
|
||||
main()
|
||||
{
|
||||
DES_cblock data;
|
||||
int i;
|
||||
|
||||
while (1)
|
||||
{
|
||||
do_initialize();
|
||||
DES_random_key(data);
|
||||
for (i = 0; i < 8; i++)
|
||||
printf("%02x", data[i]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* $Id: rsa.h,v 1.5 2006/05/07 11:34:02 lha Exp $
|
||||
*/
|
||||
|
||||
#ifndef _HEIM_RSA_H
|
||||
#define _HEIM_RSA_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define RSA_null_method hc_RSA_null_method
|
||||
#define RSA_imath_method hc_RSA_imath_method
|
||||
#define RSA_new hc_RSA_new
|
||||
#define RSA_new_method hc_RSA_new_method
|
||||
#define RSA_free hc_RSA_free
|
||||
#define RSA_up_ref hc_RSA_up_ref
|
||||
#define RSA_set_default_method hc_RSA_set_default_method
|
||||
#define RSA_get_default_method hc_RSA_get_default_method
|
||||
#define RSA_set_method hc_RSA_set_method
|
||||
#define RSA_get_method hc_RSA_get_method
|
||||
#define RSA_set_app_data hc_RSA_set_app_data
|
||||
#define RSA_get_app_data hc_RSA_get_app_data
|
||||
#define RSA_check_key hc_RSA_check_key
|
||||
#define RSA_size hc_RSA_size
|
||||
#define RSA_public_encrypt hc_RSA_public_encrypt
|
||||
#define RSA_public_decrypt hc_RSA_public_decrypt
|
||||
#define RSA_private_encrypt hc_RSA_private_encrypt
|
||||
#define RSA_private_decrypt hc_RSA_private_decrypt
|
||||
#define RSA_sign hc_RSA_sign
|
||||
#define RSA_verify hc_RSA_verify
|
||||
#define d2i_RSAPrivateKey hc_d2i_RSAPrivateKey
|
||||
#define i2d_RSAPublicKey hc_i2d_RSAPublicKey
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
typedef struct RSA RSA;
|
||||
typedef struct RSA_METHOD RSA_METHOD;
|
||||
|
||||
#include <hcrypto/bn.h>
|
||||
#include <hcrypto/engine.h>
|
||||
|
||||
struct RSA_METHOD {
|
||||
const char *name;
|
||||
int (*rsa_pub_enc)(int,const unsigned char *, unsigned char *, RSA *,int);
|
||||
int (*rsa_pub_dec)(int,const unsigned char *, unsigned char *, RSA *,int);
|
||||
int (*rsa_priv_enc)(int,const unsigned char *, unsigned char *, RSA *,int);
|
||||
int (*rsa_priv_dec)(int,const unsigned char *, unsigned char *, RSA *,int);
|
||||
void *rsa_mod_exp;
|
||||
void *bn_mod_exp;
|
||||
int (*init)(RSA *rsa);
|
||||
int (*finish)(RSA *rsa);
|
||||
int flags;
|
||||
char *app_data;
|
||||
int (*rsa_sign)(int, const unsigned char *, unsigned int,
|
||||
unsigned char *, unsigned int *, const RSA *);
|
||||
int (*rsa_verify)(int, const unsigned char *, unsigned int,
|
||||
unsigned char *, unsigned int, const RSA *);
|
||||
int (*rsa_keygen)(RSA *, int, BIGNUM *, BN_GENCB *);
|
||||
};
|
||||
|
||||
struct RSA {
|
||||
int pad;
|
||||
long version;
|
||||
const RSA_METHOD *meth;
|
||||
void *engine;
|
||||
BIGNUM *n;
|
||||
BIGNUM *e;
|
||||
BIGNUM *d;
|
||||
BIGNUM *p;
|
||||
BIGNUM *q;
|
||||
BIGNUM *dmp1;
|
||||
BIGNUM *dmq1;
|
||||
BIGNUM *iqmp;
|
||||
struct rsa_CRYPTO_EX_DATA {
|
||||
void *sk;
|
||||
int dummy;
|
||||
} ex_data;
|
||||
int references;
|
||||
int flags;
|
||||
void *_method_mod_n;
|
||||
void *_method_mod_p;
|
||||
void *_method_mod_q;
|
||||
|
||||
char *bignum_data;
|
||||
void *blinding;
|
||||
void *mt_blinding;
|
||||
};
|
||||
|
||||
#define RSA_FLAG_SIGN_VER 0x40
|
||||
|
||||
#define RSA_PKCS1_PADDING 1
|
||||
#define RSA_PKCS1_PADDING_SIZE 11
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
const RSA_METHOD *RSA_null_method(void);
|
||||
const RSA_METHOD *RSA_imath_method(void);
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
RSA * RSA_new(void);
|
||||
RSA * RSA_new_method(ENGINE *);
|
||||
void RSA_free(RSA *);
|
||||
int RSA_up_ref(RSA *);
|
||||
|
||||
void RSA_set_default_method(const RSA_METHOD *);
|
||||
const RSA_METHOD * RSA_get_default_method(void);
|
||||
|
||||
const RSA_METHOD * RSA_get_method(const RSA *);
|
||||
int RSA_set_method(RSA *, const RSA_METHOD *);
|
||||
|
||||
int RSA_set_app_data(RSA *, void *arg);
|
||||
void * RSA_get_app_data(RSA *);
|
||||
|
||||
int RSA_check_key(const RSA *);
|
||||
int RSA_size(const RSA *);
|
||||
|
||||
int RSA_public_encrypt(int,const unsigned char*,unsigned char*,RSA *,int);
|
||||
int RSA_private_encrypt(int,const unsigned char*,unsigned char*,RSA *,int);
|
||||
int RSA_public_decrypt(int,const unsigned char*,unsigned char*,RSA *,int);
|
||||
int RSA_private_decrypt(int,const unsigned char*,unsigned char*,RSA *,int);
|
||||
|
||||
int RSA_sign(int, const unsigned char *, unsigned int,
|
||||
unsigned char *, unsigned int *, RSA *);
|
||||
int RSA_verify(int, const unsigned char *, unsigned int,
|
||||
unsigned char *, unsigned int, RSA *);
|
||||
|
||||
RSA * d2i_RSAPrivateKey(RSA *, const unsigned char **, size_t);
|
||||
int i2d_RSAPublicKey(RSA *, unsigned char **);
|
||||
|
||||
#endif /* _HEIM_RSA_H */
|
||||
@@ -0,0 +1,300 @@
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
|
||||
RCSID("$Id: sha.c,v 1.19 2006/05/05 10:25:00 lha Exp $");
|
||||
#endif
|
||||
|
||||
#include "hash.h"
|
||||
#include "sha.h"
|
||||
|
||||
#define A m->counter[0]
|
||||
#define B m->counter[1]
|
||||
#define C m->counter[2]
|
||||
#define D m->counter[3]
|
||||
#define E m->counter[4]
|
||||
#define X data
|
||||
|
||||
void
|
||||
SHA1_Init (struct sha *m)
|
||||
{
|
||||
m->sz[0] = 0;
|
||||
m->sz[1] = 0;
|
||||
A = 0x67452301;
|
||||
B = 0xefcdab89;
|
||||
C = 0x98badcfe;
|
||||
D = 0x10325476;
|
||||
E = 0xc3d2e1f0;
|
||||
}
|
||||
|
||||
|
||||
#define F0(x,y,z) CRAYFIX((x & y) | (~x & z))
|
||||
#define F1(x,y,z) (x ^ y ^ z)
|
||||
#define F2(x,y,z) ((x & y) | (x & z) | (y & z))
|
||||
#define F3(x,y,z) F1(x,y,z)
|
||||
|
||||
#define K0 0x5a827999
|
||||
#define K1 0x6ed9eba1
|
||||
#define K2 0x8f1bbcdc
|
||||
#define K3 0xca62c1d6
|
||||
|
||||
#define DO(t,f,k) \
|
||||
do { \
|
||||
uint32_t temp; \
|
||||
\
|
||||
temp = cshift(AA, 5) + f(BB,CC,DD) + EE + data[t] + k; \
|
||||
EE = DD; \
|
||||
DD = CC; \
|
||||
CC = cshift(BB, 30); \
|
||||
BB = AA; \
|
||||
AA = temp; \
|
||||
} while(0)
|
||||
|
||||
static inline void
|
||||
calc (struct sha *m, uint32_t *in)
|
||||
{
|
||||
uint32_t AA, BB, CC, DD, EE;
|
||||
uint32_t data[80];
|
||||
int i;
|
||||
|
||||
AA = A;
|
||||
BB = B;
|
||||
CC = C;
|
||||
DD = D;
|
||||
EE = E;
|
||||
|
||||
for (i = 0; i < 16; ++i)
|
||||
data[i] = in[i];
|
||||
for (i = 16; i < 80; ++i)
|
||||
data[i] = cshift(data[i-3] ^ data[i-8] ^ data[i-14] ^ data[i-16], 1);
|
||||
|
||||
/* t=[0,19] */
|
||||
|
||||
DO(0,F0,K0);
|
||||
DO(1,F0,K0);
|
||||
DO(2,F0,K0);
|
||||
DO(3,F0,K0);
|
||||
DO(4,F0,K0);
|
||||
DO(5,F0,K0);
|
||||
DO(6,F0,K0);
|
||||
DO(7,F0,K0);
|
||||
DO(8,F0,K0);
|
||||
DO(9,F0,K0);
|
||||
DO(10,F0,K0);
|
||||
DO(11,F0,K0);
|
||||
DO(12,F0,K0);
|
||||
DO(13,F0,K0);
|
||||
DO(14,F0,K0);
|
||||
DO(15,F0,K0);
|
||||
DO(16,F0,K0);
|
||||
DO(17,F0,K0);
|
||||
DO(18,F0,K0);
|
||||
DO(19,F0,K0);
|
||||
|
||||
/* t=[20,39] */
|
||||
|
||||
DO(20,F1,K1);
|
||||
DO(21,F1,K1);
|
||||
DO(22,F1,K1);
|
||||
DO(23,F1,K1);
|
||||
DO(24,F1,K1);
|
||||
DO(25,F1,K1);
|
||||
DO(26,F1,K1);
|
||||
DO(27,F1,K1);
|
||||
DO(28,F1,K1);
|
||||
DO(29,F1,K1);
|
||||
DO(30,F1,K1);
|
||||
DO(31,F1,K1);
|
||||
DO(32,F1,K1);
|
||||
DO(33,F1,K1);
|
||||
DO(34,F1,K1);
|
||||
DO(35,F1,K1);
|
||||
DO(36,F1,K1);
|
||||
DO(37,F1,K1);
|
||||
DO(38,F1,K1);
|
||||
DO(39,F1,K1);
|
||||
|
||||
/* t=[40,59] */
|
||||
|
||||
DO(40,F2,K2);
|
||||
DO(41,F2,K2);
|
||||
DO(42,F2,K2);
|
||||
DO(43,F2,K2);
|
||||
DO(44,F2,K2);
|
||||
DO(45,F2,K2);
|
||||
DO(46,F2,K2);
|
||||
DO(47,F2,K2);
|
||||
DO(48,F2,K2);
|
||||
DO(49,F2,K2);
|
||||
DO(50,F2,K2);
|
||||
DO(51,F2,K2);
|
||||
DO(52,F2,K2);
|
||||
DO(53,F2,K2);
|
||||
DO(54,F2,K2);
|
||||
DO(55,F2,K2);
|
||||
DO(56,F2,K2);
|
||||
DO(57,F2,K2);
|
||||
DO(58,F2,K2);
|
||||
DO(59,F2,K2);
|
||||
|
||||
/* t=[60,79] */
|
||||
|
||||
DO(60,F3,K3);
|
||||
DO(61,F3,K3);
|
||||
DO(62,F3,K3);
|
||||
DO(63,F3,K3);
|
||||
DO(64,F3,K3);
|
||||
DO(65,F3,K3);
|
||||
DO(66,F3,K3);
|
||||
DO(67,F3,K3);
|
||||
DO(68,F3,K3);
|
||||
DO(69,F3,K3);
|
||||
DO(70,F3,K3);
|
||||
DO(71,F3,K3);
|
||||
DO(72,F3,K3);
|
||||
DO(73,F3,K3);
|
||||
DO(74,F3,K3);
|
||||
DO(75,F3,K3);
|
||||
DO(76,F3,K3);
|
||||
DO(77,F3,K3);
|
||||
DO(78,F3,K3);
|
||||
DO(79,F3,K3);
|
||||
|
||||
A += AA;
|
||||
B += BB;
|
||||
C += CC;
|
||||
D += DD;
|
||||
E += EE;
|
||||
}
|
||||
|
||||
/*
|
||||
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
|
||||
*/
|
||||
|
||||
#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
|
||||
static inline uint32_t
|
||||
swap_uint32_t (uint32_t t)
|
||||
{
|
||||
#define ROL(x,n) ((x)<<(n))|((x)>>(32-(n)))
|
||||
uint32_t temp1, temp2;
|
||||
|
||||
temp1 = cshift(t, 16);
|
||||
temp2 = temp1 >> 8;
|
||||
temp1 &= 0x00ff00ff;
|
||||
temp2 &= 0x00ff00ff;
|
||||
temp1 <<= 8;
|
||||
return temp1 | temp2;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct x32{
|
||||
unsigned int a:32;
|
||||
unsigned int b:32;
|
||||
};
|
||||
|
||||
void
|
||||
SHA1_Update (struct sha *m, const void *v, size_t len)
|
||||
{
|
||||
const unsigned char *p = v;
|
||||
size_t old_sz = m->sz[0];
|
||||
size_t offset;
|
||||
|
||||
m->sz[0] += len * 8;
|
||||
if (m->sz[0] < old_sz)
|
||||
++m->sz[1];
|
||||
offset = (old_sz / 8) % 64;
|
||||
while(len > 0){
|
||||
size_t l = min(len, 64 - offset);
|
||||
memcpy(m->save + offset, p, l);
|
||||
offset += l;
|
||||
p += l;
|
||||
len -= l;
|
||||
if(offset == 64){
|
||||
#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
|
||||
int i;
|
||||
uint32_t current[16];
|
||||
struct x32 *u = (struct x32*)m->save;
|
||||
for(i = 0; i < 8; i++){
|
||||
current[2*i+0] = swap_uint32_t(u[i].a);
|
||||
current[2*i+1] = swap_uint32_t(u[i].b);
|
||||
}
|
||||
calc(m, current);
|
||||
#else
|
||||
calc(m, (uint32_t*)m->save);
|
||||
#endif
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SHA1_Final (void *res, struct sha *m)
|
||||
{
|
||||
unsigned char zeros[72];
|
||||
unsigned offset = (m->sz[0] / 8) % 64;
|
||||
unsigned int dstart = (120 - offset - 1) % 64 + 1;
|
||||
|
||||
*zeros = 0x80;
|
||||
memset (zeros + 1, 0, sizeof(zeros) - 1);
|
||||
zeros[dstart+7] = (m->sz[0] >> 0) & 0xff;
|
||||
zeros[dstart+6] = (m->sz[0] >> 8) & 0xff;
|
||||
zeros[dstart+5] = (m->sz[0] >> 16) & 0xff;
|
||||
zeros[dstart+4] = (m->sz[0] >> 24) & 0xff;
|
||||
zeros[dstart+3] = (m->sz[1] >> 0) & 0xff;
|
||||
zeros[dstart+2] = (m->sz[1] >> 8) & 0xff;
|
||||
zeros[dstart+1] = (m->sz[1] >> 16) & 0xff;
|
||||
zeros[dstart+0] = (m->sz[1] >> 24) & 0xff;
|
||||
SHA1_Update (m, zeros, dstart + 8);
|
||||
{
|
||||
int i;
|
||||
unsigned char *r = (unsigned char*)res;
|
||||
|
||||
for (i = 0; i < 5; ++i) {
|
||||
r[4*i+3] = m->counter[i] & 0xFF;
|
||||
r[4*i+2] = (m->counter[i] >> 8) & 0xFF;
|
||||
r[4*i+1] = (m->counter[i] >> 16) & 0xFF;
|
||||
r[4*i] = (m->counter[i] >> 24) & 0xFF;
|
||||
}
|
||||
}
|
||||
#if 0
|
||||
{
|
||||
int i;
|
||||
uint32_t *r = (uint32_t *)res;
|
||||
|
||||
for (i = 0; i < 5; ++i)
|
||||
r[i] = swap_uint32_t (m->counter[i]);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: sha.h,v 1.11 2006/05/05 11:06:21 lha Exp $ */
|
||||
|
||||
#ifndef HEIM_SHA_H
|
||||
#define HEIM_SHA_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define SHA1_Init hc_SHA1_Init
|
||||
#define SHA1_Update hc_SHA1_Update
|
||||
#define SHA1_Final hc_SHA1_Final
|
||||
#define SHA256_Init hc_SHA256_Init
|
||||
#define SHA256_Update hc_SHA256_Update
|
||||
#define SHA256_Final hc_SHA256_Final
|
||||
|
||||
/*
|
||||
* SHA-1
|
||||
*/
|
||||
|
||||
#define SHA_DIGEST_LENGTH 20
|
||||
|
||||
struct sha {
|
||||
unsigned int sz[2];
|
||||
uint32_t counter[5];
|
||||
unsigned char save[64];
|
||||
};
|
||||
|
||||
typedef struct sha SHA_CTX;
|
||||
|
||||
void SHA1_Init (struct sha *m);
|
||||
void SHA1_Update (struct sha *m, const void *v, size_t len);
|
||||
void SHA1_Final (void *res, struct sha *m);
|
||||
|
||||
/*
|
||||
* SHA-2 256
|
||||
*/
|
||||
|
||||
#define SHA256_DIGEST_LENGTH 32
|
||||
|
||||
struct hc_sha256state {
|
||||
unsigned int sz[2];
|
||||
uint32_t counter[8];
|
||||
unsigned char save[64];
|
||||
};
|
||||
|
||||
typedef struct hc_sha256state SHA256_CTX;
|
||||
|
||||
void SHA256_Init (SHA256_CTX *);
|
||||
void SHA256_Update (SHA256_CTX *, const void *, size_t);
|
||||
void SHA256_Final (void *, SHA256_CTX *);
|
||||
|
||||
#endif /* HEIM_SHA_H */
|
||||
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
|
||||
RCSID("$Id: sha256.c,v 1.2 2006/05/05 10:25:37 lha Exp $");
|
||||
#endif
|
||||
|
||||
#include "hash.h"
|
||||
#include "sha.h"
|
||||
|
||||
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
|
||||
#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
||||
|
||||
#define ROTR(x,n) (((x)>>(n)) | ((x) << (32 - (n))))
|
||||
|
||||
#define Sigma0(x) (ROTR(x,2) ^ ROTR(x,13) ^ ROTR(x,22))
|
||||
#define Sigma1(x) (ROTR(x,6) ^ ROTR(x,11) ^ ROTR(x,25))
|
||||
#define sigma0(x) (ROTR(x,7) ^ ROTR(x,18) ^ ((x)>>3))
|
||||
#define sigma1(x) (ROTR(x,17) ^ ROTR(x,19) ^ ((x)>>10))
|
||||
|
||||
#define A m->counter[0]
|
||||
#define B m->counter[1]
|
||||
#define C m->counter[2]
|
||||
#define D m->counter[3]
|
||||
#define E m->counter[4]
|
||||
#define F m->counter[5]
|
||||
#define G m->counter[6]
|
||||
#define H m->counter[7]
|
||||
|
||||
static const uint32_t constant_256[64] = {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
||||
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
||||
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
||||
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
||||
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
|
||||
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
||||
};
|
||||
|
||||
void
|
||||
SHA256_Init (SHA256_CTX *m)
|
||||
{
|
||||
m->sz[0] = 0;
|
||||
m->sz[1] = 0;
|
||||
A = 0x6a09e667;
|
||||
B = 0xbb67ae85;
|
||||
C = 0x3c6ef372;
|
||||
D = 0xa54ff53a;
|
||||
E = 0x510e527f;
|
||||
F = 0x9b05688c;
|
||||
G = 0x1f83d9ab;
|
||||
H = 0x5be0cd19;
|
||||
}
|
||||
|
||||
static void
|
||||
calc (SHA256_CTX *m, uint32_t *in)
|
||||
{
|
||||
uint32_t AA, BB, CC, DD, EE, FF, GG, HH;
|
||||
uint32_t data[64];
|
||||
int i;
|
||||
|
||||
AA = A;
|
||||
BB = B;
|
||||
CC = C;
|
||||
DD = D;
|
||||
EE = E;
|
||||
FF = F;
|
||||
GG = G;
|
||||
HH = H;
|
||||
|
||||
for (i = 0; i < 16; ++i)
|
||||
data[i] = in[i];
|
||||
for (i = 16; i < 64; ++i)
|
||||
data[i] = sigma1(data[i-2]) + data[i-7] +
|
||||
sigma0(data[i-15]) + data[i - 16];
|
||||
|
||||
for (i = 0; i < 64; i++) {
|
||||
uint32_t T1, T2;
|
||||
|
||||
T1 = HH + Sigma1(EE) + Ch(EE, FF, GG) + constant_256[i] + data[i];
|
||||
T2 = Sigma0(AA) + Maj(AA,BB,CC);
|
||||
|
||||
HH = GG;
|
||||
GG = FF;
|
||||
FF = EE;
|
||||
EE = DD + T1;
|
||||
DD = CC;
|
||||
CC = BB;
|
||||
BB = AA;
|
||||
AA = T1 + T2;
|
||||
}
|
||||
|
||||
A += AA;
|
||||
B += BB;
|
||||
C += CC;
|
||||
D += DD;
|
||||
E += EE;
|
||||
F += FF;
|
||||
G += GG;
|
||||
H += HH;
|
||||
}
|
||||
|
||||
/*
|
||||
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
|
||||
*/
|
||||
|
||||
#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
|
||||
static inline uint32_t
|
||||
swap_uint32_t (uint32_t t)
|
||||
{
|
||||
#define ROL(x,n) ((x)<<(n))|((x)>>(32-(n)))
|
||||
uint32_t temp1, temp2;
|
||||
|
||||
temp1 = cshift(t, 16);
|
||||
temp2 = temp1 >> 8;
|
||||
temp1 &= 0x00ff00ff;
|
||||
temp2 &= 0x00ff00ff;
|
||||
temp1 <<= 8;
|
||||
return temp1 | temp2;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct x32{
|
||||
unsigned int a:32;
|
||||
unsigned int b:32;
|
||||
};
|
||||
|
||||
void
|
||||
SHA256_Update (SHA256_CTX *m, const void *v, size_t len)
|
||||
{
|
||||
const unsigned char *p = v;
|
||||
size_t old_sz = m->sz[0];
|
||||
size_t offset;
|
||||
|
||||
m->sz[0] += len * 8;
|
||||
if (m->sz[0] < old_sz)
|
||||
++m->sz[1];
|
||||
offset = (old_sz / 8) % 64;
|
||||
while(len > 0){
|
||||
size_t l = min(len, 64 - offset);
|
||||
memcpy(m->save + offset, p, l);
|
||||
offset += l;
|
||||
p += l;
|
||||
len -= l;
|
||||
if(offset == 64){
|
||||
#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
|
||||
int i;
|
||||
uint32_t current[16];
|
||||
struct x32 *u = (struct x32*)m->save;
|
||||
for(i = 0; i < 8; i++){
|
||||
current[2*i+0] = swap_uint32_t(u[i].a);
|
||||
current[2*i+1] = swap_uint32_t(u[i].b);
|
||||
}
|
||||
calc(m, current);
|
||||
#else
|
||||
calc(m, (uint32_t*)m->save);
|
||||
#endif
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SHA256_Final (void *res, SHA256_CTX *m)
|
||||
{
|
||||
unsigned char zeros[72];
|
||||
unsigned offset = (m->sz[0] / 8) % 64;
|
||||
unsigned int dstart = (120 - offset - 1) % 64 + 1;
|
||||
|
||||
*zeros = 0x80;
|
||||
memset (zeros + 1, 0, sizeof(zeros) - 1);
|
||||
zeros[dstart+7] = (m->sz[0] >> 0) & 0xff;
|
||||
zeros[dstart+6] = (m->sz[0] >> 8) & 0xff;
|
||||
zeros[dstart+5] = (m->sz[0] >> 16) & 0xff;
|
||||
zeros[dstart+4] = (m->sz[0] >> 24) & 0xff;
|
||||
zeros[dstart+3] = (m->sz[1] >> 0) & 0xff;
|
||||
zeros[dstart+2] = (m->sz[1] >> 8) & 0xff;
|
||||
zeros[dstart+1] = (m->sz[1] >> 16) & 0xff;
|
||||
zeros[dstart+0] = (m->sz[1] >> 24) & 0xff;
|
||||
SHA256_Update (m, zeros, dstart + 8);
|
||||
{
|
||||
int i;
|
||||
unsigned char *r = (unsigned char*)res;
|
||||
|
||||
for (i = 0; i < 8; ++i) {
|
||||
r[4*i+3] = m->counter[i] & 0xFF;
|
||||
r[4*i+2] = (m->counter[i] >> 8) & 0xFF;
|
||||
r[4*i+1] = (m->counter[i] >> 16) & 0xFF;
|
||||
r[4*i] = (m->counter[i] >> 24) & 0xFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Copyright (c) 1997 - 2000, 2005 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
RCSID("$Id: ui.c,v 1.6 2006/09/22 15:45:57 lha Exp $");
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <termios.h>
|
||||
#include <roken.h>
|
||||
|
||||
#include <ui.h>
|
||||
|
||||
static sig_atomic_t intr_flag;
|
||||
|
||||
static void
|
||||
intr(int sig)
|
||||
{
|
||||
intr_flag++;
|
||||
}
|
||||
|
||||
#ifndef NSIG
|
||||
#define NSIG 47
|
||||
#endif
|
||||
|
||||
static int
|
||||
read_string(const char *preprompt, const char *prompt,
|
||||
char *buf, size_t len, int echo)
|
||||
{
|
||||
struct sigaction sigs[NSIG];
|
||||
int oksigs[NSIG];
|
||||
struct sigaction sa;
|
||||
FILE *tty;
|
||||
int ret = 0;
|
||||
int of = 0;
|
||||
int i;
|
||||
int c;
|
||||
char *p;
|
||||
|
||||
struct termios t_new, t_old;
|
||||
|
||||
memset(&oksigs, 0, sizeof(oksigs));
|
||||
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_handler = intr;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_flags = 0;
|
||||
for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
|
||||
if (i != SIGALRM)
|
||||
if (sigaction(i, &sa, &sigs[i]) == 0)
|
||||
oksigs[i] = 1;
|
||||
|
||||
if((tty = fopen("/dev/tty", "r")) == NULL)
|
||||
tty = stdin;
|
||||
|
||||
fprintf(stderr, "%s%s", preprompt, prompt);
|
||||
fflush(stderr);
|
||||
|
||||
if(echo == 0){
|
||||
tcgetattr(fileno(tty), &t_old);
|
||||
memcpy(&t_new, &t_old, sizeof(t_new));
|
||||
t_new.c_lflag &= ~ECHO;
|
||||
tcsetattr(fileno(tty), TCSANOW, &t_new);
|
||||
}
|
||||
intr_flag = 0;
|
||||
p = buf;
|
||||
while(intr_flag == 0){
|
||||
c = getc(tty);
|
||||
if(c == EOF){
|
||||
if(!ferror(tty))
|
||||
ret = 1;
|
||||
break;
|
||||
}
|
||||
if(c == '\n')
|
||||
break;
|
||||
if(of == 0)
|
||||
*p++ = c;
|
||||
of = (p == buf + len);
|
||||
}
|
||||
if(of)
|
||||
p--;
|
||||
*p = 0;
|
||||
|
||||
if(echo == 0){
|
||||
printf("\n");
|
||||
tcsetattr(fileno(tty), TCSANOW, &t_old);
|
||||
}
|
||||
|
||||
if(tty != stdin)
|
||||
fclose(tty);
|
||||
|
||||
for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
|
||||
if (oksigs[i])
|
||||
sigaction(i, &sigs[i], NULL);
|
||||
|
||||
if(ret)
|
||||
return -3;
|
||||
if(intr_flag)
|
||||
return -2;
|
||||
if(of)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, int verify)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = read_string("", prompt, buf, length, 0);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
if (verify) {
|
||||
char *buf2;
|
||||
buf2 = malloc(length);
|
||||
if (buf2 == NULL)
|
||||
return 1;
|
||||
|
||||
ret = read_string("Verify password - ", prompt, buf2, length, 0);
|
||||
if (ret) {
|
||||
free(buf2);
|
||||
return ret;
|
||||
}
|
||||
if (strcmp(buf2, buf) != 0)
|
||||
ret = 1;
|
||||
free(buf2);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2005 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id: ui.h,v 1.1 2006/01/08 21:47:29 lha Exp $ */
|
||||
|
||||
#ifndef _HEIM_UI_H
|
||||
#define _HEIM_UI_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define UI_UTIL_read_pw_string hc_UI_UTIL_read_pw_string
|
||||
|
||||
int UI_UTIL_read_pw_string(char *, int, const char *, int); /* XXX */
|
||||
|
||||
#endif /* _HEIM_UI_H */
|
||||
|
||||
Reference in New Issue
Block a user