xref: /freebsd/sys/crypto/aesni/aesni.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
15f270659SKonstantin Belousov /*-
25f270659SKonstantin Belousov  * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
35f270659SKonstantin Belousov  * All rights reserved.
45f270659SKonstantin Belousov  *
55f270659SKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
65f270659SKonstantin Belousov  * modification, are permitted provided that the following conditions
75f270659SKonstantin Belousov  * are met:
85f270659SKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
95f270659SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
105f270659SKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
115f270659SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
125f270659SKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
135f270659SKonstantin Belousov  *
145f270659SKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
155f270659SKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
165f270659SKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
175f270659SKonstantin Belousov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
185f270659SKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
195f270659SKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
205f270659SKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
215f270659SKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
225f270659SKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
235f270659SKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
245f270659SKonstantin Belousov  * SUCH DAMAGE.
255f270659SKonstantin Belousov  */
265f270659SKonstantin Belousov 
275f270659SKonstantin Belousov #ifndef _AESNI_H_
285f270659SKonstantin Belousov #define _AESNI_H_
295f270659SKonstantin Belousov 
305f270659SKonstantin Belousov #include <sys/types.h>
315f270659SKonstantin Belousov #include <sys/malloc.h>
325f270659SKonstantin Belousov #include <sys/queue.h>
335f270659SKonstantin Belousov 
345f270659SKonstantin Belousov #include <opencrypto/cryptodev.h>
355f270659SKonstantin Belousov 
362b375b4eSYoshihiro Takahashi #if defined(__amd64__) || defined(__i386__)
375f270659SKonstantin Belousov #include <machine/cpufunc.h>
385f270659SKonstantin Belousov #include <machine/cputypes.h>
395f270659SKonstantin Belousov #include <machine/md_var.h>
405f270659SKonstantin Belousov #include <machine/specialreg.h>
415f270659SKonstantin Belousov #include <machine/fpu.h>
425f270659SKonstantin Belousov #endif
435f270659SKonstantin Belousov 
445f270659SKonstantin Belousov #define	AES128_ROUNDS	10
455f270659SKonstantin Belousov #define	AES192_ROUNDS	12
465f270659SKonstantin Belousov #define	AES256_ROUNDS	14
475f270659SKonstantin Belousov #define	AES_SCHED_LEN	((AES256_ROUNDS + 1) * AES_BLOCK_LEN)
48098c902bSMark Johnston #define	AES_SCHED_ALIGN	16
495f270659SKonstantin Belousov 
505f270659SKonstantin Belousov struct aesni_session {
51098c902bSMark Johnston 	uint8_t schedules[3 * AES_SCHED_LEN + AES_SCHED_ALIGN];
52098c902bSMark Johnston 	uint8_t *enc_schedule;
53098c902bSMark Johnston 	uint8_t *dec_schedule;
54098c902bSMark Johnston 	uint8_t *xts_schedule;
555f270659SKonstantin Belousov 	int rounds;
565f270659SKonstantin Belousov 	/* uint8_t *ses_ictx; */
575f270659SKonstantin Belousov 	/* uint8_t *ses_octx; */
585f270659SKonstantin Belousov 	int used;
59fe182ba1SConrad Meyer 	int mlen;
60c0341432SJohn Baldwin 	int hash_len;
61c0341432SJohn Baldwin 	void (*hash_init)(void *);
629b6b2f86SJohn Baldwin 	int (*hash_update)(void *, const void *, u_int);
63c0341432SJohn Baldwin 	void (*hash_finalize)(void *, void *);
64c0341432SJohn Baldwin 	bool hmac;
655f270659SKonstantin Belousov };
665f270659SKonstantin Belousov 
675f270659SKonstantin Belousov /*
685f270659SKonstantin Belousov  * Internal functions, implemented in assembler.
695f270659SKonstantin Belousov  */
70038ffd3eSJohn-Mark Gurney void aesni_set_enckey(const uint8_t *userkey,
71038ffd3eSJohn-Mark Gurney     uint8_t *encrypt_schedule /*__aligned(16)*/, int number_of_rounds);
72038ffd3eSJohn-Mark Gurney void aesni_set_deckey(const uint8_t *encrypt_schedule /*__aligned(16)*/,
73038ffd3eSJohn-Mark Gurney     uint8_t *decrypt_schedule /*__aligned(16)*/, int number_of_rounds);
745f270659SKonstantin Belousov 
755f270659SKonstantin Belousov /*
765f270659SKonstantin Belousov  * Slightly more public interfaces.
775f270659SKonstantin Belousov  */
78038ffd3eSJohn-Mark Gurney void aesni_encrypt_cbc(int rounds, const void *key_schedule /*__aligned(16)*/,
79038ffd3eSJohn-Mark Gurney     size_t len, const uint8_t *from, uint8_t *to,
808254c3c5SAlan Somers     const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
81038ffd3eSJohn-Mark Gurney void aesni_decrypt_cbc(int rounds, const void *key_schedule /*__aligned(16)*/,
828254c3c5SAlan Somers     size_t len, uint8_t *buf, const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
83038ffd3eSJohn-Mark Gurney void aesni_encrypt_ecb(int rounds, const void *key_schedule /*__aligned(16)*/,
84038ffd3eSJohn-Mark Gurney     size_t len, const uint8_t *from, uint8_t *to);
85038ffd3eSJohn-Mark Gurney void aesni_decrypt_ecb(int rounds, const void *key_schedule /*__aligned(16)*/,
86038ffd3eSJohn-Mark Gurney     size_t len, const uint8_t *from, uint8_t *to);
8708fca7a5SJohn-Mark Gurney void aesni_encrypt_icm(int rounds, const void *key_schedule /*__aligned(16)*/,
8808fca7a5SJohn-Mark Gurney     size_t len, const uint8_t *from, uint8_t *to,
898254c3c5SAlan Somers     const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
905f270659SKonstantin Belousov 
91038ffd3eSJohn-Mark Gurney void aesni_encrypt_xts(int rounds, const void *data_schedule /*__aligned(16)*/,
92038ffd3eSJohn-Mark Gurney     const void *tweak_schedule /*__aligned(16)*/, size_t len,
938254c3c5SAlan Somers     const uint8_t *from, uint8_t *to,
948254c3c5SAlan Somers     const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
95038ffd3eSJohn-Mark Gurney void aesni_decrypt_xts(int rounds, const void *data_schedule /*__aligned(16)*/,
96038ffd3eSJohn-Mark Gurney     const void *tweak_schedule /*__aligned(16)*/, size_t len,
978254c3c5SAlan Somers     const uint8_t *from, uint8_t *to,
988254c3c5SAlan Somers     const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
99ff6c7bf5SJohn-Mark Gurney 
10008fca7a5SJohn-Mark Gurney /* GCM & GHASH functions */
10108fca7a5SJohn-Mark Gurney void AES_GCM_encrypt(const unsigned char *in, unsigned char *out,
10208fca7a5SJohn-Mark Gurney     const unsigned char *addt, const unsigned char *ivec,
10308fca7a5SJohn-Mark Gurney     unsigned char *tag, uint32_t nbytes, uint32_t abytes, int ibytes,
10408fca7a5SJohn-Mark Gurney     const unsigned char *key, int nr);
10508fca7a5SJohn-Mark Gurney int AES_GCM_decrypt(const unsigned char *in, unsigned char *out,
10608fca7a5SJohn-Mark Gurney     const unsigned char *addt, const unsigned char *ivec,
107e381fd29SJohn-Mark Gurney     const unsigned char *tag, uint32_t nbytes, uint32_t abytes, int ibytes,
10808fca7a5SJohn-Mark Gurney     const unsigned char *key, int nr);
10908fca7a5SJohn-Mark Gurney 
1107cff9f37SSean Eric Fagan /* CCM + CBC-MAC functions */
1117cff9f37SSean Eric Fagan void AES_CCM_encrypt(const unsigned char *in, unsigned char *out,
1127cff9f37SSean Eric Fagan     const unsigned char *addt, const unsigned char *ivec,
113*655eb762SJohn Baldwin     unsigned char *tag, uint32_t nbytes, uint32_t abytes, int nlen,
114*655eb762SJohn Baldwin     int tag_length, const unsigned char *key, int nr);
1157cff9f37SSean Eric Fagan int AES_CCM_decrypt(const unsigned char *in, unsigned char *out,
1167cff9f37SSean Eric Fagan     const unsigned char *addt, const unsigned char *ivec,
117*655eb762SJohn Baldwin     const unsigned char *tag, uint32_t nbytes, uint32_t abytes, int nlen,
118*655eb762SJohn Baldwin     int tag_length, const unsigned char *key, int nr);
119c0341432SJohn Baldwin void aesni_cipher_setup_common(struct aesni_session *ses,
120c0341432SJohn Baldwin     const struct crypto_session_params *csp, const uint8_t *key, int keylen);
1215f270659SKonstantin Belousov 
122038ffd3eSJohn-Mark Gurney #endif /* _AESNI_H_ */
123