protected override ValidationResult IsValid(object value, ValidationContext context) { // The initial counter can be any value within the limits given by the version. // DJB version: 64-bit // IETF version: 32-bit ChaCha chacha = context.ObjectInstance as ChaCha; Version currentVersion = ((ChaChaSettings)chacha.Settings).Version; ulong maxCounter = currentVersion.CounterBits == 32 ? uint.MaxValue : ulong.MaxValue; BigInteger initialCounter = (BigInteger)value; return(initialCounter <= maxCounter ? ValidationResult.Success : new ValidationResult(ErrorMessage)); }
protected override ValidationResult IsValid(object value, ValidationContext context) { // The initialization vector must be of the size specified by the version. // DJB version: 64-bit // IETF version: 96-bit // For the same reasons as with the key, we will do no padding, // so the IV must be already in the correct size. ChaCha chacha = context.ObjectInstance as ChaCha; Version currentVersion = ((ChaChaSettings)chacha.Settings).Version; byte[] inputIV = value as byte[]; string hexIV = string.Join("", inputIV.Select(b => b.ToString("X2"))); int maxBits = (int)currentVersion.IVBits; int maxBytes = maxBits / 8; var required = new StringLengthAttribute(maxBytes * 2) { MinimumLength = maxBytes * 2 }; return(required.IsValid(hexIV) ? ValidationResult.Success : new ValidationResult(ErrorMessage)); }