Skip to content

Commit 3d3a170

Browse files
fabioaiutogregkh
authored andcommitted
staging: rtl8723bs: use in-kernel aes encryption
replace private aes encryption subroutines with public in-kernel ones in data frame encryption. Signed-off-by: Fabio Aiuto <[email protected]> Link: https://lore.kernel.org/r/bd1f47994d3a1d0f434de5b2095c327f5a25f6ef.1620139727.git.fabioaiuto83@gmail.com Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 7d40753 commit 3d3a170

File tree

1 file changed

+5
-207
lines changed

1 file changed

+5
-207
lines changed

drivers/staging/rtl8723bs/core/rtw_security.c

Lines changed: 5 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -748,44 +748,6 @@ u32 rtw_tkip_decrypt(struct adapter *padapter, u8 *precvframe)
748748

749749

750750
#define MAX_MSG_SIZE 2048
751-
/*****************************/
752-
/******** SBOX Table *********/
753-
/*****************************/
754-
755-
static const u8 sbox_table[256] = {
756-
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
757-
0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
758-
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
759-
0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
760-
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
761-
0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
762-
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
763-
0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
764-
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
765-
0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
766-
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
767-
0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
768-
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
769-
0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
770-
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
771-
0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
772-
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
773-
0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
774-
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
775-
0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
776-
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
777-
0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
778-
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
779-
0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
780-
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
781-
0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
782-
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
783-
0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
784-
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
785-
0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
786-
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
787-
0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
788-
};
789751

790752
/*****************************/
791753
/**** Function Prototypes ****/
@@ -814,13 +776,7 @@ static void construct_ctr_preload(u8 *ctr_preload,
814776
u8 *pn_vector,
815777
signed int c,
816778
uint frtype); /* for CONFIG_IEEE80211W, none 11w also can use */
817-
static void xor_128(u8 *a, u8 *b, u8 *out);
818-
static void xor_32(u8 *a, u8 *b, u8 *out);
819-
static u8 sbox(u8 a);
820-
static void next_key(u8 *key, signed int round);
821-
static void byte_sub(u8 *in, u8 *out);
822-
static void shift_row(u8 *in, u8 *out);
823-
static void mix_column(u8 *in, u8 *out);
779+
824780
static void aes128k128d(u8 *key, u8 *data, u8 *ciphertext);
825781

826782

