scal-basic.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* scal-basic.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2011 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 <stdlib.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <crypto/streamcipher_descriptor.h>
  20. #include <crypto/keysize_descriptor.h>
  21. #include <crypto/scal-basic.h>
  22. uint8_t scal_cipher_init(const scdesc_t* cipher_descriptor,
  23. const void* key, uint16_t keysize_b,
  24. const void* iv, uint16_t ivsize_b, scgen_ctx_t* ctx){
  25. ctx->buffer = NULL;
  26. ctx->ctx = NULL;
  27. if(!is_valid_keysize_P(cipher_descriptor->valid_keysize_desc, keysize_b)){
  28. return 1;
  29. }
  30. if(!is_valid_keysize_P(cipher_descriptor->valid_ivsize_desc, ivsize_b)){
  31. return 2;
  32. }
  33. uint8_t flags;
  34. sc_init_fpt init_fpt;
  35. flags = cipher_descriptor->flags;
  36. ctx->desc_ptr = cipher_descriptor;
  37. ctx->keysize = keysize_b;
  38. ctx->ivsize = ivsize_b;
  39. ctx->ctx = malloc(cipher_descriptor->ctxsize_B);
  40. if(ctx->ctx==NULL){
  41. return 3;
  42. }
  43. init_fpt = (cipher_descriptor->init);
  44. switch(flags&SC_INIT_TYPE){
  45. case SC_INIT_TYPE_1:
  46. init_fpt.init1(key, ctx->ctx);
  47. break;
  48. case SC_INIT_TYPE_2:
  49. init_fpt.init2(key, iv, ctx->ctx);
  50. break;
  51. case SC_INIT_TYPE_3:
  52. init_fpt.init3(key, keysize_b, ctx->ctx);
  53. break;
  54. case SC_INIT_TYPE_4:
  55. init_fpt.init4(key, keysize_b, iv, ctx->ctx);
  56. break;
  57. case SC_INIT_TYPE_5:
  58. init_fpt.init5(key, keysize_b, iv, ivsize_b, ctx->ctx);
  59. break;
  60. default:
  61. return 4;
  62. }
  63. uint16_t blocksize_b;
  64. blocksize_b = cipher_descriptor->gensize_b;
  65. if(blocksize_b>8){
  66. ctx->buffer=malloc((blocksize_b+7)/8);
  67. if(ctx->buffer==NULL){
  68. return 5;
  69. }
  70. ctx->index=0;
  71. }
  72. return 0;
  73. }
  74. void scal_cipher_free(scgen_ctx_t* ctx){
  75. if(ctx->buffer){
  76. free(ctx->buffer);
  77. }
  78. if(ctx->ctx){
  79. free(ctx->ctx);
  80. }
  81. }
  82. uint8_t scal_cipher_gen_byte(scgen_ctx_t* ctx){
  83. uint8_t flags;
  84. uint16_t blocksize_b;
  85. void_fpt gen_fpt;
  86. flags = ctx->desc_ptr->flags;
  87. blocksize_b = ctx->desc_ptr->gensize_b;
  88. gen_fpt = (void_fpt)(ctx->desc_ptr->gen.genvoid);
  89. if(blocksize_b==8){
  90. if((flags&SC_GEN_TYPE)==SC_GEN_TYPE_1){
  91. return ((sc_gen1_fpt)gen_fpt)(ctx->ctx);
  92. }else{
  93. uint8_t r;
  94. ((sc_gen2_fpt)gen_fpt)(&r, ctx->ctx);
  95. return r;
  96. }
  97. }
  98. if(blocksize_b<8){
  99. uint8_t r=0;
  100. uint8_t fill=0;
  101. do{
  102. r |= ((((sc_gen1_fpt)gen_fpt)(ctx->ctx))&(0xff<<(8-blocksize_b)))>>fill;
  103. fill += blocksize_b;
  104. }while(fill<8);
  105. return r;
  106. }else{
  107. uint8_t r;
  108. if(ctx->index==0){
  109. ((sc_gen2_fpt)gen_fpt)(ctx->buffer, ctx->ctx);
  110. ctx->index = blocksize_b;
  111. }
  112. r=ctx->buffer[(blocksize_b-ctx->index)/8];
  113. ctx->index -= 8;
  114. return r;
  115. }
  116. }
  117. void scal_cipher_gen_block(void* block, scgen_ctx_t* ctx){
  118. uint8_t flags;
  119. /* uint16_t blocksize_b; */
  120. sc_gen_fpt gen_fpt;
  121. flags = ctx->desc_ptr->flags;
  122. /* blocksize_b = ctx->desc_ptr->gensize_b; */
  123. gen_fpt = ctx->desc_ptr->gen;
  124. if((flags&SC_GEN_TYPE)==SC_GEN_TYPE_1){
  125. *((uint8_t*)block) = gen_fpt.gen1(ctx->ctx);
  126. }else{
  127. gen_fpt.gen2(block, ctx->ctx);
  128. }
  129. }
  130. void scal_cipher_gen_fillblock(void* block, uint16_t blocksize_B, scgen_ctx_t* ctx){
  131. while(blocksize_B){
  132. *((uint8_t*)block) = scal_cipher_gen_byte(ctx);
  133. block = (uint8_t*)block + 1;
  134. blocksize_B -= 1;
  135. }
  136. }
  137. uint16_t scal_cipher_getBlocksize_b(const scdesc_t* desc){
  138. uint16_t blocksize_b;
  139. blocksize_b = desc->gensize_b;
  140. return blocksize_b;
  141. }
  142. void* scal_cipher_getKeysizeDesc(const scdesc_t* desc){
  143. return (void*)(desc->valid_keysize_desc);
  144. }
  145. void* scal_cipher_getIVsizeDesc(const scdesc_t* desc){
  146. return (void*)(desc->valid_ivsize_desc);
  147. }