Exemple #1
0
 def _nonce_and_mutated_key(self, key):
     #
     # Note: This nonce is NOT generated using strong randomness.
     #       That is not the point and should not matter.
     #
     nonce = genkey(str(random.getrandbits(512)))[:32].strip()
     return nonce, genkey(key, nonce)[:32].strip()
Exemple #2
0
 def _nonce_and_mutated_key(self, key):
     #
     # Note: This nonce is NOT generated using strong randomness.
     #       That is not the point and should not matter.
     #
     nonce = genkey(str(random.getrandbits(512)))[:32].strip()
     return nonce, genkey(key, nonce)[:32].strip()
Exemple #3
0
 def encrypt(self, data, cipher=None):
     if not cipher:
         cipher = self.DEFAULT_CIPHER
     nonce = genkey(str(random.getrandbits(512)))[:32].strip()
     enckey = genkey(self.secret, nonce)[:32].strip()
     params = ["enc", "-e", "-a", "-%s" % cipher, "-pass", "stdin"]
     retval, res = self.run(params, output=data, passphrase=enckey)
     ret = "%s\ncipher: %s\nnonce: %s\n\n%s\n%s" % (self.BEGIN_DATA, cipher, nonce, res["stdout"], self.END_DATA)
     return ret
Exemple #4
0
 def encrypt(self, data, cipher=None):
     if not cipher:
         cipher = self.DEFAULT_CIPHER
     nonce = genkey(str(random.getrandbits(512)))[:32].strip()
     enckey = genkey(self.secret, nonce)[:32].strip()
     params = ["enc", "-e", "-a", "-%s" % cipher, "-pass", "stdin"]
     retval, res = self.run(params, output=data, passphrase=enckey)
     ret = "%s\ncipher: %s\nnonce: %s\n\n%s\n%s" % (
         self.BEGIN_DATA, cipher, nonce, res["stdout"], self.END_DATA)
     return ret
    def decrypt(self, data):
        try:
            head, enc, tail = data.split("\n\n")
            head = [h.strip() for h in head.split("\n")]
        except:
            try:
                head, enc, tail = data.split("\r\n\r\n")
                head = [h.strip() for h in head.split("\r\n")]
            except:
                raise ValueError("Not a valid OpenSSL encrypted block.")

        if (not head or not enc or not tail
                or head[0] != self.BEGIN_DATA
                or tail.strip() != self.END_DATA):
            raise ValueError("Not a valid OpenSSL encrypted block.")

        try:
            headers = dict([l.split(': ', 1) for l in head[1:]])
        except:
            raise ValueError("Message contained invalid parameter.")

        cipher = headers.get('cipher', self.DEFAULT_CIPHER)
        nonce = headers.get('nonce')
        if not nonce:
            raise ValueError("Encryption nonce not known.")

        enckey = genkey(self.secret, nonce)[:32].strip()
        params = ["enc", "-d", "-a", "-%s" % cipher, "-pass", "stdin"]
        retval, res = self.run(params, output=enc, passphrase=enckey)
        return res["stdout"]
    def decrypt(self, data):
        try:
            head, enc, tail = data.split("\n\n")
            head = [h.strip() for h in head.split("\n")]
        except:
            try:
                head, enc, tail = data.split("\r\n\r\n")
                head = [h.strip() for h in head.split("\r\n")]
            except:
                raise ValueError("Not a valid OpenSSL encrypted block.")

        if (not head or not enc or not tail
                or head[0] != self.BEGIN_DATA
                or tail.strip() != self.END_DATA):
            raise ValueError("Not a valid OpenSSL encrypted block.")

        try:
            headers = dict([l.split(': ', 1) for l in head[1:]])
        except:
            raise ValueError("Message contained invalid parameter.")

        cipher = headers.get('cipher', self.DEFAULT_CIPHER)
        nonce = headers.get('nonce')
        if not nonce:
            raise ValueError("Encryption nonce not known.")

        enckey = genkey(self.secret, nonce)[:32].strip()
        params = ["enc", "-d", "-a", "-%s" % cipher, "-pass", "stdin"]
        retval, res = self.run(params, output=enc, passphrase=enckey)
        return res["stdout"]
