Home | History | Annotate | Download | only in src
      1 /*############################################################################
      2 # Copyright 2017 Intel Corporation
      3 #
      4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #     http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 ############################################################################*/
     16 /// Implementation of hash wrap function
     17 /*! \file */
     18 #include "epid/member/tiny/math/hashwrap.h"
     19 
     20 void tinysha_init(HashAlg sha_type, tiny_sha* s) {
     21   switch (sha_type) {
     22 #ifdef SHA512_SUPPORT
     23     case kSha512:
     24       tinysha512_init(&s->sha_state_t.sha512s);
     25       break;
     26 #endif
     27 #ifdef SHA256_SUPPORT
     28     case kSha256:
     29       tc_sha256_init(&s->sha_state_t.sha256s);
     30       break;
     31 #endif
     32     default:
     33       s->hash_alg = kInvalidHashAlg;
     34       return;
     35   }
     36   s->hash_alg = sha_type;
     37 }
     38 
     39 void tinysha_update(tiny_sha* s, void const* data, size_t data_length) {
     40   switch (s->hash_alg) {
     41 #ifdef SHA512_SUPPORT
     42     case kSha512:
     43       tinysha512_update(&s->sha_state_t.sha512s, data, data_length);
     44       break;
     45 #endif
     46 #ifdef SHA256_SUPPORT
     47     case kSha256:
     48       tc_sha256_update(&s->sha_state_t.sha256s, data, data_length);
     49       break;
     50 #endif
     51     default:
     52       break;
     53   }
     54 }
     55 
     56 void tinysha_final(unsigned char* digest, tiny_sha* s) {
     57   switch (s->hash_alg) {
     58 #ifdef SHA512_SUPPORT
     59     case kSha512:
     60       tinysha512_final(digest, &s->sha_state_t.sha512s);
     61       break;
     62 #endif
     63 #ifdef SHA256_SUPPORT
     64     case kSha256:
     65       tc_sha256_final(digest, &s->sha_state_t.sha256s);
     66       break;
     67 #endif
     68     default:
     69       break;
     70   }
     71 }
     72 
     73 size_t tinysha_digest_size(tiny_sha* s) {
     74   switch (s->hash_alg) {
     75 #ifdef SHA512_SUPPORT
     76     case kSha512:
     77       return SHA512_DIGEST_SIZE;
     78 #endif
     79 #ifdef SHA256_SUPPORT
     80     case kSha256:
     81       return SHA256_DIGEST_SIZE;
     82 #endif
     83     default:
     84       return 0;
     85   }
     86 }
     87