11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
51cb0ef41Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at
71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html
81cb0ef41Sopenharmony_ci */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci/*
111cb0ef41Sopenharmony_ci * BF low level APIs are deprecated for public use, but still ok for internal
121cb0ef41Sopenharmony_ci * use.
131cb0ef41Sopenharmony_ci */
141cb0ef41Sopenharmony_ci#include "internal/deprecated.h"
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci#include <openssl/blowfish.h>
171cb0ef41Sopenharmony_ci#include "bf_local.h"
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci/*
201cb0ef41Sopenharmony_ci * Blowfish as implemented from 'Blowfish: Springer-Verlag paper' (From
211cb0ef41Sopenharmony_ci * LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, CAMBRIDGE
221cb0ef41Sopenharmony_ci * SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
231cb0ef41Sopenharmony_ci */
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
261cb0ef41Sopenharmony_ci# error If you set BF_ROUNDS to some value other than 16 or 20, you will have \
271cb0ef41Sopenharmony_cito modify the code.
281cb0ef41Sopenharmony_ci#endif
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_civoid BF_encrypt(BF_LONG *data, const BF_KEY *key)
311cb0ef41Sopenharmony_ci{
321cb0ef41Sopenharmony_ci    register BF_LONG l, r;
331cb0ef41Sopenharmony_ci    register const BF_LONG *p, *s;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    p = key->P;
361cb0ef41Sopenharmony_ci    s = &(key->S[0]);
371cb0ef41Sopenharmony_ci    l = data[0];
381cb0ef41Sopenharmony_ci    r = data[1];
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    l ^= p[0];
411cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[1]);
421cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[2]);
431cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[3]);
441cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[4]);
451cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[5]);
461cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[6]);
471cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[7]);
481cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[8]);
491cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[9]);
501cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[10]);
511cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[11]);
521cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[12]);
531cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[13]);
541cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[14]);
551cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[15]);
561cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[16]);
571cb0ef41Sopenharmony_ci# if BF_ROUNDS == 20
581cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[17]);
591cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[18]);
601cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[19]);
611cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[20]);
621cb0ef41Sopenharmony_ci# endif
631cb0ef41Sopenharmony_ci    r ^= p[BF_ROUNDS + 1];
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci    data[1] = l & 0xffffffffU;
661cb0ef41Sopenharmony_ci    data[0] = r & 0xffffffffU;
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_civoid BF_decrypt(BF_LONG *data, const BF_KEY *key)
701cb0ef41Sopenharmony_ci{
711cb0ef41Sopenharmony_ci    register BF_LONG l, r;
721cb0ef41Sopenharmony_ci    register const BF_LONG *p, *s;
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    p = key->P;
751cb0ef41Sopenharmony_ci    s = &(key->S[0]);
761cb0ef41Sopenharmony_ci    l = data[0];
771cb0ef41Sopenharmony_ci    r = data[1];
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci    l ^= p[BF_ROUNDS + 1];
801cb0ef41Sopenharmony_ci#  if BF_ROUNDS == 20
811cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[20]);
821cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[19]);
831cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[18]);
841cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[17]);
851cb0ef41Sopenharmony_ci#  endif
861cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[16]);
871cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[15]);
881cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[14]);
891cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[13]);
901cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[12]);
911cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[11]);
921cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[10]);
931cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[9]);
941cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[8]);
951cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[7]);
961cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[6]);
971cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[5]);
981cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[4]);
991cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[3]);
1001cb0ef41Sopenharmony_ci    BF_ENC(r, l, s, p[2]);
1011cb0ef41Sopenharmony_ci    BF_ENC(l, r, s, p[1]);
1021cb0ef41Sopenharmony_ci    r ^= p[0];
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci    data[1] = l & 0xffffffffU;
1051cb0ef41Sopenharmony_ci    data[0] = r & 0xffffffffU;
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_civoid BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
1091cb0ef41Sopenharmony_ci                    const BF_KEY *schedule, unsigned char *ivec, int encrypt)
1101cb0ef41Sopenharmony_ci{
1111cb0ef41Sopenharmony_ci    register BF_LONG tin0, tin1;
1121cb0ef41Sopenharmony_ci    register BF_LONG tout0, tout1, xor0, xor1;
1131cb0ef41Sopenharmony_ci    register long l = length;
1141cb0ef41Sopenharmony_ci    BF_LONG tin[2];
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci    if (encrypt) {
1171cb0ef41Sopenharmony_ci        n2l(ivec, tout0);
1181cb0ef41Sopenharmony_ci        n2l(ivec, tout1);
1191cb0ef41Sopenharmony_ci        ivec -= 8;
1201cb0ef41Sopenharmony_ci        for (l -= 8; l >= 0; l -= 8) {
1211cb0ef41Sopenharmony_ci            n2l(in, tin0);
1221cb0ef41Sopenharmony_ci            n2l(in, tin1);
1231cb0ef41Sopenharmony_ci            tin0 ^= tout0;
1241cb0ef41Sopenharmony_ci            tin1 ^= tout1;
1251cb0ef41Sopenharmony_ci            tin[0] = tin0;
1261cb0ef41Sopenharmony_ci            tin[1] = tin1;
1271cb0ef41Sopenharmony_ci            BF_encrypt(tin, schedule);
1281cb0ef41Sopenharmony_ci            tout0 = tin[0];
1291cb0ef41Sopenharmony_ci            tout1 = tin[1];
1301cb0ef41Sopenharmony_ci            l2n(tout0, out);
1311cb0ef41Sopenharmony_ci            l2n(tout1, out);
1321cb0ef41Sopenharmony_ci        }
1331cb0ef41Sopenharmony_ci        if (l != -8) {
1341cb0ef41Sopenharmony_ci            n2ln(in, tin0, tin1, l + 8);
1351cb0ef41Sopenharmony_ci            tin0 ^= tout0;
1361cb0ef41Sopenharmony_ci            tin1 ^= tout1;
1371cb0ef41Sopenharmony_ci            tin[0] = tin0;
1381cb0ef41Sopenharmony_ci            tin[1] = tin1;
1391cb0ef41Sopenharmony_ci            BF_encrypt(tin, schedule);
1401cb0ef41Sopenharmony_ci            tout0 = tin[0];
1411cb0ef41Sopenharmony_ci            tout1 = tin[1];
1421cb0ef41Sopenharmony_ci            l2n(tout0, out);
1431cb0ef41Sopenharmony_ci            l2n(tout1, out);
1441cb0ef41Sopenharmony_ci        }
1451cb0ef41Sopenharmony_ci        l2n(tout0, ivec);
1461cb0ef41Sopenharmony_ci        l2n(tout1, ivec);
1471cb0ef41Sopenharmony_ci    } else {
1481cb0ef41Sopenharmony_ci        n2l(ivec, xor0);
1491cb0ef41Sopenharmony_ci        n2l(ivec, xor1);
1501cb0ef41Sopenharmony_ci        ivec -= 8;
1511cb0ef41Sopenharmony_ci        for (l -= 8; l >= 0; l -= 8) {
1521cb0ef41Sopenharmony_ci            n2l(in, tin0);
1531cb0ef41Sopenharmony_ci            n2l(in, tin1);
1541cb0ef41Sopenharmony_ci            tin[0] = tin0;
1551cb0ef41Sopenharmony_ci            tin[1] = tin1;
1561cb0ef41Sopenharmony_ci            BF_decrypt(tin, schedule);
1571cb0ef41Sopenharmony_ci            tout0 = tin[0] ^ xor0;
1581cb0ef41Sopenharmony_ci            tout1 = tin[1] ^ xor1;
1591cb0ef41Sopenharmony_ci            l2n(tout0, out);
1601cb0ef41Sopenharmony_ci            l2n(tout1, out);
1611cb0ef41Sopenharmony_ci            xor0 = tin0;
1621cb0ef41Sopenharmony_ci            xor1 = tin1;
1631cb0ef41Sopenharmony_ci        }
1641cb0ef41Sopenharmony_ci        if (l != -8) {
1651cb0ef41Sopenharmony_ci            n2l(in, tin0);
1661cb0ef41Sopenharmony_ci            n2l(in, tin1);
1671cb0ef41Sopenharmony_ci            tin[0] = tin0;
1681cb0ef41Sopenharmony_ci            tin[1] = tin1;
1691cb0ef41Sopenharmony_ci            BF_decrypt(tin, schedule);
1701cb0ef41Sopenharmony_ci            tout0 = tin[0] ^ xor0;
1711cb0ef41Sopenharmony_ci            tout1 = tin[1] ^ xor1;
1721cb0ef41Sopenharmony_ci            l2nn(tout0, tout1, out, l + 8);
1731cb0ef41Sopenharmony_ci            xor0 = tin0;
1741cb0ef41Sopenharmony_ci            xor1 = tin1;
1751cb0ef41Sopenharmony_ci        }
1761cb0ef41Sopenharmony_ci        l2n(xor0, ivec);
1771cb0ef41Sopenharmony_ci        l2n(xor1, ivec);
1781cb0ef41Sopenharmony_ci    }
1791cb0ef41Sopenharmony_ci    tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
1801cb0ef41Sopenharmony_ci    tin[0] = tin[1] = 0;
1811cb0ef41Sopenharmony_ci}
182