| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "common.h" |
| 33 |
|
| 34 |
FAKE_INIT(raw_ofb) |
| 35 |
|
| 36 |
#include "block_base.h" |
| 37 |
|
| 38 |
#define ERR_OFB_IV_LEN ((3 << 16) | 1) |
| 39 |
|
| 40 |
#define MAX_BLOCK_LEN 16 |
| 41 |
|
| 42 |
typedef struct { |
| 43 |
BlockBase *cipher; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
size_t usedKeyStream; |
| 49 |
|
| 50 |
uint8_t keyStream[MAX_BLOCK_LEN]; |
| 51 |
} OfbModeState; |
| 52 |
|
| 53 |
EXPORT_SYM int OFB_start_operation(BlockBase *cipher, |
| 54 |
const uint8_t iv[], |
| 55 |
size_t iv_len, |
| 56 |
OfbModeState **pResult) |
| 57 |
{ |
| 58 |
if ((NULL == cipher) || (NULL == iv) || (NULL == pResult)) |
| 59 |
return ERR_NULL; |
| 60 |
|
| 61 |
if (cipher->block_len > MAX_BLOCK_LEN) |
| 62 |
return ERR_BLOCK_SIZE; |
| 63 |
|
| 64 |
if (cipher->block_len != iv_len) |
| 65 |
return ERR_OFB_IV_LEN; |
| 66 |
|
| 67 |
*pResult = calloc(1, sizeof(OfbModeState)); |
| 68 |
if (NULL == *pResult) |
| 69 |
return ERR_MEMORY; |
| 70 |
|
| 71 |
(*pResult)->cipher = cipher; |
| 72 |
(*pResult)->usedKeyStream = cipher->block_len; |
| 73 |
memcpy((*pResult)->keyStream, iv, iv_len); |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
EXPORT_SYM int OFB_encrypt(OfbModeState *ofbState, |
| 79 |
const uint8_t *in, |
| 80 |
uint8_t *out, |
| 81 |
size_t data_len) |
| 82 |
{ |
| 83 |
size_t block_len; |
| 84 |
uint8_t oldKeyStream[MAX_BLOCK_LEN]; |
| 85 |
|
| 86 |
if ((NULL == ofbState) || (NULL == in) || (NULL == out)) |
| 87 |
return ERR_NULL; |
| 88 |
|
| 89 |
block_len = ofbState->cipher->block_len; |
| 90 |
if (block_len > MAX_BLOCK_LEN) |
| 91 |
return ERR_BLOCK_SIZE; |
| 92 |
|
| 93 |
while (data_len > 0) { |
| 94 |
size_t i; |
| 95 |
size_t keyStreamToUse; |
| 96 |
|
| 97 |
if (ofbState->usedKeyStream == block_len) { |
| 98 |
int result; |
| 99 |
|
| 100 |
memcpy(oldKeyStream, ofbState->keyStream, block_len); |
| 101 |
result = ofbState->cipher->encrypt(ofbState->cipher, |
| 102 |
oldKeyStream, |
| 103 |
ofbState->keyStream, |
| 104 |
block_len); |
| 105 |
if (0 != result) |
| 106 |
return result; |
| 107 |
|
| 108 |
ofbState->usedKeyStream = 0; |
| 109 |
} |
| 110 |
|
| 111 |
keyStreamToUse = MIN(data_len, block_len - ofbState->usedKeyStream); |
| 112 |
for (i=0; i<keyStreamToUse; i++) |
| 113 |
*out++ = *in++ ^ ofbState->keyStream[i + ofbState->usedKeyStream]; |
| 114 |
|
| 115 |
data_len -= keyStreamToUse; |
| 116 |
ofbState->usedKeyStream += keyStreamToUse; |
| 117 |
} |
| 118 |
|
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
EXPORT_SYM int OFB_decrypt(OfbModeState *ofbState, |
| 123 |
const uint8_t *in, |
| 124 |
uint8_t *out, |
| 125 |
size_t data_len) |
| 126 |
{ |
| 127 |
return OFB_encrypt(ofbState, in, out, data_len); |
| 128 |
} |
| 129 |
|
| 130 |
EXPORT_SYM int OFB_stop_operation(OfbModeState *state) |
| 131 |
{ |
| 132 |
if (NULL == state) |
| 133 |
return ERR_NULL; |
| 134 |
state->cipher->destructor(state->cipher); |
| 135 |
free(state); |
| 136 |
return 0; |
| 137 |
} |