Exemple #7
0
 def _nonce_and_mutated_key(self, key):
     # This generates a nonce which may be used as a salt, IV, or
     # counter-prefix depending the algorithm and mode in use. We
     # also use it to derive a mutated key for each message, thus
     # reducing the risks of the (key, iv) pairs ever repeating even
     # if a mistake is made somewhere else.
     nonce = '%32.32x' % getrandbits(32 * 4)
     return nonce, genkey(key, nonce)[:32].strip()
Exemple #8
0
 def _nonce_and_mutated_key(self, key):
     # This generates a nonce which may be used as a salt, IV, or
     # counter-prefix depending the algorithm and mode in use. We
     # also use it to derive a mutated key for each message, thus
     # reducing the risks of the (key, iv) pairs ever repeating even
     # if a mistake is made somewhere else.
     nonce = '%32.32x' % getrandbits(32 * 4)
     return nonce, genkey(key, nonce)[:32].strip()
Exemple #9
0
 def _mutate_key(self, key, nonce):
     return genkey(key or '', nonce)[:32].strip()
Exemple #10
0
        ct1 = aes_ctr_encryptor(bogus_key, bogus_nonce)(hello)
        results.append((name, base64.b64encode(ct1)))

        ct2 = aes_ctr_encrypt(bogus_key, bogus_nonce, hello)
        results.append((name, base64.b64encode(ct2)))

        assert (aes_ctr_decrypt(bogus_key, bogus_nonce, ct1) ==
                aes_ctr_decryptor(bogus_key, bogus_nonce)(ct1) == hello)

    # Make sure all the results are the same
    okay = True
    r1 = results[0]
    for result in results[1:]:
        if r1[1] != result[1]:
            print '%s != %s' % (r1, result)
            okay = False
    assert (okay)

    # This verifies we can decrypt some snippets of data that were
    # generated with a previous iteration of mailpile.crypto.streamer
    from mailpile.util import sha512b64 as genkey
    legacy_data = "part two, yeaaaah\n"
    legacy_nonce = "2c1c43936034cae20eef86d961cb6570"
    legacy_key = genkey("test key", legacy_nonce)[:32].strip()
    legacy_ct = base64.b64decode("D+lBOPrtV+amUCAtoFPCzxsZ")
    decrypted = aes_ctr_decrypt(legacy_key, legacy_nonce, legacy_ct)
    assert (legacy_data == decrypted)

    print "ok"
Exemple #11
0
 def _mutate_key(self, key, nonce):
     return genkey(key or '', nonce)[:32].strip()
Exemple #12
0
 def _mutate_key(self, key):
     nonce = genkey(str(random.getrandbits(512)))[:32].strip()
     return nonce, genkey(key, nonce)[:32].strip()
Exemple #13
0
        results.append((name, base64.b64encode(ct1)))

        ct2 = aes_ctr_encrypt(bogus_key, bogus_nonce, hello)
        results.append((name, base64.b64encode(ct2)))

        assert(aes_ctr_decrypt(bogus_key, bogus_nonce, ct1) ==
               aes_ctr_decryptor(bogus_key, bogus_nonce)(ct1) ==
               hello)


    # Make sure all the results are the same
    okay = True
    r1 = results[0]
    for result in results[1:]:
        if r1[1] != result[1]:
            print '%s != %s' % (r1, result)
            okay = False
    assert(okay)

    # This verifies we can decrypt some snippets of data that were
    # generated with a previous iteration of mailpile.crypto.streamer
    from mailpile.util import sha512b64 as genkey
    legacy_data = "part two, yeaaaah\n"
    legacy_nonce = "2c1c43936034cae20eef86d961cb6570"
    legacy_key = genkey("test key", legacy_nonce)[:32].strip()
    legacy_ct = base64.b64decode("D+lBOPrtV+amUCAtoFPCzxsZ")
    decrypted = aes_ctr_decrypt(legacy_key, legacy_nonce, legacy_ct)
    assert(legacy_data == decrypted)

    print "ok"