@@ -829,171 +785,13 @@ static void aes128k128d(u8 *key, u8 *data, u8 *ciphertext);
829785
/* Performs a 128 bit AES encrypt with */
830786
/* 128 bit data. */
831787
/****************************************/
832-
static void xor_128(u8 *a, u8 *b, u8 *out)
833-
{
834-
signed int i;
835-
836-
for (i = 0; i < 16; i++)
837-
out[i] = a[i] ^ b[i];
838-
}
839-
840-
841-
static void xor_32(u8 *a, u8 *b, u8 *out)
842-
{
843-
signed int i;
844-
845-
for (i = 0; i < 4; i++)
846-
out[i] = a[i] ^ b[i];
847-
}
848-
849-
850-
static u8 sbox(u8 a)
851-
{
852-
return sbox_table[(signed int)a];
853-
}
854-
855-
856-
static void next_key(u8 *key, signed int round)
857-
{
858-
u8 rcon;
859-
u8 sbox_key[4];
860-
static const u8 rcon_table[12] = {
861-
0x01, 0x02, 0x04, 0x08,
862-
0x10, 0x20, 0x40, 0x80,
863-
0x1b, 0x36, 0x36, 0x36
864-
};
865-
sbox_key[0] = sbox(key[13]);
866-
sbox_key[1] = sbox(key[14]);
867-
sbox_key[2] = sbox(key[15]);
868-
sbox_key[3] = sbox(key[12]);
869-
870-
rcon = rcon_table[round];
871-
872-
xor_32(&key[0], sbox_key, &key[0]);
873-
key[0] = key[0] ^ rcon;
874-
875-
xor_32(&key[4], &key[0], &key[4]);
876-
xor_32(&key[8], &key[4], &key[8]);
877-
xor_32(&key[12], &key[8], &key[12]);
878-
}
879-
880-
881-
static void byte_sub(u8 *in, u8 *out)
882-
{
883-
signed int i;
884-
885-
for (i = 0; i < 16; i++)
886-
out[i] = sbox(in[i]);
887-
}
888-
889-
890-
static void shift_row(u8 *in, u8 *out)
891-
{
892-
out[0] = in[0];
893-
out[1] = in[5];
894-
out[2] = in[10];
895-
out[3] = in[15];
896-
out[4] = in[4];
897-
out[5] = in[9];
898-
out[6] = in[14];
899-
out[7] = in[3];
900-
out[8] = in[8];
901-
out[9] = in[13];
902-
out[10] = in[2];
903-
out[11] = in[7];
904-
out[12] = in[12];
905-
out[13] = in[1];
906-
out[14] = in[6];
907-
out[15] = in[11];
908-
}
909-
910-
static void mix_column(u8 *in, u8 *out)
911-
{
912-
signed int i;
913-
u8 add1b[4];
914-
u8 add1bf7[4];
915-
u8 rotl[4];
916-
u8 swap_halfs[4];
917-
u8 andf7[4];
918-
u8 rotr[4];
919-
u8 temp[4];
920-
u8 tempb[4];
921-
922-
for (i = 0; i < 4; i++) {
923-
if ((in[i] & 0x80) == 0x80)
924-
add1b[i] = 0x1b;
925-
else
926-
add1b[i] = 0x00;
927-
}
928-
929-
swap_halfs[0] = in[2]; /* Swap halfs */
930-
swap_halfs[1] = in[3];
931-
swap_halfs[2] = in[0];
932-
swap_halfs[3] = in[1];
933-
934-
rotl[0] = in[3]; /* Rotate left 8 bits */
935-
rotl[1] = in[0];
936-
rotl[2] = in[1];
937-
rotl[3] = in[2];
938-
939-
andf7[0] = in[0] & 0x7f;
940-
andf7[1] = in[1] & 0x7f;
941-
andf7[2] = in[2] & 0x7f;
942-
andf7[3] = in[3] & 0x7f;
943-
944-
for (i = 3; i > 0; i--) { /* logical shift left 1 bit */
945-
andf7[i] = andf7[i] << 1;
946-
if ((andf7[i-1] & 0x80) == 0x80)
947-
andf7[i] = (andf7[i] | 0x01);
948-
}
949-
andf7[0] = andf7[0] << 1;
950-
andf7[0] = andf7[0] & 0xfe;
951-
952-
xor_32(add1b, andf7, add1bf7);
953-
954-
xor_32(in, add1bf7, rotr);
955-
956-
temp[0] = rotr[0]; /* Rotate right 8 bits */
957-
rotr[0] = rotr[1];
958-
rotr[1] = rotr[2];
959-
rotr[2] = rotr[3];
960-
rotr[3] = temp[0];
961-
962-
xor_32(add1bf7, rotr, temp);
963-
xor_32(swap_halfs, rotl, tempb);
964-
xor_32(temp, tempb, out);
965-
}
966-
967788
static void aes128k128d(u8 *key, u8 *data, u8 *ciphertext)
968789
{
969-
signed int round;
970-
signed int i;
971-
u8 intermediatea[16];
972-
u8 intermediateb[16];
973-
u8 round_key[16];
790+
struct crypto_aes_ctx ctx;
974791

975-
for (i = 0; i < 16; i++)
976-
round_key[i] = key[i];
977-
978-
for (round = 0; round < 11; round++) {
979-
if (round == 0) {
980-
xor_128(round_key, data, ciphertext);
981-
next_key(round_key, round);
982-
} else if (round == 10) {
983-
byte_sub(ciphertext, intermediatea);
984-
shift_row(intermediatea, intermediateb);
985-
xor_128(intermediateb, round_key, ciphertext);
986-
} else { /* 1 - 9 */
987-
byte_sub(ciphertext, intermediatea);
988-
shift_row(intermediatea, intermediateb);
989-
mix_column(&intermediateb[0], &intermediatea[0]);
990-
mix_column(&intermediateb[4], &intermediatea[4]);
991-
mix_column(&intermediateb[8], &intermediatea[8]);
992-
mix_column(&intermediateb[12], &intermediatea[12]);
993-
xor_128(intermediatea, round_key, ciphertext);
994-
next_key(round_key, round);
995-
}
996-
}
792+
aes_expandkey(&ctx, key, 16);
793+
aes_encrypt(&ctx, ciphertext, data);
794+
memzero_explicit(&ctx, sizeof(ctx));
997795
}
998796

999797
/************************************************/

0 commit comments

Comments
 (0)