bug 467298 Add support for Intel AES
r=nelson (tested on solaris 10 and RHEL 5). git-svn-id: svn://10.0.0.236/trunk@255301 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
parent
61b0b762bb
commit
faabce5ec6
@ -138,6 +138,9 @@ ifeq ($(CPU_ARCH),x86_64)
|
||||
DEFINES += -DNSS_USE_COMBA
|
||||
DEFINES += -DMP_CHAR_STORE_SLOW -DMP_IS_LITTLE_ENDIAN
|
||||
# DEFINES += -DMPI_AMD64_ADD
|
||||
# comment the next two lines to turn off intel HW accelleration
|
||||
DEFINES != -DUSE_HW_AES
|
||||
ASFLAGS += intel-aes.s
|
||||
MPI_SRCS += mpi_amd64.c mp_comba.c
|
||||
endif
|
||||
ifeq ($(CPU_ARCH),x86)
|
||||
@ -385,6 +388,9 @@ else
|
||||
endif
|
||||
DEFINES += -DNSS_BEVAND_ARCFOUR -DMPI_AMD64 -DMP_ASSEMBLY_MULTIPLY
|
||||
DEFINES += -DNSS_USE_COMBA -DMP_CHAR_STORE_SLOW -DMP_IS_LITTLE_ENDIAN
|
||||
# comment the next two lines to turn off intel HW accelleration
|
||||
DEFINES += -DUSE_HW_AES
|
||||
ASFILES += intel-aes.s
|
||||
MPI_SRCS += mpi_amd64.c
|
||||
else
|
||||
# Solaris x86
|
||||
|
||||
151
mozilla/security/nss/lib/freebl/intel-aes.h
Normal file
151
mozilla/security/nss/lib/freebl/intel-aes.h
Normal file
@ -0,0 +1,151 @@
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Red Hat, Inc, 2008.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Ulrich Drepper <drepper@redhat.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/* Prototypes of the functions defined in the assembler file. */
|
||||
void intel_aes_encrypt_init_128(const unsigned char *key, PRUint32 *expanded);
|
||||
void intel_aes_encrypt_init_192(const unsigned char *key, PRUint32 *expanded);
|
||||
void intel_aes_encrypt_init_256(const unsigned char *key, PRUint32 *expanded);
|
||||
void intel_aes_decrypt_init_128(const unsigned char *key, PRUint32 *expanded);
|
||||
void intel_aes_decrypt_init_192(const unsigned char *key, PRUint32 *expanded);
|
||||
void intel_aes_decrypt_init_256(const unsigned char *key, PRUint32 *expanded);
|
||||
SECStatus intel_aes_encrypt_ecb_128(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_decrypt_ecb_128(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_encrypt_cbc_128(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_decrypt_cbc_128(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_encrypt_ecb_192(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_decrypt_ecb_192(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_encrypt_cbc_192(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_decrypt_cbc_192(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_encrypt_ecb_256(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_decrypt_ecb_256(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_encrypt_cbc_256(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
SECStatus intel_aes_decrypt_cbc_256(AESContext *cx, unsigned char *output,
|
||||
unsigned int *outputLen,
|
||||
unsigned int maxOutputLen,
|
||||
const unsigned char *input,
|
||||
unsigned int inputLen,
|
||||
unsigned int blocksize);
|
||||
|
||||
|
||||
#define intel_aes_ecb_worker(encrypt, keysize) \
|
||||
((encrypt) \
|
||||
? ((keysize) == 16 ? intel_aes_encrypt_ecb_128 : \
|
||||
(keysize) == 24 ? intel_aes_encrypt_ecb_192 : \
|
||||
intel_aes_encrypt_ecb_256) \
|
||||
: ((keysize) == 16 ? intel_aes_decrypt_ecb_128 : \
|
||||
(keysize) == 24 ? intel_aes_decrypt_ecb_192 : \
|
||||
intel_aes_decrypt_ecb_256))
|
||||
|
||||
|
||||
#define intel_aes_cbc_worker(encrypt, keysize) \
|
||||
((encrypt) \
|
||||
? ((keysize) == 16 ? intel_aes_encrypt_cbc_128 : \
|
||||
(keysize) == 24 ? intel_aes_encrypt_cbc_192 : \
|
||||
intel_aes_encrypt_cbc_256) \
|
||||
: ((keysize) == 16 ? intel_aes_decrypt_cbc_128 : \
|
||||
(keysize) == 24 ? intel_aes_decrypt_cbc_192 : \
|
||||
intel_aes_decrypt_cbc_256))
|
||||
|
||||
|
||||
#define intel_aes_init(encrypt, keysize) \
|
||||
do { \
|
||||
if (encrypt) { \
|
||||
if (keysize == 16) \
|
||||
intel_aes_encrypt_init_128(key, cx->expandedKey); \
|
||||
else if (keysize == 24) \
|
||||
intel_aes_encrypt_init_192(key, cx->expandedKey); \
|
||||
else \
|
||||
intel_aes_encrypt_init_256(key, cx->expandedKey); \
|
||||
} else { \
|
||||
if (keysize == 16) \
|
||||
intel_aes_decrypt_init_128(key, cx->expandedKey); \
|
||||
else if (keysize == 24) \
|
||||
intel_aes_decrypt_init_192(key, cx->expandedKey); \
|
||||
else \
|
||||
intel_aes_decrypt_init_256(key, cx->expandedKey); \
|
||||
} \
|
||||
} while (0)
|
||||
1631
mozilla/security/nss/lib/freebl/intel-aes.s
Normal file
1631
mozilla/security/nss/lib/freebl/intel-aes.s
Normal file
File diff suppressed because it is too large
Load Diff
@ -33,7 +33,7 @@
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
/* $Id: rijndael.c,v 1.21 2008-11-18 19:48:23 rrelyea%redhat.com Exp $ */
|
||||
/* $Id: rijndael.c,v 1.22 2008-12-02 22:30:50 rrelyea%redhat.com Exp $ */
|
||||
|
||||
#ifdef FREEBL_NO_DEPEND
|
||||
#include "stubs.h"
|
||||
@ -47,6 +47,10 @@
|
||||
#include "blapi.h"
|
||||
#include "rijndael.h"
|
||||
|
||||
#if USE_HW_AES
|
||||
#include "intel-aes.h"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* There are currently five ways to build this code, varying in performance
|
||||
* and code size.
|
||||
@ -849,6 +853,8 @@ rijndael_encryptECB(AESContext *cx, unsigned char *output,
|
||||
{
|
||||
SECStatus rv;
|
||||
AESBlockFunc *encryptor;
|
||||
|
||||
|
||||
encryptor = (blocksize == RIJNDAEL_MIN_BLOCKSIZE)
|
||||
? &rijndael_encryptBlock128
|
||||
: &rijndael_encryptBlock;
|
||||
@ -907,6 +913,7 @@ rijndael_decryptECB(AESContext *cx, unsigned char *output,
|
||||
{
|
||||
SECStatus rv;
|
||||
AESBlockFunc *decryptor;
|
||||
|
||||
decryptor = (blocksize == RIJNDAEL_MIN_BLOCKSIZE)
|
||||
? &rijndael_decryptBlock128
|
||||
: &rijndael_decryptBlock;
|
||||
@ -934,6 +941,7 @@ rijndael_decryptCBC(AESContext *cx, unsigned char *output,
|
||||
unsigned int j;
|
||||
unsigned char newIV[RIJNDAEL_MAX_BLOCKSIZE];
|
||||
|
||||
|
||||
if (!inputLen)
|
||||
return SECSuccess;
|
||||
PORT_Assert(output - input >= 0 || input - output >= (int)inputLen );
|
||||
@ -978,11 +986,16 @@ AESContext * AES_AllocateContext(void)
|
||||
return PORT_ZNew(AESContext);
|
||||
}
|
||||
|
||||
|
||||
SECStatus
|
||||
AES_InitContext(AESContext *cx, const unsigned char *key, unsigned int keysize,
|
||||
const unsigned char *iv, int mode, unsigned int encrypt,
|
||||
unsigned int blocksize)
|
||||
{
|
||||
#if USE_HW_AES
|
||||
static int has_intel_aes;
|
||||
PRBool use_hw_aes = PR_FALSE;
|
||||
#endif
|
||||
unsigned int Nk;
|
||||
/* According to Rijndael AES Proposal, section 12.1, block and key
|
||||
* lengths between 128 and 256 bits are supported, as long as the
|
||||
@ -1010,6 +1023,17 @@ AES_InitContext(AESContext *cx, const unsigned char *key, unsigned int keysize,
|
||||
PORT_SetError(SEC_ERROR_INVALID_ARGS);
|
||||
return SECFailure;
|
||||
}
|
||||
#if USE_HW_AES
|
||||
if (has_intel_aes == 0) {
|
||||
unsigned int eax, ebx, ecx, edx;
|
||||
__asm ("cpuid"
|
||||
: "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx)
|
||||
: "0" (1));
|
||||
has_intel_aes = (ecx & (1 << 25)) != 0 ? 1 : -1;
|
||||
}
|
||||
use_hw_aes = (PRBool)
|
||||
(has_intel_aes > 0 && (keysize % 8) == 0 && blocksize == 16);
|
||||
#endif
|
||||
/* Nb = (block size in bits) / 32 */
|
||||
cx->Nb = blocksize / 4;
|
||||
/* Nk = (key size in bits) / 32 */
|
||||
@ -1019,22 +1043,51 @@ AES_InitContext(AESContext *cx, const unsigned char *key, unsigned int keysize,
|
||||
/* copy in the iv, if neccessary */
|
||||
if (mode == NSS_AES_CBC) {
|
||||
memcpy(cx->iv, iv, blocksize);
|
||||
cx->worker = (encrypt) ? &rijndael_encryptCBC : &rijndael_decryptCBC;
|
||||
#if USE_HW_AES
|
||||
if (use_hw_aes) {
|
||||
cx->worker = intel_aes_cbc_worker(encrypt, keysize);
|
||||
} else
|
||||
#endif
|
||||
cx->worker = (encrypt
|
||||
? &rijndael_encryptCBC : &rijndael_decryptCBC);
|
||||
} else {
|
||||
cx->worker = (encrypt) ? &rijndael_encryptECB : &rijndael_decryptECB;
|
||||
#if USE_HW_AES
|
||||
if (use_hw_aes) {
|
||||
cx->worker = intel_aes_ecb_worker(encrypt, keysize);
|
||||
} else
|
||||
#endif
|
||||
cx->worker = (encrypt
|
||||
? &rijndael_encryptECB : &rijndael_decryptECB);
|
||||
}
|
||||
PORT_Assert((cx->Nb * (cx->Nr + 1)) <= RIJNDAEL_MAX_EXP_KEY_SIZE);
|
||||
if ((cx->Nb * (cx->Nr + 1)) > RIJNDAEL_MAX_EXP_KEY_SIZE) {
|
||||
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
|
||||
goto cleanup;
|
||||
}
|
||||
/* Generate expanded key */
|
||||
if (encrypt) {
|
||||
if (rijndael_key_expansion(cx, key, Nk) != SECSuccess)
|
||||
goto cleanup;
|
||||
} else {
|
||||
if (rijndael_invkey_expansion(cx, key, Nk) != SECSuccess)
|
||||
goto cleanup;
|
||||
#ifdef USE_HW_AES
|
||||
if (use_hw_aes) {
|
||||
intel_aes_init(encrypt, keysize);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
|
||||
#if defined(RIJNDAEL_GENERATE_TABLES) || \
|
||||
defined(RIJNDAEL_GENERATE_TABLES_MACRO)
|
||||
if (rijndaelTables == NULL) {
|
||||
if (PR_CallOnce(&coRTInit, init_rijndael_tables)
|
||||
!= PR_SUCCESS) {
|
||||
return SecFailure;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* Generate expanded key */
|
||||
if (encrypt) {
|
||||
if (rijndael_key_expansion(cx, key, Nk) != SECSuccess)
|
||||
goto cleanup;
|
||||
} else {
|
||||
if (rijndael_invkey_expansion(cx, key, Nk) != SECSuccess)
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
return SECSuccess;
|
||||
cleanup:
|
||||
@ -1104,15 +1157,6 @@ AES_Encrypt(AESContext *cx, unsigned char *output,
|
||||
return SECFailure;
|
||||
}
|
||||
*outputLen = inputLen;
|
||||
#if defined(RIJNDAEL_GENERATE_TABLES) || \
|
||||
defined(RIJNDAEL_GENERATE_TABLES_MACRO)
|
||||
if (rijndaelTables == NULL) {
|
||||
if (PR_CallOnce(&coRTInit, init_rijndael_tables)
|
||||
!= PR_SUCCESS) {
|
||||
return PR_FAILURE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return (*cx->worker)(cx, output, outputLen, maxOutputLen,
|
||||
input, inputLen, blocksize);
|
||||
}
|
||||
@ -1144,16 +1188,6 @@ AES_Decrypt(AESContext *cx, unsigned char *output,
|
||||
return SECFailure;
|
||||
}
|
||||
*outputLen = inputLen;
|
||||
#if defined(RIJNDAEL_GENERATE_TABLES) || \
|
||||
defined(RIJNDAEL_GENERATE_TABLES_MACRO)
|
||||
if (rijndaelTables == NULL) {
|
||||
if (PR_CallOnce(&coRTInit, init_rijndael_tables)
|
||||
!= PR_SUCCESS) {
|
||||
return PR_FAILURE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return (*cx->worker)(cx, output, outputLen, maxOutputLen,
|
||||
input, inputLen, blocksize);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user