summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/aes/aes_core.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/aes/aes_core.c')
-rw-r--r--src/lib/libcrypto/aes/aes_core.c101
1 files changed, 28 insertions, 73 deletions
diff --git a/src/lib/libcrypto/aes/aes_core.c b/src/lib/libcrypto/aes/aes_core.c
index 4383d74903..8eccb998d3 100644
--- a/src/lib/libcrypto/aes/aes_core.c
+++ b/src/lib/libcrypto/aes/aes_core.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: aes_core.c,v 1.25 2024/11/13 21:00:57 tb Exp $ */ 1/* $OpenBSD: aes_core.c,v 1.27 2025/04/21 12:23:09 jsing Exp $ */
2/** 2/**
3 * rijndael-alg-fst.c 3 * rijndael-alg-fst.c
4 * 4 *
@@ -30,7 +30,7 @@
30 * compatible API. 30 * compatible API.
31 */ 31 */
32 32
33#include <stdlib.h> 33#include <stdint.h>
34 34
35#include <openssl/aes.h> 35#include <openssl/aes.h>
36 36
@@ -55,7 +55,7 @@ Td4[x] = Si[x].[01];
55 !defined(HAVE_AES_SET_DECRYPT_KEY_INTERNAL) || \ 55 !defined(HAVE_AES_SET_DECRYPT_KEY_INTERNAL) || \
56 !defined(HAVE_AES_ENCRYPT_INTERNAL) || \ 56 !defined(HAVE_AES_ENCRYPT_INTERNAL) || \
57 !defined(HAVE_AES_DECRYPT_INTERNAL) 57 !defined(HAVE_AES_DECRYPT_INTERNAL)
58static const u32 Te0[256] = { 58static const uint32_t Te0[256] = {
59 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, 59 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
60 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, 60 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
61 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, 61 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
@@ -121,7 +121,7 @@ static const u32 Te0[256] = {
121 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, 121 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U,
122 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, 122 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU,
123}; 123};
124static const u32 Te1[256] = { 124static const uint32_t Te1[256] = {
125 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, 125 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU,
126 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, 126 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U,
127 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, 127 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU,
@@ -187,7 +187,7 @@ static const u32 Te1[256] = {
187 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, 187 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU,
188 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, 188 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U,
189}; 189};
190static const u32 Te2[256] = { 190static const uint32_t Te2[256] = {
191 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, 191 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU,
192 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, 192 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U,
193 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, 193 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU,
@@ -253,7 +253,7 @@ static const u32 Te2[256] = {
253 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, 253 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU,
254 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, 254 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U,
255}; 255};
256static const u32 Te3[256] = { 256static const uint32_t Te3[256] = {
257 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, 257 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U,
258 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, 258 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U,
259 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, 259 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U,
@@ -320,7 +320,7 @@ static const u32 Te3[256] = {
320 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, 320 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU,
321}; 321};
322 322
323static const u32 Td0[256] = { 323static const uint32_t Td0[256] = {
324 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U, 324 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
325 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U, 325 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U,
326 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U, 326 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U,
@@ -386,7 +386,7 @@ static const u32 Td0[256] = {
386 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U, 386 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U,
387 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U, 387 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U,
388}; 388};
389static const u32 Td1[256] = { 389static const uint32_t Td1[256] = {
390 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU, 390 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU,
391 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U, 391 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U,
392 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU, 392 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU,
@@ -452,7 +452,7 @@ static const u32 Td1[256] = {
452 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U, 452 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U,
453 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U, 453 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U,
454}; 454};
455static const u32 Td2[256] = { 455static const uint32_t Td2[256] = {
456 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U, 456 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U,
457 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U, 457 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U,
458 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U, 458 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U,
@@ -518,7 +518,7 @@ static const u32 Td2[256] = {
518 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U, 518 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U,
519 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U, 519 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U,
520}; 520};
521static const u32 Td3[256] = { 521static const uint32_t Td3[256] = {
522 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU, 522 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU,
523 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU, 523 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU,
524 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U, 524 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U,
@@ -588,7 +588,7 @@ static const u32 Td3[256] = {
588 588
589#if !defined(HAVE_AES_ENCRYPT_INTERNAL) || \ 589#if !defined(HAVE_AES_ENCRYPT_INTERNAL) || \
590 !defined(HAVE_AES_DECRYPT_INTERNAL) 590 !defined(HAVE_AES_DECRYPT_INTERNAL)
591static const u8 Td4[256] = { 591static const uint8_t Td4[256] = {
592 0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U, 592 0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U,
593 0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU, 593 0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU,
594 0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U, 594 0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U,
@@ -626,29 +626,24 @@ static const u8 Td4[256] = {
626 626
627#if !defined(HAVE_AES_SET_ENCRYPT_KEY_INTERNAL) || \ 627#if !defined(HAVE_AES_SET_ENCRYPT_KEY_INTERNAL) || \
628 !defined(HAVE_AES_SET_DECRYPT_KEY_INTERNAL) 628 !defined(HAVE_AES_SET_DECRYPT_KEY_INTERNAL)
629static const u32 rcon[] = { 629static const uint32_t rcon[] = {
630 0x01000000, 0x02000000, 0x04000000, 0x08000000, 630 0x01000000, 0x02000000, 0x04000000, 0x08000000,
631 0x10000000, 0x20000000, 0x40000000, 0x80000000, 631 0x10000000, 0x20000000, 0x40000000, 0x80000000,
632 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ 632 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
633}; 633};
634#endif 634#endif
635 635
636#ifdef HAVE_AES_SET_ENCRYPT_KEY_INTERNAL 636#ifndef HAVE_AES_SET_ENCRYPT_KEY_INTERNAL
637int aes_set_encrypt_key_internal(const unsigned char *userKey, const int bits,
638 AES_KEY *key);
639
640#else
641
642/* 637/*
643 * Expand the cipher key into the encryption key schedule. 638 * Expand the cipher key into the encryption key schedule.
644 */ 639 */
645static inline int 640int
646aes_set_encrypt_key_internal(const unsigned char *userKey, const int bits, 641aes_set_encrypt_key_internal(const unsigned char *userKey, const int bits,
647 AES_KEY *key) 642 AES_KEY *key)
648{ 643{
649 u32 *rk; 644 uint32_t *rk;
650 int i = 0; 645 int i = 0;
651 u32 temp; 646 uint32_t temp;
652 647
653 if (!userKey || !key) 648 if (!userKey || !key)
654 return -1; 649 return -1;
@@ -742,28 +737,17 @@ aes_set_encrypt_key_internal(const unsigned char *userKey, const int bits,
742} 737}
743#endif 738#endif
744 739
745int 740#ifndef HAVE_AES_SET_DECRYPT_KEY_INTERNAL
746AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key)
747{
748 return aes_set_encrypt_key_internal(userKey, bits, key);
749}
750LCRYPTO_ALIAS(AES_set_encrypt_key);
751
752#ifdef HAVE_AES_SET_DECRYPT_KEY_INTERNAL
753int aes_set_decrypt_key_internal(const unsigned char *userKey, const int bits,
754 AES_KEY *key);
755
756#else
757/* 741/*
758 * Expand the cipher key into the decryption key schedule. 742 * Expand the cipher key into the decryption key schedule.
759 */ 743 */
760static inline int 744int
761aes_set_decrypt_key_internal(const unsigned char *userKey, const int bits, 745aes_set_decrypt_key_internal(const unsigned char *userKey, const int bits,
762 AES_KEY *key) 746 AES_KEY *key)
763{ 747{
764 u32 *rk; 748 uint32_t *rk;
765 int i, j, status; 749 int i, j, status;
766 u32 temp; 750 uint32_t temp;
767 751
768 /* first, start with an encryption schedule */ 752 /* first, start with an encryption schedule */
769 status = AES_set_encrypt_key(userKey, bits, key); 753 status = AES_set_encrypt_key(userKey, bits, key);
@@ -815,27 +799,16 @@ aes_set_decrypt_key_internal(const unsigned char *userKey, const int bits,
815} 799}
816#endif 800#endif
817 801
818int 802#ifndef HAVE_AES_ENCRYPT_INTERNAL
819AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key)
820{
821 return aes_set_decrypt_key_internal(userKey, bits, key);
822}
823LCRYPTO_ALIAS(AES_set_decrypt_key);
824
825#ifdef HAVE_AES_ENCRYPT_INTERNAL
826void aes_encrypt_internal(const unsigned char *in, unsigned char *out,
827 const AES_KEY *key);
828
829#else
830/* 803/*
831 * Encrypt a single block - in and out can overlap. 804 * Encrypt a single block - in and out can overlap.
832 */ 805 */
833static inline void 806void
834aes_encrypt_internal(const unsigned char *in, unsigned char *out, 807aes_encrypt_internal(const unsigned char *in, unsigned char *out,
835 const AES_KEY *key) 808 const AES_KEY *key)
836{ 809{
837 const u32 *rk; 810 const uint32_t *rk;
838 u32 s0, s1, s2, s3, t0, t1, t2, t3; 811 uint32_t s0, s1, s2, s3, t0, t1, t2, t3;
839#ifndef FULL_UNROLL 812#ifndef FULL_UNROLL
840 int r; 813 int r;
841#endif /* ?FULL_UNROLL */ 814#endif /* ?FULL_UNROLL */
@@ -1018,27 +991,16 @@ aes_encrypt_internal(const unsigned char *in, unsigned char *out,
1018} 991}
1019#endif 992#endif
1020 993
1021void 994#ifndef HAVE_AES_DECRYPT_INTERNAL
1022AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
1023{
1024 aes_encrypt_internal(in, out, key);
1025}
1026LCRYPTO_ALIAS(AES_encrypt);
1027
1028#ifdef HAVE_AES_DECRYPT_INTERNAL
1029void aes_decrypt_internal(const unsigned char *in, unsigned char *out,
1030 const AES_KEY *key);
1031
1032#else
1033/* 995/*
1034 * Decrypt a single block - in and out can overlap. 996 * Decrypt a single block - in and out can overlap.
1035 */ 997 */
1036static inline void 998void
1037aes_decrypt_internal(const unsigned char *in, unsigned char *out, 999aes_decrypt_internal(const unsigned char *in, unsigned char *out,
1038 const AES_KEY *key) 1000 const AES_KEY *key)
1039{ 1001{
1040 const u32 *rk; 1002 const uint32_t *rk;
1041 u32 s0, s1, s2, s3, t0, t1, t2, t3; 1003 uint32_t s0, s1, s2, s3, t0, t1, t2, t3;
1042#ifndef FULL_UNROLL 1004#ifndef FULL_UNROLL
1043 int r; 1005 int r;
1044#endif /* ?FULL_UNROLL */ 1006#endif /* ?FULL_UNROLL */
@@ -1220,10 +1182,3 @@ aes_decrypt_internal(const unsigned char *in, unsigned char *out,
1220 crypto_store_htobe32(&out[3 * 4], s3); 1182 crypto_store_htobe32(&out[3 * 4], s3);
1221} 1183}
1222#endif 1184#endif
1223
1224void
1225AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
1226{
1227 aes_decrypt_internal(in, out, key);
1228}
1229LCRYPTO_ALIAS(AES_decrypt);