upstream: https://github.com/mirage/mirage-crypto
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #96 from mirleft/picky-b64

base64 now rejects trailing garbage

authored by

David Kaloper and committed by
GitHub
06b62edb 59b78cd0

+58 -1
+4 -1
src/base64.ml
··· 71 71 | Not_found when x = padding && pad = 1 -> (0, 2) in 72 72 73 73 emit a b c d j ; 74 - match pad with 0 -> dec (j + 3) (i + 4) | _ -> pad 74 + match pad with 75 + | 0 -> dec (j + 3) (i + 4) 76 + | _ when i + 4 <> n -> raise Not_found 77 + | _ -> pad 75 78 in 76 79 try let pad = dec 0 0 in Some (sub cs' 0 (n' - pad)) 77 80 with Invalid_argument _ | Not_found -> None
+54
tests/testlib.ml
··· 142 142 assert_cs_equal ~msg:"invert" a a1 ; 143 143 assert_cs_equal ~msg:"commut" a1 a2 144 144 145 + let b64_selftest n = 146 + "selftest" >:: times ~n @@ fun _ -> 147 + let n = Rng.Int.gen 1024 in 148 + let x = Rng.generate n in 149 + 150 + let enc = Base64.encode x in 151 + match Base64.decode enc with 152 + | Some dec -> assert_cs_equal ~msg:"b64 mismatch" dec x 153 + | None -> assert_failure "couldn't decode b64" 154 + 145 155 146 156 let gen_rsa ~bits = 147 157 let e = Z.(if bits < 24 then ~$3 else ~$0x10001) in ··· 302 312 303 313 "00", "00 01 02", "00" ; 304 314 ] 315 + 316 + (* B64 *) 317 + 318 + let b64_enc_cases = 319 + cases_of (f1_eq ~msg:"b64" Base64.encode) [ 320 + "66 6f 6f", "5a 6d 39 76" ; 321 + "66 6f 6f 6f", "5a 6d 39 76 62 77 3d 3d" ; 322 + "66 6f 6f 6f 6f", "5a 6d 39 76 62 32 38 3d" ; 323 + "66 6f 6f 6f 6f 6f", "5a 6d 39 76 62 32 39 76" ; 324 + ] 325 + 326 + 327 + let f1_opt_eq ?msg f (a, b) _ = 328 + let maybe = function None -> None | Some h -> Some (Cs.of_hex h) in 329 + let (a, b) = Cs.of_hex a, maybe b in 330 + let eq_opt eq a b = match (a, b) with 331 + | (Some x, Some y) -> eq x y 332 + | (None , None ) -> true 333 + | _ -> false 334 + in 335 + assert_equal 336 + ~cmp:(eq_opt Cstruct.equal) 337 + ~printer:(show_opt hex_of_cs) 338 + ?msg (f a) b 339 + 340 + let b64_dec_cases = 341 + cases_of (f1_opt_eq ~msg:"b64" Base64.decode) [ 342 + "00 5a 6d 39 76", None ; 343 + "5a 6d 39 76", Some "66 6f 6f" ; 344 + "5a 6d 39 76 76", None ; 345 + "5a 6d 39 76 76 76", None ; 346 + "5a 6d 39 76 76 76 76", None ; 347 + "5a 6d 39 76 00", None ; 348 + "5a 6d 39 76 62 77 3d 3d", Some "66 6f 6f 6f" ; 349 + "5a 6d 39 76 62 77 3d 3d 00", None ; 350 + "5a 6d 39 76 62 77 3d 3d 00 01", None ; 351 + "5a 6d 39 76 62 77 3d 3d 00 01 02", None ; 352 + "5a 6d 39 76 62 77 3d 3d 00 01 02 03", None ; 353 + "5a 6d 39 76 62 32 38 3d", Some "66 6f 6f 6f 6f" ; 354 + "5a 6d 39 76 62 32 39 76", Some "66 6f 6f 6f 6f 6f" ; 355 + ] 356 + 305 357 306 358 let f1_blk_eq ?msg ?(n=1) f (x, y) _ = 307 359 let (x, y) = Cs.(of_hex x, of_hex y) in ··· 877 929 ] ; 878 930 879 931 "XOR" >::: [ xor_selftest 300 ; "example" >::: xor_cases ]; 932 + 933 + "B64" >::: [ b64_selftest 300 ; "encoding" >::: b64_enc_cases ; "decoding" >::: b64_dec_cases ]; 880 934 881 935 "MD5" >::: md5_cases ; 882 936