2016-05-18 02:52:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2016-05-18 02:52:22 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/blowfish.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
#include "bf_locl.h"
|
|
|
|
#include "bf_pi.h"
|
|
|
|
|
2008-11-12 11:58:08 +08:00
|
|
|
void BF_set_key(BF_KEY *key, int len, const unsigned char *data)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BF_LONG *p, ri, in[2];
|
|
|
|
const unsigned char *d, *end;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
memcpy(key, &bf_init, sizeof(BF_KEY));
|
|
|
|
p = key->P;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len > ((BF_ROUNDS + 2) * 4))
|
|
|
|
len = (BF_ROUNDS + 2) * 4;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
d = data;
|
|
|
|
end = &(data[len]);
|
|
|
|
for (i = 0; i < (BF_ROUNDS + 2); i++) {
|
|
|
|
ri = *(d++);
|
|
|
|
if (d >= end)
|
|
|
|
d = data;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ri <<= 8;
|
|
|
|
ri |= *(d++);
|
|
|
|
if (d >= end)
|
|
|
|
d = data;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ri <<= 8;
|
|
|
|
ri |= *(d++);
|
|
|
|
if (d >= end)
|
|
|
|
d = data;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ri <<= 8;
|
|
|
|
ri |= *(d++);
|
|
|
|
if (d >= end)
|
|
|
|
d = data;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
p[i] ^= ri;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
in[0] = 0L;
|
|
|
|
in[1] = 0L;
|
|
|
|
for (i = 0; i < (BF_ROUNDS + 2); i += 2) {
|
|
|
|
BF_encrypt(in, key);
|
|
|
|
p[i] = in[0];
|
|
|
|
p[i + 1] = in[1];
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
p = key->S;
|
|
|
|
for (i = 0; i < 4 * 256; i += 2) {
|
|
|
|
BF_encrypt(in, key);
|
|
|
|
p[i] = in[0];
|
|
|
|
p[i + 1] = in[1];
|
|
|
|
}
|
|
|
|
}
|