|
| 1 | +/************************************************************************* |
| 2 | +* Copyright (C) 2024 Intel Corporation |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*************************************************************************/ |
| 16 | + |
| 17 | +/*! |
| 18 | + * |
| 19 | + * \file |
| 20 | + * |
| 21 | + * \brief AES Galois Counter mode of operation (GCM) example |
| 22 | + * |
| 23 | + * This example demonstrates usage of AES block cipher with 128-bit key |
| 24 | + * run with GCM mode of operation. Decryption scheme. |
| 25 | + * |
| 26 | + * The GCM mode of operation is implemented according to the |
| 27 | + * "NIST Special Publication 800-38D: Recommendation for Block Cipher Modes of |
| 28 | + * Operation: Galois/Counter Mode (GCM) and GMAC" document: |
| 29 | + * |
| 30 | + * https://csrc.nist.gov/pubs/sp/800/38/d/final |
| 31 | + * |
| 32 | + */ |
| 33 | + |
| 34 | +#include <cstring> |
| 35 | + |
| 36 | +#include "ippcp.h" |
| 37 | +#include "examples_common.h" |
| 38 | + |
| 39 | +/*! Key size in bytes */ |
| 40 | +static const int KEY_SIZE = 16; |
| 41 | + |
| 42 | +/*! Message size in bytes */ |
| 43 | +static const int MSG_LEN = 60; |
| 44 | + |
| 45 | +/*! Initialization vector size in bytes */ |
| 46 | +static const int IV_LEN = 12; |
| 47 | + |
| 48 | +/*! Tag size in bytes */ |
| 49 | +static const int TAG_LEN = 16; |
| 50 | + |
| 51 | +/*! Additional authenticated data size in bytes */ |
| 52 | +static const int AAD_LEN = 20; |
| 53 | + |
| 54 | +/*! 128-bit secret key */ |
| 55 | +static Ipp8u key128[KEY_SIZE] = { |
| 56 | + 0xfe,0xff,0xe9,0x92,0x86,0x65,0x73,0x1c, |
| 57 | + 0x6d,0x6a,0x8f,0x94,0x67,0x30,0x83,0x08 |
| 58 | +}; |
| 59 | + |
| 60 | +/*! Initialization vector */ |
| 61 | +static const Ipp8u iv[IV_LEN] = { |
| 62 | + 0xca,0xfe,0xba,0xbe,0xfa,0xce,0xdb,0xad, |
| 63 | + 0xde,0xca,0xf8,0x88 |
| 64 | +}; |
| 65 | + |
| 66 | +/*! Plain text */ |
| 67 | +static Ipp8u plainText[MSG_LEN] = { |
| 68 | + 0xd9,0x31,0x32,0x25,0xf8,0x84,0x06,0xe5, |
| 69 | + 0xa5,0x59,0x09,0xc5,0xaf,0xf5,0x26,0x9a, |
| 70 | + 0x86,0xa7,0xa9,0x53,0x15,0x34,0xf7,0xda, |
| 71 | + 0x2e,0x4c,0x30,0x3d,0x8a,0x31,0x8a,0x72, |
| 72 | + 0x1c,0x3c,0x0c,0x95,0x95,0x68,0x09,0x53, |
| 73 | + 0x2f,0xcf,0x0e,0x24,0x49,0xa6,0xb5,0x25, |
| 74 | + 0xb1,0x6a,0xed,0xf5,0xaa,0x0d,0xe6,0x57, |
| 75 | + 0xba,0x63,0x7b,0x39 |
| 76 | +}; |
| 77 | + |
| 78 | +/*! Cipher text */ |
| 79 | +static Ipp8u cipherText[MSG_LEN] = { |
| 80 | + 0x42,0x83,0x1e,0xc2,0x21,0x77,0x74,0x24, |
| 81 | + 0x4b,0x72,0x21,0xb7,0x84,0xd0,0xd4,0x9c, |
| 82 | + 0xe3,0xaa,0x21,0x2f,0x2c,0x02,0xa4,0xe0, |
| 83 | + 0x35,0xc1,0x7e,0x23,0x29,0xac,0xa1,0x2e, |
| 84 | + 0x21,0xd5,0x14,0xb2,0x54,0x66,0x93,0x1c, |
| 85 | + 0x7d,0x8f,0x6a,0x5a,0xac,0x84,0xaa,0x05, |
| 86 | + 0x1b,0xa3,0x0b,0x39,0x6a,0x0a,0xac,0x97, |
| 87 | + 0x3d,0x58,0xe0,0x91 |
| 88 | +}; |
| 89 | + |
| 90 | +/*! Tag */ |
| 91 | +static const Ipp8u tag[TAG_LEN] = { |
| 92 | + 0x5b,0xc9,0x4f,0xbc,0x32,0x21,0xa5,0xdb, |
| 93 | + 0x94,0xfa,0xe9,0x5a,0xe7,0x12,0x1a,0x47 |
| 94 | +}; |
| 95 | + |
| 96 | +/*! Additional authenticated data */ |
| 97 | +static const Ipp8u aad[AAD_LEN] = { |
| 98 | + 0xfe,0xed,0xfa,0xce,0xde,0xad,0xbe,0xef, |
| 99 | + 0xfe,0xed,0xfa,0xce,0xde,0xad,0xbe,0xef, |
| 100 | + 0xab,0xad,0xda,0xd2 |
| 101 | +}; |
| 102 | + |
| 103 | +/*! Main function */ |
| 104 | +int main(void) |
| 105 | +{ |
| 106 | + /* Size of AES-GCM context structure. It will be set up in ippsAES_GCMGetSize(). */ |
| 107 | + int AESGCMSize = 0; |
| 108 | + |
| 109 | + /* Output plain text */ |
| 110 | + Ipp8u pOutPlainText[MSG_LEN] = {}; |
| 111 | + /* Output tag */ |
| 112 | + Ipp8u pOutTag[TAG_LEN] = {}; |
| 113 | + |
| 114 | + /* Pointer to AES-GCM context structure */ |
| 115 | + IppsAES_GCMState* pAESGCMState = 0; |
| 116 | + |
| 117 | + /* Internal function status */ |
| 118 | + IppStatus status = ippStsNoErr; |
| 119 | + |
| 120 | + do { |
| 121 | + /* 1. Get size needed for AES-GCM context structure */ |
| 122 | + status = ippsAES_GCMGetSize(&AESGCMSize); |
| 123 | + if (!checkStatus("ippsAES_GCMGetSize", ippStsNoErr, status)) |
| 124 | + return status; |
| 125 | + |
| 126 | + /* 2. Allocate memory for AES-GCM context structure */ |
| 127 | + pAESGCMState = (IppsAES_GCMState*)(new Ipp8u[AESGCMSize]); |
| 128 | + if (NULL == pAESGCMState) { |
| 129 | + printf("ERROR: Cannot allocate memory (%d bytes) for AES-GCM state\n", AESGCMSize); |
| 130 | + return -1; |
| 131 | + } |
| 132 | + |
| 133 | + /* 3. Initialize AES-GCM context */ |
| 134 | + status = ippsAES_GCMInit(key128, KEY_SIZE, pAESGCMState, AESGCMSize); |
| 135 | + if (!checkStatus("ippsAES_GCMInit", ippStsNoErr, status)) |
| 136 | + break; |
| 137 | + |
| 138 | + /* 4. Decryption setup */ |
| 139 | + status = ippsAES_GCMStart(iv, IV_LEN, aad, AAD_LEN, pAESGCMState); |
| 140 | + if (!checkStatus("ippsAES_GCMStart", ippStsNoErr, status)) |
| 141 | + break; |
| 142 | + |
| 143 | + /* 5.Decryption */ |
| 144 | + status = ippsAES_GCMDecrypt(cipherText, pOutPlainText, MSG_LEN, pAESGCMState); |
| 145 | + if (!checkStatus("ippsAES_GCMDecrypt", ippStsNoErr, status)) |
| 146 | + break; |
| 147 | + |
| 148 | + /* 6. Get tag */ |
| 149 | + status = ippsAES_GCMGetTag(pOutTag, TAG_LEN, pAESGCMState); |
| 150 | + if (!checkStatus("ippsAES_GCMGetTag", ippStsNoErr, status)) |
| 151 | + break; |
| 152 | + |
| 153 | + /* Compare output to known answer */ |
| 154 | + if (0 != memcmp(pOutTag, tag, TAG_LEN)) { |
| 155 | + printf("ERROR: Output tag and reference tag do not match\n"); |
| 156 | + break; |
| 157 | + } |
| 158 | + if (0 != memcmp(pOutPlainText, plainText, MSG_LEN)) { |
| 159 | + printf("ERROR: Decrypted and plain text do not match\n"); |
| 160 | + break; |
| 161 | + } |
| 162 | + } while (0); |
| 163 | + |
| 164 | + /* 7. Remove secret and release resources */ |
| 165 | + ippsAES_GCMReset(pAESGCMState); |
| 166 | + if (pAESGCMState) |
| 167 | + delete [] (Ipp8u*)pAESGCMState; |
| 168 | + |
| 169 | + PRINT_EXAMPLE_STATUS("ippsAES_GCMDecrypt", "AES-GCM 128 Decryption", !status) |
| 170 | + |
| 171 | + return status; |
| 172 | +} |
0 commit comments