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.

182 lines
3.4 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. EVP_CIPHER_CTX *ctx;
  19. void slv_to_uchar(char* datain, unsigned char* dataout, int bytelen) {
  20. for (int i = 0; i < bytelen; i++) {
  21. for (int y = 0; y < 8; y++) {
  22. if (*datain == HDL_1) {
  23. *dataout |= 1 << y;
  24. } else if (*datain == HDL_0) {
  25. *dataout &= ~(1 << y);
  26. }
  27. datain++;
  28. }
  29. dataout++;
  30. }
  31. return;
  32. }
  33. void slv_to_string(char* datain, char* dataout, int bytelen) {
  34. for (int i = 0; i < bytelen; i++) {
  35. *dataout = HDL_LOGIC_CHAR[*datain];
  36. datain++;
  37. dataout++;
  38. }
  39. return;
  40. }
  41. void uchar_to_slv(unsigned char* datain, char* dataout, int bytelen) {
  42. for (int i = 0; i < bytelen; i++) {
  43. for (int y = 0; y < 8; y++) {
  44. if ((*datain >> y) & 1 == 1) {
  45. *dataout = HDL_1 ;
  46. } else {
  47. *dataout = HDL_0;
  48. }
  49. dataout++;
  50. }
  51. datain++;
  52. }
  53. return;
  54. }
  55. void handleErrors(void) {
  56. ERR_print_errors_fp(stderr);
  57. abort();
  58. }
  59. // Create and initialize the context and nitialize the
  60. // de/encryption operation with keys and iv.
  61. // No padding is done
  62. void init(unsigned char *key, unsigned char *iv, char mode) {
  63. if (!(ctx = EVP_CIPHER_CTX_new()))
  64. handleErrors();
  65. if (mode) {
  66. if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
  67. handleErrors();
  68. } else {
  69. if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
  70. handleErrors();
  71. }
  72. if(1 != EVP_CIPHER_CTX_set_padding(ctx, 0))
  73. handleErrors();
  74. }
  75. // Provide the message to be de/encrypted and
  76. // obtain the encrypted output
  77. int crypt(char mode,
  78. unsigned char *input,
  79. int input_len,
  80. unsigned char *output) {
  81. int len = 0;
  82. if (mode) {
  83. if (1 != EVP_DecryptUpdate(ctx, output, &len, input, input_len))
  84. handleErrors();
  85. } else {
  86. if (1 != EVP_EncryptUpdate(ctx, output, &len, input, input_len))
  87. handleErrors();
  88. }
  89. return len;
  90. }
  91. // Finalize the de/encryption. No further bytes are written
  92. // as padding is switched off
  93. int finalize(char mode) {
  94. int len = 0;
  95. unsigned char data[16];
  96. if (mode) {
  97. if (1 != EVP_DecryptFinal_ex(ctx, data, &len))
  98. handleErrors();
  99. } else {
  100. if (1 != EVP_EncryptFinal_ex(ctx, data, &len))
  101. handleErrors();
  102. }
  103. // Clean up
  104. EVP_CIPHER_CTX_free(ctx);
  105. return len;
  106. }
  107. void cryptData(char* datain, char* key, char* iv, char mode, char start, char final, char* dataout, int bytelen) {
  108. int crypt_len;
  109. unsigned char c_din[bytelen];
  110. unsigned char c_key[bytelen];
  111. unsigned char c_iv[bytelen];
  112. unsigned char c_dout[bytelen];
  113. slv_to_uchar(datain, c_din, bytelen);
  114. slv_to_uchar(key, c_key, bytelen);
  115. slv_to_uchar(iv, c_iv, bytelen);
  116. if (start) {
  117. init(c_key, c_iv, mode);
  118. }
  119. crypt_len = crypt(mode, c_din, bytelen, c_dout);
  120. if (crypt_len != bytelen) {
  121. printf("Warning: encrypt() returned with unexpected length %d\n", crypt_len);
  122. }
  123. if (final) {
  124. crypt_len = finalize(mode);
  125. if (crypt_len != 0) {
  126. printf("Warning: finalize() returned with unexpected length %d\n", crypt_len);
  127. }
  128. }
  129. uchar_to_slv(c_dout, dataout, bytelen);
  130. return;
  131. }