| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/modes/cfb128.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* | - | ||||||||||||
| 2 | * Copyright 2008-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 <openssl/crypto.h> | - | ||||||||||||
| 11 | #include "modes_lcl.h" | - | ||||||||||||
| 12 | #include <string.h> | - | ||||||||||||
| 13 | - | |||||||||||||
| 14 | /* | - | ||||||||||||
| 15 | * The input and output encrypted as though 128bit cfb mode is being used. | - | ||||||||||||
| 16 | * The extra state information to record how much of the 128bit block we have | - | ||||||||||||
| 17 | * used is contained in *num; | - | ||||||||||||
| 18 | */ | - | ||||||||||||
| 19 | void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out, | - | ||||||||||||
| 20 | size_t len, const void *key, | - | ||||||||||||
| 21 | unsigned char ivec[16], int *num, | - | ||||||||||||
| 22 | int enc, block128_f block) | - | ||||||||||||
| 23 | { | - | ||||||||||||
| 24 | unsigned int n; | - | ||||||||||||
| 25 | size_t l = 0; | - | ||||||||||||
| 26 | - | |||||||||||||
| 27 | n = *num; | - | ||||||||||||
| 28 | - | |||||||||||||
| 29 | if (enc) {
| 10027-10087 | ||||||||||||
| 30 | #if !defined(OPENSSL_SMALL_FOOTPRINT) | - | ||||||||||||
| 31 | if (16 % sizeof(size_t) == 0) { /* always true actually */
| 0-10087 | ||||||||||||
| 32 | do { | - | ||||||||||||
| 33 | while (n && len) {
| 154-35028 | ||||||||||||
| 34 | *(out++) = ivec[n] ^= *(in++); | - | ||||||||||||
| 35 | --len; | - | ||||||||||||
| 36 | n = (n + 1) % 16; | - | ||||||||||||
| 37 | } executed 34874 times by 1 test: end of blockExecuted by:
| 34874 | ||||||||||||
| 38 | # if defined(STRICT_ALIGNMENT) | - | ||||||||||||
| 39 | if (((size_t)in | (size_t)out | (size_t)ivec) % | - | ||||||||||||
| 40 | sizeof(size_t) != 0) | - | ||||||||||||
| 41 | break; | - | ||||||||||||
| 42 | # endif | - | ||||||||||||
| 43 | while (len >= 16) {
| 10087-195632 | ||||||||||||
| 44 | (*block) (ivec, ivec, key); | - | ||||||||||||
| 45 | for (; n < 16; n += sizeof(size_t)) {
| 195632-391264 | ||||||||||||
| 46 | *(size_t *)(out + n) = | - | ||||||||||||
| 47 | *(size_t *)(ivec + n) ^= *(size_t *)(in + n); | - | ||||||||||||
| 48 | } executed 391264 times by 1 test: end of blockExecuted by:
| 391264 | ||||||||||||
| 49 | len -= 16; | - | ||||||||||||
| 50 | out += 16; | - | ||||||||||||
| 51 | in += 16; | - | ||||||||||||
| 52 | n = 0; | - | ||||||||||||
| 53 | } executed 195632 times by 1 test: end of blockExecuted by:
| 195632 | ||||||||||||
| 54 | if (len) {
| 4223-5864 | ||||||||||||
| 55 | (*block) (ivec, ivec, key); | - | ||||||||||||
| 56 | while (len--) {
| 4223-32596 | ||||||||||||
| 57 | out[n] = ivec[n] ^= in[n]; | - | ||||||||||||
| 58 | ++n; | - | ||||||||||||
| 59 | } executed 32596 times by 1 test: end of blockExecuted by:
| 32596 | ||||||||||||
| 60 | } executed 4223 times by 1 test: end of blockExecuted by:
| 4223 | ||||||||||||
| 61 | *num = n; | - | ||||||||||||
| 62 | return; executed 10087 times by 1 test: return;Executed by:
| 10087 | ||||||||||||
| 63 | } while (0); | - | ||||||||||||
| 64 | } never executed: end of block | 0 | ||||||||||||
| 65 | /* the rest would be commonly eliminated by x86* compiler */ | - | ||||||||||||
| 66 | #endif | - | ||||||||||||
| 67 | while (l < len) {
| 0 | ||||||||||||
| 68 | if (n == 0) {
| 0 | ||||||||||||
| 69 | (*block) (ivec, ivec, key); | - | ||||||||||||
| 70 | } never executed: end of block | 0 | ||||||||||||
| 71 | out[l] = ivec[n] ^= in[l]; | - | ||||||||||||
| 72 | ++l; | - | ||||||||||||
| 73 | n = (n + 1) % 16; | - | ||||||||||||
| 74 | } never executed: end of block | 0 | ||||||||||||
| 75 | *num = n; | - | ||||||||||||
| 76 | } else { never executed: end of block | 0 | ||||||||||||
| 77 | #if !defined(OPENSSL_SMALL_FOOTPRINT) | - | ||||||||||||
| 78 | if (16 % sizeof(size_t) == 0) { /* always true actually */
| 0-10027 | ||||||||||||
| 79 | do { | - | ||||||||||||
| 80 | while (n && len) {
| 144-34378 | ||||||||||||
| 81 | unsigned char c; | - | ||||||||||||
| 82 | *(out++) = ivec[n] ^ (c = *(in++)); | - | ||||||||||||
| 83 | ivec[n] = c; | - | ||||||||||||
| 84 | --len; | - | ||||||||||||
| 85 | n = (n + 1) % 16; | - | ||||||||||||
| 86 | } executed 34234 times by 1 test: end of blockExecuted by:
| 34234 | ||||||||||||
| 87 | # if defined(STRICT_ALIGNMENT) | - | ||||||||||||
| 88 | if (((size_t)in | (size_t)out | (size_t)ivec) % | - | ||||||||||||
| 89 | sizeof(size_t) != 0) | - | ||||||||||||
| 90 | break; | - | ||||||||||||
| 91 | # endif | - | ||||||||||||
| 92 | while (len >= 16) {
| 10027-195672 | ||||||||||||
| 93 | (*block) (ivec, ivec, key); | - | ||||||||||||
| 94 | for (; n < 16; n += sizeof(size_t)) {
| 195672-391344 | ||||||||||||
| 95 | size_t t = *(size_t *)(in + n); | - | ||||||||||||
| 96 | *(size_t *)(out + n) = *(size_t *)(ivec + n) ^ t; | - | ||||||||||||
| 97 | *(size_t *)(ivec + n) = t; | - | ||||||||||||
| 98 | } executed 391344 times by 1 test: end of blockExecuted by:
| 391344 | ||||||||||||
| 99 | len -= 16; | - | ||||||||||||
| 100 | out += 16; | - | ||||||||||||
| 101 | in += 16; | - | ||||||||||||
| 102 | n = 0; | - | ||||||||||||
| 103 | } executed 195672 times by 1 test: end of blockExecuted by:
| 195672 | ||||||||||||
| 104 | if (len) {
| 4183-5844 | ||||||||||||
| 105 | (*block) (ivec, ivec, key); | - | ||||||||||||
| 106 | while (len--) {
| 4183-32596 | ||||||||||||
| 107 | unsigned char c; | - | ||||||||||||
| 108 | out[n] = ivec[n] ^ (c = in[n]); | - | ||||||||||||
| 109 | ivec[n] = c; | - | ||||||||||||
| 110 | ++n; | - | ||||||||||||
| 111 | } executed 32596 times by 1 test: end of blockExecuted by:
| 32596 | ||||||||||||
| 112 | } executed 4183 times by 1 test: end of blockExecuted by:
| 4183 | ||||||||||||
| 113 | *num = n; | - | ||||||||||||
| 114 | return; executed 10027 times by 1 test: return;Executed by:
| 10027 | ||||||||||||
| 115 | } while (0); | - | ||||||||||||
| 116 | } never executed: end of block | 0 | ||||||||||||
| 117 | /* the rest would be commonly eliminated by x86* compiler */ | - | ||||||||||||
| 118 | #endif | - | ||||||||||||
| 119 | while (l < len) {
| 0 | ||||||||||||
| 120 | unsigned char c; | - | ||||||||||||
| 121 | if (n == 0) {
| 0 | ||||||||||||
| 122 | (*block) (ivec, ivec, key); | - | ||||||||||||
| 123 | } never executed: end of block | 0 | ||||||||||||
| 124 | out[l] = ivec[n] ^ (c = in[l]); | - | ||||||||||||
| 125 | ivec[n] = c; | - | ||||||||||||
| 126 | ++l; | - | ||||||||||||
| 127 | n = (n + 1) % 16; | - | ||||||||||||
| 128 | } never executed: end of block | 0 | ||||||||||||
| 129 | *num = n; | - | ||||||||||||
| 130 | } never executed: end of block | 0 | ||||||||||||
| 131 | } | - | ||||||||||||
| 132 | - | |||||||||||||
| 133 | /* | - | ||||||||||||
| 134 | * This expects a single block of size nbits for both in and out. Note that | - | ||||||||||||
| 135 | * it corrupts any extra bits in the last byte of out | - | ||||||||||||
| 136 | */ | - | ||||||||||||
| 137 | static void cfbr_encrypt_block(const unsigned char *in, unsigned char *out, | - | ||||||||||||
| 138 | int nbits, const void *key, | - | ||||||||||||
| 139 | unsigned char ivec[16], int enc, | - | ||||||||||||
| 140 | block128_f block) | - | ||||||||||||
| 141 | { | - | ||||||||||||
| 142 | int n, rem, num; | - | ||||||||||||
| 143 | unsigned char ovec[16 * 2 + 1]; /* +1 because we dereference (but don't | - | ||||||||||||
| 144 | * use) one byte off the end */ | - | ||||||||||||
| 145 | - | |||||||||||||
| 146 | if (nbits <= 0 || nbits > 128)
| 0-562212 | ||||||||||||
| 147 | return; never executed: return; | 0 | ||||||||||||
| 148 | - | |||||||||||||
| 149 | /* fill in the first half of the new IV with the current IV */ | - | ||||||||||||
| 150 | memcpy(ovec, ivec, 16); | - | ||||||||||||
| 151 | /* construct the new IV */ | - | ||||||||||||
| 152 | (*block) (ivec, ivec, key); | - | ||||||||||||
| 153 | num = (nbits + 7) / 8; | - | ||||||||||||
| 154 | if (enc) /* encrypt the input */
| 281106 | ||||||||||||
| 155 | for (n = 0; n < num; ++n)
| 281106 | ||||||||||||
| 156 | out[n] = (ovec[16 + n] = in[n] ^ ivec[n]); executed 281106 times by 1 test: out[n] = (ovec[16 + n] = in[n] ^ ivec[n]);Executed by:
| 281106 | ||||||||||||
| 157 | else /* decrypt the input */ | - | ||||||||||||
| 158 | for (n = 0; n < num; ++n)
| 281106 | ||||||||||||
| 159 | out[n] = (ovec[16 + n] = in[n]) ^ ivec[n]; executed 281106 times by 1 test: out[n] = (ovec[16 + n] = in[n]) ^ ivec[n];Executed by:
| 281106 | ||||||||||||
| 160 | /* shift ovec left... */ | - | ||||||||||||
| 161 | rem = nbits % 8; | - | ||||||||||||
| 162 | num = nbits / 8; | - | ||||||||||||
| 163 | if (rem == 0)
| 72708-489504 | ||||||||||||
| 164 | memcpy(ivec, ovec + num, 16); executed 72708 times by 1 test: memcpy(ivec, ovec + num, 16);Executed by:
| 72708 | ||||||||||||
| 165 | else | - | ||||||||||||
| 166 | for (n = 0; n < 16; ++n)
| 489504-7832064 | ||||||||||||
| 167 | ivec[n] = ovec[n + num] << rem | ovec[n + num + 1] >> (8 - rem); executed 7832064 times by 1 test: ivec[n] = ovec[n + num] << rem | ovec[n + num + 1] >> (8 - rem);Executed by:
| 7832064 | ||||||||||||
| 168 | - | |||||||||||||
| 169 | /* it is not necessary to cleanse ovec, since the IV is not secret */ | - | ||||||||||||
| 170 | } executed 562212 times by 1 test: end of blockExecuted by:
| 562212 | ||||||||||||
| 171 | - | |||||||||||||
| 172 | /* N.B. This expects the input to be packed, MS bit first */ | - | ||||||||||||
| 173 | void CRYPTO_cfb128_1_encrypt(const unsigned char *in, unsigned char *out, | - | ||||||||||||
| 174 | size_t bits, const void *key, | - | ||||||||||||
| 175 | unsigned char ivec[16], int *num, | - | ||||||||||||
| 176 | int enc, block128_f block) | - | ||||||||||||
| 177 | { | - | ||||||||||||
| 178 | size_t n; | - | ||||||||||||
| 179 | unsigned char c[1], d[1]; | - | ||||||||||||
| 180 | - | |||||||||||||
| 181 | for (n = 0; n < bits; ++n) {
| 222-489504 | ||||||||||||
| 182 | c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
| 224519-264985 | ||||||||||||
| 183 | cfbr_encrypt_block(c, d, 1, key, ivec, enc, block); | - | ||||||||||||
| 184 | out[n / 8] = (out[n / 8] & ~(1 << (unsigned int)(7 - n % 8))) | | - | ||||||||||||
| 185 | ((d[0] & 0x80) >> (unsigned int)(n % 8)); | - | ||||||||||||
| 186 | } executed 489504 times by 1 test: end of blockExecuted by:
| 489504 | ||||||||||||
| 187 | } executed 222 times by 1 test: end of blockExecuted by:
| 222 | ||||||||||||
| 188 | - | |||||||||||||
| 189 | void CRYPTO_cfb128_8_encrypt(const unsigned char *in, unsigned char *out, | - | ||||||||||||
| 190 | size_t length, const void *key, | - | ||||||||||||
| 191 | unsigned char ivec[16], int *num, | - | ||||||||||||
| 192 | int enc, block128_f block) | - | ||||||||||||
| 193 | { | - | ||||||||||||
| 194 | size_t n; | - | ||||||||||||
| 195 | - | |||||||||||||
| 196 | for (n = 0; n < length; ++n)
| 366-72708 | ||||||||||||
| 197 | cfbr_encrypt_block(&in[n], &out[n], 8, key, ivec, enc, block); executed 72708 times by 1 test: cfbr_encrypt_block(&in[n], &out[n], 8, key, ivec, enc, block);Executed by:
| 72708 | ||||||||||||
| 198 | } executed 366 times by 1 test: end of blockExecuted by:
| 366 | ||||||||||||
| Source code | Switch to Preprocessed file |