commit 1b2b4397beceb8fab31dcaaae7f5340d77e95034
parent 2bc60b7d2f6c303799d76aedb948bb547f491c3e
Author: Jack Sangdahl <jack@pngu.org>
Date: Tue, 14 Oct 2025 09:24:55 -0600
challenge 15: pkcs#7 padding validation
Diffstat:
1 file changed, 34 insertions(+), 0 deletions(-)
diff --git a/src/15_pkcs7_padding_validation.c b/src/15_pkcs7_padding_validation.c
@@ -0,0 +1,34 @@
+// PKCS#7 padding validation
+#include "aes.h"
+
+/*
+ * write a function that takes a plaintext, determines if it has valid pkcs#7
+ * padding, and strips the padding off. the sring "ICE ICE BABY\x04\x04\x04\x04"
+ * has valid padding, and produces the result "ICE ICE BABY". the string "ICE
+ * ICE BABY\x05\x05\x05\x05" does not have valid padding, nor does "ICE ICE
+ * BABY\x01\x02\x03\x04". if you are writing a language with exceptions, like
+ * python, make your function throw an exception on bad padding.
+ */
+
+int main(void) {
+ static char *valid = "ICE ICE BABY\x04\x04\x04\x04";
+ static char *invalid1 = "ICE ICE BABY\x05\x05\x05\x05";
+ static char *invalid2 = "ICE ICE BABY\x01\x02\x03\x04";
+ struct bytes stripped = pkcs7_validate_strip((unsigned char *)valid, 16);
+ if (stripped.data)
+ printf("%s\n", stripped.data);
+ else
+ printf("Invalid padding\n");
+ stripped = pkcs7_validate_strip((unsigned char *)invalid1, 16);
+ if (stripped.data)
+ printf("%s\n", stripped.data);
+ else
+ printf("Invalid padding\n");
+ stripped = pkcs7_validate_strip((unsigned char *)invalid2, 16);
+ if (stripped.data)
+ printf("%s\n", stripped.data);
+ else
+ printf("Invalid padding\n");
+ free(stripped.data);
+ return 0;
+}