| RSA BSAFE Crypto-C |
Cryptographic Components for C |
| Search |
/* $Id: seed.c,v 1.6 2004/12/03 02:08:42 sparki Exp $ */ /* * Copyright (C) 1998-2004 RSA Security Inc. * * This file shall only be used to demonstrate how to interface to an * RSA Security Inc. licensed development product. * * You have a royalty-free right to use, reproduce and distribute this * demonstration file, provided that you agree that RSA Security Inc. * has no warranty, implied or otherwise, or liability for this * demonstration file (including any modified version). This software * is provided "as is" without warranties or representations of any * kind. RSA Security disclaims all conditions and warranties, statutory * and otherwise, both express and implied, with respect to the software, * its quality and performance, including but not limited to, all * implied warranties of merchantability, fitness for a particular * purpose, title and noninfringement of third party rights. Without * limiting the foregoing, RSA Security does not warrant that the * software is error-free or that errors in the product will be * corrected. You agree that RSA Security shall not be liable for any * direct, indirect, incidental, special, consequential, punitive or * other damages whatsoever resulting from your use of this software * or any modified version. * * */ /* * This program encrypts and decrypts using SEED in CBC with padding. */ #include "bsafe.h" #include "bsfutil.h" #include "demoutil.h" #define KEY_SIZE 16 /* SEED only takes 128-bit keys */ #define BLOCK_SIZE 16 #ifdef CRYPTOC_APP #define MAIN seedMain #else #define MAIN main #endif int MAIN(int argc, char *argv[]) { int status = 0; B_KEY_OBJ seedKey = (B_KEY_OBJ)NULL_PTR; B_ALGORITHM_OBJ seedEncrypter = (B_ALGORITHM_OBJ)NULL_PTR; B_ALGORITHM_OBJ seedDecrypter = (B_ALGORITHM_OBJ)NULL_PTR; B_ALGORITHM_OBJ randomAlgorithm = (B_ALGORITHM_OBJ)NULL_PTR; unsigned char initVector[BLOCK_SIZE]; ITEM seedKeyItem = {NULL, 0}; unsigned char *dataToEncrypt = (unsigned char *)"Encrypt this sentence."; unsigned int dataToEncryptLen; unsigned char *encryptedData = NULL_PTR; unsigned int outputLenUpdate, outputLenFinal, outputLenTotal; unsigned int encryptedDataLen; unsigned char *decryptedData = NULL_PTR; unsigned int decryptedLenUpdate, decryptedLenFinal; unsigned int decryptedLenTotal, decryptedDataLen; B_ALGORITHM_METHOD *SEED_CHOOSER[] = { &AM_SEED_CBC_ENCRYPT, &AM_SEED_CBC_DECRYPT, (B_ALGORITHM_METHOD *)NULL_PTR /* This will fix a problem that the IA64 compiler finds when * * seeing a short chooser list */ #ifdef IA64_FORCE_LARGE IA64_FORCE_LARGE #endif }; do { /* The RSA_* demo code utilities are described in common/include/demoutil.h. This procedure simply checks the command-line arguments for input or output options. */ if ((status = RSA_SetOptions (argc, argv)) != 0) break; RSA_PrintMessage ("SEED algorithm: Encryption phase \n"); RSA_PrintMessage ("================================ \n"); /* Create an algorithm object. */ if ((status = B_CreateAlgorithmObject (&seedEncrypter)) != 0) break; /* Initialize a random algorithm object using a procedure described in samples/common/include/bsfutil.h */ if ((status = RSA_CreateRandomAlgorithmObject (&randomAlgorithm)) != 0) break; /* Generate a random initialization vector */ if ((status = B_GenerateRandomBytes (randomAlgorithm, (unsigned char *)initVector, BLOCK_SIZE, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; RSA_PrintBuf ("Random Initialization Vector", initVector, sizeof (initVector)); /* Set the algorithm to a type that does SEED encryption in CBC mode with padding. */ if ((status = B_SetAlgorithmInfo (seedEncrypter, AI_SeedBlockCipherCBCPad, (POINTER)&initVector)) != 0) break; /* Create a key object */ if ((status = B_CreateKeyObject (&seedKey)) != 0) break; /* Set the key object with a random SEED key */ seedKeyItem.len = KEY_SIZE; seedKeyItem.data = T_malloc (seedKeyItem.len); if ((status = (seedKeyItem.data == NULL_PTR)) != 0) break; if ((status = B_GenerateRandomBytes (randomAlgorithm, seedKeyItem.data, seedKeyItem.len, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; RSA_PrintBuf ("Random SEED key", seedKeyItem.data, seedKeyItem.len); if ((status = B_SetKeyInfo (seedKey, KI_Item, (POINTER)&seedKeyItem)) != 0) break; /* Let's zeroize the memory and free it up immediately after setting the key for security reasons. */ if (seedKeyItem.data != NULL_PTR) { T_memset (seedKeyItem.data, 0, seedKeyItem.len); T_free (seedKeyItem.data); seedKeyItem.data = NULL_PTR; seedKeyItem.len = 0; } /* Init */ if ((status = B_EncryptInit (seedEncrypter, seedKey, SEED_CHOOSER, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; /* Update */ dataToEncryptLen = T_strlen ((char *)dataToEncrypt) + 1; RSA_PrintBuf ("Data To Encrypt", dataToEncrypt, dataToEncryptLen); encryptedDataLen = dataToEncryptLen + BLOCK_SIZE; encryptedData = T_malloc (encryptedDataLen); if ((status = (encryptedData == NULL_PTR)) != 0) { status = RSA_DEMO_E_ALLOC; break; } if ((status = B_EncryptUpdate (seedEncrypter, encryptedData, &outputLenUpdate, encryptedDataLen, dataToEncrypt, dataToEncryptLen, (B_ALGORITHM_OBJ)NULL_PTR, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; /* Final */ if ((status = B_EncryptFinal (seedEncrypter, encryptedData + outputLenUpdate, &outputLenFinal, encryptedDataLen - outputLenUpdate, (B_ALGORITHM_OBJ)NULL_PTR, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; outputLenTotal = outputLenUpdate + outputLenFinal; RSA_PrintBuf ("Encrypted Data", encryptedData, outputLenTotal); RSA_PrintMessage ("\nSEED algorithm: Decryption phase \n"); RSA_PrintMessage ("================================ \n"); if ((status = B_CreateAlgorithmObject (&seedDecrypter)) != 0) break; /* Use the same AI and parameters as for encryption */ if ((status = B_SetAlgorithmInfo (seedDecrypter, AI_SeedBlockCipherCBCPad, (POINTER)&initVector)) != 0) break; /* Use the same key as for encryption */ if ((status = B_DecryptInit (seedDecrypter, seedKey, SEED_CHOOSER, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; /* Set the buffer that will take the decrypted data to be the same size as the encrypted data's buffer. */ decryptedDataLen = encryptedDataLen; decryptedData = T_malloc (decryptedDataLen); if ((status = (decryptedData == NULL_PTR)) != 0) { status = RSA_DEMO_E_ALLOC; break; } if ((status = B_DecryptUpdate (seedDecrypter, decryptedData, &decryptedLenUpdate, decryptedDataLen, encryptedData, outputLenTotal, (B_ALGORITHM_OBJ)NULL_PTR, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; if ((status = B_DecryptFinal (seedDecrypter, decryptedData + decryptedLenUpdate, &decryptedLenFinal, decryptedDataLen - decryptedLenUpdate, (B_ALGORITHM_OBJ)NULL_PTR, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; decryptedLenTotal = decryptedLenUpdate + decryptedLenFinal; RSA_PrintBuf ("Decrypted Data", decryptedData, decryptedLenTotal); if ((decryptedLenTotal == dataToEncryptLen) && (T_memcmp (dataToEncrypt, decryptedData, decryptedLenTotal)) == 0) { RSA_PrintMessage ("Success! "); RSA_PrintMessage ("The decrypted data matches the original data.\n"); } else { RSA_PrintMessage ("The decrypted data does not match the original data."); status = RSA_DEMO_E_INFO_DOES_NOT_VERIFY; } } while (0); if (status != 0) RSA_PrintError ("seed example", status); /* Destroy the key and algorithm objects */ B_DestroyKeyObject (&seedKey); B_DestroyAlgorithmObject (&seedEncrypter); B_DestroyAlgorithmObject (&randomAlgorithm); B_DestroyAlgorithmObject (&seedDecrypter); /* Free up any memory allocated, save it to a file or print it out first if you need to save it. */ if (encryptedData != NULL_PTR) { T_memset (encryptedData, 0, encryptedDataLen); T_free (encryptedData); encryptedData = NULL_PTR; encryptedDataLen = 0; } if (decryptedData != NULL_PTR) { T_memset (decryptedData, 0, decryptedDataLen); T_free (decryptedData); decryptedData = NULL_PTR; decryptedDataLen = 0; } if (seedKeyItem.data != NULL_PTR) { T_memset (seedKeyItem.data, 0, seedKeyItem.len); T_free (seedKeyItem.data); seedKeyItem.data = NULL_PTR; seedKeyItem.len = 0; } return (status); } /* end main */