Print this page
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

Split Close
Expand all
Collapse all
          --- old/usr/src/common/crypto/aes/aes_impl.c
          +++ new/usr/src/common/crypto/aes/aes_impl.c
↓ open down ↓ 15 lines elided ↑ open up ↑
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
  25   25  
  26      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  27      -
  28   26  #include <sys/types.h>
  29   27  #include <sys/systm.h>
  30   28  #include <sys/ddi.h>
  31   29  #include <sys/sysmacros.h>
  32   30  #include <sys/strsun.h>
  33   31  #include <netinet/in.h>
  34   32  #include <sys/crypto/spi.h>
  35   33  #include <modes/modes.h>
  36   34  #include "aes_impl.h"
  37   35  #ifndef _KERNEL
↓ open down ↓ 1445 lines elided ↑ open up ↑
1483 1481                  for (i = 0, j = 0; j < keysize; i++, j += 8) {
1484 1482                          /* LINTED: pointer alignment */
1485 1483                          keyarr.ka64[i] = *((uint64_t *)&cipherKey[j]);
1486 1484                  }
1487 1485          } else {
1488 1486                  bcopy(cipherKey, keyarr.ka32, keysize);
1489 1487          }
1490 1488  
1491 1489  #else   /* byte swap */
1492 1490          for (i = 0, j = 0; j < keysize; i++, j += 4) {
1493      -                keyarr.ka32[i] = (((uint32_t)cipherKey[j] << 24) |
1494      -                    ((uint32_t)cipherKey[j + 1] << 16) |
1495      -                    ((uint32_t)cipherKey[j + 2] << 8) |
1496      -                    (uint32_t)cipherKey[j + 3]);
     1491 +                keyarr.ka32[i] = htonl(*(uint32_t *)&cipherKey[j]);
1497 1492          }
1498 1493  #endif
1499 1494  
1500 1495          aes_setupkeys(newbie, keyarr.ka32, keyBits);
1501 1496  /* EXPORT DELETE END */
1502 1497  }
1503 1498  
1504 1499  /*
1505 1500   * Encrypt one block using AES.
1506 1501   * Align if needed and (for x86 32-bit only) byte-swap.
↓ open down ↓ 16 lines elided ↑ open up ↑
1523 1518                      (uint32_t *)pt, (uint32_t *)ct);
1524 1519          } else {
1525 1520  #endif
1526 1521                  uint32_t buffer[AES_BLOCK_LEN / sizeof (uint32_t)];
1527 1522  
1528 1523                  /* Copy input block into buffer */
1529 1524  #ifndef AES_BYTE_SWAP
1530 1525                  bcopy(pt, &buffer, AES_BLOCK_LEN);
1531 1526  
1532 1527  #else   /* byte swap */
1533      -                buffer[0] = (((uint32_t)pt[0] << 24) | ((uint32_t)pt[1] << 16) |
1534      -                    ((uint32_t)pt[2] << 8) | (uint32_t)pt[3]);
1535      -                buffer[1] = (((uint32_t)pt[4] << 24) | ((uint32_t)pt[5] << 16) |
1536      -                    ((uint32_t)pt[6] << 8) | (uint32_t)pt[7]);
1537      -                buffer[2] = (((uint32_t)pt[8] << 24) | ((uint32_t)pt[9] << 16) |
1538      -                    ((uint32_t)pt[10] << 8) | (uint32_t)pt[11]);
1539      -                buffer[3] = (((uint32_t)pt[12] << 24) |
1540      -                    ((uint32_t)pt[13] << 16) | ((uint32_t)pt[14] << 8) |
1541      -                    (uint32_t)pt[15]);
     1528 +                buffer[0] = htonl(*(uint32_t *)&pt[0]);
     1529 +                buffer[1] = htonl(*(uint32_t *)&pt[4]);
     1530 +                buffer[2] = htonl(*(uint32_t *)&pt[8]);
     1531 +                buffer[3] = htonl(*(uint32_t *)&pt[12]);
1542 1532  #endif
1543 1533  
1544 1534                  AES_ENCRYPT_IMPL(&ksch->encr_ks.ks32[0], ksch->nr,
1545 1535                      buffer, buffer);
1546 1536  
1547 1537                  /* Copy result from buffer to output block */
1548 1538  #ifndef AES_BYTE_SWAP
1549 1539                  bcopy(&buffer, ct, AES_BLOCK_LEN);
1550 1540          }
1551 1541  
1552 1542  #else   /* byte swap */
1553      -                ct[0] = buffer[0] >> 24;
1554      -                ct[1] = buffer[0] >> 16;
1555      -                ct[2] = buffer[0] >> 8;
1556      -                ct[3] = (uint8_t)buffer[0];
1557      -                ct[4] = buffer[1] >> 24;
1558      -                ct[5] = buffer[1] >> 16;
1559      -                ct[6] = buffer[1] >> 8;
1560      -                ct[7] = (uint8_t)buffer[1];
1561      -                ct[8] = buffer[2] >> 24;
1562      -                ct[9] = buffer[2] >> 16;
1563      -                ct[10] = buffer[2] >> 8;
1564      -                ct[11] = (uint8_t)buffer[2];
1565      -                ct[12] = buffer[3] >> 24;
1566      -                ct[13] = buffer[3] >> 16;
1567      -                ct[14] = buffer[3] >> 8;
1568      -                ct[15] = (uint8_t)buffer[3];
     1543 +                *(uint32_t *)&ct[0] = htonl(buffer[0]);
     1544 +                *(uint32_t *)&ct[4] = htonl(buffer[1]);
     1545 +                *(uint32_t *)&ct[8] = htonl(buffer[2]);
     1546 +                *(uint32_t *)&ct[12] = htonl(buffer[3]);
