24
24
/* stream implementation */
26
26
private_st_aes_state(); /* creates a gc object for our state,
29
29
/* Store a key in our crypt context */
31
31
s_aes_set_key(stream_aes_state * state, const unsigned char *key,
36
36
if ( (keylength < 1) || (keylength > SAES_MAX_KEYLENGTH) )
37
return_error(gs_error_rangecheck);
37
return_error(gs_error_rangecheck);
39
return_error(gs_error_invalidaccess);
39
return_error(gs_error_invalidaccess);
41
41
/* we can't set the key here because the interpreter's
42
42
filter implementation wants to duplicate our state
92
92
s_aes_process(stream_state * ss, stream_cursor_read * pr,
93
stream_cursor_write * pw, bool last)
93
stream_cursor_write * pw, bool last)
95
95
stream_aes_state *const state = (stream_aes_state *) ss;
96
96
const unsigned char *limit;
102
102
/* figure out if we're going to run out of space */
103
103
if (in_size > out_size) {
104
limit = pr->ptr + out_size;
105
status = 1; /* need more output space */
104
limit = pr->ptr + out_size;
105
status = 1; /* need more output space */
108
status = last ? EOFC : 0; /* need more input */
108
status = last ? EOFC : 0; /* need more input */
111
111
/* set up state and context */
114
114
contains internal pointers, so we need to store it separately
115
115
in immovable memory like any opaque structure. */
116
116
state->ctx = (aes_context *)gs_alloc_bytes_immovable(state->memory,
117
sizeof(aes_context), "aes context structure");
117
sizeof(aes_context), "aes context structure");
118
118
if (state->ctx == NULL) {
119
gs_throw(gs_error_VMerror, "could not allocate aes context");
119
gs_throw(gs_error_VMerror, "could not allocate aes context");
122
122
if (state->keylength < 1 || state->keylength > SAES_MAX_KEYLENGTH) {
123
gs_throw1(gs_error_rangecheck, "invalid aes key length (%d bytes)",
123
gs_throw1(gs_error_rangecheck, "invalid aes key length (%d bytes)",
127
127
aes_setkey_dec(state->ctx, state->key, state->keylength * 8);
129
129
if (!state->initialized) {
130
/* read the initialization vector from the first 16 bytes */
131
if (in_size < 16) return 0; /* get more data */
132
memcpy(state->iv, pr->ptr + 1, 16);
133
state->initialized = 1;
130
/* read the initialization vector from the first 16 bytes */
131
if (in_size < 16) return 0; /* get more data */
132
memcpy(state->iv, pr->ptr + 1, 16);
133
state->initialized = 1;
137
137
/* decrypt available blocks */
138
138
while (pr->ptr + 16 <= limit) {
139
139
aes_crypt_cbc(state->ctx, AES_DECRYPT, 16, state->iv,
142
142
if (last && pr->ptr == pr->limit) {
143
143
/* we're on the last block; unpad if necessary */