cryptography ip-cores in vhdl / verilog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.0 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <openssl/conf.h>
  4. #include <openssl/evp.h>
  5. #include <openssl/err.h>
  6. static const char HDL_LOGIC_CHAR[] = { 'U', 'X', '0', '1', 'Z', 'W', 'L', 'H', '-'};
  7. enum HDL_LOGIC_STATES {
  8. HDL_U = 0,
  9. HDL_X = 1,
  10. HDL_0 = 2,
  11. HDL_1 = 3,
  12. HDL_Z = 4,
  13. HDL_W = 5,
  14. HDL_L = 6,
  15. HDL_H = 7,
  16. HDL_D = 8,
  17. };
  18. void slv_to_uchar(char* datain, unsigned char* dataout, int bytelen) {
  19. for (int i = 0; i < bytelen; i++) {
  20. for (int y = 0; y < 8; y++) {
  21. if (*datain == HDL_1) {
  22. *dataout |= 1 << y;
  23. } else if (*datain == HDL_0) {
  24. *dataout &= ~(1 << y);
  25. }
  26. datain++;
  27. }
  28. dataout++;
  29. }
  30. return;
  31. }
  32. void slv_to_string(char* datain, char* dataout, int bytelen) {
  33. for (int i = 0; i < bytelen; i++) {
  34. *dataout = HDL_LOGIC_CHAR[*datain];
  35. datain++;
  36. dataout++;
  37. }
  38. return;
  39. }
  40. void uchar_to_slv(unsigned char* datain, char* dataout, int bytelen) {
  41. for (int i = 0; i < bytelen; i++) {
  42. for (int y = 0; y < 8; y++) {
  43. if ((*datain >> y) & 1 == 1) {
  44. *dataout = HDL_1 ;
  45. } else {
  46. *dataout = HDL_0;
  47. }
  48. dataout++;
  49. }
  50. datain++;
  51. }
  52. return;
  53. }
  54. void handleErrors(void)
  55. {
  56. ERR_print_errors_fp(stderr);
  57. abort();
  58. }
  59. int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
  60. unsigned char *ciphertext)
  61. {
  62. EVP_CIPHER_CTX *ctx;
  63. int len;
  64. int ciphertext_len;
  65. /* Create and initialise the context */
  66. if(!(ctx = EVP_CIPHER_CTX_new()))
  67. handleErrors();
  68. /*
  69. * Initialise the encryption operation. IMPORTANT - ensure you use a key
  70. * and IV size appropriate for your cipher
  71. * In this example we are using 256 bit AES (i.e. a 256 bit key). The
  72. * IV size for *most* modes is the same as the block size. For AES this
  73. * is 128 bits
  74. */
  75. if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, NULL))
  76. handleErrors();
  77. if(1 != EVP_CIPHER_CTX_set_padding(ctx, 0))
  78. handleErrors();
  79. /*
  80. * Provide the message to be encrypted, and obtain the encrypted output.
  81. * EVP_EncryptUpdate can be called multiple times if necessary
  82. */
  83. if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
  84. handleErrors();
  85. ciphertext_len = len;
  86. /*
  87. * Finalise the encryption. Further ciphertext bytes may be written at
  88. * this stage.
  89. */
  90. if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
  91. handleErrors();
  92. ciphertext_len += len;
  93. /* Clean up */
  94. EVP_CIPHER_CTX_free(ctx);
  95. return ciphertext_len;
  96. }
  97. void cryptData(char* datain, char* key, char mode, char* dataout, int len) {
  98. unsigned char c_data[len+1];
  99. unsigned char c_key[len+1];
  100. unsigned char c_data_e[len+1];
  101. int ciphertext_len;
  102. c_data[len] = 0;
  103. c_key[len] = 0;
  104. c_data_e[len] = 0;
  105. slv_to_uchar(datain, c_data, 16);
  106. slv_to_uchar(key, c_key, 16);
  107. ciphertext_len = encrypt(c_data, 128/8, c_key, c_data_e);
  108. uchar_to_slv(c_data_e, dataout, 16);
  109. return;
  110. }