Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file aesni.h |
| 3 | * |
| 4 | * \brief AES-NI for hardware AES acceleration on some Intel processors |
| 5 | * |
| 6 | * Copyright (C) 2013, Brainspark B.V. |
| 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_AESNI_H |
| 28 | #define POLARSSL_AESNI_H |
| 29 | |
| 30 | #include "aes.h" |
| 31 | |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 32 | #define POLARSSL_AESNI_AES 0x02000000u |
| 33 | #define POLARSSL_AESNI_CLMUL 0x00000002u |
| 34 | |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 35 | #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \ |
| 36 | ( defined(__amd64__) || defined(__x86_64__) ) && \ |
| 37 | ! defined(POLARSSL_HAVE_X86_64) |
| 38 | #define POLARSSL_HAVE_X86_64 |
| 39 | #endif |
| 40 | |
| 41 | #if defined(POLARSSL_HAVE_X86_64) |
| 42 | |
| 43 | /** |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 44 | * \brief AES-NI features detection routine |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 45 | * |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 46 | * \param what The feature to detect |
| 47 | * (POLARSSL_AESNI_AES or POLARSSL_AESNI_CLMUL) |
| 48 | * |
| 49 | * \return 1 if CPU has support for the feature, 0 otherwise |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 50 | */ |
Manuel Pégourié-Gonnard | 8eaf20b | 2013-12-18 19:14:53 +0100 | [diff] [blame] | 51 | int aesni_supports( unsigned int what ); |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 53 | /** |
| 54 | * \brief AES-NI AES-ECB block en(de)cryption |
| 55 | * |
| 56 | * \param ctx AES context |
| 57 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 58 | * \param input 16-byte input block |
| 59 | * \param output 16-byte output block |
| 60 | * |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 61 | * \return 0 on success (cannot fail) |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 62 | */ |
| 63 | int aesni_crypt_ecb( aes_context *ctx, |
| 64 | int mode, |
| 65 | const unsigned char input[16], |
| 66 | unsigned char output[16] ); |
| 67 | |
Manuel Pégourié-Gonnard | d333f67 | 2013-12-26 11:44:46 +0100 | [diff] [blame] | 68 | /** |
| 69 | * \brief GCM multiplication: c = a * b in GF(2^128) |
| 70 | * |
| 71 | * \param c Result |
| 72 | * \param a First operand |
| 73 | * \param b Second operand |
| 74 | * |
| 75 | * \note Both operands and result are bit strings interpreted as |
| 76 | * elements of GF(2^128) as per the GCM spec. |
| 77 | * |
| 78 | * \return 0 on success (cannot fail) |
| 79 | */ |
| 80 | int aesni_gcm_mult( unsigned char c[16], |
| 81 | const unsigned char a[16], |
| 82 | const unsigned char b[16] ); |
| 83 | |
Manuel Pégourié-Gonnard | 01e31bb | 2013-12-28 15:58:30 +0100 | [diff] [blame] | 84 | /** |
| 85 | * \brief Compute decryption round keys from encryption round keys |
| 86 | * |
| 87 | * \param invkey Round keys for the equivalent inverse cipher |
| 88 | * \param fwdkey Original round keys (for encryption) |
| 89 | * \param nr Number of rounds (that is, number of round keys minus one) |
| 90 | */ |
| 91 | void aesni_inverse_key( unsigned char *invkey, |
| 92 | const unsigned char *fwdkey, int nr ); |
| 93 | |
Manuel Pégourié-Gonnard | 47a3536 | 2013-12-28 20:45:04 +0100 | [diff] [blame^] | 94 | /** |
| 95 | * \brief Perform key expansion (for encryption) |
| 96 | * |
| 97 | * \param rk Destination buffer where the round keys are written |
| 98 | * \param key Encryption key |
| 99 | * \param bits Key size in bits (must be 128, 192 or 256) |
| 100 | * |
| 101 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
| 102 | */ |
| 103 | int aesni_setkey_enc( unsigned char *rk, |
| 104 | const unsigned char *key, |
| 105 | size_t bits ); |
| 106 | |
Manuel Pégourié-Gonnard | 92ac76f | 2013-12-16 17:12:53 +0100 | [diff] [blame] | 107 | #endif /* POLARSSL_HAVE_X86_64 */ |
| 108 | |
| 109 | #endif /* POLARSSL_AESNI_H */ |