summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2024-03-27 11:15:44 +0000
committerjsing <>2024-03-27 11:15:44 +0000
commitaeccd86b593fda3b76aa590b49519a661b121928 (patch)
tree7fd465ca40831e56274be587c3df366f46f93ecc /src
parentf008058829c221e28cae597d1233b666ea9628f9 (diff)
downloadopenbsd-aeccd86b593fda3b76aa590b49519a661b121928.tar.gz
openbsd-aeccd86b593fda3b76aa590b49519a661b121928.tar.bz2
openbsd-aeccd86b593fda3b76aa590b49519a661b121928.zip
Replace GETU32 and PUTU32.
Replace GETU32 with crypto_load_be32toh() and PUTU32 with crypto_store_htobe32(). Make the offset handling cleaner at the same time. ok beck@ joshua@ tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/aes/aes_core.c50
-rw-r--r--src/lib/libcrypto/aes/aes_local.h5
2 files changed, 26 insertions, 29 deletions
diff --git a/src/lib/libcrypto/aes/aes_core.c b/src/lib/libcrypto/aes/aes_core.c
index 6449ca7cfa..bb1006acf1 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.18 2024/03/27 08:24:13 jsing Exp $ */ 1/* $OpenBSD: aes_core.c,v 1.19 2024/03/27 11:15:44 jsing Exp $ */
2/** 2/**
3 * rijndael-alg-fst.c 3 * rijndael-alg-fst.c
4 * 4 *
@@ -643,10 +643,10 @@ AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key)
643 else 643 else
644 key->rounds = 14; 644 key->rounds = 14;
645 645
646 rk[0] = GETU32(userKey); 646 rk[0] = crypto_load_be32toh(&userKey[0 * 4]);
647 rk[1] = GETU32(userKey + 4); 647 rk[1] = crypto_load_be32toh(&userKey[1 * 4]);
648 rk[2] = GETU32(userKey + 8); 648 rk[2] = crypto_load_be32toh(&userKey[2 * 4]);
649 rk[3] = GETU32(userKey + 12); 649 rk[3] = crypto_load_be32toh(&userKey[3 * 4]);
650 if (bits == 128) { 650 if (bits == 128) {
651 while (1) { 651 while (1) {
652 temp = rk[3]; 652 temp = rk[3];
@@ -665,8 +665,8 @@ AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key)
665 rk += 4; 665 rk += 4;
666 } 666 }
667 } 667 }
668 rk[4] = GETU32(userKey + 16); 668 rk[4] = crypto_load_be32toh(&userKey[4 * 4]);
669 rk[5] = GETU32(userKey + 20); 669 rk[5] = crypto_load_be32toh(&userKey[5 * 4]);
670 if (bits == 192) { 670 if (bits == 192) {
671 while (1) { 671 while (1) {
672 temp = rk[5]; 672 temp = rk[5];
@@ -687,8 +687,8 @@ AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key)
687 rk += 6; 687 rk += 6;
688 } 688 }
689 } 689 }
690 rk[6] = GETU32(userKey + 24); 690 rk[6] = crypto_load_be32toh(&userKey[6 * 4]);
691 rk[7] = GETU32(userKey + 28); 691 rk[7] = crypto_load_be32toh(&userKey[7 * 4]);
692 if (bits == 256) { 692 if (bits == 256) {
693 while (1) { 693 while (1) {
694 temp = rk[7]; 694 temp = rk[7];
@@ -799,10 +799,10 @@ AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
799 * map byte array block to cipher state 799 * map byte array block to cipher state
800 * and add initial round key: 800 * and add initial round key:
801 */ 801 */
802 s0 = GETU32(in ) ^ rk[0]; 802 s0 = crypto_load_be32toh(&in[0 * 4]) ^ rk[0];
803 s1 = GETU32(in + 4) ^ rk[1]; 803 s1 = crypto_load_be32toh(&in[1 * 4]) ^ rk[1];
804 s2 = GETU32(in + 8) ^ rk[2]; 804 s2 = crypto_load_be32toh(&in[2 * 4]) ^ rk[2];
805 s3 = GETU32(in + 12) ^ rk[3]; 805 s3 = crypto_load_be32toh(&in[3 * 4]) ^ rk[3];
806#ifdef FULL_UNROLL 806#ifdef FULL_UNROLL
807 /* round 1: */ 807 /* round 1: */
808 t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4]; 808 t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4];
@@ -946,28 +946,28 @@ AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
946 (Te0[(t2 >> 8) & 0xff] & 0x0000ff00) ^ 946 (Te0[(t2 >> 8) & 0xff] & 0x0000ff00) ^
947 (Te1[(t3) & 0xff] & 0x000000ff) ^ 947 (Te1[(t3) & 0xff] & 0x000000ff) ^
948 rk[0]; 948 rk[0];
949 PUTU32(out, s0); 949 crypto_store_htobe32(&out[0 * 4], s0);
950 s1 = 950 s1 =
951 (Te2[(t1 >> 24)] & 0xff000000) ^ 951 (Te2[(t1 >> 24)] & 0xff000000) ^
952 (Te3[(t2 >> 16) & 0xff] & 0x00ff0000) ^ 952 (Te3[(t2 >> 16) & 0xff] & 0x00ff0000) ^
953 (Te0[(t3 >> 8) & 0xff] & 0x0000ff00) ^ 953 (Te0[(t3 >> 8) & 0xff] & 0x0000ff00) ^
954 (Te1[(t0) & 0xff] & 0x000000ff) ^ 954 (Te1[(t0) & 0xff] & 0x000000ff) ^
955 rk[1]; 955 rk[1];
956 PUTU32(out + 4, s1); 956 crypto_store_htobe32(&out[1 * 4], s1);
957 s2 = 957 s2 =
958 (Te2[(t2 >> 24)] & 0xff000000) ^ 958 (Te2[(t2 >> 24)] & 0xff000000) ^
959 (Te3[(t3 >> 16) & 0xff] & 0x00ff0000) ^ 959 (Te3[(t3 >> 16) & 0xff] & 0x00ff0000) ^
960 (Te0[(t0 >> 8) & 0xff] & 0x0000ff00) ^ 960 (Te0[(t0 >> 8) & 0xff] & 0x0000ff00) ^
961 (Te1[(t1) & 0xff] & 0x000000ff) ^ 961 (Te1[(t1) & 0xff] & 0x000000ff) ^
962 rk[2]; 962 rk[2];
963 PUTU32(out + 8, s2); 963 crypto_store_htobe32(&out[2 * 4], s2);
964 s3 = 964 s3 =
965 (Te2[(t3 >> 24)] & 0xff000000) ^ 965 (Te2[(t3 >> 24)] & 0xff000000) ^
966 (Te3[(t0 >> 16) & 0xff] & 0x00ff0000) ^ 966 (Te3[(t0 >> 16) & 0xff] & 0x00ff0000) ^
967 (Te0[(t1 >> 8) & 0xff] & 0x0000ff00) ^ 967 (Te0[(t1 >> 8) & 0xff] & 0x0000ff00) ^
968 (Te1[(t2) & 0xff] & 0x000000ff) ^ 968 (Te1[(t2) & 0xff] & 0x000000ff) ^
969 rk[3]; 969 rk[3];
970 PUTU32(out + 12, s3); 970 crypto_store_htobe32(&out[3 * 4], s3);
971} 971}
972 972
973/* 973/*
@@ -989,10 +989,10 @@ AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
989 * map byte array block to cipher state 989 * map byte array block to cipher state
990 * and add initial round key: 990 * and add initial round key:
991 */ 991 */
992 s0 = GETU32(in) ^ rk[0]; 992 s0 = crypto_load_be32toh(&in[0 * 4]) ^ rk[0];
993 s1 = GETU32(in + 4) ^ rk[1]; 993 s1 = crypto_load_be32toh(&in[1 * 4]) ^ rk[1];
994 s2 = GETU32(in + 8) ^ rk[2]; 994 s2 = crypto_load_be32toh(&in[2 * 4]) ^ rk[2];
995 s3 = GETU32(in + 12) ^ rk[3]; 995 s3 = crypto_load_be32toh(&in[3 * 4]) ^ rk[3];
996#ifdef FULL_UNROLL 996#ifdef FULL_UNROLL
997 /* round 1: */ 997 /* round 1: */
998 t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[ 4]; 998 t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[ 4];
@@ -1136,27 +1136,27 @@ AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
1136 (Td4[(t2 >> 8) & 0xff] << 8) ^ 1136 (Td4[(t2 >> 8) & 0xff] << 8) ^
1137 (Td4[(t1) & 0xff]) ^ 1137 (Td4[(t1) & 0xff]) ^
1138 rk[0]; 1138 rk[0];
1139 PUTU32(out, s0); 1139 crypto_store_htobe32(&out[0 * 4], s0);
1140 s1 = 1140 s1 =
1141 (((uint32_t)Td4[(t1 >> 24)]) << 24) ^ 1141 (((uint32_t)Td4[(t1 >> 24)]) << 24) ^
1142 (Td4[(t0 >> 16) & 0xff] << 16) ^ 1142 (Td4[(t0 >> 16) & 0xff] << 16) ^
1143 (Td4[(t3 >> 8) & 0xff] << 8) ^ 1143 (Td4[(t3 >> 8) & 0xff] << 8) ^
1144 (Td4[(t2) & 0xff]) ^ 1144 (Td4[(t2) & 0xff]) ^
1145 rk[1]; 1145 rk[1];
1146 PUTU32(out + 4, s1); 1146 crypto_store_htobe32(&out[1 * 4], s1);
1147 s2 = 1147 s2 =
1148 (((uint32_t)Td4[(t2 >> 24)]) << 24) ^ 1148 (((uint32_t)Td4[(t2 >> 24)]) << 24) ^
1149 (Td4[(t1 >> 16) & 0xff] << 16) ^ 1149 (Td4[(t1 >> 16) & 0xff] << 16) ^
1150 (Td4[(t0 >> 8) & 0xff] << 8) ^ 1150 (Td4[(t0 >> 8) & 0xff] << 8) ^
1151 (Td4[(t3) & 0xff]) ^ 1151 (Td4[(t3) & 0xff]) ^
1152 rk[2]; 1152 rk[2];
1153 PUTU32(out + 8, s2); 1153 crypto_store_htobe32(&out[2 * 4], s2);
1154 s3 = 1154 s3 =
1155 (((uint32_t)Td4[(t3 >> 24)]) << 24) ^ 1155 (((uint32_t)Td4[(t3 >> 24)]) << 24) ^
1156 (Td4[(t2 >> 16) & 0xff] << 16) ^ 1156 (Td4[(t2 >> 16) & 0xff] << 16) ^
1157 (Td4[(t1 >> 8) & 0xff] << 8) ^ 1157 (Td4[(t1 >> 8) & 0xff] << 8) ^
1158 (Td4[(t0) & 0xff]) ^ 1158 (Td4[(t0) & 0xff]) ^
1159 rk[3]; 1159 rk[3];
1160 PUTU32(out + 12, s3); 1160 crypto_store_htobe32(&out[3 * 4], s3);
1161} 1161}
1162#endif /* AES_ASM */ 1162#endif /* AES_ASM */
diff --git a/src/lib/libcrypto/aes/aes_local.h b/src/lib/libcrypto/aes/aes_local.h
index ef74e71319..3f134408c9 100644
--- a/src/lib/libcrypto/aes/aes_local.h
+++ b/src/lib/libcrypto/aes/aes_local.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: aes_local.h,v 1.2 2022/11/26 17:23:17 tb Exp $ */ 1/* $OpenBSD: aes_local.h,v 1.3 2024/03/27 11:15:44 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -64,9 +64,6 @@
64 64
65__BEGIN_HIDDEN_DECLS 65__BEGIN_HIDDEN_DECLS
66 66
67#define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3]))
68#define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); }
69
70typedef unsigned int u32; 67typedef unsigned int u32;
71typedef unsigned short u16; 68typedef unsigned short u16;
72typedef unsigned char u8; 69typedef unsigned char u8;