1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * AES-NI + SSE2 implementation of AEGIS-128
5 * Copyright (c) 2017-2018 Ondrej Mosnacek <omosnacek@gmail.com>
6 * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
9 #include <linux/linkage.h>
10 #include <linux/cfi_types.h>
11 #include <asm/frame.h>
28 .section .rodata.cst16.aegis128_const, "aM", @progbits, 32
31 .byte 0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d
32 .byte 0x15, 0x22, 0x37, 0x59, 0x90, 0xe9, 0x79, 0x62
34 .byte 0xdb, 0x3d, 0x18, 0x55, 0x6d, 0xc2, 0x2f, 0xf1
35 .byte 0x20, 0x11, 0x31, 0x42, 0x73, 0xb5, 0x28, 0xdd
37 .section .rodata.cst16.aegis128_counter, "aM", @progbits, 16
40 .byte 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
41 .byte 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
48 * STATE[0-4] - input state
50 * STATE[0-4] - output state (shifted positions)
54 .macro aegis128_update
64 * __load_partial: internal ABI
75 SYM_FUNC_START_LOCAL(__load_partial)
127 SYM_FUNC_END(__load_partial)
130 * __store_partial: internal ABI
141 SYM_FUNC_START_LOCAL(__store_partial)
185 SYM_FUNC_END(__store_partial)
188 * void crypto_aegis128_aesni_init(void *state, const void *key, const void *iv);
190 SYM_FUNC_START(crypto_aegis128_aesni_init)
203 /* load the constants: */
204 movdqa .Laegis128_const_0(%rip), STATE2
205 movdqa .Laegis128_const_1(%rip), STATE1
209 /* update 10 times with KEY / KEY xor IV: */
210 aegis128_update; pxor KEY, STATE4
211 aegis128_update; pxor T1, STATE3
212 aegis128_update; pxor KEY, STATE2
213 aegis128_update; pxor T1, STATE1
214 aegis128_update; pxor KEY, STATE0
215 aegis128_update; pxor T1, STATE4
216 aegis128_update; pxor KEY, STATE3
217 aegis128_update; pxor T1, STATE2
218 aegis128_update; pxor KEY, STATE1
219 aegis128_update; pxor T1, STATE0
221 /* store the state: */
222 movdqu STATE0, 0x00(STATEP)
223 movdqu STATE1, 0x10(STATEP)
224 movdqu STATE2, 0x20(STATEP)
225 movdqu STATE3, 0x30(STATEP)
226 movdqu STATE4, 0x40(STATEP)
230 SYM_FUNC_END(crypto_aegis128_aesni_init)
233 * void crypto_aegis128_aesni_ad(void *state, unsigned int length,
236 SYM_FUNC_START(crypto_aegis128_aesni_ad)
242 /* load the state: */
243 movdqu 0x00(STATEP), STATE0
244 movdqu 0x10(STATEP), STATE1
245 movdqu 0x20(STATEP), STATE2
246 movdqu 0x30(STATEP), STATE3
247 movdqu 0x40(STATEP), STATE4
255 movdqa 0x00(SRC), MSG
262 movdqa 0x10(SRC), MSG
269 movdqa 0x20(SRC), MSG
276 movdqa 0x30(SRC), MSG
283 movdqa 0x40(SRC), MSG
295 movdqu 0x00(SRC), MSG
302 movdqu 0x10(SRC), MSG
309 movdqu 0x20(SRC), MSG
316 movdqu 0x30(SRC), MSG
323 movdqu 0x40(SRC), MSG
333 /* store the state: */
335 movdqu STATE0, 0x00(STATEP)
336 movdqu STATE1, 0x10(STATEP)
337 movdqu STATE2, 0x20(STATEP)
338 movdqu STATE3, 0x30(STATEP)
339 movdqu STATE4, 0x40(STATEP)
344 movdqu STATE4, 0x00(STATEP)
345 movdqu STATE0, 0x10(STATEP)
346 movdqu STATE1, 0x20(STATEP)
347 movdqu STATE2, 0x30(STATEP)
348 movdqu STATE3, 0x40(STATEP)
353 movdqu STATE3, 0x00(STATEP)
354 movdqu STATE4, 0x10(STATEP)
355 movdqu STATE0, 0x20(STATEP)
356 movdqu STATE1, 0x30(STATEP)
357 movdqu STATE2, 0x40(STATEP)
362 movdqu STATE2, 0x00(STATEP)
363 movdqu STATE3, 0x10(STATEP)
364 movdqu STATE4, 0x20(STATEP)
365 movdqu STATE0, 0x30(STATEP)
366 movdqu STATE1, 0x40(STATEP)
371 movdqu STATE1, 0x00(STATEP)
372 movdqu STATE2, 0x10(STATEP)
373 movdqu STATE3, 0x20(STATEP)
374 movdqu STATE4, 0x30(STATEP)
375 movdqu STATE0, 0x40(STATEP)
382 SYM_FUNC_END(crypto_aegis128_aesni_ad)
384 .macro encrypt_block a s0 s1 s2 s3 s4 i
385 movdq\a (\i * 0x10)(SRC), MSG
392 movdq\a T0, (\i * 0x10)(DST)
403 * void crypto_aegis128_aesni_enc(void *state, unsigned int length,
404 * const void *src, void *dst);
406 SYM_TYPED_FUNC_START(crypto_aegis128_aesni_enc)
412 /* load the state: */
413 movdqu 0x00(STATEP), STATE0
414 movdqu 0x10(STATEP), STATE1
415 movdqu 0x20(STATEP), STATE2
416 movdqu 0x30(STATEP), STATE3
417 movdqu 0x40(STATEP), STATE4
426 encrypt_block a STATE0 STATE1 STATE2 STATE3 STATE4 0
427 encrypt_block a STATE4 STATE0 STATE1 STATE2 STATE3 1
428 encrypt_block a STATE3 STATE4 STATE0 STATE1 STATE2 2
429 encrypt_block a STATE2 STATE3 STATE4 STATE0 STATE1 3
430 encrypt_block a STATE1 STATE2 STATE3 STATE4 STATE0 4
438 encrypt_block u STATE0 STATE1 STATE2 STATE3 STATE4 0
439 encrypt_block u STATE4 STATE0 STATE1 STATE2 STATE3 1
440 encrypt_block u STATE3 STATE4 STATE0 STATE1 STATE2 2
441 encrypt_block u STATE2 STATE3 STATE4 STATE0 STATE1 3
442 encrypt_block u STATE1 STATE2 STATE3 STATE4 STATE0 4
448 /* store the state: */
450 movdqu STATE4, 0x00(STATEP)
451 movdqu STATE0, 0x10(STATEP)
452 movdqu STATE1, 0x20(STATEP)
453 movdqu STATE2, 0x30(STATEP)
454 movdqu STATE3, 0x40(STATEP)
459 movdqu STATE3, 0x00(STATEP)
460 movdqu STATE4, 0x10(STATEP)
461 movdqu STATE0, 0x20(STATEP)
462 movdqu STATE1, 0x30(STATEP)
463 movdqu STATE2, 0x40(STATEP)
468 movdqu STATE2, 0x00(STATEP)
469 movdqu STATE3, 0x10(STATEP)
470 movdqu STATE4, 0x20(STATEP)
471 movdqu STATE0, 0x30(STATEP)
472 movdqu STATE1, 0x40(STATEP)
477 movdqu STATE1, 0x00(STATEP)
478 movdqu STATE2, 0x10(STATEP)
479 movdqu STATE3, 0x20(STATEP)
480 movdqu STATE4, 0x30(STATEP)
481 movdqu STATE0, 0x40(STATEP)
486 movdqu STATE0, 0x00(STATEP)
487 movdqu STATE1, 0x10(STATEP)
488 movdqu STATE2, 0x20(STATEP)
489 movdqu STATE3, 0x30(STATEP)
490 movdqu STATE4, 0x40(STATEP)
497 SYM_FUNC_END(crypto_aegis128_aesni_enc)
500 * void crypto_aegis128_aesni_enc_tail(void *state, unsigned int length,
501 * const void *src, void *dst);
503 SYM_TYPED_FUNC_START(crypto_aegis128_aesni_enc_tail)
506 /* load the state: */
507 movdqu 0x00(STATEP), STATE0
508 movdqu 0x10(STATEP), STATE1
509 movdqu 0x20(STATEP), STATE2
510 movdqu 0x30(STATEP), STATE3
511 movdqu 0x40(STATEP), STATE4
513 /* encrypt message: */
528 /* store the state: */
529 movdqu STATE4, 0x00(STATEP)
530 movdqu STATE0, 0x10(STATEP)
531 movdqu STATE1, 0x20(STATEP)
532 movdqu STATE2, 0x30(STATEP)
533 movdqu STATE3, 0x40(STATEP)
537 SYM_FUNC_END(crypto_aegis128_aesni_enc_tail)
539 .macro decrypt_block a s0 s1 s2 s3 s4 i
540 movdq\a (\i * 0x10)(SRC), MSG
546 movdq\a MSG, (\i * 0x10)(DST)
557 * void crypto_aegis128_aesni_dec(void *state, unsigned int length,
558 * const void *src, void *dst);
560 SYM_TYPED_FUNC_START(crypto_aegis128_aesni_dec)
566 /* load the state: */
567 movdqu 0x00(STATEP), STATE0
568 movdqu 0x10(STATEP), STATE1
569 movdqu 0x20(STATEP), STATE2
570 movdqu 0x30(STATEP), STATE3
571 movdqu 0x40(STATEP), STATE4
580 decrypt_block a STATE0 STATE1 STATE2 STATE3 STATE4 0
581 decrypt_block a STATE4 STATE0 STATE1 STATE2 STATE3 1
582 decrypt_block a STATE3 STATE4 STATE0 STATE1 STATE2 2
583 decrypt_block a STATE2 STATE3 STATE4 STATE0 STATE1 3
584 decrypt_block a STATE1 STATE2 STATE3 STATE4 STATE0 4
592 decrypt_block u STATE0 STATE1 STATE2 STATE3 STATE4 0
593 decrypt_block u STATE4 STATE0 STATE1 STATE2 STATE3 1
594 decrypt_block u STATE3 STATE4 STATE0 STATE1 STATE2 2
595 decrypt_block u STATE2 STATE3 STATE4 STATE0 STATE1 3
596 decrypt_block u STATE1 STATE2 STATE3 STATE4 STATE0 4
602 /* store the state: */
604 movdqu STATE4, 0x00(STATEP)
605 movdqu STATE0, 0x10(STATEP)
606 movdqu STATE1, 0x20(STATEP)
607 movdqu STATE2, 0x30(STATEP)
608 movdqu STATE3, 0x40(STATEP)
613 movdqu STATE3, 0x00(STATEP)
614 movdqu STATE4, 0x10(STATEP)
615 movdqu STATE0, 0x20(STATEP)
616 movdqu STATE1, 0x30(STATEP)
617 movdqu STATE2, 0x40(STATEP)
622 movdqu STATE2, 0x00(STATEP)
623 movdqu STATE3, 0x10(STATEP)
624 movdqu STATE4, 0x20(STATEP)
625 movdqu STATE0, 0x30(STATEP)
626 movdqu STATE1, 0x40(STATEP)
631 movdqu STATE1, 0x00(STATEP)
632 movdqu STATE2, 0x10(STATEP)
633 movdqu STATE3, 0x20(STATEP)
634 movdqu STATE4, 0x30(STATEP)
635 movdqu STATE0, 0x40(STATEP)
640 movdqu STATE0, 0x00(STATEP)
641 movdqu STATE1, 0x10(STATEP)
642 movdqu STATE2, 0x20(STATEP)
643 movdqu STATE3, 0x30(STATEP)
644 movdqu STATE4, 0x40(STATEP)
651 SYM_FUNC_END(crypto_aegis128_aesni_dec)
654 * void crypto_aegis128_aesni_dec_tail(void *state, unsigned int length,
655 * const void *src, void *dst);
657 SYM_TYPED_FUNC_START(crypto_aegis128_aesni_dec_tail)
660 /* load the state: */
661 movdqu 0x00(STATEP), STATE0
662 movdqu 0x10(STATEP), STATE1
663 movdqu 0x20(STATEP), STATE2
664 movdqu 0x30(STATEP), STATE3
665 movdqu 0x40(STATEP), STATE4
667 /* decrypt message: */
679 /* mask with byte count: */
685 movdqa .Laegis128_counter(%rip), T1
692 /* store the state: */
693 movdqu STATE4, 0x00(STATEP)
694 movdqu STATE0, 0x10(STATEP)
695 movdqu STATE1, 0x20(STATEP)
696 movdqu STATE2, 0x30(STATEP)
697 movdqu STATE3, 0x40(STATEP)
701 SYM_FUNC_END(crypto_aegis128_aesni_dec_tail)
704 * void crypto_aegis128_aesni_final(void *state, void *tag_xor,
705 * u64 assoclen, u64 cryptlen);
707 SYM_FUNC_START(crypto_aegis128_aesni_final)
710 /* load the state: */
711 movdqu 0x00(STATEP), STATE0
712 movdqu 0x10(STATEP), STATE1
713 movdqu 0x20(STATEP), STATE2
714 movdqu 0x30(STATEP), STATE3
715 movdqu 0x40(STATEP), STATE4
717 /* prepare length block: */
722 psllq $3, MSG /* multiply by 8 (to get bit count) */
727 aegis128_update; pxor MSG, STATE4
728 aegis128_update; pxor MSG, STATE3
729 aegis128_update; pxor MSG, STATE2
730 aegis128_update; pxor MSG, STATE1
731 aegis128_update; pxor MSG, STATE0
732 aegis128_update; pxor MSG, STATE4
733 aegis128_update; pxor MSG, STATE3
748 SYM_FUNC_END(crypto_aegis128_aesni_final)