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.

164 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. 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. void init(unsigned char *key, unsigned char *iv) {
  60. // Create and initialise the context
  61. if(!(ctx = EVP_CIPHER_CTX_new()))
  62. handleErrors();
  63. // Initialise the encryption operation, no IV needed in CTR mode
  64. if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
  65. handleErrors();
  66. // We don't want padding
  67. if(1 != EVP_CIPHER_CTX_set_padding(ctx, 0))
  68. handleErrors();
  69. }
  70. int encrypt(unsigned char *plaintext,
  71. int plaintext_len,
  72. unsigned char *ciphertext) {
  73. int len = 0;
  74. // Provide the message to be encrypted, and obtain the encrypted output
  75. if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
  76. handleErrors();
  77. return len;
  78. }
  79. int finalize(void) {
  80. int len = 0;
  81. unsigned char data[16];
  82. // Finalise the encryption. No further bytes are written as padding is switched off
  83. if (1 != EVP_EncryptFinal_ex(ctx, data, &len))
  84. handleErrors();
  85. // Clean up
  86. EVP_CIPHER_CTX_free(ctx);
  87. return len;
  88. }
  89. void cryptData(char* datain, char* key, char* iv, char start, char final, char* dataout, int bytelen) {
  90. int crypt_len;
  91. unsigned char c_din[bytelen];
  92. unsigned char c_key[bytelen];
  93. unsigned char c_iv[bytelen];
  94. unsigned char c_dout[bytelen];
  95. slv_to_uchar(datain, c_din, bytelen);
  96. slv_to_uchar(key, c_key, bytelen);
  97. slv_to_uchar(iv, c_iv, bytelen);
  98. if (start) {
  99. init(c_key, c_iv);
  100. }
  101. crypt_len = encrypt(c_din, bytelen, c_dout);
  102. if (crypt_len != bytelen) {
  103. printf("Warning: encrypt() returned with unexpected length %d\n", crypt_len);
  104. }
  105. if (final) {
  106. crypt_len = finalize();
  107. if (crypt_len != 0) {
  108. printf("Warning: finalize() returned with unexpected length %d\n", crypt_len);
  109. }
  110. }
  111. uchar_to_slv(c_dout, dataout, bytelen);
  112. return;
  113. }