diff options
| author | John Baldwin <jhb@FreeBSD.org> | 2020-05-20 21:21:01 +0000 |
|---|---|---|
| committer | John Baldwin <jhb@FreeBSD.org> | 2020-05-20 21:21:01 +0000 |
| commit | 3e9470482a1357eef90d007b27ec5d9725ae1111 (patch) | |
| tree | db3cd1b049da8705d5f9328628e9ee7b8d9d9549 /sys/opencrypto/cryptosoft.c | |
| parent | 2aa1dc7e3b637876f4bfdc6b19c35253d922e10a (diff) | |
Notes
Diffstat (limited to 'sys/opencrypto/cryptosoft.c')
| -rw-r--r-- | sys/opencrypto/cryptosoft.c | 88 |
1 files changed, 30 insertions, 58 deletions
diff --git a/sys/opencrypto/cryptosoft.c b/sys/opencrypto/cryptosoft.c index 1f545bfd199b..46b440b97baa 100644 --- a/sys/opencrypto/cryptosoft.c +++ b/sys/opencrypto/cryptosoft.c @@ -65,7 +65,7 @@ struct swcr_auth { }; struct swcr_encdec { - uint8_t *sw_kschedule; + void *sw_kschedule; struct enc_xform *sw_exf; }; @@ -131,11 +131,8 @@ swcr_encdec(struct swcr_session *ses, struct cryptop *crp) crypto_read_iv(crp, iv); if (crp->crp_cipher_key != NULL) { - if (sw->sw_kschedule) - exf->zerokey(&(sw->sw_kschedule)); - csp = crypto_get_params(crp->crp_session); - error = exf->setkey(&sw->sw_kschedule, + error = exf->setkey(sw->sw_kschedule, crp->crp_cipher_key, csp->csp_cipher_klen); if (error) return (error); @@ -197,10 +194,10 @@ swcr_encdec(struct swcr_session *ses, struct cryptop *crp) /* Actual encryption/decryption */ if (exf->reinit) { if (encrypting) { - exf->encrypt(sw->sw_kschedule, + exf->encrypt(sw->sw_kschedule, blk, blk); } else { - exf->decrypt(sw->sw_kschedule, + exf->decrypt(sw->sw_kschedule, blk, blk); } } else if (encrypting) { @@ -208,7 +205,7 @@ swcr_encdec(struct swcr_session *ses, struct cryptop *crp) for (j = 0; j < blks; j++) blk[j] ^= ivp[j]; - exf->encrypt(sw->sw_kschedule, blk); + exf->encrypt(sw->sw_kschedule, blk, blk); /* * Keep encrypted block for XOR'ing @@ -224,7 +221,7 @@ swcr_encdec(struct swcr_session *ses, struct cryptop *crp) nivp = (ivp == iv) ? iv2 : iv; bcopy(blk, nivp, blks); - exf->decrypt(sw->sw_kschedule, blk); + exf->decrypt(sw->sw_kschedule, blk, blk); /* XOR with previous block */ for (j = 0; j < blks; j++) @@ -264,25 +261,25 @@ swcr_encdec(struct swcr_session *ses, struct cryptop *crp) if (exf->reinit) { if (encrypting && exf->encrypt_multi == NULL) exf->encrypt(sw->sw_kschedule, - idat); + idat, idat); else if (encrypting) { nb = rounddown(rem, blks); exf->encrypt_multi(sw->sw_kschedule, - idat, nb); + idat, idat, nb); } else if (exf->decrypt_multi == NULL) exf->decrypt(sw->sw_kschedule, - idat); + idat, idat); else { nb = rounddown(rem, blks); exf->decrypt_multi(sw->sw_kschedule, - idat, nb); + idat, idat, nb); } } else if (encrypting) { /* XOR with previous block/IV */ for (j = 0; j < blks; j++) idat[j] ^= ivp[j]; - exf->encrypt(sw->sw_kschedule, idat); + exf->encrypt(sw->sw_kschedule, idat, idat); ivp = idat; } else { /* decrypt */ /* @@ -292,7 +289,7 @@ swcr_encdec(struct swcr_session *ses, struct cryptop *crp) nivp = (ivp == iv) ? iv2 : iv; bcopy(idat, nivp, blks); - exf->decrypt(sw->sw_kschedule, idat); + exf->decrypt(sw->sw_kschedule, idat, idat); /* XOR with previous block/IV */ for (j = 0; j < blks; j++) @@ -543,7 +540,7 @@ swcr_gcm(struct swcr_session *ses, struct cryptop *crp) bzero(blk, blksz); crypto_copydata(crp, crp->crp_payload_start + i, len, blk); if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) { - exf->encrypt(swe->sw_kschedule, blk); + exf->encrypt(swe->sw_kschedule, blk, blk); axf->Update(&ctx, blk, len); crypto_copyback(crp, crp->crp_payload_start + i, len, blk); @@ -579,7 +576,7 @@ swcr_gcm(struct swcr_session *ses, struct cryptop *crp) bzero(blk, blksz); crypto_copydata(crp, crp->crp_payload_start + i, len, blk); - exf->decrypt(swe->sw_kschedule, blk); + exf->decrypt(swe->sw_kschedule, blk, blk); crypto_copyback(crp, crp->crp_payload_start + i, len, blk); } @@ -704,7 +701,7 @@ swcr_ccm(struct swcr_session *ses, struct cryptop *crp) crypto_copydata(crp, crp->crp_payload_start + i, len, blk); if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) { axf->Update(&ctx, blk, len); - exf->encrypt(swe->sw_kschedule, blk); + exf->encrypt(swe->sw_kschedule, blk, blk); crypto_copyback(crp, crp->crp_payload_start + i, len, blk); } else { @@ -716,7 +713,7 @@ swcr_ccm(struct swcr_session *ses, struct cryptop *crp) * the tag and a second time after the tag is * verified. */ - exf->decrypt(swe->sw_kschedule, blk); + exf->decrypt(swe->sw_kschedule, blk, blk); axf->Update(&ctx, blk, len); } } @@ -741,7 +738,7 @@ swcr_ccm(struct swcr_session *ses, struct cryptop *crp) bzero(blk, blksz); crypto_copydata(crp, crp->crp_payload_start + i, len, blk); - exf->decrypt(swe->sw_kschedule, blk); + exf->decrypt(swe->sw_kschedule, blk, blk); crypto_copyback(crp, crp->crp_payload_start + i, len, blk); } @@ -854,7 +851,7 @@ swcr_compdec(struct swcr_session *ses, struct cryptop *crp) } static int -swcr_setup_encdec(struct swcr_session *ses, +swcr_setup_cipher(struct swcr_session *ses, const struct crypto_session_params *csp) { struct swcr_encdec *swe; @@ -864,8 +861,14 @@ swcr_setup_encdec(struct swcr_session *ses, swe = &ses->swcr_encdec; txf = crypto_cipher(csp); MPASS(txf->ivsize == csp->csp_ivlen); + if (txf->ctxsize != 0) { + swe->sw_kschedule = malloc(txf->ctxsize, M_CRYPTO_DATA, + M_NOWAIT); + if (swe->sw_kschedule == NULL) + return (ENOMEM); + } if (csp->csp_cipher_key != NULL) { - error = txf->setkey(&swe->sw_kschedule, + error = txf->setkey(swe->sw_kschedule, csp->csp_cipher_key, csp->csp_cipher_klen); if (error) return (error); @@ -962,11 +965,8 @@ static int swcr_setup_gcm(struct swcr_session *ses, const struct crypto_session_params *csp) { - struct swcr_encdec *swe; struct swcr_auth *swa; - struct enc_xform *txf; struct auth_hash *axf; - int error; if (csp->csp_ivlen != AES_GCM_IV_LEN) return (EINVAL); @@ -1002,28 +1002,15 @@ swcr_setup_gcm(struct swcr_session *ses, csp->csp_cipher_klen); /* Second, setup the cipher side. */ - swe = &ses->swcr_encdec; - txf = &enc_xform_aes_nist_gcm; - if (csp->csp_cipher_key != NULL) { - error = txf->setkey(&swe->sw_kschedule, - csp->csp_cipher_key, csp->csp_cipher_klen); - if (error) - return (error); - } - swe->sw_exf = txf; - - return (0); + return (swcr_setup_cipher(ses, csp)); } static int swcr_setup_ccm(struct swcr_session *ses, const struct crypto_session_params *csp) { - struct swcr_encdec *swe; struct swcr_auth *swa; - struct enc_xform *txf; struct auth_hash *axf; - int error; if (csp->csp_ivlen != AES_CCM_IV_LEN) return (EINVAL); @@ -1059,17 +1046,7 @@ swcr_setup_ccm(struct swcr_session *ses, csp->csp_cipher_klen); /* Second, setup the cipher side. */ - swe = &ses->swcr_encdec; - txf = &enc_xform_ccm; - if (csp->csp_cipher_key != NULL) { - error = txf->setkey(&swe->sw_kschedule, - csp->csp_cipher_key, csp->csp_cipher_klen); - if (error) - return (error); - } - swe->sw_exf = txf; - - return (0); + return (swcr_setup_cipher(ses, csp)); } static bool @@ -1246,7 +1223,7 @@ swcr_newsession(device_t dev, crypto_session_t cses, panic("bad cipher algo"); #endif default: - error = swcr_setup_encdec(ses, csp); + error = swcr_setup_cipher(ses, csp); if (error == 0) ses->swcr_process = swcr_encdec; } @@ -1295,7 +1272,7 @@ swcr_newsession(device_t dev, crypto_session_t cses, break; } - error = swcr_setup_encdec(ses, csp); + error = swcr_setup_cipher(ses, csp); if (error == 0) ses->swcr_process = swcr_eta; break; @@ -1313,18 +1290,13 @@ swcr_freesession(device_t dev, crypto_session_t cses) { struct swcr_session *ses; struct swcr_auth *swa; - struct enc_xform *txf; struct auth_hash *axf; ses = crypto_get_driver_session(cses); mtx_destroy(&ses->swcr_lock); - txf = ses->swcr_encdec.sw_exf; - if (txf != NULL) { - if (ses->swcr_encdec.sw_kschedule != NULL) - txf->zerokey(&(ses->swcr_encdec.sw_kschedule)); - } + zfree(ses->swcr_encdec.sw_kschedule, M_CRYPTO_DATA); axf = ses->swcr_auth.sw_axf; if (axf != NULL) { |
