xref: /freebsd/sys/crypto/chacha20/chacha-sw.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
161590291SConrad Meyer /* This file is in the public domain. */
261590291SConrad Meyer 
361590291SConrad Meyer #include <sys/cdefs.h>
461590291SConrad Meyer #include <crypto/chacha20/chacha.h>
561590291SConrad Meyer #include <opencrypto/xform_enc.h>
661590291SConrad Meyer 
761590291SConrad Meyer static int
chacha20_xform_setkey(void * ctx,const uint8_t * key,int len)83e947048SJohn Baldwin chacha20_xform_setkey(void *ctx, const uint8_t *key, int len)
961590291SConrad Meyer {
1061590291SConrad Meyer 
1161590291SConrad Meyer 	if (len != CHACHA_MINKEYLEN && len != 32)
1261590291SConrad Meyer 		return (EINVAL);
1361590291SConrad Meyer 
1461590291SConrad Meyer 	chacha_keysetup(ctx, key, len * 8);
1561590291SConrad Meyer 	return (0);
1661590291SConrad Meyer }
1761590291SConrad Meyer 
1861590291SConrad Meyer static void
chacha20_xform_reinit(void * ctx,const uint8_t * iv,size_t ivlen)191833d604SJohn Baldwin chacha20_xform_reinit(void *ctx, const uint8_t *iv, size_t ivlen)
2061590291SConrad Meyer {
211833d604SJohn Baldwin 	KASSERT(ivlen == CHACHA_NONCELEN + CHACHA_CTRLEN,
221833d604SJohn Baldwin 	    ("%s: invalid IV length", __func__));
2361590291SConrad Meyer 	chacha_ivsetup(ctx, iv + 8, iv);
2461590291SConrad Meyer }
2561590291SConrad Meyer 
2661590291SConrad Meyer static void
chacha20_xform_crypt(void * ctx,const uint8_t * in,uint8_t * out)273e947048SJohn Baldwin chacha20_xform_crypt(void *ctx, const uint8_t *in, uint8_t *out)
2861590291SConrad Meyer {
2961590291SConrad Meyer 
30723d8764SJohn Baldwin 	chacha_encrypt_bytes(ctx, in, out, CHACHA_BLOCKLEN);
3161590291SConrad Meyer }
3261590291SConrad Meyer 
3361590291SConrad Meyer static void
chacha20_xform_crypt_multi(void * ctx,const uint8_t * in,uint8_t * out,size_t len)34*d7f0b3ceSJohn Baldwin chacha20_xform_crypt_multi(void *ctx, const uint8_t *in, uint8_t *out,
35*d7f0b3ceSJohn Baldwin     size_t len)
36*d7f0b3ceSJohn Baldwin {
37*d7f0b3ceSJohn Baldwin 	KASSERT(len % CHACHA_BLOCKLEN == 0, ("%s: invalid length", __func__));
38*d7f0b3ceSJohn Baldwin 	chacha_encrypt_bytes(ctx, in, out, len);
39*d7f0b3ceSJohn Baldwin }
40*d7f0b3ceSJohn Baldwin 
41*d7f0b3ceSJohn Baldwin static void
chacha20_xform_crypt_last(void * ctx,const uint8_t * in,uint8_t * out,size_t len)42723d8764SJohn Baldwin chacha20_xform_crypt_last(void *ctx, const uint8_t *in, uint8_t *out,
433e947048SJohn Baldwin     size_t len)
4461590291SConrad Meyer {
4561590291SConrad Meyer 
463e947048SJohn Baldwin 	chacha_encrypt_bytes(ctx, in, out, len);
4761590291SConrad Meyer }
4861590291SConrad Meyer 
49d8787d4fSMark Johnston const struct enc_xform enc_xform_chacha20 = {
5061590291SConrad Meyer 	.type = CRYPTO_CHACHA20,
5161590291SConrad Meyer 	.name = "chacha20",
523e947048SJohn Baldwin 	.ctxsize = sizeof(struct chacha_ctx),
5361590291SConrad Meyer 	.blocksize = 1,
54723d8764SJohn Baldwin 	.native_blocksize = CHACHA_BLOCKLEN,
5561590291SConrad Meyer 	.ivsize = CHACHA_NONCELEN + CHACHA_CTRLEN,
5661590291SConrad Meyer 	.minkey = CHACHA_MINKEYLEN,
5761590291SConrad Meyer 	.maxkey = 32,
5861590291SConrad Meyer 	.setkey = chacha20_xform_setkey,
5961590291SConrad Meyer 	.reinit = chacha20_xform_reinit,
60*d7f0b3ceSJohn Baldwin 	.encrypt = chacha20_xform_crypt,
61*d7f0b3ceSJohn Baldwin 	.decrypt = chacha20_xform_crypt,
62*d7f0b3ceSJohn Baldwin 	.encrypt_multi = chacha20_xform_crypt_multi,
63*d7f0b3ceSJohn Baldwin 	.decrypt_multi = chacha20_xform_crypt_multi,
64723d8764SJohn Baldwin 	.encrypt_last = chacha20_xform_crypt_last,
65723d8764SJohn Baldwin 	.decrypt_last = chacha20_xform_crypt_last,
6661590291SConrad Meyer };
67