SSL/TLS Library

Dependents:  

CyaSSL is SSL/TLS library for embedded systems.

wolfssl.com

Committer:
wolfSSL
Date:
Sun Apr 20 12:40:57 2014 +0000
Revision:
0:9d17e4342598
CyaSSL SSL/TLS Library 2.9.4;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:9d17e4342598 1 /* aes.h
wolfSSL 0:9d17e4342598 2 *
wolfSSL 0:9d17e4342598 3 * Copyright (C) 2006-2013 wolfSSL Inc.
wolfSSL 0:9d17e4342598 4 *
wolfSSL 0:9d17e4342598 5 * This file is part of CyaSSL.
wolfSSL 0:9d17e4342598 6 *
wolfSSL 0:9d17e4342598 7 * CyaSSL is free software; you can redistribute it and/or modify
wolfSSL 0:9d17e4342598 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:9d17e4342598 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:9d17e4342598 10 * (at your option) any later version.
wolfSSL 0:9d17e4342598 11 *
wolfSSL 0:9d17e4342598 12 * CyaSSL is distributed in the hope that it will be useful,
wolfSSL 0:9d17e4342598 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:9d17e4342598 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:9d17e4342598 15 * GNU General Public License for more details.
wolfSSL 0:9d17e4342598 16 *
wolfSSL 0:9d17e4342598 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:9d17e4342598 18 * along with this program; if not, write to the Free Software
wolfSSL 0:9d17e4342598 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
wolfSSL 0:9d17e4342598 20 */
wolfSSL 0:9d17e4342598 21
wolfSSL 0:9d17e4342598 22
wolfSSL 0:9d17e4342598 23 #ifndef NO_AES
wolfSSL 0:9d17e4342598 24
wolfSSL 0:9d17e4342598 25 #ifndef CTAO_CRYPT_AES_H
wolfSSL 0:9d17e4342598 26 #define CTAO_CRYPT_AES_H
wolfSSL 0:9d17e4342598 27
wolfSSL 0:9d17e4342598 28
wolfSSL 0:9d17e4342598 29 #include <cyassl/ctaocrypt/types.h>
wolfSSL 0:9d17e4342598 30
wolfSSL 0:9d17e4342598 31 #ifdef HAVE_CAVIUM
wolfSSL 0:9d17e4342598 32 #include <cyassl/ctaocrypt/logging.h>
wolfSSL 0:9d17e4342598 33 #include "cavium_common.h"
wolfSSL 0:9d17e4342598 34 #endif
wolfSSL 0:9d17e4342598 35
wolfSSL 0:9d17e4342598 36 #ifdef CYASSL_AESNI
wolfSSL 0:9d17e4342598 37
wolfSSL 0:9d17e4342598 38 #include <wmmintrin.h>
wolfSSL 0:9d17e4342598 39
wolfSSL 0:9d17e4342598 40 #if !defined (ALIGN16)
wolfSSL 0:9d17e4342598 41 #if defined (__GNUC__)
wolfSSL 0:9d17e4342598 42 #define ALIGN16 __attribute__ ( (aligned (16)))
wolfSSL 0:9d17e4342598 43 #elif defined(_MSC_VER)
wolfSSL 0:9d17e4342598 44 #define ALIGN16 __declspec (align (16))
wolfSSL 0:9d17e4342598 45 #else
wolfSSL 0:9d17e4342598 46 #define ALIGN16
wolfSSL 0:9d17e4342598 47 #endif
wolfSSL 0:9d17e4342598 48 #endif
wolfSSL 0:9d17e4342598 49
wolfSSL 0:9d17e4342598 50 #endif /* CYASSL_AESNI */
wolfSSL 0:9d17e4342598 51
wolfSSL 0:9d17e4342598 52 #if !defined (ALIGN16)
wolfSSL 0:9d17e4342598 53 #define ALIGN16
wolfSSL 0:9d17e4342598 54 #endif
wolfSSL 0:9d17e4342598 55
wolfSSL 0:9d17e4342598 56 #ifdef __cplusplus
wolfSSL 0:9d17e4342598 57 extern "C" {
wolfSSL 0:9d17e4342598 58 #endif
wolfSSL 0:9d17e4342598 59
wolfSSL 0:9d17e4342598 60
wolfSSL 0:9d17e4342598 61 #define CYASSL_AES_CAVIUM_MAGIC 0xBEEF0002
wolfSSL 0:9d17e4342598 62
wolfSSL 0:9d17e4342598 63 enum {
wolfSSL 0:9d17e4342598 64 AES_ENC_TYPE = 1, /* cipher unique type */
wolfSSL 0:9d17e4342598 65 AES_ENCRYPTION = 0,
wolfSSL 0:9d17e4342598 66 AES_DECRYPTION = 1,
wolfSSL 0:9d17e4342598 67 AES_BLOCK_SIZE = 16
wolfSSL 0:9d17e4342598 68 };
wolfSSL 0:9d17e4342598 69
wolfSSL 0:9d17e4342598 70
wolfSSL 0:9d17e4342598 71 typedef struct Aes {
wolfSSL 0:9d17e4342598 72 /* AESNI needs key first, rounds 2nd, not sure why yet */
wolfSSL 0:9d17e4342598 73 ALIGN16 word32 key[60];
wolfSSL 0:9d17e4342598 74 word32 rounds;
wolfSSL 0:9d17e4342598 75
wolfSSL 0:9d17e4342598 76 ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
wolfSSL 0:9d17e4342598 77 ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)]; /* same */
wolfSSL 0:9d17e4342598 78
wolfSSL 0:9d17e4342598 79 #ifdef HAVE_AESGCM
wolfSSL 0:9d17e4342598 80 ALIGN16 byte H[AES_BLOCK_SIZE];
wolfSSL 0:9d17e4342598 81 #ifdef GCM_TABLE
wolfSSL 0:9d17e4342598 82 /* key-based fast multiplication table. */
wolfSSL 0:9d17e4342598 83 ALIGN16 byte M0[256][AES_BLOCK_SIZE];
wolfSSL 0:9d17e4342598 84 #endif /* GCM_TABLE */
wolfSSL 0:9d17e4342598 85 #endif /* HAVE_AESGCM */
wolfSSL 0:9d17e4342598 86 #ifdef CYASSL_AESNI
wolfSSL 0:9d17e4342598 87 byte use_aesni;
wolfSSL 0:9d17e4342598 88 #endif /* CYASSL_AESNI */
wolfSSL 0:9d17e4342598 89 #ifdef HAVE_CAVIUM
wolfSSL 0:9d17e4342598 90 AesType type; /* aes key type */
wolfSSL 0:9d17e4342598 91 int devId; /* nitrox device id */
wolfSSL 0:9d17e4342598 92 word32 magic; /* using cavium magic */
wolfSSL 0:9d17e4342598 93 word64 contextHandle; /* nitrox context memory handle */
wolfSSL 0:9d17e4342598 94 #endif
wolfSSL 0:9d17e4342598 95 #ifdef CYASSL_AES_COUNTER
wolfSSL 0:9d17e4342598 96 word32 left; /* unsued bytes left from last call */
wolfSSL 0:9d17e4342598 97 #endif
wolfSSL 0:9d17e4342598 98 #ifdef CYASSL_PIC32MZ_CRYPT
wolfSSL 0:9d17e4342598 99 word32 key_ce[AES_BLOCK_SIZE*2/sizeof(word32)] ;
wolfSSL 0:9d17e4342598 100 word32 iv_ce [AES_BLOCK_SIZE /sizeof(word32)] ;
wolfSSL 0:9d17e4342598 101 int keylen ;
wolfSSL 0:9d17e4342598 102 #endif
wolfSSL 0:9d17e4342598 103 } Aes;
wolfSSL 0:9d17e4342598 104
wolfSSL 0:9d17e4342598 105
wolfSSL 0:9d17e4342598 106 CYASSL_API int AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
wolfSSL 0:9d17e4342598 107 int dir);
wolfSSL 0:9d17e4342598 108 CYASSL_API int AesSetIV(Aes* aes, const byte* iv);
wolfSSL 0:9d17e4342598 109 CYASSL_API int AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz);
wolfSSL 0:9d17e4342598 110 CYASSL_API int AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz);
wolfSSL 0:9d17e4342598 111 CYASSL_API void AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz);
wolfSSL 0:9d17e4342598 112 CYASSL_API void AesEncryptDirect(Aes* aes, byte* out, const byte* in);
wolfSSL 0:9d17e4342598 113 CYASSL_API void AesDecryptDirect(Aes* aes, byte* out, const byte* in);
wolfSSL 0:9d17e4342598 114 CYASSL_API int AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
wolfSSL 0:9d17e4342598 115 const byte* iv, int dir);
wolfSSL 0:9d17e4342598 116 #ifdef HAVE_AESGCM
wolfSSL 0:9d17e4342598 117 CYASSL_API void AesGcmSetKey(Aes* aes, const byte* key, word32 len);
wolfSSL 0:9d17e4342598 118 CYASSL_API void AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
wolfSSL 0:9d17e4342598 119 const byte* iv, word32 ivSz,
wolfSSL 0:9d17e4342598 120 byte* authTag, word32 authTagSz,
wolfSSL 0:9d17e4342598 121 const byte* authIn, word32 authInSz);
wolfSSL 0:9d17e4342598 122 CYASSL_API int AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
wolfSSL 0:9d17e4342598 123 const byte* iv, word32 ivSz,
wolfSSL 0:9d17e4342598 124 const byte* authTag, word32 authTagSz,
wolfSSL 0:9d17e4342598 125 const byte* authIn, word32 authInSz);
wolfSSL 0:9d17e4342598 126
wolfSSL 0:9d17e4342598 127 typedef struct Gmac {
wolfSSL 0:9d17e4342598 128 Aes aes;
wolfSSL 0:9d17e4342598 129 } Gmac;
wolfSSL 0:9d17e4342598 130 CYASSL_API void GmacSetKey(Gmac* gmac, const byte* key, word32 len);
wolfSSL 0:9d17e4342598 131 CYASSL_API void GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
wolfSSL 0:9d17e4342598 132 const byte* authIn, word32 authInSz,
wolfSSL 0:9d17e4342598 133 byte* authTag, word32 authTagSz);
wolfSSL 0:9d17e4342598 134 #endif /* HAVE_AESGCM */
wolfSSL 0:9d17e4342598 135 #ifdef HAVE_AESCCM
wolfSSL 0:9d17e4342598 136 CYASSL_API void AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
wolfSSL 0:9d17e4342598 137 CYASSL_API void AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
wolfSSL 0:9d17e4342598 138 const byte* nonce, word32 nonceSz,
wolfSSL 0:9d17e4342598 139 byte* authTag, word32 authTagSz,
wolfSSL 0:9d17e4342598 140 const byte* authIn, word32 authInSz);
wolfSSL 0:9d17e4342598 141 CYASSL_API int AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
wolfSSL 0:9d17e4342598 142 const byte* nonce, word32 nonceSz,
wolfSSL 0:9d17e4342598 143 const byte* authTag, word32 authTagSz,
wolfSSL 0:9d17e4342598 144 const byte* authIn, word32 authInSz);
wolfSSL 0:9d17e4342598 145 #endif /* HAVE_AESCCM */
wolfSSL 0:9d17e4342598 146
wolfSSL 0:9d17e4342598 147 #ifdef HAVE_CAVIUM
wolfSSL 0:9d17e4342598 148 CYASSL_API int AesInitCavium(Aes*, int);
wolfSSL 0:9d17e4342598 149 CYASSL_API void AesFreeCavium(Aes*);
wolfSSL 0:9d17e4342598 150 #endif
wolfSSL 0:9d17e4342598 151
wolfSSL 0:9d17e4342598 152
wolfSSL 0:9d17e4342598 153 #ifdef HAVE_FIPS
wolfSSL 0:9d17e4342598 154 /* fips wrapper calls, user can call direct */
wolfSSL 0:9d17e4342598 155 CYASSL_API int AesSetKey_fips(Aes* aes, const byte* key, word32 len,
wolfSSL 0:9d17e4342598 156 const byte* iv, int dir);
wolfSSL 0:9d17e4342598 157 CYASSL_API int AesSetIV_fips(Aes* aes, const byte* iv);
wolfSSL 0:9d17e4342598 158 CYASSL_API int AesCbcEncrypt_fips(Aes* aes, byte* out, const byte* in,
wolfSSL 0:9d17e4342598 159 word32 sz);
wolfSSL 0:9d17e4342598 160 CYASSL_API int AesCbcDecrypt_fips(Aes* aes, byte* out, const byte* in,
wolfSSL 0:9d17e4342598 161 word32 sz);
wolfSSL 0:9d17e4342598 162 #ifndef FIPS_NO_WRAPPERS
wolfSSL 0:9d17e4342598 163 /* if not impl or fips.c impl wrapper force fips calls if fips build */
wolfSSL 0:9d17e4342598 164 #define AesSetKey AesSetKey_fips
wolfSSL 0:9d17e4342598 165 #define AesSetIV AesSetIV_fips
wolfSSL 0:9d17e4342598 166 #define AesCbcEncrypt AesCbcEncrypt_fips
wolfSSL 0:9d17e4342598 167 #define AesCbcDecrypt AesCbcDecrypt_fips
wolfSSL 0:9d17e4342598 168 #endif /* FIPS_NO_WRAPPERS */
wolfSSL 0:9d17e4342598 169
wolfSSL 0:9d17e4342598 170 #endif /* HAVE_FIPS */
wolfSSL 0:9d17e4342598 171
wolfSSL 0:9d17e4342598 172
wolfSSL 0:9d17e4342598 173 #ifdef __cplusplus
wolfSSL 0:9d17e4342598 174 } /* extern "C" */
wolfSSL 0:9d17e4342598 175 #endif
wolfSSL 0:9d17e4342598 176
wolfSSL 0:9d17e4342598 177
wolfSSL 0:9d17e4342598 178 #endif /* CTAO_CRYPT_AES_H */
wolfSSL 0:9d17e4342598 179 #endif /* NO_AES */
wolfSSL 0:9d17e4342598 180