smp_msg3_test.go 7.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
package otr3

import (
	"math/big"
	"testing"
)

func Test_generateSMP3_generatesLongerValuesForR4WithProtocolV3(t *testing.T) {
	otr := newConversation(otrV3{}, fixtureRand())
	smp, err := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.r4, fixtureLong1)
	assertDeepEquals(t, err, nil)
}

func Test_generateSMP3_generatesLongerValuesForR5WithProtocolV3(t *testing.T) {
	otr := newConversation(otrV3{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.r5, fixtureLong2)
}

func Test_generateSMP3_generatesLongerValuesForR6WithProtocolV3(t *testing.T) {
	otr := newConversation(otrV3{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.r6, fixtureLong3)
}

func Test_generateSMP3_generatesLongerValuesForR7WithProtocolV3(t *testing.T) {
	otr := newConversation(otrV3{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.r7, fixtureLong4)
}

func Test_generateSMP3_generatesShorterValuesForR4WithProtocolV2(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.r4, fixtureShort1)
}

func Test_generateSMP3_computesPaCorrectly(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.msg.pa, fixtureMessage3().pa)
}

func Test_generateSMP3_computesQaCorrectly(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.msg.qa, fixtureMessage3().qa)
}

func Test_generateSMP3_computesPaPbCorrectly(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.papb, fixtureSmp3().papb)
}

func Test_generateSMP3_computesQaQbCorrectly(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.qaqb, fixtureSmp3().qaqb)
}

func Test_generateSMP3_storesG3b(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.g3b, fixtureMessage2().g3b)
}

func Test_generateSMP3_computesCPCorrectly(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.msg.cp, fixtureMessage3().cp)
}

func Test_generateSMP3_computesD5Correctly(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.msg.d5, fixtureMessage3().d5)
}

func Test_generateSMP3_computesD6Correctly(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.msg.d6, fixtureMessage3().d6)
}

func Test_generateSMP3_computesRaCorrectly(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.msg.ra, fixtureMessage3().ra)
}

func Test_generateSMP3_computesCrCorrectly(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.msg.cr, fixtureMessage3().cr)
}

func Test_generateSMP3_computesD7Correctly(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	smp, _ := otr.generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, smp.msg.d7, fixtureMessage3().d7)
}

func Test_generateSMP3Parameters_returnsAnErrorIfThereIsntRandomnessToGenerate_r4(t *testing.T) {
	_, err := newConversation(otrV2{}, fixedRand([]string{
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b",
	})).generateSMP3Parameters()
	assertDeepEquals(t, err, errShortRandomRead)
}

func Test_generateSMP3Parameters_returnsAnErrorIfThereIsntRandomnessToGenerate_r5(t *testing.T) {
	_, err := newConversation(otrV2{}, fixedRand([]string{
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b",
	})).generateSMP3Parameters()
	assertDeepEquals(t, err, errShortRandomRead)
}

func Test_generateSMP3Parameters_returnsAnErrorIfThereIsntRandomnessToGenerate_r6(t *testing.T) {
	_, err := newConversation(otrV2{}, fixedRand([]string{
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b",
	})).generateSMP3Parameters()
	assertDeepEquals(t, err, errShortRandomRead)
}

func Test_generateSMP3Parameters_returnsAnErrorIfThereIsntRandomnessToGenerate_r7(t *testing.T) {
	_, err := newConversation(otrV2{}, fixedRand([]string{
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b",
	})).generateSMP3Parameters()
	assertDeepEquals(t, err, errShortRandomRead)
}

func Test_generateSMP3Parameters_returnsOKIfThereIsEnoughRandomnessToGenerateBlindingFactors(t *testing.T) {
	_, err := newConversation(otrV2{}, fixedRand([]string{
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
	})).generateSMP3Parameters()
	assertDeepEquals(t, err, nil)
}

func Test_generateSMP3_returnsAnErrorIfThereIsNotEnoughRandomnessForBlinding(t *testing.T) {
	_, err := newConversation(otrV2{}, fixedRand([]string{
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b8b",
		"1a2a3a4a5a6a7a8a1b2b3b4b5b6b7b",
	})).generateSMP3(fixtureSecret(), *fixtureSmp1(), fixtureMessage2())
	assertDeepEquals(t, err, errShortRandomRead)
}

func Test_verifySMP3_failsIfPaIsNotInTheGroupForProtocolV3(t *testing.T) {
	otr := newConversation(otrV3{}, fixtureRand())
	err := otr.verifySMP3(fixtureSmp2(), smp3Message{pa: big.NewInt(1)})
	assertDeepEquals(t, err, newOtrError("Pa is an invalid group element"))
}

func Test_verifySMP3_failsIfQaIsNotInTheGroupForProtocolV3(t *testing.T) {
	otr := newConversation(otrV3{}, fixtureRand())
	err := otr.verifySMP3(fixtureSmp2(), smp3Message{
		pa: big.NewInt(2),
		qa: big.NewInt(1),
	})
	assertDeepEquals(t, err, newOtrError("Qa is an invalid group element"))
}

func Test_verifySMP3_failsIfRaIsNotInTheGroupForProtocolV3(t *testing.T) {
	otr := newConversation(otrV3{}, fixtureRand())
	err := otr.verifySMP3(fixtureSmp2(), smp3Message{
		pa: big.NewInt(2),
		qa: big.NewInt(2),
		ra: big.NewInt(1),
	})
	assertDeepEquals(t, err, newOtrError("Ra is an invalid group element"))
}

func Test_verifySMP3_succeedsForValidZKPS(t *testing.T) {
	otr := newConversation(otrV3{}, fixtureRand())
	err := otr.verifySMP3(fixtureSmp2(), fixtureMessage3())
	assertDeepEquals(t, err, nil)
}

func Test_verifySMP3_failsIfCpIsNotAValidZKP(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	m := fixtureMessage3()
	m.cp = sub(m.cp, big.NewInt(1))
	err := otr.verifySMP3(fixtureSmp2(), m)
	assertDeepEquals(t, err, newOtrError("cP is not a valid zero knowledge proof"))
}

func Test_verifySMP3_failsIfCrIsNotAValidZKP(t *testing.T) {
	otr := newConversation(otrV2{}, fixtureRand())
	m := fixtureMessage3()
	m.cr = sub(m.cr, big.NewInt(1))
	err := otr.verifySMP3(fixtureSmp2(), m)
	assertDeepEquals(t, err, newOtrError("cR is not a valid zero knowledge proof"))
}