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 * DES 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 "des_local.h" 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_ci/* 19e1051a39Sopenharmony_ci * The input and output are loaded in multiples of 8 bits. What this means is 20e1051a39Sopenharmony_ci * that if you have numbits=12 and length=2 the first 12 bits will be 21e1051a39Sopenharmony_ci * retrieved from the first byte and half the second. The second 12 bits 22e1051a39Sopenharmony_ci * will come from the 3rd and half the 4th byte. 23e1051a39Sopenharmony_ci */ 24e1051a39Sopenharmony_civoid DES_ofb_encrypt(const unsigned char *in, unsigned char *out, int numbits, 25e1051a39Sopenharmony_ci long length, DES_key_schedule *schedule, 26e1051a39Sopenharmony_ci DES_cblock *ivec) 27e1051a39Sopenharmony_ci{ 28e1051a39Sopenharmony_ci register DES_LONG d0, d1, vv0, vv1, v0, v1, n = (numbits + 7) / 8; 29e1051a39Sopenharmony_ci register DES_LONG mask0, mask1; 30e1051a39Sopenharmony_ci register long l = length; 31e1051a39Sopenharmony_ci register int num = numbits; 32e1051a39Sopenharmony_ci DES_LONG ti[2]; 33e1051a39Sopenharmony_ci unsigned char *iv; 34e1051a39Sopenharmony_ci 35e1051a39Sopenharmony_ci if (num > 64) 36e1051a39Sopenharmony_ci return; 37e1051a39Sopenharmony_ci if (num > 32) { 38e1051a39Sopenharmony_ci mask0 = 0xffffffffL; 39e1051a39Sopenharmony_ci if (num >= 64) 40e1051a39Sopenharmony_ci mask1 = mask0; 41e1051a39Sopenharmony_ci else 42e1051a39Sopenharmony_ci mask1 = (1L << (num - 32)) - 1; 43e1051a39Sopenharmony_ci } else { 44e1051a39Sopenharmony_ci if (num == 32) 45e1051a39Sopenharmony_ci mask0 = 0xffffffffL; 46e1051a39Sopenharmony_ci else 47e1051a39Sopenharmony_ci mask0 = (1L << num) - 1; 48e1051a39Sopenharmony_ci mask1 = 0x00000000L; 49e1051a39Sopenharmony_ci } 50e1051a39Sopenharmony_ci 51e1051a39Sopenharmony_ci iv = &(*ivec)[0]; 52e1051a39Sopenharmony_ci c2l(iv, v0); 53e1051a39Sopenharmony_ci c2l(iv, v1); 54e1051a39Sopenharmony_ci ti[0] = v0; 55e1051a39Sopenharmony_ci ti[1] = v1; 56e1051a39Sopenharmony_ci while (l-- > 0) { 57e1051a39Sopenharmony_ci ti[0] = v0; 58e1051a39Sopenharmony_ci ti[1] = v1; 59e1051a39Sopenharmony_ci DES_encrypt1((DES_LONG *)ti, schedule, DES_ENCRYPT); 60e1051a39Sopenharmony_ci vv0 = ti[0]; 61e1051a39Sopenharmony_ci vv1 = ti[1]; 62e1051a39Sopenharmony_ci c2ln(in, d0, d1, n); 63e1051a39Sopenharmony_ci in += n; 64e1051a39Sopenharmony_ci d0 = (d0 ^ vv0) & mask0; 65e1051a39Sopenharmony_ci d1 = (d1 ^ vv1) & mask1; 66e1051a39Sopenharmony_ci l2cn(d0, d1, out, n); 67e1051a39Sopenharmony_ci out += n; 68e1051a39Sopenharmony_ci 69e1051a39Sopenharmony_ci if (num == 32) { 70e1051a39Sopenharmony_ci v0 = v1; 71e1051a39Sopenharmony_ci v1 = vv0; 72e1051a39Sopenharmony_ci } else if (num == 64) { 73e1051a39Sopenharmony_ci v0 = vv0; 74e1051a39Sopenharmony_ci v1 = vv1; 75e1051a39Sopenharmony_ci } else if (num > 32) { /* && num != 64 */ 76e1051a39Sopenharmony_ci v0 = ((v1 >> (num - 32)) | (vv0 << (64 - num))) & 0xffffffffL; 77e1051a39Sopenharmony_ci v1 = ((vv0 >> (num - 32)) | (vv1 << (64 - num))) & 0xffffffffL; 78e1051a39Sopenharmony_ci } else { /* num < 32 */ 79e1051a39Sopenharmony_ci 80e1051a39Sopenharmony_ci v0 = ((v0 >> num) | (v1 << (32 - num))) & 0xffffffffL; 81e1051a39Sopenharmony_ci v1 = ((v1 >> num) | (vv0 << (32 - num))) & 0xffffffffL; 82e1051a39Sopenharmony_ci } 83e1051a39Sopenharmony_ci } 84e1051a39Sopenharmony_ci iv = &(*ivec)[0]; 85e1051a39Sopenharmony_ci l2c(v0, iv); 86e1051a39Sopenharmony_ci l2c(v1, iv); 87e1051a39Sopenharmony_ci v0 = v1 = d0 = d1 = ti[0] = ti[1] = vv0 = vv1 = 0; 88e1051a39Sopenharmony_ci} 89