diff options
| author | Daniel Anderson <Daniel.Anderson@Sun.COM> | 2008-08-27 15:53:48 -0700 |
|---|---|---|
| committer | Daniel Anderson <Daniel.Anderson@Sun.COM> | 2008-08-27 15:53:48 -0700 |
| commit | 4b56a00321e0ce508e55cc5e43e3ad7b00005a39 (patch) | |
| tree | 0bf9a658189854d8a8f1f0446cf705c58fe30bd9 /usr/src | |
| parent | a588362c0375ebed265afad9dd5a60b5739820df (diff) | |
| download | illumos-joyent-4b56a00321e0ce508e55cc5e43e3ad7b00005a39.tar.gz | |
5007142 Add ntohll and htonll to sys/byteorder.h
6717509 Need to use bswap/bswapq for byte swap of 64-bit integer on x32/x64
PSARC 2008/474 Add 64-bit htonll() and ntohll() byte order conversion functions
Diffstat (limited to 'usr/src')
26 files changed, 644 insertions, 496 deletions
diff --git a/usr/src/cmd/cmd-inet/usr.sbin/ipsecutils/ikeadm.c b/usr/src/cmd/cmd-inet/usr.sbin/ipsecutils/ikeadm.c index a8c02250bc..7fc33db683 100644 --- a/usr/src/cmd/cmd-inet/usr.sbin/ipsecutils/ikeadm.c +++ b/usr/src/cmd/cmd-inet/usr.sbin/ipsecutils/ikeadm.c @@ -22,8 +22,6 @@ * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include <unistd.h> #include <stdio.h> #include <stdarg.h> @@ -376,50 +374,50 @@ parsecmd(char *cmdstr, char *objstr) {"rule", IKE_SVC_GET_RULE}, {"preshared", IKE_SVC_GET_PS}, {"defaults", IKE_SVC_GET_DEFS}, - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"set", IKE_SVC_ERROR, { {"debug", IKE_SVC_SET_DBG}, {"priv", IKE_SVC_SET_PRIV}, - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"add", IKE_SVC_ERROR, { {"rule", IKE_SVC_NEW_RULE}, {"preshared", IKE_SVC_NEW_PS}, - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"del", IKE_SVC_ERROR, { {"p1", IKE_SVC_DEL_P1}, {"rule", IKE_SVC_DEL_RULE}, {"preshared", IKE_SVC_DEL_PS}, - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"dump", IKE_SVC_ERROR, { {"p1", IKE_SVC_DUMP_P1S}, {"rule", IKE_SVC_DUMP_RULES}, {"preshared", IKE_SVC_DUMP_PS}, - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"flush", IKE_SVC_ERROR, { {"p1", IKE_SVC_FLUSH_P1S}, - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"read", IKE_SVC_ERROR, { {"rule", IKE_SVC_READ_RULES}, {"preshared", IKE_SVC_READ_PS}, - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"write", IKE_SVC_ERROR, { {"rule", IKE_SVC_WRITE_RULES}, {"preshared", IKE_SVC_WRITE_PS}, - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"help", IKEADM_HELP_GENERAL, { @@ -432,26 +430,26 @@ parsecmd(char *cmdstr, char *objstr) {"read", IKEADM_HELP_READ}, {"write", IKEADM_HELP_WRITE}, {"help", IKEADM_HELP_HELP}, - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"exit", IKEADM_EXIT, { - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"quit", IKEADM_EXIT, { - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {"dbg", IKE_SVC_ERROR, { {"rbdump", IKE_SVC_DBG_RBDUMP}, - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } }, {NULL, IKE_SVC_ERROR, { - {NULL, IKE_SVC_ERROR}, + {NULL, IKE_SVC_ERROR} } - }, + } }; struct cmdtbl *ct = table; struct objtbl *ot; @@ -1138,17 +1136,6 @@ bail: return (-1); } -/* stolen from libdhcputil (dhcp_inittab.c) */ -static uint64_t -ike_ntohll(uint64_t nll) -{ -#ifdef _LITTLE_ENDIAN - return ((uint64_t)ntohl(nll & 0xffffffff) << 32 | ntohl(nll >> 32)); -#else - return (nll); -#endif -} - /* * Printing functions * @@ -1158,11 +1145,10 @@ ike_ntohll(uint64_t nll) * the ipsec_util.c functions actually print the string to the file named * in the second arg to the function (and are named dump_foo). * - * The reason for this is that in the context of the ikeadm output, it - * seemed like the localization of the text would be more straightforward - * (and could more easily accomodate non-english grammar!) if more complete - * phrases were being translated, rather than a bit of a phrase followed by - * a call to dump_foo() followed by more of the phrase. + * Localization for ikeadm seems more straightforward when complete + * phrases are translated rather than: a part of a phrase, a call to + * dump_foo(), and more of the phrase. It could also accommodate + * non-English grammar more easily. */ static char * @@ -1393,8 +1379,8 @@ print_hdr(char *prefix, ike_p1_hdr_t *hdrp) { (void) printf( gettext("%s Cookies: Initiator 0x%llx Responder 0x%llx\n"), - prefix, ike_ntohll(hdrp->p1hdr_cookies.cky_i), - ike_ntohll(hdrp->p1hdr_cookies.cky_r)); + prefix, ntohll(hdrp->p1hdr_cookies.cky_i), + ntohll(hdrp->p1hdr_cookies.cky_r)); (void) printf(gettext("%s The local host is the %s.\n"), prefix, hdrp->p1hdr_isinit ? gettext("initiator") : gettext("responder")); (void) printf(gettext("%s ISAKMP version %d.%d; %s exchange\n"), prefix, diff --git a/usr/src/cmd/iscsi/iscsitgtd/t10_spc.h b/usr/src/cmd/iscsi/iscsitgtd/t10_spc.h index 7a7e7bb62e..be93c43e29 100644 --- a/usr/src/cmd/iscsi/iscsitgtd/t10_spc.h +++ b/usr/src/cmd/iscsi/iscsitgtd/t10_spc.h @@ -27,8 +27,6 @@ #ifndef _T10_SPC_H #define _T10_SPC_H -#pragma ident "%Z%%M% %I% %E% SMI" - #ifdef __cplusplus extern "C" { #endif @@ -46,6 +44,8 @@ extern "C" { #define FIXED_SENSE_ADDL_INFO_LEN 0xFFFFFFFF #define INFORMATION_SENSE_DESCR sizeof (struct scsi_information_sense_descr) +#include <sys/types.h> +#include <netinet/in.h> #include <sys/scsi/generic/inquiry.h> #include <sys/scsi/generic/mode.h> @@ -84,15 +84,6 @@ Boolean_t spc_encode_lu_addr(uint8_t *buf, int select_field, uint32_t lun); #define SPC_SENSE_FM 0x02 #define SPC_SENSE_ILI 0x04 -#ifdef _BIG_ENDIAN -#define htonll(x) (x) -#define ntohll(x) (x) -#else -#define htonll(x) ((((unsigned long long)htonl(x & 0xffffffff)) << 32) + \ - htonl(x >> 32)) -#define ntohll(x) ((((unsigned long long)ntohl(x)) << 32) + ntohl(x >> 32)) -#endif - /* * []------------------------------------------------------------------[] * | SPC-3, revision 21c -- ASC/ASCQ values | diff --git a/usr/src/common/crypto/aes/aes_impl.c b/usr/src/common/crypto/aes/aes_impl.c index 4257a134cd..94888462c2 100644 --- a/usr/src/common/crypto/aes/aes_impl.c +++ b/usr/src/common/crypto/aes/aes_impl.c @@ -23,8 +23,6 @@ * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include <sys/types.h> #include <sys/systm.h> #include <sys/ddi.h> @@ -1490,10 +1488,7 @@ aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits, void *keysched) #else /* byte swap */ for (i = 0, j = 0; j < keysize; i++, j += 4) { - keyarr.ka32[i] = (((uint32_t)cipherKey[j] << 24) | - ((uint32_t)cipherKey[j + 1] << 16) | - ((uint32_t)cipherKey[j + 2] << 8) | - (uint32_t)cipherKey[j + 3]); + keyarr.ka32[i] = htonl(*(uint32_t *)&cipherKey[j]); } #endif @@ -1530,15 +1525,10 @@ aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct) bcopy(pt, &buffer, AES_BLOCK_LEN); #else /* byte swap */ - buffer[0] = (((uint32_t)pt[0] << 24) | ((uint32_t)pt[1] << 16) | - ((uint32_t)pt[2] << 8) | (uint32_t)pt[3]); - buffer[1] = (((uint32_t)pt[4] << 24) | ((uint32_t)pt[5] << 16) | - ((uint32_t)pt[6] << 8) | (uint32_t)pt[7]); - buffer[2] = (((uint32_t)pt[8] << 24) | ((uint32_t)pt[9] << 16) | - ((uint32_t)pt[10] << 8) | (uint32_t)pt[11]); - buffer[3] = (((uint32_t)pt[12] << 24) | - ((uint32_t)pt[13] << 16) | ((uint32_t)pt[14] << 8) | - (uint32_t)pt[15]); + buffer[0] = htonl(*(uint32_t *)&pt[0]); + buffer[1] = htonl(*(uint32_t *)&pt[4]); + buffer[2] = htonl(*(uint32_t *)&pt[8]); + buffer[3] = htonl(*(uint32_t *)&pt[12]); #endif AES_ENCRYPT_IMPL(&ksch->encr_ks.ks32[0], ksch->nr, @@ -1550,22 +1540,10 @@ aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct) } #else /* byte swap */ - ct[0] = buffer[0] >> 24; - ct[1] = buffer[0] >> 16; - ct[2] = buffer[0] >> 8; - ct[3] = (uint8_t)buffer[0]; - ct[4] = buffer[1] >> 24; - ct[5] = buffer[1] >> 16; - ct[6] = buffer[1] >> 8; - ct[7] = (uint8_t)buffer[1]; - ct[8] = buffer[2] >> 24; - ct[9] = buffer[2] >> 16; - ct[10] = buffer[2] >> 8; - ct[11] = (uint8_t)buffer[2]; - ct[12] = buffer[3] >> 24; - ct[13] = buffer[3] >> 16; - ct[14] = buffer[3] >> 8; - ct[15] = (uint8_t)buffer[3]; + *(uint32_t *)&ct[0] = htonl(buffer[0]); + *(uint32_t *)&ct[4] = htonl(buffer[1]); + *(uint32_t *)&ct[8] = htonl(buffer[2]); + *(uint32_t *)&ct[12] = htonl(buffer[3]); #endif /* EXPORT DELETE END */ return (CRYPTO_SUCCESS); @@ -1600,18 +1578,10 @@ aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt) bcopy(ct, &buffer, AES_BLOCK_LEN); #else /* byte swap */ - buffer[0] = (((uint32_t)ct[0] << 24) | ((uint32_t)ct[1] << 16) | - ((uint32_t)ct[2] << 8) | (uint32_t)ct[3]); - - buffer[1] = (((uint32_t)ct[4] << 24) | ((uint32_t)ct[5] << 16) | - ((uint32_t)ct[6] << 8) | (uint32_t)ct[7]); - - buffer[2] = (((uint32_t)ct[8] << 24) | ((uint32_t)ct[9] << 16) | - ((uint32_t)ct[10] << 8) | (uint32_t)ct[11]); - - buffer[3] = (((uint32_t)ct[12] << 24) | - ((uint32_t)ct[13] << 16) | ((uint32_t)ct[14] << 8) | - (uint32_t)ct[15]); + buffer[0] = htonl(*(uint32_t *)&ct[0]); + buffer[1] = htonl(*(uint32_t *)&ct[4]); + buffer[2] = htonl(*(uint32_t *)&ct[8]); + buffer[3] = htonl(*(uint32_t *)&ct[12]); #endif AES_DECRYPT_IMPL(&ksch->decr_ks.ks32[0], ksch->nr, @@ -1623,22 +1593,10 @@ aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt) } #else /* byte swap */ - pt[0] = buffer[0] >> 24; - pt[1] = buffer[0] >> 16; - pt[2] = buffer[0] >> 8; - pt[3] = (uint8_t)buffer[0]; - pt[4] = buffer[1] >> 24; - pt[5] = buffer[1] >> 16; - pt[6] = buffer[1] >> 8; - pt[7] = (uint8_t)buffer[1]; - pt[8] = buffer[2] >> 24; - pt[9] = buffer[2] >> 16; - pt[10] = buffer[2] >> 8; - pt[11] = (uint8_t)buffer[2]; - pt[12] = buffer[3] >> 24; - pt[13] = buffer[3] >> 16; - pt[14] = buffer[3] >> 8; - pt[15] = (uint8_t)buffer[3]; + *(uint32_t *)&pt[0] = htonl(buffer[0]); + *(uint32_t *)&pt[4] = htonl(buffer[1]); + *(uint32_t *)&pt[8] = htonl(buffer[2]); + *(uint32_t *)&pt[12] = htonl(buffer[3]); #endif /* EXPORT DELETE END */ diff --git a/usr/src/common/crypto/aes/amd64/aesopt.h b/usr/src/common/crypto/aes/amd64/aesopt.h index 254fbd3521..169c91bf19 100644 --- a/usr/src/common/crypto/aes/amd64/aesopt.h +++ b/usr/src/common/crypto/aes/amd64/aesopt.h @@ -54,20 +54,20 @@ * from brg_endian.h * 5. Undefined VIA_ACE_POSSIBLE and ASSUME_VIA_ACE_PRESENT * 6. Changed uint_8t and uint_32t to uint8_t and uint32_t - * 7. cstyled and hdrchk code + * 7. Defined aes_sw32 as htonl() for byte swapping + * 8. Cstyled and hdrchk code * */ #ifndef _AESOPT_H #define _AESOPT_H -#pragma ident "%Z%%M% %I% %E% SMI" - #ifdef __cplusplus extern "C" { #endif #include <sys/types.h> +#include <sys/byteorder.h> #include <aes_impl.h> /* SUPPORT FEATURES */ @@ -521,17 +521,20 @@ extern "C" { #define DEC_UNROLL NONE #endif -#if defined(bswap32) +#if (ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN) +#define aes_sw32 htonl +#elif defined(bswap32) #define aes_sw32 bswap32 #elif defined(bswap_32) #define aes_sw32 bswap_32 #else -#define brot(x, n) (((uint32_t)(x) << n) | ((uint32_t)(x) >> (32 - n))) +#define brot(x, n) (((uint32_t)(x) << (n)) | ((uint32_t)(x) >> (32 - (n)))) #define aes_sw32(x) ((brot((x), 8) & 0x00ff00ff) | (brot((x), 24) & 0xff00ff00)) #endif + /* - * upr(x, n): rotates bytes within words by n positions, moving bytes to + * upr(x, n): rotates bytes within words by n positions, moving bytes to * higher index positions with wrap around into low positions * ups(x, n): moves bytes by n positions to higher index positions in * words but without wrap around diff --git a/usr/src/common/crypto/blowfish/blowfish_impl.c b/usr/src/common/crypto/blowfish/blowfish_impl.c index 22c93ab92a..b760b1c828 100644 --- a/usr/src/common/crypto/blowfish/blowfish_impl.c +++ b/usr/src/common/crypto/blowfish/blowfish_impl.c @@ -23,8 +23,6 @@ * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - /* * Blowfish encryption/decryption and keyschedule code. */ @@ -50,9 +48,13 @@ #include <strings.h> #include <stdlib.h> #define BLOWFISH_ASSERT(x) - #endif /* _KERNEL */ +#if defined(__i386) || defined(__amd64) +#include <sys/byteorder.h> +#define UNALIGNED_POINTERS_PERMITTED +#endif + /* EXPORT DELETE START */ /* @@ -390,11 +392,16 @@ blowfish_encrypt_block(const void *cookie, const uint8_t *block, b32 = (uint32_t *)block; left = b32[0]; right = b32[1]; - } else { + } else #endif + { /* * Read input block and place in left/right in big-endian order. */ +#ifdef UNALIGNED_POINTERS_PERMITTED + left = htonl(*(uint32_t *)&block[0]); + right = htonl(*(uint32_t *)&block[4]); +#else left = ((uint32_t)block[0] << 24) | ((uint32_t)block[1] << 16) | ((uint32_t)block[2] << 8) @@ -403,9 +410,8 @@ blowfish_encrypt_block(const void *cookie, const uint8_t *block, | ((uint32_t)block[5] << 16) | ((uint32_t)block[6] << 8) | (uint32_t)block[7]; -#ifdef _BIG_ENDIAN +#endif /* UNALIGNED_POINTERS_PERMITTED */ } -#endif ROUND(left, right, 0); ROUND(left, right, 1); @@ -436,20 +442,24 @@ blowfish_encrypt_block(const void *cookie, const uint8_t *block, b32 = (uint32_t *)out_block; b32[0] = left; b32[1] = right; - } else { + } else #endif - /* Put the block back into the user's block with final swap */ - out_block[0] = left >> 24; - out_block[1] = left >> 16; - out_block[2] = left >> 8; - out_block[3] = left; - out_block[4] = right >> 24; - out_block[5] = right >> 16; - out_block[6] = right >> 8; - out_block[7] = right; -#ifdef _BIG_ENDIAN + { + /* Put the block back into the user's block with final swap */ +#ifdef UNALIGNED_POINTERS_PERMITTED + *(uint32_t *)&out_block[0] = htonl(left); + *(uint32_t *)&out_block[4] = htonl(right); +#else + out_block[0] = left >> 24; + out_block[1] = left >> 16; + out_block[2] = left >> 8; + out_block[3] = left; + out_block[4] = right >> 24; + out_block[5] = right >> 16; + out_block[6] = right >> 8; + out_block[7] = right; +#endif /* UNALIGNED_POINTERS_PERMITTED */ } -#endif /* EXPORT DELETE END */ return (CRYPTO_SUCCESS); } @@ -479,11 +489,16 @@ blowfish_decrypt_block(const void *cookie, const uint8_t *block, b32 = (uint32_t *)block; left = b32[0]; right = b32[1]; - } else { + } else #endif + { /* * Read input block and place in left/right in big-endian order. */ +#ifdef UNALIGNED_POINTERS_PERMITTED + left = htonl(*(uint32_t *)&block[0]); + right = htonl(*(uint32_t *)&block[4]); +#else left = ((uint32_t)block[0] << 24) | ((uint32_t)block[1] << 16) | ((uint32_t)block[2] << 8) @@ -492,9 +507,8 @@ blowfish_decrypt_block(const void *cookie, const uint8_t *block, | ((uint32_t)block[5] << 16) | ((uint32_t)block[6] << 8) | (uint32_t)block[7]; -#ifdef _BIG_ENDIAN +#endif /* UNALIGNED_POINTERS_PERMITTED */ } -#endif ROUND(left, right, 17); ROUND(left, right, 16); @@ -525,20 +539,24 @@ blowfish_decrypt_block(const void *cookie, const uint8_t *block, b32 = (uint32_t *)out_block; b32[0] = left; b32[1] = right; - } else { + } else #endif + { /* Put the block back into the user's block with final swap */ - out_block[0] = left >> 24; - out_block[1] = left >> 16; - out_block[2] = left >> 8; - out_block[3] = left; - out_block[4] = right >> 24; - out_block[5] = right >> 16; - out_block[6] = right >> 8; - out_block[7] = right; -#ifdef _BIG_ENDIAN +#ifdef UNALIGNED_POINTERS_PERMITTED + *(uint32_t *)&out_block[0] = htonl(left); + *(uint32_t *)&out_block[4] = htonl(right); +#else + out_block[0] = left >> 24; + out_block[1] = left >> 16; + out_block[2] = left >> 8; + out_block[3] = left; + out_block[4] = right >> 24; + out_block[5] = right >> 16; + out_block[6] = right >> 8; + out_block[7] = right; +#endif /* UNALIGNED_POINTERS_PERMITTED */ } -#endif /* EXPORT DELETE END */ return (CRYPTO_SUCCESS); } diff --git a/usr/src/common/crypto/des/des_impl.c b/usr/src/common/crypto/des/des_impl.c index f69ebd88b0..3590d362ee 100644 --- a/usr/src/common/crypto/des/des_impl.c +++ b/usr/src/common/crypto/des/des_impl.c @@ -23,8 +23,6 @@ * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include <sys/types.h> #include <sys/systm.h> #include <sys/ddi.h> @@ -39,6 +37,11 @@ #include <stdlib.h> #endif /* !_KERNEL */ +#if defined(__i386) || defined(__amd64) +#include <sys/byteorder.h> +#define UNALIGNED_POINTERS_PERMITTED +#endif + /* EXPORT DELETE START */ typedef struct keysched_s { @@ -518,29 +521,35 @@ des3_crunch_block(const void *cookie, const uint8_t block[DES_BLOCK_LEN], if (decrypt == B_TRUE) /* LINTED */ *(uint64_t *)out_block = des_crypt_impl( - ksch->ksch_decrypt, - /* LINTED */ + ksch->ksch_decrypt, /* LINTED */ *(uint64_t *)block, 3); else /* LINTED */ *(uint64_t *)out_block = des_crypt_impl( - ksch->ksch_encrypt, - /* LINTED */ + ksch->ksch_encrypt, /* LINTED */ *(uint64_t *)block, 3); - } else { -#endif + } else +#endif /* _BIG_ENDIAN */ + { uint64_t tmp; +#ifdef UNALIGNED_POINTERS_PERMITTED + tmp = htonll(*(uint64_t *)&block[0]); +#else tmp = (((uint64_t)block[0] << 56) | ((uint64_t)block[1] << 48) | ((uint64_t)block[2] << 40) | ((uint64_t)block[3] << 32) | ((uint64_t)block[4] << 24) | ((uint64_t)block[5] << 16) | ((uint64_t)block[6] << 8) | (uint64_t)block[7]); +#endif /* UNALIGNED_POINTERS_PERMITTED */ if (decrypt == B_TRUE) tmp = des_crypt_impl(ksch->ksch_decrypt, tmp, 3); else tmp = des_crypt_impl(ksch->ksch_encrypt, tmp, 3); +#ifdef UNALIGNED_POINTERS_PERMITTED + *(uint64_t *)&out_block[0] = htonll(tmp); +#else out_block[0] = tmp >> 56; out_block[1] = tmp >> 48; out_block[2] = tmp >> 40; @@ -549,9 +558,8 @@ des3_crunch_block(const void *cookie, const uint8_t block[DES_BLOCK_LEN], out_block[5] = tmp >> 16; out_block[6] = tmp >> 8; out_block[7] = (uint8_t)tmp; -#ifdef _BIG_ENDIAN +#endif /* UNALIGNED_POINTERS_PERMITTED */ } -#endif /* EXPORT DELETE END */ return (CRYPTO_SUCCESS); } @@ -574,30 +582,37 @@ des_crunch_block(const void *cookie, const uint8_t block[DES_BLOCK_LEN], if (decrypt == B_TRUE) /* LINTED */ *(uint64_t *)out_block = des_crypt_impl( - ksch->ksch_decrypt, - /* LINTED */ + ksch->ksch_decrypt, /* LINTED */ *(uint64_t *)block, 1); else /* LINTED */ *(uint64_t *)out_block = des_crypt_impl( - ksch->ksch_encrypt, - /* LINTED */ + ksch->ksch_encrypt, /* LINTED */ *(uint64_t *)block, 1); - } else { -#endif + } else +#endif /* _BIG_ENDIAN */ + { uint64_t tmp; +#ifdef UNALIGNED_POINTERS_PERMITTED + tmp = htonll(*(uint64_t *)&block[0]); +#else tmp = (((uint64_t)block[0] << 56) | ((uint64_t)block[1] << 48) | ((uint64_t)block[2] << 40) | ((uint64_t)block[3] << 32) | ((uint64_t)block[4] << 24) | ((uint64_t)block[5] << 16) | ((uint64_t)block[6] << 8) | (uint64_t)block[7]); +#endif /* UNALIGNED_POINTERS_PERMITTED */ + if (decrypt == B_TRUE) tmp = des_crypt_impl(ksch->ksch_decrypt, tmp, 1); else tmp = des_crypt_impl(ksch->ksch_encrypt, tmp, 1); +#ifdef UNALIGNED_POINTERS_PERMITTED + *(uint64_t *)&out_block[0] = htonll(tmp); +#else out_block[0] = tmp >> 56; out_block[1] = tmp >> 48; out_block[2] = tmp >> 40; @@ -606,9 +621,8 @@ des_crunch_block(const void *cookie, const uint8_t block[DES_BLOCK_LEN], out_block[5] = tmp >> 16; out_block[6] = tmp >> 8; out_block[7] = (uint8_t)tmp; -#ifdef _BIG_ENDIAN +#endif /* UNALIGNED_POINTERS_PERMITTED */ } -#endif /* EXPORT DELETE END */ return (CRYPTO_SUCCESS); } @@ -650,7 +664,7 @@ keycheck(uint8_t *key, uint8_t *corrected_key) 0x1ffe1ffe0efe0efeULL, 0xfe1ffe1ffe0efe0eULL, 0x011f011f010e010eULL, 0x1f011f010e010e01ULL, 0xe0fee0fef1fef1feULL, 0xfee0fee0fef1fef1ULL, -#endif +#endif /* _LITTLE_ENDIAN */ /* We'll save the other possibly-weak keys for the future. */ }; @@ -658,6 +672,9 @@ keycheck(uint8_t *key, uint8_t *corrected_key) if (key == NULL) return (B_FALSE); +#ifdef UNALIGNED_POINTERS_PERMITTED + key_so_far = htonll(*(uint64_t *)&key[0]); +#else /* * The code below reverses the bytes on LITTLE_ENDIAN machines. * On BIG_ENDIAN, the same code copies without reversing @@ -667,6 +684,7 @@ keycheck(uint8_t *key, uint8_t *corrected_key) ((uint64_t)key[2] << 40) | ((uint64_t)key[3] << 32) | ((uint64_t)key[4] << 24) | ((uint64_t)key[5] << 16) | ((uint64_t)key[6] << 8) | (uint64_t)key[7]); +#endif /* UNALIGNED_POINTERS_PERMITTED */ /* * Fix parity. @@ -680,6 +698,9 @@ keycheck(uint8_t *key, uint8_t *corrected_key) } if (corrected_key != NULL) { +#ifdef UNALIGNED_POINTERS_PERMITTED + *(uint64_t *)&corrected_key[0] = htonll(key_so_far); +#else /* * The code below reverses the bytes on LITTLE_ENDIAN machines. * On BIG_ENDIAN, the same code copies without reversing @@ -693,6 +714,7 @@ keycheck(uint8_t *key, uint8_t *corrected_key) corrected_key[5] = key_so_far >> 16; corrected_key[6] = key_so_far >> 8; corrected_key[7] = (uint8_t)key_so_far; +#endif /* UNALIGNED_POINTERS_PERMITTED */ } /* EXPORT DELETE END */ return (B_TRUE); @@ -743,7 +765,7 @@ des3_keycheck(uint8_t *key, uint8_t *corrected_key) /* * Perform key equivalence checks, now that parity is properly set. * 1st and 2nd keys must be unique, the 3rd key can be the same as - * the 1st key for the 2 key varient of 3DES. + * the 1st key for the 2 key variant of 3DES. */ if (currentkey[0] == currentkey[1] || currentkey[1] == currentkey[2]) return (B_FALSE); @@ -785,6 +807,9 @@ des_parity_fix(uint8_t *key, des_strength_t strength, uint8_t *corrected_key) paritied_key = (uint8_t *)aligned_key; while (strength > i) { offset = 8 * i; +#ifdef UNALIGNED_POINTERS_PERMITTED + key_so_far = htonll(*(uint64_t *)&paritied_key[offset]); +#else key_so_far = (((uint64_t)paritied_key[offset + 0] << 56) | ((uint64_t)paritied_key[offset + 1] << 48) | ((uint64_t)paritied_key[offset + 2] << 40) | @@ -793,9 +818,13 @@ des_parity_fix(uint8_t *key, des_strength_t strength, uint8_t *corrected_key) ((uint64_t)paritied_key[offset + 5] << 16) | ((uint64_t)paritied_key[offset + 6] << 8) | (uint64_t)paritied_key[offset + 7]); +#endif /* UNALIGNED_POINTERS_PERMITTED */ fix_des_parity(&key_so_far); +#ifdef UNALIGNED_POINTERS_PERMITTED + *(uint64_t *)&paritied_key[offset] = htonll(key_so_far); +#else paritied_key[offset + 0] = key_so_far >> 56; paritied_key[offset + 1] = key_so_far >> 48; paritied_key[offset + 2] = key_so_far >> 40; @@ -804,6 +833,7 @@ des_parity_fix(uint8_t *key, des_strength_t strength, uint8_t *corrected_key) paritied_key[offset + 5] = key_so_far >> 16; paritied_key[offset + 6] = key_so_far >> 8; paritied_key[offset + 7] = (uint8_t)key_so_far; +#endif /* UNALIGNED_POINTERS_PERMITTED */ i++; } @@ -855,10 +885,13 @@ des_init_keysched(uint8_t *cipherKey, des_strength_t strength, void *ks) /* LINTED: pointer alignment */ key_uint64[i] = *((uint64_t *)&cipherKey[j]); } - } else { -#endif + } else +#endif /* _BIG_ENDIAN */ { for (i = 0, j = 0; j < keysize; i++, j += 8) { +#ifdef UNALIGNED_POINTERS_PERMITTED + key_uint64[i] = htonll(*(uint64_t *)&cipherKey[j]); +#else key_uint64[i] = (((uint64_t)cipherKey[j] << 56) | ((uint64_t)cipherKey[j + 1] << 48) | ((uint64_t)cipherKey[j + 2] << 40) | @@ -867,11 +900,9 @@ des_init_keysched(uint8_t *cipherKey, des_strength_t strength, void *ks) ((uint64_t)cipherKey[j + 5] << 16) | ((uint64_t)cipherKey[j + 6] << 8) | (uint64_t)cipherKey[j + 7]); +#endif /* UNALIGNED_POINTERS_PERMITTED */ } } -#ifdef _BIG_ENDIAN - } -#endif switch (strength) { case DES: @@ -951,7 +982,7 @@ des_alloc_keysched(size_t *keysched_size, des_strength_t strength, int kmflag) /* * Replace the LSB of each byte by the xor of the other * 7 bits. The tricky thing is that the original contents of the LSBs - * are nullifed by including them twice in the xor computation. + * are nullified by including them twice in the xor computation. */ static void fix_des_parity(uint64_t *keyp) @@ -988,11 +1019,11 @@ des_xor_block(uint8_t *data, uint8_t *dst) IS_P2ALIGNED(data, sizeof (uint32_t))) { /* LINTED: pointer alignment */ *(uint32_t *)&dst[0] ^= - /* LINTED: pointer alignment */ + /* LINTED: pointer alignment */ *(uint32_t *)&data[0]; - /* LINTED: pointer alignment */ + /* LINTED: pointer alignment */ *(uint32_t *)&dst[4] ^= - /* LINTED: pointer alignment */ + /* LINTED: pointer alignment */ *(uint32_t *)&data[4]; } else { DES_XOR_BLOCK(data, dst); diff --git a/usr/src/common/crypto/md4/md4.c b/usr/src/common/crypto/md4/md4.c index 7cc8ce3520..b1701ae457 100644 --- a/usr/src/common/crypto/md4/md4.c +++ b/usr/src/common/crypto/md4/md4.c @@ -1,10 +1,8 @@ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - /* * MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm */ @@ -38,6 +36,10 @@ #include <strings.h> #endif /* _KERNEL */ +#if defined(__i386) || defined(__amd64) +#define UNALIGNED_POINTERS_PERMITTED +#endif + #include <sys/md4.h> /* @@ -262,18 +264,20 @@ MD4Transform(uint32_t state[4], unsigned char block[64]) * a multiple of 4. */ static void -Encode(output, input, len) - unsigned char *output; - uint32_t *input; - unsigned int len; +Encode(unsigned char *output, uint32_t *input, unsigned int len) { unsigned int i, j; for (i = 0, j = 0; j < len; i++, j += 4) { +#if defined(_LITTLE_ENDIAN) && defined(UNALIGNED_POINTERS_PERMITTED) + *(uint32_t *)&output[j] = input[i]; +#else + /* endian-independent code */ output[j] = (unsigned char)(input[i] & 0xff); output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); +#endif /* _LITTLE_ENDIAN && UNALIGNED_POINTERS_PERMITTED */ } } @@ -282,16 +286,20 @@ Encode(output, input, len) * a multiple of 4. */ static void -Decode(output, input, len) - uint32_t *output; - unsigned char *input; - unsigned int len; +Decode(uint32_t *output, unsigned char *input, unsigned int len) { unsigned int i, j; - for (i = 0, j = 0; j < len; i++, j += 4) + for (i = 0, j = 0; j < len; i++, j += 4) { +#if defined(_LITTLE_ENDIAN) && defined(UNALIGNED_POINTERS_PERMITTED) + output[i] = *(uint32_t *)&input[j]; +#else + /* endian-independent code */ output[i] = ((uint32_t)input[j]) | - (((uint32_t)input[j+1]) << 8) | - (((uint32_t)input[j+2]) << 16) | - (((uint32_t)input[j+3]) << 24); + (((uint32_t)input[j+1]) << 8) | + (((uint32_t)input[j+2]) << 16) | + (((uint32_t)input[j+3]) << 24); +#endif /* _LITTLE_ENDIAN && UNALIGNED_POINTERS_PERMITTED */ + } + } diff --git a/usr/src/common/crypto/md5/md5_byteswap.h b/usr/src/common/crypto/md5/md5_byteswap.h index 373f0cf02d..a99429d4bc 100644 --- a/usr/src/common/crypto/md5/md5_byteswap.h +++ b/usr/src/common/crypto/md5/md5_byteswap.h @@ -20,15 +20,13 @@ */ /* - * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #ifndef _MD5_BYTESWAP_H #define _MD5_BYTESWAP_H -#pragma ident "%Z%%M% %I% %E% SMI" - /* * definitions for inline functions for little-endian loads. * @@ -40,8 +38,11 @@ * same thing and must be changed together. */ +#include <sys/types.h> #if defined(__sparc) #include <v9/sys/asi.h> +#elif defined(_LITTLE_ENDIAN) +#include <sys/byteorder.h> #endif #ifdef __cplusplus @@ -95,25 +96,11 @@ load_little_32(uint32_t *addr) __asm__( "lduwa [%1] %2, %0\n\t" - : "=r" (value) - : "r" (addr), "i" (ASI_PL)); - - return (value); -} - -static __inline__ uint16_t -load_little_16(uint16_t *addr) -{ - uint16_t value; - - __asm__( - "lduha [%1] %2, %0\n\t" - : "=r" (value) - : "r" (addr), "i" (ASI_PL)); + : "=r" (value) + : "r" (addr), "i" (ASI_PL)); return (value); } - #endif /* !__lint && __GNUC__ */ #if !defined(__GNUC__) @@ -129,13 +116,14 @@ load_little_32(uint32_t *addr) } #endif /* __lint */ -#else /* !sun4u */ +#elif defined(_LITTLE_ENDIAN) +#define LOAD_LITTLE_32(addr) htonl(addr) +#else /* big endian -- will work on little endian, but slowly */ /* Since we do byte operations, we don't have to check for alignment. */ #define LOAD_LITTLE_32(addr) \ ((addr)[0] | ((addr)[1] << 8) | ((addr)[2] << 16) | ((addr)[3] << 24)) - #endif /* sun4u */ #if defined(sun4v) @@ -175,9 +163,9 @@ static __inline__ void set_little(uint8_t asi) { __asm__ __volatile__( - "wr %%g0, %0, %%asi\n\t" - : /* Nothing */ - : "r" (asi)); + "wr %%g0, %0, %%asi\n\t" + : /* Nothing */ + : "r" (asi)); } static __inline__ uint8_t @@ -186,8 +174,8 @@ get_little(void) uint8_t asi; __asm__ __volatile__( - "rd %%asi, %0\n\t" - : "=r" (asi)); + "rd %%asi, %0\n\t" + : "=r" (asi)); return (asi); } diff --git a/usr/src/common/crypto/modes/ccm.c b/usr/src/common/crypto/modes/ccm.c index cc9fea2ac6..ea2717537c 100644 --- a/usr/src/common/crypto/modes/ccm.c +++ b/usr/src/common/crypto/modes/ccm.c @@ -36,6 +36,11 @@ #include <sys/crypto/common.h> #include <sys/crypto/impl.h> +#if defined(__i386) || defined(__amd64) +#include <sys/byteorder.h> +#define UNALIGNED_POINTERS_PERMITTED +#endif + /* * Encrypt multiple blocks of data in CCM mode. Decrypt for CCM mode * is done in another function. @@ -59,9 +64,6 @@ ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length, size_t out_data_1_len; uint64_t counter; uint8_t *mac_buf; -#ifdef _LITTLE_ENDIAN - uint8_t *p; -#endif if (length + ctx->ccm_remainder_len < block_size) { /* accumulate bytes here and return */ @@ -114,29 +116,13 @@ ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length, * Increment counter. Counter bits are confined * to the bottom 64 bits of the counter block. */ - counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask; #ifdef _LITTLE_ENDIAN - p = (uint8_t *)&counter; - counter = (((uint64_t)p[0] << 56) | - ((uint64_t)p[1] << 48) | - ((uint64_t)p[2] << 40) | - ((uint64_t)p[3] << 32) | - ((uint64_t)p[4] << 24) | - ((uint64_t)p[5] << 16) | - ((uint64_t)p[6] << 8) | - (uint64_t)p[7]); -#endif + counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask); + counter = htonll(counter + 1); +#else + counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask; counter++; -#ifdef _LITTLE_ENDIAN - counter = (((uint64_t)p[0] << 56) | - ((uint64_t)p[1] << 48) | - ((uint64_t)p[2] << 40) | - ((uint64_t)p[3] << 32) | - ((uint64_t)p[4] << 24) | - ((uint64_t)p[5] << 16) | - ((uint64_t)p[6] << 8) | - (uint64_t)p[7]); -#endif +#endif /* _LITTLE_ENDIAN */ counter &= ctx->ccm_counter_mask; ctx->ccm_cb[1] = (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter; @@ -316,8 +302,7 @@ ccm_encrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size, bcopy(ccm_mac_p, out_data_2, ctx->ccm_mac_len); } else { - size_t len_not_used - = out_data_1_len - + size_t len_not_used = out_data_1_len - ctx->ccm_remainder_len; /* * part of mac in will be in @@ -494,29 +479,13 @@ ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length, * Increment counter. * Counter bits are confined to the bottom 64 bits */ - counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask; #ifdef _LITTLE_ENDIAN - p = (uint8_t *)&counter; - counter = (((uint64_t)p[0] << 56) | - ((uint64_t)p[1] << 48) | - ((uint64_t)p[2] << 40) | - ((uint64_t)p[3] << 32) | - ((uint64_t)p[4] << 24) | - ((uint64_t)p[5] << 16) | - ((uint64_t)p[6] << 8) | - (uint64_t)p[7]); -#endif + counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask); + counter = htonll(counter + 1); +#else + counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask; counter++; -#ifdef _LITTLE_ENDIAN - counter = (((uint64_t)p[0] << 56) | - ((uint64_t)p[1] << 48) | - ((uint64_t)p[2] << 40) | - ((uint64_t)p[3] << 32) | - ((uint64_t)p[4] << 24) | - ((uint64_t)p[5] << 16) | - ((uint64_t)p[6] << 8) | - (uint64_t)p[7]); -#endif +#endif /* _LITTLE_ENDIAN */ counter &= ctx->ccm_counter_mask; ctx->ccm_cb[1] = (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter; @@ -704,9 +673,6 @@ ccm_format_initial_blocks(uchar_t *nonce, ulong_t nonceSize, int i, j, k; uint64_t mask = 0; uint8_t *cb; -#ifdef _LITTLE_ENDIAN - uint8_t *p8; -#endif /* _LITTLE_ENDIAN */ q = (uint8_t)((15 - nonceSize) & 0xFF); t = (uint8_t)((aes_ctx->ccm_mac_len) & 0xFF); @@ -748,15 +714,7 @@ ccm_format_initial_blocks(uchar_t *nonce, ulong_t nonceSize, } #ifdef _LITTLE_ENDIAN - p8 = (uint8_t *)&mask; - mask = (((uint64_t)p8[0] << 56) | - ((uint64_t)p8[1] << 48) | - ((uint64_t)p8[2] << 40) | - ((uint64_t)p8[3] << 32) | - ((uint64_t)p8[4] << 24) | - ((uint64_t)p8[5] << 16) | - ((uint64_t)p8[6] << 8) | - (uint64_t)p8[7]); + mask = htonll(mask); #endif aes_ctx->ccm_counter_mask = mask; @@ -777,6 +735,13 @@ ccm_format_initial_blocks(uchar_t *nonce, ulong_t nonceSize, static void encode_adata_len(ulong_t auth_data_len, uint8_t *encoded, size_t *encoded_len) { +#ifdef UNALIGNED_POINTERS_PERMITTED + uint32_t *lencoded_ptr; +#ifdef _LP64 + uint64_t *llencoded_ptr; +#endif +#endif /* UNALIGNED_POINTERS_PERMITTED */ + if (auth_data_len < ((1ULL<<16) - (1ULL<<8))) { /* 0 < a < (2^16-2^8) */ *encoded_len = 2; @@ -789,16 +754,26 @@ encode_adata_len(ulong_t auth_data_len, uint8_t *encoded, size_t *encoded_len) *encoded_len = 6; encoded[0] = 0xff; encoded[1] = 0xfe; +#ifdef UNALIGNED_POINTERS_PERMITTED + lencoded_ptr = (uint32_t *)&encoded[2]; + *lencoded_ptr = htonl(auth_data_len); +#else encoded[2] = (auth_data_len & 0xff000000) >> 24; encoded[3] = (auth_data_len & 0xff0000) >> 16; encoded[4] = (auth_data_len & 0xff00) >> 8; encoded[5] = auth_data_len & 0xff; +#endif /* UNALIGNED_POINTERS_PERMITTED */ + #ifdef _LP64 } else { /* 2^32 <= a < 2^64 */ *encoded_len = 10; encoded[0] = 0xff; encoded[1] = 0xff; +#ifdef UNALIGNED_POINTERS_PERMITTED + llencoded_ptr = (uint64_t *)&encoded[2]; + *llencoded_ptr = htonl(auth_data_len); +#else encoded[2] = (auth_data_len & 0xff00000000000000) >> 56; encoded[3] = (auth_data_len & 0xff000000000000) >> 48; encoded[4] = (auth_data_len & 0xff0000000000) >> 40; @@ -807,6 +782,7 @@ encode_adata_len(ulong_t auth_data_len, uint8_t *encoded, size_t *encoded_len) encoded[7] = (auth_data_len & 0xff0000) >> 16; encoded[8] = (auth_data_len & 0xff00) >> 8; encoded[9] = auth_data_len & 0xff; +#endif /* UNALIGNED_POINTERS_PERMITTED */ #endif /* _LP64 */ } } diff --git a/usr/src/common/crypto/modes/ctr.c b/usr/src/common/crypto/modes/ctr.c index 23ed013350..5a1b3fc805 100644 --- a/usr/src/common/crypto/modes/ctr.c +++ b/usr/src/common/crypto/modes/ctr.c @@ -23,8 +23,6 @@ * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #ifndef _KERNEL #include <strings.h> #include <limits.h> @@ -37,6 +35,10 @@ #include <sys/crypto/common.h> #include <sys/crypto/impl.h> +#ifdef _LITTLE_ENDIAN +#include <sys/byteorder.h> +#endif + /* * Encrypt and decrypt multiple blocks of data in counter mode. */ @@ -101,29 +103,13 @@ ctr_mode_contiguous_blocks(ctr_ctx_t *ctx, char *data, size_t length, * Increment counter. Counter bits are confined * to the bottom 64 bits of the counter block. */ - counter = ctx->ctr_cb[1] & ctx->ctr_counter_mask; #ifdef _LITTLE_ENDIAN - p = (uint8_t *)&counter; - counter = (((uint64_t)p[0] << 56) | - ((uint64_t)p[1] << 48) | - ((uint64_t)p[2] << 40) | - ((uint64_t)p[3] << 32) | - ((uint64_t)p[4] << 24) | - ((uint64_t)p[5] << 16) | - ((uint64_t)p[6] << 8) | - (uint64_t)p[7]); -#endif + counter = ntohll(ctx->ctr_cb[1] & ctx->ctr_counter_mask); + counter = htonll(counter + 1); +#else + counter = ctx->ctr_cb[1] & ctx->ctr_counter_mask; counter++; -#ifdef _LITTLE_ENDIAN - counter = (((uint64_t)p[0] << 56) | - ((uint64_t)p[1] << 48) | - ((uint64_t)p[2] << 40) | - ((uint64_t)p[3] << 32) | - ((uint64_t)p[4] << 24) | - ((uint64_t)p[5] << 16) | - ((uint64_t)p[6] << 8) | - (uint64_t)p[7]); -#endif +#endif /* _LITTLE_ENDIAN */ counter &= ctx->ctr_counter_mask; ctx->ctr_cb[1] = (ctx->ctr_cb[1] & ~(ctx->ctr_counter_mask)) | counter; @@ -224,25 +210,15 @@ ctr_init_ctx(ctr_ctx_t *ctr_ctx, ulong_t count, uint8_t *cb, void (*copy_block)(uint8_t *, uint8_t *)) { uint64_t mask = 0; -#ifdef _LITTLE_ENDIAN - uint8_t *p8; -#endif if (count == 0 || count > 64) { return (CRYPTO_MECHANISM_PARAM_INVALID); } while (count-- > 0) mask |= (1ULL << count); + #ifdef _LITTLE_ENDIAN - p8 = (uint8_t *)&mask; - mask = (((uint64_t)p8[0] << 56) | - ((uint64_t)p8[1] << 48) | - ((uint64_t)p8[2] << 40) | - ((uint64_t)p8[3] << 32) | - ((uint64_t)p8[4] << 24) | - ((uint64_t)p8[5] << 16) | - ((uint64_t)p8[6] << 8) | - (uint64_t)p8[7]); + mask = htonll(mask); #endif ctr_ctx->ctr_counter_mask = mask; copy_block(cb, (uchar_t *)ctr_ctx->ctr_cb); diff --git a/usr/src/common/crypto/sha1/sha1.c b/usr/src/common/crypto/sha1/sha1.c index c07325806f..f9d974ac1c 100644 --- a/usr/src/common/crypto/sha1/sha1.c +++ b/usr/src/common/crypto/sha1/sha1.c @@ -3,8 +3,6 @@ * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - /* * The basic framework for this code came from the reference * implementation for MD5. That implementation is Copyright (C) @@ -29,7 +27,7 @@ * documentation and/or software. * * NOTE: Cleaned-up and optimized, version of SHA1, based on the FIPS 180-1 - * standard, available at http://www.itl.nist.gov/div897/pubs/fip180-1.htm + * standard, available at http://www.itl.nist.gov/fipspubs/fip180-1.htm * Not as fast as one would like -- further optimizations are encouraged * and appreciated. */ @@ -48,6 +46,11 @@ #include <sys/systeminfo.h> #endif /* !_KERNEL */ +#ifdef _LITTLE_ENDIAN +#include <sys/byteorder.h> +#define HAVE_HTONL +#endif + static void Encode(uint8_t *, const uint32_t *, size_t); #if defined(__sparc) @@ -106,17 +109,6 @@ ROTATE_LEFT(uint64_t value, uint32_t n) #endif -#if defined(__GNUC__) && (defined(__i386) || defined(__amd64)) - -#define HAVE_BSWAP - -extern __inline__ uint32_t bswap(uint32_t value) -{ - __asm__("bswap %0" : "+r" (value)); - return (value); -} - -#endif /* * SHA1Init() @@ -296,8 +288,8 @@ SHA1Update(SHA1_CTX *ctx, const void *inptr, size_t input_len) */ for (; i + 63 < input_len; i += 64) { SHA1TransformVIS(X0, - /* LINTED E_BAD_PTR_CAST_ALIGN */ - (uint32_t *)&input[i], + /* LINTED E_BAD_PTR_CAST_ALIGN */ + (uint32_t *)&input[i], /* CSTYLED */ &ctx->state[0], VIS); } @@ -455,24 +447,16 @@ typedef uint32_t sha1word; */ #if defined(_BIG_ENDIAN) - #define LOAD_BIG_32(addr) (*(uint32_t *)(addr)) -#else /* !defined(_BIG_ENDIAN) */ - -#if defined(HAVE_BSWAP) - -#define LOAD_BIG_32(addr) bswap(*((uint32_t *)(addr))) - -#else /* !defined(HAVE_BSWAP) */ +#elif defined(HAVE_HTONL) +#define LOAD_BIG_32(addr) htonl(*((uint32_t *)(addr))) +#else /* little endian -- will work on big endian, but slowly */ #define LOAD_BIG_32(addr) \ (((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3]) - -#endif /* !defined(HAVE_BSWAP) */ - -#endif /* !defined(_BIG_ENDIAN) */ +#endif /* _BIG_ENDIAN */ /* * SHA1Transform() @@ -537,14 +521,14 @@ SHA1Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, * .rodata. * * unfortunately, loading from an array in this manner hurts - * performance under intel. so, there is a macro, + * performance under Intel. So, there is a macro, * SHA1_CONST(), used in SHA1Transform(), that either expands to * a reference to this array, or to the actual constant, * depending on what platform this code is compiled for. */ static const uint32_t sha1_consts[] = { - SHA1_CONST_0, SHA1_CONST_1, SHA1_CONST_2, SHA1_CONST_3, + SHA1_CONST_0, SHA1_CONST_1, SHA1_CONST_2, SHA1_CONST_3 }; /* @@ -629,9 +613,10 @@ SHA1Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, } #else /* !defined(__sparc) */ -void +void /* CSTYLED */ SHA1Transform(SHA1_CTX *ctx, const uint8_t blk[64]) { + /* CSTYLED */ sha1word a = ctx->state[0]; sha1word b = ctx->state[1]; sha1word c = ctx->state[2]; diff --git a/usr/src/common/crypto/sha2/sha2.c b/usr/src/common/crypto/sha2/sha2.c index 28d1783ae3..7dc2e98a97 100644 --- a/usr/src/common/crypto/sha2/sha2.c +++ b/usr/src/common/crypto/sha2/sha2.c @@ -3,8 +3,6 @@ * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - /* * The basic framework for this code came from the reference * implementation for MD5. That implementation is Copyright (C) @@ -29,7 +27,8 @@ * documentation and/or software. * * NOTE: Cleaned-up and optimized, version of SHA2, based on the FIPS 180-2 - * standard, available at http://www.itl.nist.gov/div897/pubs/fip180-2.htm + * standard, available at + * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf * Not as fast as one would like -- further optimizations are encouraged * and appreciated. */ @@ -60,6 +59,11 @@ #endif /* _KERNEL */ +#ifdef _LITTLE_ENDIAN +#include <sys/byteorder.h> +#define HAVE_HTONL +#endif + static void Encode(uint8_t *, uint32_t *, size_t); static void Encode64(uint8_t *, uint64_t *, size_t); @@ -121,28 +125,23 @@ static uint8_t PADDING[128] = { 0x80, /* all zeros */ }; */ #if defined(_BIG_ENDIAN) - #define LOAD_BIG_32(addr) (*(uint32_t *)(addr)) +#define LOAD_BIG_64(addr) (*(uint64_t *)(addr)) -#else /* little endian -- will work on big endian, but slowly */ +#elif defined(HAVE_HTONL) +#define LOAD_BIG_32(addr) htonl(*((uint32_t *)(addr))) +#define LOAD_BIG_64(addr) htonll(*((uint64_t *)(addr))) +#else +/* little endian -- will work on big endian, but slowly */ #define LOAD_BIG_32(addr) \ (((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3]) -#endif - - -#if defined(_BIG_ENDIAN) - -#define LOAD_BIG_64(addr) (*(uint64_t *)(addr)) - -#else /* little endian -- will work on big endian, but slowly */ - #define LOAD_BIG_64(addr) \ (((uint64_t)(addr)[0] << 56) | ((uint64_t)(addr)[1] << 48) | \ ((uint64_t)(addr)[2] << 40) | ((uint64_t)(addr)[3] << 32) | \ ((uint64_t)(addr)[4] << 24) | ((uint64_t)(addr)[5] << 16) | \ ((uint64_t)(addr)[6] << 8) | (uint64_t)(addr)[7]) -#endif +#endif /* _BIG_ENDIAN */ #if !defined(__amd64) @@ -715,8 +714,8 @@ SHA2Init(uint64_t mech, SHA2_CTX *ctx) break; #ifdef _KERNEL default: - cmn_err(CE_PANIC, "sha2_init: " - "failed to find a supported algorithm: 0x%x", + cmn_err(CE_PANIC, + "sha2_init: failed to find a supported algorithm: 0x%x", (uint32_t)mech); #endif /* _KERNEL */ diff --git a/usr/src/lib/libc/amd64/gen/byteorder.s b/usr/src/lib/libc/amd64/gen/byteorder.s index 5a715a58c7..5a192f0a68 100644 --- a/usr/src/lib/libc/amd64/gen/byteorder.s +++ b/usr/src/lib/libc/amd64/gen/byteorder.s @@ -19,28 +19,46 @@ * CDDL HEADER END */ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ .file "byteorder.s" -#include "SYS.h" +#include <sys/asm_linkage.h> /* - * NOTE: htonl/ntohl are identical routines, as are htons/ntohs. - * As such, they could be implemented as a single routine, using - * multiple ALTENTRY/SET_SIZE definitions. We don't do this so + * NOTE: htonll/ntohll, htonl/ntohl, and htons/ntohs are identical + * routines. As such, they could be implemented as a single routine, + * using multiple ALTENTRY/SET_SIZE definitions. We don't do this so * that they will have unique addresses, allowing DTrace and - * other debuggers to tell them apart. + * other debuggers to tell them apart. */ +/* + * unsigned long long htonll( hll ) + * unsigned long long ntohll( hll ) + * unsigned long long hll; + * reverses the byte order of 'uint64_t hll' on little endian machines + */ + ENTRY(htonll) + movq %rdi, %rax /* %rax = hll */ + bswapq %rax /* reverses the byte order of %rax */ + ret /* return (%rax) */ + SET_SIZE(htonll) + + ENTRY(ntohll) + movq %rdi, %rax /* %rax = hll */ + bswapq %rax /* reverses the byte order of %rax */ + ret /* return (%rax) */ + SET_SIZE(ntohll) + /* * unsigned long htonl( hl ) * unsigned long ntohl( hl ) - * long hl; - * reverses the byte order of 'uint32_t hl' + * unsigned long hl; + * reverses the byte order of 'uint32_t hl' on little endian machines */ ENTRY(htonl) movl %edi, %eax /* %eax = hl */ @@ -56,9 +74,8 @@ /* * unsigned short htons( hs ) - * short hs; - * - * reverses the byte order in hs. + * unsigned short hs; + * reverses the byte order of 'uint16_t hs' on little endian machines. */ ENTRY(htons) movl %edi, %eax /* %eax = hs */ diff --git a/usr/src/lib/libc/i386/Makefile.com b/usr/src/lib/libc/i386/Makefile.com index 1cb7299a8c..3a14bcdfb2 100644 --- a/usr/src/lib/libc/i386/Makefile.com +++ b/usr/src/lib/libc/i386/Makefile.com @@ -22,8 +22,6 @@ # Copyright 2008 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # -# ident "%Z%%M% %I% %E% SMI" -# LIB_PIC= libc_pic.a VERS= .1 @@ -108,6 +106,7 @@ GENOBJS= \ abs.o \ alloca.o \ byteorder.o \ + byteorder64.o \ cuexit.o \ ecvt.o \ errlst.o \ @@ -970,7 +969,7 @@ ALTPICS= $(TRACEOBJS:%=pics/%) $(DYNLIB) := PICS += $(ROOTFS_LIBDIR)/libc_i18n.a $(DYNLIB) := BUILD.SO = $(LD) -o $@ -G $(DYNFLAGS) $(PICS) $(ALTPICS) $(LDLIBS) -MAPFILES = ../port/mapfile-vers ../i386/mapfile-vers +MAPFILES = ../port/mapfile-vers ../i386/mapfile-vers # # EXTN_CPPFLAGS and EXTN_CFLAGS set in enclosing Makefile diff --git a/usr/src/lib/libc/i386/gen/byteorder64.c b/usr/src/lib/libc/i386/gen/byteorder64.c new file mode 100644 index 0000000000..3806a62f34 --- /dev/null +++ b/usr/src/lib/libc/i386/gen/byteorder64.c @@ -0,0 +1,50 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#include <sys/isa_defs.h> +#include <sys/types.h> +#include <netinet/in.h> +#include <inttypes.h> + +#if (defined(_BIG_ENDIAN) || defined(_LP64)) && !defined(__lint) + +#error Use ISA-dependent byteorder64.c only on a 32-bit little-endian machine. + +#else + +uint64_t +htonll(uint64_t in) +{ + return (htonl(in >> 32) | ((uint64_t)htonl(in) << 32)); +} + +uint64_t +ntohll(uint64_t in) +{ + return (ntohl(in >> 32) | (uint64_t)ntohl(in) << 32); +} + +#endif /* (_BIG_ENDIAN) || _LP64) && !__lint */ diff --git a/usr/src/lib/libc/port/mapfile-vers b/usr/src/lib/libc/port/mapfile-vers index b75a328198..69ef89a349 100644 --- a/usr/src/lib/libc/port/mapfile-vers +++ b/usr/src/lib/libc/port/mapfile-vers @@ -22,8 +22,6 @@ # Copyright 2008 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # -# ident "%Z%%M% %I% %E% SMI" -# # # All function names added to this or any other libc mapfile @@ -81,6 +79,7 @@ SUNW_1.23 { # SunOS 5.11 (Solaris 11) getattrat; getpagesizes2; htonl; + htonll; htons; lio_listio; mkdtemp; @@ -99,6 +98,7 @@ SUNW_1.23 { # SunOS 5.11 (Solaris 11) mq_unlink; nanosleep; ntohl; + ntohll; ntohs; posix_fadvise; posix_fallocate; @@ -1042,7 +1042,7 @@ SUNW_0.8 { # SunOS 5.4 (Solaris 2.4) iconv_close; iconv_open; lfmt; - mutex_destroy = NODYNSORT; + mutex_destroy = NODYNSORT; mutex_init; mutex_lock = NODYNSORT; mutex_trylock = NODYNSORT; diff --git a/usr/src/lib/libc/sparc/gen/byteorder.c b/usr/src/lib/libc/sparc/gen/byteorder.c index b28bfebdd0..0107a5a665 100644 --- a/usr/src/lib/libc/sparc/gen/byteorder.c +++ b/usr/src/lib/libc/sparc/gen/byteorder.c @@ -20,12 +20,10 @@ */ /* - * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include <sys/isa_defs.h> #include <sys/types.h> @@ -36,6 +34,25 @@ #else /* !_LITTLE_ENDIAN */ +/* + * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs() + * These functions just return the input parameter, as the host + * byte order is the same as the network byte order (big endian). + * On little endian machines, these functions byte swap. + */ + +uint64_t +htonll(uint64_t in) +{ + return (in); +} + +uint64_t +ntohll(uint64_t in) +{ + return (in); +} + uint32_t htonl(uint32_t in) { diff --git a/usr/src/lib/libc/sparcv9/gen/byteorder.c b/usr/src/lib/libc/sparcv9/gen/byteorder.c index 3c26622a2d..0107a5a665 100644 --- a/usr/src/lib/libc/sparcv9/gen/byteorder.c +++ b/usr/src/lib/libc/sparcv9/gen/byteorder.c @@ -20,12 +20,10 @@ */ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include <sys/isa_defs.h> #include <sys/types.h> @@ -36,6 +34,25 @@ #else /* !_LITTLE_ENDIAN */ +/* + * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs() + * These functions just return the input parameter, as the host + * byte order is the same as the network byte order (big endian). + * On little endian machines, these functions byte swap. + */ + +uint64_t +htonll(uint64_t in) +{ + return (in); +} + +uint64_t +ntohll(uint64_t in) +{ + return (in); +} + uint32_t htonl(uint32_t in) { diff --git a/usr/src/lib/libdhcputil/common/dhcp_inittab.c b/usr/src/lib/libdhcputil/common/dhcp_inittab.c index 0010fdfabe..9aa4180354 100644 --- a/usr/src/lib/libdhcputil/common/dhcp_inittab.c +++ b/usr/src/lib/libdhcputil/common/dhcp_inittab.c @@ -19,12 +19,10 @@ * CDDL HEADER END */ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include <sys/types.h> #include <string.h> #include <stdlib.h> @@ -47,8 +45,6 @@ #include "dhcp_symbol.h" #include "dhcp_inittab.h" -static uint64_t dhcp_htonll(uint64_t); -static uint64_t dhcp_ntohll(uint64_t); static void inittab_msg(const char *, ...); static uchar_t category_to_code(const char *); static boolean_t encode_number(uint8_t, uint8_t, boolean_t, uint8_t, @@ -1478,7 +1474,7 @@ decode_number(uint8_t n_entries, uint8_t size, boolean_t is_signed, case 8: (void) memcpy(&uint64, from, 8); to += sprintf(to, is_signed ? "%lld" : "%llu", - dhcp_ntohll(uint64)); + ntohll(uint64)); break; default: @@ -1574,7 +1570,7 @@ encode_number(uint8_t n_entries, uint8_t size, boolean_t is_signed, break; case 8: - uint64 = dhcp_htonll(strtoull(from, &endptr, 0)); + uint64 = htonll(strtoull(from, &endptr, 0)); if (errno != 0 || from == endptr) { goto error; } @@ -1695,37 +1691,6 @@ category_to_code(const char *category) } /* - * dhcp_htonll(): converts a 64-bit number from host to network byte order - * - * input: uint64_t: the number to convert - * output: uint64_t: its value in network byte order - */ - -static uint64_t -dhcp_htonll(uint64_t uint64_hbo) -{ - return (dhcp_ntohll(uint64_hbo)); -} - -/* - * dhcp_ntohll(): converts a 64-bit number from network to host byte order - * - * input: uint64_t: the number to convert - * output: uint64_t: its value in host byte order - */ - -static uint64_t -dhcp_ntohll(uint64_t uint64_nbo) -{ -#ifdef _LITTLE_ENDIAN - return ((uint64_t)ntohl(uint64_nbo & 0xffffffff) << 32 | - ntohl(uint64_nbo >> 32)); -#else - return (uint64_nbo); -#endif -} - -/* * our internal table of DHCP option values, used by inittab_verify() */ static dhcp_symbol_t inittab_table[] = @@ -1745,7 +1710,7 @@ static dhcp_symbol_t inittab_table[] = { DSYM_FIELD, 16, "Yiaddr", DSYM_IP, 1, 1 }, { DSYM_FIELD, 20, "BootSrvA", DSYM_IP, 1, 1 }, { DSYM_FIELD, 24, "Giaddr", DSYM_IP, 1, 1 }, -{ DSYM_FIELD, 28, "Chaddr", DSYM_OCTET, 1, 16 }, +{ DSYM_FIELD, 28, "Chaddr", DSYM_OCTET, 1, 16 }, { DSYM_FIELD, 44, "BootSrvN", DSYM_ASCII, 1, 64 }, { DSYM_FIELD, 108, "BootFile", DSYM_ASCII, 1, 128 }, { DSYM_FIELD, 236, "Magic", DSYM_OCTET, 1, 4 }, diff --git a/usr/src/stand/lib/xdr/byteorder.c b/usr/src/stand/lib/xdr/byteorder.c index 1cf20707bf..390a7bc6de 100644 --- a/usr/src/stand/lib/xdr/byteorder.c +++ b/usr/src/stand/lib/xdr/byteorder.c @@ -2,9 +2,8 @@ * CDDL HEADER START * * The contents of this file are subject to the terms of the - * Common Development and Distribution License, Version 1.0 only - * (the "License"). You may not use this file except in compliance - * with the License. + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. @@ -20,16 +19,41 @@ * CDDL HEADER END */ /* - * Copyright 1991-2000, 2003 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include <sys/types.h> #include <netinet/in.h> +/* + * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs() + * + * On little endian machines these functions reverse the byte order of the + * input parameter and returns the result. This is to convert the byte order + * from host byte order (little endian) to network byte order (big endian), + * or vice versa. + * + * On big endian machines these functions just return the input parameter, + * as the host byte order is the same as the network byte order (big endian). + */ + + #ifdef _LITTLE_ENDIAN +uint64_t +htonll(uint64_t in) +{ + return ((uint64_t)htonl((in >> 32) & 0xffffffff) | + ((uint64_t)htonl(in & 0xffffffff) << 32)); +} + +uint64_t +ntohll(uint64_t in) +{ + return ((uint64_t)ntohl((in >> 32) & 0xffffffff) | + ((uint64_t)ntohl(in & 0xffffffff) << 32)); +} + uint32_t htonl(uint32_t in) { @@ -68,6 +92,17 @@ ntohs(uint16_t in) #else /* _LITTLE_ENDIAN */ #if defined(lint) +uint64_t +htonll(uint64_t in) +{ + return (in); +} + +uint64_t +ntohll(uint64_t in) +{ + return (in); +} uint32_t htonl(uint32_t in) diff --git a/usr/src/uts/common/crypto/io/dca_3des.c b/usr/src/uts/common/crypto/io/dca_3des.c index 12c2658cf2..1d2cf0b90f 100644 --- a/usr/src/uts/common/crypto/io/dca_3des.c +++ b/usr/src/uts/common/crypto/io/dca_3des.c @@ -21,12 +21,10 @@ */ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - /* * Deimos - cryptographic acceleration based upon Broadcom 582x. */ @@ -40,6 +38,11 @@ #include <sys/crypto/spi.h> #include <sys/crypto/dca.h> +#if defined(__i386) || defined(__amd64) +#include <sys/byteorder.h> +#define UNALIGNED_POINTERS_PERMITTED +#endif + /* * 3DES implementation. */ @@ -74,9 +77,15 @@ dca_3des(crypto_ctx_t *ctx, crypto_data_t *in, * If cd_miscdata non-null then this contains the IV. */ if (in->cd_miscdata != NULL) { +#ifdef UNALIGNED_POINTERS_PERMITTED + uint32_t *p = (uint32_t *)in->cd_miscdata; + des_ctx->dr_ctx.iv[0] = htonl(p[0]); + des_ctx->dr_ctx.iv[1] = htonl(p[1]); +#else uchar_t *p = (uchar_t *)in->cd_miscdata; des_ctx->dr_ctx.iv[0] = p[0]<<24 | p[1]<<16 | p[2]<<8 | p[3]; des_ctx->dr_ctx.iv[1] = p[4]<<24 | p[5]<<16 | p[6]<<8 | p[7]; +#endif /* UNALIGNED_POINTERS_PERMITTED */ } if (len > dca_length(out)) { @@ -168,9 +177,15 @@ dca_3desupdate(crypto_ctx_t *ctx, crypto_data_t *in, * If cd_miscdata non-null then this contains the IV. */ if (in->cd_miscdata != NULL) { +#ifdef UNALIGNED_POINTERS_PERMITTED + uint32_t *p = (uint32_t *)in->cd_miscdata; + des_ctx->dr_ctx.iv[0] = htonl(p[0]); + des_ctx->dr_ctx.iv[1] = htonl(p[1]); +#else uchar_t *p = (uchar_t *)in->cd_miscdata; des_ctx->dr_ctx.iv[0] = p[0]<<24 | p[1]<<16 | p[2]<<8 | p[3]; des_ctx->dr_ctx.iv[1] = p[4]<<24 | p[5]<<16 | p[6]<<8 | p[7]; +#endif /* UNALIGNED_POINTERS_PERMITTED */ } if (len > dca_length(out)) { @@ -392,7 +407,11 @@ dca_3desstart(dca_t *dca, uint32_t flags, dca_request_t *reqp) */ if (flags & DR_DECRYPT) { uchar_t ivstore[DESBLOCK]; +#ifdef UNALIGNED_POINTERS_PERMITTED + uint32_t *ivp = (uint32_t *)ivstore; +#else uchar_t *ivp = ivstore; +#endif /* UNALIGNED_POINTERS_PERMITTED */ /* get last 8 bytes of ciphertext for IV of next op */ /* @@ -420,10 +439,15 @@ dca_3desstart(dca_t *dca, uint32_t flags, dca_request_t *reqp) } /* store as a pair of native 32-bit values */ +#ifdef UNALIGNED_POINTERS_PERMITTED + ctx->dr_ctx.iv[0] = htonl(ivp[0]); + ctx->dr_ctx.iv[1] = htonl(ivp[1]); +#else ctx->dr_ctx.iv[0] = ivp[0]<<24 | ivp[1]<<16 | ivp[2]<<8 | ivp[3]; ctx->dr_ctx.iv[1] = ivp[4]<<24 | ivp[5]<<16 | ivp[6]<<8 | ivp[7]; +#endif /* UNALIGNED_POINTERS_PERMITTED */ } /* For now we force a pullup. Add direct DMA later. */ @@ -550,20 +574,31 @@ dca_3desdone(dca_request_t *reqp, int errno) */ if (reqp->dr_flags & DR_ENCRYPT) { uchar_t ivstore[DESBLOCK]; +#ifdef UNALIGNED_POINTERS_PERMITTED + uint32_t *iv = (uint32_t *)ivstore; +#else uchar_t *iv = ivstore; +#endif /* UNALIGNED_POINTERS_PERMITTED */ /* get last 8 bytes for IV of next op */ - errno = dca_getbufbytes(out, off, DESBLOCK, iv); + errno = dca_getbufbytes(out, off, DESBLOCK, + (uchar_t *)iv); if (errno != CRYPTO_SUCCESS) { DBG(NULL, DWARN, "dca_3desdone: dca_getbufbytes() failed"); goto errout; } + /* store as a pair of native 32-bit values */ +#ifdef UNALIGNED_POINTERS_PERMITTED + ctx->dr_ctx.iv[0] = htonl(iv[0]); + ctx->dr_ctx.iv[1] = htonl(iv[1]); +#else ctx->dr_ctx.iv[0] = iv[0]<<24 | iv[1]<<16 | iv[2]<<8 | iv[3]; ctx->dr_ctx.iv[1] = iv[4]<<24 | iv[5]<<16 | iv[6]<<8 | iv[7]; +#endif /* UNALIGNED_POINTERS_PERMITTED */ } /* @@ -623,14 +658,24 @@ dca_3desctxinit(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism, { dca_request_t *des_ctx; dca_t *dca = ctx->cc_provider; +#ifdef UNALIGNED_POINTERS_PERMITTED + uint32_t *param; + uint32_t *value32; +#else uchar_t *param; +#endif /* UNALIGNED_POINTERS_PERMITTED */ uchar_t *value; size_t paramsz; unsigned len; int i, j; paramsz = mechanism->cm_param_len; +#ifdef UNALIGNED_POINTERS_PERMITTED + param = (uint32_t *)mechanism->cm_param; +#else param = (uchar_t *)mechanism->cm_param; +#endif /* UNALIGNED_POINTERS_PERMITTED */ + if ((paramsz != 0) && (paramsz != DES_IV_LEN)) { DBG(NULL, DWARN, "dca_3desctxinit: parameter(IV) length not %d (%d)", @@ -650,10 +695,15 @@ dca_3desctxinit(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism, */ if (param != NULL) { ASSERT(paramsz == DES_IV_LEN); +#ifdef UNALIGNED_POINTERS_PERMITTED + des_ctx->dr_ctx.iv[0] = htonl(param[0]); + des_ctx->dr_ctx.iv[1] = htonl(param[1]); +#else des_ctx->dr_ctx.iv[0] = param[0]<<24 | param[1]<<16 | param[2]<<8 | param[3]; des_ctx->dr_ctx.iv[1] = param[4]<<24 | param[5]<<16 | param[6]<<8 | param[7]; +#endif /* UNALIGNED_POINTERS_PERMITTED */ } des_ctx->dr_ctx.residlen = 0; des_ctx->dr_ctx.activeresidlen = 0; @@ -709,10 +759,18 @@ dca_3desctxinit(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism, dca_3desctxfree(ctx); return (CRYPTO_KEY_SIZE_RANGE); } + +#ifdef UNALIGNED_POINTERS_PERMITTED + value32 = (uint32_t *)value; + des_ctx->dr_ctx.key[0] = htonl(value32[0]); + des_ctx->dr_ctx.key[1] = htonl(value32[1]); +#else des_ctx->dr_ctx.key[0] = value[0]<<24 | value[1]<<16 | value[2]<<8 | value[3]; des_ctx->dr_ctx.key[1] = value[4]<<24 | value[5]<<16 | value[6]<<8 | value[7]; +#endif /* UNALIGNED_POINTERS_PERMITTED */ + /* for single des just repeat des key */ des_ctx->dr_ctx.key[4] = des_ctx->dr_ctx.key[2] = des_ctx->dr_ctx.key[0]; diff --git a/usr/src/uts/common/sys/byteorder.h b/usr/src/uts/common/sys/byteorder.h index 36a82e2bbf..14bce59b66 100644 --- a/usr/src/uts/common/sys/byteorder.h +++ b/usr/src/uts/common/sys/byteorder.h @@ -20,7 +20,7 @@ */ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ @@ -40,8 +40,6 @@ #ifndef _SYS_BYTEORDER_H #define _SYS_BYTEORDER_H -#pragma ident "%Z%%M% %I% %E% SMI" - #include <sys/isa_defs.h> #include <sys/int_types.h> @@ -61,8 +59,10 @@ extern "C" { #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint) /* big-endian */ #define ntohl(x) (x) +#define ntohll(x) (x) #define ntohs(x) (x) #define htonl(x) (x) +#define htonll(x) (x) #define htons(x) (x) #elif !defined(ntohl) /* little-endian */ @@ -80,14 +80,18 @@ typedef uint32_t in_addr_t; #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) extern uint32_t htonl(uint32_t); extern uint16_t htons(uint16_t); -extern uint32_t ntohl(uint32_t); +extern uint32_t ntohl(uint32_t); extern uint16_t ntohs(uint16_t); #else extern in_addr_t htonl(in_addr_t); extern in_port_t htons(in_port_t); -extern in_addr_t ntohl(in_addr_t); +extern in_addr_t ntohl(in_addr_t); extern in_port_t ntohs(in_port_t); #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */ +#if !(defined(_XPG4_2) || defined(_XPG5)) || defined(__EXTENSIONS__) +extern uint64_t htonll(uint64_t); +extern uint64_t ntohll(uint64_t); +#endif /* !(_XPG4_2||_XPG5) || __EXTENSIONS__ */ #endif #if !defined(_XPG4_2) || defined(__EXTENSIONS__) diff --git a/usr/src/uts/intel/amd64/ml/amd64.il b/usr/src/uts/intel/amd64/ml/amd64.il index e6b56fe7dd..4053f81ebd 100644 --- a/usr/src/uts/intel/amd64/ml/amd64.il +++ b/usr/src/uts/intel/amd64/ml/amd64.il @@ -19,12 +19,10 @@ * CDDL HEADER END */ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - / / In-line functions for amd64 kernels. / @@ -78,6 +76,16 @@ / Networking byte order functions (too bad, Intel has the wrong byte order) / + .inline htonll,4 + movq %rdi, %rax + bswapq %rax + .end + + .inline ntohll,4 + movq %rdi, %rax + bswapq %rax + .end + .inline htonl,4 movl %edi, %eax bswap %eax diff --git a/usr/src/uts/intel/asm/byteorder.h b/usr/src/uts/intel/asm/byteorder.h index df2fe95934..615038f71b 100644 --- a/usr/src/uts/intel/asm/byteorder.h +++ b/usr/src/uts/intel/asm/byteorder.h @@ -2,9 +2,8 @@ * CDDL HEADER START * * The contents of this file are subject to the terms of the - * Common Development and Distribution License, Version 1.0 only - * (the "License"). You may not use this file except in compliance - * with the License. + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. @@ -20,15 +19,13 @@ * CDDL HEADER END */ /* - * Copyright 2004 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #ifndef _ASM_BYTEORDER_H #define _ASM_BYTEORDER_H -#pragma ident "%Z%%M% %I% %E% SMI" - #include <sys/types.h> #ifdef __cplusplus @@ -37,19 +34,15 @@ extern "C" { #if !defined(__lint) && defined(__GNUC__) -#if defined(__i386) || defined(__amd64) +/* + * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs() + * These functions reverse the byte order of the input parameter and returns + * the result. This is to convert the byte order from host byte order + * (little endian) to network byte order (big endian), or vice versa. + */ -extern __inline__ uint32_t htonl(uint32_t value) -{ - __asm__("bswap %0" : "+r" (value)); - return (value); -} -extern __inline__ uint32_t ntohl(uint32_t value) -{ - __asm__("bswap %0" : "+r" (value)); - return (value); -} +#if defined(__i386) || defined(__amd64) extern __inline__ uint16_t htons(uint16_t value) { @@ -71,6 +64,44 @@ extern __inline__ uint16_t ntohs(uint16_t value) return (value); } +extern __inline__ uint32_t htonl(uint32_t value) +{ + __asm__("bswap %0" : "+r" (value)); + return (value); +} + +extern __inline__ uint32_t ntohl(uint32_t value) +{ + __asm__("bswap %0" : "+r" (value)); + return (value); +} + +#if defined(__amd64) +extern __inline__ uint64_t htonll(uint64_t value) +{ + __asm__("bswapq %0" : "+r" (value)); + return (value); +} + +extern __inline__ uint64_t ntohll(uint64_t value) +{ + __asm__("bswapq %0" : "+r" (value)); + return (value); +} + +#elif defined(__i386) +/* Use the htonl() and ntohl() inline functions defined above */ +extern __inline__ uint64_t htonll(uint64_t value) +{ + return (htonl(value >> 32) | ((uint64_t)htonl(value) << 32)); +} + +extern __inline__ uint64_t ntohll(uint64_t value) +{ + return (ntohl(value >> 32) | (uint64_t)ntohl(value) << 32); +} +#endif /* __amd64 */ + #endif /* __i386 || __amd64 */ #endif /* !__lint && __GNUC__ */ diff --git a/usr/src/uts/intel/ia32/ml/i86_subr.s b/usr/src/uts/intel/ia32/ml/i86_subr.s index 332fdb0628..0b41525747 100644 --- a/usr/src/uts/intel/ia32/ml/i86_subr.s +++ b/usr/src/uts/intel/ia32/ml/i86_subr.s @@ -110,7 +110,7 @@ catch_fault: SET_SIZE(no_fault) #elif defined(__i386) - + ENTRY(on_fault) movl %gs:CPU_THREAD, %edx movl 4(%esp), %eax /* jumpbuf address */ @@ -905,7 +905,7 @@ _insque(caddr_t entryp, caddr_t predp) #if defined(__amd64) ENTRY(_insque) - movq (%rsi), %rax /* predp->forw */ + movq (%rsi), %rax /* predp->forw */ movq %rsi, CPTRSIZE(%rdi) /* entryp->back = predp */ movq %rax, (%rdi) /* entryp->forw = predp->forw */ movq %rdi, (%rsi) /* predp->forw = entryp */ @@ -1080,7 +1080,7 @@ str_valid: #endif /* __lint */ /* - * Berkley 4.3 introduced symbolically named interrupt levels + * Berkeley 4.3 introduced symbolically named interrupt levels * as a way deal with priority in a machine independent fashion. * Numbered priorities are machine specific, and should be * discouraged where possible. @@ -1193,7 +1193,7 @@ splx(int level) SET_SIZE(spl0) - /* splx implentation */ + /* splx implementation */ ENTRY(splx) jmp do_splx /* redirect to common splx code */ SET_SIZE(splx) @@ -1882,7 +1882,7 @@ scanc(size_t size, uchar_t *cp, uchar_t *table, uchar_t mask) /* rdx == table */ /* rcx == mask */ addq %rsi, %rdi /* end = &cp[size] */ -.scanloop: +.scanloop: cmpq %rdi, %rsi /* while (cp < end */ jnb .scandone movzbq (%rsi), %r8 /* %r8 = *cp */ @@ -1897,7 +1897,7 @@ scanc(size_t size, uchar_t *cp, uchar_t *table, uchar_t mask) SET_SIZE(scanc) #elif defined(__i386) - + ENTRY(scanc) pushl %edi pushl %esi @@ -1923,7 +1923,7 @@ scanc(size_t size, uchar_t *cp, uchar_t *table, uchar_t mask) ret SET_SIZE(scanc) -#endif /* __i386 */ +#endif /* __i386 */ #endif /* __lint */ /* @@ -2027,61 +2027,59 @@ curcpup(void) #endif /* __i386 */ #endif /* __lint */ +/* htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs() + * These functions reverse the byte order of the input parameter and returns + * the result. This is to convert the byte order from host byte order + * (little endian) to network byte order (big endian), or vice versa. + */ + #if defined(__lint) -/* ARGSUSED */ +uint64_t +htonll(uint64_t i) +{ return (i); } + +uint64_t +ntohll(uint64_t i) +{ return (i); } + uint32_t htonl(uint32_t i) -{ return (0); } +{ return (i); } -/* ARGSUSED */ uint32_t ntohl(uint32_t i) -{ return (0); } +{ return (i); } + +uint16_t +htons(uint16_t i) +{ return (i); } + +uint16_t +ntohs(uint16_t i) +{ return (i); } #else /* __lint */ #if defined(__amd64) - /* XX64 there must be shorter sequences for this */ - ENTRY(htonl) - ALTENTRY(ntohl) - movl %edi, %eax - bswap %eax + ENTRY(htonll) + ALTENTRY(ntohll) + movq %rdi, %rax + bswapq %rax ret - SET_SIZE(ntohl) - SET_SIZE(htonl) - -#elif defined(__i386) + SET_SIZE(ntohll) + SET_SIZE(htonll) + /* XX64 there must be shorter sequences for this */ ENTRY(htonl) ALTENTRY(ntohl) - movl 4(%esp), %eax + movl %edi, %eax bswap %eax ret SET_SIZE(ntohl) SET_SIZE(htonl) -#endif /* __i386 */ -#endif /* __lint */ - -#if defined(__lint) - -/* ARGSUSED */ -uint16_t -htons(uint16_t i) -{ return (0); } - -/* ARGSUSED */ -uint16_t -ntohs(uint16_t i) -{ return (0); } - - -#else /* __lint */ - -#if defined(__amd64) - /* XX64 there must be better sequences for this */ ENTRY(htons) ALTENTRY(ntohs) @@ -2089,11 +2087,29 @@ ntohs(uint16_t i) bswap %eax shrl $16, %eax ret - SET_SIZE(ntohs) + SET_SIZE(ntohs) SET_SIZE(htons) #elif defined(__i386) + ENTRY(htonll) + ALTENTRY(ntohll) + movl 4(%esp), %edx + movl 8(%esp), %eax + bswap %edx + bswap %eax + ret + SET_SIZE(ntohll) + SET_SIZE(htonll) + + ENTRY(htonl) + ALTENTRY(ntohl) + movl 4(%esp), %eax + bswap %eax + ret + SET_SIZE(ntohl) + SET_SIZE(htonl) + ENTRY(htons) ALTENTRY(ntohs) movl 4(%esp), %eax diff --git a/usr/src/uts/intel/ia32/ml/ia32.il b/usr/src/uts/intel/ia32/ml/ia32.il index f3fd44170d..aa3fbcfeb2 100644 --- a/usr/src/uts/intel/ia32/ml/ia32.il +++ b/usr/src/uts/intel/ia32/ml/ia32.il @@ -20,12 +20,10 @@ */ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - / / Inline functions for i386 kernels. / Shared between all x86 platform variants. @@ -88,6 +86,20 @@ / / Networking byte order functions (too bad, Intel has the wrong byte order) / + .inline htonll,4 + movl (%esp), %edx + movl 4(%esp), %eax + bswap %edx + bswap %eax + .end + + .inline ntohll,4 + movl (%esp), %edx + movl 4(%esp), %eax + bswap %edx + bswap %eax + .end + .inline htonl,4 movl (%esp), %eax bswap %eax |
