OLD | NEW |
(Empty) | |
| 1 diff --git a/nss/lib/freebl/ctr.c b/nss/lib/freebl/ctr.c |
| 2 index 3a2f1a6..7807585 100644 |
| 3 --- a/nss/lib/freebl/ctr.c |
| 4 +++ b/nss/lib/freebl/ctr.c |
| 5 @@ -12,6 +12,11 @@ |
| 6 #include "pkcs11t.h" |
| 7 #include "secerr.h" |
| 8 |
| 9 +#ifdef USE_HW_AES |
| 10 +#include "intel-aes.h" |
| 11 +#include "rijndael.h" |
| 12 +#endif |
| 13 + |
| 14 SECStatus |
| 15 CTR_InitContext(CTRContext *ctr, void *context, freeblCipherFunc cipher, |
| 16 const unsigned char *param, unsigned int blocksize) |
| 17 @@ -165,3 +170,56 @@ CTR_Update(CTRContext *ctr, unsigned char *outbuf, |
| 18 *outlen += inlen; |
| 19 return SECSuccess; |
| 20 } |
| 21 + |
| 22 +#if defined(USE_HW_AES) && defined(_MSC_VER) |
| 23 +SECStatus |
| 24 +CTR_Update_HW_AES(CTRContext *ctr, unsigned char *outbuf, |
| 25 + unsigned int *outlen, unsigned int maxout, |
| 26 + const unsigned char *inbuf, unsigned int inlen, |
| 27 + unsigned int blocksize) |
| 28 +{ |
| 29 + unsigned int tmp; |
| 30 + SECStatus rv; |
| 31 + |
| 32 + if (maxout < inlen) { |
| 33 + *outlen = inlen; |
| 34 + PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
| 35 + return SECFailure; |
| 36 + } |
| 37 + *outlen = 0; |
| 38 + if (ctr->bufPtr != blocksize) { |
| 39 + unsigned int needed = PR_MIN(blocksize-ctr->bufPtr, inlen); |
| 40 + ctr_xor(outbuf, inbuf, ctr->buffer+ctr->bufPtr, needed); |
| 41 + ctr->bufPtr += needed; |
| 42 + outbuf += needed; |
| 43 + inbuf += needed; |
| 44 + *outlen += needed; |
| 45 + inlen -= needed; |
| 46 + if (inlen == 0) { |
| 47 + return SECSuccess; |
| 48 + } |
| 49 + PORT_Assert(ctr->bufPtr == blocksize); |
| 50 + } |
| 51 + |
| 52 + intel_aes_ctr_worker(((AESContext*)(ctr->context))->Nr)( |
| 53 + ctr, outbuf, outlen, maxout, inbuf, inlen, blocksize); |
| 54 + *outlen += inlen & (-16); |
| 55 + outbuf += inlen & (-16); |
| 56 + inbuf += inlen & (-16); |
| 57 + inlen &= 16 - 1; |
| 58 + |
| 59 + if (inlen == 0) { |
| 60 + return SECSuccess; |
| 61 + } |
| 62 + rv = (*ctr->cipher)(ctr->context, ctr->buffer, &tmp, blocksize, |
| 63 + ctr->counter, blocksize, blocksize); |
| 64 + ctr_GetNextCtr(ctr->counter, ctr->counterBits, blocksize); |
| 65 + if (rv != SECSuccess) { |
| 66 + return SECFailure; |
| 67 + } |
| 68 + ctr_xor(outbuf, inbuf, ctr->buffer, inlen); |
| 69 + ctr->bufPtr = inlen; |
| 70 + *outlen += inlen; |
| 71 + return SECSuccess; |
| 72 +} |
| 73 +#endif |
| 74 diff --git a/nss/lib/freebl/ctr.h b/nss/lib/freebl/ctr.h |
| 75 index 69ef150..e7645a2 100644 |
| 76 --- a/nss/lib/freebl/ctr.h |
| 77 +++ b/nss/lib/freebl/ctr.h |
| 78 @@ -41,4 +41,11 @@ SECStatus CTR_Update(CTRContext *ctr, unsigned char *outbuf, |
| 79 const unsigned char *inbuf, unsigned int inlen, |
| 80 unsigned int blocksize); |
| 81 |
| 82 +#ifdef USE_HW_AES |
| 83 +SECStatus CTR_Update_HW_AES(CTRContext *ctr, unsigned char *outbuf, |
| 84 + unsigned int *outlen, unsigned int maxout, |
| 85 + const unsigned char *inbuf, unsigned int inlen, |
| 86 + unsigned int blocksize); |
| 87 +#endif |
| 88 + |
| 89 #endif |
| 90 diff --git a/nss/lib/freebl/intel-aes.h b/nss/lib/freebl/intel-aes.h |
| 91 index 1e18007..3b71e5f 100644 |
| 92 --- a/nss/lib/freebl/intel-aes.h |
| 93 +++ b/nss/lib/freebl/intel-aes.h |
| 94 @@ -33,6 +33,12 @@ SECStatus intel_aes_decrypt_cbc_128(AESContext *cx, unsigned
char *output, |
| 95 const unsigned char *input, |
| 96 unsigned int inputLen, |
| 97 unsigned int blocksize); |
| 98 +SECStatus intel_aes_encrypt_ctr_128(CTRContext *cx, unsigned char *output, |
| 99 + unsigned int *outputLen, |
| 100 + unsigned int maxOutputLen, |
| 101 + const unsigned char *input, |
| 102 + unsigned int inputLen, |
| 103 + unsigned int blocksize); |
| 104 SECStatus intel_aes_encrypt_ecb_192(AESContext *cx, unsigned char *output, |
| 105 unsigned int *outputLen, |
| 106 unsigned int maxOutputLen, |
| 107 @@ -57,6 +63,12 @@ SECStatus intel_aes_decrypt_cbc_192(AESContext *cx, unsigned
char *output, |
| 108 const unsigned char *input, |
| 109 unsigned int inputLen, |
| 110 unsigned int blocksize); |
| 111 +SECStatus intel_aes_encrypt_ctr_192(CTRContext *cx, unsigned char *output, |
| 112 + unsigned int *outputLen, |
| 113 + unsigned int maxOutputLen, |
| 114 + const unsigned char *input, |
| 115 + unsigned int inputLen, |
| 116 + unsigned int blocksize); |
| 117 SECStatus intel_aes_encrypt_ecb_256(AESContext *cx, unsigned char *output, |
| 118 unsigned int *outputLen, |
| 119 unsigned int maxOutputLen, |
| 120 @@ -81,6 +93,12 @@ SECStatus intel_aes_decrypt_cbc_256(AESContext *cx, unsigned
char *output, |
| 121 const unsigned char *input, |
| 122 unsigned int inputLen, |
| 123 unsigned int blocksize); |
| 124 +SECStatus intel_aes_encrypt_ctr_256(CTRContext *cx, unsigned char *output, |
| 125 + unsigned int *outputLen, |
| 126 + unsigned int maxOutputLen, |
| 127 + const unsigned char *input, |
| 128 + unsigned int inputLen, |
| 129 + unsigned int blocksize); |
| 130 |
| 131 |
| 132 #define intel_aes_ecb_worker(encrypt, keysize) \ |
| 133 @@ -102,6 +120,11 @@ SECStatus intel_aes_decrypt_cbc_256(AESContext *cx, unsigne
d char *output, |
| 134 (keysize) == 24 ? intel_aes_decrypt_cbc_192 : \ |
| 135 intel_aes_decrypt_cbc_256)) |
| 136 |
| 137 +#define intel_aes_ctr_worker(nr) \ |
| 138 + ((nr) == 10 ? intel_aes_encrypt_ctr_128 : \ |
| 139 + (nr) == 12 ? intel_aes_encrypt_ctr_192 : \ |
| 140 + intel_aes_encrypt_ctr_256) |
| 141 + |
| 142 |
| 143 #define intel_aes_init(encrypt, keysize) \ |
| 144 do { \ |
| 145 diff --git a/nss/lib/freebl/intel-gcm-wrap.c b/nss/lib/freebl/intel-gcm-wrap.c |
| 146 index b2f6f5e..d49592f 100644 |
| 147 --- a/nss/lib/freebl/intel-gcm-wrap.c |
| 148 +++ b/nss/lib/freebl/intel-gcm-wrap.c |
| 149 @@ -3,7 +3,7 @@ |
| 150 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 151 /* Copyright(c) 2013, Intel Corp. */ |
| 152 |
| 153 -/* Wrapper funcions for Intel optimized implementation of AES-GCM */ |
| 154 +/* Wrapper functions for Intel optimized implementation of AES-GCM */ |
| 155 |
| 156 #ifdef USE_HW_AES |
| 157 |
| 158 @@ -24,12 +24,8 @@ |
| 159 #include "intel-gcm.h" |
| 160 #include "rijndael.h" |
| 161 |
| 162 -#if defined(__INTEL_COMPILER) |
| 163 -#include <ia32intrin.h> |
| 164 -#elif defined(__GNUC__) |
| 165 #include <emmintrin.h> |
| 166 #include <tmmintrin.h> |
| 167 -#endif |
| 168 |
| 169 |
| 170 struct intel_AES_GCMContextStr{ |
| 171 diff --git a/nss/lib/freebl/rijndael.c b/nss/lib/freebl/rijndael.c |
| 172 index 8bb8905..88f231e 100644 |
| 173 --- a/nss/lib/freebl/rijndael.c |
| 174 +++ b/nss/lib/freebl/rijndael.c |
| 175 @@ -1171,7 +1171,12 @@ AES_InitContext(AESContext *cx, const unsigned char *key,
unsigned int keysize, |
| 176 break; |
| 177 case NSS_AES_CTR: |
| 178 cx->worker_cx = CTR_CreateContext(cx, cx->worker, iv, blocksize); |
| 179 - cx->worker = (freeblCipherFunc) CTR_Update ; |
| 180 +#if defined(USE_HW_AES) && defined(_MSC_VER) |
| 181 + if (use_hw_aes) |
| 182 + cx->worker = (freeblCipherFunc) CTR_Update_HW_AES; |
| 183 + else |
| 184 +#endif |
| 185 + cx->worker = (freeblCipherFunc) CTR_Update; |
| 186 cx->destroy = (freeblDestroyFunc) CTR_DestroyContext; |
| 187 cx->isBlock = PR_FALSE; |
| 188 break; |
OLD | NEW |