mgf1.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* mgf1.c */
  2. /*
  3. This file is part of the AVR-Crypto-Lib.
  4. Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <crypto/mgf1.h>
  19. #include <crypto/hfal-basic.h>
  20. /*
  21. * MGF1 as defined in PKCS #1 v2.1 B.2.1
  22. */
  23. void mgf1_short_seed(void* dest, const void* seed, uint8_t seed_len_B, uint16_t out_length_B, const mgf1_parameter_t* p){
  24. uint8_t buffer[seed_len_B+4];
  25. uint32_t counter=1;
  26. uint8_t hv_len = hfal_hash_getHashsize(p->hashfunction)/8;
  27. memcpy(buffer, seed, seed_len_B);
  28. memset(buffer + seed_len_B, 0, 4);
  29. while(out_length_B >= hv_len){
  30. hfal_hash_mem(p->hashfunction, dest, buffer, (seed_len_B + 4) * 8);
  31. dest = (uint8_t*)dest + hv_len;
  32. out_length_B -= hv_len;
  33. buffer[seed_len_B + 3] = counter & 0xff;
  34. buffer[seed_len_B + 2] = (counter>>8) & 0xff;
  35. buffer[seed_len_B + 1] = (counter>>16) & 0xff;
  36. buffer[seed_len_B + 0] = (counter>>24) & 0xff;
  37. ++counter;
  38. }
  39. if(out_length_B){
  40. uint8_t hash_buffer[hv_len];
  41. hfal_hash_mem(p->hashfunction, hash_buffer, buffer, (seed_len_B + 4) * 8);
  42. memcpy(dest, hash_buffer, out_length_B);
  43. }
  44. }
  45. void mgf1(void* dest, const void* seed, uint16_t seed_len_B, uint16_t out_length_B, const mgf1_parameter_t* p){
  46. mgf1_short_seed(dest, seed, seed_len_B, out_length_B, p);
  47. }