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