validate.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * ZMap Copyright 2013 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. */
  8. #include <stdint.h>
  9. #include <assert.h>
  10. #include "../lib/rijndael-alg-fst.h"
  11. #include "../lib/random.h"
  12. #include "../lib/logger.h"
  13. #include "validate.h"
  14. #define AES_ROUNDS 10
  15. #define AES_BLOCK_WORDS 4
  16. #define AES_KEY_BYTES 16
  17. static int inited = 0;
  18. static uint32_t aes_input[AES_BLOCK_WORDS];
  19. static uint32_t aes_sched[(AES_ROUNDS+1)*4];
  20. void validate_init()
  21. {
  22. for (int i=0; i < AES_BLOCK_WORDS; i++) {
  23. aes_input[i] = 0;
  24. }
  25. uint8_t key[AES_KEY_BYTES];
  26. if (!random_bytes(key, AES_KEY_BYTES)) {
  27. log_fatal("validate", "couldn't get random bytes");
  28. }
  29. if (rijndaelKeySetupEnc(aes_sched, key, AES_KEY_BYTES*8) != AES_ROUNDS) {
  30. log_fatal("validate", "couldn't initialize AES key");
  31. }
  32. inited = 1;
  33. }
  34. void validate_gen(const uint32_t src, const uint32_t dst,
  35. uint8_t output[VALIDATE_BYTES])
  36. {
  37. assert(inited);
  38. aes_input[0] = src;
  39. aes_input[1] = dst;
  40. rijndaelEncrypt(aes_sched, AES_ROUNDS, (uint8_t *)aes_input, output);
  41. }