bcal-basic.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* bcal-basic.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-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 <stdlib.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <crypto/blockcipher_descriptor.h>
  20. #include <crypto/keysize_descriptor.h>
  21. uint8_t bcal_cipher_init(const bcdesc_t* cipher_descriptor,
  22. const void* key, uint16_t keysize_b, bcgen_ctx_t* ctx){
  23. if(!is_valid_keysize_P(cipher_descriptor->valid_keysize_desc, keysize_b)){
  24. return 1;
  25. }
  26. uint8_t flags;
  27. bc_init_fpt init_fpt;
  28. ctx->desc_ptr = (bcdesc_t*)cipher_descriptor;
  29. ctx->keysize = keysize_b;
  30. flags = cipher_descriptor->flags;
  31. init_fpt.initvoid = (void_fpt)(cipher_descriptor->init.initvoid);
  32. if(init_fpt.initvoid == NULL){
  33. if(!(ctx->ctx = malloc((keysize_b+7)/8)))
  34. return 2;
  35. memcpy(ctx->ctx, key, (keysize_b+7)/8);
  36. return 0;
  37. }
  38. if(!(ctx->ctx = malloc(cipher_descriptor->ctxsize_B)))
  39. return 3;
  40. if((flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
  41. init_fpt.init1((void*)key, (ctx->ctx));
  42. }else{
  43. init_fpt.init2((void*)key, keysize_b, (ctx->ctx));
  44. }
  45. return 0;
  46. }
  47. void bcal_cipher_free(bcgen_ctx_t* ctx){
  48. if(!ctx)
  49. return;
  50. bc_free_fpt free_fpt;
  51. free_fpt = (bc_free_fpt)(ctx->desc_ptr->free);
  52. if(free_fpt)
  53. free_fpt((ctx->ctx));
  54. free(ctx->ctx);
  55. }
  56. void bcal_cipher_enc(void* block, const bcgen_ctx_t* ctx){
  57. bc_enc_fpt enc_fpt;
  58. enc_fpt.encvoid = (void_fpt)(ctx->desc_ptr->enc.encvoid);
  59. if(!enc_fpt.encvoid){
  60. /* very bad error, no enciphering function specified */
  61. return;
  62. }
  63. enc_fpt.enc1(block, (ctx->ctx));
  64. }
  65. void bcal_cipher_dec(void* block, const bcgen_ctx_t* ctx){
  66. bc_dec_fpt dec_fpt;
  67. dec_fpt.decvoid = (void_fpt)(ctx->desc_ptr->dec.decvoid);
  68. if(!dec_fpt.decvoid){
  69. /* very bad error, no deciphering function specified */
  70. return;
  71. }
  72. dec_fpt.dec1(block, (ctx->ctx));
  73. }
  74. uint16_t bcal_cipher_getBlocksize_b(const bcdesc_t* desc){
  75. return (desc->blocksize_b);
  76. }
  77. const void* bcal_cipher_getKeysizeDesc(const bcdesc_t* desc){
  78. return (desc->valid_keysize_desc);
  79. }