2016-05-18 02:52:22 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:19:23 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2020-01-02 22:25:27 +08:00
|
|
|
/*
|
|
|
|
* BF low level APIs are deprecated for public use, but still ok for internal
|
|
|
|
* use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
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>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "bf_local.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
#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)
|
1998-12-21 18:52:47 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BF_LONG *p, ri, in[2];
|
2000-02-11 17:47:18 +08:00
|
|
|
const unsigned char *d, *end;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
|
|
|
memcpy(key, &bf_init, sizeof(BF_KEY));
|
2015-01-22 11:40:55 +08:00
|
|
|
p = key->P;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
|
|
|
if (len > ((BF_ROUNDS + 2) * 4))
|
|
|
|
len = (BF_ROUNDS + 2) * 4;
|
|
|
|
|
|
|
|
d = data;
|
|
|
|
end = &(data[len]);
|
|
|
|
for (i = 0; i < (BF_ROUNDS + 2); i++) {
|
|
|
|
ri = *(d++);
|
|
|
|
if (d >= end)
|
|
|
|
d = data;
|
|
|
|
|
|
|
|
ri <<= 8;
|
|
|
|
ri |= *(d++);
|
|
|
|
if (d >= end)
|
|
|
|
d = data;
|
|
|
|
|
|
|
|
ri <<= 8;
|
|
|
|
ri |= *(d++);
|
|
|
|
if (d >= end)
|
|
|
|
d = data;
|
|
|
|
|
|
|
|
ri <<= 8;
|
|
|
|
ri |= *(d++);
|
|
|
|
if (d >= end)
|
2015-01-22 11:40:55 +08:00
|
|
|
d = data;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
|
|
|
p[i] ^= ri;
|
|
|
|
}
|
|
|
|
|
|
|
|
in[0] = 0L;
|
2015-01-22 11:40:55 +08:00
|
|
|
in[1] = 0L;
|
1998-12-21 18:52:47 +08:00
|
|
|
for (i = 0; i < (BF_ROUNDS + 2); i += 2) {
|
1998-12-21 18:56:39 +08:00
|
|
|
BF_encrypt(in, key);
|
1998-12-21 18:52:47 +08:00
|
|
|
p[i] = in[0];
|
|
|
|
p[i + 1] = in[1];
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
p = key->S;
|
1998-12-21 18:52:47 +08:00
|
|
|
for (i = 0; i < 4 * 256; i += 2) {
|
1998-12-21 18:56:39 +08:00
|
|
|
BF_encrypt(in, key);
|
2015-01-22 11:40:55 +08:00
|
|
|
p[i] = in[0];
|
1998-12-21 18:52:47 +08:00
|
|
|
p[i + 1] = in[1];
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|