1 /********************************************************************** 2 Copyright(c) 2011-2017 Intel Corporation All rights reserved. 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions 6 are met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in 11 the documentation and/or other materials provided with the 12 distribution. 13 * Neither the name of Intel Corporation nor the names of its 14 contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 **********************************************************************/ 29 30 #include <stdio.h> 31 #include <stdint.h> 32 #include <string.h> 33 #include "aes_gcm.h" 34 35 #define TXT_SIZE 8 36 #define AAD_SIZE 32 37 #define TAG_SIZE 16 /* Valid values are 16, 12, or 8 */ 38 #define KEY_SIZE ISAL_GCM_256_KEY_LEN 39 #define IV_SIZE ISAL_GCM_IV_LEN 40 41 void 42 mprint(const char *msg, uint8_t *buf, int len) 43 { 44 int i; 45 printf("%s", msg); 46 for (i = 0; i < len;) { 47 printf(" %2x", 0xff & buf[i++]); 48 if (i % 32 == 0) 49 printf("\n"); 50 } 51 printf("\n"); 52 } 53 54 int 55 main(void) 56 { 57 struct gcm_key_data gkey; 58 struct gcm_context_data gctx; 59 uint8_t ct[TXT_SIZE], pt[TXT_SIZE], pt2[TXT_SIZE]; // Cipher text and plain text 60 uint8_t iv[IV_SIZE], aad[AAD_SIZE], key[KEY_SIZE]; // Key and authentication data 61 uint8_t tag1[TAG_SIZE], tag2[TAG_SIZE]; // Authentication tags for encode and decode 62 63 printf("gcm example:\n"); 64 memset(key, 0, KEY_SIZE); 65 memset(pt, 0, TXT_SIZE); 66 memset(iv, 0, IV_SIZE); 67 memset(aad, 0, AAD_SIZE); 68 69 isal_aes_gcm_pre_256(key, &gkey); 70 isal_aes_gcm_enc_256(&gkey, &gctx, ct, pt, TXT_SIZE, iv, aad, AAD_SIZE, tag1, TAG_SIZE); 71 isal_aes_gcm_dec_256(&gkey, &gctx, pt2, ct, TXT_SIZE, iv, aad, AAD_SIZE, tag2, TAG_SIZE); 72 73 mprint(" input text: ", pt, TXT_SIZE); 74 mprint(" cipher text: ", ct, TXT_SIZE); 75 mprint(" decode text: ", pt2, TXT_SIZE); 76 mprint(" ath tag1 (enc): ", tag1, TAG_SIZE); 77 mprint(" ath tag2 (dec): ", tag2, TAG_SIZE); 78 79 return memcmp(tag1, tag2, TAG_SIZE); 80 } 81