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