{"id":35533321,"url":"https://github.com/yago-123/chacha20-cipher","last_synced_at":"2026-01-12T03:02:34.936Z","repository":{"id":213340914,"uuid":"286585466","full_name":"yago-123/chacha20-cipher","owner":"yago-123","description":"ChaCha-20 cipher by Daniel J. Bernstein, proof of concept written in Golang","archived":false,"fork":false,"pushed_at":"2020-08-22T02:23:05.000Z","size":169,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-12-20T14:44:32.601Z","etag":null,"topics":["bernstein","chacha-cipher","xoring"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yago-123.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-08-10T21:49:33.000Z","updated_at":"2023-12-20T14:44:34.524Z","dependencies_parsed_at":"2023-12-20T14:54:49.074Z","dependency_job_id":null,"html_url":"https://github.com/yago-123/chacha20-cipher","commit_stats":null,"previous_names":["yago-123/chacha20-cipher"],"tags_count":0,"template":null,"template_full_name":null,"purl":"pkg:github/yago-123/chacha20-cipher","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yago-123%2Fchacha20-cipher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yago-123%2Fchacha20-cipher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yago-123%2Fchacha20-cipher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yago-123%2Fchacha20-cipher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yago-123","download_url":"https://codeload.github.com/yago-123/chacha20-cipher/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yago-123%2Fchacha20-cipher/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28332845,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T00:36:25.062Z","status":"online","status_checked_at":"2026-01-12T02:00:08.677Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bernstein","chacha-cipher","xoring"],"created_at":"2026-01-04T02:57:38.012Z","updated_at":"2026-01-12T03:02:34.926Z","avatar_url":"https://github.com/yago-123.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ChaCha cipher  \nThis repo contains the implementation of the chacha cipher created by Daniel J. Bernstein, actually used in the new TLS protocol ([`chacha20 and Poly1305`](https://tools.ietf.org/html/rfc7905)) adopted by Google and OpenSSH in order to replace RC4. \n## Example \nInside the `cmd` dir can use the ready to run `file_cipher` script: \n```sh \n$ go run file_cipher.go pg-frankenstein.txt\nInsert key (32 bytes): \naW7TjxS6myuDGTSrrRJBUFzv7VKFCFsw\nInset nonce (8 bytes): \nu2YA9JNn\nTime taken 0.01 seconds, file length 0.42 MB\nAverage time 42.61 MB/s \n```\nThe result file will be avaible with the same name with the termination `.chacha`. In order to decrypt can use the ciphered file with the old parameters used: \n```sh\n$ go run file_cipher.go pg-frankenstein.txt.chacha\nInsert key (32 bytes): \naW7TjxS6myuDGTSrrRJBUFzv7VKFCFsw\nInset nonce (8 bytes): \nu2YA9JNn\nTime taken 0.01 seconds, file length 0.42 MB\nAverage time 33.80 MB/s \n```\nAnd now check the signatures: \n```\n$ b2sum pg-frankenstein.txt pg-frankenstein.txt.chacha pg-frankenstein.txt.chacha.chacha\n89a13cef3d1e6df172faf184d670de05a57f3c8e904633271472b24c7502a9c84d7547de9eae34172b898b02b877189e40760419e17c8b43b7c56ca647a96802  pg-frankenstein.txt\n9805a6ec1e4d6af3fd8c144c40612874680ebb84064ef9ce44b5a9984f08624d2e3d1d8dc7bd765b68765ae9e60d234684757f4aa816c4a0167f0445b99ae1da  pg-frankenstein.txt.chacha\n89a13cef3d1e6df172faf184d670de05a57f3c8e904633271472b24c7502a9c84d7547de9eae34172b898b02b877189e40760419e17c8b43b7c56ca647a96802  pg-frankenstein.txt.chacha.chacha\n```\nAs you can see the check sum of the first and the last will be the same. \n## Design   \nThis encryption algorithm belongs to the symmetric key ciphers, the operations that performs are known as ARX (Addition Rotation Xoring). It works xoring plain text with a block of data provided by the user. This explanation will show the basics, for more accurate data check the official [paper](https://cr.yp.to/chacha/chacha-20080128.pdf). \n\nWe can split the explanation in diferent parts: \n- [Chacha Block](#chacha-block) \n- [Quarter Round](#quarter-round)\n- [Complete Round](#complete-round) \n- [Xoring](#xoring) \n\n### Chacha Block \nThis block it's one of the keyparts of the operation, it consists in a matrix of 16 integer values (64 characters) and it's formed by 4 different fields: \n- Constant value (4): `\"expand 32 byte k\"`: \n- Key (8): provided by the user  \n- Block Counter (2): numeration of each block  \n- Nonce (2): provided by the user \n\nThe length of each can change modifying the size of the others (see that in the paper uses 1 counter and 3 nonces). The constant prevents zero blocks and prevents the attacker surface over the block, it can be found in the code as follows: \n```go\nblock_t.chachaConst = [4]uint32{ // expand 32 byte k\n    1634760805,   // expa\n    857760878,    // nd 3\n    2036477234,   // 2 by\n    1797285236,   // te k\n}\n```\n\n\nThe set of values is stored in a matrix as follows inside the `block_t` structure: \n```\n       cccccccc  cccccccc  cccccccc  cccccccc\n       kkkkkkkk  kkkkkkkk  kkkkkkkk  kkkkkkkk\n       kkkkkkkk  kkkkkkkk  kkkkkkkk  kkkkkkkk\n       bbbbbbbb  bbbbbbbb  nnnnnnnn  nnnnnnnn\n```\nAs you can see the block is represented in bytes, but in reality this block is used with int32 values, encoded in little endian.  \n### Quarter Round \nOne time we have the block, we are going to perform operations over it, the quarter round performs the ARX operations: \n```c\na += b; d ^= a; d \u003c\u003c\u003c= 16;\nc += d; b ^= c; b \u003c\u003c\u003c= 12;\na += b; d ^= a; d \u003c\u003c\u003c= 8;\nc += d; b ^= c; b \u003c\u003c\u003c= 7;\n```\nWhere a, b, c, d correspond to the position of the value in the block, this will change in order to make the complete round, in the package we can see the signature: \n```go\nfunc quarterround(a, b, c, d uint32, input []uint32)\n```\n### Complete Round \nAs the name says, quarter round is only 1/4 of the operation, in order to perform a full round (of the 20 rounds) we will made 4 quarter round. First column by column: \n```go\nquarterround(0, 4, 8, 12, input)\nquarterround(1, 5, 9, 13, input)\nquarterround(2, 6, 10, 14, input)\nquarterround(3, 7, 11, 15, input)\n```\nThen by the diagonals:\n```go\nquarterround(0, 5, 10, 15, input)\nquarterround(1, 6, 11, 12, input)\nquarterround(2, 7, 8, 13, input)\nquarterround(3, 4, 9, 14, input)\n```\nFor get the 20 rounds we will perform 10 sequential loops.\n### Xoring \nOne time the block has been treated, we will cipher the plain text with the block with a simple XOR operation: \n```go\nfor k := 0; k \u003c 16; k++ {\n\tbufCipher[k] = XOR(bufPlain[k], streamBlock[k])\n}\n```\nWhere `bufPlain` is the text that has to be converted (represented in uint32 too) and `streamBlock` is the block created/treated build from the constant, counter block and the key/nonce provided by the user. In order to perform the decipher operation we will perform the same operation of the cipher because of the symmetric nature of the algorithm.  \n## Number of rounds\nThe number of rounds aren't a random number, the chacha design is contempled to use rounds of 8, 12 or 20 being the last the slowest, in the actuality, all of them are a secure option, can change the rounds of the package with the constant `NUMBER_ROUNDS`","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyago-123%2Fchacha20-cipher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyago-123%2Fchacha20-cipher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyago-123%2Fchacha20-cipher/lists"}