static mrb_value lib_md_digest_bang(mrb_state *mrb, struct mrb_md *md) { unsigned char mdstr[MAX_DIGEST_LENGTH]; md_final(md->type, mdstr, md->ctx); md_init(md->type, md->ctx); return mrb_str_new(mrb, (char *)mdstr, md_digest_length(md->type)); }
static mrb_value lib_hmac_digest(mrb_state *mrb, const struct mrb_hmac *hmac) { CCHmacContext ctx; unsigned char str[MAX_DIGEST_LENGTH]; memcpy(&ctx, &hmac->ctx, sizeof(ctx)); CCHmacFinal(&ctx, str); return mrb_str_new(mrb, (const char *)str, md_digest_length(hmac->type)); }
static mrb_value lib_md_digest(mrb_state *mrb, const struct mrb_md *md) { union ctx_union ctx; unsigned char mdstr[MAX_DIGEST_LENGTH]; memcpy(&ctx, md->ctx, md_ctx_size(md->type)); md_final(md->type, mdstr, &ctx); return mrb_str_new(mrb, (char *)mdstr, md_digest_length(md->type)); }
/**************** * This filter is used to en/de-cipher data with a conventional algorithm */ int cipher_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len) { size_t size = *ret_len; cipher_filter_context_t *cfx = opaque; int rc=0; if( control == IOBUFCTRL_UNDERFLOW ) { /* decrypt */ rc = -1; /* not yet used */ } else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */ assert(a); if( !cfx->header ) { write_header( cfx, a ); } if( cfx->mdc_hash ) md_write( cfx->mdc_hash, buf, size ); cipher_encrypt( cfx->cipher_hd, buf, buf, size); if( iobuf_write( a, buf, size ) ) rc = G10ERR_WRITE_FILE; } else if( control == IOBUFCTRL_FREE ) { if( cfx->mdc_hash ) { byte *hash; int hashlen = md_digest_length( md_get_algo( cfx->mdc_hash ) ); byte temp[22]; assert( hashlen == 20 ); /* we must hash the prefix of the MDC packet here */ temp[0] = 0xd3; temp[1] = 0x14; md_putc( cfx->mdc_hash, temp[0] ); md_putc( cfx->mdc_hash, temp[1] ); md_final( cfx->mdc_hash ); hash = md_read( cfx->mdc_hash, 0 ); memcpy(temp+2, hash, 20); cipher_encrypt( cfx->cipher_hd, temp, temp, 22 ); md_close( cfx->mdc_hash ); cfx->mdc_hash = NULL; if( iobuf_write( a, temp, 22 ) ) log_error("writing MDC packet failed\n" ); } cipher_close(cfx->cipher_hd); } else if( control == IOBUFCTRL_DESC ) { *(char**)buf = "cipher_filter"; } return rc; }
int algo_available( preftype_t preftype, int algo, const union pref_hint *hint ) { if( preftype == PREFTYPE_SYM ) { if(PGP6 && (algo != CIPHER_ALGO_IDEA && algo != CIPHER_ALGO_3DES && algo != CIPHER_ALGO_CAST5)) return 0; if(PGP7 && (algo != CIPHER_ALGO_IDEA && algo != CIPHER_ALGO_3DES && algo != CIPHER_ALGO_CAST5 && algo != CIPHER_ALGO_AES && algo != CIPHER_ALGO_AES192 && algo != CIPHER_ALGO_AES256 && algo != CIPHER_ALGO_TWOFISH)) return 0; /* PGP8 supports all the ciphers we do.. */ return algo && !check_cipher_algo( algo ); } else if( preftype == PREFTYPE_HASH ) { if(hint && hint->digest_length) { if(hint->digest_length!=20 || opt.flags.dsa2) { /* If --enable-dsa2 is set or the hash isn't 160 bits (which implies DSA2), then we'll accept a hash that is larger than we need. Otherwise we won't accept any hash that isn't exactly the right size. */ if(hint->digest_length > md_digest_length(algo)) return 0; } else if(hint->digest_length != md_digest_length(algo)) return 0; } if((PGP6 || PGP7) && (algo != DIGEST_ALGO_MD5 && algo != DIGEST_ALGO_SHA1 && algo != DIGEST_ALGO_RMD160)) return 0; if(PGP8 && (algo != DIGEST_ALGO_MD5 && algo != DIGEST_ALGO_SHA1 && algo != DIGEST_ALGO_RMD160 && algo != DIGEST_ALGO_SHA256)) return 0; return algo && !check_digest_algo( algo ); } else if( preftype == PREFTYPE_ZIP ) { if((PGP6 || PGP7) && (algo != COMPRESS_ALGO_NONE && algo != COMPRESS_ALGO_ZIP)) return 0; /* PGP8 supports all the compression algos we do */ return !check_compress_algo( algo ); } else return 0; }
static int lib_hmac_digest_length(const struct mrb_hmac *hmac) { return md_digest_length(hmac->type); }
static int lib_md_digest_length(const struct mrb_md *md) { return md_digest_length(md->type); }