1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * BF low level APIs are deprecated for public use, but still ok for internal
12e1051a39Sopenharmony_ci * use.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#include "internal/deprecated.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <openssl/blowfish.h>
17e1051a39Sopenharmony_ci#include "bf_local.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci/*
20e1051a39Sopenharmony_ci * The input and output encrypted as though 64bit cfb mode is being used.
21e1051a39Sopenharmony_ci * The extra state information to record how much of the 64bit block we have
22e1051a39Sopenharmony_ci * used is contained in *num;
23e1051a39Sopenharmony_ci */
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_civoid BF_cfb64_encrypt(const unsigned char *in, unsigned char *out,
26e1051a39Sopenharmony_ci                      long length, const BF_KEY *schedule,
27e1051a39Sopenharmony_ci                      unsigned char *ivec, int *num, int encrypt)
28e1051a39Sopenharmony_ci{
29e1051a39Sopenharmony_ci    register BF_LONG v0, v1, t;
30e1051a39Sopenharmony_ci    register int n = *num;
31e1051a39Sopenharmony_ci    register long l = length;
32e1051a39Sopenharmony_ci    BF_LONG ti[2];
33e1051a39Sopenharmony_ci    unsigned char *iv, c, cc;
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ci    iv = (unsigned char *)ivec;
36e1051a39Sopenharmony_ci    if (encrypt) {
37e1051a39Sopenharmony_ci        while (l--) {
38e1051a39Sopenharmony_ci            if (n == 0) {
39e1051a39Sopenharmony_ci                n2l(iv, v0);
40e1051a39Sopenharmony_ci                ti[0] = v0;
41e1051a39Sopenharmony_ci                n2l(iv, v1);
42e1051a39Sopenharmony_ci                ti[1] = v1;
43e1051a39Sopenharmony_ci                BF_encrypt((BF_LONG *)ti, schedule);
44e1051a39Sopenharmony_ci                iv = (unsigned char *)ivec;
45e1051a39Sopenharmony_ci                t = ti[0];
46e1051a39Sopenharmony_ci                l2n(t, iv);
47e1051a39Sopenharmony_ci                t = ti[1];
48e1051a39Sopenharmony_ci                l2n(t, iv);
49e1051a39Sopenharmony_ci                iv = (unsigned char *)ivec;
50e1051a39Sopenharmony_ci            }
51e1051a39Sopenharmony_ci            c = *(in++) ^ iv[n];
52e1051a39Sopenharmony_ci            *(out++) = c;
53e1051a39Sopenharmony_ci            iv[n] = c;
54e1051a39Sopenharmony_ci            n = (n + 1) & 0x07;
55e1051a39Sopenharmony_ci        }
56e1051a39Sopenharmony_ci    } else {
57e1051a39Sopenharmony_ci        while (l--) {
58e1051a39Sopenharmony_ci            if (n == 0) {
59e1051a39Sopenharmony_ci                n2l(iv, v0);
60e1051a39Sopenharmony_ci                ti[0] = v0;
61e1051a39Sopenharmony_ci                n2l(iv, v1);
62e1051a39Sopenharmony_ci                ti[1] = v1;
63e1051a39Sopenharmony_ci                BF_encrypt((BF_LONG *)ti, schedule);
64e1051a39Sopenharmony_ci                iv = (unsigned char *)ivec;
65e1051a39Sopenharmony_ci                t = ti[0];
66e1051a39Sopenharmony_ci                l2n(t, iv);
67e1051a39Sopenharmony_ci                t = ti[1];
68e1051a39Sopenharmony_ci                l2n(t, iv);
69e1051a39Sopenharmony_ci                iv = (unsigned char *)ivec;
70e1051a39Sopenharmony_ci            }
71e1051a39Sopenharmony_ci            cc = *(in++);
72e1051a39Sopenharmony_ci            c = iv[n];
73e1051a39Sopenharmony_ci            iv[n] = cc;
74e1051a39Sopenharmony_ci            *(out++) = c ^ cc;
75e1051a39Sopenharmony_ci            n = (n + 1) & 0x07;
76e1051a39Sopenharmony_ci        }
77e1051a39Sopenharmony_ci    }
78e1051a39Sopenharmony_ci    v0 = v1 = ti[0] = ti[1] = t = c = cc = 0;
79e1051a39Sopenharmony_ci    *num = n;
80e1051a39Sopenharmony_ci}
81