A VexRiscv with AES128 on the memory mapped bus. Works in simulation and on the ULX3S.
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.

97 lines
1.9 KiB

4 years ago
  1. #include <stdbool.h>
  2. #include "hal.h"
  3. #define LENGTH 4
  4. typedef struct {
  5. volatile uint32_t key[LENGTH];
  6. volatile uint32_t state_in[LENGTH];
  7. volatile uint32_t useEncDec;
  8. volatile uint32_t valid_in;
  9. volatile uint32_t valid_out;
  10. volatile uint32_t state_out[LENGTH];
  11. } Aes_Mem;
  12. #define AES_MEM ((Aes_Mem*)(0xF0030000))
  13. void writeKey(uint32_t * key) {
  14. for (unsigned i = 0; i < LENGTH; i++) {
  15. AES_MEM->key[i] = key[i];
  16. }
  17. }
  18. void writeState(uint32_t * state) {
  19. for (unsigned i = 0; i < LENGTH; i++) {
  20. AES_MEM->state_in[i] = state[i];
  21. }
  22. }
  23. void setEncDec(int encDec) {
  24. AES_MEM->useEncDec = encDec;
  25. }
  26. void setValidIn() {
  27. AES_MEM->valid_in = 1;
  28. }
  29. void readState(uint32_t * state) {
  30. for (unsigned i = 0; i < LENGTH; i++) {
  31. state[i] = AES_MEM->state_out[i];
  32. }
  33. }
  34. void printState(uint32_t * state) {
  35. printf("State:\n");
  36. for(unsigned i = 0; i < LENGTH; i++) {
  37. printf("%08x",state[LENGTH-i-1]);
  38. }
  39. printf("\n");
  40. }
  41. void printKey(uint32_t * key) {
  42. printf("Key:\n");
  43. for(unsigned i = 0; i < LENGTH; i++) {
  44. printf("%08x",key[LENGTH-i-1]);
  45. }
  46. printf("\n");
  47. }
  48. int main(void) {
  49. printf("HELLO WORLD\n");
  50. // uint32_t state[LENGTH] = {0x6bc1bee2, 0x2e409f96, 0xe93d7e11, 0x7393172a};
  51. // uint32_t key[LENGTH] = {0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c};
  52. uint32_t state[LENGTH] = {0x7393172a, 0xe93d7e11, 0x2e409f96, 0x6bc1bee2};
  53. uint32_t key[LENGTH] = {0x09cf4f3c, 0xabf71588, 0x28aed2a6, 0x2b7e1516};
  54. writeKey(key);
  55. while (1) {
  56. printState(state);
  57. printKey(key);
  58. writeState(state);
  59. setEncDec(1);
  60. setValidIn();
  61. while (AES_MEM->valid_out != 1);
  62. readState(state);
  63. printState(state);
  64. writeState(state);
  65. setEncDec(0);
  66. setValidIn();
  67. while (AES_MEM->valid_out != 1);
  68. readState(state);
  69. printState(state);
  70. }
  71. return 0;
  72. }