static crypto_error_t crypto_pad(bytestring_t *dst, const bytestring_t *ctx,const bytestring_t *src) { unsigned char e; bytestring_copy(dst,src); bytestring_get_element(&e,ctx,1); switch (((unsigned)e)<<8) { case CRYPTO_PAD_OPT_80_ZERO: if ((bytestring_get_size(dst)&0x7)==0) break; case CRYPTO_PAD_ISO9797_P2: bytestring_pushback(dst,0x80); bytestring_pad_right(dst,8,0); break; case CRYPTO_PAD_ZERO: bytestring_pad_right(dst,8,0); break; default: bytestring_clear(dst); return CRYPTO_ERROR_UNKNOWN_PADDING_METHOD; } return CRYPTO_OK; }
crypto_error_t crypto_create_context(bytestring_t *ctx, crypto_alg_t alg_type, const bytestring_t *key_bin) { DES_key_schedule ks; bytestring_clear(ctx); bytestring_pushback(ctx,ALG_TYPE(alg_type)); bytestring_pushback(ctx,PAD_TYPE(alg_type)>>8); switch (ALG_TYPE(alg_type)) { case CRYPTO_ALG_DES_ECB: case CRYPTO_ALG_DES_CBC: if (key_bin==NULL || bytestring_get_size(key_bin)!=8 || key_bin->width!=8) return CRYPTO_ERROR_BAD_KEY_FORMAT; DES_set_key_unchecked((const_DES_cblock *)key_bin->data,&ks); bytestring_append_data(ctx,DES_KS_SIZE,(unsigned char *)&ks); break; case CRYPTO_ALG_DES2_EDE_ECB: case CRYPTO_ALG_DES2_EDE_CBC: case CRYPTO_ALG_ISO9797_M3: if (key_bin==NULL || bytestring_get_size(key_bin)!=16 || key_bin->width!=8) return CRYPTO_ERROR_BAD_KEY_FORMAT; DES_set_key_unchecked((const_DES_cblock *)key_bin->data,&ks); bytestring_append_data(ctx,DES_KS_SIZE,(unsigned char *)&ks); DES_set_key_unchecked((const_DES_cblock *)(key_bin->data+8),&ks); bytestring_append_data(ctx,DES_KS_SIZE,(unsigned char *)&ks); break; case CRYPTO_ALG_SHA1: /* nothing else to do */ break; default: return CRYPTO_ERROR_UNKNOWN_KEY_TYPE; } return CRYPTO_OK; }
bytestring_t* bytestring_new_from_hex(const char *hex) { bytestring_t *dat=bytestring_new(); unsigned hex_len=strlen(hex); unsigned c; size_t i=0; bytestring_reserve(dat,hex_len/2); while (i<hex_len) { if (is_blank(hex[i])) { i++; continue; } c=(hex_nibble(hex[i])<<4) | hex_nibble(hex[i+1]); if (c>255) return dat; bytestring_pushback(dat,(unsigned char)c); i+=2; } return dat; }