Changeset 3115 for trunk/src/libopensc/padding.c
- Timestamp:
- 02/02/07 22:15:14 (2 years ago)
- Files:
-
- 1 modified
-
trunk/src/libopensc/padding.c (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/libopensc/padding.c
r3084 r3115 1 1 /* 2 * sc-padding.c: miscellaneous padding functions2 * padding.c: miscellaneous padding functions 3 3 * 4 4 * Copyright (C) 2001, 2002 Juha YrjölÀ <juha.yrjola@iki.fi> 5 * Copyright (C) 2003 Nils Larsch <larsch@trustcenter.de>5 * Copyright (C) 2003 - 2007 Nils Larsch <larsch@trustcenter.de> 6 6 * 7 7 * This library is free software; you can redistribute it and/or … … 23 23 #include <string.h> 24 24 #include <stdlib.h> 25 #include <assert.h>26 25 27 26 /* TODO doxygen comments */ … … 38 37 0x05, 0x00, 0x04, 0x14 39 38 }; 39 static const u8 hdr_sha256[] = { 40 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 41 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 42 }; 43 static const u8 hdr_sha384[] = { 44 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 45 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 46 }; 47 static const u8 hdr_sha512[] = { 48 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 49 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 50 }; 51 static const u8 hdr_sha224[] = { 52 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 53 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c 54 }; 40 55 static const u8 hdr_ripemd160[] = { 41 56 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x24, 0x03, 0x02, 0x01, … … 44 59 45 60 46 #define DIGEST_INFO_COUNT 647 61 static const struct digest_info_prefix { 48 62 unsigned int algorithm; … … 50 64 size_t hdr_len; 51 65 size_t hash_len; 52 } digest_info_prefix[ DIGEST_INFO_COUNT] = {66 } digest_info_prefix[] = { 53 67 { SC_ALGORITHM_RSA_HASH_NONE, NULL, 0, 0 }, 54 68 { SC_ALGORITHM_RSA_HASH_MD5, hdr_md5, sizeof(hdr_md5), 16 }, 55 69 { SC_ALGORITHM_RSA_HASH_SHA1, hdr_sha1, sizeof(hdr_sha1), 20 }, 70 { SC_ALGORITHM_RSA_HASH_SHA256, hdr_sha256, sizeof(hdr_sha256), 32 }, 71 { SC_ALGORITHM_RSA_HASH_SHA384, hdr_sha384, sizeof(hdr_sha384), 48 }, 72 { SC_ALGORITHM_RSA_HASH_SHA512, hdr_sha512, sizeof(hdr_sha512), 64 }, 73 { SC_ALGORITHM_RSA_HASH_SHA224, hdr_sha224, sizeof(hdr_sha224), 28 }, 56 74 { SC_ALGORITHM_RSA_HASH_RIPEMD160,hdr_ripemd160, sizeof(hdr_ripemd160), 20 }, 57 75 { SC_ALGORITHM_RSA_HASH_MD5_SHA1, NULL, 0, 36 }, … … 61 79 /* add/remove pkcs1 BT01 padding */ 62 80 63 int sc_pkcs1_add_01_padding(const u8 *in, size_t in_len, u8 *out,64 size_t *out_len, size_t mod_length)81 static int sc_pkcs1_add_01_padding(const u8 *in, size_t in_len, 82 u8 *out, size_t *out_len, size_t mod_length) 65 83 { 66 84 size_t i; … … 83 101 } 84 102 85 int sc_pkcs1_strip_01_padding(const u8 *in_dat, size_t in_len, u8 *out,86 size_t *out_len)103 int sc_pkcs1_strip_01_padding(const u8 *in_dat, size_t in_len, 104 u8 *out, size_t *out_len) 87 105 { 88 106 const u8 *tmp = in_dat; … … 91 109 if (in_dat == NULL || in_len < 10) 92 110 return SC_ERROR_INTERNAL; 93 /* ignoreleading zero byte */111 /* skip leading zero byte */ 94 112 if (*tmp == 0) { 95 113 tmp++; … … 123 141 if (data == NULL || len < 3) 124 142 return SC_ERROR_INTERNAL; 125 /* skip leading zero octet (not part of the pkcs1 BT02 padding)*/143 /* skip leading zero byte */ 126 144 if (*data == 0) { 127 145 data++; … … 148 166 149 167 /* add/remove DigestInfo prefix */ 150 int sc_pkcs1_add_digest_info_prefix(unsigned int algorithm, const u8 *in,151 size_t in_len, u8 *out, size_t *out_len)168 static int sc_pkcs1_add_digest_info_prefix(unsigned int algorithm, 169 const u8 *in, size_t in_len, u8 *out, size_t *out_len) 152 170 { 153 171 int i; 154 172 155 for (i = 0; i < DIGEST_INFO_COUNT; i++) {173 for (i = 0; digest_info_prefix[i].algorithm != 0; i++) { 156 174 if (algorithm == digest_info_prefix[i].algorithm) { 157 175 const u8 *hdr = digest_info_prefix[i].hdr; … … 176 194 int i; 177 195 178 for (i = 0; i < DIGEST_INFO_COUNT; i++) {196 for (i = 0; digest_info_prefix[i].algorithm != 0; i++) { 179 197 size_t hdr_len = digest_info_prefix[i].hdr_len, 180 198 hash_len = digest_info_prefix[i].hash_len; … … 240 258 } 241 259 242 /* strip leading zero padding (does only really work when a DigestInfo 243 * value has been padded */ 244 int sc_strip_zero_padding(const u8 *in, size_t in_len, u8 *out, 245 size_t *out_len) 246 { 247 while (*in == 0 && in_len) { 248 in++; 249 in_len--; 250 } 251 252 if (*out_len < in_len) 253 return SC_ERROR_INTERNAL; 254 255 memmove(out, in, in_len); 256 *out_len = in_len; 260 int sc_get_encoding_flags(sc_context_t *ctx, 261 unsigned long iflags, unsigned long caps, 262 unsigned long *pflags, unsigned long *sflags) 263 { 264 size_t i; 265 266 if (pflags == NULL || sflags == NULL) 267 return SC_ERROR_INVALID_ARGUMENTS; 268 269 for (i = 0; digest_info_prefix[i].algorithm != 0; i++) { 270 if (iflags & digest_info_prefix[i].algorithm) { 271 if (digest_info_prefix[i].algorithm != SC_ALGORITHM_RSA_HASH_NONE && 272 caps & digest_info_prefix[i].algorithm) 273 *sflags |= digest_info_prefix[i].algorithm; 274 else 275 *pflags |= digest_info_prefix[i].algorithm; 276 break; 277 } 278 } 279 280 if (iflags & SC_ALGORITHM_RSA_PAD_PKCS1) { 281 if (caps & SC_ALGORITHM_RSA_PAD_PKCS1) 282 *sflags |= SC_ALGORITHM_RSA_PAD_PKCS1; 283 else 284 *pflags |= SC_ALGORITHM_RSA_PAD_PKCS1; 285 } else if ((iflags & SC_ALGORITHM_RSA_PADS) == SC_ALGORITHM_RSA_PAD_NONE) { 286 if (!(caps & SC_ALGORITHM_RSA_RAW)) { 287 sc_error(ctx, "raw RSA is not supported"); 288 return SC_ERROR_NOT_SUPPORTED; 289 } 290 *sflags |= SC_ALGORITHM_RSA_RAW; 291 /* in case of raw RSA there is nothing to pad */ 292 *pflags = 0; 293 } else { 294 sc_error(ctx, "unsupported algorithm"); 295 return SC_ERROR_NOT_SUPPORTED; 296 } 257 297 258 298 return SC_SUCCESS;