1569 1547  #endif
1570 1548  /* EXPORT DELETE END */
1571 1549          return (CRYPTO_SUCCESS);
1572 1550  }
1573 1551  
1574 1552  /*
1575 1553   * Decrypt one block using AES.
1576 1554   * Align and byte-swap if needed.
1577 1555   *
1578 1556   * Parameters:
↓ open down ↓ 14 lines elided ↑ open up ↑
1593 1571                      (uint32_t *)ct, (uint32_t *)pt);
1594 1572          } else {
1595 1573  #endif
1596 1574                  uint32_t buffer[AES_BLOCK_LEN / sizeof (uint32_t)];
1597 1575  
1598 1576                  /* Copy input block into buffer */
1599 1577  #ifndef AES_BYTE_SWAP
1600 1578                  bcopy(ct, &buffer, AES_BLOCK_LEN);
1601 1579  
1602 1580  #else   /* byte swap */
1603      -                buffer[0] = (((uint32_t)ct[0] << 24) | ((uint32_t)ct[1] << 16) |
1604      -                    ((uint32_t)ct[2] << 8) | (uint32_t)ct[3]);
1605      -
1606      -                buffer[1] = (((uint32_t)ct[4] << 24) | ((uint32_t)ct[5] << 16) |
1607      -                    ((uint32_t)ct[6] << 8) | (uint32_t)ct[7]);
1608      -
1609      -                buffer[2] = (((uint32_t)ct[8] << 24) | ((uint32_t)ct[9] << 16) |
1610      -                    ((uint32_t)ct[10] << 8) | (uint32_t)ct[11]);
1611      -
1612      -                buffer[3] = (((uint32_t)ct[12] << 24) |
1613      -                    ((uint32_t)ct[13] << 16) | ((uint32_t)ct[14] << 8) |
1614      -                    (uint32_t)ct[15]);
     1581 +                buffer[0] = htonl(*(uint32_t *)&ct[0]);
     1582 +                buffer[1] = htonl(*(uint32_t *)&ct[4]);
     1583 +                buffer[2] = htonl(*(uint32_t *)&ct[8]);
     1584 +                buffer[3] = htonl(*(uint32_t *)&ct[12]);
1615 1585  #endif
1616 1586  
1617 1587                  AES_DECRYPT_IMPL(&ksch->decr_ks.ks32[0], ksch->nr,
1618 1588                      buffer, buffer);
1619 1589  
1620 1590                  /* Copy result from buffer to output block */
1621 1591  #ifndef AES_BYTE_SWAP
1622 1592                  bcopy(&buffer, pt, AES_BLOCK_LEN);
1623 1593          }
1624 1594  
1625 1595  #else   /* byte swap */
1626      -                pt[0] = buffer[0] >> 24;
1627      -                pt[1] = buffer[0] >> 16;
1628      -                pt[2] = buffer[0] >> 8;
1629      -                pt[3] = (uint8_t)buffer[0];
1630      -                pt[4] = buffer[1] >> 24;
1631      -                pt[5] = buffer[1] >> 16;
1632      -                pt[6] = buffer[1] >> 8;
1633      -                pt[7] = (uint8_t)buffer[1];
1634      -                pt[8] = buffer[2] >> 24;
1635      -                pt[9] = buffer[2] >> 16;
1636      -                pt[10] = buffer[2] >> 8;
1637      -                pt[11] = (uint8_t)buffer[2];
1638      -                pt[12] = buffer[3] >> 24;
1639      -                pt[13] = buffer[3] >> 16;
1640      -                pt[14] = buffer[3] >> 8;
1641      -                pt[15] = (uint8_t)buffer[3];
     1596 +        *(uint32_t *)&pt[0] = htonl(buffer[0]);
     1597 +        *(uint32_t *)&pt[4] = htonl(buffer[1]);
     1598 +        *(uint32_t *)&pt[8] = htonl(buffer[2]);
     1599 +        *(uint32_t *)&pt[12] = htonl(buffer[3]);
1642 1600  #endif
1643 1601  
1644 1602  /* EXPORT DELETE END */
1645 1603          return (CRYPTO_SUCCESS);
1646 1604  }
1647 1605  
1648 1606  
1649 1607  /*
1650 1608   * Allocate key schedule for AES.
1651 1609   *
↓ open down ↓ 129 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX