Libtc: Src/algorithms_pkcs1_encoding.c Source File - NIC Labs

libtc  20160415 Threshold Cryptography functions library
  • Main Page
  • Classes
  • Files
  • File List
  • File Members
  • src
algorithms_pkcs1_encoding.c Go to the documentation of this file. 1 #include <mhash.h> 2 #include "tc_internal.h" 3  4 const uint8_t MD2_PKCS_ID[] = { 5  0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 6  0xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 }; 7  8 const uint8_t MD5_PKCS_ID[] = { 9  0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 10  0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 }; 11  12 const uint8_t RIPEMD_128_PKCS_ID[] = { 13  0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02, 14  0x02, 0x05, 0x00, 0x04, 0x14 }; 15  16 const uint8_t RIPEMD_160_PKCS_ID[] = { 17  0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02, 18  0x01, 0x05, 0x00, 0x04, 0x14 }; 19  20 const uint8_t SHA_160_PKCS_ID[] = { 21  0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 22  0x1A, 0x05, 0x00, 0x04, 0x14 }; 23  24 const uint8_t SHA_224_PKCS_ID[] = { 25  0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 26  0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C }; 27  28 const uint8_t SHA_256_PKCS_ID[] = { 29  0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 30  0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 }; 31  32 const uint8_t SHA_384_PKCS_ID[] = { 33  0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 34  0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 }; 35  36 const uint8_t SHA_512_PKCS_ID[] = { 37  0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 38  0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 }; 39  40 const uint8_t TIGER_PKCS_ID[] = { 41  0x30, 0x29, 0x30, 0x0D, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04, 42  0x01, 0xDA, 0x47, 0x0C, 0x02, 0x05, 0x00, 0x04, 0x18 }; 43  44 static inline void get_hash_properties(const uint8_t ** id, int * id_len, int * digest_len, tc_hash_type_t hash_type) { 45  switch(hash_type) { 46  case TC_SHA256: 47  *id_len = sizeof(SHA_256_PKCS_ID); 48  *digest_len = 256/8; 49  *id = SHA_256_PKCS_ID; 50  break; 51  case TC_NONE: 52  *id_len = 0; 53  *digest_len = 0; 54  *id = NULL; 55  break; 56  default: 57  abort(); 58  } 59 } 60  61 static void tc_pkcs1_encoding(bytes_t * out, bytes_t * digest, tc_hash_type_t hash_type) { 62  int k = out->data_len; 63  const uint8_t *hash_desc; 64  int hash_desc_len, digest_len; 65  66  get_hash_properties(&hash_desc, &hash_desc_len, &digest_len, hash_type); 67  68  assert(digest->data_len <= digest_len); 69  70  int D_len = digest_len + hash_desc_len; 71  int P_len = k - 3 - D_len; 72  73  uint8_t *p = out->data; 74  75  /* PKCS1 padding */ 76  *p++ = 0x00; 77  *p++ = 0x01; 78  memset(p, 0xFF, P_len); 79  p += P_len; 80  *p++ = 0x00; 81  82  if (hash_desc != NULL) { // ASN.1 83  memcpy(p, hash_desc, hash_desc_len); 84  } 85  86  p += hash_desc_len; 87  88  memcpy(p, digest->data, digest->data_len); 89 } 90  91 bytes_t * tc_prepare_document(const bytes_t * doc, tc_hash_type_t hash_type, const key_metainfo_t * metainfo) { 92  size_t data_len = metainfo->public_key->n->data_len; 93  94  bytes_t * out = tc_init_bytes(malloc(data_len), data_len); 95  bytes_t digest; 96  switch(hash_type) { 97  case TC_SHA256: 98  { 99  uint8_t hash[32]; 100  MHASH sha = mhash_init(MHASH_SHA256); 101  mhash(sha, doc->data, doc->data_len); 102  mhash_deinit(sha, hash); 103  104  digest.data = hash; 105  digest.data_len = 32; 106  } 107  break; 108  case TC_NONE: 109  { 110  digest.data = doc->data; 111  digest.data_len = doc->data_len; 112  } 113  break; 114  default: 115  abort(); 116  }; 117  118  assert(digest.data_len <= data_len); 119  tc_pkcs1_encoding(out, &digest, hash_type); 120  return out; 121 }tc_prepare_documentbytes_t * tc_prepare_document(const bytes_t *doc, tc_hash_type_t hash_type, const key_metainfo_t *metainfo)Definition: algorithms_pkcs1_encoding.c:91 key_metainfo::public_keypublic_key_t * public_keyDefinition: tc_internal.h:14 RIPEMD_128_PKCS_IDconst uint8_t RIPEMD_128_PKCS_ID[]Definition: algorithms_pkcs1_encoding.c:12 bytesStructure that&#39;s stores a pointer that points to data_len bytes. Definition: tc.h:14 public_key::nbytes_t * nDefinition: tc_internal.h:9 key_metainfoStructure that represents the data about a key share, including its public key. Definition: tc_internal.h:13 TC_SHA256Definition: tc.h:48 tc_internal.h MD2_PKCS_IDconst uint8_t MD2_PKCS_ID[]Definition: algorithms_pkcs1_encoding.c:4 TC_NONEDefinition: tc.h:49 RIPEMD_160_PKCS_IDconst uint8_t RIPEMD_160_PKCS_ID[]Definition: algorithms_pkcs1_encoding.c:16 TIGER_PKCS_IDconst uint8_t TIGER_PKCS_ID[]Definition: algorithms_pkcs1_encoding.c:40 SHA_160_PKCS_IDconst uint8_t SHA_160_PKCS_ID[]Definition: algorithms_pkcs1_encoding.c:20 SHA_512_PKCS_IDconst uint8_t SHA_512_PKCS_ID[]Definition: algorithms_pkcs1_encoding.c:36 MD5_PKCS_IDconst uint8_t MD5_PKCS_ID[]Definition: algorithms_pkcs1_encoding.c:8 bytes::datavoid * dataDefinition: tc.h:15 bytes::data_lenuint32_t data_lenDefinition: tc.h:16 tc_hash_type_tenum tc_hash_type tc_hash_type_tDefinition: tc.h:51 SHA_224_PKCS_IDconst uint8_t SHA_224_PKCS_ID[]Definition: algorithms_pkcs1_encoding.c:24 tc_init_bytesbytes_t * tc_init_bytes(void *bs, size_t len)Definition: structs_init.c:18 SHA_256_PKCS_IDconst uint8_t SHA_256_PKCS_ID[]Definition: algorithms_pkcs1_encoding.c:28 SHA_384_PKCS_IDconst uint8_t SHA_384_PKCS_ID[]Definition: algorithms_pkcs1_encoding.c:32 Generated by   doxygen 1.8.11

Từ khóa » C 0x02