xref: /freebsd/crypto/openssl/crypto/bf/bf_skey.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /*
11  * BF low level APIs are deprecated for public use, but still ok for internal
12  * use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <openssl/blowfish.h>
19 #include "bf_local.h"
20 #include "bf_pi.h"
21 
22 void BF_set_key(BF_KEY *key, int len, const unsigned char *data)
23 {
24     int i;
25     BF_LONG *p, ri, in[2];
26     const unsigned char *d, *end;
27 
28     memcpy(key, &bf_init, sizeof(BF_KEY));
29     p = key->P;
30 
31     if (len > ((BF_ROUNDS + 2) * 4))
32         len = (BF_ROUNDS + 2) * 4;
33 
34     d = data;
35     end = &(data[len]);
36     for (i = 0; i < (BF_ROUNDS + 2); i++) {
37         ri = *(d++);
38         if (d >= end)
39             d = data;
40 
41         ri <<= 8;
42         ri |= *(d++);
43         if (d >= end)
44             d = data;
45 
46         ri <<= 8;
47         ri |= *(d++);
48         if (d >= end)
49             d = data;
50 
51         ri <<= 8;
52         ri |= *(d++);
53         if (d >= end)
54             d = data;
55 
56         p[i] ^= ri;
57     }
58 
59     in[0] = 0L;
60     in[1] = 0L;
61     for (i = 0; i < (BF_ROUNDS + 2); i += 2) {
62         BF_encrypt(in, key);
63         p[i] = in[0];
64         p[i + 1] = in[1];
65     }
66 
67     p = key->S;
68     for (i = 0; i < 4 * 256; i += 2) {
69         BF_encrypt(in, key);
70         p[i] = in[0];
71         p[i + 1] = in[1];
72     }
73 }
74