PyMODINIT_FUNC init_pycryptopp(void) { PyObject *module; module = Py_InitModule3("_pycryptopp", _pycryptopp_functions, _pycryptopp__doc__); if (!module) return; PyObject* version; /* a tuple of (Crypto++ version, extra-version) */ #ifndef DISABLE_EMBEDDED_CRYPTOPP /* In the version of Crypto++ which is included in pycryptopp, there is a symbol named `cryptopp_extra_version' which is declared (external variable) in config.h and defined in cryptlib.cpp. Of course it is possible that the header file we've #include'd is from the embedded-in-pycryptopp version of Crypto++ but the dynamically linked library that we load is from an older version which doesn't have this symbol. In that case, the load will fail before we get this far. */ version = Py_BuildValue("is", CRYPTOPP_VERSION, cryptopp_extra_version); #else version = Py_BuildValue("iO", CRYPTOPP_VERSION, Py_None); #endif int succ = PyModule_AddObject(module, "cryptopp_version", version); if (succ != 0) return; init_ecdsa(module); init_rsa(module); init_sha256(module); init_aes(module); init_xsalsa20(module); }
int main(int argc, char *argv[]) { // unsigned char key[KEY_128] = { // 0xff, 0xfe, 0xfd, 0xfc, // 0xfb, 0xfa, 0xf9, 0xf8, // 0xf7, 0xf6, 0xf5, 0xf4, // 0xf3, 0xf2, 0xf1, 0xf0 // }; unsigned char key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; // unsigned char ptext[16] = { // 0x00, 0x01, 0x02, 0x03, // 0x04, 0x05, 0x06, 0x07, // 0x08, 0x09, 0x0a, 0x0b, // 0x0c, 0x0d, 0x0e, 0x0f // }; unsigned char ptext[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; unsigned char ctext[16]; unsigned char decptext[16]; aes_ctx_t *ctx; int i; init_aes(); ctx = aes_alloc_ctx(key, sizeof(key)); if(!ctx) { perror("aes_alloc_ctx"); return EXIT_FAILURE; } print_block_paralel("keys ", 1, 0x40, 4, ctx->keysched); print_block_paralel("keys ", 3, 16, 4, ptext, ctext, decptext); aes_encrypt(ctx, ptext, ctext); aes_decrypt(ctx, ctext, decptext); i = 16; print_block_paralel("plaintext ciphertext dectext ", 3, 16, 4, ptext, ctext, decptext); aes_free_ctx(ctx); return EXIT_SUCCESS; }
/** * \brief Function to generate the final firmware */ static bool generate_firmware(void) { uint32_t address_offset = 0; void *buf = NULL; uint32_t buffer_size = 0; uint8_t signature_bytes[] = APP_SIGNATURE; uint8_t *temp = (uint8_t *) buffer; FRESULT res; /* Store the CRC32 value */ *(uint32_t *)temp = firmware_crc; address_offset = 4; /* Store the signature bytes */ while (address_offset < APP_BINARY_OFFSET) { temp[address_offset] = signature_bytes[address_offset - 4]; address_offset++; } buffer_size = APP_CRC_SIZE + APP_SIGNATURE_SIZE; address_offset = 0; #if FIRMWARE_AES_ENABLED /* Initialize the AES Module */ init_aes(); /* Encrypt the CRC32 and Signature bytes */ aes_encrypt((uint32_t *)buffer, buffer_size/4); /* Update the output buffer */ buf = (void *) aes_output; #else /* Update the output buffer */ buf = (void *) buffer; #endif /* Open the output file for writing the data */ res = f_open(&file_object2, (char const *)output_file_name, FA_WRITE | FA_CREATE_ALWAYS); /* Check the return status */ if (res != FR_OK) { /* LUN error */ f_close(&file_object2); return false; } /* Write the CRC32 and Signature bytes into the output firmware */ f_write(&file_object2, buf, buffer_size, &buffer_size); /* Flush the data into the file */ f_sync(&file_object2); /* Close the input file */ f_close(&file_object2); while (true) { /* Open the input file */ f_open(&file_object1, (char const *)input_file_name, FA_OPEN_EXISTING | FA_READ); /* Seek the file pointer to the current address offset */ f_lseek(&file_object1, address_offset); /* Read the data from the firmware */ f_read(&file_object1, (uint8_t *)buffer, FLASH_BUFFER_SIZE, &buffer_size); /* Check if there is any buffer */ if (!buffer_size) { /* Close the open file */ f_close(&file_object1); /* Break out of the loop */ break; } /* Close the input file */ f_close(&file_object1); #if FIRMWARE_AES_ENABLED /* Encrypt the binary data */ aes_encrypt((uint32_t *)buffer, buffer_size/4); /* Update the output buffer */ buf = (void *)aes_output; #else /* Update the output buffer */ buf = (void *)buffer; #endif /* Open the output file for writing */ f_open(&file_object2, (char const *)output_file_name, FA_OPEN_EXISTING | FA_WRITE); /* Seek the file pointer to the current address offset */ f_lseek(&file_object2, address_offset + APP_BINARY_OFFSET); /* Store the buffer into the output file */ f_write(&file_object2, buf, buffer_size, &buffer_size); /* Flush the data into the file */ f_sync(&file_object2); /* Close the output file */ f_close(&file_object2); /* Update the address offset */ address_offset += buffer_size; } return true; }