From f8a121dd87f47dff3af3f240c701f87a5f13beb1 Mon Sep 17 00:00:00 2001 From: Filippo Valsorda Date: Thu, 16 Jun 2022 15:55:29 +0200 Subject: [PATCH] tests: add more tests for hmac and X25519 encodings --- internal/testkit/testkit.go | 18 ++++++++++++ testdata/testkit/crlf | Bin 0 -> 416 bytes testdata/testkit/hmac_bad | Bin 355 -> 355 bytes testdata/testkit/hmac_extra_space | Bin 0 -> 356 bytes testdata/testkit/hmac_garbage | Bin 0 -> 358 bytes testdata/testkit/hmac_missing | Bin 0 -> 312 bytes testdata/testkit/hmac_no_space | Bin 0 -> 354 bytes testdata/testkit/hmac_not_canonical | Bin 0 -> 413 bytes testdata/testkit/hmac_trailing_space | Bin 0 -> 356 bytes testdata/testkit/hmac_truncated | Bin 0 -> 316 bytes testdata/testkit/x25519_extra_argument | Bin 0 -> 419 bytes testdata/testkit/x25519_low_order | Bin 457 -> 456 bytes testdata/testkit/x25519_not_canonical_body | Bin 0 -> 414 bytes testdata/testkit/x25519_not_canonical_share | Bin 0 -> 414 bytes tests/crlf.go | 29 ++++++++++++++++++++ tests/hmac_bad.go | 2 +- tests/hmac_extra_space.go | 24 ++++++++++++++++ tests/hmac_garbage.go | 20 ++++++++++++++ tests/hmac_missing.go | 19 +++++++++++++ tests/hmac_no_space.go | 24 ++++++++++++++++ tests/hmac_not_canonical.go | 21 ++++++++++++++ tests/hmac_trailing_space.go | 20 ++++++++++++++ tests/hmac_truncated.go | 20 ++++++++++++++ tests/x25519_extra_argument.go | 23 ++++++++++++++++ tests/x25519_low_order.go | 3 +- tests/x25519_not_canonical_body.go | 23 ++++++++++++++++ tests/x25519_not_canonical_share.go | 23 ++++++++++++++++ 27 files changed, 267 insertions(+), 2 deletions(-) create mode 100644 testdata/testkit/crlf create mode 100644 testdata/testkit/hmac_extra_space create mode 100644 testdata/testkit/hmac_garbage create mode 100644 testdata/testkit/hmac_missing create mode 100644 testdata/testkit/hmac_no_space create mode 100644 testdata/testkit/hmac_not_canonical create mode 100644 testdata/testkit/hmac_trailing_space create mode 100644 testdata/testkit/hmac_truncated create mode 100644 testdata/testkit/x25519_extra_argument create mode 100644 testdata/testkit/x25519_not_canonical_body create mode 100644 testdata/testkit/x25519_not_canonical_share create mode 100644 tests/crlf.go create mode 100644 tests/hmac_extra_space.go create mode 100644 tests/hmac_garbage.go create mode 100644 tests/hmac_missing.go create mode 100644 tests/hmac_no_space.go create mode 100644 tests/hmac_not_canonical.go create mode 100644 tests/hmac_trailing_space.go create mode 100644 tests/hmac_truncated.go create mode 100644 tests/x25519_extra_argument.go create mode 100644 tests/x25519_not_canonical_body.go create mode 100644 tests/x25519_not_canonical_share.go diff --git a/internal/testkit/testkit.go b/internal/testkit/testkit.go index fd37579..b2410ae 100644 --- a/internal/testkit/testkit.go +++ b/internal/testkit/testkit.go @@ -30,6 +30,16 @@ var _, TestX25519Identity, _ = bech32.Decode( var TestX25519Recipient, _ = curve25519.X25519(TestX25519Identity, curve25519.Basepoint) +func NotCanonicalBase64(s string) string { + // Assuming there are spare zero bits at the end of the encoded bitstring, + // the character immediately after in the alphabet compared to the last one + // in the encoding will only flip the last bit to one, making the string a + // non-canonical encoding of the same value. + alphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + idx := strings.IndexByte(alphabet, s[len(s)-1]) + return s[:len(s)-1] + string(alphabet[idx+1]) +} + type TestFile struct { Buf bytes.Buffer Rand func(n int) []byte @@ -64,6 +74,14 @@ func (f *TestFile) TextLine(s string) { f.Buf.WriteString("\n") } +func (f *TestFile) UnreadLine() string { + buf := bytes.TrimSuffix(f.Buf.Bytes(), []byte("\n")) + idx := bytes.LastIndex(buf[:len(buf)-1], []byte("\n")) + 1 + f.Buf.Reset() + f.Buf.Write(buf[:idx]) + return string(buf[idx:]) +} + func (f *TestFile) VersionLine(v string) { f.TextLine("age-encryption.org/" + v) } diff --git a/testdata/testkit/crlf b/testdata/testkit/crlf new file mode 100644 index 0000000000000000000000000000000000000000..69cafae0053238c658aeaa4fc06657af9e9a02c2 GIT binary patch literal 416 zcmXwzKX02r0L6DCcj+G8)*+#V;|_v{N-;i2%7Ont04I}yBV0o;#@Ljs`2-pJ6*^bS z5@k)NcIoG+sw`0zsVYnL#`pAlKlZ6&NizgFTc)fA*^(FAnh6;%7`SBJ5MUi)l=R84 z1|_Uuj37#pj3LqyLs;M`D;wVQ(<6(B9x>*`7j3c-Wn%effoTR%I#jS5E&>&55{(vt z4FlVapyQ0~aA8x&nxn(hqcB3!3{D|+Bq3Q{UG>-@D0s;>fR~`j*?&n^rl93b4vaZ9 z`)aJ?|s(ibuFW&9sfgrvI5yV*5fls(8b=A7wkE>m>j@-@& zK2%hjY};uXv{S0tGZLHQiIEJ1{NpmVqSm7`JyATBwvg|8(BHz0x3lBpO%ab=b%bP- zlKw*!0klu7SnHSqov=CGk|4aCD9%(}8)JGs+4*{FN@~H1k#{l>e*K6azu$gU-njYC br$4Xh9XPH5Ef`4ddHOk4iQi tN^o&nM{5dO9p>mR4h+51Bgl8pg*{={S@vt!dZ{)xAKIn?km6GL+e|QLA~OI0 diff --git a/testdata/testkit/hmac_extra_space b/testdata/testkit/hmac_extra_space new file mode 100644 index 0000000000000000000000000000000000000000..f3bfd1ec2c2f10e14f8bc39dd4942424157e32b3 GIT binary patch literal 356 zcmWN{y-vbF007{|Efyi<5hzV>-vog(-)$(|iMb)ju2`9V@5Rz1WiW5yN({)zy z(=A=QX4$s&lqkbZF$(NrL=?lqzSbGGDHos5{jOU~h^10_QX>UODibi>fG{bnUhi_@ zx6E1#N*ckOAP79~lzOUaRaG0#VkOdpn5}`+*J;sr eJ~ZR^ub+MLK+gWYpYFylpO1UDM+eu^Z}<;Gv~?W- literal 0 HcmV?d00001 diff --git a/testdata/testkit/hmac_garbage b/testdata/testkit/hmac_garbage new file mode 100644 index 0000000000000000000000000000000000000000..29e54ab697fcbf90ba368400b80f57cd019d741e GIT binary patch literal 358 zcmWN{y-tHb007{f!NuLMIaDkhccpYNp)JtDmHx_4o!~frv}q~QBC}86=qoszxENQR zbn!WiaWTfk*nI0puQE!C7MoB%q&XY;>0*;pCQ28Sy`WW#MKVN?>_kWcJQ5Lt0ESRN z0LqX;#H1lz7HP4oimq}~wKGv?oT+-8pz5=^t!XyknC$M>{I4exnxrQxO=g?2f@k~vlbtvl=xCOP*5-7TkhEE93sz$Hj)Q!v|rC@sgm-sK`_ zTa7jpG^{cl$FWXjWU^M5wc$LGVk1oG8aRD}l)cy^O{;GwVi=Z5POMl_6z1bCcz(UP f7Z0uE{p)8RKj5>!@29)T%je_X?a{%t@EiRDC*yU% literal 0 HcmV?d00001 diff --git a/testdata/testkit/hmac_missing b/testdata/testkit/hmac_missing new file mode 100644 index 0000000000000000000000000000000000000000..6ecea2aeb12154344d9665a2339fb5cb5cd53306 GIT binary patch literal 312 zcmWN{y-tHb007{f!NuLMIp`@IccpYN>CZtU2Q39D=)}*_o;EE-T4eSK9DN066BpyE zlP*4oF)qfK7@KeX*j3JwvL#Mgl(It1B0gIcOvw0*i5IMHiC9AzB^w!5p@bET5kx7H zF+>_-2n#%A^OBdF+BOJqNq0m%V3H8X#PCjiUH75XRIoh`{RUJe8ic+HebWx0W%bN) zXj018KU{OpC*RlYP!~kL}YobTEF4g(6*o*UZ zvJC9H>mTP-9jvNh>Q_UmnhuHe-k_TZ)AJ}cg36_imMCt6R*>(x&|ATbS5CiwIg2}X uql09f5&-}p3LkIr^XtvMvTIM@zkasq13mlue!3gId_Hd7?(ba7zu7;%?q*p4 literal 0 HcmV?d00001 diff --git a/testdata/testkit/hmac_no_space b/testdata/testkit/hmac_no_space new file mode 100644 index 0000000000000000000000000000000000000000..9f1d80d75142039c19cacb0cefd8d52bd9cd171c GIT binary patch literal 354 zcmWN{y-vbF007{|Efyi<5hzV>-vog(-)$(|iMb)ju2`9V@5Rz1WiW5yN({)zy z(=A=QX4$s&lqkbZF$(NrL=?lqzSbGGDHos5{jOU~h^10_QX>UODibi>fG{bnUhi_@ zx6E1#N*ckOAP7#Wr>a&}wc#vQB0Y%N8aREO7QM)$b+d2Bau5`8M$L%(c=MlMZ|>zo cGk*X2*(VR=?C<;OZv683xOaPWa4r3Y|B8cj-2eap literal 0 HcmV?d00001 diff --git a/testdata/testkit/hmac_not_canonical b/testdata/testkit/hmac_not_canonical new file mode 100644 index 0000000000000000000000000000000000000000..fd18cda65cf8b1e6cdace69b77ac1cbb86af2650 GIT binary patch literal 413 zcmW-ZJ#W)M06_N}SlH2R48_zw-^Fo;N@;2*jnB?!{ZS_SeD)orw&OT8p7{lg{R^Fu zSWwq8!NTuA6$`2=RYd4*Pfw43tT<~1Q1CeAHOykU+1DIoa>L;V-VGozF~RIWMhv79 zoe+$0iZz0;i8&@fro3omb4Z6{OIcZ?rL|TZD^fITyuS2p+ed0&CteWwJ!GgfjC=?A zju#@=J#&J{q3(Ez`xnC?#Oeag5p`9-%B!oxb^sf}VG{3n4?|wCGL=OR%k00NxWf^Y zJ6MzrWN}dzlEoVU;+!jowC<`#mc`q$&X2D(pq#)E5u%xJZAn}0s%?FgRM%`9dfmvM zR@9j7+j;7@b80vXE3waJBL>9#I2niSiY`p1uX?ma@@R$Ddz8s`F`4W($*I>n#hOhm zpePCqx@l$F$EH2I6h=Nx1>d6Je9GEM9(Ap$ Y`jx(W{(E=3{QmIm#m$>npR}j!AMNFc<^TWy literal 0 HcmV?d00001 diff --git a/testdata/testkit/hmac_trailing_space b/testdata/testkit/hmac_trailing_space new file mode 100644 index 0000000000000000000000000000000000000000..2e216bddae73ff7a6441fa2e95bba6d5feb958d4 GIT binary patch literal 356 zcmWN{y-vbF007{|Efyi<5hzV>-vog(-)$(|iMb)ju2`9V@5Rz1WiW5yN({)zy z(=A=QX4$s&lqkbZF$(NrL=?lqzSbGGDHos5{jOU~h^10_QX>UODibi>fG{bnUhi_@ zx6E1#N*ckOAPBru>Zz(#Rc$znl}Ha_wgygLr$sOFXx;4Fu^a?NoKZ94xsNyh`Ss>r eJ~ZR^ub+MLK+gWYpYFylpO1UDM+eu^Z}<;P_;nos literal 0 HcmV?d00001 diff --git a/testdata/testkit/hmac_truncated b/testdata/testkit/hmac_truncated new file mode 100644 index 0000000000000000000000000000000000000000..e7f719664c0c53563061b543ac17bb333520eb95 GIT binary patch literal 316 zcmWN{y-tHb007{f!NuLMIn+})?n>!k(jTBH2Q3Bp(TSgO13gALjg+|BZyKY zVu%z(5$0G*7bPpVwWF)RQF{~B1%?^|QPtg3Pt!aov?Of#q1S}6K!VUSpl4VCG|j%@ zhXyhA32L2m`~V3y97AFX92ZR~po^ra^O7wWhs$Dmv=KRQ%m)x-QQ=*cX+oW^ibkAo zl2u^UJ#Uy3d9<#^saK7OY}jh7^+&ygo1I6o9#jsol|*uyq=KyBKz9u@R@sBWt~ldkh8zUDwY+j1!czf=axw&-!pK-`UyZ5M34@Y$kY_1 zBLp>AKox1wf(#;w7ff`#8`9}BEQi<*@J3$ZR8|u=j%~#tsg5xU(oN!8?wm|tXj^1B zj-wl);->yAh{K3%iD9@ATqk;H&JBsyhr_U)fUaU7&rZyMfC*X`T4t zAnFd#Ow@wYY%fXKj>$vY_D#oY@uY5d*SD%9KLv?~kZOSq=5wWQj_oLKZs{@cdOLC& zVy?~?zlbhAF_&wc&u1%}0#(zYgb&!BV!HH3;yOB6VIK2n^!%)}BRmdFb5_J@UyOBe zF}IQ|%b;4X`w(vvGHzDq#jsV5$!l&HK~v2f-&!nP58IXJEc_ek``7%_m-~16W3T%5 c^Y=sYp1l0~>*L$t^N$Y?-dsJpQ~#9z05vs=L;wH) literal 0 HcmV?d00001 diff --git a/testdata/testkit/x25519_low_order b/testdata/testkit/x25519_low_order index 84e9fa3a5d4c8f444737179b738dd0baa0094970..f528a0d49b2d5f9269d775101a4a6b12bce5186c 100644 GIT binary patch delta 9 RcmX@fe1dtx?TL5Z0st3K1zi9D delta 11 TcmX@Xe3E&>ZAOKOx8DK)9oGeY diff --git a/testdata/testkit/x25519_not_canonical_body b/testdata/testkit/x25519_not_canonical_body new file mode 100644 index 0000000000000000000000000000000000000000..11138dbef72e8a926e631f0558f1765ad53028cd GIT binary patch literal 414 zcmW-ZJ#W)M06_N}Sh}Oz7+h0-+}Uvkq{z9nO6)YTQ-9Fud_E@^lCx_&P2IJ>pkx0+ zXCxNH3KJ~+4un{$3JDQaZ+m)r{HEfp?ZXwPIj>2nOA9&Y?*g+Vv0|mAX z4VxGv1La7^267Nb7>Jz9RWS8Szc-qf`fFC_>MpF2Jejv zY>mfb(+V{|oehIHjJylaviu0m6En0&7GULed)W42yW%jT4Yv^Fl9joTOIQ{!x>?bh zL(xE4wvbU-O2Oy`0J`Mrp{={B6{UPz*2|M?9jNbMVqmO0a6!aK>#D0-$*OC1l}tM~ z8dsiuzT408Xg~Ap$(&@P;knBI*%Eg^*kr26lc;gREEbV+w$o~b^a9%+=5gBPJyY)O z2&k$GdAMG?>v>n1T6Z>_?cLjU5-NIo(fZ_P=iI0NwONA~sm{YUf7boKMk a^O5(-yZra`{R8qHG>a@M2%}1G%*l7%{ou=uPu<{ES`3uZQ zEQs}PIu?EhLM#X&Au9B?r>DnW*Q{su6aLPo>+jBacU^T>UfS2kNr>dU#aqMaqNllDVkS-J7@yBGiHde^; zJkPX3%}*DjAPyt4B9`Sx@H#O=du%C%-0jZW0ca}*^6bDY2$(1&XJyrmI^n#f5A(mp8hiJ_U(^knVsb=3}j^_f0RaZ;E{~>)gnz zi9J7_7Bo67h&{c=`FJ#Use*Ud9pYj->xs+g;DmY1qu%qQ*7R^cu