Changeset View
Changeset View
Standalone View
Standalone View
sys/opencrypto/cryptosoft.c
Show First 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | |||||
swcr_encdec(const struct swcr_session *ses, struct cryptop *crp) | swcr_encdec(const struct swcr_session *ses, struct cryptop *crp) | ||||
{ | { | ||||
unsigned char blk[EALG_MAX_BLOCK_LEN]; | unsigned char blk[EALG_MAX_BLOCK_LEN]; | ||||
const struct crypto_session_params *csp; | const struct crypto_session_params *csp; | ||||
const struct enc_xform *exf; | const struct enc_xform *exf; | ||||
const struct swcr_encdec *sw; | const struct swcr_encdec *sw; | ||||
void *ctx; | void *ctx; | ||||
size_t inlen, outlen, todo; | size_t inlen, outlen, todo; | ||||
int blks, resid; | int blksz, resid; | ||||
struct crypto_buffer_cursor cc_in, cc_out; | struct crypto_buffer_cursor cc_in, cc_out; | ||||
const unsigned char *inblk; | const unsigned char *inblk; | ||||
unsigned char *outblk; | unsigned char *outblk; | ||||
int error; | int error; | ||||
bool encrypting; | bool encrypting; | ||||
error = 0; | error = 0; | ||||
sw = &ses->swcr_encdec; | sw = &ses->swcr_encdec; | ||||
exf = sw->sw_exf; | exf = sw->sw_exf; | ||||
csp = crypto_get_params(crp->crp_session); | csp = crypto_get_params(crp->crp_session); | ||||
if (exf->native_blocksize == 0) { | if (exf->native_blocksize == 0) { | ||||
/* Check for non-padded data */ | /* Check for non-padded data */ | ||||
if ((crp->crp_payload_length % exf->blocksize) != 0) | if ((crp->crp_payload_length % exf->blocksize) != 0) | ||||
return (EINVAL); | return (EINVAL); | ||||
blks = exf->blocksize; | blksz = exf->blocksize; | ||||
} else | } else | ||||
blks = exf->native_blocksize; | blksz = exf->native_blocksize; | ||||
if (exf == &enc_xform_aes_icm && | if (exf == &enc_xform_aes_icm && | ||||
(crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0) | (crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0) | ||||
return (EINVAL); | return (EINVAL); | ||||
ctx = __builtin_alloca(exf->ctxsize); | ctx = __builtin_alloca(exf->ctxsize); | ||||
if (crp->crp_cipher_key != NULL) { | if (crp->crp_cipher_key != NULL) { | ||||
error = exf->setkey(ctx, crp->crp_cipher_key, | error = exf->setkey(ctx, crp->crp_cipher_key, | ||||
Show All 19 Lines | swcr_encdec(const struct swcr_session *ses, struct cryptop *crp) | ||||
encrypting = CRYPTO_OP_IS_ENCRYPT(crp->crp_op); | encrypting = CRYPTO_OP_IS_ENCRYPT(crp->crp_op); | ||||
/* | /* | ||||
* Loop through encrypting blocks. 'inlen' is the remaining | * Loop through encrypting blocks. 'inlen' is the remaining | ||||
* length of the current segment in the input buffer. | * length of the current segment in the input buffer. | ||||
* 'outlen' is the remaining length of current segment in the | * 'outlen' is the remaining length of current segment in the | ||||
* output buffer. | * output buffer. | ||||
*/ | */ | ||||
for (resid = crp->crp_payload_length; resid >= blks; resid -= todo) { | for (resid = crp->crp_payload_length; resid >= blksz; resid -= todo) { | ||||
/* | /* | ||||
* If the current block is not contained within the | * If the current block is not contained within the | ||||
* current input/output segment, use 'blk' as a local | * current input/output segment, use 'blk' as a local | ||||
* buffer. | * buffer. | ||||
*/ | */ | ||||
if (inlen < blks) { | if (inlen < blksz) { | ||||
crypto_cursor_copydata(&cc_in, blks, blk); | crypto_cursor_copydata(&cc_in, blksz, blk); | ||||
inblk = blk; | inblk = blk; | ||||
inlen = blks; | inlen = blksz; | ||||
} | } | ||||
if (outlen < blks) { | if (outlen < blksz) { | ||||
outblk = blk; | outblk = blk; | ||||
outlen = blks; | outlen = blksz; | ||||
} | } | ||||
todo = rounddown2(MIN(resid, MIN(inlen, outlen)), blks); | todo = rounddown2(MIN(resid, MIN(inlen, outlen)), blksz); | ||||
if (encrypting) | if (encrypting) | ||||
exf->encrypt_multi(ctx, inblk, outblk, todo); | exf->encrypt_multi(ctx, inblk, outblk, todo); | ||||
else | else | ||||
exf->decrypt_multi(ctx, inblk, outblk, todo); | exf->decrypt_multi(ctx, inblk, outblk, todo); | ||||
if (inblk == blk) { | if (inblk == blk) { | ||||
inblk = crypto_cursor_segment(&cc_in, &inlen); | inblk = crypto_cursor_segment(&cc_in, &inlen); | ||||
} else { | } else { | ||||
crypto_cursor_advance(&cc_in, todo); | crypto_cursor_advance(&cc_in, todo); | ||||
inlen -= todo; | inlen -= todo; | ||||
inblk += todo; | inblk += todo; | ||||
if (inlen == 0) | if (inlen == 0) | ||||
inblk = crypto_cursor_segment(&cc_in, &inlen); | inblk = crypto_cursor_segment(&cc_in, &inlen); | ||||
} | } | ||||
if (outblk == blk) { | if (outblk == blk) { | ||||
crypto_cursor_copyback(&cc_out, blks, blk); | crypto_cursor_copyback(&cc_out, blksz, blk); | ||||
outblk = crypto_cursor_segment(&cc_out, &outlen); | outblk = crypto_cursor_segment(&cc_out, &outlen); | ||||
} else { | } else { | ||||
crypto_cursor_advance(&cc_out, todo); | crypto_cursor_advance(&cc_out, todo); | ||||
outlen -= todo; | outlen -= todo; | ||||
outblk += todo; | outblk += todo; | ||||
if (outlen == 0) | if (outlen == 0) | ||||
outblk = crypto_cursor_segment(&cc_out, | outblk = crypto_cursor_segment(&cc_out, | ||||
&outlen); | &outlen); | ||||
} | } | ||||
} | } | ||||
/* Handle trailing partial block for stream ciphers. */ | /* Handle trailing partial block for stream ciphers. */ | ||||
if (resid > 0) { | if (resid > 0) { | ||||
KASSERT(exf->native_blocksize != 0, | KASSERT(exf->native_blocksize != 0, | ||||
("%s: partial block of %d bytes for cipher %s", | ("%s: partial block of %d bytes for cipher %s", | ||||
__func__, resid, exf->name)); | __func__, resid, exf->name)); | ||||
KASSERT(resid < blks, ("%s: partial block too big", __func__)); | KASSERT(resid < blksz, ("%s: partial block too big", __func__)); | ||||
inblk = crypto_cursor_segment(&cc_in, &inlen); | inblk = crypto_cursor_segment(&cc_in, &inlen); | ||||
outblk = crypto_cursor_segment(&cc_out, &outlen); | outblk = crypto_cursor_segment(&cc_out, &outlen); | ||||
if (inlen < resid) { | if (inlen < resid) { | ||||
crypto_cursor_copydata(&cc_in, resid, blk); | crypto_cursor_copydata(&cc_in, resid, blk); | ||||
inblk = blk; | inblk = blk; | ||||
} | } | ||||
if (outlen < resid) | if (outlen < resid) | ||||
▲ Show 20 Lines • Show All 1,383 Lines • Show Last 20 Lines |